1 /* du -- summarize disk usage
2 Copyright (C) 1988-1991, 1995-2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Differences from the Unix du:
19 * Doesn't simply ignore the names of regular files given as arguments
22 By tege@sics.se, Torbjorn Granlund,
23 and djm@ai.mit.edu, David MacKenzie.
24 Variable blocks added by lm@sgi.com and eggert@twinsun.com.
25 Rewritten to use nftw, then to use fts by Jim Meyering. */
30 #include <sys/types.h>
34 #include "dirname.h" /* for strip_trailing_slashes */
45 /* The official name of this program (e.g., no `g' prefix). */
46 #define PROGRAM_NAME "du"
49 "Torbjorn Granlund", "David MacKenzie, Paul Eggert", "Jim Meyering"
51 /* Initial size of the hash table. */
52 #define INITIAL_TABLE_SIZE 103
54 /* Hash structure for inode and device numbers. The separate entry
55 structure makes it easier to rehash "in place". */
63 /* A set of dev/ino pairs. */
64 static Hash_table *htab;
66 /* Name under which this program was invoked. */
69 /* If nonzero, display counts for all files, not just directories. */
70 static int opt_all = 0;
72 /* If nonzero, rather than using the disk usage of each file,
73 use the apparent size (a la stat.st_size). */
74 static int apparent_size = 0;
76 /* If nonzero, count each hard link of files with multiple links. */
77 static int opt_count_all = 0;
79 /* If nonzero, print a grand total at the end. */
80 static int print_totals = 0;
82 /* If nonzero, do not add sizes of subdirectories. */
83 static int opt_separate_dirs = 0;
85 /* Show the total for each directory (and file if --all) that is at
86 most MAX_DEPTH levels down from the root of the hierarchy. The root
87 is at level 0, so `du --max-depth=0' is equivalent to `du -s'. */
88 static int max_depth = INT_MAX;
90 /* Human-readable options for output. */
91 static int human_output_opts;
93 /* The units to use when printing sizes. */
94 static uintmax_t output_block_size;
96 /* File name patterns to exclude. */
97 static struct exclude *exclude;
99 /* Grand total size of all args, in bytes. */
100 static uintmax_t tot_size = 0;
102 /* Nonzero indicates that du should exit with EXIT_FAILURE upon completion. */
105 #define IS_DIR_TYPE(Type) \
107 || (Type) == FTS_DNR)
109 /* For long options that have no equivalent short option, use a
110 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
113 APPARENT_SIZE_OPTION = CHAR_MAX + 1,
119 static struct option const long_options[] =
121 {"all", no_argument, NULL, 'a'},
122 {"apparent-size", no_argument, NULL, APPARENT_SIZE_OPTION},
123 {"block-size", required_argument, 0, 'B'},
124 {"bytes", no_argument, NULL, 'b'},
125 {"count-links", no_argument, NULL, 'l'},
126 {"dereference", no_argument, NULL, 'L'},
127 {"dereference-args", no_argument, NULL, 'D'},
128 {"exclude", required_argument, 0, EXCLUDE_OPTION},
129 {"exclude-from", required_argument, 0, 'X'},
130 {"human-readable", no_argument, NULL, 'h'},
131 {"si", no_argument, 0, HUMAN_SI_OPTION},
132 {"kilobytes", no_argument, NULL, 'k'}, /* long form is obsolescent */
133 {"max-depth", required_argument, NULL, MAX_DEPTH_OPTION},
134 {"megabytes", no_argument, NULL, 'm'}, /* obsolescent */
135 {"no-dereference", no_argument, NULL, 'P'},
136 {"one-file-system", no_argument, NULL, 'x'},
137 {"separate-dirs", no_argument, NULL, 'S'},
138 {"summarize", no_argument, NULL, 's'},
139 {"total", no_argument, NULL, 'c'},
140 {GETOPT_HELP_OPTION_DECL},
141 {GETOPT_VERSION_OPTION_DECL},
149 fprintf (stderr, _("Try `%s --help' for more information.\n"),
153 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
155 Summarize disk usage of each FILE, recursively for directories.\n\
159 Mandatory arguments to long options are mandatory for short options too.\n\
162 -a, --all write counts for all files, not just directories\n\
163 --apparent-size print apparent sizes, rather than disk usage; although\n\
164 the apparent size is usually smaller, it may be\n\
165 larger due to holes in (`sparse') files, internal\n\
166 fragmentation, indirect blocks, and the like\n\
167 -B, --block-size=SIZE use SIZE-byte blocks\n\
168 -b, --bytes equivalent to `--apparent-size --block-size=1'\n\
169 -c, --total produce a grand total\n\
170 -D, --dereference-args dereference FILEs that are symbolic links\n\
173 -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)\n\
174 -H, --si likewise, but use powers of 1000 not 1024\n\
175 -k like --block-size=1K\n\
176 -l, --count-links count sizes many times if hard linked\n\
179 -L, --dereference dereference all symbolic links\n\
180 -P, --no-dereference don't follow any symbolic links (this is the default)\n\
181 -S, --separate-dirs do not include size of subdirectories\n\
182 -s, --summarize display only a total for each argument\n\
185 -x, --one-file-system skip directories on different filesystems\n\
186 -X FILE, --exclude-from=FILE Exclude files that match any pattern in FILE.\n\
187 --exclude=PATTERN Exclude files that match PATTERN.\n\
188 --max-depth=N print the total for a directory (or file, with --all)\n\
189 only if it is N or fewer levels below the command\n\
190 line argument; --max-depth=0 is the same as\n\
193 fputs (HELP_OPTION_DESCRIPTION, stdout);
194 fputs (VERSION_OPTION_DESCRIPTION, stdout);
196 SIZE may be (or may be an integer optionally followed by) one of following:\n\
197 kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n\
199 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
205 entry_hash (void const *x, unsigned int table_size)
207 struct entry const *p = x;
209 /* Ignoring the device number here should be fine. */
210 /* The cast to uintmax_t prevents negative remainders
211 if st_ino is negative. */
212 return (uintmax_t) p->st_ino % table_size;
215 /* Compare two dev/ino pairs. Return true if they are the same. */
217 entry_compare (void const *x, void const *y)
219 struct entry const *a = x;
220 struct entry const *b = y;
221 return SAME_INODE (*a, *b) ? true : false;
224 /* Try to insert the INO/DEV pair into the global table, HTAB.
225 If the pair is successfully inserted, return zero.
226 Upon failed memory allocation exit nonzero.
227 If the pair is already in the table, return nonzero. */
229 hash_ins (ino_t ino, dev_t dev)
232 struct entry *ent_from_table;
234 ent = xmalloc (sizeof *ent);
238 ent_from_table = hash_insert (htab, ent);
239 if (ent_from_table == NULL)
241 /* Insertion failed due to lack of memory. */
245 if (ent_from_table == ent)
247 /* Insertion succeeded. */
251 /* That pair is already in the table, so ENT was not inserted. Free it. */
257 /* Initialize the hash table. */
261 htab = hash_initialize (INITIAL_TABLE_SIZE, NULL,
262 entry_hash, entry_compare, free);
267 /* Print N_BYTES. Convert it to a readable value before printing. */
270 print_only_size (uintmax_t n_bytes)
272 char buf[LONGEST_HUMAN_READABLE + 1];
273 fputs (human_readable (n_bytes, buf, human_output_opts,
274 1, output_block_size), stdout);
277 /* Print N_BYTES followed by STRING on a line.
278 Convert N_BYTES to a readable value before printing. */
281 print_size (uintmax_t n_bytes, const char *string)
283 print_only_size (n_bytes);
284 printf ("\t%s\n", string);
288 /* This function is called once for every file system object that fts
289 encounters. fts does a depth-first traversal. This function knows
290 that and accumulates per-directory totals based on changes in
291 the depth of the current entry. */
294 process_file (FTS *fts, FTSENT *ent)
297 uintmax_t size_to_print;
298 static int first_call = 1;
299 static size_t prev_level;
300 static size_t n_alloc;
301 /* The sum of the st_size values of all entries in the single directory
302 at the corresponding level. Although this does include the st_size
303 corresponding to each subdirectory, it does not include the size of
304 any file in a subdirectory. */
305 static uintmax_t *sum_ent;
307 /* The sum of the sizes of all entries in the hierarchy at or below the
308 directory at the specified level. */
309 static uintmax_t *sum_subdir;
312 const char *file = ent->fts_path;
313 const struct stat *sb = ent->fts_statp;
316 /* If necessary, set FTS_SKIP before returning. */
317 skip = excluded_filename (exclude, ent->fts_name);
319 fts_set (fts, ent, FTS_SKIP);
321 switch (ent->fts_info)
324 error (0, ent->fts_errno, _("cannot access %s"), quote (file));
329 /* if (S_ISDIR (ent->fts_statp->st_mode) && FIXME */
330 error (0, ent->fts_errno, _("%s"), quote (file));
335 /* Don't return just yet, since although the directory is not readable,
336 we were able to stat it, so we do have a size. */
337 error (0, ent->fts_errno, _("cannot read directory %s"), quote (file));
345 /* If this is the first (pre-order) encounter with a directory,
346 or if it's the second encounter for a skipped directory, then
347 return right away. */
348 if (ent->fts_info == FTS_D || skip)
351 /* If the file is being excluded or if it has already been counted
352 via a hard link, then don't let it contribute to the sums. */
356 && hash_ins (sb->st_ino, sb->st_dev)))
358 /* Note that we must not simply return here.
359 We still have to update prev_level and maybe propagate
360 some sums up the hierarchy. */
366 size = (apparent_size
368 : ST_NBLOCKS (*sb) * ST_NBLOCKSIZE);
373 n_alloc = ent->fts_level + 10;
374 sum_ent = XCALLOC (uintmax_t, n_alloc);
375 sum_subdir = XCALLOC (uintmax_t, n_alloc);
379 /* FIXME: it's a shame that we need these `size_t' casts to avoid
380 warnings from gcc about `comparison between signed and unsigned'.
381 Probably unavoidable, assuming that the struct members
382 are of type `int' (historical), since I want variables like
383 n_alloc and prev_level to have types that make sense. */
384 if (n_alloc <= (size_t) ent->fts_level)
386 n_alloc = ent->fts_level * 2;
387 sum_ent = XREALLOC (sum_ent, uintmax_t, n_alloc);
388 sum_subdir = XREALLOC (sum_subdir, uintmax_t, n_alloc);
392 size_to_print = size;
396 if ((size_t) ent->fts_level == prev_level)
398 /* This is usually the most common case. Do nothing. */
400 else if (ent->fts_level > prev_level)
402 /* Descending the hierarchy.
403 Clear the accumulators for *all* levels between prev_level
404 and the current one. The depth may change dramatically,
405 e.g., from 1 to 10. */
407 for (i = prev_level + 1; i <= ent->fts_level; i++)
413 else /* ent->fts_level < prev_level */
415 /* Ascending the hierarchy.
416 Process a directory only after all entries in that
417 directory have been processed. When the depth decreases,
418 propagate sums from the children (prev_level) to the parent.
419 Here, the current level is always one smaller than the
421 assert ((size_t) ent->fts_level == prev_level - 1);
422 size_to_print += sum_ent[prev_level];
423 if (!opt_separate_dirs)
424 size_to_print += sum_subdir[prev_level];
425 sum_subdir[ent->fts_level] += (sum_ent[prev_level]
426 + sum_subdir[prev_level]);
430 prev_level = ent->fts_level;
433 /* Let the size of a directory entry contribute to the total for the
434 containing directory, unless --separate-dirs (-S) is specified. */
435 if ( ! (opt_separate_dirs && IS_DIR_TYPE (ent->fts_info)))
436 sum_ent[ent->fts_level] += size;
438 /* Even if this directory is unreadable or we can't chdir into it,
439 do let its size contribute to the total, ... */
442 /* ... but don't print out a total for it, since without the size(s)
443 of any potential entries, it could be very misleading. */
444 if (ent->fts_info == FTS_DNR)
447 /* If we're not counting an entry, e.g., because it's a hard link
448 to a file we've already counted (and --count-links), then don't
449 print a line for it. */
453 if ((IS_DIR_TYPE (ent->fts_info) && ent->fts_level <= max_depth)
454 || ((opt_all && ent->fts_level <= max_depth) || ent->fts_level == 0))
456 print_only_size (size_to_print);
457 fputc ('\t', stdout);
458 fputs (file, stdout);
459 fputc ('\n', stdout);
464 /* Recursively print the sizes of the directories (and, if selected, files)
465 named in FILES, the last entry of which is NULL.
466 BIT_FLAGS controls how fts works.
467 If the fts_open call fails, exit nonzero.
468 Otherwise, return nonzero upon error. */
471 du_files (char **files, int bit_flags)
475 FTS *fts = xfts_open (files, bit_flags, NULL);
481 ent = fts_read (fts);
486 /* FIXME: try to give a better message */
487 error (0, errno, _("fts_read failed"));
493 /* This is a space optimization. If we aren't printing totals,
494 then it's ok to clear the duplicate-detection tables after
495 each command line hierarchy has been processed. */
496 if (ent->fts_level == 0 && ent->fts_info == FTS_D && !print_totals)
499 process_file (fts, ent);
502 /* Ignore failure, since the only way it can do so is in failing to
503 return to the original directory, and since we're about to exit,
504 that doesn't matter. */
508 print_size (tot_size, _("total"));
514 main (int argc, char **argv)
518 int max_depth_specified = 0;
522 /* Bit flags that control how fts works. */
523 int bit_flags = FTS_PHYSICAL;
525 /* If nonzero, display only a total for each argument. */
526 int opt_summarize_only = 0;
531 initialize_main (&argc, &argv);
532 program_name = argv[0];
533 setlocale (LC_ALL, "");
534 bindtextdomain (PACKAGE, LOCALEDIR);
535 textdomain (PACKAGE);
537 atexit (close_stdout);
539 exclude = new_exclude ();
541 human_output_opts = human_options (getenv ("DU_BLOCK_SIZE"), false,
545 while ((c = getopt_long (argc, argv, "abchHklmsxB:DLPSX:",
546 long_options, NULL)) != -1)
551 case 0: /* Long option. */
558 case APPARENT_SIZE_OPTION:
564 human_output_opts = 0;
565 output_block_size = 1;
573 human_output_opts = human_autoscale | human_SI | human_base_1024;
574 output_block_size = 1;
578 error (0, 0, _("WARNING: use --si, not -H; the meaning of the -H\
579 option will soon\nchange to be the same as that of --dereference-args (-D)"));
581 case HUMAN_SI_OPTION:
582 human_output_opts = human_autoscale | human_SI;
583 output_block_size = 1;
587 human_output_opts = 0;
588 output_block_size = 1024;
591 case MAX_DEPTH_OPTION: /* --max-depth=N */
592 if (xstrtol (optarg, NULL, 0, &tmp_long, NULL) == LONGINT_OK
593 && 0 <= tmp_long && tmp_long <= INT_MAX)
595 max_depth_specified = 1;
596 max_depth = (int) tmp_long;
600 error (0, 0, _("invalid maximum depth %s"),
606 case 'm': /* obsolescent: FIXME: remove in 2005. */
607 human_output_opts = 0;
608 output_block_size = 1024 * 1024;
616 opt_summarize_only = 1;
620 bit_flags |= FTS_XDEV;
624 human_output_opts = human_options (optarg, true, &output_block_size);
627 case 'D': /* This will eventually be 'H' (-H), too. */
628 bit_flags = FTS_COMFOLLOW;
631 case 'L': /* --dereference */
632 bit_flags = FTS_LOGICAL;
635 case 'P': /* --no-dereference */
636 bit_flags = FTS_PHYSICAL;
640 opt_separate_dirs = 1;
644 if (add_exclude_file (add_exclude, exclude, optarg,
645 EXCLUDE_WILDCARDS, '\n'))
647 error (0, errno, "%s", quotearg_colon (optarg));
653 add_exclude (exclude, optarg, EXCLUDE_WILDCARDS);
656 case_GETOPT_HELP_CHAR;
658 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
666 usage (EXIT_FAILURE);
668 if (opt_all && opt_summarize_only)
670 error (0, 0, _("cannot both summarize and show all entries"));
671 usage (EXIT_FAILURE);
674 if (opt_summarize_only && max_depth_specified && max_depth == 0)
677 _("warning: summarizing is the same as using --max-depth=0"));
680 if (opt_summarize_only && max_depth_specified && max_depth != 0)
683 _("warning: summarizing conflicts with --max-depth=%d"),
685 usage (EXIT_FAILURE);
688 if (opt_summarize_only)
691 files = (optind < argc ? argv + optind : cwd_only);
693 /* Initialize the hash structure for inode numbers. */
696 exit (du_files (files, bit_flags) || G_fail
697 ? EXIT_FAILURE : EXIT_SUCCESS);