From 03fbfa6e96f64486850fe7e2e1605231c9690f38 Mon Sep 17 00:00:00 2001 From: Jiri Srain Date: Tue, 10 Jan 2006 09:32:46 +0000 Subject: [PATCH] Moved DiskUsage class out of PackageImplIf --- zypp/DiskUsage.cc | 57 +++++++++++++ zypp/DiskUsage.h | 173 ++++++++++++++++++++++++++++++++++++++ zypp/Makefile.am | 6 +- zypp/Package.h | 1 - zypp/detail/PackageImplIf.cc | 37 -------- zypp/detail/PackageImplIf.h | 147 -------------------------------- zypp/target/rpm/RpmHeader.cc | 6 +- zypp/target/rpm/RpmHeader.h | 3 +- zypp/target/rpm/RpmPackageImpl.cc | 2 +- 9 files changed, 240 insertions(+), 192 deletions(-) create mode 100644 zypp/DiskUsage.cc create mode 100644 zypp/DiskUsage.h diff --git a/zypp/DiskUsage.cc b/zypp/DiskUsage.cc new file mode 100644 index 0000000..31fc0f0 --- /dev/null +++ b/zypp/DiskUsage.cc @@ -0,0 +1,57 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ +/** \file zypp/DiskUsage.cc + * +*/ +#include "zypp/DiskUsage.h" +#include + +using namespace std; + +/////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// + std::ostream & operator<<( std::ostream & str, const DiskUsage::Entry & obj ) + { + return str << obj.path << '\t' << obj._size << "; files " << obj._files; + } + + DiskUsage::Entry DiskUsage::extract( const std::string & dirname_r ) + { + Entry ret( dirname_r ); + + iterator fst = begin(); + for ( ; fst != end() && !fst->isBelow( ret ); ++fst ) + ; // seek 1st equal or below + + if ( fst != end() ) { + iterator lst = fst; + for ( ; lst != end() && lst->isBelow( ret ); ++lst ) { + // collect while below + ret += *lst; + } + // remove + _dirs.erase( fst, lst ); + } + + return ret; + } + + std::ostream & operator<<( std::ostream & str, const DiskUsage & obj ) + { + str << "Package Disk Usage {" << endl; + for ( DiskUsage::EntrySet::const_iterator it = obj._dirs.begin(); it != obj._dirs.end(); ++it ) { + str << " " << *it << endl; + } + return str << "}"; + } + + +} // namespace zypp +/////////////////////////////////////////////////////////////////// diff --git a/zypp/DiskUsage.h b/zypp/DiskUsage.h new file mode 100644 index 0000000..72fa82f --- /dev/null +++ b/zypp/DiskUsage.h @@ -0,0 +1,173 @@ +/*---------------------------------------------------------------------\ +| ____ _ __ __ ___ | +| |__ / \ / / . \ . \ | +| / / \ V /| _/ _/ | +| / /__ | | | | | | | +| /_____||_| |_| |_| | +| | +\---------------------------------------------------------------------*/ +/** \file zypp/DiskUsage.h + * +*/ +#ifndef ZYPP_DISKUSAGE_H +#define ZYPP_DISKUSAGE_H + +#include +#include + +/////////////////////////////////////////////////////////////////// +namespace zypp +{ ///////////////////////////////////////////////////////////////// + + class DiskUsage { + public: + /** + * @short Holds data about how much space will be needed per directory + **/ + struct Entry { + Entry() : _size(0), _files(0) {}; + Entry(const std::string& path_r, + const unsigned size_r = 0, + const unsigned files_r = 0) + : path(path_r), _size(size_r), _files(files_r) + {} + const std::string path; + mutable unsigned _size; + mutable unsigned _files; + friend std::ostream & operator<<( std::ostream & str, const Entry & obj ); + /** + * Test for equality based on directory name. + **/ + bool operator==( const Entry & rhs ) const { + return path == rhs.path; + } + /** + * Order based on directory name. + **/ + bool operator<( const Entry & rhs ) const { + return path < rhs.path; + } + /** + * Return true if this entry denotes a directory equal to or below rhs._dirname. + **/ + bool isBelow( const Entry & rhs ) const { + // whether _dirname has prefix rhs._dirname + return( path.compare( 0, rhs.path.size(), rhs.path ) == 0 ); + } + /** + * Return true if this entry denotes a directory equal to or below dirname_r. + **/ + bool isBelow( const std::string & dirname_r ) const { + return isBelow( Entry( dirname_r ) ); + } + + /** + * + **/ + const Entry & operator=( const Entry & rhs ) const { + return rhs; + } + /** + * Numerical operation based on size and files values. + **/ + const Entry & operator+=( const Entry & rhs ) const { + _size += rhs._size; + _files += rhs._files; + return *this; + } + /** + * Numerical operation based on size and files values. + **/ + const Entry & operator-=( const Entry & rhs ) const { + _size -= rhs._size; + _files -= rhs._files; + return *this; + } + }; + private: + typedef std::set EntrySet; + EntrySet _dirs; + public: + + DiskUsage() {}; + /** + * Add an entry. If already present, sum up the new entries size and files value. + **/ + void add( const Entry & newent_r ) { + std::pair res = _dirs.insert( newent_r ); + if ( !res.second ) { + *res.first += newent_r; + } + } + /** + * Add an entry. If already present, sum up the new entries size and files value. + **/ + void add( const std::string & dirname_r, const unsigned & size_r = 0, const unsigned & files_r = 0 ) { + add( Entry( dirname_r, size_r, files_r ) ); + } + /** + * Number of entries + **/ + unsigned size() const { return _dirs.size(); } + /** + * Clear EntrySet + **/ + void clear() { _dirs.clear(); } + /** + * Sum up any entries for dirname_r and its descendants and remove them + * on the fly. Return the result. + **/ + Entry extract( const std::string & dirname_r ); + + public: + + typedef EntrySet::iterator iterator; + typedef EntrySet::reverse_iterator reverse_iterator; + + /** + * Forward iterator pointing to the first entry (if any) + **/ + iterator begin() { return _dirs.begin(); } + /** + * Forward iterator pointing behind the last entry. + **/ + iterator end() { return _dirs.end(); } + /** + * Reverse iterator pointing to the last entry (if any) + **/ + reverse_iterator rbegin() { return _dirs.rbegin(); } + /** + * Reverse iterator pointing before the first entry. + **/ + reverse_iterator rend() { return _dirs.rend(); } + + typedef EntrySet::const_iterator const_iterator; + typedef EntrySet::const_reverse_iterator const_reverse_iterator; + + /** + * Forward const iterator pointing to the first entry (if any) + **/ + const_iterator begin() const { return _dirs.begin(); } + /** + * Forward const iterator pointing behind the last entry. + **/ + const_iterator end() const { return _dirs.end(); } + /** + * Reverse const iterator pointing to the last entry (if any) + **/ + const_reverse_iterator rbegin() const { return _dirs.rbegin(); } + /** + * Reverse const iterator pointing before the first entry. + **/ + const_reverse_iterator rend()const { return _dirs.rend(); } + + public: + + friend std::ostream & operator<<( std::ostream & str, const DiskUsage & obj ); + + }; + /////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////// +} // namespace zypp +/////////////////////////////////////////////////////////////////// +#endif // ZYPP_DISKUSAGE_H diff --git a/zypp/Makefile.am b/zypp/Makefile.am index d0d77a6..3763d0e 100644 --- a/zypp/Makefile.am +++ b/zypp/Makefile.am @@ -40,7 +40,8 @@ include_HEADERS = NeedAType.h \ Url.h \ TmpPath.h \ KVMap.h \ - Callback.h + Callback.h \ + DiskUsage.h ## ################################################## @@ -80,7 +81,8 @@ lib@PACKAGE@_la_SOURCES = \ PathInfo.cc \ Digest.cc \ Url.cc \ - TmpPath.cc + TmpPath.cc \ + DiskUsage.cc lib@PACKAGE@_la_LDFLAGS = @LIB_VERSION_INFO@ diff --git a/zypp/Package.h b/zypp/Package.h index 232cce7..ce9baa0 100644 --- a/zypp/Package.h +++ b/zypp/Package.h @@ -33,7 +33,6 @@ namespace zypp typedef ResTraits TraitsType; typedef TraitsType::PtrType Ptr; typedef TraitsType::constPtrType constPtr; - typedef Impl::DiskUsage DiskUsage; public: /** Time of package installation */ diff --git a/zypp/detail/PackageImplIf.cc b/zypp/detail/PackageImplIf.cc index 40bcf13..0f386ac 100644 --- a/zypp/detail/PackageImplIf.cc +++ b/zypp/detail/PackageImplIf.cc @@ -86,43 +86,6 @@ namespace zypp License PackageImplIf::licenseToConfirm() const { return License(); } - // disk usage class methods - - std::ostream & operator<<( std::ostream & str, const PackageImplIf::DiskUsage::Entry & obj ) - { - return str << obj.path << '\t' << obj._size << "; files " << obj._files; - } - - PackageImplIf::DiskUsage::Entry PackageImplIf::DiskUsage::extract( const std::string & dirname_r ) - { - Entry ret( dirname_r ); - - iterator fst = begin(); - for ( ; fst != end() && !fst->isBelow( ret ); ++fst ) - ; // seek 1st equal or below - - if ( fst != end() ) { - iterator lst = fst; - for ( ; lst != end() && lst->isBelow( ret ); ++lst ) { - // collect while below - ret += *lst; - } - // remove - _dirs.erase( fst, lst ); - } - - return ret; - } - - std::ostream & operator<<( std::ostream & str, const PackageImplIf::DiskUsage & obj ) - { - str << "Package Disk Usage {" << endl; - for ( PackageImplIf::DiskUsage::EntrySet::const_iterator it = obj._dirs.begin(); it != obj._dirs.end(); ++it ) { - str << " " << *it << endl; - } - return str << "}"; - } - ///////////////////////////////////////////////////////////////// } // namespace detail /////////////////////////////////////////////////////////////////// diff --git a/zypp/detail/PackageImplIf.h b/zypp/detail/PackageImplIf.h index 4ac38a0..752f93f 100644 --- a/zypp/detail/PackageImplIf.h +++ b/zypp/detail/PackageImplIf.h @@ -131,153 +131,6 @@ namespace zypp std::list _base_versions; }; - class DiskUsage { - public: - /** - * @short Holds data about how much space will be needed per directory - **/ - struct Entry { - Entry() : _size(0), _files(0) {}; - Entry(const std::string& path_r, - const unsigned size_r = 0, - const unsigned files_r = 0) - : path(path_r), _size(size_r), _files(files_r) - {} - const std::string path; - mutable unsigned _size; - mutable unsigned _files; - friend std::ostream & operator<<( std::ostream & str, const Entry & obj ); - /** - * Test for equality based on directory name. - **/ - bool operator==( const Entry & rhs ) const { - return path == rhs.path; - } - /** - * Order based on directory name. - **/ - bool operator<( const Entry & rhs ) const { - return path < rhs.path; - } - /** - * Return true if this entry denotes a directory equal to or below rhs._dirname. - **/ - bool isBelow( const Entry & rhs ) const { - // whether _dirname has prefix rhs._dirname - return( path.compare( 0, rhs.path.size(), rhs.path ) == 0 ); - } - /** - * Return true if this entry denotes a directory equal to or below dirname_r. - **/ - bool isBelow( const std::string & dirname_r ) const { - return isBelow( Entry( dirname_r ) ); - } - - /** - * - **/ - const Entry & operator=( const Entry & rhs ) const { - return rhs; - } - /** - * Numerical operation based on size and files values. - **/ - const Entry & operator+=( const Entry & rhs ) const { - _size += rhs._size; - _files += rhs._files; - return *this; - } - /** - * Numerical operation based on size and files values. - **/ - const Entry & operator-=( const Entry & rhs ) const { - _size -= rhs._size; - _files -= rhs._files; - return *this; - } - }; - private: - typedef std::set EntrySet; - EntrySet _dirs; - public: - - DiskUsage() {}; - /** - * Add an entry. If already present, sum up the new entries size and files value. - **/ - void add( const Entry & newent_r ) { - std::pair res = _dirs.insert( newent_r ); - if ( !res.second ) { - *res.first += newent_r; - } - } - /** - * Add an entry. If already present, sum up the new entries size and files value. - **/ - void add( const std::string & dirname_r, const unsigned & size_r = 0, const unsigned & files_r = 0 ) { - add( Entry( dirname_r, size_r, files_r ) ); - } - /** - * Number of entries - **/ - unsigned size() const { return _dirs.size(); } - /** - * Clear EntrySet - **/ - void clear() { _dirs.clear(); } - /** - * Sum up any entries for dirname_r and its descendants and remove them - * on the fly. Return the result. - **/ - Entry extract( const std::string & dirname_r ); - - public: - - typedef EntrySet::iterator iterator; - typedef EntrySet::reverse_iterator reverse_iterator; - - /** - * Forward iterator pointing to the first entry (if any) - **/ - iterator begin() { return _dirs.begin(); } - /** - * Forward iterator pointing behind the last entry. - **/ - iterator end() { return _dirs.end(); } - /** - * Reverse iterator pointing to the last entry (if any) - **/ - reverse_iterator rbegin() { return _dirs.rbegin(); } - /** - * Reverse iterator pointing before the first entry. - **/ - reverse_iterator rend() { return _dirs.rend(); } - - typedef EntrySet::const_iterator const_iterator; - typedef EntrySet::const_reverse_iterator const_reverse_iterator; - - /** - * Forward const iterator pointing to the first entry (if any) - **/ - const_iterator begin() const { return _dirs.begin(); } - /** - * Forward const iterator pointing behind the last entry. - **/ - const_iterator end() const { return _dirs.end(); } - /** - * Reverse const iterator pointing to the last entry (if any) - **/ - const_reverse_iterator rbegin() const { return _dirs.rbegin(); } - /** - * Reverse const iterator pointing before the first entry. - **/ - const_reverse_iterator rend()const { return _dirs.rend(); } - - public: - - friend std::ostream & operator<<( std::ostream & str, const PackageImplIf::DiskUsage & obj ); - - }; #if 0 /** diff --git a/zypp/target/rpm/RpmHeader.cc b/zypp/target/rpm/RpmHeader.cc index 9e80ef0..fe8902e 100644 --- a/zypp/target/rpm/RpmHeader.cc +++ b/zypp/target/rpm/RpmHeader.cc @@ -655,7 +655,7 @@ namespace zypp { // // DESCRIPTION : // - Package::DiskUsage & RpmHeader::tag_du( Package::DiskUsage & dudata_r ) const + DiskUsage & RpmHeader::tag_du( DiskUsage & dudata_r ) const { dudata_r.clear(); stringList basenames; @@ -679,10 +679,10 @@ namespace zypp { // filter out hardliks ( different name but same device and inode ). /////////////////////////////////////////////////////////////////// filesystem::DevInoCache trace; - vector entries; + vector entries; entries.resize( dirnames.size() ); for ( unsigned i = 0; i < dirnames.size(); ++i ) { - entries[i] = Package::DiskUsage::Entry(dirnames[i]); + entries[i] = DiskUsage::Entry(dirnames[i]); } for ( unsigned i = 0; i < basenames.size(); ++ i ) { diff --git a/zypp/target/rpm/RpmHeader.h b/zypp/target/rpm/RpmHeader.h index 5cd8243..b6fb1af 100644 --- a/zypp/target/rpm/RpmHeader.h +++ b/zypp/target/rpm/RpmHeader.h @@ -21,6 +21,7 @@ #include "zypp/Changelog.h" #include "zypp/CapSetFwd.h" #include "zypp/Pathname.h" +#include "zypp/DiskUsage.h" namespace zypp { namespace target { @@ -124,7 +125,7 @@ namespace zypp { /** * Returns reference to arg dudata_r. **/ - Package::DiskUsage & tag_du( Package::DiskUsage & dudata_r ) const; + DiskUsage & tag_du( DiskUsage & dudata_r ) const; public: diff --git a/zypp/target/rpm/RpmPackageImpl.cc b/zypp/target/rpm/RpmPackageImpl.cc index 8756d95..d63498b 100644 --- a/zypp/target/rpm/RpmPackageImpl.cc +++ b/zypp/target/rpm/RpmPackageImpl.cc @@ -187,7 +187,7 @@ namespace zypp { return _keywords; } /** */ - Package::DiskUsage RPMPackageImpl::diskUsage() const + DiskUsage RPMPackageImpl::diskUsage() const { return _disk_usage; } #if 0 /** */ -- 2.7.4