blob: 9cb328aa3117b36ba4bc5a1bc959633497b39031 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
3 * Mini df implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00006 * based on original code by (I think) Bruce Perens <bruce@pixar.com>.
7 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenc4996011999-10-20 22:08:37 +00009 */
10
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing. Also blocksize. */
12/* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
13
14/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
15 *
16 * Size reduction. Removed floating point dependency. Added error checking
Eric Andersenaff114c2004-04-14 17:51:38 +000017 * on output. Output stats on 0-sized filesystems if specifically listed on
Manuel Novoa III cad53642003-03-19 09:13:01 +000018 * the command line. Properly round *-blocks, Used, and Available quantities.
19 */
20
Eric Andersencc8ed391999-10-05 16:24:54 +000021#include <mntent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000022#include <sys/vfs.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000023#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000024
Denis Vlasenkoe3241842007-08-13 10:36:25 +000025#if !ENABLE_FEATURE_HUMAN_READABLE
Denis Vlasenko08294db2007-08-13 12:27:49 +000026static unsigned long kscale(unsigned long b, unsigned long bs)
Eric Andersencc8ed391999-10-05 16:24:54 +000027{
Denis Vlasenko08294db2007-08-13 12:27:49 +000028 return (b * (unsigned long long) bs + 1024/2) / 1024;
Eric Andersencc8ed391999-10-05 16:24:54 +000029}
Manuel Novoa III cad53642003-03-19 09:13:01 +000030#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000031
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000032int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000033int df_main(int argc, char **argv)
Eric Andersenc4996011999-10-20 22:08:37 +000034{
Denis Vlasenko08294db2007-08-13 12:27:49 +000035 unsigned long blocks_used;
36 unsigned blocks_percent_used;
37#if ENABLE_FEATURE_HUMAN_READABLE
38 unsigned df_disp_hr = 1024;
Manuel Novoa III cad53642003-03-19 09:13:01 +000039#endif
Matt Kraai92ed8a32000-12-06 15:55:23 +000040 int status = EXIT_SUCCESS;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000041 unsigned opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 FILE *mount_table;
43 struct mntent *mount_entry;
44 struct statfs s;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000045 /* default display is kilobytes */
Denis Vlasenko08294db2007-08-13 12:27:49 +000046 const char *disp_units_hdr = "1k-blocks";
Richard June6d0921c2001-01-22 22:35:38 +000047
Denis Vlasenkof430cdb2007-11-15 07:02:55 +000048 enum {
Denis Vlasenko6d38dc32007-11-15 09:02:12 +000049 OPT_ALL = (1 << 0),
50 OPT_INODE = (ENABLE_FEATURE_HUMAN_READABLE ? (1 << 4) : (1 << 2))
Denis Vlasenkof430cdb2007-11-15 07:02:55 +000051 * ENABLE_FEATURE_DF_INODE
52 };
53
Denis Vlasenko08294db2007-08-13 12:27:49 +000054#if ENABLE_FEATURE_HUMAN_READABLE
Denis Vlasenko67b23e62006-10-03 21:00:06 +000055 opt_complementary = "h-km:k-hm:m-hk";
Denis Vlasenko6d38dc32007-11-15 09:02:12 +000056 opt = getopt32(argv, "ahmk" USE_FEATURE_DF_INODE("i"));
57 if (opt & (1 << 1)) { // -h
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000058 df_disp_hr = 0;
59 disp_units_hdr = " Size";
Eric Andersen8876fb22003-06-20 09:01:58 +000060 }
Denis Vlasenko6d38dc32007-11-15 09:02:12 +000061 if (opt & (1 << 2)) { // -m
Denis Vlasenkodca0b702006-10-27 09:05:02 +000062 df_disp_hr = 1024*1024;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000063 disp_units_hdr = "1M-blocks";
Richard June6d0921c2001-01-22 22:35:38 +000064 }
Denis Vlasenkof430cdb2007-11-15 07:02:55 +000065 if (opt & OPT_INODE) {
66 disp_units_hdr = " Inodes";
67 }
Eric Andersen8876fb22003-06-20 09:01:58 +000068#else
Denis Vlasenko6d38dc32007-11-15 09:02:12 +000069 opt = getopt32(argv, "ak" USE_FEATURE_DF_INODE("i"));
Eric Andersen8876fb22003-06-20 09:01:58 +000070#endif
Matt Kraai92ed8a32000-12-06 15:55:23 +000071
Denis Vlasenko6d38dc32007-11-15 09:02:12 +000072 printf("Filesystem %-15sUsed Available Use%% Mounted on\n",
73 disp_units_hdr);
Eric Andersenc4996011999-10-20 22:08:37 +000074
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 mount_table = NULL;
76 argv += optind;
77 if (optind >= argc) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000078 mount_table = setmntent(bb_path_mtab_file, "r");
79 if (!mount_table) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 bb_perror_msg_and_die(bb_path_mtab_file);
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 }
Eric Andersenc4996011999-10-20 22:08:37 +000082 }
83
Denis Vlasenko08294db2007-08-13 12:27:49 +000084 while (1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 const char *device;
86 const char *mount_point;
87
88 if (mount_table) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000089 mount_entry = getmntent(mount_table);
90 if (!mount_entry) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 endmntent(mount_table);
92 break;
93 }
94 } else {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000095 mount_point = *argv++;
96 if (!mount_point) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 break;
98 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000099 mount_entry = find_mount_point(mount_point, bb_path_mtab_file);
100 if (!mount_entry) {
101 bb_error_msg("%s: can't find mount point", mount_point);
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000102 SET_ERROR:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 status = EXIT_FAILURE;
104 continue;
105 }
106 }
107
108 device = mount_entry->mnt_fsname;
109 mount_point = mount_entry->mnt_dir;
110
111 if (statfs(mount_point, &s) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000112 bb_simple_perror_msg(mount_point);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 goto SET_ERROR;
114 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000115
Denis Vlasenko6d38dc32007-11-15 09:02:12 +0000116 if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
Denis Vlasenkof430cdb2007-11-15 07:02:55 +0000117 if (opt & OPT_INODE) {
118 s.f_blocks = s.f_files;
119 s.f_bavail = s.f_bfree = s.f_ffree;
120 s.f_bsize = 1;
121#if ENABLE_FEATURE_HUMAN_READABLE
122 if (df_disp_hr)
123 df_disp_hr = 1;
124#endif
125 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 blocks_used = s.f_blocks - s.f_bfree;
127 blocks_percent_used = 0;
128 if (blocks_used + s.f_bavail) {
Denis Vlasenko08294db2007-08-13 12:27:49 +0000129 blocks_percent_used = (blocks_used * 100ULL
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000130 + (blocks_used + s.f_bavail)/2
131 ) / (blocks_used + s.f_bavail);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000132 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000133
Denis Vlasenko6d38dc32007-11-15 09:02:12 +0000134#ifdef WHY_IT_SHOULD_BE_HIDDEN
Eric Andersen9d7f0f02003-06-20 09:36:49 +0000135 if (strcmp(device, "rootfs") == 0) {
136 continue;
Denis Vlasenkof430cdb2007-11-15 07:02:55 +0000137 }
Denis Vlasenko6d38dc32007-11-15 09:02:12 +0000138#endif
139#ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
140/* ... and also this is the only user of find_block_device */
Denis Vlasenkof430cdb2007-11-15 07:02:55 +0000141 if (strcmp(device, "/dev/root") == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 /* Adjusts device to be the real root device,
143 * or leaves device alone if it can't find it */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000144 device = find_block_device("/");
145 if (!device) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000146 goto SET_ERROR;
147 }
148 }
Denis Vlasenko6d38dc32007-11-15 09:02:12 +0000149#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000150
Denis Vlasenko08294db2007-08-13 12:27:49 +0000151 if (printf("\n%-20s" + 1, device) > 20)
152 printf("\n%-20s", "");
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000153#if ENABLE_FEATURE_HUMAN_READABLE
Denis Vlasenko08294db2007-08-13 12:27:49 +0000154 printf(" %9s ",
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000155 make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000156
Denis Vlasenko08294db2007-08-13 12:27:49 +0000157 printf(" %9s " + 1,
158 make_human_readable_str((s.f_blocks - s.f_bfree),
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000159 s.f_bsize, df_disp_hr));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000160
Denis Vlasenko08294db2007-08-13 12:27:49 +0000161 printf("%9s %3u%% %s\n",
162 make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
163 blocks_percent_used, mount_point);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164#else
Denis Vlasenko08294db2007-08-13 12:27:49 +0000165 printf(" %9lu %9lu %9lu %3u%% %s\n",
166 kscale(s.f_blocks, s.f_bsize),
167 kscale(s.f_blocks-s.f_bfree, s.f_bsize),
168 kscale(s.f_bavail, s.f_bsize),
169 blocks_percent_used, mount_point);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000170#endif
171 }
Denis Vlasenko08294db2007-08-13 12:27:49 +0000172 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000173
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000174 fflush_stdout_and_exit(status);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000175}