libzypp: zypp/DiskUsageCounter.h Source File

libzypp 17.38.14

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#include <utility>
19
20#include <zypp/ResPool.h>
21#include <zypp/Bitmap.h>
22#include <zypp-core/base/Flags.h>
23
25namespace zypp
26{
27
32 class ZYPP_API DiskUsageCounter
33 {
34
35 public:
43 struct MountPoint
44 {
45 friend std::ostream & operator<<( std::ostream & str, const MountPoint & obj );
46 std::string dir;
47 std::string fstype;
48 long long block_size;
49 long long total_size;
50 long long used_size;
51 mutable long long pkg_size;
52 // hint bits:
53 bool readonly:1;
54 bool growonly:1;
55
56
58 enum Hint
59 {
60 NoHint = 0,
61 Hint_readonly = (1<<0),
62 Hint_growonly = (1<<1),
63 };
64 ZYPP_DECLARE_FLAGS(HintFlags,Hint);
65
67 MountPoint( std::string d = "/",
68 std::string f = std::string(),
69 long long bs = 0LL, long long total = 0LL, long long used = 0LL, long long pkg = 0LL,
70 HintFlags hints = NoHint )
71 : dir(std::move(d)), fstype(std::move(f))
72 , block_size(bs), total_size(total), used_size(used), pkg_size(pkg)
73 , readonly(hints.testFlag(Hint_readonly))
74 , growonly(hints.testFlag(Hint_growonly))
75 {}
76
81 MountPoint( const char * d,
82 const std::string & f = std::string(),
83 long long bs = 0LL, long long total = 0LL, long long used = 0LL, long long pkg = 0LL,
84 HintFlags hints = NoHint )
85 : MountPoint( std::string(d?d:""), f, bs, total, used, pkg, hints )
86 {}
87
88
90 MountPoint( const std::string & d,
91 long long bs, long long total = 0LL, long long used = 0LL, long long pkg = 0LL,
92 HintFlags hints = NoHint )
93 : MountPoint( d, std::string(), bs, total, used, pkg, hints )
94 {}
95
96 MountPoint( const char * d,
97 long long bs, long long total = 0LL, long long used = 0LL, long long pkg = 0LL,
98 HintFlags hints = NoHint )
99 : MountPoint( std::string(d?d:""), bs, total, used, pkg, hints )
100 {}
101
102
104 MountPoint( const std::string & d, HintFlags hints )
105 : MountPoint( d, std::string(), 0LL, 0LL, 0LL, 0LL, hints )
106 {}
107
108 MountPoint( const char * d, HintFlags hints )
109 : MountPoint( std::string(d?d:""), hints )
110 {}
111
112 MountPoint( const std::string & d, Hint hint )
113 : MountPoint( d, HintFlags(hint) )
114 {}
115
116 MountPoint( const char * d, Hint hint )
117 : MountPoint( std::string(d?d:""), HintFlags(hint) )
118 {}
119
121 bool operator<( const MountPoint & rhs ) const
122 { return dir < rhs.dir; }
123
125 ByteCount blockSize() const
126 { return ByteCount( block_size, ByteCount::B ); }
127
129 ByteCount totalSize() const
130 { return ByteCount( total_size, ByteCount::K ); }
131
133 ByteCount usedSize() const
134 { return ByteCount( used_size, ByteCount::K ); }
135
137 ByteCount freeSize() const
138 { return ByteCount( total_size-used_size, ByteCount::K ); }
139
141 ByteCount usedAfterCommit() const
142 { return ByteCount( pkg_size, ByteCount::K ); }
143
145 ByteCount freeAfterCommit() const
146 { return ByteCount( total_size-pkg_size, ByteCount::K ); }
147
149 ByteCount commitDiff() const
150 { return ByteCount( pkg_size-used_size, ByteCount::K ); }
151 };
152
153
154 using MountPointSet = std::set<MountPoint>;
155
156 DiskUsageCounter()
157 {}
158
160 DiskUsageCounter( MountPointSet mps_r )
161 : _mps(std::move( mps_r ))
162 {}
163
165 void setMountPoints( const MountPointSet & mps_r )
166 { _mps = mps_r; }
167
169 const MountPointSet & getMountPoints() const
170 { return _mps; }
171
177 static MountPointSet detectMountPoints( const std::string & rootdir = "/" );
178
180 static MountPointSet justRootPartition();
181
182
184 MountPointSet disk_usage( const ResPool & pool ) const;
185
187 MountPointSet disk_usage( sat::Solvable solv_r ) const;
189 MountPointSet disk_usage( const PoolItem & pi_r ) const
190 { return disk_usage( sat::asSolvable()( pi_r ) ); }
191
192 MountPointSet disk_usage( const ResObject::constPtr & obj_r ) const
193 { return disk_usage( sat::asSolvable()( obj_r ) ); }
194
196 MountPointSet disk_usage( const Bitmap & bitmap_r ) const;
197
199 template<class Iterator>
200 MountPointSet disk_usage( Iterator begin_r, Iterator end_r ) const
201 {
202 Bitmap bitmap( Bitmap::poolSize );
203 for_( it, begin_r, end_r )
204 bitmap.set( sat::asSolvable()( *it ).id() );
205 return disk_usage( bitmap );
206 }
207
208 private:
209 MountPointSet _mps;
210 };
211
212
213 ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DiskUsageCounter::MountPoint::HintFlags);
214
216 std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPoint & obj ) ZYPP_API;
217
219 std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPointSet & obj ) ZYPP_API;
220
222 inline std::ostream & operator<<( std::ostream & str, const DiskUsageCounter & obj )
223 { return str << obj.getMountPoints(); }
224
226} // namespace zypp
228#endif // ZYPP_DISKUSAGE_COUNTER_H
Bitmap.h
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
Flags.h
ZYPP_DECLARE_OPERATORS_FOR_FLAGS
#define ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name)
relates: Flags
Definition Flags.h:177
operator<<
std::ostream & operator<<(std::ostream &str, const zypp::sat::detail::CDataiterator *obj)
relates: zypp::sat::LookupAttr::iterator Stream output of the underlying iterator for debug.
Definition LookupAttr.cc:808
ResPool.h
zypp::ByteCount
Store and operate with byte count.
Definition ByteCount.h:32
zypp::ByteCount::K
static const Unit K
1024 Byte
Definition ByteCount.h:46
zypp::ByteCount::B
static const Unit B
1 Byte
Definition ByteCount.h:43
zypp::DiskUsageCounter
Compute disk space occupied by packages across partitions/directories.
Definition DiskUsageCounter.h:33
zypp::DiskUsageCounter::getMountPoints
const MountPointSet & getMountPoints() const
Get the current MountPointSet.
Definition DiskUsageCounter.h:169
zypp::DiskUsageCounter::_mps
MountPointSet _mps
Definition DiskUsageCounter.h:209
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:200
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:165
zypp::DiskUsageCounter::DiskUsageCounter
DiskUsageCounter()
Definition DiskUsageCounter.h:156
zypp::DiskUsageCounter::disk_usage
MountPointSet disk_usage(const PoolItem &pi_r) const
Definition DiskUsageCounter.h:189
zypp::DiskUsageCounter::MountPointSet
std::set< MountPoint > MountPointSet
Definition DiskUsageCounter.h:154
zypp::DiskUsageCounter::disk_usage
MountPointSet disk_usage(const ResObject::constPtr &obj_r) const
Definition DiskUsageCounter.h:192
zypp::DiskUsageCounter::DiskUsageCounter
DiskUsageCounter(MountPointSet mps_r)
Ctor taking the MountPointSet to compute.
Definition DiskUsageCounter.h:160
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:62
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 ansi.h:855
str
String related utilities and Regular expression matching.
zypp::pool
Definition ByIdent.h:24
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::Bitmap
sat::Map Bitmap
Definition Bitmap.h:19
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition Capabilities.cc:65
zypp::DiskUsageCounter::MountPoint
Mount point description If block_size is set DiskUsageCoutner will assume half a block_size is wasted...
Definition DiskUsageCounter.h:44
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:90
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:50
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const std::string &d, Hint hint)
Definition DiskUsageCounter.h:112
zypp::DiskUsageCounter::MountPoint::commitDiff
ByteCount commitDiff() const
Size change due to installation as ByteCount for convenience.
Definition DiskUsageCounter.h:149
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const std::string &d, HintFlags hints)
Ctor just name and hints, all sizes 0.
Definition DiskUsageCounter.h:104
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:133
zypp::DiskUsageCounter::MountPoint::freeSize
ByteCount freeSize() const
Free size of the filesystem as ByteCount for convenience.
Definition DiskUsageCounter.h:137
zypp::DiskUsageCounter::MountPoint::Hint
Hint
HinFlags for ctor.
Definition DiskUsageCounter.h:59
zypp::DiskUsageCounter::MountPoint::Hint_readonly
@ Hint_readonly
readonly partitions
Definition DiskUsageCounter.h:61
zypp::DiskUsageCounter::MountPoint::NoHint
@ NoHint
Definition DiskUsageCounter.h:60
zypp::DiskUsageCounter::MountPoint::Hint_growonly
@ Hint_growonly
growonly partitions (e.g. snapshotting btrfs)
Definition DiskUsageCounter.h:62
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:81
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:48
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const char *d, HintFlags hints)
Definition DiskUsageCounter.h:108
zypp::DiskUsageCounter::MountPoint::freeAfterCommit
ByteCount freeAfterCommit() const
Free size after installation as ByteCount for convenience.
Definition DiskUsageCounter.h:145
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:49
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:96
zypp::DiskUsageCounter::MountPoint::readonly
bool readonly
hint for readonly partitions
Definition DiskUsageCounter.h:53
zypp::DiskUsageCounter::MountPoint::dir
std::string dir
Directory name.
Definition DiskUsageCounter.h:46
zypp::DiskUsageCounter::MountPoint::blockSize
ByteCount blockSize() const
Block size of the filesystem as ByteCount for convenience.
Definition DiskUsageCounter.h:125
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(const char *d, Hint hint)
Definition DiskUsageCounter.h:116
zypp::DiskUsageCounter::MountPoint::pkg_size
long long pkg_size
Used size after installation in KiB (computed by DiskUsageCoutner).
Definition DiskUsageCounter.h:51
zypp::DiskUsageCounter::MountPoint::growonly
bool growonly
hint for growonly partitions (e.g. snapshotting btrfs)
Definition DiskUsageCounter.h:54
zypp::DiskUsageCounter::MountPoint::operator<
bool operator<(const MountPoint &rhs) const
Sort by directory name.
Definition DiskUsageCounter.h:121
zypp::DiskUsageCounter::MountPoint::usedAfterCommit
ByteCount usedAfterCommit() const
Used size after installation as ByteCount for convenience.
Definition DiskUsageCounter.h:141
zypp::DiskUsageCounter::MountPoint::MountPoint
MountPoint(std::string d="/", 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:67
zypp::DiskUsageCounter::MountPoint::fstype
std::string fstype
Filesystem type (provided by detectMountPoints).
Definition DiskUsageCounter.h:47
zypp::DiskUsageCounter::MountPoint::totalSize
ByteCount totalSize() const
Total size of the filesystem as ByteCount for convenience.
Definition DiskUsageCounter.h:129
zypp::sat::asSolvable
To Solvable transform functor.
Definition Solvable.h:598