3
/* NetBSD: du.c,v 1.33 2008/07/30 22:03:40 dsl Exp */
6
* Copyright (c) 1989, 1993, 1994
7
* The Regents of the University of California. All rights reserved.
9
* This code is derived from software contributed to Berkeley by
12
* Redistribution and use in source and binary forms, with or without
13
* modification, are permitted provided that the following conditions
15
* 1. Redistributions of source code must retain the above copyright
16
* notice, this list of conditions and the following disclaimer.
17
* 2. Redistributions in binary form must reproduce the above copyright
18
* notice, this list of conditions and the following disclaimer in the
19
* documentation and/or other materials provided with the distribution.
20
* 3. Neither the name of the University nor the names of its contributors
21
* may be used to endorse or promote products derived from this software
22
* without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37
#include <sys/cdefs.h>
39
__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
40
The Regents of the University of California. All rights reserved.");
45
static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
47
__RCSID("$NetBSD: du.c,v 1.33 2008/07/30 22:03:40 dsl Exp $");
51
#include <sys/types.h>
68
#include <rump/ukfs.h>
70
#include <fts2fsufts.h>
71
#include <fsu_mount.h>
76
int linkchk(dev_t, ino_t);
77
void prstat(const char *, int64_t);
84
main(int argc, char *argv[])
89
int ftsoptions, listfiles;
91
int Hflag, Lflag, aflag, ch, cflag, dflag, gkmflag, nflag, rval, sflag;
92
const char *noargv[2];
97
FSU_MOUNT(argc, argv, ukfs);
100
Hflag = Lflag = aflag = cflag = dflag = gkmflag = nflag = sflag = 0;
102
ftsoptions = FTS_PHYSICAL;
104
while ((ch = getopt(argc, argv, "HLPacd:ghkmnrsx")) != -1)
125
depth = atoi(optarg);
126
if (depth < 0 || depth > SHRT_MAX) {
127
warnx("invalid argument to option d: %s",
133
blocksize = 1024 * 1024 * 1024;
144
blocksize = 1024 * 1024;
156
ftsoptions |= FTS_XDEV;
167
* Because of the way that fts(3) works, logical walks will not count
168
* the blocks actually used by symbolic links. We rationalize this by
169
* noting that users computing logical sizes are likely to do logical
170
* copies, so not counting the links is correct. The real reason is
171
* that we'd have to re-implement the kernel's symbolic link traversing
172
* algorithm to get this right. If, for example, you have relative
173
* symbolic links referencing other relative symbolic links, it gets
174
* very nasty, very fast. The bottom line is that it's documented in
175
* the man page, so it's a feature.
178
ftsoptions |= FTS_COMFOLLOW;
180
ftsoptions &= ~FTS_PHYSICAL;
181
ftsoptions |= FTS_LOGICAL;
198
argv = __UNCONST(noargv);
202
(void)getbsize(NULL, &blocksize);
205
if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
206
err(1, "fts_open `%s'", *argv);
208
for (rval = 0; (p = fts_read(fts)) != NULL;) {
210
switch (p->fts_info) {
216
if (p->fts_statp->st_flags & UF_NODUMP) {
217
fts_set(fts, p, FTS_SKIP);
222
switch (p->fts_info) {
223
case FTS_D: /* Ignore. */
226
p->fts_parent->fts_number +=
227
p->fts_number += p->fts_statp->st_blocks;
229
totalblocks += p->fts_statp->st_blocks;
231
* If listing each directory, or not listing files
232
* or directories and this is post-order of the
233
* root of a traversal, display the total.
235
if (p->fts_level <= depth
236
|| (!listfiles && !p->fts_level))
237
prstat(p->fts_path, p->fts_number);
239
case FTS_DC: /* Ignore. */
241
case FTS_DNR: /* Warn, continue. */
244
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
248
if (p->fts_statp->st_nlink > 1 &&
249
linkchk(p->fts_statp->st_dev, p->fts_statp->st_ino))
252
* If listing each file, or a non-directory file was
253
* the root of a traversal, display the total.
255
if (listfiles || !p->fts_level)
256
prstat(p->fts_path, p->fts_statp->st_blocks);
257
p->fts_parent->fts_number += p->fts_statp->st_blocks;
259
totalblocks += p->fts_statp->st_blocks;
265
prstat("total", totalblocks);
270
prstat(const char *fname, int64_t blocks)
274
int64_t sz = blocks * 512;
276
humanize_number(buf, sizeof(buf), sz, "", HN_AUTOSCALE,
277
HN_B | HN_NOSPACE | HN_DECIMAL);
279
(void)printf("%s\t%s\n", buf, fname);
281
(void)printf("%lld\t%s\n",
282
(long long)howmany(blocks, (int64_t)blocksize),
287
linkchk(dev_t dev, ino_t ino)
289
static struct entry {
293
static int htshift; /* log(allocated size) */
294
static int htmask; /* allocated size - 1 */
295
static int htused; /* 2*number of insertions */
296
static int sawzero; /* Whether zero is in table or not */
299
/* this constant is (1<<64)/((1+sqrt(5))/2)
300
* aka (word size)/(golden ratio)
302
const uint64_t HTCONST = 11400714819323198485ULL;
303
const int HTBITS = CHAR_BIT * sizeof(tmp);
305
/* Never store zero in hashtable */
306
if (dev == 0 && ino == 0) {
312
/* Extend hash table if necessary, keep load under 0.5 */
313
if (htused<<1 >= htmask) {
314
struct entry *ohtable;
317
htshift = 10; /* starting hashtable size */
319
htshift++; /* exponential hashtable growth */
321
htmask = (1 << htshift) - 1;
325
htable = calloc(htmask+1, sizeof(*htable));
329
/* populate newly allocated hashtable */
332
for (i = 0; i <= htmask>>1; i++)
333
if (ohtable[i].ino || ohtable[i].dev)
334
linkchk(ohtable[i].dev, ohtable[i].ino);
339
/* multiplicative hashing */
344
h = tmp >> (HTBITS - htshift);
345
h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
347
/* open address hashtable search with double hash probing */
348
while (htable[h].ino || htable[h].dev) {
349
if ((htable[h].ino == ino) && (htable[h].dev == dev))
351
h = (h + h2) & htmask;
354
/* Insert the current entry into hashtable */
365
(void)fprintf(stderr,
366
"usage: %s %s [-H | -L | -P] [-a | -d depth | -s] [-cghkmnrx] [file ...]\n",
367
getprogname(), fsu_mount_usage());
369
(void)fprintf(stderr,
370
"usage: %s [-H | -L | -P] [-a | -d depth | -s] [-cghkmnrx] [file ...]\n",