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 namespace ept {
33 namespace apt {
34 
35 class Exception : public wibble::exception::Generic
36 {
37 protected:
38  std::string m_message;
39 
40 public:
41  Exception(const std::string& context) throw ();
42  ~Exception() throw () {}
43 
44  virtual const char* type() const throw () { return "Apt"; }
45  virtual std::string desc() const throw () { return m_message; }
46 };
47 
48 class Apt;
49 class AptImplementation;
50 class RecordIteratorImpl;
51 
52 struct PackageState {
53  enum Query {
54  Install = 1 << 0,
55  Upgrade = 1 << 1,
56  Keep = 1 << 2,
57  Remove = 1 << 3,
58  Installed = 1 << 4,
59  Upgradable = 1 << 5,
60  NowBroken = 1 << 6,
61  WillBreak = 1 << 7,
62  ReInstall = 1 << 8,
63  Purge = 1 << 9,
64  Hold = 1 << 10,
65  Valid = 1 << 11
66  };
67 
68  typedef unsigned state;
69 
70  operator unsigned() { return m_state; };
71 
72  PackageState &operator=( unsigned i ) {
73  m_state = i;
74  return *this;
75  }
76 
78  m_state |= s.m_state;
79  return *this;
80  }
81 
82  PackageState( unsigned a ) {
83  m_state = a;
84  }
85 
86  PackageState() : m_state( 0 ) {}
87 
88  // FIXME this probably needs to be used consistently in core and out of core
89  bool isValid() const { return m_state & Valid; }
90  // FIXME compatibility API for non-core apt
91  bool isInstalled() const { return installed(); }
92 
93  bool install() const { return m_state & Install; }
94  // reinstall() implies install()
95  bool reinstall() const { return m_state & ReInstall; }
96  bool remove() const { return m_state & Remove; }
97  // purge() implies remove()
98  bool purge() const { return m_state & Purge; }
99  bool keep() const { return m_state & Keep; }
100  bool willBreak() const { return m_state & WillBreak; }
101  // upgrade() implies install()
102  bool upgrade() const { return hasNewVersion() && install(); }
103  // newInsstal() implies install()
104  bool newInstall() const { return !installed() && install(); }
105  bool hold() const { return m_state & Hold; }
106 
107  bool installed() const { return m_state & Installed; }
108  bool hasNewVersion() const { return m_state & Upgradable; }
109  bool upgradable() const { return hasNewVersion() && !hold(); }
110  bool held() const { return hasNewVersion() && hold(); }
111  bool nowBroken() const { return m_state & NowBroken; }
112 
113  bool modify() const { return install() || remove(); }
114 
115 protected:
116  unsigned m_state;
117 };
118 
125 class Apt
126 {
127 protected:
128  AptImplementation* impl;
129 
130 public:
131  // Iterate Packages in the Apt cache
132  class Iterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
133  {
134  void* cur;
135 
136  protected:
137  // Construct a valid iterator
138  Iterator(void* cur) : cur(cur) {}
139 
140  // Construct and end iterator
141  Iterator() : cur(0) {}
142 
143  public:
144  // Copy constructor
145  Iterator(const Iterator&);
146  ~Iterator();
147  std::string operator*();
148  Iterator& operator++();
149  Iterator& operator=(const Iterator&);
150  bool operator==(const Iterator&) const;
151  bool operator!=(const Iterator&) const;
152 
153  // FIXME: Iterator operator++(int); cannot be easily implemented
154  // because of how Apt's pkgIterator works
155 
156  friend class Apt;
157  };
158 
159  // Iterate Package records in the Apt cache
160  class RecordIterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
161  {
162  RecordIteratorImpl* impl;
163  size_t pos;
164  std::string cur;
165  size_t cur_pos;
166 
167  protected:
168  // Construct a valid iterator
169  RecordIterator(RecordIteratorImpl* cur, size_t pos = 0);
170 
171  // Construct and end iterator
172  RecordIterator() : impl(0), pos(0), cur_pos(0) {}
173 
174  public:
175  // Copy constructor
176  RecordIterator(const RecordIterator& r);
177 
178  ~RecordIterator();
179  std::string operator*();
180  std::string* operator->();
183  bool operator==(const RecordIterator&) const;
184  bool operator!=(const RecordIterator&) const;
185 
186  // FIXME: Iterator operator++(int); cannot be easily implemented
187  // because of how Apt's pkgIterator works
188 
189  friend class Apt;
190  };
191 
194 
198  Apt();
199  ~Apt();
200 
201  iterator begin() const;
202  iterator end() const;
203 
205  record_iterator recordEnd() const;
206 
207 
209  size_t size() const;
210 
215  bool isValid(const std::string& pkg) const;
216 
219  std::string validate(const std::string& pkg) const
220  {
221  if (isValid(pkg))
222  return pkg;
223  return std::string();
224  }
225 
228  Version validate(const Version& ver) const;
229 
231  Version installedVersion(const std::string& pkg) const;
232 
234  Version candidateVersion(const std::string& pkg) const;
235 
240  Version anyVersion(const std::string& pkg) const;
241 
243  PackageState state(const std::string& pkg) const;
244 
251  //template<typename FILTER, typename OUT>
252  //void search(const FILTER& filter, OUT& out);
253 
255  std::string rawRecord(const std::string& pkg) const;
256 
258  std::string rawRecord(const Version& ver) const;
259 
261  time_t timestamp();
262 
269  void checkCacheUpdates();
270 
277  void invalidateTimestamp();
278 };
279 
280 }
281 }
282 
283 // vim:set ts=4 sw=4:
284 #endif