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