3
0

du.c 7.3 KB

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