1 /* du.c - disk usage program.
3 * Copyright 2012 Ashwini Kumar <ak.ashwini@gmail.com>
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/du.html
9 USE_DU(NEWTOY(du, "d#<0hmlcaHkKLsx[-HL][-kKmh]", TOYFLAG_USR|TOYFLAG_BIN))
15 usage: du [-d N] [-askxHLlmc] [file...]
17 Show disk usage, space consumed by files and directories.
20 -k 1024 byte blocks (default)
21 -K 512 byte blocks (posix)
23 -h human readable format (e.g., 1K 243M 2G )
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
33 -l disable hardlink filter
47 typedef struct node_size {
52 // Print the size and name, given size in bytes
53 static void print(long long size, struct dirtree *node)
57 if (TT.maxdepth && TT.depth > TT.maxdepth) return;
59 if (toys.optflags & FLAG_h) {
60 human_readable(toybuf, size);
65 if (toys.optflags & FLAG_K) bits = 9;
66 else if (toys.optflags & FLAG_m) bits = 20;
68 printf("%llu", (size>>bits)+!!(size&((1<<bits)-1)));
70 if (node) name = dirtree_path(node, NULL);
71 xprintf("\t%s\n", name);
75 // Return whether or not we've seen this inode+dev, adding it to the list if
77 static int seen_inode(void **list, struct stat *st)
79 if (!st) llist_traverse(st, free);
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) {
87 struct inode_list *next;
92 for (new = *list; new; new = new->next)
93 if(new->ino == st->st_ino && new->dev == st->st_dev)
96 new = xzalloc(sizeof(*new));
97 new->ino = st->st_ino;
98 new->dev = st->st_dev;
106 // dirtree callback, comput/display size of node
107 static int do_du(struct dirtree *node)
109 if (!node->parent) TT.st_dev = node->st.st_dev;
110 else if (!dirtree_notdotdot(node)) return 0;
112 // detect swiching filesystems
113 if ((toys.optflags & FLAG_x) && (TT.st_dev != node->st.st_dev))
116 // Don't loop endlessly on recursive directory symlink
117 if (toys.optflags & FLAG_L) {
118 struct dirtree *try = node;
120 while ((try = try->parent))
121 if (node->st.st_dev==try->st.st_dev && node->st.st_ino==try->st.st_ino)
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;
129 // Collect child info before printing directory size
130 if (S_ISDIR(node->st.st_mode)) {
133 return DIRTREE_COMEAGAIN|(DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L));
137 node->extra += node->st.st_blocks;
138 if (node->parent) node->parent->extra += node->extra;
139 else TT.total += node->extra;
141 if ((toys.optflags & FLAG_a) || !node->parent
142 || (S_ISDIR(node->st.st_mode) && !(toys.optflags & FLAG_s)))
144 print(node->extra*512, node);
152 char *noargs[] = {".", 0}, **args;
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)),
158 if (toys.optflags & FLAG_c) print(TT.total*512, 0);
160 if (CFG_TOYBOX_FREE) seen_inode(TT.inodes, 0);