file_cmds-188.tar.gz
[apple/file_cmds.git] / du / du.c
1 /*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Newcomb.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 #if 0
45 static const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
46 #endif
47 #endif /* not lint */
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD: src/usr.bin/du/du.c,v 1.38 2005/04/09 14:31:40 stefanf Exp $");
50
51 #include <sys/mount.h>
52 #include <sys/param.h>
53 #include <sys/queue.h>
54 #include <sys/stat.h>
55
56 #include <err.h>
57 #include <errno.h>
58 #include <fnmatch.h>
59 #include <fts.h>
60 #include <locale.h>
61 #include <math.h>
62 #include <stdint.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <sysexits.h>
67 #include <unistd.h>
68
69 #ifdef __APPLE__
70 #include <get_compat.h>
71 #else
72 #define COMPAT_MODE(func, mode) (1)
73 #endif
74
75 #define KILO_SZ(n) (n)
76 #define MEGA_SZ(n) ((n) * (n))
77 #define GIGA_SZ(n) ((n) * (n) * (n))
78 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
79 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
80
81 #define KILO_2_SZ (KILO_SZ(1024ULL))
82 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
83 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
84 #define TERA_2_SZ (TERA_SZ(1024ULL))
85 #define PETA_2_SZ (PETA_SZ(1024ULL))
86
87 #define KILO_SI_SZ (KILO_SZ(1000ULL))
88 #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
89 #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
90 #define TERA_SI_SZ (TERA_SZ(1000ULL))
91 #define PETA_SI_SZ (PETA_SZ(1000ULL))
92
93 #define TWO_TB (2LL * 1024LL * 1024LL * 1024LL * 1024LL)
94
95 unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
96 unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
97 unsigned long long *valp;
98
99 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
100
101 int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
102
103 SLIST_HEAD(ignhead, ignentry) ignores;
104 struct ignentry {
105 char *mask;
106 SLIST_ENTRY(ignentry) next;
107 };
108
109 static int linkchk(FTSENT *);
110 static void usage(void);
111 void prthumanval(double);
112 unit_t unit_adjust(double *);
113 void ignoreadd(const char *);
114 void ignoreclean(void);
115 int ignorep(FTSENT *);
116
117 int
118 main(int argc, char *argv[])
119 {
120 FTS *fts;
121 FTSENT *p;
122 off_t savednumber = 0;
123 long blocksize;
124 int ftsoptions;
125 int listall;
126 int depth;
127 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
128 char **save;
129 static char dot[] = ".";
130 off_t *ftsnum, *ftsparnum;
131
132 setlocale(LC_ALL, "");
133
134 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
135
136 save = argv;
137 ftsoptions = 0;
138 depth = INT_MAX;
139 SLIST_INIT(&ignores);
140
141 while ((ch = getopt(argc, argv, "HI:LPasd:cghkmrx")) != -1)
142 switch (ch) {
143 case 'H':
144 Lflag = Pflag = 0;
145 Hflag = 1;
146 break;
147 case 'I':
148 ignoreadd(optarg);
149 break;
150 case 'L':
151 Hflag = Pflag = 0;
152 Lflag = 1;
153 break;
154 case 'P':
155 Hflag = Lflag = 0;
156 Pflag = 1;
157 break;
158 case 'a':
159 aflag = 1;
160 break;
161 case 's':
162 sflag = 1;
163 break;
164 case 'd':
165 dflag = 1;
166 errno = 0;
167 depth = atoi(optarg);
168 if (errno == ERANGE || depth < 0) {
169 warnx("invalid argument to option d: %s", optarg);
170 usage();
171 }
172 break;
173 case 'c':
174 cflag = 1;
175 break;
176 case 'h':
177 putenv("BLOCKSIZE=512");
178 hflag = 1;
179 valp = vals_base2;
180 break;
181 case 'k':
182 hflag = 0;
183 putenv("BLOCKSIZE=1024");
184 break;
185 case 'm':
186 hflag = 0;
187 putenv("BLOCKSIZE=1048576");
188 break;
189 case 'g':
190 hflag = 0;
191 putenv("BLOCKSIZE=1g");
192 break;
193 case 'r': /* Compatibility. */
194 break;
195 case 'x':
196 ftsoptions |= FTS_XDEV;
197 break;
198 case '?':
199 default:
200 usage();
201 }
202
203 argc -= optind;
204 argv += optind;
205
206 /*
207 * XXX
208 * Because of the way that fts(3) works, logical walks will not count
209 * the blocks actually used by symbolic links. We rationalize this by
210 * noting that users computing logical sizes are likely to do logical
211 * copies, so not counting the links is correct. The real reason is
212 * that we'd have to re-implement the kernel's symbolic link traversing
213 * algorithm to get this right. If, for example, you have relative
214 * symbolic links referencing other relative symbolic links, it gets
215 * very nasty, very fast. The bottom line is that it's documented in
216 * the man page, so it's a feature.
217 */
218
219 if (Hflag + Lflag + Pflag > 1)
220 usage();
221
222 if (Hflag + Lflag + Pflag == 0)
223 Pflag = 1; /* -P (physical) is default */
224
225 if (Hflag)
226 ftsoptions |= FTS_COMFOLLOW;
227
228 if (Lflag)
229 ftsoptions |= FTS_LOGICAL;
230
231 if (Pflag)
232 ftsoptions |= FTS_PHYSICAL;
233
234 listall = 0;
235
236 if (aflag) {
237 if (sflag || dflag)
238 usage();
239 listall = 1;
240 } else if (sflag) {
241 if (dflag)
242 usage();
243 depth = 0;
244 }
245
246 if (!*argv) {
247 argv = save;
248 argv[0] = dot;
249 argv[1] = NULL;
250 }
251
252 (void) getbsize(&notused, &blocksize);
253 blocksize /= 512;
254
255 rval = 0;
256
257 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
258 err(1, "fts_open");
259
260 while ((p = fts_read(fts)) != NULL) {
261 switch (p->fts_info) {
262 case FTS_D: /* Ignore. */
263 if (ignorep(p))
264 fts_set(fts, p, FTS_SKIP);
265 break;
266 case FTS_DP:
267 if (ignorep(p))
268 break;
269
270 ftsparnum = (off_t *)&p->fts_parent->fts_number;
271 ftsnum = (off_t *)&p->fts_number;
272 if (p->fts_statp->st_size < TWO_TB) {
273 ftsparnum[0] += ftsnum[0] += p->fts_statp->st_blocks;
274 } else {
275 ftsparnum[0] += ftsnum[0] += howmany(p->fts_statp->st_size, 512LL);
276 }
277
278 if (p->fts_level <= depth) {
279 if (hflag) {
280 (void) prthumanval(howmany(*ftsnum, blocksize));
281 (void) printf("\t%s\n", p->fts_path);
282 } else {
283 (void) printf("%jd\t%s\n",
284 (intmax_t)howmany(*ftsnum, blocksize),
285 p->fts_path);
286 }
287 }
288 break;
289 case FTS_DC: /* Ignore. */
290 if (COMPAT_MODE("bin/du", "unix2003")) {
291 errx(1, "Can't follow symlink cycle from %s to %s", p->fts_path, p->fts_cycle->fts_path);
292 }
293 break;
294 case FTS_DNR: /* Warn, continue. */
295 case FTS_ERR:
296 case FTS_NS:
297 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
298 rval = 1;
299 break;
300 case FTS_SLNONE:
301 if (COMPAT_MODE("bin/du", "unix2003")) {
302 struct stat sb;
303 int rc = stat(p->fts_path, &sb);
304 if (rc < 0 && errno == ELOOP) {
305 errx(1, "Too many symlinks at %s", p->fts_path);
306 }
307 }
308 default:
309 if (ignorep(p))
310 break;
311
312 if (p->fts_statp->st_nlink > 1 && linkchk(p))
313 break;
314
315 if (listall || p->fts_level == 0) {
316 if (hflag) {
317 if (p->fts_statp->st_size < TWO_TB) {
318 (void) prthumanval(howmany(p->fts_statp->st_blocks,
319 blocksize));
320 } else {
321 (void) prthumanval(howmany(howmany(p->fts_statp->st_size, 512LL),
322 blocksize));
323 }
324 (void) printf("\t%s\n", p->fts_path);
325 } else {
326 if (p->fts_statp->st_size < TWO_TB) {
327 (void) printf("%jd\t%s\n",
328 (intmax_t)howmany(p->fts_statp->st_blocks, blocksize),
329 p->fts_path);
330 } else {
331 (void) printf("%jd\t%s\n",
332 (intmax_t)howmany(howmany(p->fts_statp->st_size, 512LL), blocksize),
333 p->fts_path);
334 }
335 }
336 }
337
338 ftsparnum = (off_t *)&p->fts_parent->fts_number;
339 if (p->fts_statp->st_size < TWO_TB) {
340 ftsparnum[0] += p->fts_statp->st_blocks;
341 } else {
342 ftsparnum[0] += p->fts_statp->st_size / 512LL;
343 }
344 }
345 savednumber = ((off_t *)&p->fts_parent->fts_number)[0];
346 }
347
348 if (errno)
349 err(1, "fts_read");
350
351 if (cflag) {
352 if (hflag) {
353 (void) prthumanval(howmany(savednumber, blocksize));
354 (void) printf("\ttotal\n");
355 } else {
356 (void) printf("%jd\ttotal\n", (intmax_t)howmany(savednumber, blocksize));
357 }
358 }
359
360 ignoreclean();
361 exit(rval);
362 }
363
364 static int
365 linkchk(FTSENT *p)
366 {
367 struct links_entry {
368 struct links_entry *next;
369 struct links_entry *previous;
370 int links;
371 dev_t dev;
372 ino_t ino;
373 };
374 static const size_t links_hash_initial_size = 8192;
375 static struct links_entry **buckets;
376 static struct links_entry *free_list;
377 static size_t number_buckets;
378 static unsigned long number_entries;
379 static char stop_allocating;
380 struct links_entry *le, **new_buckets;
381 struct stat *st;
382 size_t i, new_size;
383 int hash;
384
385 st = p->fts_statp;
386
387 /* If necessary, initialize the hash table. */
388 if (buckets == NULL) {
389 number_buckets = links_hash_initial_size;
390 buckets = malloc(number_buckets * sizeof(buckets[0]));
391 if (buckets == NULL)
392 errx(1, "No memory for hardlink detection");
393 for (i = 0; i < number_buckets; i++)
394 buckets[i] = NULL;
395 }
396
397 /* If the hash table is getting too full, enlarge it. */
398 if (number_entries > number_buckets * 10 && !stop_allocating) {
399 new_size = number_buckets * 2;
400 new_buckets = malloc(new_size * sizeof(struct links_entry *));
401
402 /* Try releasing the free list to see if that helps. */
403 if (new_buckets == NULL && free_list != NULL) {
404 while (free_list != NULL) {
405 le = free_list;
406 free_list = le->next;
407 free(le);
408 }
409 new_buckets = malloc(new_size * sizeof(new_buckets[0]));
410 }
411
412 if (new_buckets == NULL) {
413 stop_allocating = 1;
414 warnx("No more memory for tracking hard links");
415 } else {
416 memset(new_buckets, 0,
417 new_size * sizeof(struct links_entry *));
418 for (i = 0; i < number_buckets; i++) {
419 while (buckets[i] != NULL) {
420 /* Remove entry from old bucket. */
421 le = buckets[i];
422 buckets[i] = le->next;
423
424 /* Add entry to new bucket. */
425 hash = (le->dev ^ le->ino) % new_size;
426
427 if (new_buckets[hash] != NULL)
428 new_buckets[hash]->previous =
429 le;
430 le->next = new_buckets[hash];
431 le->previous = NULL;
432 new_buckets[hash] = le;
433 }
434 }
435 free(buckets);
436 buckets = new_buckets;
437 number_buckets = new_size;
438 }
439 }
440
441 /* Try to locate this entry in the hash table. */
442 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
443 for (le = buckets[hash]; le != NULL; le = le->next) {
444 if (le->dev == st->st_dev && le->ino == st->st_ino) {
445 /*
446 * Save memory by releasing an entry when we've seen
447 * all of it's links.
448 */
449 if (--le->links <= 0) {
450 if (le->previous != NULL)
451 le->previous->next = le->next;
452 if (le->next != NULL)
453 le->next->previous = le->previous;
454 if (buckets[hash] == le)
455 buckets[hash] = le->next;
456 number_entries--;
457 /* Recycle this node through the free list */
458 if (stop_allocating) {
459 free(le);
460 } else {
461 le->next = free_list;
462 free_list = le;
463 }
464 }
465 return (1);
466 }
467 }
468
469 if (stop_allocating)
470 return (0);
471
472 /* Add this entry to the links cache. */
473 if (free_list != NULL) {
474 /* Pull a node from the free list if we can. */
475 le = free_list;
476 free_list = le->next;
477 } else
478 /* Malloc one if we have to. */
479 le = malloc(sizeof(struct links_entry));
480 if (le == NULL) {
481 stop_allocating = 1;
482 warnx("No more memory for tracking hard links");
483 return (0);
484 }
485 le->dev = st->st_dev;
486 le->ino = st->st_ino;
487 le->links = st->st_nlink - 1;
488 number_entries++;
489 le->next = buckets[hash];
490 le->previous = NULL;
491 if (buckets[hash] != NULL)
492 buckets[hash]->previous = le;
493 buckets[hash] = le;
494 return (0);
495 }
496
497 /*
498 * Output in "human-readable" format. Uses 3 digits max and puts
499 * unit suffixes at the end. Makes output compact and easy to read,
500 * especially on huge disks.
501 *
502 */
503 unit_t
504 unit_adjust(double *val)
505 {
506 double abval;
507 unit_t unit;
508 unsigned int unit_sz;
509
510 abval = fabs(*val);
511
512 unit_sz = abval ? ilogb(abval) / 10 : 0;
513
514 if (unit_sz >= UNIT_MAX) {
515 unit = NONE;
516 } else {
517 unit = unitp[unit_sz];
518 *val /= (double)valp[unit_sz];
519 }
520
521 return (unit);
522 }
523
524 void
525 prthumanval(double bytes)
526 {
527 unit_t unit;
528
529 bytes *= 512;
530 unit = unit_adjust(&bytes);
531
532 if (bytes == 0)
533 (void)printf(" 0B");
534 else if (bytes > 10)
535 (void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
536 else
537 (void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
538 }
539
540 static void
541 usage(void)
542 {
543 (void)fprintf(stderr,
544 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k | -m | -g] [-x] [-I mask] [file ...]\n");
545 exit(EX_USAGE);
546 }
547
548 void
549 ignoreadd(const char *mask)
550 {
551 struct ignentry *ign;
552
553 ign = calloc(1, sizeof(*ign));
554 if (ign == NULL)
555 errx(1, "cannot allocate memory");
556 ign->mask = strdup(mask);
557 if (ign->mask == NULL)
558 errx(1, "cannot allocate memory");
559 SLIST_INSERT_HEAD(&ignores, ign, next);
560 }
561
562 void
563 ignoreclean(void)
564 {
565 struct ignentry *ign;
566
567 while (!SLIST_EMPTY(&ignores)) {
568 ign = SLIST_FIRST(&ignores);
569 SLIST_REMOVE_HEAD(&ignores, next);
570 free(ign->mask);
571 free(ign);
572 }
573 }
574
575 int
576 ignorep(FTSENT *ent)
577 {
578 struct ignentry *ign;
579
580 #ifdef __APPLE__
581 if (S_ISDIR(ent->fts_statp->st_mode) && !strcmp("fd", ent->fts_name)) {
582 struct statfs sfsb;
583 int rc = statfs(ent->fts_accpath, &sfsb);
584 if (rc >= 0 && !strcmp("fdesc", sfsb.f_fstypename)) {
585 /* Don't cd into /dev/fd/N since one of those is likely to be
586 the cwd as of the start of du which causes all manner of
587 unpleasant surprises */
588 return 1;
589 }
590 }
591 #endif /* __APPLE__ */
592 SLIST_FOREACH(ign, &ignores, next)
593 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
594 return 1;
595 return 0;
596 }