du.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 (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. //applet:IF_DU(APPLET(du, BB_DIR_USR_BIN, BB_SUID_DROP))
  35. //kbuild:lib-$(CONFIG_DU) += du.o
  36. /* BB_AUDIT SUSv3 compliant (unless default blocksize set to 1k) */
  37. /* http://www.opengroup.org/onlinepubs/007904975/utilities/du.html */
  38. //usage:#define du_trivial_usage
  39. //usage: "[-aHLdclsx" IF_FEATURE_HUMAN_READABLE("hm") "k] [FILE]..."
  40. //usage:#define du_full_usage "\n\n"
  41. //usage: "Summarize disk space used for each FILE and/or directory\n"
  42. //usage: "\n -a Show file sizes too"
  43. //usage: "\n -L Follow all symlinks"
  44. //usage: "\n -H Follow symlinks on command line"
  45. //usage: "\n -d N Limit output to directories (and files with -a) of depth < N"
  46. //usage: "\n -c Show grand total"
  47. //usage: "\n -l Count sizes many times if hard linked"
  48. //usage: "\n -s Display only a total for each argument"
  49. //usage: "\n -x Skip directories on different filesystems"
  50. //usage: IF_FEATURE_HUMAN_READABLE(
  51. //usage: "\n -h Sizes in human readable format (e.g., 1K 243M 2G)"
  52. //usage: "\n -m Sizes in megabytes"
  53. //usage: )
  54. //usage: "\n -k Sizes in kilobytes" IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(" (default)")
  55. //usage: IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(
  56. //usage: "\n Default unit is 512 bytes"
  57. //usage: )
  58. //usage:
  59. //usage:#define du_example_usage
  60. //usage: "$ du\n"
  61. //usage: "16 ./CVS\n"
  62. //usage: "12 ./kernel-patches/CVS\n"
  63. //usage: "80 ./kernel-patches\n"
  64. //usage: "12 ./tests/CVS\n"
  65. //usage: "36 ./tests\n"
  66. //usage: "12 ./scripts/CVS\n"
  67. //usage: "16 ./scripts\n"
  68. //usage: "12 ./docs/CVS\n"
  69. //usage: "104 ./docs\n"
  70. //usage: "2417 .\n"
  71. #include "libbb.h"
  72. #include "common_bufsiz.h"
  73. enum {
  74. OPT_a_files_too = (1 << 0),
  75. OPT_H_follow_links = (1 << 1),
  76. OPT_k_kbytes = (1 << 2),
  77. OPT_L_follow_links = (1 << 3),
  78. OPT_s_total_norecurse = (1 << 4),
  79. OPT_x_one_FS = (1 << 5),
  80. OPT_d_maxdepth = (1 << 6),
  81. OPT_l_hardlinks = (1 << 7),
  82. OPT_c_total = (1 << 8),
  83. OPT_h_for_humans = (1 << 9),
  84. OPT_m_mbytes = (1 << 10),
  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 comtat:
  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, 512, G.disp_unit),
  121. filename);
  122. #else
  123. if (G.disp_k) {
  124. size++;
  125. size >>= 1;
  126. }
  127. printf("%llu\t%s\n", size, filename);
  128. #endif
  129. }
  130. /* tiny recursive du */
  131. static unsigned long long du(const char *filename)
  132. {
  133. struct stat statbuf;
  134. unsigned long long sum;
  135. if (lstat(filename, &statbuf) != 0) {
  136. bb_simple_perror_msg(filename);
  137. G.status = EXIT_FAILURE;
  138. return 0;
  139. }
  140. if (option_mask32 & OPT_x_one_FS) {
  141. if (G.du_depth == 0) {
  142. G.dir_dev = statbuf.st_dev;
  143. } else if (G.dir_dev != statbuf.st_dev) {
  144. return 0;
  145. }
  146. }
  147. sum = statbuf.st_blocks;
  148. if (S_ISLNK(statbuf.st_mode)) {
  149. if (G.slink_depth > G.du_depth) { /* -H or -L */
  150. if (stat(filename, &statbuf) != 0) {
  151. bb_simple_perror_msg(filename);
  152. G.status = EXIT_FAILURE;
  153. return 0;
  154. }
  155. sum = statbuf.st_blocks;
  156. if (G.slink_depth == 1) {
  157. /* Convert -H to -L */
  158. G.slink_depth = INT_MAX;
  159. }
  160. }
  161. }
  162. if (!(option_mask32 & OPT_l_hardlinks)
  163. && statbuf.st_nlink > 1
  164. ) {
  165. /* Add files/directories with links only once */
  166. if (is_in_ino_dev_hashtable(&statbuf)) {
  167. return 0;
  168. }
  169. add_to_ino_dev_hashtable(&statbuf, NULL);
  170. }
  171. if (S_ISDIR(statbuf.st_mode)) {
  172. DIR *dir;
  173. struct dirent *entry;
  174. char *newfile;
  175. dir = warn_opendir(filename);
  176. if (!dir) {
  177. G.status = EXIT_FAILURE;
  178. return sum;
  179. }
  180. while ((entry = readdir(dir))) {
  181. newfile = concat_subpath_file(filename, entry->d_name);
  182. if (newfile == NULL)
  183. continue;
  184. ++G.du_depth;
  185. sum += du(newfile);
  186. --G.du_depth;
  187. free(newfile);
  188. }
  189. closedir(dir);
  190. } else {
  191. if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0)
  192. return sum;
  193. }
  194. if (G.du_depth <= G.max_print_depth) {
  195. print(sum, filename);
  196. }
  197. return sum;
  198. }
  199. int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  200. int du_main(int argc UNUSED_PARAM, char **argv)
  201. {
  202. unsigned long long total;
  203. int slink_depth_save;
  204. unsigned opt;
  205. INIT_G();
  206. #if ENABLE_FEATURE_HUMAN_READABLE
  207. IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;)
  208. IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;)
  209. if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
  210. G.disp_unit = 512;
  211. #else
  212. IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
  213. /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
  214. #endif
  215. G.max_print_depth = INT_MAX;
  216. /* Note: SUSv3 specifies that -a and -s options cannot be used together
  217. * in strictly conforming applications. However, it also says that some
  218. * du implementations may produce output when -a and -s are used together.
  219. * gnu du exits with an error code in this case. We choose to simply
  220. * ignore -a. This is consistent with -s being equivalent to -d 0.
  221. */
  222. #if ENABLE_FEATURE_HUMAN_READABLE
  223. opt = getopt32(argv, "^"
  224. "aHkLsxd:+lchm"
  225. "\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s",
  226. &G.max_print_depth
  227. );
  228. argv += optind;
  229. if (opt & OPT_h_for_humans) {
  230. G.disp_unit = 0;
  231. }
  232. if (opt & OPT_m_mbytes) {
  233. G.disp_unit = 1024*1024;
  234. }
  235. if (opt & OPT_k_kbytes) {
  236. G.disp_unit = 1024;
  237. }
  238. #else
  239. opt = getopt32(argv, "^"
  240. "aHkLsxd:+lc"
  241. "\0" "H-L:L-H:s-d:d-s",
  242. &G.max_print_depth
  243. );
  244. argv += optind;
  245. #if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
  246. if (opt & OPT_k_kbytes) {
  247. G.disp_k = 1;
  248. }
  249. #endif
  250. #endif
  251. if (opt & OPT_H_follow_links) {
  252. G.slink_depth = 1;
  253. }
  254. if (opt & OPT_L_follow_links) {
  255. G.slink_depth = INT_MAX;
  256. }
  257. if (opt & OPT_s_total_norecurse) {
  258. G.max_print_depth = 0;
  259. }
  260. /* go through remaining args (if any) */
  261. if (!*argv) {
  262. *--argv = (char*)".";
  263. if (G.slink_depth == 1) {
  264. G.slink_depth = 0;
  265. }
  266. }
  267. slink_depth_save = G.slink_depth;
  268. total = 0;
  269. do {
  270. total += du(*argv);
  271. /* otherwise du /dir /dir won't show /dir twice: */
  272. reset_ino_dev_hashtable();
  273. G.slink_depth = slink_depth_save;
  274. } while (*++argv);
  275. if (opt & OPT_c_total)
  276. print(total, "total");
  277. fflush_stdout_and_exit(G.status);
  278. }