2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1989, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Chris Newcomb.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-12-06 22:58:23 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1989, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
2000-01-25 17:46:59 +00:00
|
|
|
#if 0
|
1998-12-06 22:58:23 +00:00
|
|
|
static const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
|
2000-01-25 17:46:59 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:25:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1997-12-29 20:56:21 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/param.h>
|
2001-06-13 06:38:58 +00:00
|
|
|
#include <sys/queue.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2001-06-13 06:38:58 +00:00
|
|
|
#include <fnmatch.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <fts.h>
|
2017-08-09 01:19:19 +00:00
|
|
|
#include <getopt.h>
|
2004-05-24 22:22:29 +00:00
|
|
|
#include <libutil.h>
|
2004-07-15 08:54:41 +00:00
|
|
|
#include <locale.h>
|
2005-01-07 00:12:24 +00:00
|
|
|
#include <stdint.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-01-25 17:46:59 +00:00
|
|
|
#include <sysexits.h>
|
1997-03-11 13:43:33 +00:00
|
|
|
#include <unistd.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2017-08-09 01:19:19 +00:00
|
|
|
#define SI_OPT (CHAR_MAX + 1)
|
|
|
|
|
|
|
|
#define UNITS_2 1
|
|
|
|
#define UNITS_SI 2
|
|
|
|
|
2011-11-06 08:15:17 +00:00
|
|
|
static SLIST_HEAD(ignhead, ignentry) ignores;
|
2001-06-13 06:38:58 +00:00
|
|
|
struct ignentry {
|
|
|
|
char *mask;
|
|
|
|
SLIST_ENTRY(ignentry) next;
|
|
|
|
};
|
|
|
|
|
2004-04-30 18:17:51 +00:00
|
|
|
static int linkchk(FTSENT *);
|
2002-03-22 01:22:50 +00:00
|
|
|
static void usage(void);
|
2008-11-04 19:23:48 +00:00
|
|
|
static void prthumanval(int64_t);
|
|
|
|
static void ignoreadd(const char *);
|
|
|
|
static void ignoreclean(void);
|
|
|
|
static int ignorep(FTSENT *);
|
2009-04-30 01:24:53 +00:00
|
|
|
static void siginfo(int __unused);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2008-11-04 19:23:48 +00:00
|
|
|
static int nodumpflag = 0;
|
2017-08-09 01:19:19 +00:00
|
|
|
static int Aflag, hflag;
|
2008-11-06 16:30:38 +00:00
|
|
|
static long blocksize, cblocksize;
|
2009-04-30 01:24:53 +00:00
|
|
|
static volatile sig_atomic_t info;
|
2006-05-06 22:04:59 +00:00
|
|
|
|
2017-08-09 01:19:19 +00:00
|
|
|
static const struct option long_options[] =
|
|
|
|
{
|
|
|
|
{ "si", no_argument, NULL, SI_OPT },
|
|
|
|
{ NULL, no_argument, NULL, 0 },
|
|
|
|
};
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
int
|
2002-07-28 15:50:38 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
1997-12-29 20:56:21 +00:00
|
|
|
FTS *fts;
|
|
|
|
FTSENT *p;
|
2008-11-06 16:30:38 +00:00
|
|
|
off_t savednumber, curblocks;
|
2010-06-20 08:27:03 +00:00
|
|
|
off_t threshold, threshold_sign;
|
1997-12-29 20:56:21 +00:00
|
|
|
int ftsoptions;
|
|
|
|
int depth;
|
2011-12-17 23:18:14 +00:00
|
|
|
int Hflag, Lflag, aflag, sflag, dflag, cflag;
|
2017-08-09 01:19:19 +00:00
|
|
|
int lflag, ch, notused, rval;
|
1997-12-29 20:56:21 +00:00
|
|
|
char **save;
|
2001-12-02 13:48:40 +00:00
|
|
|
static char dot[] = ".";
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-07-15 08:54:41 +00:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
2017-08-09 01:19:19 +00:00
|
|
|
Hflag = Lflag = aflag = sflag = dflag = cflag = lflag = Aflag = 0;
|
2004-04-30 18:17:51 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
save = argv;
|
2011-12-17 23:18:14 +00:00
|
|
|
ftsoptions = FTS_PHYSICAL;
|
2008-11-04 19:17:32 +00:00
|
|
|
savednumber = 0;
|
2010-06-20 08:27:03 +00:00
|
|
|
threshold = 0;
|
|
|
|
threshold_sign = 1;
|
2008-11-06 16:30:38 +00:00
|
|
|
cblocksize = DEV_BSIZE;
|
|
|
|
blocksize = 0;
|
1996-10-23 06:53:57 +00:00
|
|
|
depth = INT_MAX;
|
2001-06-13 06:38:58 +00:00
|
|
|
SLIST_INIT(&ignores);
|
2004-04-30 18:17:51 +00:00
|
|
|
|
2017-08-09 01:19:19 +00:00
|
|
|
while ((ch = getopt_long(argc, argv, "+AB:HI:LPasd:cghklmnrt:x",
|
|
|
|
long_options, NULL)) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (ch) {
|
2008-11-06 16:30:38 +00:00
|
|
|
case 'A':
|
|
|
|
Aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
errno = 0;
|
|
|
|
cblocksize = atoi(optarg);
|
|
|
|
if (errno == ERANGE || cblocksize <= 0) {
|
|
|
|
warnx("invalid argument to option B: %s",
|
|
|
|
optarg);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
case 'H':
|
|
|
|
Hflag = 1;
|
2011-12-17 23:18:14 +00:00
|
|
|
Lflag = 0;
|
2008-11-04 19:17:32 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
ignoreadd(optarg);
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
Lflag = 1;
|
2011-12-17 23:18:14 +00:00
|
|
|
Hflag = 0;
|
2008-11-04 19:17:32 +00:00
|
|
|
break;
|
|
|
|
case 'P':
|
2011-12-17 23:18:14 +00:00
|
|
|
Hflag = Lflag = 0;
|
2008-11-04 19:17:32 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
errno = 0;
|
|
|
|
depth = atoi(optarg);
|
|
|
|
if (errno == ERANGE || depth < 0) {
|
|
|
|
warnx("invalid argument to option d: %s",
|
|
|
|
optarg);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
2012-07-18 19:28:22 +00:00
|
|
|
case 'g':
|
|
|
|
hflag = 0;
|
|
|
|
blocksize = 1073741824;
|
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
case 'h':
|
2017-08-09 01:19:19 +00:00
|
|
|
hflag = UNITS_2;
|
2008-11-04 19:17:32 +00:00
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
hflag = 0;
|
2008-11-06 16:30:38 +00:00
|
|
|
blocksize = 1024;
|
2008-11-04 19:17:32 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
hflag = 0;
|
2008-11-06 16:30:38 +00:00
|
|
|
blocksize = 1048576;
|
2008-11-04 19:17:32 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nodumpflag = 1;
|
|
|
|
break;
|
|
|
|
case 'r': /* Compatibility. */
|
|
|
|
break;
|
2010-06-20 08:27:03 +00:00
|
|
|
case 't' :
|
|
|
|
if (expand_number(optarg, &threshold) != 0 ||
|
|
|
|
threshold == 0) {
|
|
|
|
warnx("invalid threshold: %s", optarg);
|
|
|
|
usage();
|
|
|
|
} else if (threshold < 0)
|
|
|
|
threshold_sign = -1;
|
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
case 'x':
|
|
|
|
ftsoptions |= FTS_XDEV;
|
|
|
|
break;
|
2017-08-09 01:19:19 +00:00
|
|
|
case SI_OPT:
|
|
|
|
hflag = UNITS_SI;
|
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* Because of the way that fts(3) works, logical walks will not count
|
|
|
|
* the blocks actually used by symbolic links. We rationalize this by
|
|
|
|
* noting that users computing logical sizes are likely to do logical
|
|
|
|
* copies, so not counting the links is correct. The real reason is
|
|
|
|
* that we'd have to re-implement the kernel's symbolic link traversing
|
|
|
|
* algorithm to get this right. If, for example, you have relative
|
|
|
|
* symbolic links referencing other relative symbolic links, it gets
|
|
|
|
* very nasty, very fast. The bottom line is that it's documented in
|
|
|
|
* the man page, so it's a feature.
|
|
|
|
*/
|
1997-12-29 20:56:21 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if (Hflag)
|
|
|
|
ftsoptions |= FTS_COMFOLLOW;
|
2011-12-17 23:18:14 +00:00
|
|
|
if (Lflag) {
|
|
|
|
ftsoptions &= ~FTS_PHYSICAL;
|
1994-05-27 12:33:43 +00:00
|
|
|
ftsoptions |= FTS_LOGICAL;
|
2011-12-17 23:18:14 +00:00
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
if (!Aflag && (cblocksize % DEV_BSIZE) != 0)
|
|
|
|
cblocksize = howmany(cblocksize, DEV_BSIZE) * DEV_BSIZE;
|
|
|
|
|
2011-11-08 11:36:46 +00:00
|
|
|
if (aflag + dflag + sflag > 1)
|
|
|
|
usage();
|
|
|
|
if (sflag)
|
1997-12-29 20:56:21 +00:00
|
|
|
depth = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (!*argv) {
|
|
|
|
argv = save;
|
2001-12-02 13:48:40 +00:00
|
|
|
argv[0] = dot;
|
1994-05-27 12:33:43 +00:00
|
|
|
argv[1] = NULL;
|
|
|
|
}
|
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
if (blocksize == 0)
|
|
|
|
(void)getbsize(¬used, &blocksize);
|
|
|
|
|
|
|
|
if (!Aflag) {
|
|
|
|
cblocksize /= DEV_BSIZE;
|
|
|
|
blocksize /= DEV_BSIZE;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2010-06-20 08:27:03 +00:00
|
|
|
if (threshold != 0)
|
|
|
|
threshold = howmany(threshold / DEV_BSIZE * cblocksize,
|
|
|
|
blocksize);
|
|
|
|
|
1997-12-29 20:56:21 +00:00
|
|
|
rval = 0;
|
2004-04-30 18:17:51 +00:00
|
|
|
|
2009-04-30 01:24:53 +00:00
|
|
|
(void)signal(SIGINFO, siginfo);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
|
1997-12-29 20:56:21 +00:00
|
|
|
err(1, "fts_open");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2020-12-08 23:38:26 +00:00
|
|
|
while (errno = 0, (p = fts_read(fts)) != NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (p->fts_info) {
|
2008-11-04 19:17:32 +00:00
|
|
|
case FTS_D: /* Ignore. */
|
|
|
|
if (ignorep(p))
|
|
|
|
fts_set(fts, p, FTS_SKIP);
|
|
|
|
break;
|
|
|
|
case FTS_DP:
|
|
|
|
if (ignorep(p))
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
curblocks = Aflag ?
|
|
|
|
howmany(p->fts_statp->st_size, cblocksize) :
|
|
|
|
howmany(p->fts_statp->st_blocks, cblocksize);
|
2008-11-04 19:17:32 +00:00
|
|
|
p->fts_parent->fts_bignum += p->fts_bignum +=
|
2008-11-06 16:30:38 +00:00
|
|
|
curblocks;
|
2008-11-04 19:17:32 +00:00
|
|
|
|
2010-06-20 08:27:03 +00:00
|
|
|
if (p->fts_level <= depth && threshold <=
|
|
|
|
threshold_sign * howmany(p->fts_bignum *
|
|
|
|
cblocksize, blocksize)) {
|
2017-08-09 01:19:19 +00:00
|
|
|
if (hflag > 0) {
|
2008-11-06 16:30:38 +00:00
|
|
|
prthumanval(p->fts_bignum);
|
2008-11-04 19:17:32 +00:00
|
|
|
(void)printf("\t%s\n", p->fts_path);
|
|
|
|
} else {
|
|
|
|
(void)printf("%jd\t%s\n",
|
2008-11-06 23:55:28 +00:00
|
|
|
(intmax_t)howmany(p->fts_bignum *
|
|
|
|
cblocksize, blocksize),
|
|
|
|
p->fts_path);
|
2000-03-26 14:21:57 +00:00
|
|
|
}
|
2008-11-04 19:17:32 +00:00
|
|
|
}
|
2009-04-30 01:24:53 +00:00
|
|
|
if (info) {
|
|
|
|
info = 0;
|
|
|
|
(void)printf("\t%s\n", p->fts_path);
|
|
|
|
}
|
2008-11-04 19:17:32 +00:00
|
|
|
break;
|
|
|
|
case FTS_DC: /* Ignore. */
|
|
|
|
break;
|
|
|
|
case FTS_DNR: /* Warn, continue. */
|
|
|
|
case FTS_ERR:
|
|
|
|
case FTS_NS:
|
|
|
|
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
|
|
|
|
rval = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (ignorep(p))
|
1997-12-29 20:56:21 +00:00
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
|
|
|
|
if (lflag == 0 && p->fts_statp->st_nlink > 1 &&
|
|
|
|
linkchk(p))
|
1997-12-29 20:56:21 +00:00
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
curblocks = Aflag ?
|
|
|
|
howmany(p->fts_statp->st_size, cblocksize) :
|
|
|
|
howmany(p->fts_statp->st_blocks, cblocksize);
|
|
|
|
|
2011-11-08 11:36:46 +00:00
|
|
|
if (aflag || p->fts_level == 0) {
|
2017-08-09 01:19:19 +00:00
|
|
|
if (hflag > 0) {
|
2008-11-06 16:30:38 +00:00
|
|
|
prthumanval(curblocks);
|
2008-11-04 19:17:32 +00:00
|
|
|
(void)printf("\t%s\n", p->fts_path);
|
|
|
|
} else {
|
|
|
|
(void)printf("%jd\t%s\n",
|
2008-11-06 23:55:28 +00:00
|
|
|
(intmax_t)howmany(curblocks *
|
|
|
|
cblocksize, blocksize),
|
|
|
|
p->fts_path);
|
2000-03-26 14:21:57 +00:00
|
|
|
}
|
2008-11-04 19:17:32 +00:00
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
p->fts_parent->fts_bignum += curblocks;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-01-07 00:12:24 +00:00
|
|
|
savednumber = p->fts_parent->fts_bignum;
|
1997-12-29 20:56:21 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if (errno)
|
|
|
|
err(1, "fts_read");
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2000-03-26 14:21:57 +00:00
|
|
|
if (cflag) {
|
2017-08-09 01:19:19 +00:00
|
|
|
if (hflag > 0) {
|
2008-11-06 16:30:38 +00:00
|
|
|
prthumanval(savednumber);
|
2008-11-04 19:17:32 +00:00
|
|
|
(void)printf("\ttotal\n");
|
2000-01-25 17:46:59 +00:00
|
|
|
} else {
|
2008-11-04 19:17:32 +00:00
|
|
|
(void)printf("%jd\ttotal\n", (intmax_t)howmany(
|
2008-11-06 16:30:38 +00:00
|
|
|
savednumber * cblocksize, blocksize));
|
2000-01-25 17:46:59 +00:00
|
|
|
}
|
2000-03-26 14:21:57 +00:00
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2001-06-13 06:38:58 +00:00
|
|
|
ignoreclean();
|
1997-08-29 11:40:00 +00:00
|
|
|
exit(rval);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2004-04-30 18:17:51 +00:00
|
|
|
static int
|
|
|
|
linkchk(FTSENT *p)
|
|
|
|
{
|
|
|
|
struct links_entry {
|
2004-05-01 21:47:31 +00:00
|
|
|
struct links_entry *next;
|
|
|
|
struct links_entry *previous;
|
|
|
|
int links;
|
|
|
|
dev_t dev;
|
|
|
|
ino_t ino;
|
2004-04-30 18:17:51 +00:00
|
|
|
};
|
2004-05-01 21:47:31 +00:00
|
|
|
static const size_t links_hash_initial_size = 8192;
|
|
|
|
static struct links_entry **buckets;
|
|
|
|
static struct links_entry *free_list;
|
|
|
|
static size_t number_buckets;
|
|
|
|
static unsigned long number_entries;
|
|
|
|
static char stop_allocating;
|
|
|
|
struct links_entry *le, **new_buckets;
|
|
|
|
struct stat *st;
|
|
|
|
size_t i, new_size;
|
2005-04-09 14:31:41 +00:00
|
|
|
int hash;
|
2004-04-30 18:17:51 +00:00
|
|
|
|
|
|
|
st = p->fts_statp;
|
|
|
|
|
|
|
|
/* If necessary, initialize the hash table. */
|
|
|
|
if (buckets == NULL) {
|
|
|
|
number_buckets = links_hash_initial_size;
|
|
|
|
buckets = malloc(number_buckets * sizeof(buckets[0]));
|
|
|
|
if (buckets == NULL)
|
2004-05-02 17:54:57 +00:00
|
|
|
errx(1, "No memory for hardlink detection");
|
2004-04-30 18:17:51 +00:00
|
|
|
for (i = 0; i < number_buckets; i++)
|
|
|
|
buckets[i] = NULL;
|
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2004-04-30 18:17:51 +00:00
|
|
|
/* If the hash table is getting too full, enlarge it. */
|
|
|
|
if (number_entries > number_buckets * 10 && !stop_allocating) {
|
|
|
|
new_size = number_buckets * 2;
|
2015-02-17 21:12:45 +00:00
|
|
|
new_buckets = calloc(new_size, sizeof(struct links_entry *));
|
2004-04-30 18:17:51 +00:00
|
|
|
|
|
|
|
/* Try releasing the free list to see if that helps. */
|
|
|
|
if (new_buckets == NULL && free_list != NULL) {
|
|
|
|
while (free_list != NULL) {
|
|
|
|
le = free_list;
|
|
|
|
free_list = le->next;
|
|
|
|
free(le);
|
|
|
|
}
|
2015-02-17 21:12:45 +00:00
|
|
|
new_buckets = calloc(new_size, sizeof(new_buckets[0]));
|
2004-04-30 18:17:51 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-04-30 18:17:51 +00:00
|
|
|
if (new_buckets == NULL) {
|
|
|
|
stop_allocating = 1;
|
2004-05-02 17:54:57 +00:00
|
|
|
warnx("No more memory for tracking hard links");
|
2004-04-30 18:17:51 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < number_buckets; i++) {
|
|
|
|
while (buckets[i] != NULL) {
|
|
|
|
/* Remove entry from old bucket. */
|
|
|
|
le = buckets[i];
|
|
|
|
buckets[i] = le->next;
|
|
|
|
|
|
|
|
/* Add entry to new bucket. */
|
|
|
|
hash = (le->dev ^ le->ino) % new_size;
|
|
|
|
|
|
|
|
if (new_buckets[hash] != NULL)
|
|
|
|
new_buckets[hash]->previous =
|
|
|
|
le;
|
|
|
|
le->next = new_buckets[hash];
|
|
|
|
le->previous = NULL;
|
|
|
|
new_buckets[hash] = le;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(buckets);
|
|
|
|
buckets = new_buckets;
|
|
|
|
number_buckets = new_size;
|
|
|
|
}
|
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2004-04-30 18:17:51 +00:00
|
|
|
/* Try to locate this entry in the hash table. */
|
|
|
|
hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
|
|
|
|
for (le = buckets[hash]; le != NULL; le = le->next) {
|
|
|
|
if (le->dev == st->st_dev && le->ino == st->st_ino) {
|
|
|
|
/*
|
|
|
|
* Save memory by releasing an entry when we've seen
|
2016-11-08 23:59:41 +00:00
|
|
|
* all of its links.
|
2004-04-30 18:17:51 +00:00
|
|
|
*/
|
|
|
|
if (--le->links <= 0) {
|
|
|
|
if (le->previous != NULL)
|
|
|
|
le->previous->next = le->next;
|
|
|
|
if (le->next != NULL)
|
|
|
|
le->next->previous = le->previous;
|
|
|
|
if (buckets[hash] == le)
|
|
|
|
buckets[hash] = le->next;
|
|
|
|
number_entries--;
|
|
|
|
/* Recycle this node through the free list */
|
|
|
|
if (stop_allocating) {
|
|
|
|
free(le);
|
|
|
|
} else {
|
|
|
|
le->next = free_list;
|
|
|
|
free_list = le;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stop_allocating)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* Add this entry to the links cache. */
|
|
|
|
if (free_list != NULL) {
|
|
|
|
/* Pull a node from the free list if we can. */
|
|
|
|
le = free_list;
|
|
|
|
free_list = le->next;
|
|
|
|
} else
|
|
|
|
/* Malloc one if we have to. */
|
|
|
|
le = malloc(sizeof(struct links_entry));
|
|
|
|
if (le == NULL) {
|
|
|
|
stop_allocating = 1;
|
2004-05-02 17:54:57 +00:00
|
|
|
warnx("No more memory for tracking hard links");
|
2004-04-30 18:17:51 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
le->dev = st->st_dev;
|
|
|
|
le->ino = st->st_ino;
|
|
|
|
le->links = st->st_nlink - 1;
|
|
|
|
number_entries++;
|
|
|
|
le->next = buckets[hash];
|
|
|
|
le->previous = NULL;
|
|
|
|
if (buckets[hash] != NULL)
|
|
|
|
buckets[hash]->previous = le;
|
|
|
|
buckets[hash] = le;
|
1994-05-27 12:33:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-11-04 19:23:48 +00:00
|
|
|
static void
|
2004-05-24 22:22:29 +00:00
|
|
|
prthumanval(int64_t bytes)
|
2000-01-25 17:46:59 +00:00
|
|
|
{
|
2004-05-24 22:22:29 +00:00
|
|
|
char buf[5];
|
2017-08-09 01:19:19 +00:00
|
|
|
int flags;
|
2004-05-24 22:22:29 +00:00
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
bytes *= cblocksize;
|
2017-08-09 01:19:19 +00:00
|
|
|
flags = HN_B | HN_NOSPACE | HN_DECIMAL;
|
2008-11-06 16:30:38 +00:00
|
|
|
if (!Aflag)
|
|
|
|
bytes *= DEV_BSIZE;
|
2017-08-09 01:19:19 +00:00
|
|
|
if (hflag == UNITS_SI)
|
|
|
|
flags |= HN_DIVISOR_1000;
|
2000-01-25 17:46:59 +00:00
|
|
|
|
2017-08-09 01:19:19 +00:00
|
|
|
humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, flags);
|
2000-01-25 17:46:59 +00:00
|
|
|
|
2004-05-24 22:22:29 +00:00
|
|
|
(void)printf("%4s", buf);
|
2000-01-25 17:46:59 +00:00
|
|
|
}
|
|
|
|
|
1997-06-30 06:50:07 +00:00
|
|
|
static void
|
2002-07-28 15:50:38 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
2012-07-26 20:41:36 +00:00
|
|
|
"usage: du [-Aclnx] [-H | -L | -P] [-g | -h | -k | -m] "
|
2011-12-09 02:30:56 +00:00
|
|
|
"[-a | -s | -d depth] [-B blocksize] [-I mask] "
|
|
|
|
"[-t threshold] [file ...]\n");
|
2000-01-25 17:46:59 +00:00
|
|
|
exit(EX_USAGE);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-06-13 06:38:58 +00:00
|
|
|
|
2008-11-04 19:23:48 +00:00
|
|
|
static void
|
2002-07-28 15:50:38 +00:00
|
|
|
ignoreadd(const char *mask)
|
2001-06-13 06:38:58 +00:00
|
|
|
{
|
|
|
|
struct ignentry *ign;
|
|
|
|
|
|
|
|
ign = calloc(1, sizeof(*ign));
|
|
|
|
if (ign == NULL)
|
|
|
|
errx(1, "cannot allocate memory");
|
|
|
|
ign->mask = strdup(mask);
|
|
|
|
if (ign->mask == NULL)
|
|
|
|
errx(1, "cannot allocate memory");
|
|
|
|
SLIST_INSERT_HEAD(&ignores, ign, next);
|
|
|
|
}
|
|
|
|
|
2008-11-04 19:23:48 +00:00
|
|
|
static void
|
2002-07-28 15:50:38 +00:00
|
|
|
ignoreclean(void)
|
2001-06-13 06:38:58 +00:00
|
|
|
{
|
|
|
|
struct ignentry *ign;
|
2017-06-12 02:42:39 +00:00
|
|
|
|
2001-06-13 06:38:58 +00:00
|
|
|
while (!SLIST_EMPTY(&ignores)) {
|
|
|
|
ign = SLIST_FIRST(&ignores);
|
|
|
|
SLIST_REMOVE_HEAD(&ignores, next);
|
|
|
|
free(ign->mask);
|
|
|
|
free(ign);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 19:23:48 +00:00
|
|
|
static int
|
2002-07-28 15:50:38 +00:00
|
|
|
ignorep(FTSENT *ent)
|
2001-06-13 06:38:58 +00:00
|
|
|
{
|
|
|
|
struct ignentry *ign;
|
|
|
|
|
2006-05-06 22:04:59 +00:00
|
|
|
if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
|
|
|
|
return 1;
|
2001-06-13 06:38:58 +00:00
|
|
|
SLIST_FOREACH(ign, &ignores, next)
|
|
|
|
if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-04-30 01:24:53 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
siginfo(int sig __unused)
|
|
|
|
{
|
|
|
|
|
|
|
|
info = 1;
|
|
|
|
}
|