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 DiskUsageCounter::MountPointSet DiskUsageCounter::disk_usage( const ResPool & pool_r )
36 DiskUsageCounter::MountPointSet result = mps;
40 // partitioning is not set
44 sat::Pool satpool( sat::Pool::instance() );
46 // init satsolver result vector with mountpoints
47 static const ::DUChanges _initdu = { 0, 0, 0 };
48 std::vector< ::DUChanges> duchanges( result.size(), _initdu );
51 for_( it, result.begin(), result.end() )
53 duchanges[idx].path = it->dir.c_str();
58 // build installedmap (installed != transact)
59 // stays installed or gets installed
61 ::map_init( &installedmap, satpool.capacity() );
62 for_( it, pool_r.begin(), pool_r.end() )
64 if ( it->status().isInstalled() != it->status().transacts() )
66 MAPSET( &installedmap, it->satSolvable().id() );
71 ::pool_calc_duchanges( satpool.get(),
72 satpool.systemRepo().get(),
77 // and process the result
80 for_( it, result.begin(), result.end() )
82 static const ByteCount blockAdjust( 2, ByteCount::K ); // (files * blocksize) / (2 * 1K)
84 it->pkg_size = it->used_size // current usage
85 + duchanges[idx].kbytes // package data size
86 + ( duchanges[idx].files * it->block_size / blockAdjust ); // half block per file
94 DiskUsageCounter::MountPointSet DiskUsageCounter::detectMountPoints(const std::string &rootdir)
96 DiskUsageCounter::MountPointSet ret;
98 std::ifstream procmounts( "/proc/mounts" );
101 WAR << "Unable to read /proc/mounts" << std::endl;
105 if ( rootdir != "/" )
106 prfx = rootdir; // rootdir not /
108 while ( procmounts ) {
109 std::string l = str::getline( procmounts );
110 if ( !(procmounts.fail() || procmounts.bad()) ) {
113 // rootfs / rootfs rw 0 0
114 // /dev/root / reiserfs rw 0 0
115 // proc /proc proc rw 0 0
116 // devpts /dev/pts devpts rw 0 0
117 // /dev/hda5 /boot ext2 rw 0 0
118 // shmfs /dev/shm shm rw 0 0
119 // usbdevfs /proc/bus/usb usbdevfs rw 0 0
121 std::vector<std::string> words;
122 str::split( l, std::back_inserter(words) );
124 if ( words.size() < 3 ) {
125 WAR << "Suspicious entry in /proc/mounts: " << l << std::endl;
130 // Filter devices without '/' (proc,shmfs,..)
132 if ( words[0].find( '/' ) == std::string::npos ) {
133 DBG << "Discard mount point : " << l << std::endl;
138 // Filter mountpoints not at or below _rootdir
140 std::string mp = words[1];
142 if ( mp.compare( 0, prfx.size(), prfx ) != 0 ) {
143 // mountpoint not below rootdir
144 DBG << "Unwanted mount point : " << l << std::endl;
148 mp.erase( 0, prfx.size() );
151 } else if ( mp[0] != '/' ) {
152 // mountpoint not below rootdir
153 DBG << "Unwanted mount point : " << l << std::endl;
161 if ( words[2] == "iso9660" ) {
162 DBG << "Discard cdrom : " << l << std::endl;
166 if ( words[2] == "vfat" || words[2] == "fat" || words[2] == "ntfs" || words[2] == "ntfs-3g")
168 MIL << words[1] << " contains ignored fs (" << words[2] << ')' << std::endl;
173 // Filter some common unwanted mountpoints
175 const char * mpunwanted[] = {
176 "/mnt", "/media", "/mounts", "/floppy", "/cdrom",
177 "/suse", "/var/tmp", "/var/adm/mount", "/var/adm/YaST",
181 const char ** nomp = mpunwanted;
182 for ( ; *nomp; ++nomp ) {
183 std::string pre( *nomp );
184 if ( mp.compare( 0, pre.size(), pre ) == 0 // mp has prefix pre
185 && ( mp.size() == pre.size() || mp[pre.size()] == '/' ) ) {
190 DBG << "Filter mount point : " << l << std::endl;
195 // Check whether mounted readonly
198 std::vector<std::string> flags;
199 str::split( words[3], std::back_inserter(flags), "," );
201 for ( unsigned i = 0; i < flags.size(); ++i ) {
202 if ( flags[i] == "ro" ) {
208 DBG << "Filter ro mount point : " << l << std::endl;
213 // statvfs (full path!) and get the data
216 if ( statvfs( words[1].c_str(), &sb ) != 0 ) {
217 WAR << "Unable to statvfs(" << words[1] << "); errno " << errno << std::endl;
218 ret.insert( DiskUsageCounter::MountPoint( mp ) );
222 ret.insert( DiskUsageCounter::MountPoint( mp, sb.f_bsize,
223 ((long long)sb.f_blocks)*sb.f_bsize/1024,
224 ((long long)(sb.f_blocks - sb.f_bfree))*sb.f_bsize/1024, 0LL, ro ) );
233 std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPoint & obj )
235 str << "dir:[" << obj.dir << "] [ bs: " << obj.block_size
236 << " ts: " << obj.total_size
237 << " us: " << obj.used_size
238 << " (+-: " << (obj.pkg_size-obj.used_size)
244 ///////////////////////////////////////////////////////////////////