224bac1f2c4ad21a90d332d5b3821eba41b169b5
[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       std::string fstype;               ///< Filesystem type  (provided by \ref detectMountPoints)
47       long long block_size;             ///< Block size of the filesystem in B (0 if you don't care)
48       long long total_size;             ///< Total size of the filesystem in KiB (0 if you don't care)
49       long long used_size;              ///< Used size of the filesystem in KiB (0 if you don't care)
50       mutable long long pkg_size;       ///< Used size after installation in KiB (computed by \ref DiskUsageCoutner)
51       // hint bits:
52       bool readonly:1;                  ///< hint for readonly partitions
53       bool growonly:1;                  ///< hint for growonly partitions (e.g. snapshotting btrfs)
54
55
56       /** HinFlags for ctor */
57       enum Hint
58       {
59         NoHint          = 0,
60         Hint_readonly   = (1<<0),       ///< readonly partitions
61         Hint_growonly   = (1<<1),       ///< growonly partitions (e.g. snapshotting btrfs)
62       };
63       ZYPP_DECLARE_FLAGS(HintFlags,Hint);
64
65       /** Ctor initialize directory, fstype and sizes */
66       MountPoint( const std::string & d = "/",
67                   const std::string & f = std::string(),
68                   long long bs = 0LL, long long total = 0LL, long long used = 0LL, long long pkg = 0LL,
69                   HintFlags hints = NoHint )
70         : dir(d), fstype(f)
71         , block_size(bs), total_size(total), used_size(used), pkg_size(pkg)
72         , readonly(hints.testFlag(Hint_readonly))
73         , growonly(hints.testFlag(Hint_growonly))
74       {}
75        /** \overload <tt>const char *</tt> to allow e.g. initiailzer lists
76        * \code
77        *   MountPointSet( { "/", "/usr", "/var" } )
78        * \endcode
79        */
80       MountPoint( const char * d,
81                   const std::string & f = std::string(),
82                   long long bs = 0LL, long long total = 0LL, long long used = 0LL, long long pkg = 0LL,
83                   HintFlags hints = NoHint )
84         : MountPoint( std::string(d?d:""), f, bs, total, used, pkg, hints )
85       {}
86
87
88       /** Ctor initialize directory and sizes */
89       MountPoint( const std::string & d,
90                   long long bs, long long total = 0LL, long long used = 0LL, long long pkg = 0LL,
91                   HintFlags hints = NoHint )
92         : MountPoint( d, std::string(), bs, total, used, pkg, hints )
93       {}
94       /** \overload <tt>const char *</tt> */
95       MountPoint( const char * d,
96                   long long bs, long long total = 0LL, long long used = 0LL, long long pkg = 0LL,
97                   HintFlags hints = NoHint )
98         : MountPoint( std::string(d?d:""), bs, total, used, pkg, hints )
99       {}
100
101
102       /** Ctor just name and hints, all sizes 0 */
103       MountPoint( const std::string & d, HintFlags hints )
104         : MountPoint( d, std::string(), 0LL, 0LL, 0LL, 0LL, hints )
105       {}
106       /** \overload <tt>const char *</tt> */
107       MountPoint( const char * d, HintFlags hints )
108         : MountPoint( std::string(d?d:""), hints )
109       {}
110       /** \overload to prevent propagation Hint -> long long */
111       MountPoint( const std::string & d, Hint hint )
112         : MountPoint( d, HintFlags(hint) )
113       {}
114       /** \overload to prevent propagation Hint -> long long */
115       MountPoint( const char * d, Hint hint )
116         : MountPoint( std::string(d?d:""), HintFlags(hint) )
117       {}
118
119
120       /** \deprecated Use HintFlags instead of a trailing 'bool ro' argument.
121        * \code
122        *   -  MountPoint( "/usr", ..., true ); // readonly
123        *   +  MountPoint( "/usr", ..., MountPoint::Hint_readonly );
124        * \endcode
125        */
126       ZYPP_DEPRECATED MountPoint( const std::string & d, long long bs, long long total, long long used, long long pkg, bool ro )
127         : MountPoint( d, bs, total, used, pkg, HintFlags(ro?Hint_readonly:NoHint) )
128       {}
129       /** \deprecated Use HintFlags instead of a trailing 'bool ro' argument.
130        * \code
131        *   -  MountPoint( "/usr", ..., true ); // readonly
132        *   +  MountPoint( "/usr", ..., MountPoint::Hint_readonly );
133        * \endcode
134        */
135       ZYPP_DEPRECATED MountPoint( const char * d, long long bs, long long total, long long used, long long pkg, bool ro )
136         : MountPoint( d, bs, total, used, pkg, HintFlags(ro?Hint_readonly:NoHint) )
137       {}
138
139
140       /** Sort by directory name */
141       bool operator<( const MountPoint & rhs ) const
142       { return dir < rhs.dir; }
143
144       /** Block size of the filesystem as \ref ByteCount for convenience. */
145       ByteCount blockSize() const
146       { return ByteCount( block_size, ByteCount::B ); }
147
148       /** Total size of the filesystem as \ref ByteCount for convenience. */
149       ByteCount totalSize() const
150       { return ByteCount( total_size, ByteCount::K ); }
151
152       /** Used size of the filesystem as \ref ByteCount for convenience. */
153       ByteCount usedSize() const
154       { return ByteCount( used_size, ByteCount::K ); }
155
156       /** Free size of the filesystem as \ref ByteCount for convenience. */
157       ByteCount freeSize() const
158       { return ByteCount( total_size-used_size, ByteCount::K ); }
159
160       /** Used size after installation as \ref ByteCount for convenience. */
161       ByteCount usedAfterCommit() const
162       { return ByteCount( pkg_size, ByteCount::K ); }
163
164       /** Free size after installation as \ref ByteCount for convenience. */
165       ByteCount freeAfterCommit() const
166       { return ByteCount( total_size-pkg_size, ByteCount::K ); }
167
168       /** Size change due to installation as \ref ByteCount for convenience. */
169       ByteCount commitDiff() const
170       { return ByteCount( pkg_size-used_size, ByteCount::K ); }
171     };
172     ///////////////////////////////////////////////////////////////////
173
174     typedef std::set<MountPoint> MountPointSet;
175
176     DiskUsageCounter()
177     {}
178
179     /** Ctor taking the MountPointSet to compute */
180     DiskUsageCounter( const MountPointSet & mps_r )
181     : _mps( mps_r )
182     {}
183
184     /** Set a MountPointSet to compute */
185     void setMountPoints( const MountPointSet & mps_r )
186     { _mps = mps_r; }
187
188     /** Get the current MountPointSet */
189     const MountPointSet & getMountPoints() const
190     { return _mps; }
191
192     /** Get mountpoints of system below \a rootdir
193      * If we happen to detect snapshotting btrfs partitions, the MountPoint::growonly
194      * hint is set. Disk usage computation will assume that deleted packages will not
195      * free any space (kept in a snapshot).
196      */
197     static MountPointSet detectMountPoints( const std::string & rootdir = "/" );
198
199     /** Only one entry for "/" to collect total sizes */
200     static MountPointSet justRootPartition();
201
202
203     /** Compute disk usage if the current transaction woud be commited. */
204     MountPointSet disk_usage( const ResPool & pool ) const;
205
206     /** Compute disk usage of a single Solvable */
207     MountPointSet disk_usage( sat::Solvable solv_r ) const;
208     /** \overload for PoolItem */
209     MountPointSet disk_usage( const PoolItem & pi_r  ) const
210     { return disk_usage( sat::asSolvable()( pi_r ) ); }
211     /** \overload for ResObject */
212     MountPointSet disk_usage( const ResObject::constPtr & obj_r ) const
213     { return disk_usage( sat::asSolvable()( obj_r ) ); }
214
215     /** Compute disk usage of a collection defined by a solvable bitmap. */
216     MountPointSet disk_usage( const Bitmap & bitmap_r ) const;
217
218     /** Compute disk usage of a collection (convertible by \ref asSolvable). */
219     template<class Iterator>
220     MountPointSet disk_usage( Iterator begin_r, Iterator end_r ) const
221     {
222       Bitmap bitmap( Bitmap::poolSize );
223       for_( it, begin_r, end_r )
224         bitmap.set( sat::asSolvable()( *it ).id() );
225       return disk_usage( bitmap );
226     }
227
228   private:
229     MountPointSet _mps;
230   };
231   ///////////////////////////////////////////////////////////////////
232
233   ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DiskUsageCounter::MountPoint::HintFlags);
234
235   /** \relates DiskUsageCounter::MountPoint Stream output */
236   std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPoint & obj );
237
238   /** \relates DiskUsageCounter::MountPointSet Stream output */
239   std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPointSet & obj );
240
241   /** \relates DiskUsageCounter Stream output */
242   inline std::ostream & operator<<( std::ostream & str, const DiskUsageCounter & obj )
243   { return str << obj.getMountPoints(); }
244
245   /////////////////////////////////////////////////////////////////
246 } // namespace zypp
247 ///////////////////////////////////////////////////////////////////
248 #endif // ZYPP_DISKUSAGE_COUNTER_H