libzypp  14.48.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 
19 #include "zypp/ResPool.h"
20 #include "zypp/Bitmap.h"
21 #include "zypp/base/Flags.h"
22 
24 namespace zypp
25 {
26 
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 
119 
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  {}
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 
141  bool operator<( const MountPoint & rhs ) const
142  { return dir < rhs.dir; }
143 
146  { return ByteCount( block_size, ByteCount::B ); }
147 
150  { return ByteCount( total_size, ByteCount::K ); }
151 
154  { return ByteCount( used_size, ByteCount::K ); }
155 
159 
162  { return ByteCount( pkg_size, ByteCount::K ); }
163 
166  { return ByteCount( total_size-pkg_size, ByteCount::K ); }
167 
170  { return ByteCount( pkg_size-used_size, ByteCount::K ); }
171  };
173 
174  typedef std::set<MountPoint> MountPointSet;
175 
177  {}
178 
181  : _mps( mps_r )
182  {}
183 
185  void setMountPoints( const MountPointSet & mps_r )
186  { _mps = mps_r; }
187 
190  { return _mps; }
191 
197  static MountPointSet detectMountPoints( const std::string & rootdir = "/" );
198 
201 
202 
204  MountPointSet disk_usage( const ResPool & pool ) const;
205 
207  MountPointSet disk_usage( sat::Solvable solv_r ) const;
209  MountPointSet disk_usage( const PoolItem & pi_r ) const
210  { return disk_usage( sat::asSolvable()( pi_r ) ); }
213  { return disk_usage( sat::asSolvable()( obj_r ) ); }
214 
216  MountPointSet disk_usage( const Bitmap & bitmap_r ) const;
217 
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:
230  };
232 
233  ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DiskUsageCounter::MountPoint::HintFlags);
234 
236  std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPoint & obj );
237 
239  std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPointSet & obj );
240 
242  inline std::ostream & operator<<( std::ostream & str, const DiskUsageCounter & obj )
243  { return str << obj.getMountPoints(); }
244 
246 } // namespace zypp
248 #endif // ZYPP_DISKUSAGE_COUNTER_H
ByteCount blockSize() const
Block size of the filesystem as ByteCount for convenience.
A Solvable object within the sat Pool.
Definition: Solvable.h:55
ByteCount commitDiff() const
Size change due to installation as ByteCount for convenience.
MountPoint(const std::string &d, Hint hint)
ZYPP_DEPRECATED MountPoint(const char *d, long long bs, long long total, long long used, long long pkg, bool ro)
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
ZYPP_DECLARE_OPERATORS_FOR_FLAGS(DiskUsageCounter::MountPoint::HintFlags)
void set(size_type idx_r)
Set bit idx_r.
Definition: Map.cc:79
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.
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
friend std::ostream & operator<<(std::ostream &str, const MountPoint &obj)
TraitsType::constPtrType constPtr
Definition: ResObject.h:50
long long total_size
Total size of the filesystem in KiB (0 if you don't care)
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)
DiskUsageCounter(const MountPointSet &mps_r)
Ctor taking the MountPointSet to compute.
std::string fstype
Filesystem type (provided by detectMountPoints)
long long used_size
Used size of the filesystem in KiB (0 if you don't care)
MountPointSet disk_usage(Iterator begin_r, Iterator end_r) const
Compute disk usage of a collection (convertible by asSolvable).
std::set< MountPoint > MountPointSet
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.
ZYPP_DEPRECATED MountPoint(const std::string &d, long long bs, long long total, long long used, long long pkg, bool ro)
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
ByteCount freeAfterCommit() const
Free size after installation 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.
MountPoint(const char *d, Hint hint)
ByteCount totalSize() const
Total size of the filesystem as ByteCount for convenience.
long long pkg_size
Used size after installation in KiB (computed by DiskUsageCoutner)
static constexpr PoolSizeType poolSize
An object indicating the bitmap should match the current pools capacity.
Definition: Map.h:41
MountPointSet disk_usage(const PoolItem &pi_r) const
Global ResObject pool.
Definition: ResPool.h:48
static const Unit B
1 Byte
Definition: ByteCount.h:42
bool operator<(const MountPoint &rhs) const
Sort by directory name.
Compute disk space occupied by packages across partitions/directories.
MountPoint(const char *d, HintFlags hints)
MountPointSet disk_usage(const ResObject::constPtr &obj_r) const
static const Unit K
1024 Byte
Definition: ByteCount.h:45
Reference to a PoolItem connecting ResObject and ResStatus.
Definition: PoolItem.h:50
ByteCount freeSize() const
Free size of the filesystem as ByteCount for convenience.
MountPoint(const char *d, long long bs, long long total=0LL, long long used=0LL, long long pkg=0LL, HintFlags hints=NoHint)
MountPointSet disk_usage(const ResPool &pool) const
Compute disk usage if the current transaction woud be commited.
bool growonly
hint for growonly partitions (e.g. snapshotting btrfs)
Libsolv (bit)Map wrapper.
Definition: Map.h:33
#define ZYPP_DEPRECATED
The ZYPP_DEPRECATED macro can be used to trigger compile-time warnings with gcc >= 3...
Definition: APIConfig.h:86
To Solvable transform functor.
Definition: Solvable.h:421
const MountPointSet & getMountPoints() const
Get the current MountPointSet.
ByteCount usedSize() const
Used size of the filesystem as ByteCount for convenience.
long long block_size
Block size of the filesystem in B (0 if you don't care)
ByteCount usedAfterCommit() const
Used size after installation as ByteCount for convenience.
void setMountPoints(const MountPointSet &mps_r)
Set a MountPointSet to compute.
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.