- Create the cache directly from the schema (installed) file.
[platform/upstream/libzypp.git] / zypp / DiskUsage.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/DiskUsage.cc
10  *
11 */
12 #include "zypp/DiskUsage.h"
13 #include <iostream>
14
15 using namespace std;
16
17 ///////////////////////////////////////////////////////////////////
18 namespace zypp
19 { /////////////////////////////////////////////////////////////////
20   std::ostream & operator<<( std::ostream & str, const DiskUsage::Entry & obj )
21   {
22     return str << obj.path << '\t' << obj._size << "; files " << obj._files;
23   }
24
25   DiskUsage::Entry DiskUsage::extract( const std::string & dirname_r )
26   {
27     Entry ret( dirname_r );
28
29     iterator fst = begin();
30     for ( ; fst != end() && !fst->isBelow( ret ); ++fst )
31       ; // seek 1st equal or below
32  
33     bool found = false; 
34     if ( fst != end() ) {
35       iterator lst = fst;
36       found = true;
37       // return the first found, the value is sum of all subdirectories below
38       ret += *lst;
39       for ( ; lst != end() && lst->isBelow( ret ); ++lst ) {
40       }
41       // remove
42       _dirs.erase( fst, lst );
43     }
44
45     // the dir entry has been found, update all parent entries
46     if (found)
47     {
48         std::string dname = dirname_r;
49         if (dname.size() > 1 && dname[0] != '/')
50         {
51             dname.insert(dname.begin(), '/');
52         }
53             
54         Entry tmp( dname );
55
56         tmp._size = ret._size;
57         tmp._files = ret._files;
58         // subtract du from directories above
59         iterator fst = begin();
60         for ( ; fst != end(); ++fst )
61         {
62             // add slash if it's missing
63             std::string dd = fst->path;
64             if (dd.size() > 1 && dd[0] != '/')
65             {
66                 dd.insert(dd.begin(), '/');
67             }
68
69             // update the entry
70             if (tmp.isBelow(dd))
71             {
72                 *fst -= tmp;
73             }
74         }
75     }
76   
77     return ret;
78   }
79
80   std::ostream & operator<<( std::ostream & str, const DiskUsage & obj )
81   {
82     str << "Package Disk Usage {" << endl;
83     for ( DiskUsage::EntrySet::const_iterator it = obj._dirs.begin(); it != obj._dirs.end(); ++it ) {
84       str << "   " << *it << endl;
85     }
86     return str << "}";
87   }
88
89
90 } // namespace zypp
91 ///////////////////////////////////////////////////////////////////