added disk usage data to PackageImplIf interface, provide this info from
authorJiri Srain <jsrain@suse.cz>
Fri, 6 Jan 2006 15:28:58 +0000 (15:28 +0000)
committerJiri Srain <jsrain@suse.cz>
Fri, 6 Jan 2006 15:28:58 +0000 (15:28 +0000)
the RPM database

zypp/Package.h
zypp/detail/PackageImplIf.cc
zypp/detail/PackageImplIf.h
zypp/target/rpm/RpmHeader.cc
zypp/target/rpm/RpmHeader.h
zypp/target/rpm/RpmPackageImpl.cc
zypp/target/rpm/RpmPackageImpl.h

index ba3bc89..b35b1a1 100644 (file)
@@ -33,6 +33,7 @@ namespace zypp
     typedef ResTraits<Self>          TraitsType;
     typedef TraitsType::PtrType      Ptr;
     typedef TraitsType::constPtrType constPtr;
+    typedef Impl::DiskUsage         DiskUsage;
 
   public:
     /** Time of package installation */
index c034bde..baa76ee 100644 (file)
@@ -10,6 +10,9 @@
  *
 */
 #include "zypp/detail/PackageImplIf.h"
+#include <iostream>
+
+using namespace std;
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -83,6 +86,43 @@ 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
   ///////////////////////////////////////////////////////////////////
index c2d4afa..4ac38a0 100644 (file)
@@ -12,6 +12,8 @@
 #ifndef ZYPP_DETAIL_PACKAGEIMPLIF_H
 #define ZYPP_DETAIL_PACKAGEIMPLIF_H
 
+#include <set>
+
 #include "zypp/detail/ResObjectImplIf.h"
 #include "zypp/Edition.h"
 #include "zypp/Arch.h"
@@ -129,22 +131,154 @@ namespace zypp
        std::list<BaseVersion> _base_versions;
       };
 
-#if 0
-      /**
-      * @short Holds data about how much space will be needed per directory
-      **/
-      class DirSize {
+      class DiskUsage {
       public:
-        DirSize();
-        DirSize(const std::string& path,
-                const std::string& size,
-                const std::string& fileCount)
-       : path(path), sizeKByte(size), fileCount(fileCount)
-       {}
-        const std::string path;
-        const std::string sizeKByte;
-        const std::string fileCount;
+       /**
+       * @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<Entry> 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<EntrySet::iterator,bool> 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
     
       /**
        * @short Holds Data about file and file type
index f8a8b0a..9e80ef0 100644 (file)
@@ -14,6 +14,7 @@
 #include <iostream>
 #include <map>
 #include <set>
+#include <vector>
 
 #include "zypp/base/Logger.h"
 #include "zypp/PathInfo.h"
 #include "zypp/target/rpm/RpmHeader.h"
 #include "zypp/CapFactory.h"
 #include "zypp/Rel.h"
-#warning FIXME this include
-#if 0
-#include <y2pm/PkgDu.h>
-#endif
+#include "zypp/Package.h"
 
 using namespace std;
 
@@ -649,8 +647,6 @@ namespace zypp {
         return ret;
       }
      
-#warning FIXME disk usage data
-#if 0 
       ///////////////////////////////////////////////////////////////////
       //
       //
@@ -659,7 +655,7 @@ namespace zypp {
       //
       //        DESCRIPTION :
       //
-      PkgDu & RpmHeader::tag_du( PkgDu & dudata_r ) const
+      Package::DiskUsage & RpmHeader::tag_du( Package::DiskUsage & dudata_r ) const
       {
         dudata_r.clear();
         stringList basenames;
@@ -682,15 +678,15 @@ namespace zypp {
           // Create and collect Entries by index. devino_cache is used to
           // filter out hardliks ( different name but same device and inode ).
           ///////////////////////////////////////////////////////////////////
-          PathInfo::devino_cache trace;
-          vector<PkgDu::Entry> entries;
+          filesystem::DevInoCache trace;
+          vector<Package::DiskUsage::Entry> entries;
           entries.resize( dirnames.size() );
           for ( unsigned i = 0; i < dirnames.size(); ++i ) {
-            entries[i] = dirnames[i];
+            entries[i] = Package::DiskUsage::Entry(dirnames[i]);
           }
       
           for ( unsigned i = 0; i < basenames.size(); ++ i ) {
-            PathInfo::stat_mode mode( filemodes[i] );
+            filesystem::StatMode mode( filemodes[i] );
             if ( mode.isFile() ) {
               if ( trace.insert( filedevices[i], fileinodes[i] ) ) {
                 // Count full 1K blocks
@@ -713,7 +709,6 @@ namespace zypp {
         }
         return dudata_r;
       }
-#endif
 
     } // namespace rpm
   } // namespace target
index 18f64cb..5cd8243 100644 (file)
@@ -26,9 +26,6 @@ namespace zypp {
   namespace target {
     namespace rpm {
 
-#if 0
-      class PkgDu;
-#endif
       
       ///////////////////////////////////////////////////////////////////
       //
@@ -124,13 +121,10 @@ namespace zypp {
       
           Changelog tag_changelog() const;
      
-#warning FIXME disk usage data
-#if 0 
           /**
            * Returns reference to arg <code>dudata_r</code>.
            **/
-          PkgDu & tag_du( PkgDu & dudata_r ) const;
-#endif
+          Package::DiskUsage & tag_du( Package::DiskUsage & dudata_r ) const;
       
         public:
       
index 372f731..b9f2e11 100644 (file)
@@ -322,6 +322,9 @@ namespace zypp
       /** */
       bool RPMPackageImpl::prefererCandidate() const
       {}
+      /** */
+      DiskUsage RPMPackageImpl::diskUsage() const
+      { return _disk_usage; }
 
 #endif
 
index 66e5de5..3fd5d57 100644 (file)
@@ -95,6 +95,8 @@ namespace zypp
         virtual std::string type() const;
         /** */
         virtual std::list<std::string> keywords() const;
+        /** */
+        virtual DiskUsage diskUsage() const;
 
       protected:
        Label _summary;
@@ -112,6 +114,7 @@ namespace zypp
        Text _authors;
        std::list<std::string>_keywords;
        Text _filenames;
+       DiskUsage _disk_usage;
        };
       ///////////////////////////////////////////////////////////////////
     } // namespace rpm