du.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini du implementation for busybox
  4. *
  5. * Copyright (C) 1999,2000,2001 by Lineo, inc. and John Beppu
  6. * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
  7. * Copyright (C) 2002 Edward Betts <edward@debian.org>
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. */
  11. /* 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. */
  21. //config:config DU
  22. //config: bool "du (6.5 kb)"
  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 default blocksize of 1024 bytes (else it's 512 bytes)"
  30. //config: default y
  31. //config: depends on DU
  32. //applet:IF_DU(APPLET(du, BB_DIR_USR_BIN, BB_SUID_DROP))
  33. //kbuild:lib-$(CONFIG_DU) += du.o
  34. /* BB_AUDIT SUSv3 compliant (unless default blocksize set to 1k) */
  35. /* http://www.opengroup.org/onlinepubs/007904975/utilities/du.html */
  36. //usage:#define du_trivial_usage
  37. //usage: "[-aHLdclsx" IF_FEATURE_HUMAN_READABLE("hm") "k] [FILE]..."
  38. //usage:#define du_full_usage "\n\n"
  39. //usage: "Summarize disk space used for FILEs (or directories)\n"
  40. //usage: "\n -a Show file sizes too"
  41. //usage: "\n -b Apparent size (including holes)"
  42. //usage: "\n -L Follow all symlinks"
  43. //usage: "\n -H Follow symlinks on command line"
  44. //usage: "\n -d N Limit output to directories (and files with -a) of depth < N"
  45. //usage: "\n -c Show grand total"
  46. //usage: "\n -l Count sizes many times if hard linked"
  47. //usage: "\n -s Display only a total for each argument"
  48. //usage: "\n -x Skip directories on different filesystems"
  49. //usage: IF_FEATURE_HUMAN_READABLE(
  50. //usage: "\n -h Sizes in human readable format (e.g., 1K 243M 2G)"
  51. //usage: "\n -m Sizes in megabytes"
  52. //usage: )
  53. //usage: "\n -k Sizes in kilobytes" IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(" (default)")
  54. //usage: IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(
  55. //usage: "\n Default unit is 512 bytes"
  56. //usage: )
  57. //usage:
  58. //usage:#define du_example_usage
  59. //usage: "$ du\n"
  60. //usage: "16 ./CVS\n"
  61. //usage: "12 ./kernel-patches/CVS\n"
  62. //usage: "80 ./kernel-patches\n"
  63. //usage: "12 ./tests/CVS\n"
  64. //usage: "36 ./tests\n"
  65. //usage: "12 ./scripts/CVS\n"
  66. //usage: "16 ./scripts\n"
  67. //usage: "12 ./docs/CVS\n"
  68. //usage: "104 ./docs\n"
  69. //usage: "2417 .\n"
  70. #include "libbb.h"
  71. #include "common_bufsiz.h"
  72. enum {
  73. OPT_a_files_too = (1 << 0),
  74. OPT_H_follow_links = (1 << 1),
  75. OPT_k_kbytes = (1 << 2),
  76. OPT_L_follow_links = (1 << 3),
  77. OPT_s_total_norecurse = (1 << 4),
  78. OPT_x_one_FS = (1 << 5),
  79. OPT_d_maxdepth = (1 << 6),
  80. OPT_l_hardlinks = (1 << 7),
  81. OPT_c_total = (1 << 8),
  82. OPT_b = (1 << 9),
  83. OPT_h_for_humans = (1 << 10),
  84. OPT_m_mbytes = (1 << 11),
  85. };
  86. struct globals {
  87. #if ENABLE_FEATURE_HUMAN_READABLE
  88. unsigned long disp_unit;
  89. #else
  90. unsigned disp_k;
  91. #endif
  92. int max_print_depth;
  93. bool status;
  94. int slink_depth;
  95. int du_depth;
  96. dev_t dir_dev;
  97. } FIX_ALIASING;
  98. #define G (*(struct globals*)bb_common_bufsiz1)
  99. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  100. static void print(unsigned long long size, const char *filename)
  101. {
  102. /* TODO - May not want to defer error checking here. */
  103. #if ENABLE_FEATURE_HUMAN_READABLE
  104. # if ENABLE_DESKTOP
  105. /* ~30 bytes of code for extra compat:
  106. * coreutils' du rounds sizes up:
  107. * for example, 1025k file is shown as "2" by du -m.
  108. * We round to nearest if human-readable [too hard to fix],
  109. * else (fixed scale such as -m), we round up. To that end,
  110. * add yet another half of the unit before displaying:
  111. */
  112. if (G.disp_unit)
  113. size += (G.disp_unit-1) / (unsigned)(512 * 2);
  114. # endif
  115. printf("%s\t%s\n",
  116. /* size x 512 / G.disp_unit.
  117. * If G.disp_unit == 0, show one fractional
  118. * and use suffixes
  119. */
  120. make_human_readable_str(size, (option_mask32 & OPT_b) ? 1 : 512, G.disp_unit),
  121. filename);
  122. #else
  123. if (G.disp_k) {
  124. if (!(option_mask32 & OPT_b)) {
  125. size++;
  126. size >>= 1;
  127. } else {
  128. size >>= 10;
  129. }
  130. }
  131. printf("%llu\t%s\n", size, filename);
  132. #endif
  133. }
  134. /* tiny recursive du */
  135. static unsigned long long du(const char *filename)
  136. {
  137. struct stat statbuf;
  138. unsigned long long sum;
  139. if (lstat(filename, &statbuf) != 0) {
  140. bb_simple_perror_msg(filename);
  141. G.status = EXIT_FAILURE;
  142. return 0;
  143. }
  144. if (option_mask32 & OPT_x_one_FS) {
  145. if (G.du_depth == 0) {
  146. G.dir_dev = statbuf.st_dev;
  147. } else if (G.dir_dev != statbuf.st_dev) {
  148. return 0;
  149. }
  150. }
  151. sum = ((option_mask32 & OPT_b) ? statbuf.st_size : statbuf.st_blocks);
  152. if (S_ISLNK(statbuf.st_mode)) {
  153. if (G.slink_depth > G.du_depth) { /* -H or -L */
  154. if (stat(filename, &statbuf) != 0) {
  155. bb_simple_perror_msg(filename);
  156. G.status = EXIT_FAILURE;
  157. return 0;
  158. }
  159. sum = ((option_mask32 & OPT_b) ? statbuf.st_size : statbuf.st_blocks);
  160. if (G.slink_depth == 1) {
  161. /* Convert -H to -L */
  162. G.slink_depth = INT_MAX;
  163. }
  164. }
  165. }
  166. if (!(option_mask32 & OPT_l_hardlinks)
  167. && statbuf.st_nlink > 1
  168. ) {
  169. /* Add files/directories with links only once */
  170. if (is_in_ino_dev_hashtable(&statbuf)) {
  171. return 0;
  172. }
  173. add_to_ino_dev_hashtable(&statbuf, NULL);
  174. }
  175. if (S_ISDIR(statbuf.st_mode)) {
  176. DIR *dir;
  177. struct dirent *entry;
  178. char *newfile;
  179. dir = warn_opendir(filename);
  180. if (!dir) {
  181. G.status = EXIT_FAILURE;
  182. return sum;
  183. }
  184. while ((entry = readdir(dir))) {
  185. newfile = concat_subpath_file(filename, entry->d_name);
  186. if (newfile == NULL)
  187. continue;
  188. ++G.du_depth;
  189. sum += du(newfile);
  190. --G.du_depth;
  191. free(newfile);
  192. }
  193. closedir(dir);
  194. } else {
  195. if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0)
  196. return sum;
  197. }
  198. if (G.du_depth <= G.max_print_depth) {
  199. print(sum, filename);
  200. }
  201. return sum;
  202. }
  203. int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  204. int du_main(int argc UNUSED_PARAM, char **argv)
  205. {
  206. unsigned long long total;
  207. int slink_depth_save;
  208. unsigned opt;
  209. INIT_G();
  210. #if ENABLE_FEATURE_HUMAN_READABLE
  211. IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;)
  212. IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;)
  213. if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
  214. G.disp_unit = 512;
  215. #else
  216. IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
  217. /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
  218. #endif
  219. G.max_print_depth = INT_MAX;
  220. /* Note: SUSv3 specifies that -a and -s options cannot be used together
  221. * in strictly conforming applications. However, it also says that some
  222. * du implementations may produce output when -a and -s are used together.
  223. * gnu du exits with an error code in this case. We choose to simply
  224. * ignore -a. This is consistent with -s being equivalent to -d 0.
  225. */
  226. #if ENABLE_FEATURE_HUMAN_READABLE
  227. opt = getopt32(argv, "^"
  228. "aHkLsxd:+lcbhm"
  229. "\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s",
  230. &G.max_print_depth
  231. );
  232. argv += optind;
  233. if (opt & OPT_b) {
  234. G.disp_unit = 1;
  235. }
  236. if (opt & OPT_h_for_humans) {
  237. G.disp_unit = 0;
  238. }
  239. if (opt & OPT_m_mbytes) {
  240. G.disp_unit = 1024*1024;
  241. }
  242. if (opt & OPT_k_kbytes) {
  243. G.disp_unit = 1024;
  244. }
  245. #else
  246. opt = getopt32(argv, "^"
  247. "aHkLsxd:+lcb"
  248. "\0" "H-L:L-H:s-d:d-s",
  249. &G.max_print_depth
  250. );
  251. argv += optind;
  252. # if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
  253. if (opt & OPT_k_kbytes) {
  254. G.disp_k = 1;
  255. }
  256. # endif
  257. #endif
  258. if (opt & OPT_H_follow_links) {
  259. G.slink_depth = 1;
  260. }
  261. if (opt & OPT_L_follow_links) {
  262. G.slink_depth = INT_MAX;
  263. }
  264. if (opt & OPT_s_total_norecurse) {
  265. G.max_print_depth = 0;
  266. }
  267. /* go through remaining args (if any) */
  268. if (!*argv) {
  269. *--argv = (char*)".";
  270. if (G.slink_depth == 1) {
  271. G.slink_depth = 0;
  272. }
  273. }
  274. slink_depth_save = G.slink_depth;
  275. total = 0;
  276. do {
  277. total += du(*argv);
  278. G.slink_depth = slink_depth_save;
  279. } while (*++argv);
  280. if (ENABLE_FEATURE_CLEAN_UP)
  281. reset_ino_dev_hashtable();
  282. if (opt & OPT_c_total)
  283. print(total, "total");
  284. fflush_stdout_and_exit(G.status);
  285. }