blob: 03e31a0e4109d7c7a70c652a850a1c2981e9a366 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
John Beppu0f5e1ab1999-12-09 18:23:54 +000010 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
12 *
13 * Mostly rewritten for SUSv3 compliance and to fix bugs/defects.
14 * 1) Added support for SUSv3 -a, -H, -L, gnu -c, and (busybox) -d options.
15 * The -d option allows setting of max depth (similar to gnu --max-depth).
16 * 2) Fixed incorrect size calculations for links and directories, especially
17 * when errors occurred. Calculates sizes should now match gnu du output.
18 * 3) Added error checking of output.
19 * 4) Fixed busybox bug #1284 involving long overflow with human_readable.
20 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010021//config:config DU
22//config: bool "du (default blocksize of 512 bytes)"
23//config: default y
24//config: help
25//config: du is used to report the amount of disk space used
26//config: for specified files.
27//config:
28//config:config FEATURE_DU_DEFAULT_BLOCKSIZE_1K
29//config: bool "Use a default blocksize of 1024 bytes (1K)"
30//config: default y
31//config: depends on DU
32//config: help
33//config: Use a blocksize of (1K) instead of the default 512b.
34
35//applet:IF_DU(APPLET(du, BB_DIR_USR_BIN, BB_SUID_DROP))
36
37//kbuild:lib-$(CONFIG_DU) += du.o
38
39/* BB_AUDIT SUSv3 compliant (unless default blocksize set to 1k) */
40/* http://www.opengroup.org/onlinepubs/007904975/utilities/du.html */
Manuel Novoa III cad53642003-03-19 09:13:01 +000041
Pere Orga34425382011-03-31 14:43:25 +020042//usage:#define du_trivial_usage
43//usage: "[-aHLdclsx" IF_FEATURE_HUMAN_READABLE("hm") "k] [FILE]..."
44//usage:#define du_full_usage "\n\n"
Denys Vlasenkobb9254a2012-03-05 09:59:56 +010045//usage: "Summarize disk space used for each FILE and/or directory\n"
Pere Orga34425382011-03-31 14:43:25 +020046//usage: "\n -a Show file sizes too"
47//usage: "\n -L Follow all symlinks"
48//usage: "\n -H Follow symlinks on command line"
49//usage: "\n -d N Limit output to directories (and files with -a) of depth < N"
50//usage: "\n -c Show grand total"
51//usage: "\n -l Count sizes many times if hard linked"
52//usage: "\n -s Display only a total for each argument"
53//usage: "\n -x Skip directories on different filesystems"
54//usage: IF_FEATURE_HUMAN_READABLE(
Denys Vlasenko81b6bf12012-03-05 09:52:19 +010055//usage: "\n -h Sizes in human readable format (e.g., 1K 243M 2G)"
Pere Orga34425382011-03-31 14:43:25 +020056//usage: "\n -m Sizes in megabytes"
57//usage: )
Denys Vlasenkobb9254a2012-03-05 09:59:56 +010058//usage: "\n -k Sizes in kilobytes" IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(" (default)")
59//usage: IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(
60//usage: "\n Default unit is 512 bytes"
61//usage: )
Pere Orga34425382011-03-31 14:43:25 +020062//usage:
63//usage:#define du_example_usage
64//usage: "$ du\n"
65//usage: "16 ./CVS\n"
66//usage: "12 ./kernel-patches/CVS\n"
67//usage: "80 ./kernel-patches\n"
68//usage: "12 ./tests/CVS\n"
69//usage: "36 ./tests\n"
70//usage: "12 ./scripts/CVS\n"
71//usage: "16 ./scripts\n"
72//usage: "12 ./docs/CVS\n"
73//usage: "104 ./docs\n"
74//usage: "2417 .\n"
75
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000076#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020077#include "common_bufsiz.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000078
Denis Vlasenko618a3022008-11-11 21:15:56 +000079enum {
80 OPT_a_files_too = (1 << 0),
81 OPT_H_follow_links = (1 << 1),
82 OPT_k_kbytes = (1 << 2),
83 OPT_L_follow_links = (1 << 3),
84 OPT_s_total_norecurse = (1 << 4),
85 OPT_x_one_FS = (1 << 5),
86 OPT_d_maxdepth = (1 << 6),
87 OPT_l_hardlinks = (1 << 7),
88 OPT_c_total = (1 << 8),
89 OPT_h_for_humans = (1 << 9),
90 OPT_m_mbytes = (1 << 10),
91};
92
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000093struct globals {
Denis Vlasenkof42ff902006-12-18 21:22:16 +000094#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +020095 unsigned long disp_unit;
Manuel Novoa III cad53642003-03-19 09:13:01 +000096#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000097 unsigned disp_k;
Richard June6d0921c2001-01-22 22:35:38 +000098#endif
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000099 int max_print_depth;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000100 bool status;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000101 int slink_depth;
102 int du_depth;
103 dev_t dir_dev;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100104} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200105#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200106#define INIT_G() do { setup_common_bufsiz(); } while (0)
John Beppue1618e41999-12-15 18:52:17 +0000107
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108
Ian Wienand694738f2012-02-28 03:10:31 +0100109static void print(unsigned long long size, const char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +0000110{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111 /* TODO - May not want to defer error checking here. */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000112#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200113# if ENABLE_DESKTOP
114 /* ~30 bytes of code for extra comtat:
115 * coreutils' du rounds sizes up:
116 * for example, 1025k file is shown as "2" by du -m.
117 * We round to nearest if human-readable [too hard to fix],
118 * else (fixed scale such as -m), we round up. To that end,
119 * add yet another half of the unit before displaying:
120 */
121 if (G.disp_unit)
122 size += (G.disp_unit-1) / (unsigned)(512 * 2);
123# endif
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200124 printf("%s\t%s\n",
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200125 /* size x 512 / G.disp_unit.
126 * If G.disp_unit == 0, show one fractional
127 * and use suffixes
128 */
129 make_human_readable_str(size, 512, G.disp_unit),
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000130 filename);
Richard June6d0921c2001-01-22 22:35:38 +0000131#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000132 if (G.disp_k) {
Glenn L McGrathc66ebe42004-03-10 09:58:51 +0000133 size++;
134 size >>= 1;
135 }
Ian Wienand694738f2012-02-28 03:10:31 +0100136 printf("%llu\t%s\n", size, filename);
Richard June6d0921c2001-01-22 22:35:38 +0000137#endif
John Beppu0f5e1ab1999-12-09 18:23:54 +0000138}
139
140/* tiny recursive du */
Ian Wienand694738f2012-02-28 03:10:31 +0100141static unsigned long long du(const char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +0000142{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 struct stat statbuf;
Ian Wienand694738f2012-02-28 03:10:31 +0100144 unsigned long long sum;
John Beppu14c82b61999-12-10 06:15:27 +0000145
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000146 if (lstat(filename, &statbuf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000147 bb_simple_perror_msg(filename);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000148 G.status = EXIT_FAILURE;
Eric Andersene5dfced2001-04-09 22:48:12 +0000149 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000150 }
151
Denis Vlasenko618a3022008-11-11 21:15:56 +0000152 if (option_mask32 & OPT_x_one_FS) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000153 if (G.du_depth == 0) {
154 G.dir_dev = statbuf.st_dev;
155 } else if (G.dir_dev != statbuf.st_dev) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156 return 0;
Eric Andersen8fa1bf72001-06-30 17:54:20 +0000157 }
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000158 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000159
160 sum = statbuf.st_blocks;
161
162 if (S_ISLNK(statbuf.st_mode)) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000163 if (G.slink_depth > G.du_depth) { /* -H or -L */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000164 if (stat(filename, &statbuf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000165 bb_simple_perror_msg(filename);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000166 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 return 0;
168 }
169 sum = statbuf.st_blocks;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000170 if (G.slink_depth == 1) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000171 /* Convert -H to -L */
172 G.slink_depth = INT_MAX;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000173 }
174 }
175 }
176
Denis Vlasenko618a3022008-11-11 21:15:56 +0000177 if (!(option_mask32 & OPT_l_hardlinks)
178 && statbuf.st_nlink > 1
179 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 /* Add files/directories with links only once */
Denis Vlasenkoe1e93c12007-03-14 22:06:57 +0000181 if (is_in_ino_dev_hashtable(&statbuf)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 return 0;
183 }
184 add_to_ino_dev_hashtable(&statbuf, NULL);
185 }
186
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 if (S_ISDIR(statbuf.st_mode)) {
188 DIR *dir;
189 struct dirent *entry;
Eric Andersen04b03542001-05-07 22:49:43 +0000190 char *newfile;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191
Rob Landley86b4d642006-08-03 17:58:17 +0000192 dir = warn_opendir(filename);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193 if (!dir) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000194 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 return sum;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 }
Erik Andersen42387e42000-02-21 17:27:17 +0000197
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198 while ((entry = readdir(dir))) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000199 newfile = concat_subpath_file(filename, entry->d_name);
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000200 if (newfile == NULL)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000201 continue;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000202 ++G.du_depth;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000203 sum += du(newfile);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000204 --G.du_depth;
Eric Andersene5dfced2001-04-09 22:48:12 +0000205 free(newfile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 }
207 closedir(dir);
Denis Vlasenko618a3022008-11-11 21:15:56 +0000208 } else {
209 if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0)
210 return sum;
Erik Andersen27fdd082000-02-19 18:16:49 +0000211 }
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000212 if (G.du_depth <= G.max_print_depth) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000213 print(sum, filename);
214 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 return sum;
John Beppu0f5e1ab1999-12-09 18:23:54 +0000216}
217
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000218int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000219int du_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220{
Ian Wienand694738f2012-02-28 03:10:31 +0100221 unsigned long long total;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 int slink_depth_save;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000223 unsigned opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224
Denys Vlasenko16714242011-09-21 01:59:15 +0200225 INIT_G();
226
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000227#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200228 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;)
229 IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;)
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000230 if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200231 G.disp_unit = 512;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000232#else
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000233 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
234 /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000235#endif
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000236 G.max_print_depth = INT_MAX;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000238 /* Note: SUSv3 specifies that -a and -s options cannot be used together
Manuel Novoa III cad53642003-03-19 09:13:01 +0000239 * in strictly conforming applications. However, it also says that some
240 * du implementations may produce output when -a and -s are used together.
241 * gnu du exits with an error code in this case. We choose to simply
242 * ignore -a. This is consistent with -s being equivalent to -d 0.
243 */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000244#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200245 opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s";
246 opt = getopt32(argv, "aHkLsx" "d:+" "lc" "hm", &G.max_print_depth);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000247 argv += optind;
Denis Vlasenko618a3022008-11-11 21:15:56 +0000248 if (opt & OPT_h_for_humans) {
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200249 G.disp_unit = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000251 if (opt & OPT_m_mbytes) {
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200252 G.disp_unit = 1024*1024;
Eric Andersen8876fb22003-06-20 09:01:58 +0000253 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000254 if (opt & OPT_k_kbytes) {
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200255 G.disp_unit = 1024;
Eric Andersen8876fb22003-06-20 09:01:58 +0000256 }
257#else
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200258 opt_complementary = "H-L:L-H:s-d:d-s";
259 opt = getopt32(argv, "aHkLsx" "d:+" "lc", &G.max_print_depth);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000260 argv += optind;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000261#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
Denis Vlasenko618a3022008-11-11 21:15:56 +0000262 if (opt & OPT_k_kbytes) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000263 G.disp_k = 1;
Eric Andersen8876fb22003-06-20 09:01:58 +0000264 }
265#endif
266#endif
Denis Vlasenko618a3022008-11-11 21:15:56 +0000267 if (opt & OPT_H_follow_links) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000268 G.slink_depth = 1;
Eric Andersen8876fb22003-06-20 09:01:58 +0000269 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000270 if (opt & OPT_L_follow_links) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000271 G.slink_depth = INT_MAX;
Eric Andersen8876fb22003-06-20 09:01:58 +0000272 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000273 if (opt & OPT_s_total_norecurse) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000274 G.max_print_depth = 0;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000275 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276
277 /* go through remaining args (if any) */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000278 if (!*argv) {
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000279 *--argv = (char*)".";
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000280 if (G.slink_depth == 1) {
281 G.slink_depth = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000282 }
283 }
284
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000285 slink_depth_save = G.slink_depth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000286 total = 0;
287 do {
288 total += du(*argv);
Denis Vlasenko618a3022008-11-11 21:15:56 +0000289 /* otherwise du /dir /dir won't show /dir twice: */
290 reset_ino_dev_hashtable();
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000291 G.slink_depth = slink_depth_save;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000292 } while (*++argv);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000293
Denis Vlasenko618a3022008-11-11 21:15:56 +0000294 if (opt & OPT_c_total)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000295 print(total, "total");
Manuel Novoa III cad53642003-03-19 09:13:01 +0000296
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000297 fflush_stdout_and_exit(G.status);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000298}