3
0

du.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 tarball for details.
  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. #include "libbb.h"
  24. struct globals {
  25. #if ENABLE_FEATURE_HUMAN_READABLE
  26. unsigned long disp_hr;
  27. #else
  28. unsigned disp_k;
  29. #endif
  30. int max_print_depth;
  31. nlink_t count_hardlinks;
  32. bool status;
  33. bool one_file_system;
  34. int print_files;
  35. int slink_depth;
  36. int du_depth;
  37. dev_t dir_dev;
  38. };
  39. #define G (*(struct globals*)&bb_common_bufsiz1)
  40. static void print(unsigned long size, const char *filename)
  41. {
  42. /* TODO - May not want to defer error checking here. */
  43. #if ENABLE_FEATURE_HUMAN_READABLE
  44. printf("%s\t%s\n", make_human_readable_str(size, 512, G.disp_hr),
  45. filename);
  46. #else
  47. if (G.disp_k) {
  48. size++;
  49. size >>= 1;
  50. }
  51. printf("%ld\t%s\n", size, filename);
  52. #endif
  53. }
  54. /* tiny recursive du */
  55. static unsigned long du(const char *filename)
  56. {
  57. struct stat statbuf;
  58. unsigned long sum;
  59. if (lstat(filename, &statbuf) != 0) {
  60. bb_simple_perror_msg(filename);
  61. G.status = EXIT_FAILURE;
  62. return 0;
  63. }
  64. if (G.one_file_system) {
  65. if (G.du_depth == 0) {
  66. G.dir_dev = statbuf.st_dev;
  67. } else if (G.dir_dev != statbuf.st_dev) {
  68. return 0;
  69. }
  70. }
  71. sum = statbuf.st_blocks;
  72. if (S_ISLNK(statbuf.st_mode)) {
  73. if (G.slink_depth > G.du_depth) { /* -H or -L */
  74. if (stat(filename, &statbuf) != 0) {
  75. bb_simple_perror_msg(filename);
  76. G.status = EXIT_FAILURE;
  77. return 0;
  78. }
  79. sum = statbuf.st_blocks;
  80. if (G.slink_depth == 1) {
  81. G.slink_depth = INT_MAX; /* Convert -H to -L. */
  82. }
  83. }
  84. }
  85. if (statbuf.st_nlink > G.count_hardlinks) {
  86. /* Add files/directories with links only once */
  87. if (is_in_ino_dev_hashtable(&statbuf)) {
  88. return 0;
  89. }
  90. add_to_ino_dev_hashtable(&statbuf, NULL);
  91. }
  92. if (S_ISDIR(statbuf.st_mode)) {
  93. DIR *dir;
  94. struct dirent *entry;
  95. char *newfile;
  96. dir = warn_opendir(filename);
  97. if (!dir) {
  98. G.status = EXIT_FAILURE;
  99. return sum;
  100. }
  101. newfile = last_char_is(filename, '/');
  102. if (newfile)
  103. *newfile = '\0';
  104. while ((entry = readdir(dir))) {
  105. char *name = entry->d_name;
  106. newfile = concat_subpath_file(filename, name);
  107. if (newfile == NULL)
  108. continue;
  109. ++G.du_depth;
  110. sum += du(newfile);
  111. --G.du_depth;
  112. free(newfile);
  113. }
  114. closedir(dir);
  115. } else if (G.du_depth > G.print_files) {
  116. return sum;
  117. }
  118. if (G.du_depth <= G.max_print_depth) {
  119. print(sum, filename);
  120. }
  121. return sum;
  122. }
  123. int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  124. int du_main(int argc UNUSED_PARAM, char **argv)
  125. {
  126. unsigned long total;
  127. int slink_depth_save;
  128. bool print_final_total;
  129. unsigned opt;
  130. #if ENABLE_FEATURE_HUMAN_READABLE
  131. USE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;)
  132. SKIP_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;)
  133. if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
  134. G.disp_hr = 512;
  135. #else
  136. USE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
  137. /* SKIP_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
  138. #endif
  139. G.max_print_depth = INT_MAX;
  140. G.count_hardlinks = 1;
  141. /* Note: SUSv3 specifies that -a and -s options cannot be used together
  142. * in strictly conforming applications. However, it also says that some
  143. * du implementations may produce output when -a and -s are used together.
  144. * gnu du exits with an error code in this case. We choose to simply
  145. * ignore -a. This is consistent with -s being equivalent to -d 0.
  146. */
  147. #if ENABLE_FEATURE_HUMAN_READABLE
  148. opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s:d+";
  149. opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &G.max_print_depth);
  150. argv += optind;
  151. if (opt & (1 << 9)) {
  152. /* -h opt */
  153. G.disp_hr = 0;
  154. }
  155. if (opt & (1 << 10)) {
  156. /* -m opt */
  157. G.disp_hr = 1024*1024;
  158. }
  159. if (opt & (1 << 2)) {
  160. /* -k opt */
  161. G.disp_hr = 1024;
  162. }
  163. #else
  164. opt_complementary = "H-L:L-H:s-d:d-s:d+";
  165. opt = getopt32(argv, "aHkLsx" "d:" "lc", &G.max_print_depth);
  166. argv += optind;
  167. #if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
  168. if (opt & (1 << 2)) {
  169. /* -k opt */
  170. G.disp_k = 1;
  171. }
  172. #endif
  173. #endif
  174. if (opt & (1 << 0)) {
  175. /* -a opt */
  176. G.print_files = INT_MAX;
  177. }
  178. if (opt & (1 << 1)) {
  179. /* -H opt */
  180. G.slink_depth = 1;
  181. }
  182. if (opt & (1 << 3)) {
  183. /* -L opt */
  184. G.slink_depth = INT_MAX;
  185. }
  186. if (opt & (1 << 4)) {
  187. /* -s opt */
  188. G.max_print_depth = 0;
  189. }
  190. G.one_file_system = opt & (1 << 5); /* -x opt */
  191. if (opt & (1 << 7)) {
  192. /* -l opt */
  193. G.count_hardlinks = MAXINT(nlink_t);
  194. }
  195. print_final_total = opt & (1 << 8); /* -c opt */
  196. /* go through remaining args (if any) */
  197. if (!*argv) {
  198. *--argv = (char*)".";
  199. if (G.slink_depth == 1) {
  200. G.slink_depth = 0;
  201. }
  202. }
  203. slink_depth_save = G.slink_depth;
  204. total = 0;
  205. do {
  206. total += du(*argv);
  207. G.slink_depth = slink_depth_save;
  208. } while (*++argv);
  209. if (ENABLE_FEATURE_CLEAN_UP)
  210. reset_ino_dev_hashtable();
  211. if (print_final_total)
  212. print(total, "total");
  213. fflush_stdout_and_exit(G.status);
  214. }