apt.h

Go to the documentation of this file.
00001 #ifndef EPT_APT_APT_H
00002 #define EPT_APT_APT_H
00003 
00008 /*
00009  * Copyright (C) 2007  Enrico Zini <enrico@enricozini.org>
00010  *
00011  * This library is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2.1 of the License, or (at your option) any later version.
00015  *
00016  * This library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this library; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00024  */
00025 
00026 #include <wibble/exception.h>
00027 #include <ept/apt/version.h>
00028 
00029 #include <iterator>
00030 
00031 namespace ept {
00032 namespace apt {
00033 
00034 class Exception : public wibble::exception::Generic
00035 {
00036 protected:
00037     std::string m_message;
00038 
00039 public:
00040     Exception(const std::string& context) throw ();
00041     ~Exception() throw () {}
00042 
00043     virtual const char* type() const throw () { return "Apt"; }
00044     virtual std::string desc() const throw () { return m_message; }
00045 };
00046 
00047 class Apt;
00048 class AptImplementation;
00049 class RecordIteratorImpl;
00050 
00051 class PackageState
00052 {
00053 protected:
00054     enum Query {
00055         Install = 1 << 0,
00056         Upgrade = 1 << 1,
00057         Keep = 1 << 2,
00058         Remove = 1 << 3,
00059         Installed = 1 << 4,
00060         Upgradable = 1 << 5,
00061         NowBroken = 1 << 6,
00062         WillBreak = 1 << 7,
00063         ReInstall = 1 << 8,
00064         Purge = 1 << 9,
00065         Valid = 1 << 10
00066     };
00067 
00068     unsigned int m_flags;
00069 
00070 public:
00071     PackageState() : m_flags(0) {}
00072     PackageState(unsigned int flags) : m_flags(flags) {}
00073 
00074     bool isValid() const { return m_flags & Valid; }
00075 
00076     bool markedInstall() const { return m_flags & Install || markedReInstall(); }
00077     bool markedUpgrade() const { return markedInstall() && isUpgradable(); }
00078     bool markedNewInstall() const { return markedInstall() && !isUpgradable() && !markedReInstall(); }
00079     bool markedReInstall() const { return m_flags & ReInstall; }
00080     bool markedKeep() const { return m_flags & Keep; }
00081     bool markedRemove() const { return m_flags & Remove; }
00082     bool markedPurge() const { return m_flags & Purge; }
00083 
00084     bool isInstalled() const { return m_flags & Installed; }
00085     bool isUpgradable() const { return m_flags & Upgradable; }
00086     bool isBroken() const { return m_flags & NowBroken; }
00087     bool willBreak() const { return m_flags & WillBreak; }
00088 
00089     bool canInstall() const
00090     {
00091         return ((! isInstalled()) && ! markedInstall()) || markedRemove() || isBroken();
00092     }
00093     bool canRemove() const { return isInstalled() && not markedRemove(); }
00094     bool canKeep() const
00095     {
00096         return markedUpgrade() || markedRemove() || markedInstall();
00097     }
00098     bool canUpgrade() const { return isUpgradable() && ! markedUpgrade(); }
00099     bool canReInstall() const { return isInstalled() && !isUpgradable() && !markedReInstall(); }
00100     bool canRevertInstall() const { return markedNewInstall(); }
00101 
00102     friend class Apt;
00103 };
00104 
00111 class Apt
00112 {
00113 protected:
00114     AptImplementation* impl;
00115 
00116 public:
00117     // Iterate Packages in the Apt cache
00118     class Iterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
00119     {
00120         void* cur;
00121 
00122     protected:
00123         // Construct a valid iterator
00124         Iterator(void* cur) : cur(cur) {}
00125 
00126         // Construct and end iterator
00127         Iterator() : cur(0) {}
00128 
00129     public:
00130         // Copy constructor
00131         Iterator(const Iterator&);
00132         ~Iterator();
00133         std::string operator*();
00134         Iterator& operator++();
00135         Iterator& operator=(const Iterator&);
00136         bool operator==(const Iterator&) const;
00137         bool operator!=(const Iterator&) const;
00138 
00139         // FIXME: Iterator operator++(int); cannot be easily implemented
00140         // because of how Apt's pkgIterator works
00141 
00142         friend class Apt;
00143     };
00144 
00145     // Iterate Package records in the Apt cache
00146     class RecordIterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
00147     {
00148         RecordIteratorImpl* impl;
00149         size_t pos;
00150         std::string cur;
00151         size_t cur_pos;
00152 
00153     protected:
00154         // Construct a valid iterator
00155         RecordIterator(RecordIteratorImpl* cur, size_t pos = 0);
00156 
00157         // Construct and end iterator
00158         RecordIterator() : impl(0), pos(0), cur_pos(0) {}
00159 
00160     public:
00161         // Copy constructor
00162         RecordIterator(const RecordIterator& r);
00163 
00164         ~RecordIterator();
00165         std::string operator*();
00166         std::string* operator->();
00167         RecordIterator& operator++();
00168         RecordIterator& operator=(const RecordIterator& r);
00169         bool operator==(const RecordIterator&) const;
00170         bool operator!=(const RecordIterator&) const;
00171 
00172         // FIXME: Iterator operator++(int); cannot be easily implemented
00173         // because of how Apt's pkgIterator works
00174 
00175         friend class Apt;
00176     };
00177 
00178     typedef Iterator iterator;
00179     typedef RecordIterator record_iterator;
00180 
00184     Apt();
00185     ~Apt();
00186 
00187     iterator begin() const;
00188     iterator end() const;
00189 
00190     record_iterator recordBegin() const;
00191     record_iterator recordEnd() const;
00192 
00193 
00195     size_t size() const;
00196 
00201     bool isValid(const std::string& pkg) const;
00202 
00205     std::string validate(const std::string& pkg) const
00206     {
00207         if (isValid(pkg))
00208             return pkg;
00209         return std::string();
00210     }
00211 
00214     Version validate(const Version& ver) const;
00215 
00217     Version installedVersion(const std::string& pkg) const;
00218 
00220     Version candidateVersion(const std::string& pkg) const;
00221 
00226     Version anyVersion(const std::string& pkg) const;
00227 
00229     PackageState state(const std::string& pkg) const;
00230 
00237     //template<typename FILTER, typename OUT>
00238     //void search(const FILTER& filter, OUT& out);
00239 
00241     std::string rawRecord(const std::string& pkg) const;
00242 
00244     std::string rawRecord(const Version& ver) const;
00245 
00247     time_t timestamp();
00248 };
00249 
00250 }
00251 }
00252 
00253 // vim:set ts=4 sw=4:
00254 #endif

Generated on Fri Sep 14 23:09:13 2007 for libept by  doxygen 1.5.3