du.c revision 184656
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 184656 2008-11-04 19:23:48Z mlaier $");
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 <libutil.h>
60#include <locale.h>
61#include <stdint.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <sysexits.h>
66#include <unistd.h>
67
68SLIST_HEAD(ignhead, ignentry) ignores;
69struct ignentry {
70	char			*mask;
71	SLIST_ENTRY(ignentry)	next;
72};
73
74static int	linkchk(FTSENT *);
75static void	usage(void);
76static void	prthumanval(int64_t);
77static void	ignoreadd(const char *);
78static void	ignoreclean(void);
79static int	ignorep(FTSENT *);
80
81static int	nodumpflag = 0;
82
83int
84main(int argc, char *argv[])
85{
86	FTS		*fts;
87	FTSENT		*p;
88	off_t		savednumber;
89	long		blocksize;
90	int		ftsoptions;
91	int		listall;
92	int		depth;
93	int		Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag;
94	int		hflag, lflag, ch, notused, rval;
95	char 		**save;
96	static char	dot[] = ".";
97
98	setlocale(LC_ALL, "");
99
100	Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag =
101	    lflag = 0;
102
103	save = argv;
104	ftsoptions = 0;
105	savednumber = 0;
106	depth = INT_MAX;
107	SLIST_INIT(&ignores);
108
109	while ((ch = getopt(argc, argv, "HI:LPasd:chklmnrx")) != -1)
110		switch (ch) {
111		case 'H':
112			Hflag = 1;
113			break;
114		case 'I':
115			ignoreadd(optarg);
116			break;
117		case 'L':
118			if (Pflag)
119				usage();
120			Lflag = 1;
121			break;
122		case 'P':
123			if (Lflag)
124				usage();
125			Pflag = 1;
126			break;
127		case 'a':
128			aflag = 1;
129			break;
130		case 's':
131			sflag = 1;
132			break;
133		case 'd':
134			dflag = 1;
135			errno = 0;
136			depth = atoi(optarg);
137			if (errno == ERANGE || depth < 0) {
138				warnx("invalid argument to option d: %s",
139				    optarg);
140				usage();
141			}
142			break;
143		case 'c':
144			cflag = 1;
145			break;
146		case 'h':
147			if (setenv("BLOCKSIZE", "512", 1) == -1)
148				warn("setenv: cannot set BLOCKSIZE=512");
149			hflag = 1;
150			break;
151		case 'k':
152			hflag = 0;
153			if (setenv("BLOCKSIZE", "1024", 1) == -1)
154				warn("setenv: cannot set BLOCKSIZE=1024");
155			break;
156		case 'l':
157			lflag = 1;
158			break;
159		case 'm':
160			hflag = 0;
161			if (setenv("BLOCKSIZE", "1048576", 1) == -1)
162				warn("setenv: cannot set BLOCKSIZE=1048576");
163			break;
164		case 'n':
165			nodumpflag = 1;
166			break;
167		case 'r':		 /* Compatibility. */
168			break;
169		case 'x':
170			ftsoptions |= FTS_XDEV;
171			break;
172		case '?':
173		default:
174			usage();
175			/* NOTREACHED */
176		}
177
178	argc -= optind;
179	argv += optind;
180
181	/*
182	 * XXX
183	 * Because of the way that fts(3) works, logical walks will not count
184	 * the blocks actually used by symbolic links.  We rationalize this by
185	 * noting that users computing logical sizes are likely to do logical
186	 * copies, so not counting the links is correct.  The real reason is
187	 * that we'd have to re-implement the kernel's symbolic link traversing
188	 * algorithm to get this right.  If, for example, you have relative
189	 * symbolic links referencing other relative symbolic links, it gets
190	 * very nasty, very fast.  The bottom line is that it's documented in
191	 * the man page, so it's a feature.
192	 */
193
194	if (Hflag + Lflag + Pflag > 1)
195		usage();
196
197	if (Hflag + Lflag + Pflag == 0)
198		Pflag = 1;			/* -P (physical) is default */
199
200	if (Hflag)
201		ftsoptions |= FTS_COMFOLLOW;
202
203	if (Lflag)
204		ftsoptions |= FTS_LOGICAL;
205
206	if (Pflag)
207		ftsoptions |= FTS_PHYSICAL;
208
209	listall = 0;
210
211	if (aflag) {
212		if (sflag || dflag)
213			usage();
214		listall = 1;
215	} else if (sflag) {
216		if (dflag)
217			usage();
218		depth = 0;
219	}
220
221	if (!*argv) {
222		argv = save;
223		argv[0] = dot;
224		argv[1] = NULL;
225	}
226
227	(void)getbsize(&notused, &blocksize);
228	blocksize /= 512;
229
230	rval = 0;
231
232	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
233		err(1, "fts_open");
234
235	while ((p = fts_read(fts)) != NULL) {
236		switch (p->fts_info) {
237		case FTS_D:			/* Ignore. */
238			if (ignorep(p))
239				fts_set(fts, p, FTS_SKIP);
240			break;
241		case FTS_DP:
242			if (ignorep(p))
243				break;
244
245			p->fts_parent->fts_bignum += p->fts_bignum +=
246			    p->fts_statp->st_blocks;
247
248			if (p->fts_level <= depth) {
249				if (hflag) {
250					prthumanval(howmany(p->fts_bignum,
251					    blocksize));
252					(void)printf("\t%s\n", p->fts_path);
253				} else {
254					(void)printf("%jd\t%s\n",
255					    (intmax_t)howmany(p->fts_bignum,
256					    blocksize), p->fts_path);
257				}
258			}
259			break;
260		case FTS_DC:			/* Ignore. */
261			break;
262		case FTS_DNR:			/* Warn, continue. */
263		case FTS_ERR:
264		case FTS_NS:
265			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
266			rval = 1;
267			break;
268		default:
269			if (ignorep(p))
270				break;
271
272			if (lflag == 0 && p->fts_statp->st_nlink > 1 &&
273			    linkchk(p))
274				break;
275
276			if (listall || p->fts_level == 0) {
277				if (hflag) {
278					prthumanval(howmany(
279					    p->fts_statp->st_blocks,
280					    blocksize));
281					(void)printf("\t%s\n", p->fts_path);
282				} else {
283					(void)printf("%jd\t%s\n",
284					    (intmax_t)howmany(
285					    p->fts_statp->st_blocks,
286					    blocksize),	p->fts_path);
287				}
288			}
289
290			p->fts_parent->fts_bignum += p->fts_statp->st_blocks;
291		}
292		savednumber = p->fts_parent->fts_bignum;
293	}
294
295	if (errno)
296		err(1, "fts_read");
297
298	if (cflag) {
299		if (hflag) {
300			prthumanval(howmany(savednumber, blocksize));
301			(void)printf("\ttotal\n");
302		} else {
303			(void)printf("%jd\ttotal\n", (intmax_t)howmany(
304			    savednumber, blocksize));
305		}
306	}
307
308	ignoreclean();
309	exit(rval);
310}
311
312static int
313linkchk(FTSENT *p)
314{
315	struct links_entry {
316		struct links_entry *next;
317		struct links_entry *previous;
318		int	 links;
319		dev_t	 dev;
320		ino_t	 ino;
321	};
322	static const size_t links_hash_initial_size = 8192;
323	static struct links_entry **buckets;
324	static struct links_entry *free_list;
325	static size_t number_buckets;
326	static unsigned long number_entries;
327	static char stop_allocating;
328	struct links_entry *le, **new_buckets;
329	struct stat *st;
330	size_t i, new_size;
331	int hash;
332
333	st = p->fts_statp;
334
335	/* If necessary, initialize the hash table. */
336	if (buckets == NULL) {
337		number_buckets = links_hash_initial_size;
338		buckets = malloc(number_buckets * sizeof(buckets[0]));
339		if (buckets == NULL)
340			errx(1, "No memory for hardlink detection");
341		for (i = 0; i < number_buckets; i++)
342			buckets[i] = NULL;
343	}
344
345	/* If the hash table is getting too full, enlarge it. */
346	if (number_entries > number_buckets * 10 && !stop_allocating) {
347		new_size = number_buckets * 2;
348		new_buckets = malloc(new_size * sizeof(struct links_entry *));
349
350		/* Try releasing the free list to see if that helps. */
351		if (new_buckets == NULL && free_list != NULL) {
352			while (free_list != NULL) {
353				le = free_list;
354				free_list = le->next;
355				free(le);
356			}
357			new_buckets = malloc(new_size *
358			    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
446static void
447prthumanval(int64_t bytes)
448{
449	char buf[5];
450
451	bytes *= DEV_BSIZE;
452
453	humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
454	    HN_B | HN_NOSPACE | HN_DECIMAL);
455
456	(void)printf("%4s", buf);
457}
458
459static void
460usage(void)
461{
462	(void)fprintf(stderr,
463		"usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] "
464		"[-l] [-h | -k | -m] [-n] [-x] [-I mask] [file ...]\n");
465	exit(EX_USAGE);
466}
467
468static void
469ignoreadd(const char *mask)
470{
471	struct ignentry *ign;
472
473	ign = calloc(1, sizeof(*ign));
474	if (ign == NULL)
475		errx(1, "cannot allocate memory");
476	ign->mask = strdup(mask);
477	if (ign->mask == NULL)
478		errx(1, "cannot allocate memory");
479	SLIST_INSERT_HEAD(&ignores, ign, next);
480}
481
482static void
483ignoreclean(void)
484{
485	struct ignentry *ign;
486
487	while (!SLIST_EMPTY(&ignores)) {
488		ign = SLIST_FIRST(&ignores);
489		SLIST_REMOVE_HEAD(&ignores, next);
490		free(ign->mask);
491		free(ign);
492	}
493}
494
495static int
496ignorep(FTSENT *ent)
497{
498	struct ignentry *ign;
499
500	if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
501		return 1;
502	SLIST_FOREACH(ign, &ignores, next)
503		if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
504			return 1;
505	return 0;
506}
507