xref: /freebsd/usr.bin/du/du.c (revision 74a9aac40e4283503952489645476155a3593954)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993, 1994
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes  * Chris Newcomb.
99b50d902SRodney W. Grimes  *
109b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes  * are met:
139b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes  *    without specific prior written permission.
219b50d902SRodney W. Grimes  *
229b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes  * SUCH DAMAGE.
339b50d902SRodney W. Grimes  */
349b50d902SRodney W. Grimes 
359b50d902SRodney W. Grimes #include <sys/param.h>
364bb69e35SPeter Pentchev #include <sys/queue.h>
379b50d902SRodney W. Grimes #include <sys/stat.h>
389b50d902SRodney W. Grimes #include <err.h>
399b50d902SRodney W. Grimes #include <errno.h>
404bb69e35SPeter Pentchev #include <fnmatch.h>
419b50d902SRodney W. Grimes #include <fts.h>
42476c601bSKyle Evans #include <getopt.h>
437d3940bbSPawel Jakub Dawidek #include <libutil.h>
44e781c653STim J. Robbins #include <locale.h>
45dc347bc4SPawel Jakub Dawidek #include <stdint.h>
469b50d902SRodney W. Grimes #include <stdio.h>
479b50d902SRodney W. Grimes #include <stdlib.h>
48*74a9aac4SMichal Scigocki #include <stdbool.h>
499b50d902SRodney W. Grimes #include <string.h>
5054aa1771SMichael Haro #include <sysexits.h>
51df3f5d9dSPeter Wemm #include <unistd.h>
52b2b8fa1aSNathan Huff #include <libxo/xo.h>
539b50d902SRodney W. Grimes 
54476c601bSKyle Evans #define SI_OPT	(CHAR_MAX + 1)
55476c601bSKyle Evans 
56476c601bSKyle Evans #define UNITS_2		1
57476c601bSKyle Evans #define UNITS_SI	2
58476c601bSKyle Evans 
5971978fa4SBram #define DU_XO_VERSION "1"
6071978fa4SBram 
61d2aa301dSEd Schouten static SLIST_HEAD(ignhead, ignentry) ignores;
624bb69e35SPeter Pentchev struct ignentry {
634bb69e35SPeter Pentchev 	char			*mask;
644bb69e35SPeter Pentchev 	SLIST_ENTRY(ignentry)	next;
654bb69e35SPeter Pentchev };
664bb69e35SPeter Pentchev 
67*74a9aac4SMichal Scigocki static bool	check_threshold(FTSENT *);
680551897aSMax Laier static void	ignoreadd(const char *);
690551897aSMax Laier static void	ignoreclean(void);
700551897aSMax Laier static int	ignorep(FTSENT *);
71*74a9aac4SMichal Scigocki static int	linkchk(FTSENT *);
72*74a9aac4SMichal Scigocki static void	print_file_size(FTSENT *);
73*74a9aac4SMichal Scigocki static void	prthumanval(const char *, int64_t);
74*74a9aac4SMichal Scigocki static void	record_file_size(FTSENT *);
75d1588599SWarner Losh static void	siginfo(int __unused);
76*74a9aac4SMichal Scigocki static void	usage(void);
779b50d902SRodney W. Grimes 
780551897aSMax Laier static int	nodumpflag = 0;
79476c601bSKyle Evans static int	Aflag, hflag;
80fd543f27SMax Laier static long	blocksize, cblocksize;
81d1588599SWarner Losh static volatile sig_atomic_t info;
82*74a9aac4SMichal Scigocki static off_t	threshold, threshold_sign;
83fadc7151SMaxim Konovalov 
84*74a9aac4SMichal Scigocki static const struct option long_options[] = {
85476c601bSKyle Evans 	{ "si", no_argument, NULL, SI_OPT },
86476c601bSKyle Evans 	{ NULL, no_argument, NULL, 0 },
87476c601bSKyle Evans };
88476c601bSKyle Evans 
899b50d902SRodney W. Grimes int
main(int argc,char * argv[])90c10b37bfSDavid Malone main(int argc, char *argv[])
919b50d902SRodney W. Grimes {
929b50d902SRodney W. Grimes 	FTS		*fts;
939b50d902SRodney W. Grimes 	FTSENT		*p;
94*74a9aac4SMichal Scigocki 	off_t		savednumber;
951aa8a0a9SJordan K. Hubbard 	int		ftsoptions;
961aa8a0a9SJordan K. Hubbard 	int		depth;
976530d2f2SJilles Tjoelker 	int		Hflag, Lflag, aflag, sflag, dflag, cflag;
98476c601bSKyle Evans 	int		lflag, ch, notused, rval;
999b50d902SRodney W. Grimes 	char 		**save;
100ef0ea716SMark Murray 	static char	dot[] = ".";
1019b50d902SRodney W. Grimes 
102e781c653STim J. Robbins 	setlocale(LC_ALL, "");
103e781c653STim J. Robbins 
104476c601bSKyle Evans 	Hflag = Lflag = aflag = sflag = dflag = cflag = lflag = Aflag = 0;
1051aa8a0a9SJordan K. Hubbard 
1069b50d902SRodney W. Grimes 	save = argv;
1076530d2f2SJilles Tjoelker 	ftsoptions = FTS_PHYSICAL;
10840850176SMax Laier 	savednumber = 0;
109f0cc075cSBrian Somers 	threshold = 0;
110f0cc075cSBrian Somers 	threshold_sign = 1;
111fd543f27SMax Laier 	cblocksize = DEV_BSIZE;
112fd543f27SMax Laier 	blocksize = 0;
1130282769eSMarc G. Fournier 	depth = INT_MAX;
1144bb69e35SPeter Pentchev 	SLIST_INIT(&ignores);
1151aa8a0a9SJordan K. Hubbard 
116b2b8fa1aSNathan Huff 	argc = xo_parse_args(argc, argv);
117b2b8fa1aSNathan Huff 	if (argc < 0)
118b2b8fa1aSNathan Huff 		exit(EX_USAGE);
119b2b8fa1aSNathan Huff 
120476c601bSKyle Evans 	while ((ch = getopt_long(argc, argv, "+AB:HI:LPasd:cghklmnrt:x",
121476c601bSKyle Evans 	    long_options, NULL)) != -1)
1229b50d902SRodney W. Grimes 		switch (ch) {
123fd543f27SMax Laier 		case 'A':
124fd543f27SMax Laier 			Aflag = 1;
125fd543f27SMax Laier 			break;
126fd543f27SMax Laier 		case 'B':
127fd543f27SMax Laier 			errno = 0;
128fd543f27SMax Laier 			cblocksize = atoi(optarg);
129fd543f27SMax Laier 			if (errno == ERANGE || cblocksize <= 0) {
130b2b8fa1aSNathan Huff 				xo_warnx("invalid argument to option B: %s",
131fd543f27SMax Laier 				    optarg);
132fd543f27SMax Laier 				usage();
133fd543f27SMax Laier 			}
134fd543f27SMax Laier 			break;
1359b50d902SRodney W. Grimes 		case 'H':
1369b50d902SRodney W. Grimes 			Hflag = 1;
1376530d2f2SJilles Tjoelker 			Lflag = 0;
1389b50d902SRodney W. Grimes 			break;
1394bb69e35SPeter Pentchev 		case 'I':
1404bb69e35SPeter Pentchev 			ignoreadd(optarg);
1414bb69e35SPeter Pentchev 			break;
1429b50d902SRodney W. Grimes 		case 'L':
1439b50d902SRodney W. Grimes 			Lflag = 1;
1446530d2f2SJilles Tjoelker 			Hflag = 0;
1459b50d902SRodney W. Grimes 			break;
1469b50d902SRodney W. Grimes 		case 'P':
1476530d2f2SJilles Tjoelker 			Hflag = Lflag = 0;
1489b50d902SRodney W. Grimes 			break;
1499b50d902SRodney W. Grimes 		case 'a':
1509b50d902SRodney W. Grimes 			aflag = 1;
1519b50d902SRodney W. Grimes 			break;
1529b50d902SRodney W. Grimes 		case 's':
1539b50d902SRodney W. Grimes 			sflag = 1;
1549b50d902SRodney W. Grimes 			break;
1550282769eSMarc G. Fournier 		case 'd':
1560282769eSMarc G. Fournier 			dflag = 1;
1571aa8a0a9SJordan K. Hubbard 			errno = 0;
1580282769eSMarc G. Fournier 			depth = atoi(optarg);
1591aa8a0a9SJordan K. Hubbard 			if (errno == ERANGE || depth < 0) {
160b2b8fa1aSNathan Huff 				xo_warnx("invalid argument to option d: %s",
16140850176SMax Laier 				    optarg);
1620282769eSMarc G. Fournier 				usage();
1630282769eSMarc G. Fournier 			}
1640282769eSMarc G. Fournier 			break;
1651aa8a0a9SJordan K. Hubbard 		case 'c':
1661aa8a0a9SJordan K. Hubbard 			cflag = 1;
1671aa8a0a9SJordan K. Hubbard 			break;
16869739e8dSDag-Erling Smørgrav 		case 'g':
16969739e8dSDag-Erling Smørgrav 			hflag = 0;
17069739e8dSDag-Erling Smørgrav 			blocksize = 1073741824;
17169739e8dSDag-Erling Smørgrav 			break;
17254aa1771SMichael Haro 		case 'h':
173476c601bSKyle Evans 			hflag = UNITS_2;
17454aa1771SMichael Haro 			break;
17554aa1771SMichael Haro 		case 'k':
176d84f9f6cSDavid E. O'Brien 			hflag = 0;
177fd543f27SMax Laier 			blocksize = 1024;
17854aa1771SMichael Haro 			break;
179fe5628d3SGiorgos Keramidas 		case 'l':
180fe5628d3SGiorgos Keramidas 			lflag = 1;
181fe5628d3SGiorgos Keramidas 			break;
182b933ee54SPoul-Henning Kamp 		case 'm':
183b933ee54SPoul-Henning Kamp 			hflag = 0;
184fd543f27SMax Laier 			blocksize = 1048576;
185b933ee54SPoul-Henning Kamp 			break;
186fadc7151SMaxim Konovalov 		case 'n':
187fadc7151SMaxim Konovalov 			nodumpflag = 1;
188fadc7151SMaxim Konovalov 			break;
18954aa1771SMichael Haro 		case 'r':		 /* Compatibility. */
19054aa1771SMichael Haro 			break;
191f0cc075cSBrian Somers 		case 't':
192f0cc075cSBrian Somers 			if (expand_number(optarg, &threshold) != 0 ||
193f0cc075cSBrian Somers 			    threshold == 0) {
194b2b8fa1aSNathan Huff 				xo_warnx("invalid threshold: %s", optarg);
195f0cc075cSBrian Somers 				usage();
196f0cc075cSBrian Somers 			} else if (threshold < 0)
197f0cc075cSBrian Somers 				threshold_sign = -1;
198f0cc075cSBrian Somers 			break;
19954aa1771SMichael Haro 		case 'x':
20054aa1771SMichael Haro 			ftsoptions |= FTS_XDEV;
20154aa1771SMichael Haro 			break;
202476c601bSKyle Evans 		case SI_OPT:
203476c601bSKyle Evans 			hflag = UNITS_SI;
204476c601bSKyle Evans 			break;
2059b50d902SRodney W. Grimes 		case '?':
2069b50d902SRodney W. Grimes 		default:
2079b50d902SRodney W. Grimes 			usage();
20840850176SMax Laier 			/* NOTREACHED */
2099b50d902SRodney W. Grimes 		}
2101aa8a0a9SJordan K. Hubbard 
2119b50d902SRodney W. Grimes 	argc -= optind;
2129b50d902SRodney W. Grimes 	argv += optind;
2139b50d902SRodney W. Grimes 
2149b50d902SRodney W. Grimes 	/*
2159b50d902SRodney W. Grimes 	 * XXX
2169b50d902SRodney W. Grimes 	 * Because of the way that fts(3) works, logical walks will not count
2179b50d902SRodney W. Grimes 	 * the blocks actually used by symbolic links.  We rationalize this by
2189b50d902SRodney W. Grimes 	 * noting that users computing logical sizes are likely to do logical
2199b50d902SRodney W. Grimes 	 * copies, so not counting the links is correct.  The real reason is
2209b50d902SRodney W. Grimes 	 * that we'd have to re-implement the kernel's symbolic link traversing
2219b50d902SRodney W. Grimes 	 * algorithm to get this right.  If, for example, you have relative
2229b50d902SRodney W. Grimes 	 * symbolic links referencing other relative symbolic links, it gets
2239b50d902SRodney W. Grimes 	 * very nasty, very fast.  The bottom line is that it's documented in
2249b50d902SRodney W. Grimes 	 * the man page, so it's a feature.
2259b50d902SRodney W. Grimes 	 */
2261aa8a0a9SJordan K. Hubbard 
2279b50d902SRodney W. Grimes 	if (Hflag)
2289b50d902SRodney W. Grimes 		ftsoptions |= FTS_COMFOLLOW;
2296530d2f2SJilles Tjoelker 	if (Lflag) {
2306530d2f2SJilles Tjoelker 		ftsoptions &= ~FTS_PHYSICAL;
2319b50d902SRodney W. Grimes 		ftsoptions |= FTS_LOGICAL;
2326530d2f2SJilles Tjoelker 	}
2331aa8a0a9SJordan K. Hubbard 
234fd543f27SMax Laier 	if (!Aflag && (cblocksize % DEV_BSIZE) != 0)
235fd543f27SMax Laier 		cblocksize = howmany(cblocksize, DEV_BSIZE) * DEV_BSIZE;
236fd543f27SMax Laier 
23766c5875aSEd Schouten 	if (aflag + dflag + sflag > 1)
2389b50d902SRodney W. Grimes 		usage();
23966c5875aSEd Schouten 	if (sflag)
2401aa8a0a9SJordan K. Hubbard 		depth = 0;
2419b50d902SRodney W. Grimes 
2429b50d902SRodney W. Grimes 	if (!*argv) {
2439b50d902SRodney W. Grimes 		argv = save;
244ef0ea716SMark Murray 		argv[0] = dot;
2459b50d902SRodney W. Grimes 		argv[1] = NULL;
2469b50d902SRodney W. Grimes 	}
2479b50d902SRodney W. Grimes 
248fd543f27SMax Laier 	if (blocksize == 0)
2499b50d902SRodney W. Grimes 		(void)getbsize(&notused, &blocksize);
250fd543f27SMax Laier 
251fd543f27SMax Laier 	if (!Aflag) {
252fd543f27SMax Laier 		cblocksize /= DEV_BSIZE;
253fd543f27SMax Laier 		blocksize /= DEV_BSIZE;
254fd543f27SMax Laier 	}
2559b50d902SRodney W. Grimes 
256f0cc075cSBrian Somers 	if (threshold != 0)
257f0cc075cSBrian Somers 		threshold = howmany(threshold / DEV_BSIZE * cblocksize,
258f0cc075cSBrian Somers 		    blocksize);
259f0cc075cSBrian Somers 
2601aa8a0a9SJordan K. Hubbard 	rval = 0;
2619b50d902SRodney W. Grimes 
262d1588599SWarner Losh 	(void)signal(SIGINFO, siginfo);
263d1588599SWarner Losh 
2641aa8a0a9SJordan K. Hubbard 	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
2651aa8a0a9SJordan K. Hubbard 		err(1, "fts_open");
2661aa8a0a9SJordan K. Hubbard 
26771978fa4SBram 
26871978fa4SBram 	xo_set_version(DU_XO_VERSION);
269b2b8fa1aSNathan Huff 	xo_open_container("disk-usage-information");
270b2b8fa1aSNathan Huff 	xo_open_list("paths");
2712dfa4b66SBryan Drewery 	while (errno = 0, (p = fts_read(fts)) != NULL) {
2729b50d902SRodney W. Grimes 		switch (p->fts_info) {
2739b50d902SRodney W. Grimes 		case FTS_D:			/* Ignore. */
2744bb69e35SPeter Pentchev 			if (ignorep(p))
2754bb69e35SPeter Pentchev 				fts_set(fts, p, FTS_SKIP);
2769b50d902SRodney W. Grimes 			break;
277*74a9aac4SMichal Scigocki 		case FTS_DP:			/* Directory files */
2784bb69e35SPeter Pentchev 			if (ignorep(p))
2794bb69e35SPeter Pentchev 				break;
2804bb69e35SPeter Pentchev 
281*74a9aac4SMichal Scigocki 			record_file_size(p);
2821aa8a0a9SJordan K. Hubbard 
283*74a9aac4SMichal Scigocki 			if (p->fts_level <= depth && check_threshold(p))
284*74a9aac4SMichal Scigocki 				print_file_size(p);
285*74a9aac4SMichal Scigocki 
286d1588599SWarner Losh 			if (info) {
287d1588599SWarner Losh 				info = 0;
288d1588599SWarner Losh 				(void)printf("\t%s\n", p->fts_path);
289d1588599SWarner Losh 			}
2909b50d902SRodney W. Grimes 			break;
2919b50d902SRodney W. Grimes 		case FTS_DC:			/* Ignore. */
2929b50d902SRodney W. Grimes 			break;
2939b50d902SRodney W. Grimes 		case FTS_DNR:			/* Warn, continue. */
2949b50d902SRodney W. Grimes 		case FTS_ERR:
2959b50d902SRodney W. Grimes 		case FTS_NS:
296b2b8fa1aSNathan Huff 			xo_warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
2979b50d902SRodney W. Grimes 			rval = 1;
2989b50d902SRodney W. Grimes 			break;
299*74a9aac4SMichal Scigocki 		default:			/* All other files */
3004bb69e35SPeter Pentchev 			if (ignorep(p))
3014bb69e35SPeter Pentchev 				break;
3024bb69e35SPeter Pentchev 
30340850176SMax Laier 			if (lflag == 0 && p->fts_statp->st_nlink > 1 &&
30440850176SMax Laier 			    linkchk(p))
3059b50d902SRodney W. Grimes 				break;
3061aa8a0a9SJordan K. Hubbard 
307*74a9aac4SMichal Scigocki 			record_file_size(p);
308fd543f27SMax Laier 
309*74a9aac4SMichal Scigocki 			if ((aflag || p->fts_level == 0) && check_threshold(p))
310*74a9aac4SMichal Scigocki 				print_file_size(p);
31154aa1771SMichael Haro 		}
312*74a9aac4SMichal Scigocki 		savednumber = p->fts_parent->fts_number;
3131aa8a0a9SJordan K. Hubbard 	}
314b2b8fa1aSNathan Huff 	xo_close_list("paths");
3151aa8a0a9SJordan K. Hubbard 
3169b50d902SRodney W. Grimes 	if (errno)
317b2b8fa1aSNathan Huff 		xo_err(1, "fts_read");
3181aa8a0a9SJordan K. Hubbard 
319884bd44cSPhilippe Charnier 	if (cflag) {
320476c601bSKyle Evans 		if (hflag > 0) {
321b2b8fa1aSNathan Huff 			prthumanval("{:total-blocks/%4s}\ttotal\n",
322b2b8fa1aSNathan Huff 			    savednumber);
32354aa1771SMichael Haro 		} else {
324b2b8fa1aSNathan Huff 			xo_emit("{:total-blocks/%jd}\ttotal\n",
325b2b8fa1aSNathan Huff 			    (intmax_t)howmany(
326fd543f27SMax Laier 			    savednumber * cblocksize, blocksize));
32754aa1771SMichael Haro 		}
328884bd44cSPhilippe Charnier 	}
3291aa8a0a9SJordan K. Hubbard 
3304bb69e35SPeter Pentchev 	ignoreclean();
331b2b8fa1aSNathan Huff 	xo_close_container("disk-usage-information");
332b2b8fa1aSNathan Huff 	if (xo_finish() < 0)
333b2b8fa1aSNathan Huff 		xo_err(1, "stdout");
3345d712866SWolfram Schneider 	exit(rval);
3359b50d902SRodney W. Grimes }
3369b50d902SRodney W. Grimes 
337231a9731STim Kientzle static int
linkchk(FTSENT * p)338c10b37bfSDavid Malone linkchk(FTSENT *p)
3399b50d902SRodney W. Grimes {
340231a9731STim Kientzle 	struct links_entry {
341231a9731STim Kientzle 		struct links_entry *next;
342231a9731STim Kientzle 		struct links_entry *previous;
343231a9731STim Kientzle 		int	 links;
3449b50d902SRodney W. Grimes 		dev_t	 dev;
345231a9731STim Kientzle 		ino_t	 ino;
346231a9731STim Kientzle 	};
3472622aab4STim Kientzle 	static const size_t links_hash_initial_size = 8192;
348231a9731STim Kientzle 	static struct links_entry **buckets;
349231a9731STim Kientzle 	static struct links_entry *free_list;
3502622aab4STim Kientzle 	static size_t number_buckets;
3512622aab4STim Kientzle 	static unsigned long number_entries;
352231a9731STim Kientzle 	static char stop_allocating;
353231a9731STim Kientzle 	struct links_entry *le, **new_buckets;
354231a9731STim Kientzle 	struct stat *st;
355231a9731STim Kientzle 	size_t i, new_size;
3566c97c3d1SStefan Farfeleder 	int hash;
357231a9731STim Kientzle 
358231a9731STim Kientzle 	st = p->fts_statp;
359231a9731STim Kientzle 
360231a9731STim Kientzle 	/* If necessary, initialize the hash table. */
361231a9731STim Kientzle 	if (buckets == NULL) {
362231a9731STim Kientzle 		number_buckets = links_hash_initial_size;
363231a9731STim Kientzle 		buckets = malloc(number_buckets * sizeof(buckets[0]));
364231a9731STim Kientzle 		if (buckets == NULL)
365aef39ef7STim Kientzle 			errx(1, "No memory for hardlink detection");
366231a9731STim Kientzle 		for (i = 0; i < number_buckets; i++)
367231a9731STim Kientzle 			buckets[i] = NULL;
368231a9731STim Kientzle 	}
369231a9731STim Kientzle 
370231a9731STim Kientzle 	/* If the hash table is getting too full, enlarge it. */
371231a9731STim Kientzle 	if (number_entries > number_buckets * 10 && !stop_allocating) {
372231a9731STim Kientzle 		new_size = number_buckets * 2;
3738c5a59eeSPedro F. Giffuni 		new_buckets = calloc(new_size, sizeof(struct links_entry *));
374231a9731STim Kientzle 
375231a9731STim Kientzle 		/* Try releasing the free list to see if that helps. */
376231a9731STim Kientzle 		if (new_buckets == NULL && free_list != NULL) {
377231a9731STim Kientzle 			while (free_list != NULL) {
378231a9731STim Kientzle 				le = free_list;
379231a9731STim Kientzle 				free_list = le->next;
380231a9731STim Kientzle 				free(le);
381231a9731STim Kientzle 			}
3828c5a59eeSPedro F. Giffuni 			new_buckets = calloc(new_size, sizeof(new_buckets[0]));
383231a9731STim Kientzle 		}
384231a9731STim Kientzle 
385231a9731STim Kientzle 		if (new_buckets == NULL) {
386231a9731STim Kientzle 			stop_allocating = 1;
387b2b8fa1aSNathan Huff 			xo_warnx("No more memory for tracking hard links");
388231a9731STim Kientzle 		} else {
389231a9731STim Kientzle 			for (i = 0; i < number_buckets; i++) {
390231a9731STim Kientzle 				while (buckets[i] != NULL) {
391231a9731STim Kientzle 					/* Remove entry from old bucket. */
392231a9731STim Kientzle 					le = buckets[i];
393231a9731STim Kientzle 					buckets[i] = le->next;
394231a9731STim Kientzle 
395231a9731STim Kientzle 					/* Add entry to new bucket. */
396231a9731STim Kientzle 					hash = (le->dev ^ le->ino) % new_size;
397231a9731STim Kientzle 
398231a9731STim Kientzle 					if (new_buckets[hash] != NULL)
399231a9731STim Kientzle 						new_buckets[hash]->previous =
400231a9731STim Kientzle 						    le;
401231a9731STim Kientzle 					le->next = new_buckets[hash];
402231a9731STim Kientzle 					le->previous = NULL;
403231a9731STim Kientzle 					new_buckets[hash] = le;
404231a9731STim Kientzle 				}
405231a9731STim Kientzle 			}
406231a9731STim Kientzle 			free(buckets);
407231a9731STim Kientzle 			buckets = new_buckets;
408231a9731STim Kientzle 			number_buckets = new_size;
409231a9731STim Kientzle 		}
410231a9731STim Kientzle 	}
411231a9731STim Kientzle 
412231a9731STim Kientzle 	/* Try to locate this entry in the hash table. */
413231a9731STim Kientzle 	hash = (st->st_dev ^ st->st_ino) % number_buckets;
414231a9731STim Kientzle 	for (le = buckets[hash]; le != NULL; le = le->next) {
415231a9731STim Kientzle 		if (le->dev == st->st_dev && le->ino == st->st_ino) {
416231a9731STim Kientzle 			/*
417231a9731STim Kientzle 			 * Save memory by releasing an entry when we've seen
41828323addSBryan Drewery 			 * all of its links.
419231a9731STim Kientzle 			 */
420231a9731STim Kientzle 			if (--le->links <= 0) {
421231a9731STim Kientzle 				if (le->previous != NULL)
422231a9731STim Kientzle 					le->previous->next = le->next;
423231a9731STim Kientzle 				if (le->next != NULL)
424231a9731STim Kientzle 					le->next->previous = le->previous;
425231a9731STim Kientzle 				if (buckets[hash] == le)
426231a9731STim Kientzle 					buckets[hash] = le->next;
427231a9731STim Kientzle 				number_entries--;
428231a9731STim Kientzle 				/* Recycle this node through the free list */
429231a9731STim Kientzle 				if (stop_allocating) {
430231a9731STim Kientzle 					free(le);
431231a9731STim Kientzle 				} else {
432231a9731STim Kientzle 					le->next = free_list;
433231a9731STim Kientzle 					free_list = le;
434231a9731STim Kientzle 				}
435231a9731STim Kientzle 			}
4369b50d902SRodney W. Grimes 			return (1);
437231a9731STim Kientzle 		}
438231a9731STim Kientzle 	}
4399b50d902SRodney W. Grimes 
440231a9731STim Kientzle 	if (stop_allocating)
441231a9731STim Kientzle 		return (0);
442231a9731STim Kientzle 
443231a9731STim Kientzle 	/* Add this entry to the links cache. */
444231a9731STim Kientzle 	if (free_list != NULL) {
445231a9731STim Kientzle 		/* Pull a node from the free list if we can. */
446231a9731STim Kientzle 		le = free_list;
447231a9731STim Kientzle 		free_list = le->next;
448231a9731STim Kientzle 	} else
449231a9731STim Kientzle 		/* Malloc one if we have to. */
450231a9731STim Kientzle 		le = malloc(sizeof(struct links_entry));
451231a9731STim Kientzle 	if (le == NULL) {
452231a9731STim Kientzle 		stop_allocating = 1;
453b2b8fa1aSNathan Huff 		xo_warnx("No more memory for tracking hard links");
454231a9731STim Kientzle 		return (0);
455231a9731STim Kientzle 	}
456231a9731STim Kientzle 	le->dev = st->st_dev;
457231a9731STim Kientzle 	le->ino = st->st_ino;
458231a9731STim Kientzle 	le->links = st->st_nlink - 1;
459231a9731STim Kientzle 	number_entries++;
460231a9731STim Kientzle 	le->next = buckets[hash];
461231a9731STim Kientzle 	le->previous = NULL;
462231a9731STim Kientzle 	if (buckets[hash] != NULL)
463231a9731STim Kientzle 		buckets[hash]->previous = le;
464231a9731STim Kientzle 	buckets[hash] = le;
4659b50d902SRodney W. Grimes 	return (0);
4669b50d902SRodney W. Grimes }
4679b50d902SRodney W. Grimes 
4680551897aSMax Laier static void
prthumanval(const char * fmt,int64_t bytes)469b2b8fa1aSNathan Huff prthumanval(const char *fmt, int64_t bytes)
47054aa1771SMichael Haro {
4717d3940bbSPawel Jakub Dawidek 	char buf[5];
472476c601bSKyle Evans 	int flags;
47354aa1771SMichael Haro 
474fd543f27SMax Laier 	bytes *= cblocksize;
475476c601bSKyle Evans 	flags = HN_B | HN_NOSPACE | HN_DECIMAL;
476fd543f27SMax Laier 	if (!Aflag)
4777d3940bbSPawel Jakub Dawidek 		bytes *= DEV_BSIZE;
478476c601bSKyle Evans 	if (hflag == UNITS_SI)
479476c601bSKyle Evans 		flags |= HN_DIVISOR_1000;
48054aa1771SMichael Haro 
481476c601bSKyle Evans 	humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, flags);
4827d3940bbSPawel Jakub Dawidek 
483b2b8fa1aSNathan Huff 	xo_emit(fmt, buf);
48454aa1771SMichael Haro }
48554aa1771SMichael Haro 
4861aa67f69SPhilippe Charnier static void
usage(void)487c10b37bfSDavid Malone usage(void)
4889b50d902SRodney W. Grimes {
489*74a9aac4SMichal Scigocki 	xo_error("%s\n%s\n%s\n",
490*74a9aac4SMichal Scigocki 	    "usage: du [--libxo] [-Aclnx] [-H | -L | -P] [-g | -h | -k | -m]",
491*74a9aac4SMichal Scigocki 	    "          [-a | -s | -d depth] [-B blocksize] [-I mask] [-t threshold]",
492*74a9aac4SMichal Scigocki 	    "          [file ...]");
49354aa1771SMichael Haro 	exit(EX_USAGE);
4949b50d902SRodney W. Grimes }
4954bb69e35SPeter Pentchev 
4960551897aSMax Laier static void
ignoreadd(const char * mask)497c10b37bfSDavid Malone ignoreadd(const char *mask)
4984bb69e35SPeter Pentchev {
4994bb69e35SPeter Pentchev 	struct ignentry *ign;
5004bb69e35SPeter Pentchev 
5014bb69e35SPeter Pentchev 	ign = calloc(1, sizeof(*ign));
5024bb69e35SPeter Pentchev 	if (ign == NULL)
5034bb69e35SPeter Pentchev 		errx(1, "cannot allocate memory");
5044bb69e35SPeter Pentchev 	ign->mask = strdup(mask);
5054bb69e35SPeter Pentchev 	if (ign->mask == NULL)
5064bb69e35SPeter Pentchev 		errx(1, "cannot allocate memory");
5074bb69e35SPeter Pentchev 	SLIST_INSERT_HEAD(&ignores, ign, next);
5084bb69e35SPeter Pentchev }
5094bb69e35SPeter Pentchev 
5100551897aSMax Laier static void
ignoreclean(void)511c10b37bfSDavid Malone ignoreclean(void)
5124bb69e35SPeter Pentchev {
5134bb69e35SPeter Pentchev 	struct ignentry *ign;
5144bb69e35SPeter Pentchev 
5154bb69e35SPeter Pentchev 	while (!SLIST_EMPTY(&ignores)) {
5164bb69e35SPeter Pentchev 		ign = SLIST_FIRST(&ignores);
5174bb69e35SPeter Pentchev 		SLIST_REMOVE_HEAD(&ignores, next);
5184bb69e35SPeter Pentchev 		free(ign->mask);
5194bb69e35SPeter Pentchev 		free(ign);
5204bb69e35SPeter Pentchev 	}
5214bb69e35SPeter Pentchev }
5224bb69e35SPeter Pentchev 
5230551897aSMax Laier static int
ignorep(FTSENT * ent)524c10b37bfSDavid Malone ignorep(FTSENT *ent)
5254bb69e35SPeter Pentchev {
5264bb69e35SPeter Pentchev 	struct ignentry *ign;
5274bb69e35SPeter Pentchev 
528fadc7151SMaxim Konovalov 	if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
529*74a9aac4SMichal Scigocki 		return (1);
5304bb69e35SPeter Pentchev 	SLIST_FOREACH(ign, &ignores, next)
5314bb69e35SPeter Pentchev 		if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
532*74a9aac4SMichal Scigocki 			return (1);
533*74a9aac4SMichal Scigocki 	return (0);
5344bb69e35SPeter Pentchev }
535d1588599SWarner Losh 
536d1588599SWarner Losh static void
siginfo(int sig __unused)537d1588599SWarner Losh siginfo(int sig __unused)
538d1588599SWarner Losh {
539d1588599SWarner Losh 	info = 1;
540d1588599SWarner Losh }
541*74a9aac4SMichal Scigocki 
542*74a9aac4SMichal Scigocki /*
543*74a9aac4SMichal Scigocki  * Record the total disk/block size of the file or directory. The fts_number
544*74a9aac4SMichal Scigocki  * variable provided in FTSENT is used for keeping track of the total size.
545*74a9aac4SMichal Scigocki  * See FTS(3).
546*74a9aac4SMichal Scigocki  */
547*74a9aac4SMichal Scigocki static void
record_file_size(FTSENT * p)548*74a9aac4SMichal Scigocki record_file_size(FTSENT *p)
549*74a9aac4SMichal Scigocki {
550*74a9aac4SMichal Scigocki 	p->fts_number += Aflag ?
551*74a9aac4SMichal Scigocki 	    howmany(p->fts_statp->st_size, cblocksize) :
552*74a9aac4SMichal Scigocki 	    howmany(p->fts_statp->st_blocks, cblocksize);
553*74a9aac4SMichal Scigocki 
554*74a9aac4SMichal Scigocki 	p->fts_parent->fts_number += p->fts_number;
555*74a9aac4SMichal Scigocki }
556*74a9aac4SMichal Scigocki 
557*74a9aac4SMichal Scigocki static bool
check_threshold(FTSENT * p)558*74a9aac4SMichal Scigocki check_threshold(FTSENT *p)
559*74a9aac4SMichal Scigocki {
560*74a9aac4SMichal Scigocki 	return (threshold <= threshold_sign *
561*74a9aac4SMichal Scigocki 	    howmany(p->fts_number * cblocksize, blocksize));
562*74a9aac4SMichal Scigocki }
563*74a9aac4SMichal Scigocki 
564*74a9aac4SMichal Scigocki static void
print_file_size(FTSENT * p)565*74a9aac4SMichal Scigocki print_file_size(FTSENT *p)
566*74a9aac4SMichal Scigocki {
567*74a9aac4SMichal Scigocki 	xo_open_instance("paths");
568*74a9aac4SMichal Scigocki 	if (hflag > 0) {
569*74a9aac4SMichal Scigocki 		prthumanval("{:blocks/%4s}", p->fts_number);
570*74a9aac4SMichal Scigocki 		xo_emit("\t{:path/%s}\n", p->fts_path);
571*74a9aac4SMichal Scigocki 	} else {
572*74a9aac4SMichal Scigocki 		xo_emit("{:blocks/%jd}\t{:path/%s}\n",
573*74a9aac4SMichal Scigocki 		    (intmax_t)howmany(p->fts_number * cblocksize, blocksize),
574*74a9aac4SMichal Scigocki 		p->fts_path);
575*74a9aac4SMichal Scigocki 	}
576*74a9aac4SMichal Scigocki 	xo_close_instance("paths");
577*74a9aac4SMichal Scigocki }
578