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