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 <ept/apt/version.h>
28 #include <iterator>
29 #include <stdexcept>
30 
31 class pkgCache;
32 
33 namespace ept {
34 namespace apt {
35 
36 class Exception : public std::runtime_error
37 {
38 public:
39  Exception(const std::string& message);
40  ~Exception() noexcept override;
41 };
42 
43 class Apt;
44 class AptImplementation;
45 class RecordIteratorImpl;
46 
47 struct PackageState {
48  enum Query {
49  Install = 1 << 0,
50  Upgrade = 1 << 1,
51  Keep = 1 << 2,
52  Remove = 1 << 3,
53  Installed = 1 << 4,
54  Upgradable = 1 << 5,
55  NowBroken = 1 << 6,
56  WillBreak = 1 << 7,
57  ReInstall = 1 << 8,
58  Purge = 1 << 9,
59  Hold = 1 << 10,
60  Valid = 1 << 11
61  };
62 
63  typedef unsigned state;
64 
65  operator unsigned() { return m_state; };
66 
67  PackageState &operator=( unsigned i ) {
68  m_state = i;
69  return *this;
70  }
71 
73  m_state |= s.m_state;
74  return *this;
75  }
76 
77  PackageState( unsigned a ) {
78  m_state = a;
79  }
80 
81  PackageState() : m_state( 0 ) {}
82 
83  // FIXME this probably needs to be used consistently in core and out of core
84  bool isValid() const { return m_state & Valid; }
85  // FIXME compatibility API for non-core apt
86  bool isInstalled() const { return installed(); }
87 
88  bool install() const { return m_state & Install; }
89  // reinstall() implies install()
90  bool reinstall() const { return m_state & ReInstall; }
91  bool remove() const { return m_state & Remove; }
92  // purge() implies remove()
93  bool purge() const { return m_state & Purge; }
94  bool keep() const { return m_state & Keep; }
95  bool willBreak() const { return m_state & WillBreak; }
96  // upgrade() implies install()
97  bool upgrade() const { return hasNewVersion() && install(); }
98  // newInsstal() implies install()
99  bool newInstall() const { return !installed() && install(); }
100  bool hold() const { return m_state & Hold; }
101 
102  bool installed() const { return m_state & Installed; }
103  bool hasNewVersion() const { return m_state & Upgradable; }
104  bool upgradable() const { return hasNewVersion() && !hold(); }
105  bool held() const { return hasNewVersion() && hold(); }
106  bool nowBroken() const { return m_state & NowBroken; }
107 
108  bool modify() const { return install() || remove(); }
109 
110 protected:
111  unsigned m_state;
112 };
113 
120 class Apt
121 {
122 protected:
123  AptImplementation* impl;
124 
125 public:
126  // Iterate Packages in the Apt cache
127  class Iterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
128  {
129  void* cur;
130 
131  protected:
132  // Construct a valid iterator
133  Iterator(void* cur) : cur(cur) {}
134 
135  // Construct and end iterator
136  Iterator() : cur(0) {}
137 
138  public:
139  // Copy constructor
140  Iterator(const Iterator&);
141  ~Iterator();
142  std::string operator*();
143  Iterator& operator++();
144  Iterator& operator=(const Iterator&);
145  bool operator==(const Iterator&) const;
146  bool operator!=(const Iterator&) const;
147 
148  // FIXME: Iterator operator++(int); cannot be easily implemented
149  // because of how Apt's pkgIterator works
150 
151  friend class Apt;
152  };
153 
154  // Iterate Package records in the Apt cache
155  class RecordIterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
156  {
157  RecordIteratorImpl* impl;
158  size_t pos;
159  std::string cur;
160  size_t cur_pos;
161 
162  protected:
163  // Construct a valid iterator
164  RecordIterator(RecordIteratorImpl* cur, size_t pos = 0);
165 
166  // Construct and end iterator
167  RecordIterator() : impl(0), pos(0), cur_pos(0) {}
168 
169  public:
170  // Copy constructor
171  RecordIterator(const RecordIterator& r);
172 
173  ~RecordIterator();
174  std::string operator*();
175  std::string* operator->();
178  bool operator==(const RecordIterator&) const;
179  bool operator!=(const RecordIterator&) const;
180 
181  // FIXME: Iterator operator++(int); cannot be easily implemented
182  // because of how Apt's pkgIterator works
183 
184  friend class Apt;
185  };
186 
189 
193  Apt();
194  ~Apt();
195 
196 
197  iterator begin() const;
198  iterator end() const;
199 
201  record_iterator recordEnd() const;
202 
203 
205  size_t size() const;
206 
211  bool isValid(const std::string& pkg) const;
212 
215  std::string validate(const std::string& pkg) const
216  {
217  if (isValid(pkg))
218  return pkg;
219  return std::string();
220  }
221 
224  Version validate(const Version& ver) const;
225 
227  Version installedVersion(const std::string& pkg) const;
228 
230  Version candidateVersion(const std::string& pkg) const;
231 
236  Version anyVersion(const std::string& pkg) const;
237 
239  PackageState state(const std::string& pkg) const;
240 
247  //template<typename FILTER, typename OUT>
248  //void search(const FILTER& filter, OUT& out);
249 
251  std::string rawRecord(const std::string& pkg) const;
252 
254  std::string rawRecord(const Version& ver) const;
255 
257  const pkgCache* aptPkgCache() const;
258 
259 
260 
262  time_t timestamp();
263 
270  void checkCacheUpdates();
271 
278  void invalidateTimestamp();
279 };
280 
281 }
282 }
283 
284 // vim:set ts=4 sw=4:
285 #endif
ept::apt::PackageState::Remove
Definition: apt.h:52
ept::apt::PackageState::upgradable
bool upgradable() const
Definition: apt.h:104
register_tests
void register_tests() override
Definition: apt-test.cc:4
ept::apt::Apt::Iterator::operator!=
bool operator!=(const Iterator &) const
Definition: apt.cc:358
sys.h
ept::apt::PackageState::Installed
Definition: apt.h:53
ept::apt::PackageState::remove
bool remove() const
Definition: apt.h:91
ept::apt::Apt::RecordIterator::operator->
std::string * operator->()
Definition: apt.cc:400
ept::apt::Apt::iterator
Iterator iterator
Definition: apt.h:187
ept::apt::Apt::size
size_t size() const
Return the number of packages in the archive.
Definition: apt.cc:469
ept::apt::PackageState::hasNewVersion
bool hasNewVersion() const
Definition: apt.h:103
ept::axi::timestamp
time_t timestamp()
Return the last update timestamp of the index.
Definition: axi.cc:48
ept::apt::Apt::Apt
Apt()
Create the Apt data provider.
Definition: apt.cc:444
ept::apt::Exception::Exception
Exception(const std::string &message)
Definition: apt.cc:68
ept::apt::Apt::candidateVersion
Version candidateVersion(const std::string &pkg) const
Return the candidate version for a package.
Definition: apt.cc:499
ept::apt::PackageState::state
unsigned state
Definition: apt.h:63
ept::apt::PackageState::NowBroken
Definition: apt.h:55
ept::apt::Apt::checkCacheUpdates
void checkCacheUpdates()
Check if the cache has been changed by another process, and reopen it if that is the case.
Definition: apt.cc:637
ept::apt::Apt::RecordIterator::operator==
bool operator==(const RecordIterator &) const
Definition: apt.cc:434
ept::apt::Apt::Iterator::operator==
bool operator==(const Iterator &) const
Definition: apt.cc:348
ept::apt::PackageState::operator|=
PackageState & operator|=(const PackageState &s)
Definition: apt.h:72
ept::apt::Apt::Iterator::Iterator
Iterator(void *cur)
Definition: apt.h:133
ept::apt::Apt::RecordIterator::operator=
RecordIterator & operator=(const RecordIterator &r)
Definition: apt.cc:422
ept::tests
Definition: tests.cc:23
ept::apt::Apt::anyVersion
Version anyVersion(const std::string &pkg) const
Return the candidate version for a package, if available, or the installed version otherwise.
Definition: apt.cc:518
ept::apt::PackageState::Install
Definition: apt.h:49
ept::apt::PackageState::Purge
Definition: apt.h:58
ept::apt::PackageState::willBreak
bool willBreak() const
Definition: apt.h:95
version.h
ept::apt::PackageState::Hold
Definition: apt.h:59
ept::apt::PackageState::m_state
unsigned m_state
Definition: apt.h:111
ept::apt::PackageState::Upgrade
Definition: apt.h:50
ept::apt::Version::name
std::string name() const
Return the package name.
Definition: version.h:61
ept
String functions.
Definition: apt.cc:40
ept::apt::Apt::Iterator::operator*
std::string operator*()
Definition: apt.cc:331
res
set< string > & res
Definition: packagerecord.cc:73
ept::apt::PackageState::isValid
bool isValid() const
Definition: apt.h:84
ept::apt::Apt::state
PackageState state(const std::string &pkg) const
Return state information on a package.
Definition: apt.cc:533
ept::apt::PackageState::PackageState
PackageState(unsigned a)
Definition: apt.h:77
ept::apt::localityCompare
bool localityCompare(const pkgCache::VerFile *a, const pkgCache::VerFile *b)
Definition: apt.cc:160
ept::apt::PackageState::Valid
Definition: apt.h:60
apt.h
ept::apt::Apt::begin
iterator begin() const
Definition: apt.cc:447
ept::apt::PackageState::PackageState
PackageState()
Definition: apt.h:81
ept::apt::PackageState::operator=
PackageState & operator=(unsigned i)
Definition: apt.h:67
ept::apt::Exception::~Exception
~Exception() noexcept override
Definition: apt.cc:73
ept::apt::Apt::timestamp
time_t timestamp()
Timestamp of when the apt index was last modified.
Definition: apt.cc:474
ept::apt::tests
struct ept::apt::AptImplementation tests
ept::tests::actual
Actual< A > actual(const A &actual)
Definition: tests.h:320
ept::apt::Apt::aptPkgCache
const pkgCache * aptPkgCache() const
Returns the pointer to the internal libapt pkgCache object used.
Definition: apt.cc:631
ept::apt::Apt::RecordIterator::operator++
RecordIterator & operator++()
Definition: apt.cc:409
ept::apt::PackageState::Keep
Definition: apt.h:51
test.h
ept::apt::Apt::invalidateTimestamp
void invalidateTimestamp()
Invalidate the cache timestamp used to track cache updates.
Definition: apt.cc:647
ept::apt::Apt::Iterator::Iterator
Iterator()
Definition: apt.h:136
ept::apt::Apt::rawRecord
std::string rawRecord(const std::string &pkg) const
Perform a package search.
Definition: apt.cc:580
ept::apt::Apt::RecordIterator::RecordIterator
RecordIterator()
Definition: apt.h:167
ept::apt::Apt::impl
AptImplementation * impl
Definition: apt.h:123
ept::tests::TestCase
Test case collecting several test methods, and self-registering with the singleton instance of TestRe...
Definition: tests.h:614
ept::apt::PackageState::nowBroken
bool nowBroken() const
Definition: apt.h:106
ept::apt::PackageState::WillBreak
Definition: apt.h:56
ept::sys::size
size_t size(const std::string &file)
File size.
Definition: sys.cc:116
ept::apt::Version::isValid
bool isValid() const
Return true if this package contains a valid value.
Definition: version.h:77
ept::apt::PackageState::ReInstall
Definition: apt.h:57
ept::apt::Apt::Iterator::~Iterator
~Iterator()
Definition: apt.cc:327
ept::apt::PackageState::purge
bool purge() const
Definition: apt.h:93
ept::apt::Apt::RecordIterator::~RecordIterator
~RecordIterator()
Definition: apt.cc:386
ept::apt::PackageState::Query
Query
Definition: apt.h:48
ept::apt::PackageState::isInstalled
bool isInstalled() const
Definition: apt.h:86
ept::apt::Apt::Iterator::operator=
Iterator & operator=(const Iterator &)
Definition: apt.cc:311
std
Definition: packagerecord-test.cc:4
ept::apt::Apt::RecordIterator
Definition: apt.h:155
ept::apt::PackageState::Upgradable
Definition: apt.h:54
ept::apt::Apt
High-level access to the Apt cache, as a data provider for the ept framework.
Definition: apt.h:120
ept::apt::Apt::RecordIterator::operator*
std::string operator*()
Definition: apt.cc:391
ept::apt::Apt::~Apt
~Apt()
Definition: apt.cc:445
ept::apt::Apt::recordEnd
record_iterator recordEnd() const
Definition: apt.cc:464
ept::apt
Definition: apt.cc:41
ept::apt::PackageState::reinstall
bool reinstall() const
Definition: apt.h:90
ept::apt::PackageState::newInstall
bool newInstall() const
Definition: apt.h:99
ept::apt::Apt::record_iterator
RecordIterator record_iterator
Definition: apt.h:188
wassert_true
#define wassert_true(...)
Shortcut to check that a given expression returns true.
Definition: tests.h:354
wassert
#define wassert(...)
Run the given command, raising TestFailed with the appropriate backtrace information if it threw an e...
Definition: tests.h:343
ept::apt::Apt::end
iterator end() const
Definition: apt.cc:454
ept::apt::Version::version
std::string version() const
Return the package version, or the empty string if this is a versionless package.
Definition: version.h:67
ept::apt::Apt::recordBegin
record_iterator recordBegin() const
Definition: apt.cc:459
ept::apt::PackageState::upgrade
bool upgrade() const
Definition: apt.h:97
ept::apt::PackageState
Definition: apt.h:47
ept::apt::Apt::installedVersion
Version installedVersion(const std::string &pkg) const
Return the installed version for a package.
Definition: apt.cc:508
ept::apt::PackageState::modify
bool modify() const
Definition: apt.h:108
ept::apt::PackageState::held
bool held() const
Definition: apt.h:105
ept::apt::PackageState::installed
bool installed() const
Definition: apt.h:102
ept::apt::Apt::Iterator::operator++
Iterator & operator++()
Definition: apt.cc:335
ept::apt::PackageState::hold
bool hold() const
Definition: apt.h:100
ept::apt::Exception
Definition: apt.h:36
ept::apt::Apt::RecordIterator::operator!=
bool operator!=(const RecordIterator &) const
Definition: apt.cc:438
ept::apt::Apt::isValid
bool isValid(const std::string &pkg) const
Validate a package name, returning trye if it exists in the APT database, or false if it does not.
Definition: apt.cc:479
ept::apt::PackageState::keep
bool keep() const
Definition: apt.h:94
ept::apt::PackageState::install
bool install() const
Definition: apt.h:88
ept::apt::Apt::Iterator
Definition: apt.h:127
ept::apt::Version
Lightweight Version class that represent a package with a version, with very cheap value copy operati...
Definition: version.h:40
ept::apt::Apt::validate
std::string validate(const std::string &pkg) const
Validate a package name, returning it if it exists in the APT database, or returning the empty string...
Definition: apt.h:215