du.c revision 1.26 1 /* $NetBSD: du.c,v 1.26 2005/02/17 17:40:59 xtraeme 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\n\
38 The Regents of the University of California. All rights reserved.\n");
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.26 2005/02/17 17:40:59 xtraeme 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 Hflag, Lflag, Pflag, aflag, ch, cflag, gkmflag, nflag, rval, sflag;
78 const char *noargv[2];
79
80 Hflag = Lflag = Pflag = aflag = cflag = gkmflag = nflag = sflag = 0;
81 totalblocks = 0;
82 ftsoptions = FTS_PHYSICAL;
83 while ((ch = getopt(argc, argv, "HLPacghkmnrsx")) != -1)
84 switch (ch) {
85 case 'H':
86 Hflag = 1;
87 Lflag = Pflag = 0;
88 break;
89 case 'L':
90 Lflag = 1;
91 Hflag = Pflag = 0;
92 break;
93 case 'P':
94 Pflag = 1;
95 Hflag = Lflag = 0;
96 break;
97 case 'a':
98 aflag = 1;
99 break;
100 case 'c':
101 cflag = 1;
102 break;
103 case 'g':
104 blocksize = 1024 * 1024 * 1024;
105 gkmflag = 1;
106 break;
107 case 'h':
108 hflag = 1;
109 break;
110 case 'k':
111 blocksize = 1024;
112 gkmflag = 1;
113 break;
114 case 'm':
115 blocksize = 1024 * 1024;
116 gkmflag = 1;
117 break;
118 case 'n':
119 nflag = 1;
120 break;
121 case 'r':
122 break;
123 case 's':
124 sflag = 1;
125 break;
126 case 'x':
127 ftsoptions |= FTS_XDEV;
128 break;
129 case '?':
130 default:
131 usage();
132 }
133 argc -= optind;
134 argv += optind;
135
136 /*
137 * XXX
138 * Because of the way that fts(3) works, logical walks will not count
139 * the blocks actually used by symbolic links. We rationalize this by
140 * noting that users computing logical sizes are likely to do logical
141 * copies, so not counting the links is correct. The real reason is
142 * that we'd have to re-implement the kernel's symbolic link traversing
143 * algorithm to get this right. If, for example, you have relative
144 * symbolic links referencing other relative symbolic links, it gets
145 * very nasty, very fast. The bottom line is that it's documented in
146 * the man page, so it's a feature.
147 */
148 if (Hflag)
149 ftsoptions |= FTS_COMFOLLOW;
150 if (Lflag) {
151 ftsoptions &= ~FTS_PHYSICAL;
152 ftsoptions |= FTS_LOGICAL;
153 }
154
155 if (aflag) {
156 if (sflag)
157 usage();
158 listdirs = listfiles = 1;
159 } else if (sflag)
160 listdirs = listfiles = 0;
161 else {
162 listfiles = 0;
163 listdirs = 1;
164 }
165
166 if (!*argv) {
167 noargv[0] = ".";
168 noargv[1] = NULL;
169 (const char *)argv = noargv;
170 }
171
172 if (!gkmflag)
173 (void)getbsize(NULL, &blocksize);
174 blocksize /= 512;
175
176 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
177 err(1, "fts_open `%s'", *argv);
178
179 for (rval = 0; (p = fts_read(fts)) != NULL;) {
180 if (nflag) {
181 switch (p->fts_info) {
182 case FTS_NS:
183 case FTS_SLNONE:
184 /* nothing */
185 break;
186 default:
187 if (p->fts_statp->st_flags & UF_NODUMP) {
188 fts_set(fts, p, FTS_SKIP);
189 continue;
190 }
191 }
192 }
193 switch (p->fts_info) {
194 case FTS_D: /* Ignore. */
195 break;
196 case FTS_DP:
197 p->fts_parent->fts_number +=
198 p->fts_number += p->fts_statp->st_blocks;
199 if (cflag)
200 totalblocks += p->fts_statp->st_blocks;
201 /*
202 * If listing each directory, or not listing files
203 * or directories and this is post-order of the
204 * root of a traversal, display the total.
205 */
206 if (listdirs || (!listfiles && !p->fts_level))
207 prstat(p->fts_path, p->fts_number);
208 break;
209 case FTS_DC: /* Ignore. */
210 break;
211 case FTS_DNR: /* Warn, continue. */
212 case FTS_ERR:
213 case FTS_NS:
214 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
215 rval = 1;
216 break;
217 default:
218 if (p->fts_statp->st_nlink > 1 &&
219 linkchk(p->fts_statp->st_dev, p->fts_statp->st_ino))
220 break;
221 /*
222 * If listing each file, or a non-directory file was
223 * the root of a traversal, display the total.
224 */
225 if (listfiles || !p->fts_level)
226 prstat(p->fts_path, p->fts_statp->st_blocks);
227 p->fts_parent->fts_number += p->fts_statp->st_blocks;
228 if (cflag)
229 totalblocks += p->fts_statp->st_blocks;
230 }
231 }
232 if (errno)
233 err(1, "fts_read");
234 if (cflag)
235 prstat("total", totalblocks);
236 exit(rval);
237 }
238
239 void
240 prstat(const char *fname, int64_t blocks)
241 {
242 if (hflag) {
243 char buf[5];
244 int64_t sz = blocks * 512;
245
246 humanize_number(buf, sizeof(buf), sz, "", HN_AUTOSCALE,
247 HN_B | HN_NOSPACE | HN_DECIMAL);
248
249 (void)printf("%s\t%s\n", buf, fname);
250 } else
251 (void)printf("%lld\t%s\n",
252 (long long)howmany(blocks, (int64_t)blocksize),
253 fname);
254 }
255
256 int
257 linkchk(dev_t dev, ino_t ino)
258 {
259 static struct entry {
260 dev_t dev;
261 ino_t ino;
262 } *htable;
263 static int htshift; /* log(allocated size) */
264 static int htmask; /* allocated size - 1 */
265 static int htused; /* 2*number of insertions */
266 static int sawzero; /* Whether zero is in table or not */
267 int h, h2;
268 uint64_t tmp;
269 /* this constant is (1<<64)/((1+sqrt(5))/2)
270 * aka (word size)/(golden ratio)
271 */
272 const uint64_t HTCONST = 11400714819323198485ULL;
273 const int HTBITS = CHAR_BIT * sizeof(tmp);
274
275 /* Never store zero in hashtable */
276 if (dev == 0 && ino == 0) {
277 h = sawzero;
278 sawzero = 1;
279 return h;
280 }
281
282 /* Extend hash table if necessary, keep load under 0.5 */
283 if (htused<<1 >= htmask) {
284 struct entry *ohtable;
285
286 if (!htable)
287 htshift = 10; /* starting hashtable size */
288 else
289 htshift++; /* exponential hashtable growth */
290
291 htmask = (1 << htshift) - 1;
292 htused = 0;
293
294 ohtable = htable;
295 htable = calloc(htmask+1, sizeof(*htable));
296 if (!htable)
297 err(1, "calloc");
298
299 /* populate newly allocated hashtable */
300 if (ohtable) {
301 int i;
302 for (i = 0; i <= htmask>>1; i++)
303 if (ohtable[i].ino || ohtable[i].dev)
304 linkchk(ohtable[i].dev, ohtable[i].ino);
305 free(ohtable);
306 }
307 }
308
309 /* multiplicative hashing */
310 tmp = dev;
311 tmp <<= HTBITS>>1;
312 tmp |= ino;
313 tmp *= HTCONST;
314 h = tmp >> (HTBITS - htshift);
315 h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
316
317 /* open address hashtable search with double hash probing */
318 while (htable[h].ino || htable[h].dev) {
319 if ((htable[h].ino == ino) && (htable[h].dev == dev))
320 return 1;
321 h = (h + h2) & htmask;
322 }
323
324 /* Insert the current entry into hashtable */
325 htable[h].dev = dev;
326 htable[h].ino = ino;
327 htused++;
328 return 0;
329 }
330
331 void
332 usage(void)
333 {
334
335 (void)fprintf(stderr,
336 "usage: du [-H | -L | -P] [-a | -s] [-cghkmrx] [file ...]\n");
337 exit(1);
338 }
339