added disk usage data to PackageImplIf interface, provide this info from
[platform/upstream/libzypp.git] / zypp / detail / PackageImplIf.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/detail/PackageImplIf.h
10  *
11 */
12 #ifndef ZYPP_DETAIL_PACKAGEIMPLIF_H
13 #define ZYPP_DETAIL_PACKAGEIMPLIF_H
14
15 #include <set>
16
17 #include "zypp/detail/ResObjectImplIf.h"
18 #include "zypp/Edition.h"
19 #include "zypp/Arch.h"
20 #include "zypp/Changelog.h"
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25
26   class Package;
27
28   ///////////////////////////////////////////////////////////////////
29   namespace detail
30   { /////////////////////////////////////////////////////////////////
31
32     ///////////////////////////////////////////////////////////////////
33     //
34     //  CLASS NAME : PackageImplIf
35     //
36     /** Abstact Package implementation interface.
37     */
38     class PackageImplIf : public ResObjectImplIf
39     {
40     public:
41       typedef Package ResType;
42       class CheckSum
43       {
44       public:
45         CheckSum(const std::string & type, const std::string & checksum)
46         : _type(type)
47         , _checksum(checksum)
48         {}
49         std::string type() { return _type; }
50         std::string checksum() { return _checksum; }
51       private:
52         std::string _type;
53         std::string _checksum;
54       };
55       class BaseVersion
56       {
57       public:
58         BaseVersion(const Edition & edition,
59                     const CheckSum & checksum,
60                     const Date & buildtime)
61         : _edition(edition)
62         , _checksum(checksum)
63         , _buildtime(buildtime)
64         {}
65         Edition edition() const { return _edition; }
66         CheckSum checksum() const { return _checksum; }
67         Date buildtime() const { return _buildtime; }
68       private:
69         Edition _edition;
70         PackageImplIf::CheckSum _checksum;
71         Date _buildtime;
72       };
73       class DeltaRpm
74       {
75       public:
76         DeltaRpm(const Arch & arch,
77                  const std::string & filename, 
78                  const ByteCount & downloadsize,
79                  const CheckSum & checksum,
80                  const Date & buildtime,
81                  const BaseVersion & base_version)
82         : _arch(arch)
83         , _filename(filename)
84         , _downloadsize(downloadsize)
85         , _checksum(checksum)
86         , _buildtime(buildtime)
87         , _base_version(base_version)
88         {}
89         Arch arch() { return _arch; }
90         std::string filename() { return _filename; }
91         ByteCount downloadsize() { return _downloadsize; }
92         CheckSum checksum() { return _checksum; }
93         Date buildtime() { return _buildtime; }
94         BaseVersion baseVersion() { return _base_version; }
95       private:
96         Arch _arch;
97         std::string _filename;
98         ByteCount _downloadsize;
99         CheckSum _checksum;
100         Date _buildtime;
101         BaseVersion _base_version;
102       };
103       class PatchRpm
104       {
105       public:
106         PatchRpm(const Arch & arch,
107                  const std::string & filename, 
108                  const ByteCount & downloadsize,
109                  const CheckSum & checksum,
110                  const Date & buildtime,
111                  const std::list<BaseVersion> & base_versions)
112         : _arch(arch)
113         , _filename(filename)
114         , _downloadsize(downloadsize)
115         , _checksum(checksum)
116         , _buildtime(buildtime)
117         , _base_versions(base_versions)
118         {}
119         Arch arch() { return _arch; }
120         std::string filename() { return _filename; }
121         ByteCount downloadsize() { return _downloadsize; }
122         CheckSum checksum() { return _checksum; }
123         Date buildtime() { return _buildtime; }
124         std::list<BaseVersion> baseVersions() { return _base_versions; }
125       private:
126         Arch _arch;
127         std::string _filename;
128         ByteCount _downloadsize;
129         CheckSum _checksum;
130         Date _buildtime;
131         std::list<BaseVersion> _base_versions;
132       };
133
134       class DiskUsage {
135       public:
136         /**
137         * @short Holds data about how much space will be needed per directory
138         **/
139         struct Entry {
140           Entry() : _size(0), _files(0) {};
141           Entry(const std::string& path_r,
142                   const unsigned size_r = 0,
143                   const unsigned files_r = 0)
144           : path(path_r), _size(size_r), _files(files_r)
145           {}
146           const std::string path;
147           mutable unsigned _size;
148           mutable unsigned _files;
149           friend std::ostream & operator<<( std::ostream & str, const Entry & obj );
150           /**
151            * Test for equality based on directory name.
152            **/
153           bool operator==( const Entry & rhs ) const {
154             return  path == rhs.path;
155           }
156           /**
157            * Order based on directory name.
158            **/
159           bool operator<( const Entry & rhs ) const {
160             return  path < rhs.path;
161           }
162           /**
163            * Return true if this entry denotes a directory equal to or below rhs._dirname.
164            **/
165           bool isBelow( const Entry & rhs ) const {
166             // whether _dirname has prefix rhs._dirname
167             return( path.compare( 0, rhs.path.size(), rhs.path ) == 0 );
168           }
169           /**
170            * Return true if this entry denotes a directory equal to or below dirname_r.
171            **/
172           bool isBelow( const std::string & dirname_r ) const {
173             return  isBelow( Entry( dirname_r ) );
174           }
175     
176           /**
177            * 
178            **/
179           const Entry & operator=( const Entry & rhs ) const {
180             return rhs;
181           }
182           /**
183            * Numerical operation based on size and files values.
184            **/
185           const Entry & operator+=( const Entry & rhs ) const {
186             _size  += rhs._size;
187             _files += rhs._files;
188             return *this;
189           }
190           /**
191            * Numerical operation based on size and files values.
192            **/
193           const Entry & operator-=( const Entry & rhs ) const {
194             _size  -= rhs._size;
195             _files -= rhs._files;
196             return *this;
197           }
198         };
199       private:
200         typedef std::set<Entry> EntrySet;
201         EntrySet _dirs;
202       public:
203
204         DiskUsage() {};
205        /**
206          * Add an entry. If already present, sum up the new entries size and files value.
207          **/
208         void add( const Entry & newent_r ) {
209           std::pair<EntrySet::iterator,bool> res = _dirs.insert( newent_r );
210           if ( !res.second ) {
211             *res.first += newent_r;
212           }
213         }
214        /**
215          * Add an entry. If already present, sum up the new entries size and files value.
216          **/
217         void add( const std::string & dirname_r, const unsigned & size_r = 0, const unsigned & files_r = 0 ) {
218           add( Entry( dirname_r, size_r, files_r ) );
219         }
220         /**
221          * Number of entries
222          **/
223         unsigned size() const { return _dirs.size(); }
224         /**
225          * Clear EntrySet
226          **/
227         void clear() { _dirs.clear(); }
228         /**
229          * Sum up any entries for dirname_r and its descendants and remove them
230          * on the fly. Return the result.
231          **/
232         Entry extract( const std::string & dirname_r );
233
234       public:
235
236         typedef EntrySet::iterator               iterator;
237         typedef EntrySet::reverse_iterator       reverse_iterator;
238      
239         /**
240          * Forward iterator pointing to the first entry (if any)
241          **/
242         iterator begin() { return _dirs.begin(); }
243         /**
244          * Forward iterator pointing behind the last entry.
245          **/
246         iterator end() { return _dirs.end(); }
247         /**
248          * Reverse iterator pointing to the last entry (if any)
249          **/
250         reverse_iterator rbegin() { return _dirs.rbegin(); }
251         /**
252          * Reverse iterator pointing before the first entry.
253          **/
254         reverse_iterator rend() { return _dirs.rend(); }
255      
256         typedef EntrySet::const_iterator         const_iterator;
257         typedef EntrySet::const_reverse_iterator const_reverse_iterator;
258      
259         /**
260          * Forward const iterator pointing to the first entry (if any)
261          **/
262         const_iterator begin() const { return _dirs.begin(); }
263         /**
264          * Forward const iterator pointing behind the last entry.
265          **/
266         const_iterator end() const { return _dirs.end(); }
267         /**
268          * Reverse const iterator pointing to the last entry (if any)
269          **/
270         const_reverse_iterator rbegin() const { return _dirs.rbegin(); }
271         /**
272          * Reverse const iterator pointing before the first entry.
273          **/
274         const_reverse_iterator rend()const { return _dirs.rend(); }
275
276       public:
277
278         friend std::ostream & operator<<( std::ostream & str, const PackageImplIf::DiskUsage & obj );
279
280       };
281 #if 0
282     
283       /**
284        * @short Holds Data about file and file type
285        *  (directory, plain)
286        **/
287       class FileData {
288       public:
289         std::string name;
290         std::string type;
291         FileData();
292         FileData(const std::string &name,
293                  const std::string &type)
294         : name(name), type(type)
295         {}
296       };
297 #endif
298
299     public:
300       /** \name Rpm Package Attributes. */
301       //@{
302       /** */
303       virtual Date buildtime() const PURE_VIRTUAL;
304       /** */
305       virtual std::string buildhost() const PURE_VIRTUAL;
306       /** */
307       virtual Date installtime() const PURE_VIRTUAL;
308       /** */
309       virtual std::string distribution() const PURE_VIRTUAL;
310       /** */
311       virtual Vendor vendor() const PURE_VIRTUAL;
312       /** */
313       virtual Label license() const PURE_VIRTUAL;
314       /** */
315       virtual std::string packager() const PURE_VIRTUAL;
316       /** */
317       virtual PackageGroup group() const PURE_VIRTUAL;
318       /** */
319       virtual Changelog changelog() const PURE_VIRTUAL;
320       /** Don't ship it as class Url, because it might be
321        * in fact anything but a legal Url. */
322       virtual std::string url() const PURE_VIRTUAL;
323       /** */
324       virtual std::string os() const PURE_VIRTUAL;
325       /** */
326       virtual Text prein() const PURE_VIRTUAL;
327       /** */
328       virtual Text postin() const PURE_VIRTUAL;
329       /** */
330       virtual Text preun() const PURE_VIRTUAL;
331       /** */
332       virtual Text postun() const PURE_VIRTUAL;
333       /** */
334       virtual ByteCount sourcesize() const PURE_VIRTUAL;
335       /** */
336       virtual ByteCount archivesize() const PURE_VIRTUAL;
337       /** */
338       virtual Text authors() const PURE_VIRTUAL;
339       /** */
340       virtual Text filenames() const PURE_VIRTUAL;
341       //@}
342
343       /** \name Additional Package Attributes.
344        * \todo review what's actually needed here. Maybe worth grouping
345        * all the package rertieval related stuff in a class. Easier to ship
346        * and handle it.
347       */
348       //@{
349       /** */
350       virtual License licenseToConfirm() const PURE_VIRTUAL;
351 #if 0
352       /** */
353       virtual std::string sourceloc() const PURE_VIRTUAL;
354       /** */
355       virtual void du( PkgDu & dudata_r ) const PURE_VIRTUAL;
356       /** */
357       virtual std::string location() const PURE_VIRTUAL;
358       /** */
359       virtual unsigned int medianr() const PURE_VIRTUAL;
360       /** */
361       virtual PackageKeywords keywords() const PURE_VIRTUAL;
362       /** */
363       virtual std::string md5sum() const PURE_VIRTUAL;
364       /** */
365       virtual std::string externalUrl() const PURE_VIRTUAL;
366       /** */
367       virtual std::list<Edition> patchRpmBaseVersions() const PURE_VIRTUAL;
368       /** */
369       virtual ByteCount patchRpmSize() const PURE_VIRTUAL;
370       /** */
371       virtual bool forceInstall() const PURE_VIRTUAL;
372       /** */
373       virtual std::string patchRpmMD5() const PURE_VIRTUAL;
374       /** */
375       virtual bool isRemote() const PURE_VIRTUAL;
376       /** */
377       virtual PMError providePkgToInstall( Pathname& path_r ) const PURE_VIRTUAL;
378       /** */
379       virtual PMError provideSrcPkgToInstall( Pathname& path_r ) const PURE_VIRTUAL;
380       /** */
381       virtual constInstSrcPtr source() const PURE_VIRTUAL;
382       /** */
383       virtual std::list<PMPackageDelta> deltas() const PURE_VIRTUAL;
384 #endif
385       //@}
386     };
387     ///////////////////////////////////////////////////////////////////
388
389     /////////////////////////////////////////////////////////////////
390   } // namespace detail
391   ///////////////////////////////////////////////////////////////////
392   /////////////////////////////////////////////////////////////////
393 } // namespace zypp
394 ///////////////////////////////////////////////////////////////////
395 #endif // ZYPP_DETAIL_PACKAGEIMPLIF_H