libept
apt.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 #ifndef EPT_APT_APT_H
00003 #define EPT_APT_APT_H
00004 
00009 /*
00010  * Copyright (C) 2007,2008  Enrico Zini <enrico@enricozini.org>
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2.1 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser General Public
00023  * License along with this library; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00025  */
00026 
00027 #include <wibble/exception.h>
00028 #include <ept/apt/version.h>
00029 
00030 #include <iterator>
00031 
00032 namespace ept {
00033 namespace apt {
00034 
00035 class Exception : public wibble::exception::Generic
00036 {
00037 protected:
00038     std::string m_message;
00039 
00040 public:
00041     Exception(const std::string& context) throw ();
00042     ~Exception() throw () {}
00043 
00044     virtual const char* type() const throw () { return "Apt"; }
00045     virtual std::string desc() const throw () { return m_message; }
00046 };
00047 
00048 class Apt;
00049 class AptImplementation;
00050 class RecordIteratorImpl;
00051 
00052 struct PackageState {
00053     enum Query {
00054         Install = 1 << 0,
00055         Upgrade = 1 << 1,
00056         Keep = 1 << 2,
00057         Remove = 1 << 3,
00058         Installed = 1 << 4,
00059         Upgradable = 1 << 5,
00060         NowBroken = 1 << 6,
00061         WillBreak = 1 << 7,
00062         ReInstall = 1 << 8,
00063         Purge = 1 << 9,
00064         Hold = 1 << 10,
00065         Valid = 1 << 11
00066     };
00067 
00068     typedef unsigned state;
00069     
00070     operator unsigned() { return m_state; };
00071     
00072     PackageState &operator=( unsigned i ) {
00073         m_state = i;
00074         return *this;
00075     }
00076     
00077     PackageState &operator|=( const PackageState &s ) {
00078         m_state |= s.m_state;
00079         return *this;
00080     }
00081     
00082     PackageState( unsigned a ) {
00083         m_state = a;
00084     }
00085     
00086     PackageState() : m_state( 0 ) {}
00087 
00088     // FIXME this probably needs to be used consistently in core and out of core
00089     bool isValid() const { return m_state & Valid; }
00090     // FIXME compatibility API for non-core apt
00091     bool isInstalled() const { return installed(); }
00092 
00093     bool install() const { return m_state & Install; }
00094     // reinstall() implies install()
00095     bool reinstall() const { return m_state & ReInstall; }
00096     bool remove() const { return m_state & Remove; }
00097     // purge() implies remove()
00098     bool purge() const { return m_state & Purge; }
00099     bool keep() const { return m_state & Keep; }
00100     bool willBreak() const { return m_state & WillBreak; }
00101     // upgrade() implies install()
00102     bool upgrade() const { return hasNewVersion() && install(); }
00103     // newInsstal() implies install()
00104     bool newInstall() const { return !installed() && install(); }
00105     bool hold() const { return m_state & Hold; }
00106 
00107     bool installed() const { return m_state & Installed; }
00108     bool hasNewVersion() const { return m_state & Upgradable; }
00109     bool upgradable() const { return hasNewVersion() && !hold(); }
00110     bool held() const { return hasNewVersion() && hold(); }
00111     bool nowBroken() const { return m_state & NowBroken; }
00112 
00113     bool modify() const { return install() || remove(); }
00114     
00115 protected:
00116     unsigned m_state;
00117 };
00118 
00125 class Apt
00126 {
00127 protected:
00128     AptImplementation* impl;
00129 
00130 public:
00131     // Iterate Packages in the Apt cache
00132     class Iterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
00133     {
00134         void* cur;
00135 
00136     protected:
00137         // Construct a valid iterator
00138         Iterator(void* cur) : cur(cur) {}
00139 
00140         // Construct and end iterator
00141         Iterator() : cur(0) {}
00142 
00143     public:
00144         // Copy constructor
00145         Iterator(const Iterator&);
00146         ~Iterator();
00147         std::string operator*();
00148         Iterator& operator++();
00149         Iterator& operator=(const Iterator&);
00150         bool operator==(const Iterator&) const;
00151         bool operator!=(const Iterator&) const;
00152 
00153         // FIXME: Iterator operator++(int); cannot be easily implemented
00154         // because of how Apt's pkgIterator works
00155 
00156         friend class Apt;
00157     };
00158 
00159     // Iterate Package records in the Apt cache
00160     class RecordIterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
00161     {
00162         RecordIteratorImpl* impl;
00163         size_t pos;
00164         std::string cur;
00165         size_t cur_pos;
00166 
00167     protected:
00168         // Construct a valid iterator
00169         RecordIterator(RecordIteratorImpl* cur, size_t pos = 0);
00170 
00171         // Construct and end iterator
00172         RecordIterator() : impl(0), pos(0), cur_pos(0) {}
00173 
00174     public:
00175         // Copy constructor
00176         RecordIterator(const RecordIterator& r);
00177 
00178         ~RecordIterator();
00179         std::string operator*();
00180         std::string* operator->();
00181         RecordIterator& operator++();
00182         RecordIterator& operator=(const RecordIterator& r);
00183         bool operator==(const RecordIterator&) const;
00184         bool operator!=(const RecordIterator&) const;
00185 
00186         // FIXME: Iterator operator++(int); cannot be easily implemented
00187         // because of how Apt's pkgIterator works
00188 
00189         friend class Apt;
00190     };
00191 
00192     typedef Iterator iterator;
00193     typedef RecordIterator record_iterator;
00194 
00198     Apt();
00199     ~Apt();
00200 
00201     iterator begin() const;
00202     iterator end() const;
00203 
00204     record_iterator recordBegin() const;
00205     record_iterator recordEnd() const;
00206 
00207 
00209     size_t size() const;
00210 
00215     bool isValid(const std::string& pkg) const;
00216 
00219     std::string validate(const std::string& pkg) const
00220     {
00221         if (isValid(pkg))
00222             return pkg;
00223         return std::string();
00224     }
00225 
00228     Version validate(const Version& ver) const;
00229 
00231     Version installedVersion(const std::string& pkg) const;
00232 
00234     Version candidateVersion(const std::string& pkg) const;
00235 
00240     Version anyVersion(const std::string& pkg) const;
00241 
00243     PackageState state(const std::string& pkg) const;
00244 
00251     //template<typename FILTER, typename OUT>
00252     //void search(const FILTER& filter, OUT& out);
00253 
00255     std::string rawRecord(const std::string& pkg) const;
00256 
00258     std::string rawRecord(const Version& ver) const;
00259 
00261     time_t timestamp();
00262 
00269     void checkCacheUpdates();
00270 
00277     void invalidateTimestamp();
00278 };
00279 
00280 }
00281 }
00282 
00283 // vim:set ts=4 sw=4:
00284 #endif