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