du.c 5.6 KB

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