/
DiskUsageCounter.cc
328 lines (284 loc) · 9.54 KB
/
DiskUsageCounter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*---------------------------------------------------------------------\
| ____ _ __ __ ___ |
| |__ / \ / / . \ . \ |
| / / \ V /| _/ _/ |
| / /__ | | | | | | |
| /_____||_| |_| |_| |
| |
\---------------------------------------------------------------------*/
/** \file zypp/DiskUsageCounter.cc
*
*/
extern "C"
{
#include <sys/statvfs.h>
}
#include <iostream>
#include <fstream>
#include "zypp/base/Easy.h"
#include "zypp/base/LogTools.h"
#include "zypp/base/DtorReset.h"
#include "zypp/base/String.h"
#include "zypp/DiskUsageCounter.h"
#include "zypp/ExternalProgram.h"
#include "zypp/sat/Pool.h"
#include "zypp/sat/detail/PoolImpl.h"
using std::endl;
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
namespace
{ /////////////////////////////////////////////////////////////////
DiskUsageCounter::MountPointSet calcDiskUsage( DiskUsageCounter::MountPointSet result, const Bitmap & installedmap_r )
{
if ( result.empty() )
{
// partitioning is not set
return result;
}
sat::Pool satpool( sat::Pool::instance() );
// init libsolv result vector with mountpoints
static const ::DUChanges _initdu = { 0, 0, 0, 0 };
std::vector< ::DUChanges> duchanges( result.size(), _initdu );
{
unsigned idx = 0;
for_( it, result.begin(), result.end() )
{
duchanges[idx].path = it->dir.c_str();
if ( it->growonly )
duchanges[idx].flags |= DUCHANGES_ONLYADD;
++idx;
}
}
// now calc...
::pool_calc_duchanges( satpool.get(),
const_cast<Bitmap &>(installedmap_r),
&duchanges[0],
duchanges.size() );
// and process the result
{
unsigned idx = 0;
for_( it, result.begin(), result.end() )
{
static const ByteCount blockAdjust( 2, ByteCount::K ); // (files * blocksize) / (2 * 1K)
it->pkg_size = it->used_size // current usage
+ duchanges[idx].kbytes // package data size
+ ( duchanges[idx].files * it->block_size / blockAdjust ); // half block per file
++idx;
}
}
return result;
}
/////////////////////////////////////////////////////////////////
} // namespace
///////////////////////////////////////////////////////////////////
DiskUsageCounter::MountPointSet DiskUsageCounter::disk_usage( const ResPool & pool_r ) const
{
Bitmap bitmap( Bitmap::poolSize );
// build installedmap (installed != transact)
// stays installed or gets installed
for_( it, pool_r.begin(), pool_r.end() )
{
if ( it->status().isInstalled() != it->status().transacts() )
{
bitmap.set( sat::asSolvable()(*it).id() );
}
}
return calcDiskUsage( _mps, bitmap );
}
DiskUsageCounter::MountPointSet DiskUsageCounter::disk_usage( sat::Solvable solv_r ) const
{
Bitmap bitmap( Bitmap::poolSize );
bitmap.set( solv_r.id() );
// temp. unset @system Repo
DtorReset tmp( sat::Pool::instance().get()->installed );
sat::Pool::instance().get()->installed = nullptr;
return calcDiskUsage( _mps, bitmap );
}
DiskUsageCounter::MountPointSet DiskUsageCounter::disk_usage( const Bitmap & bitmap_r ) const
{
// temp. unset @system Repo
DtorReset tmp( sat::Pool::instance().get()->installed );
sat::Pool::instance().get()->installed = nullptr;
return calcDiskUsage( _mps, bitmap_r );
}
DiskUsageCounter::MountPointSet DiskUsageCounter::detectMountPoints( const std::string & rootdir )
{
DiskUsageCounter::MountPointSet ret;
std::ifstream procmounts( "/proc/mounts" );
if ( !procmounts ) {
WAR << "Unable to read /proc/mounts" << std::endl;
} else {
std::string prfx;
if ( rootdir != "/" )
prfx = rootdir; // rootdir not /
while ( procmounts ) {
std::string l = str::getline( procmounts );
if ( !(procmounts.fail() || procmounts.bad()) ) {
// data to consume
// rootfs / rootfs rw 0 0
// /dev/root / reiserfs rw 0 0
// proc /proc proc rw 0 0
// devpts /dev/pts devpts rw 0 0
// /dev/hda5 /boot ext2 rw 0 0
// shmfs /dev/shm shm rw 0 0
// usbdevfs /proc/bus/usb usbdevfs rw 0 0
std::vector<std::string> words;
str::split( l, std::back_inserter(words) );
if ( words.size() < 3 ) {
WAR << "Suspicious entry in /proc/mounts: " << l << std::endl;
continue;
}
//
// Filter devices without '/' (proc,shmfs,..)
//
if ( words[0].find( '/' ) == std::string::npos ) {
DBG << "Discard mount point : " << l << std::endl;
continue;
}
// remove /proc entry
if (words[0] == "/proc")
{
DBG << "Discard /proc filesystem: " << l << std::endl;
continue;
}
//
// Filter mountpoints not at or below _rootdir
//
std::string mp = words[1];
if ( prfx.size() ) {
if ( mp.compare( 0, prfx.size(), prfx ) != 0 ) {
// mountpoint not below rootdir
DBG << "Unwanted mount point : " << l << std::endl;
continue;
}
// strip prfx
mp.erase( 0, prfx.size() );
if ( mp.empty() ) {
mp = "/";
} else if ( mp[0] != '/' ) {
// mountpoint not below rootdir
DBG << "Unwanted mount point : " << l << std::endl;
continue;
}
}
//
// Filter cdrom
//
if ( words[2] == "iso9660" ) {
DBG << "Discard cdrom : " << l << std::endl;
continue;
}
if ( words[2] == "vfat" || words[2] == "fat" || words[2] == "ntfs" || words[2] == "ntfs-3g")
{
MIL << words[1] << " contains ignored fs (" << words[2] << ')' << std::endl;
continue;
}
//
// Filter some common unwanted mountpoints
//
const char * mpunwanted[] = {
"/mnt", "/media", "/mounts", "/floppy", "/cdrom",
"/suse", "/var/tmp", "/var/adm/mount", "/var/adm/YaST",
/*last*/0/*entry*/
};
const char ** nomp = mpunwanted;
for ( ; *nomp; ++nomp ) {
std::string pre( *nomp );
if ( mp.compare( 0, pre.size(), pre ) == 0 // mp has prefix pre
&& ( mp.size() == pre.size() || mp[pre.size()] == '/' ) ) {
break;
}
}
if ( *nomp ) {
DBG << "Filter mount point : " << l << std::endl;
continue;
}
//
// Check whether mounted readonly
//
MountPoint::HintFlags hints;
std::vector<std::string> flags;
str::split( words[3], std::back_inserter(flags), "," );
for ( unsigned i = 0; i < flags.size(); ++i ) {
if ( flags[i] == "ro" ) {
hints |= MountPoint::Hint_readonly;
break;
}
}
if ( hints.testFlag( MountPoint::Hint_readonly ) ) {
DBG << "Filter ro mount point : " << l << std::endl;
continue;
}
//
// check for snapshotting btrfs
//
if ( words[2] == "btrfs" )
{
if ( geteuid() != 0 )
{
DBG << "Assume snapshots on " << words[1] << ": non-root user can't check" << std::endl;
hints |= MountPoint::Hint_growonly;
}
else
{
// For now just check whether there is
// at least one snapshot on the volume:
ExternalProgram prog({"btrfs","subvolume","list","-s",words[1]});
std::string line( prog.receiveLine() );
if ( ! line.empty() )
{
DBG << "Found a snapshot on " << words[1] << ": " << line; // has trailing std::endl
hints |= MountPoint::Hint_growonly;
}
prog.kill();
}
}
//
// statvfs (full path!) and get the data
//
struct statvfs sb;
if ( statvfs( words[1].c_str(), &sb ) != 0 ) {
WAR << "Unable to statvfs(" << words[1] << "); errno " << errno << std::endl;
ret.insert( DiskUsageCounter::MountPoint( mp, words[2], 0LL, 0LL, 0LL, 0LL, hints ) );
}
else
{
//
// Filter zero sized devices (bnc#769819)
//
if ( sb.f_blocks == 0 || sb.f_bsize == 0 )
{
DBG << "Filter zero-sized mount point : " << l << std::endl;
continue;
}
ret.insert( DiskUsageCounter::MountPoint( mp, words[2], sb.f_bsize,
((long long)sb.f_blocks)*sb.f_bsize/1024,
((long long)(sb.f_blocks - sb.f_bfree))*sb.f_bsize/1024, 0LL, hints ) );
}
}
}
}
return ret;
}
DiskUsageCounter::MountPointSet DiskUsageCounter::justRootPartition()
{
DiskUsageCounter::MountPointSet ret;
ret.insert( DiskUsageCounter::MountPoint() );
return ret;
}
std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPoint & obj )
{
str << "dir:[" << obj.dir << "] [ bs: " << obj.blockSize()
<< " ts: " << obj.totalSize()
<< " us: " << obj.usedSize()
<< " (+-: " << obj.commitDiff()
<< ")" << (obj.readonly?"r":"") << (obj.growonly?"g":"") << " " << obj.fstype << "]";
return str;
}
std::ostream & operator<<( std::ostream & str, const DiskUsageCounter::MountPointSet & obj )
{ return dumpRange( str, obj.begin(), obj.end() ); }
/////////////////////////////////////////////////////////////////
} // namespace zypp
///////////////////////////////////////////////////////////////////