58c799ab3e06308fb2403f2a79016ee879ad3eba
[platform/upstream/coreutils.git] / src / du.c
1 /* du -- summarize disk usage
2    Copyright (C) 1988-1991, 1995-2003 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 /* Differences from the Unix du:
19    * Doesn't simply ignore the names of regular files given as arguments
20      when -a is given.
21
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.  */
26
27 #include <config.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <sys/types.h>
31 #include <assert.h>
32
33 #include "system.h"
34 #include "dirname.h" /* for strip_trailing_slashes */
35 #include "error.h"
36 #include "exclude.h"
37 #include "hash.h"
38 #include "human.h"
39 #include "quote.h"
40 #include "quotearg.h"
41 #include "same.h"
42 #include "xfts.h"
43 #include "xstrtol.h"
44
45 /* The official name of this program (e.g., no `g' prefix).  */
46 #define PROGRAM_NAME "du"
47
48 #define WRITTEN_BY \
49   _("Written by Torbjorn Granlund, David MacKenzie, Paul Eggert, and Jim Meyering.")
50
51 /* Initial size of the hash table.  */
52 #define INITIAL_TABLE_SIZE 103
53
54 /* Hash structure for inode and device numbers.  The separate entry
55    structure makes it easier to rehash "in place".  */
56
57 struct entry
58 {
59   ino_t st_ino;
60   dev_t st_dev;
61 };
62
63 /* A set of dev/ino pairs.  */
64 static Hash_table *htab;
65
66 /* Name under which this program was invoked.  */
67 char *program_name;
68
69 /* If nonzero, display counts for all files, not just directories.  */
70 static int opt_all = 0;
71
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;
75
76 /* If nonzero, count each hard link of files with multiple links.  */
77 static int opt_count_all = 0;
78
79 /* If nonzero, print a grand total at the end.  */
80 static int print_totals = 0;
81
82 /* If nonzero, do not add sizes of subdirectories.  */
83 static int opt_separate_dirs = 0;
84
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;
89
90 /* Human-readable options for output.  */
91 static int human_output_opts;
92
93 /* The units to use when printing sizes.  */
94 static uintmax_t output_block_size;
95
96 /* File name patterns to exclude.  */
97 static struct exclude *exclude;
98
99 /* Grand total size of all args, in bytes. */
100 static uintmax_t tot_size = 0;
101
102 /* Nonzero indicates that du should exit with EXIT_FAILURE upon completion.  */
103 int G_fail;
104
105 #define IS_DIR_TYPE(Type)       \
106   ((Type) == FTS_DP             \
107    || (Type) == FTS_DNR)
108
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.  */
111 enum
112 {
113   APPARENT_SIZE_OPTION = CHAR_MAX + 1,
114   EXCLUDE_OPTION,
115   HUMAN_SI_OPTION,
116   MAX_DEPTH_OPTION
117 };
118
119 static struct option const long_options[] =
120 {
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},
142   {NULL, 0, NULL, 0}
143 };
144
145 void
146 usage (int status)
147 {
148   if (status != 0)
149     fprintf (stderr, _("Try `%s --help' for more information.\n"),
150              program_name);
151   else
152     {
153       printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
154       fputs (_("\
155 Summarize disk usage of each FILE, recursively for directories.\n\
156 \n\
157 "), stdout);
158       fputs (_("\
159 Mandatory arguments to long options are mandatory for short options too.\n\
160 "), stdout);
161       fputs (_("\
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\
171 "), stdout);
172       fputs (_("\
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\
177 "), stdout);
178       fputs (_("\
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\
183 "), stdout);
184       fputs (_("\
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\
191                           --summarize\n\
192 "), stdout);
193       fputs (HELP_OPTION_DESCRIPTION, stdout);
194       fputs (VERSION_OPTION_DESCRIPTION, stdout);
195       fputs (_("\n\
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\
198 "), stdout);
199       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
200     }
201   exit (status);
202 }
203
204 static unsigned int
205 entry_hash (void const *x, unsigned int table_size)
206 {
207   struct entry const *p = x;
208
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;
213 }
214
215 /* Compare two dev/ino pairs.  Return true if they are the same.  */
216 static bool
217 entry_compare (void const *x, void const *y)
218 {
219   struct entry const *a = x;
220   struct entry const *b = y;
221   return SAME_INODE (*a, *b) ? true : false;
222 }
223
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.  */
228 static int
229 hash_ins (ino_t ino, dev_t dev)
230 {
231   struct entry *ent;
232   struct entry *ent_from_table;
233
234   ent = xmalloc (sizeof *ent);
235   ent->st_ino = ino;
236   ent->st_dev = dev;
237
238   ent_from_table = hash_insert (htab, ent);
239   if (ent_from_table == NULL)
240     {
241       /* Insertion failed due to lack of memory.  */
242       xalloc_die ();
243     }
244
245   if (ent_from_table == ent)
246     {
247       /* Insertion succeeded.  */
248       return 0;
249     }
250
251   /* That pair is already in the table, so ENT was not inserted.  Free it.  */
252   free (ent);
253
254   return 1;
255 }
256
257 /* Initialize the hash table.  */
258 static void
259 hash_init (void)
260 {
261   htab = hash_initialize (INITIAL_TABLE_SIZE, NULL,
262                           entry_hash, entry_compare, free);
263   if (htab == NULL)
264     xalloc_die ();
265 }
266
267 /* Print N_BYTES.  Convert it to a readable value before printing.  */
268
269 static void
270 print_only_size (uintmax_t n_bytes)
271 {
272   char buf[LONGEST_HUMAN_READABLE + 1];
273   fputs (human_readable (n_bytes, buf, human_output_opts,
274                          1, output_block_size), stdout);
275 }
276
277 /* Print N_BYTES followed by STRING on a line.
278    Convert N_BYTES to a readable value before printing.  */
279
280 static void
281 print_size (uintmax_t n_bytes, const char *string)
282 {
283   print_only_size (n_bytes);
284   printf ("\t%s\n", string);
285   fflush (stdout);
286 }
287
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.  */
292
293 static void
294 process_file (FTS *fts, FTSENT *ent)
295 {
296   uintmax_t size;
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;
306
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;
310   int print = 1;
311
312   const char *file = ent->fts_path;
313   const struct stat *sb = ent->fts_statp;
314   int skip;
315
316   /* If necessary, set FTS_SKIP before returning.  */
317   skip = excluded_filename (exclude, ent->fts_name);
318   if (skip)
319     fts_set (fts, ent, FTS_SKIP);
320
321   switch (ent->fts_info)
322     {
323     case FTS_NS:
324       error (0, ent->fts_errno, _("cannot access %s"), quote (file));
325       G_fail = 1;
326       return;
327
328     case FTS_ERR:
329       /* if (S_ISDIR (ent->fts_statp->st_mode) && FIXME */
330       error (0, ent->fts_errno, _("%s"), quote (file));
331       G_fail = 1;
332       return;
333
334     case FTS_DNR:
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));
338       G_fail = 1;
339       break;
340
341     default:
342       break;
343     }
344
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)
349     return;
350
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.  */
353   if (skip
354       || (!opt_count_all
355           && 1 < sb->st_nlink
356           && hash_ins (sb->st_ino, sb->st_dev)))
357     {
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.  */
361       size = 0;
362       print = 0;
363     }
364   else
365     {
366       size = (apparent_size
367               ? sb->st_size
368               : ST_NBLOCKS (*sb) * ST_NBLOCKSIZE);
369     }
370
371   if (first_call)
372     {
373       n_alloc = ent->fts_level + 10;
374       sum_ent = XCALLOC (uintmax_t, n_alloc);
375       sum_subdir = XCALLOC (uintmax_t, n_alloc);
376     }
377   else
378     {
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)
385         {
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);
389         }
390     }
391
392   size_to_print = size;
393
394   if (! first_call)
395     {
396       if ((size_t) ent->fts_level == prev_level)
397         {
398           /* This is usually the most common case.  Do nothing.  */
399         }
400       else if (ent->fts_level > prev_level)
401         {
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.  */
406           int i;
407           for (i = prev_level + 1; i <= ent->fts_level; i++)
408             {
409               sum_ent[i] = 0;
410               sum_subdir[i] = 0;
411             }
412         }
413       else /* ent->fts_level < prev_level */
414         {
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
420              previous one.  */
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]);
427         }
428     }
429
430   prev_level = ent->fts_level;
431   first_call = 0;
432
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;
437
438   /* Even if this directory is unreadable or we can't chdir into it,
439      do let its size contribute to the total, ... */
440   tot_size += size;
441
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)
445     return;
446
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.  */
450   if (!print)
451     return;
452
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))
455     {
456       print_only_size (size_to_print);
457       fputc ('\t', stdout);
458       fputs (file, stdout);
459       fputc ('\n', stdout);
460       fflush (stdout);
461     }
462 }
463
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.  */
469
470 static int
471 du_files (char **files, int bit_flags)
472 {
473   int fail = 0;
474
475   FTS *fts = xfts_open (files, bit_flags, NULL);
476
477   while (1)
478     {
479       FTSENT *ent;
480
481       ent = fts_read (fts);
482       if (ent == NULL)
483         {
484           if (errno != 0)
485             {
486               /* FIXME: try to give a better message  */
487               error (0, errno, _("fts_read failed"));
488               fail = 1;
489             }
490           break;
491         }
492
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)
497         hash_clear (htab);
498
499       process_file (fts, ent);
500     }
501
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.  */
505   fts_close (fts);
506
507   if (print_totals)
508     print_size (tot_size, _("total"));
509
510   return fail;
511 }
512
513 int
514 main (int argc, char **argv)
515 {
516   int c;
517   char *cwd_only[2];
518   int max_depth_specified = 0;
519   char **files;
520   int fail;
521
522   /* Bit flags that control how fts works.  */
523   int bit_flags = FTS_PHYSICAL;
524
525   /* If nonzero, display only a total for each argument. */
526   int opt_summarize_only = 0;
527
528   cwd_only[0] = ".";
529   cwd_only[1] = NULL;
530
531   initialize_main (&argc, &argv);
532   program_name = argv[0];
533   setlocale (LC_ALL, "");
534   bindtextdomain (PACKAGE, LOCALEDIR);
535   textdomain (PACKAGE);
536
537   atexit (close_stdout);
538
539   exclude = new_exclude ();
540
541   human_output_opts = human_options (getenv ("DU_BLOCK_SIZE"), false,
542                                      &output_block_size);
543
544   fail = 0;
545   while ((c = getopt_long (argc, argv, "abchHklmsxB:DLPSX:",
546                            long_options, NULL)) != -1)
547     {
548       long int tmp_long;
549       switch (c)
550         {
551         case 0:                 /* Long option. */
552           break;
553
554         case 'a':
555           opt_all = 1;
556           break;
557
558         case APPARENT_SIZE_OPTION:
559           apparent_size = 1;
560           break;
561
562         case 'b':
563           apparent_size = 1;
564           human_output_opts = 0;
565           output_block_size = 1;
566           break;
567
568         case 'c':
569           print_totals = 1;
570           break;
571
572         case 'h':
573           human_output_opts = human_autoscale | human_SI | human_base_1024;
574           output_block_size = 1;
575           break;
576
577         case 'H':
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)"));
580           /* fall through */
581         case HUMAN_SI_OPTION:
582           human_output_opts = human_autoscale | human_SI;
583           output_block_size = 1;
584           break;
585
586         case 'k':
587           human_output_opts = 0;
588           output_block_size = 1024;
589           break;
590
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)
594             {
595               max_depth_specified = 1;
596               max_depth = (int) tmp_long;
597             }
598           else
599             {
600               error (0, 0, _("invalid maximum depth %s"),
601                      quote (optarg));
602               fail = 1;
603             }
604           break;
605
606         case 'm': /* obsolescent: FIXME: remove in 2005. */
607           human_output_opts = 0;
608           output_block_size = 1024 * 1024;
609           break;
610
611         case 'l':
612           opt_count_all = 1;
613           break;
614
615         case 's':
616           opt_summarize_only = 1;
617           break;
618
619         case 'x':
620           bit_flags |= FTS_XDEV;
621           break;
622
623         case 'B':
624           human_output_opts = human_options (optarg, true, &output_block_size);
625           break;
626
627         case 'D': /* This will eventually be 'H' (-H), too.  */
628           bit_flags = FTS_COMFOLLOW;
629           break;
630
631         case 'L': /* --dereference */
632           bit_flags = FTS_LOGICAL;
633           break;
634
635         case 'P': /* --no-dereference */
636           bit_flags = FTS_PHYSICAL;
637           break;
638
639         case 'S':
640           opt_separate_dirs = 1;
641           break;
642
643         case 'X':
644           if (add_exclude_file (add_exclude, exclude, optarg,
645                                 EXCLUDE_WILDCARDS, '\n'))
646             {
647               error (0, errno, "%s", quotearg_colon (optarg));
648               fail = 1;
649             }
650           break;
651
652         case EXCLUDE_OPTION:
653           add_exclude (exclude, optarg, EXCLUDE_WILDCARDS);
654           break;
655
656         case_GETOPT_HELP_CHAR;
657
658         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
659
660         default:
661           fail = 1;
662         }
663     }
664
665   if (fail)
666     usage (EXIT_FAILURE);
667
668   if (opt_all && opt_summarize_only)
669     {
670       error (0, 0, _("cannot both summarize and show all entries"));
671       usage (EXIT_FAILURE);
672     }
673
674   if (opt_summarize_only && max_depth_specified && max_depth == 0)
675     {
676       error (0, 0,
677              _("warning: summarizing is the same as using --max-depth=0"));
678     }
679
680   if (opt_summarize_only && max_depth_specified && max_depth != 0)
681     {
682       error (0, 0,
683              _("warning: summarizing conflicts with --max-depth=%d"),
684                max_depth);
685       usage (EXIT_FAILURE);
686     }
687
688   if (opt_summarize_only)
689     max_depth = 0;
690
691   files = (optind < argc ? argv + optind : cwd_only);
692
693   /* Initialize the hash structure for inode numbers.  */
694   hash_init ();
695
696   exit (du_files (files, bit_flags) || G_fail
697         ? EXIT_FAILURE : EXIT_SUCCESS);
698 }