-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdkusage.c
More file actions
458 lines (405 loc) · 11.2 KB
/
Copy pathdkusage.c
File metadata and controls
458 lines (405 loc) · 11.2 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* dkusage by jeff hodd
*
* This source code is in the public domain.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> /* flags for read and write */
#include <gdbm.h>
#include <sys/types.h> /* typedefs */
#include <sys/stat.h> /* structure returned by stat */
#include <sys/dir.h> /* local directory structure */
/***************************/
/* CONSTANT DEFINITIONS */
/***************************/
#define WS_NONE 0
#define WS_RECURSIVE (1 << 0)
#define WS_DEFAULT WS_RECURSIVE
#define WS_FOLLOWLINK (1 << 1) /* follow symlinks */
#define WS_DOTFILES (1 << 2) /* per unix convention, .file is hidden */
#define WS_TARFILES (1 << 3) /* add tarfile overhead */
#define TAR_REC_SIZE 512
#define TAR_BLK_SIZE 20
#define TAR_BLK_FACTOR (TAR_REC_SIZE * TAR_BLK_SIZE)
#define TAR_STDHDR_SZ TAR_REC_SIZE
#define TAR_LRGHDR_SZ (TAR_STDHDR_SZ * 3)
#define TAR_STD_PATHSZ 100
#define DB_FILENAME "/tmp/inode.db"
#define FALSE 0
#define TRUE 1
/***************************/
/* FUNCTION PROTOTYPES */
/***************************/
void fsize(char *, int);
void print_usage(void);
void dirwalk(char *, int, void (*fcn)(char *, int));
char * adjleading(char *);
GDBM_FILE opendb(char *);
void closedb(GDBM_FILE, char *);
/***************************/
/* STATIC VARIABLES */
/***************************/
static unsigned long long total_size = 0;
static GDBM_FILE inode_db = NULL;
static int verbose = FALSE;
static char tmpname[FILENAME_MAX];
/*
* calculate disk usage
*/
int main(int argc, char **argv)
{
/*
* set up the default options
*/
int spec = WS_DEFAULT|WS_DOTFILES;
/*
* create and open the temporary inode db
*/
inode_db = opendb(DB_FILENAME);
if (inode_db == NULL)
return 1;
if (argc == 1) /* default: current directory */
{
fsize(".", spec);
}
else
{
while (--argc > 0)
{
if ((*++argv)[0] == '-')
{
/*
* process options here : -f, -d, -t -v -h
*/
if (strcmp(*argv, "-f") == 0)
spec |= WS_FOLLOWLINK;
else if (strcmp(*argv, "-d") == 0)
spec &= ~WS_DOTFILES;
else if (strcmp(*argv, "-t") == 0)
spec |= WS_TARFILES;
else if (strcmp(*argv, "-v") == 0)
verbose = TRUE;
else if (strcmp(*argv, "-h") == 0)
{
/*
* print out the help text and close the inode db
*/
print_usage();
closedb(inode_db, DB_FILENAME);
return 0;
}
}
else
{
/*
* check for -f -t combo. disable -f if true
*/
if ((spec & WS_TARFILES) && (spec & WS_FOLLOWLINK))
{
fprintf(stderr, "Follow-links option (-f) invalid in tarfile mode : FOLLOWLINK disabled\n");
spec &= ~WS_FOLLOWLINK;
}
fsize(*argv, spec);
}
}
}
/*
* adjust tarfile size estimate for 10240 byte blocking
*/
if (spec & WS_TARFILES)
total_size = (((total_size / TAR_BLK_FACTOR) + 1) * TAR_BLK_FACTOR);
/*
* print out the total size
*/
printf("%lld\n", total_size);
/*
* close the inode db
*/
closedb(inode_db, DB_FILENAME);
return 0;
}
/*
* open the inode db
*/
GDBM_FILE opendb(char * db_filename)
{
GDBM_FILE idb = gdbm_open(DB_FILENAME, 512, GDBM_NEWDB, 0600, NULL);
if (idb == NULL)
{
perror("gdbm_open");
}
return idb;
}
/*
* close the inode db and remove the file
*/
void closedb(GDBM_FILE idb, char * db_filename)
{
/*
* clean up the temp db
*/
if (idb != NULL)
{
gdbm_close(idb);
unlink(db_filename);
}
}
/*
* print out the usage instructions
*/
void print_usage()
{
printf("\n");
printf("dkusage v0.3.5\n");
printf("\n");
printf("Usage: dkusage [-d][[-f]|[-t]][-v][-h] [directory_names]\n");
printf("Options:\n");
printf(" -d do not include dotfiles\n");
printf(" -f follow symbolic links\n");
printf(" -t calculate tarfile size\n");
printf(" -v verbose mode - print filenames\n");
printf(" -h help - usage\n");
printf("\n");
printf("If no directory names are provided, dkusage defaults\n");
printf("to the current directory. The default behaviour is\n");
printf("not to follow symlinks and to include dotfiles.\n");
printf("\n");
printf("Note that the tarfile size calculation assumes a standard,\n");
printf("non-compressed, tar file with standard blocking (20x512).\n");
printf("\n");
printf("The -f and -t options are incompatible. The -f option will be\n");
printf("disabled if both options are used together.\n");
printf("\n");
}
/*
* fsize: accumulate file sizes and make adjustments for tarfiles
*/
void fsize(char *name, int spec)
{
struct stat stbuf;
/*
* get the appropriate stat structure
*/
int rtstat = (spec & WS_FOLLOWLINK) ? stat(name, &stbuf) : lstat(name, &stbuf);
if (rtstat == -1)
{
fprintf(stderr, "fsize: can't access %s\n", name);
return;
}
/*
* tar won't archive socket files
*/
if ((spec & WS_TARFILES) && S_ISSOCK(stbuf.st_mode))
{
fprintf(stderr, "socket: %s - ignored\n", name);
return;
}
/*
* removing leading /, ./ and/or ../
*/
char * pname = adjleading(name);
/*
* add a trailing / to directory names
*/
if (S_ISDIR(stbuf.st_mode) && (pname[strlen(pname)] != '/'))
pname[strlen(pname)] = '/';
/*
* print the filename if verbose
*/
if (verbose)
printf("%s\n", pname);
/*
* set up for inode db access
*/
datum key, data; // Key and value
char tmpkey[32];
/*
* create key and data elements
*/
memset(tmpkey, 0, sizeof(tmpkey));
sprintf(tmpkey, "%lu", stbuf.st_ino);
key.dptr = tmpkey;
key.dsize = strlen(tmpkey);
data.dptr = key.dptr;
data.dsize = key.dsize;
/*
* has this inode already been found
* if yes, then we have a hard link
*/
if (gdbm_store(inode_db, key, data, GDBM_INSERT) == 1)
{
/*
* if we're doing tarfile size, add the correct size
*
* 512 bytes header for filenames up to 100 characters long
* 1536 byte header for filenames over 100 characters long
* no data body
*/
if (spec & WS_TARFILES)
{
if (strlen(pname) > TAR_STD_PATHSZ)
total_size += TAR_LRGHDR_SZ;
else
total_size += TAR_STDHDR_SZ;
}
}
else
{
/*
* if we're doing tarfile size, add the correct size
*
* 512 bytes header for filenames up to 100 characters long
* 1536 byte header for filenames over 100 characters long
* st_size for size if st_size divisible by 512
* st_size rounded up to the nearest multiple of 512
*/
if (spec & WS_TARFILES)
{
if (S_ISLNK(stbuf.st_mode))
{
char lnkdata[FILENAME_MAX];
memset(lnkdata, 0, FILENAME_MAX);
/*
* get the name of the file pointed to by the symlink
*/
if (readlink(name, lnkdata, FILENAME_MAX) == -1)
{
perror("readlink");
return;
}
/*
* if the symlink name or the name of the file it points to
* is longer than 100 bytes, then this is a LONGLINK header.
*/
if ((strlen(lnkdata) > TAR_STD_PATHSZ) || (strlen(pname) > TAR_STD_PATHSZ))
total_size += TAR_LRGHDR_SZ;
else
total_size += TAR_STDHDR_SZ;
}
else
{
/*
* add in file header
*/
if (strlen(pname) > TAR_STD_PATHSZ)
total_size += TAR_LRGHDR_SZ;
else
total_size += TAR_STDHDR_SZ;
/*
* add the file size adjusted to a 512 byte blocking factor
*/
if (S_ISREG(stbuf.st_mode))
{
if (stbuf.st_size > 0)
{
off_t t_size = (stbuf.st_size % TAR_REC_SIZE) ? (((stbuf.st_size / TAR_REC_SIZE) + 1) * TAR_REC_SIZE) : stbuf.st_size;
total_size += t_size;
}
}
}
}
else
{
/*
* not doing tarfiles - just add st_size
*/
if (stbuf.st_size > 0)
total_size += stbuf.st_size;
}
}
/*
* walk the directory
*/
if (S_ISDIR(stbuf.st_mode))
dirwalk(name, spec, fsize);
}
/*
* dirwalk: apply fcn (fsize) to all files in dir
*/
void dirwalk(char *dir, int spec, void (*fcn)(char *, int))
{
char name[FILENAME_MAX];
struct dirent *dp;
DIR *dfd;
/*
* open the directory
*/
if ((dfd = opendir(dir)) == NULL)
{
fprintf(stderr, "dirwalk: can't open %s\n", dir);
return;
}
/*
* iterate through the directory entries
*/
while ((dp = readdir(dfd)) != NULL)
{
/*
* check for dot-files/-dirs, the . and .. directories
*/
if (dp->d_name[0] == '.')
{
if (!(spec & WS_DOTFILES))
continue;
if ((!strcmp(dp->d_name, ".")) || (!strcmp(dp->d_name, "..")))
continue;
}
/*
* build the full path name and recurse into fsize
*/
if (strlen(dir)+strlen(dp->d_name)+2 > FILENAME_MAX)
fprintf(stderr, "dirwalk: name %s %s too long\n", dir, dp->d_name);
else
{
sprintf(name, "%s/%s", dir, dp->d_name);
(*fcn)(name, spec);
}
}
/*
* close the directory
*/
closedir(dfd);
}
/*
* strip the leading /, ./ and ../ from the file path
*/
char * adjleading(char * nmstr)
{
memset(tmpname, 0, FILENAME_MAX);
/*
* strip the leading /
*/
if (nmstr[0] == '/')
memcpy(tmpname, &nmstr[1], strlen(nmstr) - 1);
else
memcpy(tmpname, nmstr, strlen(nmstr));
/*
* strip any leading ./
*/
char * pname = tmpname;
if (pname[0] == '.')
{
if (strncmp(pname, "./", 2) == 0)
{
pname = &tmpname[2];
}
else
{
/*
* strip any leading ../
*/
int i;
for (i = 0; i < strlen(tmpname); i+=3)
{
if (strncmp("../", &tmpname[i], 3) == 0)
pname = &tmpname[i+3];
else
break;
}
}
}
return pname;
}