Store DU entries, if they are equal to or parent of a mountpoint. If no
authorMichael Andres <ma@suse.de>
Wed, 12 Sep 2007 15:15:55 +0000 (15:15 +0000)
committerMichael Andres <ma@suse.de>
Wed, 12 Sep 2007 15:15:55 +0000 (15:15 +0000)
mountpoints are available limit DU entries to depth 3.

zypp/parser/susetags/PackagesDuFileReader.cc

index bf6ca76..b508de1 100644 (file)
@@ -13,6 +13,7 @@
 #include "zypp/base/Easy.h"
 #include "zypp/base/Regex.h"
 #include "zypp/base/Logger.h"
+#include "zypp/base/String.h"
 #include "zypp/ZYppFactory.h"
 
 #include "zypp/parser/susetags/PackagesDuFileReader.h"
@@ -42,25 +43,20 @@ namespace zypp
        public:
          Impl( const PackagesDuFileReader & parent_r )
          : BaseImpl( parent_r )
-          , _counted_entries(0)
-          , _discarded_entries(0)
          {
             _mounts = getZYpp()->getPartitions();
-            
+
             for ( DiskUsageCounter::MountPointSet::const_iterator it = _mounts.begin();
                   it != _mounts.end();
                   ++ it )
             {
               MIL << "Partition " << *it << endl;
             }
-          
+
           }
 
          virtual ~Impl()
-         {
-            MIL << "DiskUsage: consumed:[ " << _counted_entries
-                << " ] discarded:[ " << _discarded_entries << " ]" << endl;
-          }
+         {}
 
          bool hasPackage() const
          { return _pkgData; }
@@ -133,58 +129,52 @@ namespace zypp
               if ( str::regex_match( *it, what, sizeEntryRX ) )
               {
                 bool skip = true;
-                
-                // add slash if it's missing
-                std::string dd(what[1]);
-                if (dd.size() > 1 && dd[0] != '/')
-                {
-                 dd.insert(dd.begin(), '/');
-                }
-                
-                DiskUsage::Entry entry(dd,
-                                       str::strtonum<unsigned>(what[2]) + str::strtonum<unsigned>(what[3]),
-                                       str::strtonum<unsigned>(what[4]) + str::strtonum<unsigned>(what[5]) );
-                
+                DiskUsage::Entry entry( what[1],
+                                        str::strtonum<unsigned>(what[2]) + str::strtonum<unsigned>(what[3]),
+                                        str::strtonum<unsigned>(what[4]) + str::strtonum<unsigned>(what[5]) );
+
                 if ( _mounts.empty() )
                 {
-                  // if no mount information, parse it all
-                  // FIXME at least detect levels?
-                  skip = false;
+                  // if no mount information, cut off deeper directory
+                  // levels in DiskUsage::Entry to reduce data size.
+                  unsigned level             = 3; // number of '/' incl. a trailing one
+                  std::string::size_type pos = 0; // we store absolute pathnames
+                  while ( --level && pos != std::string::npos )
+                  {
+                    pos = entry.path.find( "/", pos+1 );
+                  }
+                  if ( pos != std::string::npos )
+                  {
+                    // found 'level' number of '/'es.
+                    skip = ( entry.path[pos] != '\0' );
+                  }
+                  else
+                  {
+                    // less than 'level' number of '/'es.
+                    skip = false;
+                  }
                 }
                 else
                 {
-                  // iterate over important mounts in reverse order, from the leaves to
-                  // the root
+                  // Store entries, if they are equal to or parent of a mountpoint.
                   for ( DiskUsageCounter::MountPointSet::reverse_iterator mit = _mounts.rbegin();
-                    mit != _mounts.rend();
-                    ++ mit )
+                        mit != _mounts.rend();
+                        ++ mit )
                   {
-                    // if the directory we are adding is below one of the mount points
-                    // just add the mount point so it gets summed.
-                    //MIL << "is '" << entry.path << "' == '" << (*mit).dir << "' ?" << endl;
-                    // FIXME make this more clear
-                    if ( entry.path == ( ((*mit).dir[(*mit).dir.size()-1] == '/') ? (*mit).dir : ((*mit).dir + '/' ) ) )
+                    // Entry.path has leading and trailing '/' (asserted by DiskUsage::Entry).
+                    // So we append a '/' to the mountpoint and test for prefix_r.
+                    if ( str::hasPrefix( (mit->dir+"/"), entry.path ) )
                     {
-                      // entry is a mountpoint, so we need to keep it
-                      //MIL << "yes" << endl;
-                      // just discard it
-                      _discarded_entries++;
                       skip = false;
                       break;
                     }
-                    else
-                    {
-                      //MIL << "no" << endl;
-                    }
                   }
                 }
                 // try next entry
                 if ( skip )
                   continue;
-                
-                //MIL << "adding entry for " << entry.path << endl;
+
                 _data->diskusage.add(entry);
-                _counted_entries++;
               }
               else
               {
@@ -202,8 +192,6 @@ namespace zypp
          data::Package_Ptr       _pkgData;
          data::SrcPackage_Ptr    _srcpkgData;
           DiskUsageCounter::MountPointSet _mounts;
-          int _counted_entries;
-          int _discarded_entries;
       };
       ///////////////////////////////////////////////////////////////////