blob: d14d9e4eaba68ef8ffe4058558b687a3611d4441 [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
Denys Vlasenkob86b39b2018-12-28 17:45:35 +010022//config: bool "du (6.3 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010023//config: default y
24//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020025//config: du is used to report the amount of disk space used
26//config: for specified files.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010027//config:
28//config:config FEATURE_DU_DEFAULT_BLOCKSIZE_1K
Denys Vlasenkob86b39b2018-12-28 17:45:35 +010029//config: bool "Use default blocksize of 1024 bytes (else it's 512 bytes)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010030//config: default y
31//config: depends on DU
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010032
33//applet:IF_DU(APPLET(du, BB_DIR_USR_BIN, BB_SUID_DROP))
34
35//kbuild:lib-$(CONFIG_DU) += du.o
36
37/* BB_AUDIT SUSv3 compliant (unless default blocksize set to 1k) */
38/* http://www.opengroup.org/onlinepubs/007904975/utilities/du.html */
Manuel Novoa III cad53642003-03-19 09:13:01 +000039
Pere Orga34425382011-03-31 14:43:25 +020040//usage:#define du_trivial_usage
41//usage: "[-aHLdclsx" IF_FEATURE_HUMAN_READABLE("hm") "k] [FILE]..."
42//usage:#define du_full_usage "\n\n"
Denys Vlasenkobb9254a2012-03-05 09:59:56 +010043//usage: "Summarize disk space used for each FILE and/or directory\n"
Pere Orga34425382011-03-31 14:43:25 +020044//usage: "\n -a Show file sizes too"
45//usage: "\n -L Follow all symlinks"
46//usage: "\n -H Follow symlinks on command line"
47//usage: "\n -d N Limit output to directories (and files with -a) of depth < N"
48//usage: "\n -c Show grand total"
49//usage: "\n -l Count sizes many times if hard linked"
50//usage: "\n -s Display only a total for each argument"
51//usage: "\n -x Skip directories on different filesystems"
52//usage: IF_FEATURE_HUMAN_READABLE(
Denys Vlasenko81b6bf12012-03-05 09:52:19 +010053//usage: "\n -h Sizes in human readable format (e.g., 1K 243M 2G)"
Pere Orga34425382011-03-31 14:43:25 +020054//usage: "\n -m Sizes in megabytes"
55//usage: )
Denys Vlasenkobb9254a2012-03-05 09:59:56 +010056//usage: "\n -k Sizes in kilobytes" IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(" (default)")
57//usage: IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(
58//usage: "\n Default unit is 512 bytes"
59//usage: )
Pere Orga34425382011-03-31 14:43:25 +020060//usage:
61//usage:#define du_example_usage
62//usage: "$ du\n"
63//usage: "16 ./CVS\n"
64//usage: "12 ./kernel-patches/CVS\n"
65//usage: "80 ./kernel-patches\n"
66//usage: "12 ./tests/CVS\n"
67//usage: "36 ./tests\n"
68//usage: "12 ./scripts/CVS\n"
69//usage: "16 ./scripts\n"
70//usage: "12 ./docs/CVS\n"
71//usage: "104 ./docs\n"
72//usage: "2417 .\n"
73
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000074#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020075#include "common_bufsiz.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000076
Denis Vlasenko618a3022008-11-11 21:15:56 +000077enum {
78 OPT_a_files_too = (1 << 0),
79 OPT_H_follow_links = (1 << 1),
80 OPT_k_kbytes = (1 << 2),
81 OPT_L_follow_links = (1 << 3),
82 OPT_s_total_norecurse = (1 << 4),
83 OPT_x_one_FS = (1 << 5),
84 OPT_d_maxdepth = (1 << 6),
85 OPT_l_hardlinks = (1 << 7),
86 OPT_c_total = (1 << 8),
87 OPT_h_for_humans = (1 << 9),
88 OPT_m_mbytes = (1 << 10),
89};
90
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000091struct globals {
Denis Vlasenkof42ff902006-12-18 21:22:16 +000092#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +020093 unsigned long disp_unit;
Manuel Novoa III cad53642003-03-19 09:13:01 +000094#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000095 unsigned disp_k;
Richard June6d0921c2001-01-22 22:35:38 +000096#endif
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000097 int max_print_depth;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000098 bool status;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000099 int slink_depth;
100 int du_depth;
101 dev_t dir_dev;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100102} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200103#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200104#define INIT_G() do { setup_common_bufsiz(); } while (0)
John Beppue1618e41999-12-15 18:52:17 +0000105
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106
Ian Wienand694738f2012-02-28 03:10:31 +0100107static void print(unsigned long long size, const char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +0000108{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 /* TODO - May not want to defer error checking here. */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000110#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200111# if ENABLE_DESKTOP
112 /* ~30 bytes of code for extra comtat:
113 * coreutils' du rounds sizes up:
114 * for example, 1025k file is shown as "2" by du -m.
115 * We round to nearest if human-readable [too hard to fix],
116 * else (fixed scale such as -m), we round up. To that end,
117 * add yet another half of the unit before displaying:
118 */
119 if (G.disp_unit)
120 size += (G.disp_unit-1) / (unsigned)(512 * 2);
121# endif
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200122 printf("%s\t%s\n",
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200123 /* size x 512 / G.disp_unit.
124 * If G.disp_unit == 0, show one fractional
125 * and use suffixes
126 */
127 make_human_readable_str(size, 512, G.disp_unit),
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000128 filename);
Richard June6d0921c2001-01-22 22:35:38 +0000129#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000130 if (G.disp_k) {
Glenn L McGrathc66ebe42004-03-10 09:58:51 +0000131 size++;
132 size >>= 1;
133 }
Ian Wienand694738f2012-02-28 03:10:31 +0100134 printf("%llu\t%s\n", size, filename);
Richard June6d0921c2001-01-22 22:35:38 +0000135#endif
John Beppu0f5e1ab1999-12-09 18:23:54 +0000136}
137
138/* tiny recursive du */
Ian Wienand694738f2012-02-28 03:10:31 +0100139static unsigned long long du(const char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +0000140{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 struct stat statbuf;
Ian Wienand694738f2012-02-28 03:10:31 +0100142 unsigned long long sum;
John Beppu14c82b61999-12-10 06:15:27 +0000143
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000144 if (lstat(filename, &statbuf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000145 bb_simple_perror_msg(filename);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000146 G.status = EXIT_FAILURE;
Eric Andersene5dfced2001-04-09 22:48:12 +0000147 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 }
149
Denis Vlasenko618a3022008-11-11 21:15:56 +0000150 if (option_mask32 & OPT_x_one_FS) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000151 if (G.du_depth == 0) {
152 G.dir_dev = statbuf.st_dev;
153 } else if (G.dir_dev != statbuf.st_dev) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154 return 0;
Eric Andersen8fa1bf72001-06-30 17:54:20 +0000155 }
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000156 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000157
158 sum = statbuf.st_blocks;
159
160 if (S_ISLNK(statbuf.st_mode)) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000161 if (G.slink_depth > G.du_depth) { /* -H or -L */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000162 if (stat(filename, &statbuf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000163 bb_simple_perror_msg(filename);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000164 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000165 return 0;
166 }
167 sum = statbuf.st_blocks;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000168 if (G.slink_depth == 1) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000169 /* Convert -H to -L */
170 G.slink_depth = INT_MAX;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 }
172 }
173 }
174
Denis Vlasenko618a3022008-11-11 21:15:56 +0000175 if (!(option_mask32 & OPT_l_hardlinks)
176 && statbuf.st_nlink > 1
177 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 /* Add files/directories with links only once */
Denis Vlasenkoe1e93c12007-03-14 22:06:57 +0000179 if (is_in_ino_dev_hashtable(&statbuf)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 return 0;
181 }
182 add_to_ino_dev_hashtable(&statbuf, NULL);
183 }
184
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 if (S_ISDIR(statbuf.st_mode)) {
186 DIR *dir;
187 struct dirent *entry;
Eric Andersen04b03542001-05-07 22:49:43 +0000188 char *newfile;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189
Rob Landley86b4d642006-08-03 17:58:17 +0000190 dir = warn_opendir(filename);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 if (!dir) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000192 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000193 return sum;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 }
Erik Andersen42387e42000-02-21 17:27:17 +0000195
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 while ((entry = readdir(dir))) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000197 newfile = concat_subpath_file(filename, entry->d_name);
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000198 if (newfile == NULL)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 continue;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000200 ++G.du_depth;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000201 sum += du(newfile);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000202 --G.du_depth;
Eric Andersene5dfced2001-04-09 22:48:12 +0000203 free(newfile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 }
205 closedir(dir);
Denis Vlasenko618a3022008-11-11 21:15:56 +0000206 } else {
207 if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0)
208 return sum;
Erik Andersen27fdd082000-02-19 18:16:49 +0000209 }
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000210 if (G.du_depth <= G.max_print_depth) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000211 print(sum, filename);
212 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 return sum;
John Beppu0f5e1ab1999-12-09 18:23:54 +0000214}
215
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000216int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000217int du_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218{
Ian Wienand694738f2012-02-28 03:10:31 +0100219 unsigned long long total;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 int slink_depth_save;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000221 unsigned opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222
Denys Vlasenko16714242011-09-21 01:59:15 +0200223 INIT_G();
224
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000225#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200226 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;)
227 IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;)
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000228 if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200229 G.disp_unit = 512;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000230#else
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000231 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
232 /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233#endif
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000234 G.max_print_depth = INT_MAX;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000236 /* Note: SUSv3 specifies that -a and -s options cannot be used together
Manuel Novoa III cad53642003-03-19 09:13:01 +0000237 * in strictly conforming applications. However, it also says that some
238 * du implementations may produce output when -a and -s are used together.
239 * gnu du exits with an error code in this case. We choose to simply
240 * ignore -a. This is consistent with -s being equivalent to -d 0.
241 */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000242#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200243 opt = getopt32(argv, "^"
244 "aHkLsxd:+lchm"
245 "\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s",
246 &G.max_print_depth
247 );
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000248 argv += optind;
Denis Vlasenko618a3022008-11-11 21:15:56 +0000249 if (opt & OPT_h_for_humans) {
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200250 G.disp_unit = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000252 if (opt & OPT_m_mbytes) {
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200253 G.disp_unit = 1024*1024;
Eric Andersen8876fb22003-06-20 09:01:58 +0000254 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000255 if (opt & OPT_k_kbytes) {
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200256 G.disp_unit = 1024;
Eric Andersen8876fb22003-06-20 09:01:58 +0000257 }
258#else
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200259 opt = getopt32(argv, "^"
260 "aHkLsxd:+lc"
261 "\0" "H-L:L-H:s-d:d-s",
262 &G.max_print_depth
263 );
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000264 argv += optind;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000265#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
Denis Vlasenko618a3022008-11-11 21:15:56 +0000266 if (opt & OPT_k_kbytes) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000267 G.disp_k = 1;
Eric Andersen8876fb22003-06-20 09:01:58 +0000268 }
269#endif
270#endif
Denis Vlasenko618a3022008-11-11 21:15:56 +0000271 if (opt & OPT_H_follow_links) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000272 G.slink_depth = 1;
Eric Andersen8876fb22003-06-20 09:01:58 +0000273 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000274 if (opt & OPT_L_follow_links) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000275 G.slink_depth = INT_MAX;
Eric Andersen8876fb22003-06-20 09:01:58 +0000276 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000277 if (opt & OPT_s_total_norecurse) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000278 G.max_print_depth = 0;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000279 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280
281 /* go through remaining args (if any) */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000282 if (!*argv) {
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000283 *--argv = (char*)".";
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000284 if (G.slink_depth == 1) {
285 G.slink_depth = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286 }
287 }
288
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000289 slink_depth_save = G.slink_depth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000290 total = 0;
291 do {
292 total += du(*argv);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000293 G.slink_depth = slink_depth_save;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000294 } while (*++argv);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000295
Ron Yorstone563f9e2019-02-16 10:22:31 +0000296 if (ENABLE_FEATURE_CLEAN_UP)
297 reset_ino_dev_hashtable();
Denis Vlasenko618a3022008-11-11 21:15:56 +0000298 if (opt & OPT_c_total)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000299 print(total, "total");
Manuel Novoa III cad53642003-03-19 09:13:01 +0000300
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000301 fflush_stdout_and_exit(G.status);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000302}