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