1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/DiskUsageCounter.h
12 #ifndef ZYPP_DISKUSAGE_COUNTER_H
13 #define ZYPP_DISKUSAGE_COUNTER_H
19 #include "zypp/ResPool.h"
20 #include "zypp/Bitmap.h"
21 #include "zypp/base/Flags.h"
23 ///////////////////////////////////////////////////////////////////
25 { /////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////
28 /// \class DiskUsageCounter
29 /// \brief Compute disk space occupied by packages across partitions/directories
30 ///////////////////////////////////////////////////////////////////
31 class DiskUsageCounter
35 ///////////////////////////////////////////////////////////////////
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 ///////////////////////////////////////////////////////////////////
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)
51 bool readonly:1; ///< hint for readonly partitions
52 bool growonly:1; ///< hint for growonly partitions (e.g. snapshotting btrfs)
55 /** HinFlags for ctor */
59 Hint_readonly = (1<<0), ///< readonly partitions
60 Hint_growonly = (1<<1), ///< growonly partitions (e.g. snapshotting btrfs)
62 ZYPP_DECLARE_FLAGS(HintFlags,Hint);
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 )
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))
74 /** \overload <tt>const char *</tt> to allow e.g. initiailzer lists
76 * MountPointSet( { "/", "/usr", "/var" } )
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 )
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 )
87 /** \overload just name and hints, all sizes 0 */
88 MountPoint( const char * d, HintFlags hints )
89 : MountPoint( std::string(d?d:""), hints )
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) )
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) )
101 /** \deprecated Use HintFlags instead of a trailing 'bool ro' argument.
103 * - MountPoint( "/usr", ..., true ); // readonly
104 * + MountPoint( "/usr", ..., MountPoint::Hint_readonly );
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) )
110 /** \deprecated Use HintFlags instead of a trailing 'bool ro' argument.
112 * - MountPoint( "/usr", ..., true ); // readonly
113 * + MountPoint( "/usr", ..., MountPoint::Hint_readonly );
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) )
121 /** Sort by directory name */
122 bool operator<( const MountPoint & rhs ) const
123 { return dir < rhs.dir; }
125 /** Block size of the filesystem as \ref ByteCount for convenience. */
126 ByteCount blockSize() const
127 { return ByteCount( block_size, ByteCount::B ); }
129 /** Total size of the filesystem as \ref ByteCount for convenience. */
130 ByteCount totalSize() const
131 { return ByteCount( total_size, ByteCount::K ); }
133 /** Used size of the filesystem as \ref ByteCount for convenience. */
134 ByteCount usedSize() const
135 { return ByteCount( used_size, ByteCount::K ); }
137 /** Free size of the filesystem as \ref ByteCount for convenience. */
138 ByteCount freeSize() const
139 { return ByteCount( total_size-used_size, ByteCount::K ); }
141 /** Used size after installation as \ref ByteCount for convenience. */
142 ByteCount usedAfterCommit() const
143 { return ByteCount( pkg_size, ByteCount::K ); }
145 /** Free size after installation as \ref ByteCount for convenience. */
146 ByteCount freeAfterCommit() const
147 { return ByteCount( total_size-pkg_size, ByteCount::K ); }
149 /** Size change due to installation as \ref ByteCount for convenience. */
150 ByteCount commitDiff() const
151 { return ByteCount( pkg_size-used_size, ByteCount::K ); }
153 ///////////////////////////////////////////////////////////////////
155 typedef std::set<MountPoint> MountPointSet;
160 /** Ctor taking the MountPointSet to compute */
161 DiskUsageCounter( const MountPointSet & mps_r )
165 /** Set a MountPointSet to compute */
166 void setMountPoints( const MountPointSet & mps_r )
169 /** Get the current MountPointSet */
170 const MountPointSet & getMountPoints() const
174 /** Get mountpoints of system below \a rootdir */
175 static MountPointSet detectMountPoints( const std::string & rootdir = "/" );
177 /** Only one entry for "/" to collect total sizes */
178 static MountPointSet justRootPartition();
181 /** Compute disk usage if the current transaction woud be commited. */
182 MountPointSet disk_usage( const ResPool & pool ) const;
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 ) ); }
193 /** Compute disk usage of a collection defined by a solvable bitmap. */
194 MountPointSet disk_usage( const Bitmap & bitmap_r ) const;
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
200 Bitmap bitmap( Bitmap::poolSize );
201 for_( it, begin_r, end_r )
202 bitmap.set( sat::asSolvable()( *it ).id() );
203 return disk_usage( bitmap );
209 ///////////////////////////////////////////////////////////////////
211 ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DiskUsageCounter::MountPoint::HintFlags);
213 /** \relates DiskUsageCounter::MountPoint Stream output */
214 std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPoint & obj );
216 /** \relates DiskUsageCounter::MountPointSet Stream output */
217 std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPointSet & obj );
219 /** \relates DiskUsageCounter Stream output */
220 inline std::ostream & operator<<( std::ostream & str, const DiskUsageCounter & obj )
221 { return str << obj.getMountPoints(); }
223 /////////////////////////////////////////////////////////////////
225 ///////////////////////////////////////////////////////////////////
226 #endif // ZYPP_DISKUSAGE_COUNTER_H