du.c revision 128806
1/*
2 * Copyright (c) 1989, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Newcomb.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. 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.
23 *
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
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static const char copyright[] =
39"@(#) Copyright (c) 1989, 1993, 1994\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static const char sccsid[] = "@(#)du.c	8.5 (Berkeley) 5/4/95";
46#endif
47#endif /* not lint */
48#include <sys/cdefs.h>
49__FBSDID("$FreeBSD: head/usr.bin/du/du.c 128806 2004-05-01 21:47:31Z kientzle $");
50
51#include <sys/param.h>
52#include <sys/queue.h>
53#include <sys/stat.h>
54
55#include <err.h>
56#include <errno.h>
57#include <fnmatch.h>
58#include <fts.h>
59#include <math.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63#include <sysexits.h>
64#include <unistd.h>
65
66#define	KILO_SZ(n) (n)
67#define	MEGA_SZ(n) ((n) * (n))
68#define	GIGA_SZ(n) ((n) * (n) * (n))
69#define	TERA_SZ(n) ((n) * (n) * (n) * (n))
70#define	PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
71
72#define	KILO_2_SZ (KILO_SZ(1024ULL))
73#define	MEGA_2_SZ (MEGA_SZ(1024ULL))
74#define	GIGA_2_SZ (GIGA_SZ(1024ULL))
75#define	TERA_2_SZ (TERA_SZ(1024ULL))
76#define	PETA_2_SZ (PETA_SZ(1024ULL))
77
78#define	KILO_SI_SZ (KILO_SZ(1000ULL))
79#define	MEGA_SI_SZ (MEGA_SZ(1000ULL))
80#define	GIGA_SI_SZ (GIGA_SZ(1000ULL))
81#define	TERA_SI_SZ (TERA_SZ(1000ULL))
82#define	PETA_SI_SZ (PETA_SZ(1000ULL))
83
84unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
85unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
86unsigned long long *valp;
87
88typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
89
90int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
91
92SLIST_HEAD(ignhead, ignentry) ignores;
93struct ignentry {
94	char			*mask;
95	SLIST_ENTRY(ignentry)	next;
96};
97
98static int	linkchk(FTSENT *);
99static void	usage(void);
100void		prthumanval(double);
101unit_t		unit_adjust(double *);
102void		ignoreadd(const char *);
103void		ignoreclean(void);
104int		ignorep(FTSENT *);
105
106int
107main(int argc, char *argv[])
108{
109	FTS		*fts;
110	FTSENT		*p;
111	long		blocksize, savednumber = 0;
112	int		ftsoptions;
113	int		listall;
114	int		depth;
115	int		Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
116	char 		**save;
117	static char	dot[] = ".";
118
119	Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
120
121	save = argv;
122	ftsoptions = 0;
123	depth = INT_MAX;
124	SLIST_INIT(&ignores);
125
126	while ((ch = getopt(argc, argv, "HI:LPasd:chkrx")) != -1)
127		switch (ch) {
128			case 'H':
129				Hflag = 1;
130				break;
131			case 'I':
132				ignoreadd(optarg);
133				break;
134			case 'L':
135				if (Pflag)
136					usage();
137				Lflag = 1;
138				break;
139			case 'P':
140				if (Lflag)
141					usage();
142				Pflag = 1;
143				break;
144			case 'a':
145				aflag = 1;
146				break;
147			case 's':
148				sflag = 1;
149				break;
150			case 'd':
151				dflag = 1;
152				errno = 0;
153				depth = atoi(optarg);
154				if (errno == ERANGE || depth < 0) {
155					warnx("invalid argument to option d: %s", optarg);
156					usage();
157				}
158				break;
159			case 'c':
160				cflag = 1;
161				break;
162			case 'h':
163				putenv("BLOCKSIZE=512");
164				hflag = 1;
165				valp = vals_base2;
166				break;
167			case 'k':
168				hflag = 0;
169				putenv("BLOCKSIZE=1024");
170				break;
171			case 'r':		 /* Compatibility. */
172				break;
173			case 'x':
174				ftsoptions |= FTS_XDEV;
175				break;
176			case '?':
177			default:
178				usage();
179		}
180
181	argc -= optind;
182	argv += optind;
183
184	/*
185	 * XXX
186	 * Because of the way that fts(3) works, logical walks will not count
187	 * the blocks actually used by symbolic links.  We rationalize this by
188	 * noting that users computing logical sizes are likely to do logical
189	 * copies, so not counting the links is correct.  The real reason is
190	 * that we'd have to re-implement the kernel's symbolic link traversing
191	 * algorithm to get this right.  If, for example, you have relative
192	 * symbolic links referencing other relative symbolic links, it gets
193	 * very nasty, very fast.  The bottom line is that it's documented in
194	 * the man page, so it's a feature.
195	 */
196
197	if (Hflag + Lflag + Pflag > 1)
198		usage();
199
200	if (Hflag + Lflag + Pflag == 0)
201		Pflag = 1;			/* -P (physical) is default */
202
203	if (Hflag)
204		ftsoptions |= FTS_COMFOLLOW;
205
206	if (Lflag)
207		ftsoptions |= FTS_LOGICAL;
208
209	if (Pflag)
210		ftsoptions |= FTS_PHYSICAL;
211
212	listall = 0;
213
214	if (aflag) {
215		if (sflag || dflag)
216			usage();
217		listall = 1;
218	} else if (sflag) {
219		if (dflag)
220			usage();
221		depth = 0;
222	}
223
224	if (!*argv) {
225		argv = save;
226		argv[0] = dot;
227		argv[1] = NULL;
228	}
229
230	(void) getbsize(&notused, &blocksize);
231	blocksize /= 512;
232
233	rval = 0;
234
235	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
236		err(1, "fts_open");
237
238	while ((p = fts_read(fts)) != NULL) {
239		switch (p->fts_info) {
240			case FTS_D:			/* Ignore. */
241				if (ignorep(p))
242					fts_set(fts, p, FTS_SKIP);
243				break;
244			case FTS_DP:
245				if (ignorep(p))
246					break;
247
248				p->fts_parent->fts_number +=
249				    p->fts_number += p->fts_statp->st_blocks;
250
251				if (p->fts_level <= depth) {
252					if (hflag) {
253						(void) prthumanval(howmany(p->fts_number, blocksize));
254						(void) printf("\t%s\n", p->fts_path);
255					} else {
256					(void) printf("%ld\t%s\n",
257					    howmany(p->fts_number, blocksize),
258					    p->fts_path);
259					}
260				}
261				break;
262			case FTS_DC:			/* Ignore. */
263				break;
264			case FTS_DNR:			/* Warn, continue. */
265			case FTS_ERR:
266			case FTS_NS:
267				warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
268				rval = 1;
269				break;
270			default:
271				if (ignorep(p))
272					break;
273
274				if (p->fts_statp->st_nlink > 1 && linkchk(p))
275					break;
276
277				if (listall || p->fts_level == 0) {
278					if (hflag) {
279						(void) prthumanval(howmany(p->fts_statp->st_blocks,
280							blocksize));
281						(void) printf("\t%s\n", p->fts_path);
282					} else {
283						(void) printf("%qd\t%s\n",
284							(long long)howmany(p->fts_statp->st_blocks, blocksize),
285							p->fts_path);
286					}
287				}
288
289				p->fts_parent->fts_number += p->fts_statp->st_blocks;
290		}
291		savednumber = p->fts_parent->fts_number;
292	}
293
294	if (errno)
295		err(1, "fts_read");
296
297	if (cflag) {
298		if (hflag) {
299			(void) prthumanval(howmany(savednumber, blocksize));
300			(void) printf("\ttotal\n");
301		} else {
302			(void) printf("%ld\ttotal\n", howmany(savednumber, blocksize));
303		}
304	}
305
306	ignoreclean();
307	exit(rval);
308}
309
310static int
311linkchk(FTSENT *p)
312{
313	struct links_entry {
314		struct links_entry *next;
315		struct links_entry *previous;
316		int	 links;
317		dev_t	 dev;
318		ino_t	 ino;
319	};
320	static const size_t links_hash_initial_size = 8192;
321	static struct links_entry **buckets;
322	static struct links_entry *free_list;
323	static size_t number_buckets;
324	static unsigned long number_entries;
325	static char stop_allocating;
326	struct links_entry *le, **new_buckets;
327	struct stat *st;
328	size_t i, new_size;
329	int count;
330	int hash;
331
332	st = p->fts_statp;
333
334	/* If necessary, initialize the hash table. */
335	if (buckets == NULL) {
336		number_buckets = links_hash_initial_size;
337		buckets = malloc(number_buckets * sizeof(buckets[0]));
338		if (buckets == NULL)
339			errx(1, "No memory for hardlink detection.");
340		for (i = 0; i < number_buckets; i++)
341			buckets[i] = NULL;
342	}
343
344	/* If the hash table is getting too full, enlarge it. */
345	if (number_entries > number_buckets * 10 && !stop_allocating) {
346
347		new_size = number_buckets * 2;
348		new_buckets = malloc(new_size * sizeof(struct links_entry *));
349		count = 0;
350
351		/* Try releasing the free list to see if that helps. */
352		if (new_buckets == NULL && free_list != NULL) {
353			while (free_list != NULL) {
354				le = free_list;
355				free_list = le->next;
356				free(le);
357			}
358			new_buckets = malloc(new_size * sizeof(new_buckets[0]));
359		}
360
361		if (new_buckets == NULL) {
362			stop_allocating = 1;
363			warnx("No more memory for tracking hard links.");
364		} else {
365			memset(new_buckets, 0,
366			    new_size * sizeof(struct links_entry *));
367			for (i = 0; i < number_buckets; i++) {
368				while (buckets[i] != NULL) {
369					/* Remove entry from old bucket. */
370					le = buckets[i];
371					buckets[i] = le->next;
372
373					/* Add entry to new bucket. */
374					hash = (le->dev ^ le->ino) % new_size;
375
376					if (new_buckets[hash] != NULL)
377						new_buckets[hash]->previous =
378						    le;
379					le->next = new_buckets[hash];
380					le->previous = NULL;
381					new_buckets[hash] = le;
382				}
383			}
384			free(buckets);
385			buckets = new_buckets;
386			number_buckets = new_size;
387		}
388	}
389
390	/* Try to locate this entry in the hash table. */
391	hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
392	for (le = buckets[hash]; le != NULL; le = le->next) {
393		if (le->dev == st->st_dev && le->ino == st->st_ino) {
394			/*
395			 * Save memory by releasing an entry when we've seen
396			 * all of it's links.
397			 */
398			if (--le->links <= 0) {
399				if (le->previous != NULL)
400					le->previous->next = le->next;
401				if (le->next != NULL)
402					le->next->previous = le->previous;
403				if (buckets[hash] == le)
404					buckets[hash] = le->next;
405				number_entries--;
406				/* Recycle this node through the free list */
407				if (stop_allocating) {
408					free(le);
409				} else {
410					le->next = free_list;
411					free_list = le;
412				}
413			}
414			return (1);
415		}
416	}
417
418	if (stop_allocating)
419		return (0);
420
421	/* Add this entry to the links cache. */
422	if (free_list != NULL) {
423		/* Pull a node from the free list if we can. */
424		le = free_list;
425		free_list = le->next;
426	} else
427		/* Malloc one if we have to. */
428		le = malloc(sizeof(struct links_entry));
429	if (le == NULL) {
430		stop_allocating = 1;
431		warnx("No more memory for tracking hard links.");
432		return (0);
433	}
434	le->dev = st->st_dev;
435	le->ino = st->st_ino;
436	le->links = st->st_nlink - 1;
437	number_entries++;
438	le->next = buckets[hash];
439	le->previous = NULL;
440	if (buckets[hash] != NULL)
441		buckets[hash]->previous = le;
442	buckets[hash] = le;
443	return (0);
444}
445
446/*
447 * Output in "human-readable" format.  Uses 3 digits max and puts
448 * unit suffixes at the end.  Makes output compact and easy to read,
449 * especially on huge disks.
450 *
451 */
452unit_t
453unit_adjust(double *val)
454{
455	double abval;
456	unit_t unit;
457	unsigned int unit_sz;
458
459	abval = fabs(*val);
460
461	unit_sz = abval ? ilogb(abval) / 10 : 0;
462
463	if (unit_sz >= UNIT_MAX) {
464		unit = NONE;
465	} else {
466		unit = unitp[unit_sz];
467		*val /= (double)valp[unit_sz];
468	}
469
470	return (unit);
471}
472
473void
474prthumanval(double bytes)
475{
476	unit_t unit;
477
478	bytes *= 512;
479	unit = unit_adjust(&bytes);
480
481	if (bytes == 0)
482		(void)printf("  0B");
483	else if (bytes > 10)
484		(void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
485	else
486		(void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
487}
488
489static void
490usage(void)
491{
492	(void)fprintf(stderr,
493		"usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
494	exit(EX_USAGE);
495}
496
497void
498ignoreadd(const char *mask)
499{
500	struct ignentry *ign;
501
502	ign = calloc(1, sizeof(*ign));
503	if (ign == NULL)
504		errx(1, "cannot allocate memory");
505	ign->mask = strdup(mask);
506	if (ign->mask == NULL)
507		errx(1, "cannot allocate memory");
508	SLIST_INSERT_HEAD(&ignores, ign, next);
509}
510
511void
512ignoreclean(void)
513{
514	struct ignentry *ign;
515
516	while (!SLIST_EMPTY(&ignores)) {
517		ign = SLIST_FIRST(&ignores);
518		SLIST_REMOVE_HEAD(&ignores, next);
519		free(ign->mask);
520		free(ign);
521	}
522}
523
524int
525ignorep(FTSENT *ent)
526{
527	struct ignentry *ign;
528
529	SLIST_FOREACH(ign, &ignores, next)
530		if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
531			return 1;
532	return 0;
533}
534