xref: /freebsd/usr.bin/du/du.c (revision cded07a8783dedbb0634ebde78764f935f26467d)
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
38 static 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
45 static 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$");
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 
68 SLIST_HEAD(ignhead, ignentry) ignores;
69 struct ignentry {
70 	char			*mask;
71 	SLIST_ENTRY(ignentry)	next;
72 };
73 
74 static int	linkchk(FTSENT *);
75 static void	usage(void);
76 static void	prthumanval(int64_t);
77 static void	ignoreadd(const char *);
78 static void	ignoreclean(void);
79 static int	ignorep(FTSENT *);
80 static void	siginfo(int __unused);
81 
82 static int	nodumpflag = 0;
83 static int	Aflag;
84 static long	blocksize, cblocksize;
85 static volatile sig_atomic_t info;
86 
87 int
88 main(int argc, char *argv[])
89 {
90 	FTS		*fts;
91 	FTSENT		*p;
92 	off_t		savednumber, curblocks;
93 	off_t		threshold, threshold_sign;
94 	int		ftsoptions;
95 	int		listall;
96 	int		depth;
97 	int		Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag;
98 	int		hflag, lflag, ch, notused, rval;
99 	char 		**save;
100 	static char	dot[] = ".";
101 
102 	setlocale(LC_ALL, "");
103 
104 	Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag =
105 	    lflag = Aflag = 0;
106 
107 	save = argv;
108 	ftsoptions = 0;
109 	savednumber = 0;
110 	threshold = 0;
111 	threshold_sign = 1;
112 	cblocksize = DEV_BSIZE;
113 	blocksize = 0;
114 	depth = INT_MAX;
115 	SLIST_INIT(&ignores);
116 
117 	while ((ch = getopt(argc, argv, "AB:HI:LPasd:chklmnrt:x")) != -1)
118 		switch (ch) {
119 		case 'A':
120 			Aflag = 1;
121 			break;
122 		case 'B':
123 			errno = 0;
124 			cblocksize = atoi(optarg);
125 			if (errno == ERANGE || cblocksize <= 0) {
126 				warnx("invalid argument to option B: %s",
127 				    optarg);
128 				usage();
129 			}
130 			break;
131 		case 'H':
132 			Hflag = 1;
133 			break;
134 		case 'I':
135 			ignoreadd(optarg);
136 			break;
137 		case 'L':
138 			if (Pflag)
139 				usage();
140 			Lflag = 1;
141 			break;
142 		case 'P':
143 			if (Lflag)
144 				usage();
145 			Pflag = 1;
146 			break;
147 		case 'a':
148 			aflag = 1;
149 			break;
150 		case 's':
151 			sflag = 1;
152 			break;
153 		case 'd':
154 			dflag = 1;
155 			errno = 0;
156 			depth = atoi(optarg);
157 			if (errno == ERANGE || depth < 0) {
158 				warnx("invalid argument to option d: %s",
159 				    optarg);
160 				usage();
161 			}
162 			break;
163 		case 'c':
164 			cflag = 1;
165 			break;
166 		case 'h':
167 			hflag = 1;
168 			break;
169 		case 'k':
170 			hflag = 0;
171 			blocksize = 1024;
172 			break;
173 		case 'l':
174 			lflag = 1;
175 			break;
176 		case 'm':
177 			hflag = 0;
178 			blocksize = 1048576;
179 			break;
180 		case 'n':
181 			nodumpflag = 1;
182 			break;
183 		case 'r':		 /* Compatibility. */
184 			break;
185 		case 't' :
186 			if (expand_number(optarg, &threshold) != 0 ||
187 			    threshold == 0) {
188 				warnx("invalid threshold: %s", optarg);
189 				usage();
190 			} else if (threshold < 0)
191 				threshold_sign = -1;
192 			break;
193 		case 'x':
194 			ftsoptions |= FTS_XDEV;
195 			break;
196 		case '?':
197 		default:
198 			usage();
199 			/* NOTREACHED */
200 		}
201 
202 	argc -= optind;
203 	argv += optind;
204 
205 	/*
206 	 * XXX
207 	 * Because of the way that fts(3) works, logical walks will not count
208 	 * the blocks actually used by symbolic links.  We rationalize this by
209 	 * noting that users computing logical sizes are likely to do logical
210 	 * copies, so not counting the links is correct.  The real reason is
211 	 * that we'd have to re-implement the kernel's symbolic link traversing
212 	 * algorithm to get this right.  If, for example, you have relative
213 	 * symbolic links referencing other relative symbolic links, it gets
214 	 * very nasty, very fast.  The bottom line is that it's documented in
215 	 * the man page, so it's a feature.
216 	 */
217 
218 	if (Hflag + Lflag + Pflag > 1)
219 		usage();
220 
221 	if (Hflag + Lflag + Pflag == 0)
222 		Pflag = 1;			/* -P (physical) is default */
223 
224 	if (Hflag)
225 		ftsoptions |= FTS_COMFOLLOW;
226 
227 	if (Lflag)
228 		ftsoptions |= FTS_LOGICAL;
229 
230 	if (Pflag)
231 		ftsoptions |= FTS_PHYSICAL;
232 
233 	if (!Aflag && (cblocksize % DEV_BSIZE) != 0)
234 		cblocksize = howmany(cblocksize, DEV_BSIZE) * DEV_BSIZE;
235 
236 	listall = 0;
237 
238 	if (aflag) {
239 		if (sflag || dflag)
240 			usage();
241 		listall = 1;
242 	} else if (sflag) {
243 		if (dflag)
244 			usage();
245 		depth = 0;
246 	}
247 
248 	if (!*argv) {
249 		argv = save;
250 		argv[0] = dot;
251 		argv[1] = NULL;
252 	}
253 
254 	if (blocksize == 0)
255 		(void)getbsize(&notused, &blocksize);
256 
257 	if (!Aflag) {
258 		cblocksize /= DEV_BSIZE;
259 		blocksize /= DEV_BSIZE;
260 	}
261 
262 	if (threshold != 0)
263 		threshold = howmany(threshold / DEV_BSIZE * cblocksize,
264 		    blocksize);
265 
266 	rval = 0;
267 
268 	(void)signal(SIGINFO, siginfo);
269 
270 	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
271 		err(1, "fts_open");
272 
273 	while ((p = fts_read(fts)) != NULL) {
274 		switch (p->fts_info) {
275 		case FTS_D:			/* Ignore. */
276 			if (ignorep(p))
277 				fts_set(fts, p, FTS_SKIP);
278 			break;
279 		case FTS_DP:
280 			if (ignorep(p))
281 				break;
282 
283 			curblocks = Aflag ?
284 			    howmany(p->fts_statp->st_size, cblocksize) :
285 			    howmany(p->fts_statp->st_blocks, cblocksize);
286 			p->fts_parent->fts_bignum += p->fts_bignum +=
287 			    curblocks;
288 
289 			if (p->fts_level <= depth && threshold <=
290 			    threshold_sign * howmany(p->fts_bignum *
291 			    cblocksize, blocksize)) {
292 				if (hflag) {
293 					prthumanval(p->fts_bignum);
294 					(void)printf("\t%s\n", p->fts_path);
295 				} else {
296 					(void)printf("%jd\t%s\n",
297 					    (intmax_t)howmany(p->fts_bignum *
298 					    cblocksize, blocksize),
299 					    p->fts_path);
300 				}
301 			}
302 			if (info) {
303 				info = 0;
304 				(void)printf("\t%s\n", p->fts_path);
305 			}
306 			break;
307 		case FTS_DC:			/* Ignore. */
308 			break;
309 		case FTS_DNR:			/* Warn, continue. */
310 		case FTS_ERR:
311 		case FTS_NS:
312 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
313 			rval = 1;
314 			break;
315 		default:
316 			if (ignorep(p))
317 				break;
318 
319 			if (lflag == 0 && p->fts_statp->st_nlink > 1 &&
320 			    linkchk(p))
321 				break;
322 
323 			curblocks = Aflag ?
324 			    howmany(p->fts_statp->st_size, cblocksize) :
325 			    howmany(p->fts_statp->st_blocks, cblocksize);
326 
327 			if (listall || p->fts_level == 0) {
328 				if (hflag) {
329 					prthumanval(curblocks);
330 					(void)printf("\t%s\n", p->fts_path);
331 				} else {
332 					(void)printf("%jd\t%s\n",
333 					    (intmax_t)howmany(curblocks *
334 					    cblocksize, blocksize),
335 					    p->fts_path);
336 				}
337 			}
338 
339 			p->fts_parent->fts_bignum += curblocks;
340 		}
341 		savednumber = p->fts_parent->fts_bignum;
342 	}
343 
344 	if (errno)
345 		err(1, "fts_read");
346 
347 	if (cflag) {
348 		if (hflag) {
349 			prthumanval(savednumber);
350 			(void)printf("\ttotal\n");
351 		} else {
352 			(void)printf("%jd\ttotal\n", (intmax_t)howmany(
353 			    savednumber * cblocksize, blocksize));
354 		}
355 	}
356 
357 	ignoreclean();
358 	exit(rval);
359 }
360 
361 static int
362 linkchk(FTSENT *p)
363 {
364 	struct links_entry {
365 		struct links_entry *next;
366 		struct links_entry *previous;
367 		int	 links;
368 		dev_t	 dev;
369 		ino_t	 ino;
370 	};
371 	static const size_t links_hash_initial_size = 8192;
372 	static struct links_entry **buckets;
373 	static struct links_entry *free_list;
374 	static size_t number_buckets;
375 	static unsigned long number_entries;
376 	static char stop_allocating;
377 	struct links_entry *le, **new_buckets;
378 	struct stat *st;
379 	size_t i, new_size;
380 	int hash;
381 
382 	st = p->fts_statp;
383 
384 	/* If necessary, initialize the hash table. */
385 	if (buckets == NULL) {
386 		number_buckets = links_hash_initial_size;
387 		buckets = malloc(number_buckets * sizeof(buckets[0]));
388 		if (buckets == NULL)
389 			errx(1, "No memory for hardlink detection");
390 		for (i = 0; i < number_buckets; i++)
391 			buckets[i] = NULL;
392 	}
393 
394 	/* If the hash table is getting too full, enlarge it. */
395 	if (number_entries > number_buckets * 10 && !stop_allocating) {
396 		new_size = number_buckets * 2;
397 		new_buckets = malloc(new_size * sizeof(struct links_entry *));
398 
399 		/* Try releasing the free list to see if that helps. */
400 		if (new_buckets == NULL && free_list != NULL) {
401 			while (free_list != NULL) {
402 				le = free_list;
403 				free_list = le->next;
404 				free(le);
405 			}
406 			new_buckets = malloc(new_size *
407 			    sizeof(new_buckets[0]));
408 		}
409 
410 		if (new_buckets == NULL) {
411 			stop_allocating = 1;
412 			warnx("No more memory for tracking hard links");
413 		} else {
414 			memset(new_buckets, 0,
415 			    new_size * sizeof(struct links_entry *));
416 			for (i = 0; i < number_buckets; i++) {
417 				while (buckets[i] != NULL) {
418 					/* Remove entry from old bucket. */
419 					le = buckets[i];
420 					buckets[i] = le->next;
421 
422 					/* Add entry to new bucket. */
423 					hash = (le->dev ^ le->ino) % new_size;
424 
425 					if (new_buckets[hash] != NULL)
426 						new_buckets[hash]->previous =
427 						    le;
428 					le->next = new_buckets[hash];
429 					le->previous = NULL;
430 					new_buckets[hash] = le;
431 				}
432 			}
433 			free(buckets);
434 			buckets = new_buckets;
435 			number_buckets = new_size;
436 		}
437 	}
438 
439 	/* Try to locate this entry in the hash table. */
440 	hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
441 	for (le = buckets[hash]; le != NULL; le = le->next) {
442 		if (le->dev == st->st_dev && le->ino == st->st_ino) {
443 			/*
444 			 * Save memory by releasing an entry when we've seen
445 			 * all of it's links.
446 			 */
447 			if (--le->links <= 0) {
448 				if (le->previous != NULL)
449 					le->previous->next = le->next;
450 				if (le->next != NULL)
451 					le->next->previous = le->previous;
452 				if (buckets[hash] == le)
453 					buckets[hash] = le->next;
454 				number_entries--;
455 				/* Recycle this node through the free list */
456 				if (stop_allocating) {
457 					free(le);
458 				} else {
459 					le->next = free_list;
460 					free_list = le;
461 				}
462 			}
463 			return (1);
464 		}
465 	}
466 
467 	if (stop_allocating)
468 		return (0);
469 
470 	/* Add this entry to the links cache. */
471 	if (free_list != NULL) {
472 		/* Pull a node from the free list if we can. */
473 		le = free_list;
474 		free_list = le->next;
475 	} else
476 		/* Malloc one if we have to. */
477 		le = malloc(sizeof(struct links_entry));
478 	if (le == NULL) {
479 		stop_allocating = 1;
480 		warnx("No more memory for tracking hard links");
481 		return (0);
482 	}
483 	le->dev = st->st_dev;
484 	le->ino = st->st_ino;
485 	le->links = st->st_nlink - 1;
486 	number_entries++;
487 	le->next = buckets[hash];
488 	le->previous = NULL;
489 	if (buckets[hash] != NULL)
490 		buckets[hash]->previous = le;
491 	buckets[hash] = le;
492 	return (0);
493 }
494 
495 static void
496 prthumanval(int64_t bytes)
497 {
498 	char buf[5];
499 
500 	bytes *= cblocksize;
501 	if (!Aflag)
502 		bytes *= DEV_BSIZE;
503 
504 	humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
505 	    HN_B | HN_NOSPACE | HN_DECIMAL);
506 
507 	(void)printf("%4s", buf);
508 }
509 
510 static void
511 usage(void)
512 {
513 	(void)fprintf(stderr,
514 		"usage: du [-A] [-H | -L | -P] [-a | -s | -d depth] [-c] "
515 		"[-l] [-h | -k | -m | -B bsize] [-n] [-x] [-I mask] "
516 		"[file ...]\n");
517 	exit(EX_USAGE);
518 }
519 
520 static void
521 ignoreadd(const char *mask)
522 {
523 	struct ignentry *ign;
524 
525 	ign = calloc(1, sizeof(*ign));
526 	if (ign == NULL)
527 		errx(1, "cannot allocate memory");
528 	ign->mask = strdup(mask);
529 	if (ign->mask == NULL)
530 		errx(1, "cannot allocate memory");
531 	SLIST_INSERT_HEAD(&ignores, ign, next);
532 }
533 
534 static void
535 ignoreclean(void)
536 {
537 	struct ignentry *ign;
538 
539 	while (!SLIST_EMPTY(&ignores)) {
540 		ign = SLIST_FIRST(&ignores);
541 		SLIST_REMOVE_HEAD(&ignores, next);
542 		free(ign->mask);
543 		free(ign);
544 	}
545 }
546 
547 static int
548 ignorep(FTSENT *ent)
549 {
550 	struct ignentry *ign;
551 
552 	if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
553 		return 1;
554 	SLIST_FOREACH(ign, &ignores, next)
555 		if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
556 			return 1;
557 	return 0;
558 }
559 
560 static void
561 siginfo(int sig __unused)
562 {
563 
564 	info = 1;
565 }
566