1 : // -*- C++ -*-
2 : #ifndef EPT_APT_APT_H
3 : #define EPT_APT_APT_H
4 :
5 : /** \file
6 : * High-level front-end to libapt-pkg, as a data provider for the ept framework.
7 : */
8 :
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 : #include <ept/core/apt.h>
30 :
31 : #include <iterator>
32 :
33 : namespace ept {
34 : namespace apt {
35 :
36 : class Exception : public wibble::exception::Generic
37 : {
38 : protected:
39 : std::string m_message;
40 :
41 : public:
42 : Exception(const std::string& context) throw ();
43 0 : ~Exception() throw () {}
44 :
45 0 : virtual const char* type() const throw () { return "Apt"; }
46 0 : virtual std::string desc() const throw () { return m_message; }
47 : };
48 :
49 : class Apt;
50 : class AptImplementation;
51 : class RecordIteratorImpl;
52 : using core::PackageState;
53 :
54 : /**
55 : * High-level access to the Apt cache, as a data provider for the ept
56 : * framework.
57 : *
58 : * This class wraps the Apt cache and allows to query it in various ways.
59 : */
60 : class Apt
61 : {
62 : protected:
63 : AptImplementation* impl;
64 :
65 : public:
66 : // Iterate Packages in the Apt cache
67 : class Iterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
68 : {
69 : void* cur;
70 :
71 : protected:
72 : // Construct a valid iterator
73 4 : Iterator(void* cur) : cur(cur) {}
74 :
75 : // Construct and end iterator
76 3581 : Iterator() : cur(0) {}
77 :
78 : public:
79 : // Copy constructor
80 : Iterator(const Iterator&);
81 : ~Iterator();
82 : std::string operator*();
83 : Iterator& operator++();
84 : Iterator& operator=(const Iterator&);
85 : bool operator==(const Iterator&) const;
86 : bool operator!=(const Iterator&) const;
87 :
88 : // FIXME: Iterator operator++(int); cannot be easily implemented
89 : // because of how Apt's pkgIterator works
90 :
91 : friend class Apt;
92 : };
93 :
94 : // Iterate Package records in the Apt cache
95 : class RecordIterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
96 : {
97 : RecordIteratorImpl* impl;
98 : size_t pos;
99 : std::string cur;
100 : size_t cur_pos;
101 :
102 : protected:
103 : // Construct a valid iterator
104 : RecordIterator(RecordIteratorImpl* cur, size_t pos = 0);
105 :
106 : // Construct and end iterator
107 3481 : RecordIterator() : impl(0), pos(0), cur_pos(0) {}
108 :
109 : public:
110 : // Copy constructor
111 : RecordIterator(const RecordIterator& r);
112 :
113 : ~RecordIterator();
114 : std::string operator*();
115 : std::string* operator->();
116 : RecordIterator& operator++();
117 : RecordIterator& operator=(const RecordIterator& r);
118 : bool operator==(const RecordIterator&) const;
119 : bool operator!=(const RecordIterator&) const;
120 :
121 : // FIXME: Iterator operator++(int); cannot be easily implemented
122 : // because of how Apt's pkgIterator works
123 :
124 : friend class Apt;
125 : };
126 :
127 : typedef Iterator iterator;
128 : typedef RecordIterator record_iterator;
129 :
130 : /**
131 : * Create the Apt data provider
132 : */
133 : Apt();
134 : ~Apt();
135 :
136 : iterator begin() const;
137 : iterator end() const;
138 :
139 : record_iterator recordBegin() const;
140 : record_iterator recordEnd() const;
141 :
142 :
143 : /// Return the number of packages in the archive
144 : size_t size() const;
145 :
146 : /**
147 : * Validate a package name, returning trye if it exists in the APT database,
148 : * or false if it does not.
149 : */
150 : bool isValid(const std::string& pkg) const;
151 :
152 : /// Validate a package name, returning it if it exists in the APT database,
153 : /// or returning the empty string if it does not.
154 : std::string validate(const std::string& pkg) const
155 : {
156 : if (isValid(pkg))
157 : return pkg;
158 : return std::string();
159 : }
160 :
161 : /// Validate a Version, returning it if it exists in the APT database, or
162 : /// returning the invalid version if it does not.
163 : Version validate(const Version& ver) const;
164 :
165 : /// Return the installed version for a package
166 : Version installedVersion(const std::string& pkg) const;
167 :
168 : /// Return the candidate version for a package
169 : Version candidateVersion(const std::string& pkg) const;
170 :
171 : /**
172 : * Return the candidate version for a package, if available, or the
173 : * installed version otherwise
174 : */
175 : Version anyVersion(const std::string& pkg) const;
176 :
177 : /// Return state information on a package
178 : PackageState state(const std::string& pkg) const;
179 :
180 : /**
181 : * Perform a package search.
182 : *
183 : * All packages for which the functor filter returns true, are passed to
184 : * the functor out.
185 : */
186 : //template<typename FILTER, typename OUT>
187 : //void search(const FILTER& filter, OUT& out);
188 :
189 : /// Get the raw package record for the given Version
190 : std::string rawRecord(const std::string& pkg) const;
191 :
192 : /// Get the raw package record for the given Version
193 : std::string rawRecord(const Version& ver) const;
194 :
195 : /// Timestamp of when the apt index was last modified
196 : time_t timestamp();
197 :
198 : /**
199 : * Check if the cache has been changed by another process, and reopen it if
200 : * that is the case.
201 : *
202 : * Note that this method can invalidate all existing iterators.
203 : */
204 : void checkCacheUpdates();
205 :
206 : /**
207 : * Invalidate the cache timestamp used to track cache updates.
208 : *
209 : * @warning Do not use this method: it is here only to support the test
210 : * cases, and may disappear in any future version.
211 : */
212 : void invalidateTimestamp();
213 : };
214 :
215 : }
216 : }
217 :
218 : // vim:set ts=4 sw=4:
219 : #endif
|