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