blob: 3941151e1da3a261c6006049279981c730e8c3a6 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppu0f5e1ab1999-12-09 18:23:54 +00002/*
3 * Mini du implementation for busybox
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
Eric Andersen265d2292002-04-06 23:16:44 +00007 * Copyright (C) 2002 Edward Betts <edward@debian.org>
John Beppu0f5e1ab1999-12-09 18:23:54 +00008 *
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
John Beppu0f5e1ab1999-12-09 18:23:54 +000010 */
11
Manuel Novoa III cad53642003-03-19 09:13:01 +000012/* BB_AUDIT SUSv3 compliant (unless default blocksize set to 1k) */
13/* http://www.opengroup.org/onlinepubs/007904975/utilities/du.html */
14
15/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
16 *
17 * Mostly rewritten for SUSv3 compliance and to fix bugs/defects.
18 * 1) Added support for SUSv3 -a, -H, -L, gnu -c, and (busybox) -d options.
19 * The -d option allows setting of max depth (similar to gnu --max-depth).
20 * 2) Fixed incorrect size calculations for links and directories, especially
21 * when errors occurred. Calculates sizes should now match gnu du output.
22 * 3) Added error checking of output.
23 * 4) Fixed busybox bug #1284 involving long overflow with human_readable.
24 */
25
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000026#include "libbb.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000027
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000028struct globals {
Denis Vlasenkof42ff902006-12-18 21:22:16 +000029#if ENABLE_FEATURE_HUMAN_READABLE
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000030 unsigned long disp_hr;
Manuel Novoa III cad53642003-03-19 09:13:01 +000031#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000032 unsigned disp_k;
Richard June6d0921c2001-01-22 22:35:38 +000033#endif
34
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000035 int max_print_depth;
36 nlink_t count_hardlinks;
Manuel Novoa III cad53642003-03-19 09:13:01 +000037
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000038 bool status;
39 bool one_file_system;
40 int print_files;
41 int slink_depth;
42 int du_depth;
43 dev_t dir_dev;
44};
45#define G (*(struct globals*)&bb_common_bufsiz1)
John Beppue1618e41999-12-15 18:52:17 +000046
Erik Andersene49d5ec2000-02-08 19:58:47 +000047
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000048static void print(unsigned long size, const char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +000049{
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 /* TODO - May not want to defer error checking here. */
Denis Vlasenkof42ff902006-12-18 21:22:16 +000051#if ENABLE_FEATURE_HUMAN_READABLE
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000052 printf("%s\t%s\n", make_human_readable_str(size, 512, G.disp_hr),
Denis Vlasenkof42ff902006-12-18 21:22:16 +000053 filename);
Richard June6d0921c2001-01-22 22:35:38 +000054#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000055 if (G.disp_k) {
Glenn L McGrathc66ebe42004-03-10 09:58:51 +000056 size++;
57 size >>= 1;
58 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000059 printf("%ld\t%s\n", size, filename);
Richard June6d0921c2001-01-22 22:35:38 +000060#endif
John Beppu0f5e1ab1999-12-09 18:23:54 +000061}
62
63/* tiny recursive du */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000064static unsigned long du(const char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +000065{
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 struct stat statbuf;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000067 unsigned long sum;
John Beppu14c82b61999-12-10 06:15:27 +000068
Denis Vlasenkof42ff902006-12-18 21:22:16 +000069 if (lstat(filename, &statbuf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000070 bb_simple_perror_msg(filename);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000071 G.status = EXIT_FAILURE;
Eric Andersene5dfced2001-04-09 22:48:12 +000072 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 }
74
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000075 if (G.one_file_system) {
76 if (G.du_depth == 0) {
77 G.dir_dev = statbuf.st_dev;
78 } else if (G.dir_dev != statbuf.st_dev) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 return 0;
Eric Andersen8fa1bf72001-06-30 17:54:20 +000080 }
Erik Andersen9ffdaa62000-02-11 21:55:04 +000081 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000082
83 sum = statbuf.st_blocks;
84
85 if (S_ISLNK(statbuf.st_mode)) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000086 if (G.slink_depth > G.du_depth) { /* -H or -L */
Denis Vlasenkof42ff902006-12-18 21:22:16 +000087 if (stat(filename, &statbuf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000088 bb_simple_perror_msg(filename);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000089 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 return 0;
91 }
92 sum = statbuf.st_blocks;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000093 if (G.slink_depth == 1) {
94 G.slink_depth = INT_MAX; /* Convert -H to -L. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 }
96 }
97 }
98
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000099 if (statbuf.st_nlink > G.count_hardlinks) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 /* Add files/directories with links only once */
Denis Vlasenkoe1e93c12007-03-14 22:06:57 +0000101 if (is_in_ino_dev_hashtable(&statbuf)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 return 0;
103 }
104 add_to_ino_dev_hashtable(&statbuf, NULL);
105 }
106
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 if (S_ISDIR(statbuf.st_mode)) {
108 DIR *dir;
109 struct dirent *entry;
Eric Andersen04b03542001-05-07 22:49:43 +0000110 char *newfile;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111
Rob Landley86b4d642006-08-03 17:58:17 +0000112 dir = warn_opendir(filename);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 if (!dir) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000114 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 return sum;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 }
Erik Andersen42387e42000-02-21 17:27:17 +0000117
Eric Andersen04b03542001-05-07 22:49:43 +0000118 newfile = last_char_is(filename, '/');
119 if (newfile)
120 *newfile = '\0';
Erik Andersen42387e42000-02-21 17:27:17 +0000121
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 while ((entry = readdir(dir))) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 char *name = entry->d_name;
124
Glenn L McGrath393183d2003-05-26 14:07:50 +0000125 newfile = concat_subpath_file(filename, name);
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000126 if (newfile == NULL)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 continue;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000128 ++G.du_depth;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 sum += du(newfile);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000130 --G.du_depth;
Eric Andersene5dfced2001-04-09 22:48:12 +0000131 free(newfile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 }
133 closedir(dir);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000134 } else if (G.du_depth > G.print_files) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 return sum;
Erik Andersen27fdd082000-02-19 18:16:49 +0000136 }
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000137 if (G.du_depth <= G.max_print_depth) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000138 print(sum, filename);
139 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 return sum;
John Beppu0f5e1ab1999-12-09 18:23:54 +0000141}
142
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000143int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144int du_main(int argc, char **argv)
145{
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000146 unsigned long total;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000147 int slink_depth_save;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000148 bool print_final_total;
Eric Andersen8876fb22003-06-20 09:01:58 +0000149 char *smax_print_depth;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000150 unsigned opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000152#if ENABLE_FEATURE_HUMAN_READABLE
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000153 USE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;)
154 SKIP_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;)
155 if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
156 G.disp_hr = 512;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000157#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000158 USE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
159 /* SKIP_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160#endif
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000161 G.max_print_depth = INT_MAX;
162 G.count_hardlinks = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000164 /* Note: SUSv3 specifies that -a and -s options cannot be used together
Manuel Novoa III cad53642003-03-19 09:13:01 +0000165 * in strictly conforming applications. However, it also says that some
166 * du implementations may produce output when -a and -s are used together.
167 * gnu du exits with an error code in this case. We choose to simply
168 * ignore -a. This is consistent with -s being equivalent to -d 0.
169 */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000170#if ENABLE_FEATURE_HUMAN_READABLE
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000171 opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000172 opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &smax_print_depth);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000173 argv += optind;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000174 if (opt & (1 << 9)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000175 /* -h opt */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000176 G.disp_hr = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 }
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000178 if (opt & (1 << 10)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000179 /* -m opt */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000180 G.disp_hr = 1024*1024;
Eric Andersen8876fb22003-06-20 09:01:58 +0000181 }
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000182 if (opt & (1 << 2)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000183 /* -k opt */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000184 G.disp_hr = 1024;
Eric Andersen8876fb22003-06-20 09:01:58 +0000185 }
186#else
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000187 opt_complementary = "H-L:L-H:s-d:d-s";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000188 opt = getopt32(argv, "aHkLsx" "d:" "lc", &smax_print_depth);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000189 argv += optind;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000190#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
191 if (opt & (1 << 2)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000192 /* -k opt */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000193 G.disp_k = 1;
Eric Andersen8876fb22003-06-20 09:01:58 +0000194 }
195#endif
196#endif
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000197 if (opt & (1 << 0)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000198 /* -a opt */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000199 G.print_files = INT_MAX;
Eric Andersen8876fb22003-06-20 09:01:58 +0000200 }
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000201 if (opt & (1 << 1)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000202 /* -H opt */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000203 G.slink_depth = 1;
Eric Andersen8876fb22003-06-20 09:01:58 +0000204 }
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000205 if (opt & (1 << 3)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000206 /* -L opt */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000207 G.slink_depth = INT_MAX;
Eric Andersen8876fb22003-06-20 09:01:58 +0000208 }
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000209 if (opt & (1 << 4)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000210 /* -s opt */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000211 G.max_print_depth = 0;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000212 }
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000213 G.one_file_system = opt & (1 << 5); /* -x opt */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000214 if (opt & (1 << 6)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000215 /* -d opt */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000216 G.max_print_depth = xatoi_u(smax_print_depth);
Eric Andersen8876fb22003-06-20 09:01:58 +0000217 }
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000218 if (opt & (1 << 7)) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000219 /* -l opt */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000220 G.count_hardlinks = MAXINT(nlink_t);
Eric Andersen8876fb22003-06-20 09:01:58 +0000221 }
222 print_final_total = opt & (1 << 8); /* -c opt */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223
224 /* go through remaining args (if any) */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000225 if (!*argv) {
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000226 *--argv = (char*)".";
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000227 if (G.slink_depth == 1) {
228 G.slink_depth = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 }
230 }
231
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000232 slink_depth_save = G.slink_depth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 total = 0;
234 do {
235 total += du(*argv);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000236 G.slink_depth = slink_depth_save;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000237 } while (*++argv);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000238
Bernhard Reutner-Fischerbdd253e2007-04-05 09:21:24 +0000239 if (ENABLE_FEATURE_CLEAN_UP)
240 reset_ino_dev_hashtable();
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000241 if (print_final_total)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000242 print(total, "total");
Manuel Novoa III cad53642003-03-19 09:13:01 +0000243
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000244 fflush_stdout_and_exit(G.status);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000245}