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