df.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini df implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. * based on original code by (I think) Bruce Perens <bruce@pixar.com>.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  11. *
  12. * Size reduction. Removed floating point dependency. Added error checking
  13. * on output. Output stats on 0-sized filesystems if specifically listed on
  14. * the command line. Properly round *-blocks, Used, and Available quantities.
  15. *
  16. * Aug 28, 2008 Bernhard Reutner-Fischer
  17. *
  18. * Implement -P and -B; better coreutils compat; cleanup
  19. */
  20. //config:config DF
  21. //config: bool "df (6.8 kb)"
  22. //config: default y
  23. //config: help
  24. //config: df reports the amount of disk space used and available
  25. //config: on filesystems.
  26. //config:
  27. //config:config FEATURE_DF_FANCY
  28. //config: bool "Enable -a, -i, -B"
  29. //config: default y
  30. //config: depends on DF
  31. //config: help
  32. //config: -a Show all filesystems
  33. //config: -i Inodes
  34. //config: -B <SIZE> Blocksize
  35. //config:
  36. //config:config FEATURE_SKIP_ROOTFS
  37. //config: bool "Skip rootfs in mount table"
  38. //config: default y
  39. //config: depends on DF
  40. //config: help
  41. //config: Ignore rootfs entry in mount table.
  42. //config:
  43. //config: In Linux, kernel has a special filesystem, rootfs, which is initially
  44. //config: mounted on /. It contains initramfs data, if kernel is configured
  45. //config: to have one. Usually, another file system is mounted over / early
  46. //config: in boot process, and therefore most tools which manipulate
  47. //config: mount table, such as df, will skip rootfs entry.
  48. //config:
  49. //config: However, some systems do not mount anything on /.
  50. //config: If you need to configure busybox for one of these systems,
  51. //config: you may find it useful to turn this option off to make df show
  52. //config: initramfs statistics.
  53. //config:
  54. //config: Otherwise, choose Y.
  55. //applet:IF_DF(APPLET_NOEXEC(df, df, BB_DIR_BIN, BB_SUID_DROP, df))
  56. //kbuild:lib-$(CONFIG_DF) += df.o
  57. /* BB_AUDIT SUSv3 _NOT_ compliant -- option -t missing. */
  58. /* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
  59. //usage:#define df_trivial_usage
  60. //usage: "[-Pk"
  61. //usage: IF_FEATURE_HUMAN_READABLE("mh")
  62. //usage: "T"
  63. //usage: IF_FEATURE_DF_FANCY("ai] [-B SIZE")
  64. //usage: "] [-t TYPE] [FILESYSTEM]..."
  65. //usage:#define df_full_usage "\n\n"
  66. //usage: "Print filesystem usage statistics\n"
  67. //usage: "\n -P POSIX output format"
  68. //usage: "\n -k 1024-byte blocks (default)"
  69. //usage: IF_FEATURE_HUMAN_READABLE(
  70. //usage: "\n -m 1M-byte blocks"
  71. //usage: "\n -h Human readable (e.g. 1K 243M 2G)"
  72. //usage: )
  73. //usage: "\n -T Print filesystem type"
  74. //usage: "\n -t TYPE Print only mounts of this type"
  75. //usage: IF_FEATURE_DF_FANCY(
  76. //usage: "\n -a Show all filesystems"
  77. //usage: "\n -i Inodes"
  78. //usage: "\n -B SIZE Blocksize"
  79. //usage: )
  80. //usage:
  81. //usage:#define df_example_usage
  82. //usage: "$ df\n"
  83. //usage: "Filesystem 1K-blocks Used Available Use% Mounted on\n"
  84. //usage: "/dev/sda3 8690864 8553540 137324 98% /\n"
  85. //usage: "/dev/sda1 64216 36364 27852 57% /boot\n"
  86. //usage: "$ df /dev/sda3\n"
  87. //usage: "Filesystem 1K-blocks Used Available Use% Mounted on\n"
  88. //usage: "/dev/sda3 8690864 8553540 137324 98% /\n"
  89. //usage: "$ POSIXLY_CORRECT=sure df /dev/sda3\n"
  90. //usage: "Filesystem 512B-blocks Used Available Use% Mounted on\n"
  91. //usage: "/dev/sda3 17381728 17107080 274648 98% /\n"
  92. //usage: "$ POSIXLY_CORRECT=yep df -P /dev/sda3\n"
  93. //usage: "Filesystem 512-blocks Used Available Capacity Mounted on\n"
  94. //usage: "/dev/sda3 17381728 17107080 274648 98% /\n"
  95. #include <mntent.h>
  96. #include <sys/statvfs.h>
  97. #include "libbb.h"
  98. #include "unicode.h"
  99. #if !ENABLE_FEATURE_HUMAN_READABLE
  100. static unsigned long kscale(unsigned long b, unsigned long bs)
  101. {
  102. return (b * (unsigned long long) bs + 1024/2) / 1024;
  103. }
  104. #endif
  105. int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  106. int df_main(int argc UNUSED_PARAM, char **argv)
  107. {
  108. unsigned long df_disp_hr = 1024;
  109. int status = EXIT_SUCCESS;
  110. unsigned opt;
  111. FILE *mount_table;
  112. struct mntent *mount_entry;
  113. struct statvfs s;
  114. enum {
  115. OPT_KILO = (1 << 0),
  116. OPT_POSIX = (1 << 1),
  117. OPT_FSTYPE = (1 << 2),
  118. OPT_t = (1 << 3),
  119. OPT_ALL = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
  120. OPT_INODE = (1 << 5) * ENABLE_FEATURE_DF_FANCY,
  121. OPT_BSIZE = (1 << 6) * ENABLE_FEATURE_DF_FANCY,
  122. OPT_HUMAN = (1 << (4 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  123. OPT_HUMANDEC = (1 << (5 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  124. OPT_MEGA = (1 << (6 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
  125. };
  126. const char *disp_units_hdr = NULL;
  127. char *chp, *opt_t;
  128. init_unicode();
  129. /* From the manpage of df from coreutils-6.10:
  130. * Disk space is shown in 1K blocks by default, unless the environment
  131. * variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
  132. */
  133. if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
  134. df_disp_hr = 512;
  135. opt = getopt32(argv, "^"
  136. "kPTt:"
  137. IF_FEATURE_DF_FANCY("aiB:")
  138. IF_FEATURE_HUMAN_READABLE("hHm")
  139. "\0"
  140. #if ENABLE_FEATURE_HUMAN_READABLE && ENABLE_FEATURE_DF_FANCY
  141. "k-mB:m-Bk:B-km"
  142. #elif ENABLE_FEATURE_HUMAN_READABLE
  143. "k-m:m-k"
  144. #endif
  145. , &opt_t
  146. IF_FEATURE_DF_FANCY(, &chp)
  147. );
  148. if (opt & OPT_MEGA)
  149. df_disp_hr = 1024*1024;
  150. if (opt & OPT_BSIZE) {
  151. /* GNU coreutils 8.25 accepts "-BMiB" form too */
  152. int i;
  153. for (i = 0; kmg_i_suffixes[i].suffix[0]; i++) {
  154. if (strcmp(kmg_i_suffixes[i].suffix, chp) == 0) {
  155. df_disp_hr = kmg_i_suffixes[i].mult;
  156. goto got_it;
  157. }
  158. }
  159. /* Range used to disallow 0 */
  160. df_disp_hr = xatoul_range_sfx(chp, 1, ULONG_MAX, kmg_i_suffixes);
  161. got_it: ;
  162. }
  163. if (opt & (OPT_HUMAN|OPT_HUMANDEC)) {
  164. df_disp_hr = 0;
  165. //TODO: need to add support in make_human_readable_str() for "decimal human readable"
  166. //if (opt & OPT_HUMANDEC)
  167. // df_disp_hr--;
  168. disp_units_hdr = " Size";
  169. }
  170. if (opt & OPT_INODE)
  171. disp_units_hdr = " Inodes";
  172. if (disp_units_hdr == NULL) {
  173. #if ENABLE_FEATURE_HUMAN_READABLE
  174. disp_units_hdr = xasprintf("%s-blocks",
  175. /* print df_disp_hr, show no fractionals,
  176. * use suffixes if OPT_POSIX is set in opt */
  177. make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))
  178. );
  179. #else
  180. disp_units_hdr = xasprintf("%lu-blocks", df_disp_hr);
  181. #endif
  182. }
  183. printf("Filesystem %s%-15sUsed Available %s Mounted on\n",
  184. (opt & OPT_FSTYPE) ? "Type " : "",
  185. disp_units_hdr,
  186. (opt & OPT_POSIX) ? "Capacity" : "Use%");
  187. mount_table = NULL;
  188. argv += optind;
  189. if (!argv[0]) {
  190. mount_table = setmntent(bb_path_mtab_file, "r");
  191. if (!mount_table)
  192. bb_simple_perror_msg_and_die(bb_path_mtab_file);
  193. }
  194. while (1) {
  195. const char *device;
  196. const char *mount_point;
  197. const char *fs_type;
  198. if (mount_table) {
  199. mount_entry = getmntent(mount_table);
  200. if (!mount_entry) {
  201. endmntent(mount_table);
  202. break;
  203. }
  204. } else {
  205. mount_point = *argv++;
  206. if (!mount_point)
  207. break;
  208. mount_entry = find_mount_point(mount_point, 1);
  209. if (!mount_entry) {
  210. bb_error_msg("%s: can't find mount point", mount_point);
  211. set_error:
  212. status = EXIT_FAILURE;
  213. continue;
  214. }
  215. }
  216. device = mount_entry->mnt_fsname;
  217. /* GNU coreutils 6.10 skips certain mounts, try to be compatible */
  218. if (ENABLE_FEATURE_SKIP_ROOTFS && strcmp(device, "rootfs") == 0)
  219. continue;
  220. mount_point = mount_entry->mnt_dir;
  221. fs_type = mount_entry->mnt_type;
  222. if (opt & OPT_t) {
  223. if (strcmp(fs_type, opt_t) != 0)
  224. continue;
  225. }
  226. if (statvfs(mount_point, &s) != 0) {
  227. bb_simple_perror_msg(mount_point);
  228. goto set_error;
  229. }
  230. /* Some uclibc versions were seen to lose f_frsize
  231. * (kernel does return it, but then uclibc does not copy it)
  232. */
  233. if (s.f_frsize == 0)
  234. s.f_frsize = s.f_bsize;
  235. if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
  236. unsigned long long blocks_used;
  237. unsigned long long blocks_total;
  238. unsigned blocks_percent_used;
  239. if (opt & OPT_INODE) {
  240. s.f_blocks = s.f_files;
  241. s.f_bavail = s.f_bfree = s.f_ffree;
  242. s.f_frsize = 1;
  243. if (df_disp_hr)
  244. df_disp_hr = 1;
  245. }
  246. blocks_used = s.f_blocks - s.f_bfree;
  247. blocks_total = blocks_used + s.f_bavail;
  248. blocks_percent_used = blocks_total; /* 0% if blocks_total == 0, else... */
  249. if (blocks_total != 0) {
  250. /* Downscale sizes for narrower division */
  251. unsigned u;
  252. while (blocks_total >= INT_MAX / 101) {
  253. blocks_total >>= 1;
  254. blocks_used >>= 1;
  255. }
  256. u = (unsigned)blocks_used * 100u + (unsigned)blocks_total / 2;
  257. blocks_percent_used = u / (unsigned)blocks_total;
  258. }
  259. #ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
  260. if (strcmp(device, "/dev/root") == 0) {
  261. /* Adjusts device to be the real root device,
  262. * or leaves device alone if it can't find it */
  263. device = find_block_device("/");
  264. if (!device) {
  265. goto set_error;
  266. }
  267. }
  268. #endif
  269. #if ENABLE_UNICODE_SUPPORT
  270. {
  271. uni_stat_t uni_stat;
  272. char *uni_dev = unicode_conv_to_printable(&uni_stat, device);
  273. if (uni_stat.unicode_width > 20 && !(opt & OPT_POSIX)) {
  274. printf("%s\n%20s", uni_dev, "");
  275. } else {
  276. printf("%s%*s", uni_dev, 20 - (int)uni_stat.unicode_width, "");
  277. }
  278. free(uni_dev);
  279. if (opt & OPT_FSTYPE) {
  280. char *uni_type = unicode_conv_to_printable(&uni_stat, fs_type);
  281. if (uni_stat.unicode_width > 10 && !(opt & OPT_POSIX))
  282. printf(" %s\n%31s", uni_type, "");
  283. else
  284. printf(" %s%*s", uni_type, 10 - (int)uni_stat.unicode_width, "");
  285. free(uni_type);
  286. }
  287. }
  288. #else
  289. if (printf("\n%-20s" + 1, device) > 20 && !(opt & OPT_POSIX))
  290. printf("\n%-20s", "");
  291. if (opt & OPT_FSTYPE) {
  292. if (printf(" %-10s", fs_type) > 11 && !(opt & OPT_POSIX))
  293. printf("\n%-30s", "");
  294. }
  295. #endif
  296. #if ENABLE_FEATURE_HUMAN_READABLE
  297. printf(" %9s ",
  298. /* f_blocks x f_frsize / df_disp_hr, show one fractional,
  299. * use suffixes if df_disp_hr == 0 */
  300. make_human_readable_str(s.f_blocks, s.f_frsize, df_disp_hr));
  301. printf(" %9s " + 1,
  302. /* EXPR x f_frsize / df_disp_hr, show one fractional,
  303. * use suffixes if df_disp_hr == 0 */
  304. make_human_readable_str((s.f_blocks - s.f_bfree),
  305. s.f_frsize, df_disp_hr));
  306. printf("%9s %3u%% %s\n",
  307. /* f_bavail x f_frsize / df_disp_hr, show one fractional,
  308. * use suffixes if df_disp_hr == 0 */
  309. make_human_readable_str(s.f_bavail, s.f_frsize, df_disp_hr),
  310. blocks_percent_used, mount_point);
  311. #else
  312. printf(" %9lu %9lu %9lu %3u%% %s\n",
  313. kscale(s.f_blocks, s.f_frsize),
  314. kscale(s.f_blocks - s.f_bfree, s.f_frsize),
  315. kscale(s.f_bavail, s.f_frsize),
  316. blocks_percent_used, mount_point);
  317. #endif
  318. }
  319. }
  320. return status;
  321. }