du.c 5.6 KB

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