libzypp: zypp/DiskUsageCounter.h Source File

libzypp 17.25.10

DiskUsageCounter.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
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
24 namespace zypp
25 {
26
31 class DiskUsageCounter
32 {
33
34 public:
42 struct MountPoint
43 {
44 friend std::ostream & operator<<( std::ostream & str, const MountPoint & obj );
45 std::string dir;
46 std::string fstype;
47 long long block_size;
48 long long total_size;
49 long long used_size;
50 mutable long long pkg_size;
51 // hint bits:
52 bool readonly:1;
53 bool growonly:1;
54
55
57 enum Hint
58 {
59 NoHint = 0,
60 Hint_readonly = (1<<0),
61 Hint_growonly = (1<<1),
62 };
63 ZYPP_DECLARE_FLAGS(HintFlags,Hint);
64
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 {}
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
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 {}
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
103 MountPoint( const std::string & d, HintFlags hints )
104 : MountPoint( d, std::string(), 0LL, 0LL, 0LL, 0LL, hints )
105 {}
107 MountPoint( const char * d, HintFlags hints )
108 : MountPoint( std::string(d?d:""), hints )
109 {}
111 MountPoint( const std::string & d, Hint hint )
112 : MountPoint( d, HintFlags(hint) )
113 {}
115 MountPoint( const char * d, Hint hint )
116 : MountPoint( std::string(d?d:""), HintFlags(hint) )
117 {}
118
120 bool operator<( const MountPoint & rhs ) const
121 { return dir < rhs.dir; }
122
124 ByteCount blockSize() const
125 { return ByteCount( block_size, ByteCount::B ); }
126
128 ByteCount totalSize() const
129 { return ByteCount( total_size, ByteCount::K ); }
130
132 ByteCount usedSize() const
133 { return ByteCount( used_size, ByteCount::K ); }
134
136 ByteCount freeSize() const
137 { return ByteCount( total_size-used_size, ByteCount::K ); }
138
140 ByteCount usedAfterCommit() const
141 { return ByteCount( pkg_size, ByteCount::K ); }
142
144 ByteCount freeAfterCommit() const
145 { return ByteCount( total_size-pkg_size, ByteCount::K ); }
146
148 ByteCount commitDiff() const
149 { return ByteCount( pkg_size-used_size, ByteCount::K ); }
150 };
152
153 typedef std::set<MountPoint> MountPointSet;
154
155 DiskUsageCounter()
156 {}
157
159 DiskUsageCounter( const MountPointSet & mps_r )
160 : _mps( mps_r )
161 {}
162
164 void setMountPoints( const MountPointSet & mps_r )
165 { _mps = mps_r; }
166
168 const MountPointSet & getMountPoints() const
169 { return _mps; }
170
176 static MountPointSet detectMountPoints( const std::string & rootdir = "/" );
177
179 static MountPointSet justRootPartition();
180
181
183 MountPointSet disk_usage( const ResPool & pool ) const;
184
186 MountPointSet disk_usage( sat::Solvable solv_r ) const;
188 MountPointSet disk_usage( const PoolItem & pi_r ) const
189 { return disk_usage( sat::asSolvable()( pi_r ) ); }
191 MountPointSet disk_usage( const ResObject::constPtr & obj_r ) const
192 { return disk_usage( sat::asSolvable()( obj_r ) ); }
193
195 MountPointSet disk_usage( const Bitmap & bitmap_r ) const;
196
198 template<class Iterator>
199 MountPointSet disk_usage( Iterator begin_r, Iterator end_r ) const
200 {
201 Bitmap bitmap( Bitmap::poolSize );
202 for_( it, begin_r, end_r )
203 bitmap.set( sat::asSolvable()( *it ).id() );
204 return disk_usage( bitmap );
205 }
206
207 private:
208 MountPointSet _mps;
209 };
211
212 ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DiskUsageCounter::MountPoint::HintFlags);
213
215 std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPoint & obj );
216
218 std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPointSet & obj );
219
221 inline std::ostream & operator<<( std::ostream & str, const DiskUsageCounter & obj )
222 { return str << obj.getMountPoints(); }
223
225 } // namespace zypp
227 #endif // ZYPP_DISKUSAGE_COUNTER_H
Bitmap.h
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
Flags.h
ResPool.h
zypp::ByteCount
Store and operate with byte count.
Definition: ByteCount.h:31
zypp::ByteCount::K
static const Unit K
1024 Byte
Definition: ByteCount.h:45
zypp::ByteCount::B
static const Unit B
1 Byte
Definition: ByteCount.h:42
zypp::DiskUsageCounter
Compute disk space occupied by packages across partitions/directories.
Definition: DiskUsageCounter.h:32
zypp::DiskUsageCounter::justRootPartition
static MountPointSet justRootPartition()
Only one entry for "/" to collect total sizes.
Definition: DiskUsageCounter.cc:337
zypp::DiskUsageCounter::_mps
MountPointSet _mps
Definition: DiskUsageCounter.h:208
zypp::DiskUsageCounter::disk_usage
MountPointSet disk_usage(Iterator begin_r, Iterator end_r) const
Compute disk usage of a collection (convertible by asSolvable).
Definition: DiskUsageCounter.h:199
zypp::DiskUsageCounter::disk_usage
MountPointSet disk_usage(const ResPool &pool) const
Compute disk usage if the current transaction woud be commited.
Definition: DiskUsageCounter.cc:92
zypp::DiskUsageCounter::setMountPoints
void setMountPoints(const MountPointSet &mps_r)
Set a MountPointSet to compute.
Definition: DiskUsageCounter.h:164
zypp::DiskUsageCounter::DiskUsageCounter
DiskUsageCounter()
Definition: DiskUsageCounter.h:155
zypp::DiskUsageCounter::MountPointSet
std::set< MountPoint > MountPointSet
Definition: DiskUsageCounter.h:153
zypp::DiskUsageCounter::detectMountPoints
static MountPointSet detectMountPoints(const std::string &rootdir="/")
Get mountpoints of system below rootdir If we happen to detect snapshotting btrfs partitions,...
Definition: DiskUsageCounter.cc:129
zypp::DiskUsageCounter::disk_usage
MountPointSet disk_usage(const PoolItem &pi_r) const
Definition: DiskUsageCounter.h:188
zypp::DiskUsageCounter::getMountPoints
const MountPointSet & getMountPoints() const
Get the current MountPointSet.
Definition: DiskUsageCounter.h:168
zypp::DiskUsageCounter::disk_usage
MountPointSet disk_usage(const ResObject::constPtr &obj_r) const
Definition: DiskUsageCounter.h:191
zypp::DiskUsageCounter::operator<<
std::ostream & operator<<(std::ostream &str, const DiskUsageCounter &obj)
Stream output.
Definition: DiskUsageCounter.h:221
zypp::DiskUsageCounter::DiskUsageCounter
DiskUsageCounter(const MountPointSet &mps_r)
Ctor taking the MountPointSet to compute.
Definition: DiskUsageCounter.h:159
zypp::PoolItem
Combining sat::Solvable and ResStatus.
Definition: PoolItem.h:51
zypp::ResObject::constPtr
TraitsType::constPtrType constPtr
Definition: ResObject.h:43
zypp::ResPool
Global ResObject pool.
Definition: ResPool.h:61
zypp::sat::Map
Libsolv (bit)Map wrapper.
Definition: Map.h:34
zypp::sat::Map::poolSize
static constexpr PoolSizeType poolSize
An object indicating the bitmap should match the current pools capacity.
Definition: Map.h:41
zypp::sat::Map::set
void set(size_type idx_r)
Set bit idx_r.
Definition: Map.cc:81
zypp::sat::Solvable
A Solvable object within the sat Pool.
Definition: Solvable.h:54
std
Definition: Arch.h:348
str
String related utilities and Regular expression matching.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
zypp::ZYPP_DECLARE_OPERATORS_FOR_FLAGS
ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DiskUsageCounter::MountPoint::HintFlags)
zypp::DiskUsageCounter::MountPoint
Mount point description If block_size is set DiskUsageCoutner will assume half a block_size is wasted...
Definition: DiskUsageCounter.h:43
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const std::string &d, long long bs, long long total=0LL, long long used=0LL, long long pkg=0LL, HintFlags hints=NoHint)
Ctor initialize directory and sizes.
Definition: DiskUsageCounter.h:89
zypp::DiskUsageCounter::MountPoint::used_size
long long used_size
Used size of the filesystem in KiB (0 if you don't care)
Definition: DiskUsageCounter.h:49
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const std::string &d, Hint hint)
Definition: DiskUsageCounter.h:111
zypp::DiskUsageCounter::MountPoint::commitDiff
ByteCount commitDiff() const
Size change due to installation as ByteCount for convenience.
Definition: DiskUsageCounter.h:148
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const std::string &d, HintFlags hints)
Ctor just name and hints, all sizes 0.
Definition: DiskUsageCounter.h:103
zypp::DiskUsageCounter::MountPoint::ZYPP_DECLARE_FLAGS
ZYPP_DECLARE_FLAGS(HintFlags, Hint)
zypp::DiskUsageCounter::MountPoint::usedSize
ByteCount usedSize() const
Used size of the filesystem as ByteCount for convenience.
Definition: DiskUsageCounter.h:132
zypp::DiskUsageCounter::MountPoint::freeSize
ByteCount freeSize() const
Free size of the filesystem as ByteCount for convenience.
Definition: DiskUsageCounter.h:136
zypp::DiskUsageCounter::MountPoint::Hint
Hint
HinFlags for ctor.
Definition: DiskUsageCounter.h:58
zypp::DiskUsageCounter::MountPoint::Hint_readonly
@ Hint_readonly
readonly partitions
Definition: DiskUsageCounter.h:60
zypp::DiskUsageCounter::MountPoint::NoHint
@ NoHint
Definition: DiskUsageCounter.h:59
zypp::DiskUsageCounter::MountPoint::Hint_growonly
@ Hint_growonly
growonly partitions (e.g. snapshotting btrfs)
Definition: DiskUsageCounter.h:61
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const char *d, const std::string &f=std::string(), long long bs=0LL, long long total=0LL, long long used=0LL, long long pkg=0LL, HintFlags hints=NoHint)
Definition: DiskUsageCounter.h:80
zypp::DiskUsageCounter::MountPoint::block_size
long long block_size
Block size of the filesystem in B (0 if you don't care)
Definition: DiskUsageCounter.h:47
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const char *d, HintFlags hints)
Definition: DiskUsageCounter.h:107
zypp::DiskUsageCounter::MountPoint::freeAfterCommit
ByteCount freeAfterCommit() const
Free size after installation as ByteCount for convenience.
Definition: DiskUsageCounter.h:144
zypp::DiskUsageCounter::MountPoint::total_size
long long total_size
Total size of the filesystem in KiB (0 if you don't care)
Definition: DiskUsageCounter.h:48
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const char *d, long long bs, long long total=0LL, long long used=0LL, long long pkg=0LL, HintFlags hints=NoHint)
Definition: DiskUsageCounter.h:95
zypp::DiskUsageCounter::MountPoint::readonly
bool readonly
hint for readonly partitions
Definition: DiskUsageCounter.h:52
zypp::DiskUsageCounter::MountPoint::dir
std::string dir
Directory name.
Definition: DiskUsageCounter.h:45
zypp::DiskUsageCounter::MountPoint::blockSize
ByteCount blockSize() const
Block size of the filesystem as ByteCount for convenience.
Definition: DiskUsageCounter.h:124
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const char *d, Hint hint)
Definition: DiskUsageCounter.h:115
zypp::DiskUsageCounter::MountPoint::pkg_size
long long pkg_size
Used size after installation in KiB (computed by DiskUsageCoutner)
Definition: DiskUsageCounter.h:50
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const std::string &d="/", const std::string &f=std::string(), long long bs=0LL, long long total=0LL, long long used=0LL, long long pkg=0LL, HintFlags hints=NoHint)
Ctor initialize directory, fstype and sizes.
Definition: DiskUsageCounter.h:66
zypp::DiskUsageCounter::MountPoint::growonly
bool growonly
hint for growonly partitions (e.g. snapshotting btrfs)
Definition: DiskUsageCounter.h:53
zypp::DiskUsageCounter::MountPoint::operator<
bool operator<(const MountPoint &rhs) const
Sort by directory name.
Definition: DiskUsageCounter.h:120
zypp::DiskUsageCounter::MountPoint::operator<<
friend std::ostream & operator<<(std::ostream &str, const MountPoint &obj)
Definition: DiskUsageCounter.cc:344
zypp::DiskUsageCounter::MountPoint::usedAfterCommit
ByteCount usedAfterCommit() const
Used size after installation as ByteCount for convenience.
Definition: DiskUsageCounter.h:140
zypp::DiskUsageCounter::MountPoint::fstype
std::string fstype
Filesystem type (provided by detectMountPoints)
Definition: DiskUsageCounter.h:46
zypp::DiskUsageCounter::MountPoint::totalSize
ByteCount totalSize() const
Total size of the filesystem as ByteCount for convenience.
Definition: DiskUsageCounter.h:128
zypp::sat::asSolvable
To Solvable transform functor.
Definition: Solvable.h:529