du.c revision 128772
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 128772 2004-04-30 18:17:51Z 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	static const size_t links_hash_initial_size = 8192;
314	struct links_entry {
315		struct links_entry	*next;
316		struct links_entry	*previous;
317		int			 links;
318		dev_t			 dev;
319		ino_t			 ino;
320	};
321	static unsigned long		  number_entries;
322	static size_t			  number_buckets;
323	static struct links_entry	**buckets;
324	static struct links_entry	*free_list;
325	static char			 stop_allocating;
326
327	struct links_entry	*le, **new_buckets;
328	struct stat		*st;
329	int			 hash;
330	size_t			 i, new_size;
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			err(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		int count;
347
348		new_size = number_buckets * 2;
349		new_buckets = malloc(new_size * sizeof(struct links_entry *));
350		count = 0;
351
352		/* Try releasing the free list to see if that helps. */
353		if (new_buckets == NULL && free_list != NULL) {
354			while (free_list != NULL) {
355				le = free_list;
356				free_list = le->next;
357				free(le);
358			}
359			new_buckets = malloc(new_size * sizeof(new_buckets[0]));
360		}
361
362		if (new_buckets == NULL) {
363			stop_allocating = 1;
364			warnc(ENOMEM, "No more memory for recording "
365			    "hard links; Remaining hard links will be "
366			    "counted as separate files.");
367		} else {
368			memset(new_buckets, 0,
369			    new_size * sizeof(struct links_entry *));
370			for (i = 0; i < number_buckets; i++) {
371				while (buckets[i] != NULL) {
372					/* Remove entry from old bucket. */
373					le = buckets[i];
374					buckets[i] = le->next;
375
376					/* Add entry to new bucket. */
377					hash = (le->dev ^ le->ino) % new_size;
378
379					if (new_buckets[hash] != NULL)
380						new_buckets[hash]->previous =
381						    le;
382					le->next = new_buckets[hash];
383					le->previous = NULL;
384					new_buckets[hash] = le;
385				}
386			}
387			free(buckets);
388			buckets = new_buckets;
389			number_buckets = new_size;
390		}
391	}
392
393	/* Try to locate this entry in the hash table. */
394	hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
395	for (le = buckets[hash]; le != NULL; le = le->next) {
396		if (le->dev == st->st_dev && le->ino == st->st_ino) {
397			/*
398			 * Save memory by releasing an entry when we've seen
399			 * all of it's links.
400			 */
401			if (--le->links <= 0) {
402				if (le->previous != NULL)
403					le->previous->next = le->next;
404				if (le->next != NULL)
405					le->next->previous = le->previous;
406				if (buckets[hash] == le)
407					buckets[hash] = le->next;
408				number_entries--;
409				/* Recycle this node through the free list */
410				if (stop_allocating) {
411					free(le);
412				} else {
413					le->next = free_list;
414					free_list = le;
415				}
416			}
417			return (1);
418		}
419	}
420
421	if (stop_allocating)
422		return (0);
423
424	/* Add this entry to the links cache. */
425	if (free_list != NULL) {
426		/* Pull a node from the free list if we can. */
427		le = free_list;
428		free_list = le->next;
429	} else
430		/* Malloc one if we have to. */
431		le = malloc(sizeof(struct links_entry));
432	if (le == NULL) {
433		stop_allocating = 1;
434		warnc(ENOMEM, "No more memory for recording "
435		    "hard links; Remaining hard links will be counted "
436		    "as separate files.");
437		return (0);
438	}
439	le->dev = st->st_dev;
440	le->ino = st->st_ino;
441	le->links = st->st_nlink - 1;
442	number_entries++;
443	le->next = buckets[hash];
444	le->previous = NULL;
445	if (buckets[hash] != NULL)
446		buckets[hash]->previous = le;
447	buckets[hash] = le;
448	return (0);
449}
450
451/*
452 * Output in "human-readable" format.  Uses 3 digits max and puts
453 * unit suffixes at the end.  Makes output compact and easy to read,
454 * especially on huge disks.
455 *
456 */
457unit_t
458unit_adjust(double *val)
459{
460	double abval;
461	unit_t unit;
462	unsigned int unit_sz;
463
464	abval = fabs(*val);
465
466	unit_sz = abval ? ilogb(abval) / 10 : 0;
467
468	if (unit_sz >= UNIT_MAX) {
469		unit = NONE;
470	} else {
471		unit = unitp[unit_sz];
472		*val /= (double)valp[unit_sz];
473	}
474
475	return (unit);
476}
477
478void
479prthumanval(double bytes)
480{
481	unit_t unit;
482
483	bytes *= 512;
484	unit = unit_adjust(&bytes);
485
486	if (bytes == 0)
487		(void)printf("  0B");
488	else if (bytes > 10)
489		(void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
490	else
491		(void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
492}
493
494static void
495usage(void)
496{
497	(void)fprintf(stderr,
498		"usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
499	exit(EX_USAGE);
500}
501
502void
503ignoreadd(const char *mask)
504{
505	struct ignentry *ign;
506
507	ign = calloc(1, sizeof(*ign));
508	if (ign == NULL)
509		errx(1, "cannot allocate memory");
510	ign->mask = strdup(mask);
511	if (ign->mask == NULL)
512		errx(1, "cannot allocate memory");
513	SLIST_INSERT_HEAD(&ignores, ign, next);
514}
515
516void
517ignoreclean(void)
518{
519	struct ignentry *ign;
520
521	while (!SLIST_EMPTY(&ignores)) {
522		ign = SLIST_FIRST(&ignores);
523		SLIST_REMOVE_HEAD(&ignores, next);
524		free(ign->mask);
525		free(ign);
526	}
527}
528
529int
530ignorep(FTSENT *ent)
531{
532	struct ignentry *ign;
533
534	SLIST_FOREACH(ign, &ignores, next)
535		if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
536			return 1;
537	return 0;
538}
539