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