du.c revision 1.32 1 /* $NetBSD: du.c,v 1.32 2008/07/21 14:19:22 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Newcomb.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
44 #else
45 __RCSID("$NetBSD: du.c,v 1.32 2008/07/21 14:19:22 lukem Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <sys/stat.h>
51
52 #include <dirent.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <fts.h>
56 #include <util.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <limits.h>
62
63 int linkchk(dev_t, ino_t);
64 void prstat(const char *, int64_t);
65 void usage(void);
66
67 int hflag;
68 long blocksize;
69
70 int
71 main(int argc, char *argv[])
72 {
73 FTS *fts;
74 FTSENT *p;
75 int64_t totalblocks;
76 int ftsoptions, listdirs, listfiles;
77 int depth;
78 int Hflag, Lflag, aflag, ch, cflag, dflag, gkmflag, nflag, rval, sflag;
79 const char *noargv[2];
80
81 Hflag = Lflag = aflag = cflag = dflag = gkmflag = nflag = sflag = 0;
82 totalblocks = 0;
83 ftsoptions = FTS_PHYSICAL;
84 depth = INT_MAX;
85 while ((ch = getopt(argc, argv, "HLPacd:ghkmnrsx")) != -1)
86 switch (ch) {
87 case 'H':
88 Hflag = 1;
89 Lflag = 0;
90 break;
91 case 'L':
92 Lflag = 1;
93 Hflag = 0;
94 break;
95 case 'P':
96 Hflag = Lflag = 0;
97 break;
98 case 'a':
99 aflag = 1;
100 break;
101 case 'c':
102 cflag = 1;
103 break;
104 case 'd':
105 dflag = 1;
106 depth = atoi(optarg);
107 if (depth < 0 || depth > SHRT_MAX) {
108 warnx("invalid argument to option d: %s",
109 optarg);
110 usage();
111 }
112 break;
113 case 'g':
114 blocksize = 1024 * 1024 * 1024;
115 gkmflag = 1;
116 break;
117 case 'h':
118 hflag = 1;
119 break;
120 case 'k':
121 blocksize = 1024;
122 gkmflag = 1;
123 break;
124 case 'm':
125 blocksize = 1024 * 1024;
126 gkmflag = 1;
127 break;
128 case 'n':
129 nflag = 1;
130 break;
131 case 'r':
132 break;
133 case 's':
134 sflag = 1;
135 break;
136 case 'x':
137 ftsoptions |= FTS_XDEV;
138 break;
139 case '?':
140 default:
141 usage();
142 }
143 argc -= optind;
144 argv += optind;
145
146 /*
147 * XXX
148 * Because of the way that fts(3) works, logical walks will not count
149 * the blocks actually used by symbolic links. We rationalize this by
150 * noting that users computing logical sizes are likely to do logical
151 * copies, so not counting the links is correct. The real reason is
152 * that we'd have to re-implement the kernel's symbolic link traversing
153 * algorithm to get this right. If, for example, you have relative
154 * symbolic links referencing other relative symbolic links, it gets
155 * very nasty, very fast. The bottom line is that it's documented in
156 * the man page, so it's a feature.
157 */
158 if (Hflag)
159 ftsoptions |= FTS_COMFOLLOW;
160 if (Lflag) {
161 ftsoptions &= ~FTS_PHYSICAL;
162 ftsoptions |= FTS_LOGICAL;
163 }
164
165 if (aflag) {
166 if (sflag || dflag)
167 usage();
168 listdirs = listfiles = 1;
169 } else if (sflag) {
170 if (dflag)
171 usage();
172 listdirs = listfiles = depth = 0;
173 } else {
174 listfiles = 0;
175 listdirs = 1;
176 }
177
178 if (!*argv) {
179 noargv[0] = ".";
180 noargv[1] = NULL;
181 argv = __UNCONST(noargv);
182 }
183
184 if (!gkmflag)
185 (void)getbsize(NULL, &blocksize);
186 blocksize /= 512;
187
188 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
189 err(1, "fts_open `%s'", *argv);
190
191 for (rval = 0; (p = fts_read(fts)) != NULL;) {
192 if (nflag) {
193 switch (p->fts_info) {
194 case FTS_NS:
195 case FTS_SLNONE:
196 /* nothing */
197 break;
198 default:
199 if (p->fts_statp->st_flags & UF_NODUMP) {
200 fts_set(fts, p, FTS_SKIP);
201 continue;
202 }
203 }
204 }
205 switch (p->fts_info) {
206 case FTS_D: /* Ignore. */
207 break;
208 case FTS_DP:
209 p->fts_parent->fts_number +=
210 p->fts_number += p->fts_statp->st_blocks;
211
212 if (p->fts_level > depth) {
213 fts_set(fts, p, FTS_SKIP);
214 continue;
215 }
216
217 if (cflag)
218 totalblocks += p->fts_statp->st_blocks;
219 /*
220 * If listing each directory, or not listing files
221 * or directories and this is post-order of the
222 * root of a traversal, display the total.
223 */
224 if (listdirs || (!listfiles && !p->fts_level))
225 prstat(p->fts_path, p->fts_number);
226 break;
227 case FTS_DC: /* Ignore. */
228 break;
229 case FTS_DNR: /* Warn, continue. */
230 case FTS_ERR:
231 case FTS_NS:
232 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
233 rval = 1;
234 break;
235 default:
236 if (p->fts_statp->st_nlink > 1 &&
237 linkchk(p->fts_statp->st_dev, p->fts_statp->st_ino))
238 break;
239 /*
240 * If listing each file, or a non-directory file was
241 * the root of a traversal, display the total.
242 */
243 if (listfiles || !p->fts_level)
244 prstat(p->fts_path, p->fts_statp->st_blocks);
245 p->fts_parent->fts_number += p->fts_statp->st_blocks;
246 if (cflag)
247 totalblocks += p->fts_statp->st_blocks;
248 }
249 }
250 if (errno)
251 err(1, "fts_read");
252 if (cflag)
253 prstat("total", totalblocks);
254 exit(rval);
255 }
256
257 void
258 prstat(const char *fname, int64_t blocks)
259 {
260 if (hflag) {
261 char buf[5];
262 int64_t sz = blocks * 512;
263
264 humanize_number(buf, sizeof(buf), sz, "", HN_AUTOSCALE,
265 HN_B | HN_NOSPACE | HN_DECIMAL);
266
267 (void)printf("%s\t%s\n", buf, fname);
268 } else
269 (void)printf("%lld\t%s\n",
270 (long long)howmany(blocks, (int64_t)blocksize),
271 fname);
272 }
273
274 int
275 linkchk(dev_t dev, ino_t ino)
276 {
277 static struct entry {
278 dev_t dev;
279 ino_t ino;
280 } *htable;
281 static int htshift; /* log(allocated size) */
282 static int htmask; /* allocated size - 1 */
283 static int htused; /* 2*number of insertions */
284 static int sawzero; /* Whether zero is in table or not */
285 int h, h2;
286 uint64_t tmp;
287 /* this constant is (1<<64)/((1+sqrt(5))/2)
288 * aka (word size)/(golden ratio)
289 */
290 const uint64_t HTCONST = 11400714819323198485ULL;
291 const int HTBITS = CHAR_BIT * sizeof(tmp);
292
293 /* Never store zero in hashtable */
294 if (dev == 0 && ino == 0) {
295 h = sawzero;
296 sawzero = 1;
297 return h;
298 }
299
300 /* Extend hash table if necessary, keep load under 0.5 */
301 if (htused<<1 >= htmask) {
302 struct entry *ohtable;
303
304 if (!htable)
305 htshift = 10; /* starting hashtable size */
306 else
307 htshift++; /* exponential hashtable growth */
308
309 htmask = (1 << htshift) - 1;
310 htused = 0;
311
312 ohtable = htable;
313 htable = calloc(htmask+1, sizeof(*htable));
314 if (!htable)
315 err(1, "calloc");
316
317 /* populate newly allocated hashtable */
318 if (ohtable) {
319 int i;
320 for (i = 0; i <= htmask>>1; i++)
321 if (ohtable[i].ino || ohtable[i].dev)
322 linkchk(ohtable[i].dev, ohtable[i].ino);
323 free(ohtable);
324 }
325 }
326
327 /* multiplicative hashing */
328 tmp = dev;
329 tmp <<= HTBITS>>1;
330 tmp |= ino;
331 tmp *= HTCONST;
332 h = tmp >> (HTBITS - htshift);
333 h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
334
335 /* open address hashtable search with double hash probing */
336 while (htable[h].ino || htable[h].dev) {
337 if ((htable[h].ino == ino) && (htable[h].dev == dev))
338 return 1;
339 h = (h + h2) & htmask;
340 }
341
342 /* Insert the current entry into hashtable */
343 htable[h].dev = dev;
344 htable[h].ino = ino;
345 htused++;
346 return 0;
347 }
348
349 void
350 usage(void)
351 {
352
353 (void)fprintf(stderr,
354 "usage: du [-H | -L | -P] [-a | -d depth | -s] [-cghkmnrx] [file ...]\n");
355 exit(1);
356 }
357