00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef PkgDu_h
00022 #define PkgDu_h
00023
00024 #include <iosfwd>
00025 #include <string>
00026 #include <list>
00027 #include <set>
00028
00029 #include <y2util/FSize.h>
00030
00031 #include <y2pm/PMPackagePtr.h>
00032
00034
00035
00064 class PkgDu {
00065
00066 public:
00067
00069
00070
00074 struct Entry {
00075
00076 friend std::ostream & operator<<( std::ostream & str, const Entry & obj );
00077
00081 std::string _dirname;
00085 mutable FSize _size;
00089 mutable unsigned _files;
00090
00094 Entry( const std::string & dirname_r, const FSize & size_r = 0, const unsigned & files_r = 0 )
00095 : _dirname( dirname_r ), _size( size_r ), _files( files_r )
00096 {
00097 if ( _dirname.size() && _dirname[_dirname.size()-1] != '/' ) {
00098 _dirname += '/';
00099 }
00100 }
00101
00105 Entry() : _files( 0 ) {}
00109 bool operator==( const Entry & rhs ) const {
00110 return _dirname == rhs._dirname;
00111 }
00115 bool operator<( const Entry & rhs ) const {
00116 return _dirname < rhs._dirname;
00117 }
00118
00122 bool isBelow( const Entry & rhs ) const {
00123
00124 return( _dirname.compare( 0, rhs._dirname.size(), rhs._dirname ) == 0 );
00125 }
00129 bool isBelow( const std::string & dirname_r ) const {
00130 return isBelow( Entry( dirname_r ) );
00131 }
00132
00136 const Entry & operator+=( const Entry & rhs ) const {
00137 _size += rhs._size;
00138 _files += rhs._files;
00139 return *this;
00140 }
00144 const Entry & operator-=( const Entry & rhs ) const {
00145 _size -= rhs._size;
00146 _files -= rhs._files;
00147 return *this;
00148 }
00149 };
00150
00152
00153 private:
00154
00155 typedef std::set<Entry> EntrySet;
00156
00157 EntrySet _dirs;
00158
00159 public:
00160
00164 PkgDu() {}
00165
00169 void add( const Entry & newent_r ) {
00170 std::pair<EntrySet::iterator,bool> res = _dirs.insert( newent_r );
00171 if ( !res.second ) {
00172 *res.first += newent_r;
00173 }
00174 }
00175
00179 void add( const std::string & dirname_r, const FSize & size_r = 0, const unsigned & files_r = 0 ) {
00180 add( Entry( dirname_r, size_r, files_r ) );
00181 }
00182
00186 unsigned size() const { return _dirs.size(); }
00187
00191 void clear() { _dirs.clear(); }
00192
00193 public:
00194
00198 void addFrom( const std::list<std::string> & dudata_r );
00199
00203 void setFrom( const std::list<std::string> & dudata_r ) {
00204 clear();
00205 addFrom( dudata_r );
00206 }
00207
00208 public:
00209
00214 Entry extract( const std::string & dirname_r );
00215
00216 public:
00217
00218 typedef EntrySet::iterator iterator;
00219 typedef EntrySet::reverse_iterator reverse_iterator;
00220
00224 iterator begin() { return _dirs.begin(); }
00228 iterator end() { return _dirs.end(); }
00232 reverse_iterator rbegin() { return _dirs.rbegin(); }
00236 reverse_iterator rend() { return _dirs.rend(); }
00237
00238 typedef EntrySet::const_iterator const_iterator;
00239 typedef EntrySet::const_reverse_iterator const_reverse_iterator;
00240
00244 const_iterator begin() const { return _dirs.begin(); }
00248 const_iterator end() const { return _dirs.end(); }
00252 const_reverse_iterator rbegin() const { return _dirs.rbegin(); }
00256 const_reverse_iterator rend()const { return _dirs.rend(); }
00257
00258 public:
00259
00260 friend std::ostream & operator<<( std::ostream & str, const PkgDu & obj );
00261 };
00262
00264
00266
00267
00282 class PkgDuMaster {
00283
00284
00285
00286
00287 public:
00288
00290
00291
00292
00293 class MountPoint {
00294 public:
00295
00296 const std::string _mountpoint;
00297 const FSize _blocksize;
00298 public:
00299
00300 mutable FSize _total;
00301 mutable FSize _used;
00302 mutable bool _readonly;
00303 public:
00304
00305
00306 mutable FSize _pkgusage;
00307 public:
00308 const std::string & mountpoint() const { return _mountpoint; }
00309 FSize total() const { return _total; }
00310 bool readonly() const { return _readonly; }
00311
00312 FSize initial_used() const { return _used; }
00313 FSize initial_available() const { return total() - initial_used(); }
00314 int initial_u_percent() const { return( total() ? initial_used() * 100 / total() : 0 ); }
00315
00316 FSize pkg_diff() const { return _pkgusage; }
00317 FSize pkg_used() const { return _used + _pkgusage; }
00318 FSize pkg_available() const { return total() - pkg_used(); }
00319 int pkg_u_percent() const { return( total() ? pkg_used() * 100 / total() : 0 ); }
00320 public:
00321 MountPoint( const std::string & mountpoint_r,
00322 const FSize & blocksize_r = 1024,
00323 const FSize & total_r = 0,
00324 const FSize & used_r = 0,
00325 bool readonly_r = false )
00326 : _mountpoint( mountpoint_r )
00327 , _blocksize( blocksize_r )
00328 , _total( total_r )
00329 , _used( used_r )
00330 , _readonly( readonly_r )
00331 , _pkgusage( 0 )
00332 {}
00333 ~MountPoint() {}
00334 public:
00335 bool assignData( const MountPoint & rhs ) const;
00336 public:
00337 friend std::ostream & operator<<( std::ostream & str, const MountPoint & obj );
00338 public:
00339 bool operator==( const MountPoint & rhs ) const { return( _mountpoint == rhs._mountpoint ); }
00340 bool operator<( const MountPoint & rhs ) const { return( _mountpoint < rhs._mountpoint ); }
00341 };
00343
00344 private:
00345
00346 static unsigned _counter;
00347
00351 unsigned _count;
00352
00356 void newcount();
00357
00358 private:
00359
00363 std::set<MountPoint> _mountpoints;
00364
00368 FSize _pkg_diff;
00369
00373 std::string _src_on;
00374
00375 private:
00376
00377 friend class PkgDuSlave;
00378
00382 void add( FSize * data_r );
00386 void sub( FSize * data_r );
00387
00388 public:
00389
00390 PkgDuMaster();
00391 ~PkgDuMaster();
00392
00393 public:
00394
00398 unsigned sync_count() const { return _count; }
00399
00404 unsigned resetStats();
00405
00409 void setMountPoints( const std::set<MountPoint> & mountpoints_r );
00410
00414 const std::set<MountPoint> & mountpoints() const { return _mountpoints; }
00415
00419 FSize pkg_diff() const { return _pkg_diff; }
00420
00424 void addSrcPkgs( const FSize & srcSize_r );
00425
00426 public:
00427
00428 friend std::ostream & operator<<( std::ostream & str, const PkgDuMaster & obj );
00429 friend std::ostream & operator<<( std::ostream & str, const std::set<MountPoint> & obj );
00430 };
00431
00433
00435
00436
00440 class PkgDuSlave {
00441
00442 PkgDuSlave & operator=( const PkgDuSlave & );
00443 PkgDuSlave ( const PkgDuSlave & );
00444
00445 private:
00446
00447 friend class PMPackage;
00448
00449 PkgDuSlave();
00450 ~PkgDuSlave();
00451
00452 typedef PkgDuMaster::MountPoint MountPoint;
00453
00454 private:
00455
00459 mutable unsigned _count;
00460
00464 mutable FSize * _data;
00465
00470 bool sync( const PMPackage & pkg_r, PkgDuMaster & master_r ) const;
00471
00476 bool add( const PMPackage & pkg_r, PkgDuMaster & master_r ) const;
00477
00482 bool sub( const PMPackage & pkg_r, PkgDuMaster & master_r ) const;
00483 };
00484
00486
00487 #endif // PkgDu_h