xref: /freebsd/usr.bin/du/du.c (revision 2357939bc239bd5334a169b62313806178dd8f30)
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 <math.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <sysexits.h>
64 #include <unistd.h>
65 
66 #define	KILO_SZ(n) (n)
67 #define	MEGA_SZ(n) ((n) * (n))
68 #define	GIGA_SZ(n) ((n) * (n) * (n))
69 #define	TERA_SZ(n) ((n) * (n) * (n) * (n))
70 #define	PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
71 
72 #define	KILO_2_SZ (KILO_SZ(1024ULL))
73 #define	MEGA_2_SZ (MEGA_SZ(1024ULL))
74 #define	GIGA_2_SZ (GIGA_SZ(1024ULL))
75 #define	TERA_2_SZ (TERA_SZ(1024ULL))
76 #define	PETA_2_SZ (PETA_SZ(1024ULL))
77 
78 #define	KILO_SI_SZ (KILO_SZ(1000ULL))
79 #define	MEGA_SI_SZ (MEGA_SZ(1000ULL))
80 #define	GIGA_SI_SZ (GIGA_SZ(1000ULL))
81 #define	TERA_SI_SZ (TERA_SZ(1000ULL))
82 #define	PETA_SI_SZ (PETA_SZ(1000ULL))
83 
84 unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
85 unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
86 unsigned long long *valp;
87 
88 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
89 
90 int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
91 
92 SLIST_HEAD(ignhead, ignentry) ignores;
93 struct ignentry {
94 	char			*mask;
95 	SLIST_ENTRY(ignentry)	next;
96 };
97 
98 static int	linkchk(FTSENT *);
99 static void	usage(void);
100 void		prthumanval(double);
101 unit_t		unit_adjust(double *);
102 void		ignoreadd(const char *);
103 void		ignoreclean(void);
104 int		ignorep(FTSENT *);
105 
106 int
107 main(int argc, char *argv[])
108 {
109 	FTS		*fts;
110 	FTSENT		*p;
111 	long		blocksize, savednumber = 0;
112 	int		ftsoptions;
113 	int		listall;
114 	int		depth;
115 	int		Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
116 	char 		**save;
117 	static char	dot[] = ".";
118 
119 	Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
120 
121 	save = argv;
122 	ftsoptions = 0;
123 	depth = INT_MAX;
124 	SLIST_INIT(&ignores);
125 
126 	while ((ch = getopt(argc, argv, "HI:LPasd:chkrx")) != -1)
127 		switch (ch) {
128 			case 'H':
129 				Hflag = 1;
130 				break;
131 			case 'I':
132 				ignoreadd(optarg);
133 				break;
134 			case 'L':
135 				if (Pflag)
136 					usage();
137 				Lflag = 1;
138 				break;
139 			case 'P':
140 				if (Lflag)
141 					usage();
142 				Pflag = 1;
143 				break;
144 			case 'a':
145 				aflag = 1;
146 				break;
147 			case 's':
148 				sflag = 1;
149 				break;
150 			case 'd':
151 				dflag = 1;
152 				errno = 0;
153 				depth = atoi(optarg);
154 				if (errno == ERANGE || depth < 0) {
155 					warnx("invalid argument to option d: %s", optarg);
156 					usage();
157 				}
158 				break;
159 			case 'c':
160 				cflag = 1;
161 				break;
162 			case 'h':
163 				putenv("BLOCKSIZE=512");
164 				hflag = 1;
165 				valp = vals_base2;
166 				break;
167 			case 'k':
168 				hflag = 0;
169 				putenv("BLOCKSIZE=1024");
170 				break;
171 			case 'r':		 /* Compatibility. */
172 				break;
173 			case 'x':
174 				ftsoptions |= FTS_XDEV;
175 				break;
176 			case '?':
177 			default:
178 				usage();
179 		}
180 
181 	argc -= optind;
182 	argv += optind;
183 
184 	/*
185 	 * XXX
186 	 * Because of the way that fts(3) works, logical walks will not count
187 	 * the blocks actually used by symbolic links.  We rationalize this by
188 	 * noting that users computing logical sizes are likely to do logical
189 	 * copies, so not counting the links is correct.  The real reason is
190 	 * that we'd have to re-implement the kernel's symbolic link traversing
191 	 * algorithm to get this right.  If, for example, you have relative
192 	 * symbolic links referencing other relative symbolic links, it gets
193 	 * very nasty, very fast.  The bottom line is that it's documented in
194 	 * the man page, so it's a feature.
195 	 */
196 
197 	if (Hflag + Lflag + Pflag > 1)
198 		usage();
199 
200 	if (Hflag + Lflag + Pflag == 0)
201 		Pflag = 1;			/* -P (physical) is default */
202 
203 	if (Hflag)
204 		ftsoptions |= FTS_COMFOLLOW;
205 
206 	if (Lflag)
207 		ftsoptions |= FTS_LOGICAL;
208 
209 	if (Pflag)
210 		ftsoptions |= FTS_PHYSICAL;
211 
212 	listall = 0;
213 
214 	if (aflag) {
215 		if (sflag || dflag)
216 			usage();
217 		listall = 1;
218 	} else if (sflag) {
219 		if (dflag)
220 			usage();
221 		depth = 0;
222 	}
223 
224 	if (!*argv) {
225 		argv = save;
226 		argv[0] = dot;
227 		argv[1] = NULL;
228 	}
229 
230 	(void) getbsize(&notused, &blocksize);
231 	blocksize /= 512;
232 
233 	rval = 0;
234 
235 	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
236 		err(1, "fts_open");
237 
238 	while ((p = fts_read(fts)) != NULL) {
239 		switch (p->fts_info) {
240 			case FTS_D:			/* Ignore. */
241 				if (ignorep(p))
242 					fts_set(fts, p, FTS_SKIP);
243 				break;
244 			case FTS_DP:
245 				if (ignorep(p))
246 					break;
247 
248 				p->fts_parent->fts_number +=
249 				    p->fts_number += p->fts_statp->st_blocks;
250 
251 				if (p->fts_level <= depth) {
252 					if (hflag) {
253 						(void) prthumanval(howmany(p->fts_number, blocksize));
254 						(void) printf("\t%s\n", p->fts_path);
255 					} else {
256 					(void) printf("%ld\t%s\n",
257 					    howmany(p->fts_number, blocksize),
258 					    p->fts_path);
259 					}
260 				}
261 				break;
262 			case FTS_DC:			/* Ignore. */
263 				break;
264 			case FTS_DNR:			/* Warn, continue. */
265 			case FTS_ERR:
266 			case FTS_NS:
267 				warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
268 				rval = 1;
269 				break;
270 			default:
271 				if (ignorep(p))
272 					break;
273 
274 				if (p->fts_statp->st_nlink > 1 && linkchk(p))
275 					break;
276 
277 				if (listall || p->fts_level == 0) {
278 					if (hflag) {
279 						(void) prthumanval(howmany(p->fts_statp->st_blocks,
280 							blocksize));
281 						(void) printf("\t%s\n", p->fts_path);
282 					} else {
283 						(void) printf("%qd\t%s\n",
284 							(long long)howmany(p->fts_statp->st_blocks, blocksize),
285 							p->fts_path);
286 					}
287 				}
288 
289 				p->fts_parent->fts_number += p->fts_statp->st_blocks;
290 		}
291 		savednumber = p->fts_parent->fts_number;
292 	}
293 
294 	if (errno)
295 		err(1, "fts_read");
296 
297 	if (cflag) {
298 		if (hflag) {
299 			(void) prthumanval(howmany(savednumber, blocksize));
300 			(void) printf("\ttotal\n");
301 		} else {
302 			(void) printf("%ld\ttotal\n", howmany(savednumber, blocksize));
303 		}
304 	}
305 
306 	ignoreclean();
307 	exit(rval);
308 }
309 
310 static int
311 linkchk(FTSENT *p)
312 {
313 	struct links_entry {
314 		struct links_entry *next;
315 		struct links_entry *previous;
316 		int	 links;
317 		dev_t	 dev;
318 		ino_t	 ino;
319 	};
320 	static const size_t links_hash_initial_size = 8192;
321 	static struct links_entry **buckets;
322 	static struct links_entry *free_list;
323 	static size_t number_buckets;
324 	static unsigned long number_entries;
325 	static char stop_allocating;
326 	struct links_entry *le, **new_buckets;
327 	struct stat *st;
328 	size_t i, new_size;
329 	int count, hash;
330 
331 	st = p->fts_statp;
332 
333 	/* If necessary, initialize the hash table. */
334 	if (buckets == NULL) {
335 		number_buckets = links_hash_initial_size;
336 		buckets = malloc(number_buckets * sizeof(buckets[0]));
337 		if (buckets == NULL)
338 			errx(1, "No memory for hardlink detection");
339 		for (i = 0; i < number_buckets; i++)
340 			buckets[i] = NULL;
341 	}
342 
343 	/* If the hash table is getting too full, enlarge it. */
344 	if (number_entries > number_buckets * 10 && !stop_allocating) {
345 		new_size = number_buckets * 2;
346 		new_buckets = malloc(new_size * sizeof(struct links_entry *));
347 		count = 0;
348 
349 		/* Try releasing the free list to see if that helps. */
350 		if (new_buckets == NULL && free_list != NULL) {
351 			while (free_list != NULL) {
352 				le = free_list;
353 				free_list = le->next;
354 				free(le);
355 			}
356 			new_buckets = malloc(new_size * sizeof(new_buckets[0]));
357 		}
358 
359 		if (new_buckets == NULL) {
360 			stop_allocating = 1;
361 			warnx("No more memory for tracking hard links");
362 		} else {
363 			memset(new_buckets, 0,
364 			    new_size * sizeof(struct links_entry *));
365 			for (i = 0; i < number_buckets; i++) {
366 				while (buckets[i] != NULL) {
367 					/* Remove entry from old bucket. */
368 					le = buckets[i];
369 					buckets[i] = le->next;
370 
371 					/* Add entry to new bucket. */
372 					hash = (le->dev ^ le->ino) % new_size;
373 
374 					if (new_buckets[hash] != NULL)
375 						new_buckets[hash]->previous =
376 						    le;
377 					le->next = new_buckets[hash];
378 					le->previous = NULL;
379 					new_buckets[hash] = le;
380 				}
381 			}
382 			free(buckets);
383 			buckets = new_buckets;
384 			number_buckets = new_size;
385 		}
386 	}
387 
388 	/* Try to locate this entry in the hash table. */
389 	hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
390 	for (le = buckets[hash]; le != NULL; le = le->next) {
391 		if (le->dev == st->st_dev && le->ino == st->st_ino) {
392 			/*
393 			 * Save memory by releasing an entry when we've seen
394 			 * all of it's links.
395 			 */
396 			if (--le->links <= 0) {
397 				if (le->previous != NULL)
398 					le->previous->next = le->next;
399 				if (le->next != NULL)
400 					le->next->previous = le->previous;
401 				if (buckets[hash] == le)
402 					buckets[hash] = le->next;
403 				number_entries--;
404 				/* Recycle this node through the free list */
405 				if (stop_allocating) {
406 					free(le);
407 				} else {
408 					le->next = free_list;
409 					free_list = le;
410 				}
411 			}
412 			return (1);
413 		}
414 	}
415 
416 	if (stop_allocating)
417 		return (0);
418 
419 	/* Add this entry to the links cache. */
420 	if (free_list != NULL) {
421 		/* Pull a node from the free list if we can. */
422 		le = free_list;
423 		free_list = le->next;
424 	} else
425 		/* Malloc one if we have to. */
426 		le = malloc(sizeof(struct links_entry));
427 	if (le == NULL) {
428 		stop_allocating = 1;
429 		warnx("No more memory for tracking hard links");
430 		return (0);
431 	}
432 	le->dev = st->st_dev;
433 	le->ino = st->st_ino;
434 	le->links = st->st_nlink - 1;
435 	number_entries++;
436 	le->next = buckets[hash];
437 	le->previous = NULL;
438 	if (buckets[hash] != NULL)
439 		buckets[hash]->previous = le;
440 	buckets[hash] = le;
441 	return (0);
442 }
443 
444 /*
445  * Output in "human-readable" format.  Uses 3 digits max and puts
446  * unit suffixes at the end.  Makes output compact and easy to read,
447  * especially on huge disks.
448  *
449  */
450 unit_t
451 unit_adjust(double *val)
452 {
453 	double abval;
454 	unit_t unit;
455 	unsigned int unit_sz;
456 
457 	abval = fabs(*val);
458 
459 	unit_sz = abval ? ilogb(abval) / 10 : 0;
460 
461 	if (unit_sz >= UNIT_MAX) {
462 		unit = NONE;
463 	} else {
464 		unit = unitp[unit_sz];
465 		*val /= (double)valp[unit_sz];
466 	}
467 
468 	return (unit);
469 }
470 
471 void
472 prthumanval(double bytes)
473 {
474 	unit_t unit;
475 
476 	bytes *= 512;
477 	unit = unit_adjust(&bytes);
478 
479 	if (bytes == 0)
480 		(void)printf("  0B");
481 	else if (bytes > 10)
482 		(void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
483 	else
484 		(void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
485 }
486 
487 static void
488 usage(void)
489 {
490 	(void)fprintf(stderr,
491 		"usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
492 	exit(EX_USAGE);
493 }
494 
495 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 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 int
523 ignorep(FTSENT *ent)
524 {
525 	struct ignentry *ign;
526 
527 	SLIST_FOREACH(ign, &ignores, next)
528 		if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
529 			return 1;
530 	return 0;
531 }
532