blob: 5894ed43824157bce72d3e92c3305533fc359cbd [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 Vlasenko618a3022008-11-11 21:15:56 +000028enum {
29 OPT_a_files_too = (1 << 0),
30 OPT_H_follow_links = (1 << 1),
31 OPT_k_kbytes = (1 << 2),
32 OPT_L_follow_links = (1 << 3),
33 OPT_s_total_norecurse = (1 << 4),
34 OPT_x_one_FS = (1 << 5),
35 OPT_d_maxdepth = (1 << 6),
36 OPT_l_hardlinks = (1 << 7),
37 OPT_c_total = (1 << 8),
38 OPT_h_for_humans = (1 << 9),
39 OPT_m_mbytes = (1 << 10),
40};
41
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000042struct globals {
Denis Vlasenkof42ff902006-12-18 21:22:16 +000043#if ENABLE_FEATURE_HUMAN_READABLE
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000044 unsigned long disp_hr;
Manuel Novoa III cad53642003-03-19 09:13:01 +000045#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000046 unsigned disp_k;
Richard June6d0921c2001-01-22 22:35:38 +000047#endif
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000048 int max_print_depth;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000049 bool status;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000050 int slink_depth;
51 int du_depth;
52 dev_t dir_dev;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010053} FIX_ALIASING;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000054#define G (*(struct globals*)&bb_common_bufsiz1)
John Beppue1618e41999-12-15 18:52:17 +000055
Erik Andersene49d5ec2000-02-08 19:58:47 +000056
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000057static void print(unsigned long size, const char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +000058{
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 /* TODO - May not want to defer error checking here. */
Denis Vlasenkof42ff902006-12-18 21:22:16 +000060#if ENABLE_FEATURE_HUMAN_READABLE
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020061 printf("%s\t%s\n",
62 /* size x 512 / G.disp_hr, show one fractional,
63 * use suffixes if G.disp_hr == 0 */
64 make_human_readable_str(size, 512, G.disp_hr),
Denis Vlasenkof42ff902006-12-18 21:22:16 +000065 filename);
Richard June6d0921c2001-01-22 22:35:38 +000066#else
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000067 if (G.disp_k) {
Glenn L McGrathc66ebe42004-03-10 09:58:51 +000068 size++;
69 size >>= 1;
70 }
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020071 printf("%lu\t%s\n", size, filename);
Richard June6d0921c2001-01-22 22:35:38 +000072#endif
John Beppu0f5e1ab1999-12-09 18:23:54 +000073}
74
75/* tiny recursive du */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000076static unsigned long du(const char *filename)
John Beppu0f5e1ab1999-12-09 18:23:54 +000077{
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 struct stat statbuf;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000079 unsigned long sum;
John Beppu14c82b61999-12-10 06:15:27 +000080
Denis Vlasenkof42ff902006-12-18 21:22:16 +000081 if (lstat(filename, &statbuf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000082 bb_simple_perror_msg(filename);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000083 G.status = EXIT_FAILURE;
Eric Andersene5dfced2001-04-09 22:48:12 +000084 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 }
86
Denis Vlasenko618a3022008-11-11 21:15:56 +000087 if (option_mask32 & OPT_x_one_FS) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +000088 if (G.du_depth == 0) {
89 G.dir_dev = statbuf.st_dev;
90 } else if (G.dir_dev != statbuf.st_dev) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 return 0;
Eric Andersen8fa1bf72001-06-30 17:54:20 +000092 }
Erik Andersen9ffdaa62000-02-11 21:55:04 +000093 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000094
95 sum = statbuf.st_blocks;
96
97 if (S_ISLNK(statbuf.st_mode)) {
Denis Vlasenko618a3022008-11-11 21:15:56 +000098 if (G.slink_depth > G.du_depth) { /* -H or -L */
Denis Vlasenkof42ff902006-12-18 21:22:16 +000099 if (stat(filename, &statbuf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000100 bb_simple_perror_msg(filename);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000101 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 return 0;
103 }
104 sum = statbuf.st_blocks;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000105 if (G.slink_depth == 1) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000106 /* Convert -H to -L */
107 G.slink_depth = INT_MAX;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 }
109 }
110 }
111
Denis Vlasenko618a3022008-11-11 21:15:56 +0000112 if (!(option_mask32 & OPT_l_hardlinks)
113 && statbuf.st_nlink > 1
114 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 /* Add files/directories with links only once */
Denis Vlasenkoe1e93c12007-03-14 22:06:57 +0000116 if (is_in_ino_dev_hashtable(&statbuf)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 return 0;
118 }
119 add_to_ino_dev_hashtable(&statbuf, NULL);
120 }
121
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 if (S_ISDIR(statbuf.st_mode)) {
123 DIR *dir;
124 struct dirent *entry;
Eric Andersen04b03542001-05-07 22:49:43 +0000125 char *newfile;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126
Rob Landley86b4d642006-08-03 17:58:17 +0000127 dir = warn_opendir(filename);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 if (!dir) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000129 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 return sum;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 }
Erik Andersen42387e42000-02-21 17:27:17 +0000132
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133 while ((entry = readdir(dir))) {
Denis Vlasenko618a3022008-11-11 21:15:56 +0000134 newfile = concat_subpath_file(filename, entry->d_name);
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000135 if (newfile == NULL)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 continue;
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000137 ++G.du_depth;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 sum += du(newfile);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000139 --G.du_depth;
Eric Andersene5dfced2001-04-09 22:48:12 +0000140 free(newfile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 }
142 closedir(dir);
Denis Vlasenko618a3022008-11-11 21:15:56 +0000143 } else {
144 if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0)
145 return sum;
Erik Andersen27fdd082000-02-19 18:16:49 +0000146 }
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000147 if (G.du_depth <= G.max_print_depth) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 print(sum, filename);
149 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000150 return sum;
John Beppu0f5e1ab1999-12-09 18:23:54 +0000151}
152
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000153int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000154int du_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155{
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000156 unsigned long total;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000157 int slink_depth_save;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000158 unsigned opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000160#if ENABLE_FEATURE_HUMAN_READABLE
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000161 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;)
162 IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;)
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000163 if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
164 G.disp_hr = 512;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000165#else
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000166 IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
167 /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168#endif
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000169 G.max_print_depth = INT_MAX;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000171 /* Note: SUSv3 specifies that -a and -s options cannot be used together
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 * in strictly conforming applications. However, it also says that some
173 * du implementations may produce output when -a and -s are used together.
174 * gnu du exits with an error code in this case. We choose to simply
175 * ignore -a. This is consistent with -s being equivalent to -d 0.
176 */
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000177#if ENABLE_FEATURE_HUMAN_READABLE
Denis Vlasenko1d426652008-03-17 09:09:09 +0000178 opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s:d+";
179 opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &G.max_print_depth);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000180 argv += optind;
Denis Vlasenko618a3022008-11-11 21:15:56 +0000181 if (opt & OPT_h_for_humans) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000182 G.disp_hr = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000184 if (opt & OPT_m_mbytes) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000185 G.disp_hr = 1024*1024;
Eric Andersen8876fb22003-06-20 09:01:58 +0000186 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000187 if (opt & OPT_k_kbytes) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000188 G.disp_hr = 1024;
Eric Andersen8876fb22003-06-20 09:01:58 +0000189 }
190#else
Denis Vlasenko1d426652008-03-17 09:09:09 +0000191 opt_complementary = "H-L:L-H:s-d:d-s:d+";
192 opt = getopt32(argv, "aHkLsx" "d:" "lc", &G.max_print_depth);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000193 argv += optind;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000194#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
Denis Vlasenko618a3022008-11-11 21:15:56 +0000195 if (opt & OPT_k_kbytes) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000196 G.disp_k = 1;
Eric Andersen8876fb22003-06-20 09:01:58 +0000197 }
198#endif
199#endif
Denis Vlasenko618a3022008-11-11 21:15:56 +0000200 if (opt & OPT_H_follow_links) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000201 G.slink_depth = 1;
Eric Andersen8876fb22003-06-20 09:01:58 +0000202 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000203 if (opt & OPT_L_follow_links) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000204 G.slink_depth = INT_MAX;
Eric Andersen8876fb22003-06-20 09:01:58 +0000205 }
Denis Vlasenko618a3022008-11-11 21:15:56 +0000206 if (opt & OPT_s_total_norecurse) {
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000207 G.max_print_depth = 0;
Denis Vlasenkof42ff902006-12-18 21:22:16 +0000208 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209
210 /* go through remaining args (if any) */
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000211 if (!*argv) {
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000212 *--argv = (char*)".";
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000213 if (G.slink_depth == 1) {
214 G.slink_depth = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 }
216 }
217
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000218 slink_depth_save = G.slink_depth;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219 total = 0;
220 do {
221 total += du(*argv);
Denis Vlasenko618a3022008-11-11 21:15:56 +0000222 /* otherwise du /dir /dir won't show /dir twice: */
223 reset_ino_dev_hashtable();
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000224 G.slink_depth = slink_depth_save;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000225 } while (*++argv);
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000226
Denis Vlasenko618a3022008-11-11 21:15:56 +0000227 if (opt & OPT_c_total)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000228 print(total, "total");
Manuel Novoa III cad53642003-03-19 09:13:01 +0000229
Denis Vlasenko21b83cf2007-09-03 20:05:58 +0000230 fflush_stdout_and_exit(G.status);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000231}