3
0

du.c 8.0 KB

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