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