1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/DiskUsageCounter.cc
14 #include <sys/statvfs.h>
20 #include "zypp/base/Easy.h"
21 #include "zypp/base/LogTools.h"
22 #include "zypp/base/String.h"
24 #include "zypp/DiskUsageCounter.h"
25 #include "zypp/sat/Pool.h"
26 #include "zypp/sat/detail/PoolImpl.h"
30 ///////////////////////////////////////////////////////////////////
32 { /////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////
36 { /////////////////////////////////////////////////////////////////
38 struct SatMap : private base::NonCopyable
40 SatMap( unsigned capacity_r = 1 )
42 ::map_init( &_installedmap, sat::Pool::instance().capacity() );
45 void add( sat::Solvable solv_r )
47 MAPSET( &_installedmap, solv_r.id() );
50 void add( const PoolItem & pi_r )
51 { add( pi_r->satSolvable() ); }
53 void add( const ResObject::constPtr & obj_r )
54 { add( obj_r->satSolvable() ); }
56 mutable ::Map _installedmap;
59 DiskUsageCounter::MountPointSet calcDiskUsage( const DiskUsageCounter::MountPointSet & mps_r, const SatMap & installedmap_r )
61 DiskUsageCounter::MountPointSet result = mps_r;
65 // partitioning is not set
69 sat::Pool satpool( sat::Pool::instance() );
71 // init satsolver result vector with mountpoints
72 static const ::DUChanges _initdu = { 0, 0, 0 };
73 std::vector< ::DUChanges> duchanges( result.size(), _initdu );
76 for_( it, result.begin(), result.end() )
78 duchanges[idx].path = it->dir.c_str();
84 ::pool_calc_duchanges( satpool.get(),
85 &installedmap_r._installedmap,
89 // and process the result
92 for_( it, result.begin(), result.end() )
94 static const ByteCount blockAdjust( 2, ByteCount::K ); // (files * blocksize) / (2 * 1K)
96 it->pkg_size = it->used_size // current usage
97 + duchanges[idx].kbytes // package data size
98 + ( duchanges[idx].files * it->block_size / blockAdjust ); // half block per file
106 /////////////////////////////////////////////////////////////////
108 ///////////////////////////////////////////////////////////////////
110 DiskUsageCounter::MountPointSet DiskUsageCounter::disk_usage( const ResPool & pool_r )
112 SatMap installedmap( sat::Pool::instance().capacity() );
113 // build installedmap (installed != transact)
114 // stays installed or gets installed
115 for_( it, pool_r.begin(), pool_r.end() )
117 if ( it->status().isInstalled() != it->status().transacts() )
119 installedmap.add( *it );
122 return calcDiskUsage( mps, installedmap );
125 DiskUsageCounter::MountPointSet DiskUsageCounter::disk_usage( sat::Solvable solv_r )
128 installedmap.add( solv_r );
129 return calcDiskUsage( mps, installedmap );
132 DiskUsageCounter::MountPointSet DiskUsageCounter::detectMountPoints(const std::string &rootdir)
134 DiskUsageCounter::MountPointSet ret;
136 std::ifstream procmounts( "/proc/mounts" );
139 WAR << "Unable to read /proc/mounts" << std::endl;
143 if ( rootdir != "/" )
144 prfx = rootdir; // rootdir not /
146 while ( procmounts ) {
147 std::string l = str::getline( procmounts );
148 if ( !(procmounts.fail() || procmounts.bad()) ) {
151 // rootfs / rootfs rw 0 0
152 // /dev/root / reiserfs rw 0 0
153 // proc /proc proc rw 0 0
154 // devpts /dev/pts devpts rw 0 0
155 // /dev/hda5 /boot ext2 rw 0 0
156 // shmfs /dev/shm shm rw 0 0
157 // usbdevfs /proc/bus/usb usbdevfs rw 0 0
159 std::vector<std::string> words;
160 str::split( l, std::back_inserter(words) );
162 if ( words.size() < 3 ) {
163 WAR << "Suspicious entry in /proc/mounts: " << l << std::endl;
168 // Filter devices without '/' (proc,shmfs,..)
170 if ( words[0].find( '/' ) == std::string::npos ) {
171 DBG << "Discard mount point : " << l << std::endl;
175 // remove /proc entry
176 if (words[0] == "/proc")
178 DBG << "Discard /proc filesystem: " << l << std::endl;
183 // Filter mountpoints not at or below _rootdir
185 std::string mp = words[1];
187 if ( mp.compare( 0, prfx.size(), prfx ) != 0 ) {
188 // mountpoint not below rootdir
189 DBG << "Unwanted mount point : " << l << std::endl;
193 mp.erase( 0, prfx.size() );
196 } else if ( mp[0] != '/' ) {
197 // mountpoint not below rootdir
198 DBG << "Unwanted mount point : " << l << std::endl;
206 if ( words[2] == "iso9660" ) {
207 DBG << "Discard cdrom : " << l << std::endl;
211 if ( words[2] == "vfat" || words[2] == "fat" || words[2] == "ntfs" || words[2] == "ntfs-3g")
213 MIL << words[1] << " contains ignored fs (" << words[2] << ')' << std::endl;
218 // Filter some common unwanted mountpoints
220 const char * mpunwanted[] = {
221 "/mnt", "/media", "/mounts", "/floppy", "/cdrom",
222 "/suse", "/var/tmp", "/var/adm/mount", "/var/adm/YaST",
226 const char ** nomp = mpunwanted;
227 for ( ; *nomp; ++nomp ) {
228 std::string pre( *nomp );
229 if ( mp.compare( 0, pre.size(), pre ) == 0 // mp has prefix pre
230 && ( mp.size() == pre.size() || mp[pre.size()] == '/' ) ) {
235 DBG << "Filter mount point : " << l << std::endl;
240 // Check whether mounted readonly
243 std::vector<std::string> flags;
244 str::split( words[3], std::back_inserter(flags), "," );
246 for ( unsigned i = 0; i < flags.size(); ++i ) {
247 if ( flags[i] == "ro" ) {
253 DBG << "Filter ro mount point : " << l << std::endl;
258 // statvfs (full path!) and get the data
261 if ( statvfs( words[1].c_str(), &sb ) != 0 ) {
262 WAR << "Unable to statvfs(" << words[1] << "); errno " << errno << std::endl;
263 ret.insert( DiskUsageCounter::MountPoint( mp ) );
267 ret.insert( DiskUsageCounter::MountPoint( mp, sb.f_bsize,
268 ((long long)sb.f_blocks)*sb.f_bsize/1024,
269 ((long long)(sb.f_blocks - sb.f_bfree))*sb.f_bsize/1024, 0LL, ro ) );
278 DiskUsageCounter::MountPointSet DiskUsageCounter::justRootPartition()
280 DiskUsageCounter::MountPointSet ret;
281 ret.insert( DiskUsageCounter::MountPoint() );
285 std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPoint & obj )
287 str << "dir:[" << obj.dir << "] [ bs: " << obj.blockSize()
288 << " ts: " << obj.totalSize()
289 << " us: " << obj.usedSize()
290 << " (+-: " << obj.commitDiff()
295 /////////////////////////////////////////////////////////////////
297 ///////////////////////////////////////////////////////////////////