libzypp  10.5.0
DiskUsage.cc
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include "zypp/DiskUsage.h"
00013 #include <iostream>
00014 
00015 using namespace std;
00016 
00018 namespace zypp
00019 { 
00020   std::ostream & operator<<( std::ostream & str, const DiskUsage::Entry & obj )
00021   {
00022     return str << obj.path << '\t' << obj._size << "; files " << obj._files;
00023   }
00024 
00025   DiskUsage::Entry DiskUsage::extract( const std::string & dirname_r )
00026   {
00027     Entry ret( dirname_r );
00028 
00029     iterator fst = begin();
00030     for ( ; fst != end() && !fst->isBelow( ret ); ++fst )
00031       ; // seek 1st equal or below
00032 
00033     bool found = false;
00034     if ( fst != end() ) {
00035       iterator lst = fst;
00036       found = true;
00037       // return the first found, the value is sum of all subdirectories below
00038       ret += *lst;
00039       for ( ; lst != end() && lst->isBelow( ret ); ++lst ) {
00040       }
00041       // remove
00042       _dirs.erase( fst, lst );
00043     }
00044 
00045     // the dir entry has been found, update all parent entries
00046     if (found)
00047     {
00048         std::string dname = dirname_r;
00049         if (dname.size() > 1 && dname[0] != '/')
00050         {
00051             dname.insert(dname.begin(), '/');
00052         }
00053 
00054         Entry tmp( dname );
00055 
00056         tmp._size = ret._size;
00057         tmp._files = ret._files;
00058         // subtract du from directories above
00059         iterator fst = begin();
00060         for ( ; fst != end(); ++fst )
00061         {
00062             // add slash if it's missing
00063             std::string dd = fst->path;
00064             if (dd.size() > 1 && dd[0] != '/')
00065             {
00066                 dd.insert(dd.begin(), '/');
00067             }
00068 
00069             // update the entry
00070             if (tmp.isBelow(dd))
00071             {
00072                 *fst -= tmp;
00073             }
00074         }
00075     }
00076 
00077     return ret;
00078   }
00079 
00080   std::ostream & operator<<( std::ostream & str, const DiskUsage & obj )
00081   {
00082     str << "Package Disk Usage {" << endl;
00083     for ( DiskUsage::EntrySet::const_iterator it = obj._dirs.begin(); it != obj._dirs.end(); ++it ) {
00084       str << "   " << *it << endl;
00085     }
00086     return str << "}";
00087   }
00088 
00089 
00090 } // namespace zypp