libept
apt.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 #ifndef EPT_APT_APT_H
3 #define EPT_APT_APT_H
4 
9 /*
10  * Copyright (C) 2007,2008 Enrico Zini <enrico@enricozini.org>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  */
26 
27 #include <wibble/exception.h>
28 #include <ept/apt/version.h>
29 
30 #include <iterator>
31 
32 class pkgCache;
33 
34 namespace ept {
35 namespace apt {
36 
37 class Exception : public wibble::exception::Generic
38 {
39 protected:
40  std::string m_message;
41 
42 public:
43  Exception(const std::string& context) throw ();
44  ~Exception() throw () {}
45 
46  virtual const char* type() const throw () { return "Apt"; }
47  virtual std::string desc() const throw () { return m_message; }
48 };
49 
50 class Apt;
51 class AptImplementation;
52 class RecordIteratorImpl;
53 
54 struct PackageState {
55  enum Query {
56  Install = 1 << 0,
57  Upgrade = 1 << 1,
58  Keep = 1 << 2,
59  Remove = 1 << 3,
60  Installed = 1 << 4,
61  Upgradable = 1 << 5,
62  NowBroken = 1 << 6,
63  WillBreak = 1 << 7,
64  ReInstall = 1 << 8,
65  Purge = 1 << 9,
66  Hold = 1 << 10,
67  Valid = 1 << 11
68  };
69 
70  typedef unsigned state;
71 
72  operator unsigned() { return m_state; };
73 
74  PackageState &operator=( unsigned i ) {
75  m_state = i;
76  return *this;
77  }
78 
80  m_state |= s.m_state;
81  return *this;
82  }
83 
84  PackageState( unsigned a ) {
85  m_state = a;
86  }
87 
88  PackageState() : m_state( 0 ) {}
89 
90  // FIXME this probably needs to be used consistently in core and out of core
91  bool isValid() const { return m_state & Valid; }
92  // FIXME compatibility API for non-core apt
93  bool isInstalled() const { return installed(); }
94 
95  bool install() const { return m_state & Install; }
96  // reinstall() implies install()
97  bool reinstall() const { return m_state & ReInstall; }
98  bool remove() const { return m_state & Remove; }
99  // purge() implies remove()
100  bool purge() const { return m_state & Purge; }
101  bool keep() const { return m_state & Keep; }
102  bool willBreak() const { return m_state & WillBreak; }
103  // upgrade() implies install()
104  bool upgrade() const { return hasNewVersion() && install(); }
105  // newInsstal() implies install()
106  bool newInstall() const { return !installed() && install(); }
107  bool hold() const { return m_state & Hold; }
108 
109  bool installed() const { return m_state & Installed; }
110  bool hasNewVersion() const { return m_state & Upgradable; }
111  bool upgradable() const { return hasNewVersion() && !hold(); }
112  bool held() const { return hasNewVersion() && hold(); }
113  bool nowBroken() const { return m_state & NowBroken; }
114 
115  bool modify() const { return install() || remove(); }
116 
117 protected:
118  unsigned m_state;
119 };
120 
127 class Apt
128 {
129 protected:
130  AptImplementation* impl;
131 
132 public:
133  // Iterate Packages in the Apt cache
134  class Iterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
135  {
136  void* cur;
137 
138  protected:
139  // Construct a valid iterator
140  Iterator(void* cur) : cur(cur) {}
141 
142  // Construct and end iterator
143  Iterator() : cur(0) {}
144 
145  public:
146  // Copy constructor
147  Iterator(const Iterator&);
148  ~Iterator();
149  std::string operator*();
150  Iterator& operator++();
151  Iterator& operator=(const Iterator&);
152  bool operator==(const Iterator&) const;
153  bool operator!=(const Iterator&) const;
154 
155  // FIXME: Iterator operator++(int); cannot be easily implemented
156  // because of how Apt's pkgIterator works
157 
158  friend class Apt;
159  };
160 
161  // Iterate Package records in the Apt cache
162  class RecordIterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
163  {
164  RecordIteratorImpl* impl;
165  size_t pos;
166  std::string cur;
167  size_t cur_pos;
168 
169  protected:
170  // Construct a valid iterator
171  RecordIterator(RecordIteratorImpl* cur, size_t pos = 0);
172 
173  // Construct and end iterator
174  RecordIterator() : impl(0), pos(0), cur_pos(0) {}
175 
176  public:
177  // Copy constructor
178  RecordIterator(const RecordIterator& r);
179 
180  ~RecordIterator();
181  std::string operator*();
182  std::string* operator->();
185  bool operator==(const RecordIterator&) const;
186  bool operator!=(const RecordIterator&) const;
187 
188  // FIXME: Iterator operator++(int); cannot be easily implemented
189  // because of how Apt's pkgIterator works
190 
191  friend class Apt;
192  };
193 
196 
200  Apt();
201  ~Apt();
202 
203 
204  iterator begin() const;
205  iterator end() const;
206 
208  record_iterator recordEnd() const;
209 
210 
212  size_t size() const;
213 
218  bool isValid(const std::string& pkg) const;
219 
222  std::string validate(const std::string& pkg) const
223  {
224  if (isValid(pkg))
225  return pkg;
226  return std::string();
227  }
228 
231  Version validate(const Version& ver) const;
232 
234  Version installedVersion(const std::string& pkg) const;
235 
237  Version candidateVersion(const std::string& pkg) const;
238 
243  Version anyVersion(const std::string& pkg) const;
244 
246  PackageState state(const std::string& pkg) const;
247 
254  //template<typename FILTER, typename OUT>
255  //void search(const FILTER& filter, OUT& out);
256 
258  std::string rawRecord(const std::string& pkg) const;
259 
261  std::string rawRecord(const Version& ver) const;
262 
264  const pkgCache* aptPkgCache() const;
265 
266 
267 
269  time_t timestamp();
270 
277  void checkCacheUpdates();
278 
285  void invalidateTimestamp();
286 };
287 
288 }
289 }
290 
291 // vim:set ts=4 sw=4:
292 #endif