file_cmds-287.40.2.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 #include <sys/cdefs.h>
38 #ifndef lint
39 __used static const char copyright[] =
40 "@(#) Copyright (c) 1989, 1993, 1994\n\
41 The Regents of the University of California. All rights reserved.\n";
42 #endif /* not lint */
43
44 #ifndef lint
45 #if 0
46 static const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
47 #endif
48 #endif /* not lint */
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD: src/usr.bin/du/du.c,v 1.38 2005/04/09 14:31:40 stefanf Exp $");
51
52 #include <sys/mount.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/stat.h>
56 #include <sys/attr.h>
57
58 #include <err.h>
59 #include <errno.h>
60 #include <fnmatch.h>
61 #include <fts.h>
62 #include <locale.h>
63 #include <math.h>
64 #include <stdint.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <sysexits.h>
69 #include <unistd.h>
70
71 #ifdef __APPLE__
72 #include <get_compat.h>
73 #include <sys/sysctl.h>
74 #else
75 #define COMPAT_MODE(func, mode) (1)
76 #endif
77
78 #define KILO_SZ(n) (n)
79 #define MEGA_SZ(n) ((n) * (n))
80 #define GIGA_SZ(n) ((n) * (n) * (n))
81 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
82 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
83
84 #define KILO_2_SZ (KILO_SZ(1024ULL))
85 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
86 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
87 #define TERA_2_SZ (TERA_SZ(1024ULL))
88 #define PETA_2_SZ (PETA_SZ(1024ULL))
89
90 #define KILO_SI_SZ (KILO_SZ(1000ULL))
91 #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
92 #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
93 #define TERA_SI_SZ (TERA_SZ(1000ULL))
94 #define PETA_SI_SZ (PETA_SZ(1000ULL))
95
96 unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
97 unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
98 unsigned long long *valp;
99
100 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
101
102 int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
103
104 SLIST_HEAD(ignhead, ignentry) ignores;
105 struct ignentry {
106 char *mask;
107 SLIST_ENTRY(ignentry) next;
108 };
109
110 static int linkchk(FTSENT *);
111 static int dirlinkchk(FTSENT *);
112 static void usage(void);
113 void prthumanval(double);
114 unit_t unit_adjust(double *);
115 void ignoreadd(const char *);
116 void ignoreclean(void);
117 int ignorep(FTSENT *);
118
119 int
120 main(int argc, char *argv[])
121 {
122 FTS *fts;
123 FTSENT *p;
124 off_t savednumber = 0;
125 long blocksize;
126 int ftsoptions;
127 int listall;
128 int depth;
129 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
130 char **save;
131 static char dot[] = ".";
132 off_t *ftsnum, *ftsparnum;
133
134 setlocale(LC_ALL, "");
135
136 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
137
138 save = argv;
139 ftsoptions = FTS_NOCHDIR;
140 depth = INT_MAX;
141 SLIST_INIT(&ignores);
142
143 while ((ch = getopt(argc, argv, "HI:LPasd:cghkmrx")) != -1)
144 switch (ch) {
145 case 'H':
146 Lflag = Pflag = 0;
147 Hflag = 1;
148 break;
149 case 'I':
150 ignoreadd(optarg);
151 break;
152 case 'L':
153 Hflag = Pflag = 0;
154 Lflag = 1;
155 break;
156 case 'P':
157 Hflag = Lflag = 0;
158 Pflag = 1;
159 break;
160 case 'a':
161 aflag = 1;
162 break;
163 case 's':
164 sflag = 1;
165 break;
166 case 'd':
167 dflag = 1;
168 errno = 0;
169 depth = atoi(optarg);
170 if (errno == ERANGE || depth < 0) {
171 warnx("invalid argument to option d: %s", optarg);
172 usage();
173 }
174 break;
175 case 'c':
176 cflag = 1;
177 break;
178 case 'h':
179 putenv("BLOCKSIZE=512");
180 hflag = 1;
181 valp = vals_base2;
182 break;
183 case 'k':
184 hflag = 0;
185 putenv("BLOCKSIZE=1024");
186 break;
187 case 'm':
188 hflag = 0;
189 putenv("BLOCKSIZE=1048576");
190 break;
191 case 'g':
192 hflag = 0;
193 putenv("BLOCKSIZE=1g");
194 break;
195 case 'r': /* Compatibility. */
196 break;
197 case 'x':
198 ftsoptions |= FTS_XDEV;
199 break;
200 case '?':
201 default:
202 usage();
203 }
204
205 // argc -= optind;
206 argv += optind;
207
208 /*
209 * XXX
210 * Because of the way that fts(3) works, logical walks will not count
211 * the blocks actually used by symbolic links. We rationalize this by
212 * noting that users computing logical sizes are likely to do logical
213 * copies, so not counting the links is correct. The real reason is
214 * that we'd have to re-implement the kernel's symbolic link traversing
215 * algorithm to get this right. If, for example, you have relative
216 * symbolic links referencing other relative symbolic links, it gets
217 * very nasty, very fast. The bottom line is that it's documented in
218 * the man page, so it's a feature.
219 */
220
221 if (Hflag + Lflag + Pflag > 1)
222 usage();
223
224 if (Hflag + Lflag + Pflag == 0)
225 Pflag = 1; /* -P (physical) is default */
226
227 if (Hflag)
228 ftsoptions |= FTS_COMFOLLOW;
229
230 if (Lflag)
231 ftsoptions |= FTS_LOGICAL;
232
233 if (Pflag)
234 ftsoptions |= FTS_PHYSICAL;
235
236 listall = 0;
237
238 if (aflag) {
239 if (sflag || dflag)
240 usage();
241 listall = 1;
242 } else if (sflag) {
243 if (dflag)
244 usage();
245 depth = 0;
246 }
247
248 if (!*argv) {
249 argv = save;
250 argv[0] = dot;
251 argv[1] = NULL;
252 }
253
254 (void) getbsize(&notused, &blocksize);
255 blocksize /= 512;
256
257 #ifdef __APPLE__
258 // "du" should not have any side effect on disk usage,
259 // so prevent materializing dataless directories upon traversal
260 rval = 1;
261 (void) sysctlbyname("vfs.nspace.prevent_materialization", NULL, NULL, &rval, sizeof(rval));
262 #endif /* __APPLE__ */
263
264 rval = 0;
265
266 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
267 err(1, "fts_open");
268
269 while ((p = fts_read(fts)) != NULL) {
270 switch (p->fts_info) {
271 case FTS_D:
272 if (ignorep(p) || dirlinkchk(p))
273 fts_set(fts, p, FTS_SKIP);
274 break;
275 case FTS_DP:
276 if (ignorep(p))
277 break;
278
279 ftsparnum = (off_t *)&p->fts_parent->fts_number;
280 ftsnum = (off_t *)&p->fts_number;
281 ftsparnum[0] += ftsnum[0] += p->fts_statp->st_blocks;
282
283 if (p->fts_level <= depth) {
284 if (hflag) {
285 (void) prthumanval(howmany(*ftsnum, blocksize));
286 (void) printf("\t%s\n", p->fts_path);
287 } else {
288 (void) printf("%jd\t%s\n",
289 (intmax_t)howmany(*ftsnum, blocksize),
290 p->fts_path);
291 }
292 }
293 break;
294 case FTS_DC: /* Ignore. */
295 if (COMPAT_MODE("bin/du", "unix2003")) {
296 errx(1, "Can't follow symlink cycle from %s to %s", p->fts_path, p->fts_cycle->fts_path);
297 }
298 break;
299 case FTS_DNR: /* Warn, continue. */
300 case FTS_ERR:
301 case FTS_NS:
302 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
303 rval = 1;
304 break;
305 case FTS_SLNONE:
306 if (COMPAT_MODE("bin/du", "unix2003")) {
307 struct stat sb;
308 int rc = stat(p->fts_path, &sb);
309 if (rc < 0 && errno == ELOOP) {
310 errx(1, "Too many symlinks at %s", p->fts_path);
311 }
312 }
313 default:
314 if (ignorep(p))
315 break;
316
317 if (p->fts_statp->st_nlink > 1 && linkchk(p))
318 break;
319
320 if (listall || p->fts_level == 0) {
321 if (hflag) {
322 (void) prthumanval(howmany(p->fts_statp->st_blocks,
323 blocksize));
324 (void) printf("\t%s\n", p->fts_path);
325 } else {
326 (void) printf("%jd\t%s\n",
327 (intmax_t)howmany(p->fts_statp->st_blocks, blocksize),
328 p->fts_path);
329 }
330 }
331
332 ftsparnum = (off_t *)&p->fts_parent->fts_number;
333 ftsparnum[0] += p->fts_statp->st_blocks;
334 }
335 savednumber = ((off_t *)&p->fts_parent->fts_number)[0];
336 }
337
338 if (errno)
339 err(1, "fts_read");
340
341 if (cflag) {
342 if (hflag) {
343 (void) prthumanval(howmany(savednumber, blocksize));
344 (void) printf("\ttotal\n");
345 } else {
346 (void) printf("%jd\ttotal\n", (intmax_t)howmany(savednumber, blocksize));
347 }
348 }
349
350 ignoreclean();
351 exit(rval);
352 }
353
354 static int
355 linkchk(FTSENT *p)
356 {
357 struct links_entry {
358 struct links_entry *next;
359 struct links_entry *previous;
360 int links;
361 dev_t dev;
362 ino_t ino;
363 };
364 static const size_t links_hash_initial_size = 8192;
365 static struct links_entry **buckets;
366 static struct links_entry *free_list;
367 static size_t number_buckets;
368 static unsigned long number_entries;
369 static char stop_allocating;
370 struct links_entry *le, **new_buckets;
371 struct stat *st;
372 size_t i, new_size;
373 int hash;
374
375 st = p->fts_statp;
376
377 /* If necessary, initialize the hash table. */
378 if (buckets == NULL) {
379 number_buckets = links_hash_initial_size;
380 buckets = malloc(number_buckets * sizeof(buckets[0]));
381 if (buckets == NULL)
382 errx(1, "No memory for hardlink detection");
383 for (i = 0; i < number_buckets; i++)
384 buckets[i] = NULL;
385 }
386
387 /* If the hash table is getting too full, enlarge it. */
388 if (number_entries > number_buckets * 10 && !stop_allocating) {
389 new_size = number_buckets * 2;
390 new_buckets = malloc(new_size * sizeof(struct links_entry *));
391
392 /* Try releasing the free list to see if that helps. */
393 if (new_buckets == NULL && free_list != NULL) {
394 while (free_list != NULL) {
395 le = free_list;
396 free_list = le->next;
397 free(le);
398 }
399 new_buckets = malloc(new_size * sizeof(new_buckets[0]));
400 }
401
402 if (new_buckets == NULL) {
403 stop_allocating = 1;
404 warnx("No more memory for tracking hard links");
405 } else {
406 memset(new_buckets, 0,
407 new_size * sizeof(struct links_entry *));
408 for (i = 0; i < number_buckets; i++) {
409 while (buckets[i] != NULL) {
410 /* Remove entry from old bucket. */
411 le = buckets[i];
412 buckets[i] = le->next;
413
414 /* Add entry to new bucket. */
415 hash = (le->dev ^ le->ino) % new_size;
416
417 if (new_buckets[hash] != NULL)
418 new_buckets[hash]->previous =
419 le;
420 le->next = new_buckets[hash];
421 le->previous = NULL;
422 new_buckets[hash] = le;
423 }
424 }
425 free(buckets);
426 buckets = new_buckets;
427 number_buckets = new_size;
428 }
429 }
430
431 /* Try to locate this entry in the hash table. */
432 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
433 for (le = buckets[hash]; le != NULL; le = le->next) {
434 if (le->dev == st->st_dev && le->ino == st->st_ino) {
435 /*
436 * Save memory by releasing an entry when we've seen
437 * all of it's links.
438 */
439 if (--le->links <= 0) {
440 if (le->previous != NULL)
441 le->previous->next = le->next;
442 if (le->next != NULL)
443 le->next->previous = le->previous;
444 if (buckets[hash] == le)
445 buckets[hash] = le->next;
446 number_entries--;
447 /* Recycle this node through the free list */
448 if (stop_allocating) {
449 free(le);
450 } else {
451 le->next = free_list;
452 free_list = le;
453 }
454 }
455 return (1);
456 }
457 }
458
459 if (stop_allocating)
460 return (0);
461
462 /* Add this entry to the links cache. */
463 if (free_list != NULL) {
464 /* Pull a node from the free list if we can. */
465 le = free_list;
466 free_list = le->next;
467 } else
468 /* Malloc one if we have to. */
469 le = malloc(sizeof(struct links_entry));
470 if (le == NULL) {
471 stop_allocating = 1;
472 warnx("No more memory for tracking hard links");
473 return (0);
474 }
475 le->dev = st->st_dev;
476 le->ino = st->st_ino;
477 le->links = st->st_nlink - 1;
478 number_entries++;
479 le->next = buckets[hash];
480 le->previous = NULL;
481 if (buckets[hash] != NULL)
482 buckets[hash]->previous = le;
483 buckets[hash] = le;
484 return (0);
485 }
486
487 static int
488 dirlinkchk(FTSENT *p)
489 {
490 struct links_entry {
491 struct links_entry *next;
492 struct links_entry *previous;
493 int links;
494 dev_t dev;
495 ino_t ino;
496 };
497 static const size_t links_hash_initial_size = 8192;
498 static struct links_entry **buckets;
499 static struct links_entry *free_list;
500 static size_t number_buckets;
501 static unsigned long number_entries;
502 static char stop_allocating;
503 struct links_entry *le, **new_buckets;
504 struct stat *st;
505 size_t i, new_size;
506 int hash;
507 struct attrbuf {
508 int size;
509 int linkcount;
510 } buf;
511 struct attrlist attrList;
512
513 memset(&attrList, 0, sizeof(attrList));
514 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
515 attrList.dirattr = ATTR_DIR_LINKCOUNT;
516 if (-1 == getattrlist(p->fts_path, &attrList, &buf, sizeof(buf), 0))
517 return 0;
518 if (buf.linkcount == 1)
519 return 0;
520 st = p->fts_statp;
521
522 /* If necessary, initialize the hash table. */
523 if (buckets == NULL) {
524 number_buckets = links_hash_initial_size;
525 buckets = malloc(number_buckets * sizeof(buckets[0]));
526 if (buckets == NULL)
527 errx(1, "No memory for directory hardlink detection");
528 for (i = 0; i < number_buckets; i++)
529 buckets[i] = NULL;
530 }
531
532 /* If the hash table is getting too full, enlarge it. */
533 if (number_entries > number_buckets * 10 && !stop_allocating) {
534 new_size = number_buckets * 2;
535 new_buckets = malloc(new_size * sizeof(struct links_entry *));
536
537 /* Try releasing the free list to see if that helps. */
538 if (new_buckets == NULL && free_list != NULL) {
539 while (free_list != NULL) {
540 le = free_list;
541 free_list = le->next;
542 free(le);
543 }
544 new_buckets = malloc(new_size * sizeof(new_buckets[0]));
545 }
546
547 if (new_buckets == NULL) {
548 stop_allocating = 1;
549 warnx("No more memory for tracking directory hard links");
550 } else {
551 memset(new_buckets, 0,
552 new_size * sizeof(struct links_entry *));
553 for (i = 0; i < number_buckets; i++) {
554 while (buckets[i] != NULL) {
555 /* Remove entry from old bucket. */
556 le = buckets[i];
557 buckets[i] = le->next;
558
559 /* Add entry to new bucket. */
560 hash = (le->dev ^ le->ino) % new_size;
561
562 if (new_buckets[hash] != NULL)
563 new_buckets[hash]->previous =
564 le;
565 le->next = new_buckets[hash];
566 le->previous = NULL;
567 new_buckets[hash] = le;
568 }
569 }
570 free(buckets);
571 buckets = new_buckets;
572 number_buckets = new_size;
573 }
574 }
575
576 /* Try to locate this entry in the hash table. */
577 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
578 for (le = buckets[hash]; le != NULL; le = le->next) {
579 if (le->dev == st->st_dev && le->ino == st->st_ino) {
580 /*
581 * Save memory by releasing an entry when we've seen
582 * all of it's links.
583 */
584 if (--le->links <= 0) {
585 if (le->previous != NULL)
586 le->previous->next = le->next;
587 if (le->next != NULL)
588 le->next->previous = le->previous;
589 if (buckets[hash] == le)
590 buckets[hash] = le->next;
591 number_entries--;
592 /* Recycle this node through the free list */
593 if (stop_allocating) {
594 free(le);
595 } else {
596 le->next = free_list;
597 free_list = le;
598 }
599 }
600 return (1);
601 }
602 }
603
604 if (stop_allocating)
605 return (0);
606 /* Add this entry to the links cache. */
607 if (free_list != NULL) {
608 /* Pull a node from the free list if we can. */
609 le = free_list;
610 free_list = le->next;
611 } else
612 /* Malloc one if we have to. */
613 le = malloc(sizeof(struct links_entry));
614 if (le == NULL) {
615 stop_allocating = 1;
616 warnx("No more memory for tracking hard links");
617 return (0);
618 }
619 le->dev = st->st_dev;
620 le->ino = st->st_ino;
621 le->links = buf.linkcount - 1;
622 number_entries++;
623 le->next = buckets[hash];
624 le->previous = NULL;
625 if (buckets[hash] != NULL)
626 buckets[hash]->previous = le;
627 buckets[hash] = le;
628 return (0);
629 }
630
631 /*
632 * Output in "human-readable" format. Uses 3 digits max and puts
633 * unit suffixes at the end. Makes output compact and easy to read,
634 * especially on huge disks.
635 *
636 */
637 unit_t
638 unit_adjust(double *val)
639 {
640 double abval;
641 unit_t unit;
642 unsigned int unit_sz;
643
644 abval = fabs(*val);
645
646 unit_sz = abval ? ilogb(abval) / 10 : 0;
647
648 if (unit_sz >= UNIT_MAX) {
649 unit = NONE;
650 } else {
651 unit = unitp[unit_sz];
652 *val /= (double)valp[unit_sz];
653 }
654
655 return (unit);
656 }
657
658 void
659 prthumanval(double bytes)
660 {
661 unit_t unit;
662
663 bytes *= 512;
664 unit = unit_adjust(&bytes);
665
666 if (bytes == 0)
667 (void)printf(" 0B");
668 else if (bytes > 10)
669 (void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
670 else
671 (void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
672 }
673
674 static void
675 usage(void)
676 {
677 (void)fprintf(stderr,
678 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k | -m | -g] [-x] [-I mask] [file ...]\n");
679 exit(EX_USAGE);
680 }
681
682 void
683 ignoreadd(const char *mask)
684 {
685 struct ignentry *ign;
686
687 ign = calloc(1, sizeof(*ign));
688 if (ign == NULL)
689 errx(1, "cannot allocate memory");
690 ign->mask = strdup(mask);
691 if (ign->mask == NULL)
692 errx(1, "cannot allocate memory");
693 SLIST_INSERT_HEAD(&ignores, ign, next);
694 }
695
696 void
697 ignoreclean(void)
698 {
699 struct ignentry *ign;
700
701 while (!SLIST_EMPTY(&ignores)) {
702 ign = SLIST_FIRST(&ignores);
703 SLIST_REMOVE_HEAD(&ignores, next);
704 free(ign->mask);
705 free(ign);
706 }
707 }
708
709 int
710 ignorep(FTSENT *ent)
711 {
712 struct ignentry *ign;
713
714 #ifdef __APPLE__
715 if (S_ISDIR(ent->fts_statp->st_mode) && !strcmp("fd", ent->fts_name)) {
716 struct statfs sfsb;
717 int rc = statfs(ent->fts_accpath, &sfsb);
718 if (rc >= 0 && !strcmp("devfs", sfsb.f_fstypename)) {
719 /* Don't cd into /dev/fd/N since one of those is likely to be
720 the cwd as of the start of du which causes all manner of
721 unpleasant surprises */
722 return 1;
723 }
724 }
725 #endif /* __APPLE__ */
726 SLIST_FOREACH(ign, &ignores, next)
727 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
728 return 1;
729 return 0;
730 }