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