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