Imported Upstream version 14.29.0
[platform/upstream/libzypp.git] / zypp / DiskUsageCounter.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/DiskUsageCounter.h
10  *
11 */
12 #ifndef ZYPP_DISKUSAGE_COUNTER_H
13 #define ZYPP_DISKUSAGE_COUNTER_H
14
15 #include <set>
16 #include <string>
17 #include <iosfwd>
18
19 #include "zypp/ResPool.h"
20 #include "zypp/Bitmap.h"
21 #include "zypp/base/Flags.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26
27   ///////////////////////////////////////////////////////////////////
28   /// \class DiskUsageCounter
29   /// \brief Compute disk space occupied by packages across partitions/directories
30   ///////////////////////////////////////////////////////////////////
31   class DiskUsageCounter
32   {
33
34   public:
35     ///////////////////////////////////////////////////////////////////
36     /// \class MountPoint
37     /// \brief Mount point description
38     /// If \ref block_size is set \ref DiskUsageCoutner will assume
39     /// half a block_size is wasted per file, in case a package
40     /// provides detailed isk usage information.
41     ///////////////////////////////////////////////////////////////////
42     struct MountPoint
43     {
44       friend std::ostream & operator<<( std::ostream & str, const MountPoint & obj );
45       std::string dir;                  ///< Directory name
46       long long block_size;             ///< Block size of the filesystem in B (0 if you don't care)
47       long long total_size;             ///< Total size of the filesystem in KiB (0 if you don't care)
48       long long used_size;              ///< Used size of the filesystem in KiB (0 if you don't care)
49       mutable long long pkg_size;       ///< Used size after installation in KiB (computed by \ref DiskUsageCoutner)
50       // hint bits:
51       bool readonly:1;                  ///< hint for readonly partitions
52       bool growonly:1;                  ///< hint for growonly partitions (e.g. snapshotting btrfs)
53
54
55       /** HinFlags for ctor */
56       enum Hint
57       {
58         NoHint          = 0,
59         Hint_readonly   = (1<<0),       ///< readonly partitions
60         Hint_growonly   = (1<<1),       ///< growonly partitions (e.g. snapshotting btrfs)
61       };
62       ZYPP_DECLARE_FLAGS(HintFlags,Hint);
63
64
65       /** Ctor initialize directory and sizes */
66       MountPoint( const std::string & d = "/",
67                   long long bs = 0LL, long long total = 0LL, long long used = 0LL, long long pkg = 0LL,
68                   HintFlags hints = NoHint )
69         : dir(d)
70         , block_size(bs), total_size(total), used_size(used), pkg_size(pkg)
71         , readonly(hints.testFlag(Hint_readonly))
72         , growonly(hints.testFlag(Hint_growonly))
73       {}
74        /** \overload <tt>const char *</tt> to allow e.g. initiailzer lists
75        * \code
76        *   MountPointSet( { "/", "/usr", "/var" } )
77        * \endcode
78        */
79       MountPoint( const char * d, long long bs = 0LL, long long total = 0LL, long long used = 0LL, long long pkg = 0LL, HintFlags hints = NoHint )
80         : MountPoint( std::string(d?d:""), bs, total, used, pkg, hints )
81       {}
82
83       /** \overload just name and hints, all sizes 0 */
84       MountPoint( const std::string & d, HintFlags hints )
85         : MountPoint( d, 0LL, 0LL, 0LL, 0LL, hints )
86       {}
87       /** \overload just name and hints, all sizes 0 */
88       MountPoint( const char * d, HintFlags hints )
89         : MountPoint( std::string(d?d:""), hints )
90       {}
91       /** \overload just name and hints, all sizes 0 */
92       MountPoint( const std::string & d, Hint hint )    // !explicitly overload to prevent propagation enum value -> long long
93         : MountPoint( d, HintFlags(hint) )
94       {}
95       /** \overload just name and hints, all sizes 0 */
96       MountPoint( const char * d, Hint hint )           // !explicitly overload to prevent propagation enum value -> long long
97         : MountPoint( std::string(d?d:""), HintFlags(hint) )
98       {}
99
100
101       /** \deprecated Use HintFlags instead of a trailing 'bool ro' argument.
102        * \code
103        *   -  MountPoint( "/usr", ..., true ); // readonly
104        *   +  MountPoint( "/usr", ..., MountPoint::Hint_readonly );
105        * \endcode
106        */
107       ZYPP_DEPRECATED MountPoint( const std::string & d, long long bs, long long total, long long used, long long pkg, bool ro )
108         : MountPoint( d, bs, total, used, pkg, HintFlags(ro?Hint_readonly:NoHint) )
109       {}
110       /** \deprecated Use HintFlags instead of a trailing 'bool ro' argument.
111        * \code
112        *   -  MountPoint( "/usr", ..., true ); // readonly
113        *   +  MountPoint( "/usr", ..., MountPoint::Hint_readonly );
114        * \endcode
115        */
116       ZYPP_DEPRECATED MountPoint( const char * d, long long bs, long long total, long long used, long long pkg, bool ro )
117         : MountPoint( d, bs, total, used, pkg, HintFlags(ro?Hint_readonly:NoHint) )
118       {}
119
120
121       /** Sort by directory name */
122       bool operator<( const MountPoint & rhs ) const
123       { return dir < rhs.dir; }
124
125       /** Block size of the filesystem as \ref ByteCount for convenience. */
126       ByteCount blockSize() const
127       { return ByteCount( block_size, ByteCount::B ); }
128
129       /** Total size of the filesystem as \ref ByteCount for convenience. */
130       ByteCount totalSize() const
131       { return ByteCount( total_size, ByteCount::K ); }
132
133       /** Used size of the filesystem as \ref ByteCount for convenience. */
134       ByteCount usedSize() const
135       { return ByteCount( used_size, ByteCount::K ); }
136
137       /** Free size of the filesystem as \ref ByteCount for convenience. */
138       ByteCount freeSize() const
139       { return ByteCount( total_size-used_size, ByteCount::K ); }
140
141       /** Used size after installation as \ref ByteCount for convenience. */
142       ByteCount usedAfterCommit() const
143       { return ByteCount( pkg_size, ByteCount::K ); }
144
145       /** Free size after installation as \ref ByteCount for convenience. */
146       ByteCount freeAfterCommit() const
147       { return ByteCount( total_size-pkg_size, ByteCount::K ); }
148
149       /** Size change due to installation as \ref ByteCount for convenience. */
150       ByteCount commitDiff() const
151       { return ByteCount( pkg_size-used_size, ByteCount::K ); }
152     };
153     ///////////////////////////////////////////////////////////////////
154
155     typedef std::set<MountPoint> MountPointSet;
156
157     DiskUsageCounter()
158     {}
159
160     /** Ctor taking the MountPointSet to compute */
161     DiskUsageCounter( const MountPointSet & mps_r )
162     : _mps( mps_r )
163     {}
164
165     /** Set a MountPointSet to compute */
166     void setMountPoints( const MountPointSet & mps_r )
167     { _mps = mps_r; }
168
169     /** Get the current MountPointSet */
170     const MountPointSet & getMountPoints() const
171     { return _mps; }
172
173
174     /** Get mountpoints of system below \a rootdir */
175     static MountPointSet detectMountPoints( const std::string & rootdir = "/" );
176
177     /** Only one entry for "/" to collect total sizes */
178     static MountPointSet justRootPartition();
179
180
181     /** Compute disk usage if the current transaction woud be commited. */
182     MountPointSet disk_usage( const ResPool & pool ) const;
183
184     /** Compute disk usage of a single Solvable */
185     MountPointSet disk_usage( sat::Solvable solv_r ) const;
186     /** \overload for PoolItem */
187     MountPointSet disk_usage( const PoolItem & pi_r  ) const
188     { return disk_usage( sat::asSolvable()( pi_r ) ); }
189     /** \overload for ResObject */
190     MountPointSet disk_usage( const ResObject::constPtr & obj_r ) const
191     { return disk_usage( sat::asSolvable()( obj_r ) ); }
192
193     /** Compute disk usage of a collection defined by a solvable bitmap. */
194     MountPointSet disk_usage( const Bitmap & bitmap_r ) const;
195
196     /** Compute disk usage of a collection (convertible by \ref asSolvable). */
197     template<class Iterator>
198     MountPointSet disk_usage( Iterator begin_r, Iterator end_r ) const
199     {
200       Bitmap bitmap( Bitmap::poolSize );
201       for_( it, begin_r, end_r )
202         bitmap.set( sat::asSolvable()( *it ).id() );
203       return disk_usage( bitmap );
204     }
205
206   private:
207     MountPointSet _mps;
208   };
209   ///////////////////////////////////////////////////////////////////
210
211   ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DiskUsageCounter::MountPoint::HintFlags);
212
213   /** \relates DiskUsageCounter::MountPoint Stream output */
214   std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPoint & obj );
215
216   /** \relates DiskUsageCounter::MountPointSet Stream output */
217   std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPointSet & obj );
218
219   /** \relates DiskUsageCounter Stream output */
220   inline std::ostream & operator<<( std::ostream & str, const DiskUsageCounter & obj )
221   { return str << obj.getMountPoints(); }
222
223   /////////////////////////////////////////////////////////////////
224 } // namespace zypp
225 ///////////////////////////////////////////////////////////////////
226 #endif // ZYPP_DISKUSAGE_COUNTER_H