blob: 5f104736b0cdeed7fd7d7092fa14bb1f80de6dee [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 */
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
Pere Orga34425382011-03-31 14:43:25 +020026//usage:#define du_trivial_usage
27//usage: "[-aHLdclsx" IF_FEATURE_HUMAN_READABLE("hm") "k] [FILE]..."
28//usage:#define du_full_usage "\n\n"
Denys Vlasenkobb9254a2012-03-05 09:59:56 +010029//usage: "Summarize disk space used for each FILE and/or directory\n"
Pere Orga34425382011-03-31 14:43:25 +020030//usage: "\n -a Show file sizes too"
31//usage: "\n -L Follow all symlinks"
32//usage: "\n -H Follow symlinks on command line"
33//usage: "\n -d N Limit output to directories (and files with -a) of depth < N"
34//usage: "\n -c Show grand total"
35//usage: "\n -l Count sizes many times if hard linked"
36//usage: "\n -s Display only a total for each argument"
37//usage: "\n -x Skip directories on different filesystems"
38//usage: IF_FEATURE_HUMAN_READABLE(
Denys Vlasenko81b6bf12012-03-05 09:52:19 +010039//usage: "\n -h Sizes in human readable format (e.g., 1K 243M 2G)"
Pere Orga34425382011-03-31 14:43:25 +020040//usage: "\n -m Sizes in megabytes"
41//usage: )
Denys Vlasenkobb9254a2012-03-05 09:59:56 +010042//usage: "\n -k Sizes in kilobytes" IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(" (default)")
43//usage: IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(
44//usage: "\n Default unit is 512 bytes"
45//usage: )
Pere Orga34425382011-03-31 14:43:25 +020046//usage:
47//usage:#define du_example_usage
48//usage: "$ du\n"
49//usage: "16 ./CVS\n"
50//usage: "12 ./kernel-patches/CVS\n"
51//usage: "80 ./kernel-patches\n"
52//usage: "12 ./tests/CVS\n"
53//usage: "36 ./tests\n"
54//usage: "12 ./scripts/CVS\n"
55//usage: "16 ./scripts\n"
56//usage: "12 ./docs/CVS\n"
57//usage: "104 ./docs\n"
58//usage: "2417 .\n"
59
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000060#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020061#include "common_bufsiz.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000062
Denis Vlasenko618a3022008-11-11 21:15:56 +000063enum {
64 OPT_a_files_too = (1 << 0),
65 OPT_H_follow_links = (1 << 1),
66 OPT_k_kbytes = (1 << 2),
67 OPT_L_follow_links = (1 << 3),
68 OPT_s_total_norecurse = (1 << 4),
69 OPT_x_one_FS = (1 << 5),
70 OPT_d_maxdepth = (1 << 6),
71 OPT_l_hardlinks = (1 << 7),
72 OPT_c_total = (1 << 8),
73 OPT_h_for_humans = (1 << 9),
74 OPT_m_mbytes = (1 << 10),
75};
76
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000077struct globals {
Denis Vlasenkof42ff902006-12-18 21:22:16 +000078#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +020079 unsigned long disp_unit;
Manuel Novoa III cad53642003-03-19 09:13:01 +000080#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000081 unsigned disp_k;
Richard June6d0921c2001-01-22 22:35:38 +000082#endif
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000083 int max_print_depth;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000084 bool status;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000085 int slink_depth;
86 int du_depth;
87 dev_t dir_dev;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010088} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020089#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020090#define INIT_G() do { setup_common_bufsiz(); } while (0)
John Beppue1618e41999-12-15 18:52:17 +000091
Erik Andersene49d5ec2000-02-08 19:58:47 +000092
Ian Wienand694738f2012-02-28 03:10:31 +010093static void print(unsigned long long size, const char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +000094{
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 /* TODO - May not want to defer error checking here. */
Denis Vlasenkof42ff902006-12-18 21:22:16 +000096#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +020097# if ENABLE_DESKTOP
98 /* ~30 bytes of code for extra comtat:
99 * coreutils' du rounds sizes up:
100 * for example, 1025k file is shown as "2" by du -m.
101 * We round to nearest if human-readable [too hard to fix],
102 * else (fixed scale such as -m), we round up. To that end,
103 * add yet another half of the unit before displaying:
104 */
105 if (G.disp_unit)
106 size += (G.disp_unit-1) / (unsigned)(512 * 2);
107# endif
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200108 printf("%s\t%s\n",
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200109 /* size x 512 / G.disp_unit.
110 * If G.disp_unit == 0, show one fractional
111 * and use suffixes
112 */
113 make_human_readable_str(size, 512, G.disp_unit),
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000114 filename);
Richard June6d0921c2001-01-22 22:35:38 +0000115#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000116 if (G.disp_k) {
Glenn L McGrathc66ebe42004-03-10 09:58:51 +0000117 size++;
118 size >>= 1;
119 }
Ian Wienand694738f2012-02-28 03:10:31 +0100120 printf("%llu\t%s\n", size, filename);
Richard June6d0921c2001-01-22 22:35:38 +0000121#endif
John Beppu0f5e1ab1999-12-09 18:23:54 +0000122}
123
124/* tiny recursive du */
Ian Wienand694738f2012-02-28 03:10:31 +0100125static unsigned long long du(const char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +0000126{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 struct stat statbuf;
Ian Wienand694738f2012-02-28 03:10:31 +0100128 unsigned long long sum;
John Beppu14c82b61999-12-10 06:15:27 +0000129
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000130 if (lstat(filename, &statbuf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000131 bb_simple_perror_msg(filename);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000132 G.status = EXIT_FAILURE;
Eric Andersene5dfced2001-04-09 22:48:12 +0000133 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 }
135
Denis Vlasenko618a3022008-11-11 21:15:56 +0000136 if (option_mask32 & OPT_x_one_FS) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000137 if (G.du_depth == 0) {
138 G.dir_dev = statbuf.st_dev;
139 } else if (G.dir_dev != statbuf.st_dev) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 return 0;
Eric Andersen8fa1bf72001-06-30 17:54:20 +0000141 }
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000142 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143
144 sum = statbuf.st_blocks;
145
146 if (S_ISLNK(statbuf.st_mode)) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000147 if (G.slink_depth > G.du_depth) { /* -H or -L */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000148 if (stat(filename, &statbuf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000149 bb_simple_perror_msg(filename);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000150 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151 return 0;
152 }
153 sum = statbuf.st_blocks;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000154 if (G.slink_depth == 1) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000155 /* Convert -H to -L */
156 G.slink_depth = INT_MAX;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000157 }
158 }
159 }
160
Denis Vlasenko618a3022008-11-11 21:15:56 +0000161 if (!(option_mask32 & OPT_l_hardlinks)
162 && statbuf.st_nlink > 1
163 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164 /* Add files/directories with links only once */
Denis Vlasenkoe1e93c12007-03-14 22:06:57 +0000165 if (is_in_ino_dev_hashtable(&statbuf)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166 return 0;
167 }
168 add_to_ino_dev_hashtable(&statbuf, NULL);
169 }
170
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 if (S_ISDIR(statbuf.st_mode)) {
172 DIR *dir;
173 struct dirent *entry;
Eric Andersen04b03542001-05-07 22:49:43 +0000174 char *newfile;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175
Rob Landley86b4d642006-08-03 17:58:17 +0000176 dir = warn_opendir(filename);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 if (!dir) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000178 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000179 return sum;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000180 }
Erik Andersen42387e42000-02-21 17:27:17 +0000181
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 while ((entry = readdir(dir))) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000183 newfile = concat_subpath_file(filename, entry->d_name);
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000184 if (newfile == NULL)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 continue;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000186 ++G.du_depth;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 sum += du(newfile);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000188 --G.du_depth;
Eric Andersene5dfced2001-04-09 22:48:12 +0000189 free(newfile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 }
191 closedir(dir);
Denis Vlasenko618a3022008-11-11 21:15:56 +0000192 } else {
193 if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0)
194 return sum;
Erik Andersen27fdd082000-02-19 18:16:49 +0000195 }
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000196 if (G.du_depth <= G.max_print_depth) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000197 print(sum, filename);
198 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 return sum;
John Beppu0f5e1ab1999-12-09 18:23:54 +0000200}
201
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000202int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000203int du_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204{
Ian Wienand694738f2012-02-28 03:10:31 +0100205 unsigned long long total;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000206 int slink_depth_save;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000207 unsigned opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208
Denys Vlasenko16714242011-09-21 01:59:15 +0200209 INIT_G();
210
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000211#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200212 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;)
213 IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;)
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000214 if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200215 G.disp_unit = 512;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000216#else
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000217 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
218 /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219#endif
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000220 G.max_print_depth = INT_MAX;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000222 /* Note: SUSv3 specifies that -a and -s options cannot be used together
Manuel Novoa III cad53642003-03-19 09:13:01 +0000223 * in strictly conforming applications. However, it also says that some
224 * du implementations may produce output when -a and -s are used together.
225 * gnu du exits with an error code in this case. We choose to simply
226 * ignore -a. This is consistent with -s being equivalent to -d 0.
227 */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000228#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200229 opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s";
230 opt = getopt32(argv, "aHkLsx" "d:+" "lc" "hm", &G.max_print_depth);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000231 argv += optind;
Denis Vlasenko618a3022008-11-11 21:15:56 +0000232 if (opt & OPT_h_for_humans) {
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200233 G.disp_unit = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000234 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000235 if (opt & OPT_m_mbytes) {
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200236 G.disp_unit = 1024*1024;
Eric Andersen8876fb22003-06-20 09:01:58 +0000237 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000238 if (opt & OPT_k_kbytes) {
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +0200239 G.disp_unit = 1024;
Eric Andersen8876fb22003-06-20 09:01:58 +0000240 }
241#else
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200242 opt_complementary = "H-L:L-H:s-d:d-s";
243 opt = getopt32(argv, "aHkLsx" "d:+" "lc", &G.max_print_depth);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000244 argv += optind;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000245#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
Denis Vlasenko618a3022008-11-11 21:15:56 +0000246 if (opt & OPT_k_kbytes) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000247 G.disp_k = 1;
Eric Andersen8876fb22003-06-20 09:01:58 +0000248 }
249#endif
250#endif
Denis Vlasenko618a3022008-11-11 21:15:56 +0000251 if (opt & OPT_H_follow_links) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000252 G.slink_depth = 1;
Eric Andersen8876fb22003-06-20 09:01:58 +0000253 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000254 if (opt & OPT_L_follow_links) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000255 G.slink_depth = INT_MAX;
Eric Andersen8876fb22003-06-20 09:01:58 +0000256 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000257 if (opt & OPT_s_total_norecurse) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000258 G.max_print_depth = 0;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000259 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000260
261 /* go through remaining args (if any) */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000262 if (!*argv) {
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000263 *--argv = (char*)".";
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000264 if (G.slink_depth == 1) {
265 G.slink_depth = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 }
267 }
268
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000269 slink_depth_save = G.slink_depth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000270 total = 0;
271 do {
272 total += du(*argv);
Denis Vlasenko618a3022008-11-11 21:15:56 +0000273 /* otherwise du /dir /dir won't show /dir twice: */
274 reset_ino_dev_hashtable();
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000275 G.slink_depth = slink_depth_save;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000276 } while (*++argv);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000277
Denis Vlasenko618a3022008-11-11 21:15:56 +0000278 if (opt & OPT_c_total)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000279 print(total, "total");
Manuel Novoa III cad53642003-03-19 09:13:01 +0000280
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000281 fflush_stdout_and_exit(G.status);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000282}