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