libzypp  17.32.5
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/base/Flags.h>
23 
25 namespace zypp
26 {
27 
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  {}
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  {}
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  {}
108  MountPoint( const char * d, HintFlags hints )
109  : MountPoint( std::string(d?d:""), hints )
110  {}
112  MountPoint( const std::string & d, Hint hint )
113  : MountPoint( d, HintFlags(hint) )
114  {}
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 
126  { return ByteCount( block_size, ByteCount::B ); }
127 
130  { return ByteCount( total_size, ByteCount::K ); }
131 
134  { return ByteCount( used_size, ByteCount::K ); }
135 
139 
142  { return ByteCount( pkg_size, ByteCount::K ); }
143 
146  { return ByteCount( total_size-pkg_size, ByteCount::K ); }
147 
150  { return ByteCount( pkg_size-used_size, ByteCount::K ); }
151  };
153 
154  using MountPointSet = std::set<MountPoint>;
155 
157  {}
158 
161  : _mps(std::move( mps_r ))
162  {}
163 
165  void setMountPoints( const MountPointSet & mps_r )
166  { _mps = mps_r; }
167 
170  { return _mps; }
171 
177  static MountPointSet detectMountPoints( const std::string & rootdir = "/" );
178 
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 ) ); }
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:
210  };
212 
213  ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DiskUsageCounter::MountPoint::HintFlags);
214 
216  std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPoint & obj );
217 
219  std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPointSet & obj );
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
static constexpr PoolSizeType poolSize
An object indicating the bitmap should match the current pools capacity.
Definition: Map.h:41
A Solvable object within the sat Pool.
Definition: Solvable.h:53
ByteCount freeAfterCommit() const
Free size after installation as ByteCount for convenience.
MountPoint(const std::string &d, Hint hint)
ByteCount blockSize() const
Block size of the filesystem as ByteCount for convenience.
std::set< MountPoint > MountPointSet
growonly partitions (e.g. snapshotting btrfs)
Store and operate with byte count.
Definition: ByteCount.h:30
static MountPointSet justRootPartition()
Only one entry for "/" to collect total sizes.
bool readonly
hint for readonly partitions
ByteCount usedSize() const
Used size of the filesystem as ByteCount for convenience.
ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DiskUsageCounter::MountPoint::HintFlags)
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
void set(size_type idx_r)
Set bit idx_r.
Definition: Map.cc:81
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
Definition: Arch.h:363
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.
friend std::ostream & operator<<(std::ostream &str, const MountPoint &obj)
long long total_size
Total size of the filesystem in KiB (0 if you don&#39;t care)
bool operator<(const MountPoint &rhs) const
Sort by directory name.
std::ostream & operator<<(std::ostream &str, const DiskUsageCounter &obj)
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)
std::string fstype
Filesystem type (provided by detectMountPoints)
long long used_size
Used size of the filesystem in KiB (0 if you don&#39;t care)
ByteCount totalSize() const
Total size of the filesystem as ByteCount for convenience.
Mount point description If block_size is set DiskUsageCoutner will assume half a block_size is wasted...
std::string dir
Directory name.
TraitsType::constPtrType constPtr
Definition: ResObject.h:43
MountPoint(const char *d, Hint hint)
MountPointSet disk_usage(Iterator begin_r, Iterator end_r) const
Compute disk usage of a collection (convertible by asSolvable).
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.
MountPointSet disk_usage(const ResObject::constPtr &obj_r) const
long long pkg_size
Used size after installation in KiB (computed by DiskUsageCoutner)
const MountPointSet & getMountPoints() const
Get the current MountPointSet.
Global ResObject pool.
Definition: ResPool.h:61
MountPointSet disk_usage(const ResPool &pool) const
Compute disk usage if the current transaction woud be commited.
static const Unit B
1 Byte
Definition: ByteCount.h:42
MountPointSet disk_usage(const PoolItem &pi_r) const
DiskUsageCounter(MountPointSet mps_r)
Ctor taking the MountPointSet to compute.
Compute disk space occupied by packages across partitions/directories.
MountPoint(const char *d, HintFlags hints)
static const Unit K
1024 Byte
Definition: ByteCount.h:45
ByteCount freeSize() const
Free size of the filesystem as ByteCount for convenience.
Combining sat::Solvable and ResStatus.
Definition: PoolItem.h:50
MountPoint(const char *d, long long bs, long long total=0LL, long long used=0LL, long long pkg=0LL, HintFlags hints=NoHint)
bool growonly
hint for growonly partitions (e.g. snapshotting btrfs)
Libsolv (bit)Map wrapper.
Definition: Map.h:33
To Solvable transform functor.
Definition: Solvable.h:566
ByteCount usedAfterCommit() const
Used size after installation as ByteCount for convenience.
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
long long block_size
Block size of the filesystem in B (0 if you don&#39;t care)
void setMountPoints(const MountPointSet &mps_r)
Set a MountPointSet to compute.
ByteCount commitDiff() const
Size change due to installation as ByteCount for convenience.
static MountPointSet detectMountPoints(const std::string &rootdir="/")
Get mountpoints of system below rootdir If we happen to detect snapshotting btrfs partitions...
MountPoint(const std::string &d, HintFlags hints)
Ctor just name and hints, all sizes 0.