du.c 5.7 KB

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