Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

PkgDu.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                        (C) SuSE GmbH |
00011 \----------------------------------------------------------------------/
00012 
00013   File:       PkgDu.h
00014 
00015   Author:     Michael Andres <ma@suse.de>
00016   Maintainer: Michael Andres <ma@suse.de>
00017 
00018   Purpose: Helper classes to collect package disk usage info.
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 //      CLASS NAME : PkgDu
00064 class PkgDu {
00065 
00066   public:
00067 
00069     //
00070     //  CLASS NAME : PkgDu::Entry
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         // whether _dirname has prefix rhs._dirname
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 //      CLASS NAME : PkgDuMaster
00282 class PkgDuMaster {
00283 
00284   //PkgDuMaster & operator=( const PkgDuMaster & ); // no assign
00285   //PkgDuMaster            ( const PkgDuMaster & ); // no copy
00286 
00287   public:
00288 
00290     //
00291     //  CLASS NAME : PkgDuMaster::MountPoint
00292     //
00293     class MountPoint {
00294       public:
00295         // vital data (must not be modified)
00296         const std::string _mountpoint;
00297         const FSize       _blocksize;
00298       public:
00299         // additional MountPoint data
00300         mutable FSize     _total;
00301         mutable FSize     _used;
00302       public:
00303         // statistics counted by add/sub
00304         // contains the change of diskusage
00305         mutable FSize     _pkgusage;
00306       public:
00307         const std::string & mountpoint()        const { return _mountpoint; }
00308         FSize               total()             const { return _total; }
00309         // current usage without packages taken into accout
00310         FSize               initial_used()      const { return _used; }
00311         FSize               initial_available() const { return total() - initial_used(); }
00312         int                 initial_u_percent() const { return( total() ? initial_used() * 100 / total() : 0 ); }
00313         // current usage with packages taken into accout
00314         FSize               pkg_diff()          const { return _pkgusage; }
00315         FSize               pkg_used()          const { return _used + _pkgusage; }
00316         FSize               pkg_available()     const { return total() - pkg_used(); }
00317         int                 pkg_u_percent()     const { return( total() ? pkg_used() * 100 / total() : 0 ); }
00318       public:
00319         MountPoint( const std::string & mountpoint_r,
00320                     const FSize & blocksize_r = 1024,
00321                     const FSize & total_r = 0,
00322                     const FSize & used_r  = 0 )
00323           : _mountpoint( mountpoint_r )
00324           , _blocksize( blocksize_r )
00325           , _total( total_r )
00326           , _used( used_r )
00327           , _pkgusage( 0 )
00328         {}
00329         ~MountPoint() {}
00330       public:
00331         bool assignData( const MountPoint & rhs ) const;
00332       public:
00333         friend std::ostream & operator<<( std::ostream & str, const MountPoint & obj );
00334       public:
00335         bool operator==( const MountPoint & rhs ) const { return( _mountpoint == rhs._mountpoint ); }
00336         bool operator<( const MountPoint & rhs )  const { return( _mountpoint < rhs._mountpoint ); }
00337     };
00339 
00340   private:
00341 
00342     static unsigned _counter;
00343 
00347     unsigned _count;
00348 
00352     void newcount();
00353 
00354   private:
00355 
00359     std::set<MountPoint> _mountpoints;
00360 
00364     FSize _pkg_diff;
00365 
00369     std::string _src_on;
00370 
00371   private:
00372 
00373     friend class PkgDuSlave;
00374 
00378     void add( FSize * data_r );
00382     void sub( FSize * data_r );
00383 
00384   public:
00385 
00386     PkgDuMaster();
00387     ~PkgDuMaster();
00388 
00389   public:
00390 
00394     unsigned sync_count() const { return _count; }
00395 
00400     unsigned resetStats();
00401 
00405     void setMountPoints( const std::set<MountPoint> & mountpoints_r );
00406 
00410     const std::set<MountPoint> & mountpoints() const { return _mountpoints; }
00411 
00415     FSize pkg_diff() const { return _pkg_diff; }
00416 
00420     void addSrcPkgs( const FSize & srcSize_r );
00421 
00422   public:
00423 
00424     friend std::ostream & operator<<( std::ostream & str, const PkgDuMaster & obj );
00425     friend std::ostream & operator<<( std::ostream & str, const std::set<MountPoint> & obj );
00426 };
00427 
00429 
00431 //
00432 //      CLASS NAME : PkgDuSlave
00436 class PkgDuSlave {
00437 
00438   PkgDuSlave & operator=( const PkgDuSlave & ); // no assign
00439   PkgDuSlave            ( const PkgDuSlave & ); // no copy
00440 
00441   private:
00442 
00443     friend class PMPackage;
00444 
00445     PkgDuSlave();
00446     ~PkgDuSlave();
00447 
00448     typedef PkgDuMaster::MountPoint MountPoint;
00449 
00450   private:
00451 
00455     mutable unsigned _count;
00456 
00460     mutable FSize * _data;
00461 
00466     bool sync( const PMPackage & pkg_r, PkgDuMaster & master_r ) const;
00467 
00472     bool add( const PMPackage & pkg_r, PkgDuMaster & master_r ) const;
00473 
00478     bool sub( const PMPackage & pkg_r, PkgDuMaster & master_r ) const;
00479 };
00480 
00482 
00483 #endif // PkgDu_h

Generated on Fri Nov 9 14:30:31 2007 for yast2-packagemanager by doxygen 1.3.6