du.c 7.6 KB

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