1 /* df.c - report free disk space.
2  *
3  * Copyright 2006 Rob Landley <rob@landley.net>
4  *
5  * See http://opengroup.org/onlinepubs/9699919799/utilities/df.html
6 
7 USE_DF(NEWTOY(df, "Pkt*a[-Pk]", TOYFLAG_SBIN))
8 
9 config DF
10   bool "df"
11   default y
12   help
13     usage: df [-t type] [FILESYSTEM ...]
14 
15     The "disk free" command shows total/used/available disk space for
16     each filesystem listed on the command line, or all currently mounted
17     filesystems.
18 
19     -P	The SUSv3 "Pedantic" option
20     -k	Sets units back to 1024 bytes (the default without -P)
21     -t type	Display only filesystems of this type.
22 
23     Pedantic provides a slightly less useful output format dictated by Posix,
24     and sets the units to 512 bytes instead of the default 1024 bytes.
25 */
26 
27 #define FOR_df
28 #include "toys.h"
29 
GLOBALS(struct arg_list * fstype;long units;)30 GLOBALS(
31   struct arg_list *fstype;
32 
33   long units;
34 )
35 
36 static void show_mt(struct mtab_list *mt)
37 {
38   int len;
39   long long size, used, avail, percent, block;
40   char *device;
41 
42   // Return if it wasn't found (should never happen, but with /etc/mtab...)
43   if (!mt) return;
44 
45   // If we have -t, skip other filesystem types
46   if (TT.fstype) {
47     struct arg_list *al;
48 
49     for (al = TT.fstype; al; al = al->next)
50       if (!strcmp(mt->type, al->arg)) break;
51 
52     if (!al) return;
53   }
54 
55   // If we don't have -a, skip synthetic filesystems
56   if (!(toys.optflags & FLAG_a) && !mt->statvfs.f_blocks) return;
57 
58   // Figure out how much total/used/free space this filesystem has,
59   // forcing 64-bit math because filesystems are big now.
60   block = mt->statvfs.f_bsize ? mt->statvfs.f_bsize : 1;
61   size = (block * mt->statvfs.f_blocks) / TT.units;
62   used = (block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree)) / TT.units;
63   avail = (block*(getuid()?mt->statvfs.f_bavail:mt->statvfs.f_bfree))/TT.units;
64   if (!(used+avail)) percent = 0;
65   else {
66     percent = (used*100)/(used+avail);
67     if (used*100 != percent*(used+avail)) percent++;
68   }
69 
70   device = *mt->device == '/' ? realpath(mt->device, NULL) : NULL;
71   if (!device) device = mt->device;
72 
73   // Figure out appropriate spacing
74   len = 25 - strlen(device);
75   if (len < 1) len = 1;
76   xprintf("%s% *lld % 10lld % 10lld % *lld%% %s\n", device, len,
77     size, used, avail, (toys.optflags & FLAG_P) ? 7 : 3, percent, mt->dir);
78 
79   if (device != mt->device) free(device);
80 }
81 
df_main(void)82 void df_main(void)
83 {
84   struct mtab_list *mt, *mtstart, *mtend;
85   int p = toys.optflags & FLAG_P;
86 
87   // Units are 512 bytes if you select "pedantic" without "kilobytes".
88   TT.units = p ? 512 : 1024;
89   xprintf("Filesystem%8s-blocks\tUsed  Available %s Mounted on\n",
90     p ? "512" : "1K", p ? "Capacity" : "Use%");
91 
92   if (!(mtstart = xgetmountlist(0))) return;
93   mtend = dlist_terminate(mtstart);
94 
95   // If we have a list of filesystems on the command line, loop through them.
96   if (*toys.optargs) {
97     char **next;
98 
99     for(next = toys.optargs; *next; next++) {
100       struct stat st;
101 
102       // Stat it (complain if we can't).
103       if(stat(*next, &st)) {
104         perror_msg("'%s'", *next);
105         continue;
106       }
107 
108       // Find and display this filesystem.  Use _last_ hit in case of
109       // overmounts (which is first hit in the reversed list).
110       for (mt = mtend; mt; mt = mt->prev) {
111         if (st.st_dev == mt->stat.st_dev
112             || (st.st_rdev && (st.st_rdev == mt->stat.st_dev)))
113         {
114           show_mt(mt);
115           break;
116         }
117       }
118     }
119   } else {
120     // Loop through mount list to filter out overmounts.
121     for (mt = mtend; mt; mt = mt->prev) {
122       struct mtab_list *mt2, *mt3;
123 
124       // 0:0 is LANANA null device
125       if (!mt->stat.st_dev) continue;
126 
127       // Filter out overmounts.
128       mt3 = mt;
129       for (mt2 = mt->prev; mt2; mt2 = mt2->prev) {
130         if (mt->stat.st_dev == mt2->stat.st_dev) {
131           // For --bind mounts, show earliest mount
132           if (!strcmp(mt->device, mt2->device)) {
133             if (!toys.optflags & FLAG_a) mt3->stat.st_dev = 0;
134             mt3 = mt2;
135           } else mt2->stat.st_dev = 0;
136         }
137       }
138     }
139     // Cosmetic: show filesystems in creation order
140     for (mt = mtstart; mt; mt = mt->next) if (mt->stat.st_dev) show_mt(mt);
141   }
142 
143   if (CFG_TOYBOX_FREE) llist_traverse(mtstart, free);
144 }
145