1 /* du.c - disk usage program.
2  *
3  * Copyright 2012 Ashwini Kumar <ak.ashwini@gmail.com>
4  *
5  * See http://opengroup.org/onlinepubs/9699919799/utilities/du.html
6 
7 USE_DU(NEWTOY(du, "d#<0hmlcaHkKLsx[-HL][-kKmh]", TOYFLAG_USR|TOYFLAG_BIN))
8 
9 config DU
10   bool "du"
11   default y
12   help
13     usage: du [-d N] [-askxHLlmc] [file...]
14 
15     Show disk usage, space consumed by files and directories.
16 
17     Size in:
18     -k    1024 byte blocks (default)
19     -K    512 byte blocks (posix)
20     -m    megabytes
21     -h    human readable format (e.g., 1K 243M 2G )
22 
23     What to show:
24     -a    all files, not just directories
25     -H    follow symlinks on cmdline
26     -L    follow all symlinks
27     -s    only total size of each argument
28     -x    don't leave this filesystem
29     -c    cumulative total
30     -d N  only depth < N
31     -l    disable hardlink filter
32 */
33 
34 #define FOR_du
35 #include "toys.h"
36 
37 GLOBALS(
38   long maxdepth;
39 
40   long depth, total;
41   dev_t st_dev;
42   void *inodes;
43 )
44 
45 typedef struct node_size {
46   struct dirtree *node;
47   long size;
48 } node_size;
49 
50 // Print the size and name, given size in bytes
print(long long size,struct dirtree * node)51 static void print(long long size, struct dirtree *node)
52 {
53   char *name = "total";
54 
55   if (TT.maxdepth && TT.depth > TT.maxdepth) return;
56 
57   if (toys.optflags & FLAG_h) {
58     human_readable(toybuf, size);
59     printf("%s", toybuf);
60   } else {
61     int bits = 10;
62 
63     if (toys.optflags & FLAG_K) bits = 9;
64     else if (toys.optflags & FLAG_m) bits = 20;
65 
66     printf("%llu", (size>>bits)+!!(size&((1<<bits)-1)));
67   }
68   if (node) name = dirtree_path(node, NULL);
69   xprintf("\t%s\n", name);
70   if (node) free(name);
71 }
72 
73 // Return whether or not we've seen this inode+dev, adding it to the list if
74 // we haven't.
seen_inode(void ** list,struct stat * st)75 static int seen_inode(void **list, struct stat *st)
76 {
77   if (!st) llist_traverse(st, free);
78 
79   // Skipping dir nodes isn't _quite_ right. They're not hardlinked, but could
80   // be bind mounted. Still, it's more efficient and the archivers can't use
81   // hardlinked directory info anyway. (Note that we don't catch bind mounted
82   // _files_ because it doesn't change st_nlink.)
83   else if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
84     struct inode_list {
85       struct inode_list *next;
86       ino_t ino;
87       dev_t dev;
88     } *new;
89 
90     for (new = *list; new; new = new->next)
91       if(new->ino == st->st_ino && new->dev == st->st_dev)
92         return 1;
93 
94     new = xzalloc(sizeof(*new));
95     new->ino = st->st_ino;
96     new->dev = st->st_dev;
97     new->next = *list;
98     *list = new;
99   }
100 
101   return 0;
102 }
103 
104 // dirtree callback, comput/display size of node
do_du(struct dirtree * node)105 static int do_du(struct dirtree *node)
106 {
107   if (!node->parent) TT.st_dev = node->st.st_dev;
108   else if (!dirtree_notdotdot(node)) return 0;
109 
110   // detect swiching filesystems
111   if ((toys.optflags & FLAG_x) && (TT.st_dev != node->st.st_dev))
112     return 0;
113 
114   // Don't loop endlessly on recursive directory symlink
115   if (toys.optflags & FLAG_L) {
116     struct dirtree *try = node;
117 
118     while ((try = try->parent))
119       if (node->st.st_dev==try->st.st_dev && node->st.st_ino==try->st.st_ino)
120         return 0;
121   }
122 
123   // Don't count hard links twice
124   if (!(toys.optflags & FLAG_l) && !node->again)
125     if (seen_inode(&TT.inodes, &node->st)) return 0;
126 
127   // Collect child info before printing directory size
128   if (S_ISDIR(node->st.st_mode)) {
129     if (!node->again) {
130       TT.depth++;
131       return DIRTREE_COMEAGAIN|(DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L));
132     } else TT.depth--;
133   }
134 
135   node->extra += node->st.st_blocks;
136   if (node->parent) node->parent->extra += node->extra;
137   else TT.total += node->extra;
138 
139   if ((toys.optflags & FLAG_a) || !node->parent
140       || (S_ISDIR(node->st.st_mode) && !(toys.optflags & FLAG_s)))
141   {
142     print(node->extra*512, node);
143   }
144 
145   return 0;
146 }
147 
du_main(void)148 void du_main(void)
149 {
150   char *noargs[] = {".", 0}, **args;
151 
152   // Loop over command line arguments, recursing through children
153   for (args = toys.optc ? toys.optargs : noargs; *args; args++)
154     dirtree_handle_callback(dirtree_start(*args, toys.optflags&(FLAG_H|FLAG_L)),
155       do_du);
156   if (toys.optflags & FLAG_c) print(TT.total*512, 0);
157 
158   if (CFG_TOYBOX_FREE) seen_inode(TT.inodes, 0);
159 }
160