3
0

du.c 7.6 KB

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