From nobody Mon Apr 28 18:21:15 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4ZmWwr4rY3z5thVc; Mon, 28 Apr 2025 18:21:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZmWwr0B60z3YWH; Mon, 28 Apr 2025 18:21:16 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1745864476; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LBelwbX5iKill1fhZfiXfxiMDuDt16jQIGLR7ZkbxN0=; b=acBwEjE8wW1odGpuDJ8XD2NQue6Dv8TXd+I7koK/PvSUWAfV+MfKO+eQEwhccN5eiw7wzw Tm0slepzDAqv83ZySkiiMarqb+kZKzKRlisXjwKe9QIG3MiG+tcOOSLamaJkUm6+OAuvNn sWIk9C+LwrJyvULzGI2y2AbjtFUpfowXivPsjedUJI6itg8SwnnT5kO2JbeZZXkYLoPWI9 Ga0EPy5Bg8W6Oc1MTOIVJZr9i4aQm/nO3lYJ09rzzWJj9is8x+RwS4z1G/XjGCBWd7oSiv uFcBQJJhJgGzuSD2qoNPFUshmIcrsRzKhGt/vgEE6lVBBNpTE7nDXCfCumPb/g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1745864476; a=rsa-sha256; cv=none; b=sJ3NdbNjWYQNWoAEcovDRxsfPoKp685Y2OXX+oz+DAsASBr9VFdPoiO0WSyYslTN/czSbP kPRfItsPkWGYV94uJYVpOp7ViTESek0JCmEnWcaijNSfTHqNIYrSo+KjftnT0ZTIur+Gnk YdFByQFAmExA+eqwywrh1BGNDlhWAg9ReCUR+YatN9rdE4lcb8ewOlSJL1yFd3KVb0zzh8 ZXEEGGcb3Lq6q7dMcRsXvlQqLzRhtETMF9P3U/igrARhqox/Mh8BndqNHIC34J4exLqQ3k 1t9nmxX2eguK0p8VD25eS+GkLxn8pQrBo1robAn4vtmKR5LviAgdfevKZP2eGw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1745864476; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LBelwbX5iKill1fhZfiXfxiMDuDt16jQIGLR7ZkbxN0=; b=hG7mg+RFZ7QXbxiR7HZ5K//rH0hb/3V9ZZXEi9/vSVCIbGp7NBWIQEtbmG9eTMG2FU+wq1 1mQq9r7xUN2p9WPoLgz9jKO/uaxg5/42fCTF6SMPp4j1mM9AjdccfFzOJxqvYOcQaAzgfE 8BM9YUOS5/GiBVYPD5vj5evz8ZUcZwyFfFC76w2Mqvuo6sp48vRRNiYEqywkF+r/KJlkqY B4TVo4wYpRJIqtKv7PAKd5SgpnSVcsgLw+ou+OgCpLOijVD9BNg+6jICfuDq5UfqMpMidB UPbJO1BfX7CF71Y5rgBcQbs8mP0te2Xu/yMeC9rp+1BQwgkPopYNlJgiIY+HWw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4ZmWwq6d98zmgy; Mon, 28 Apr 2025 18:21:15 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 53SILF0L093271; Mon, 28 Apr 2025 18:21:15 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 53SILFjf093268; Mon, 28 Apr 2025 18:21:15 GMT (envelope-from git) Date: Mon, 28 Apr 2025 18:21:15 GMT Message-Id: <202504281821.53SILFjf093268@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 74a9aac40e42 - main - du: General cleanup and style(9) List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 74a9aac40e4283503952489645476155a3593954 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=74a9aac40e4283503952489645476155a3593954 commit 74a9aac40e4283503952489645476155a3593954 Author: Michal Scigocki AuthorDate: 2025-04-07 11:36:06 +0000 Commit: Mark Johnston CommitDate: 2025-04-28 18:20:50 +0000 du: General cleanup and style(9) - Refactor duplicate threshold and print code. - Replace obselete fts_bignum with fts_number. - Add missing "--libxo" to usage(). - Minor style(9) fixes. - Update list of POSIX extensions in du(1) man page. Reviewed by: markj MFC after: 1 month Pull Request: https://github.com/freebsd/freebsd-src/pull/1648 --- usr.bin/du/du.1 | 4 +- usr.bin/du/du.c | 129 +++++++++++++++++++++++++++++--------------------------- 2 files changed, 70 insertions(+), 63 deletions(-) diff --git a/usr.bin/du/du.1 b/usr.bin/du/du.1 index 568fded38073..37f7d7837b11 100644 --- a/usr.bin/du/du.1 +++ b/usr.bin/du/du.1 @@ -236,7 +236,9 @@ utility is compliant with the specification. .Pp The flags -.Op Fl cdhP , +.Op Fl ABIPcdghlmnt , +.Op Fl -libxo , +.Op Fl -si , as well as the .Ev BLOCKSIZE environment variable, diff --git a/usr.bin/du/du.c b/usr.bin/du/du.c index 5b71ceeb531f..16ca05fa9edc 100644 --- a/usr.bin/du/du.c +++ b/usr.bin/du/du.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -63,21 +64,24 @@ struct ignentry { SLIST_ENTRY(ignentry) next; }; -static int linkchk(FTSENT *); -static void usage(void); -static void prthumanval(const char *, int64_t); +static bool check_threshold(FTSENT *); static void ignoreadd(const char *); static void ignoreclean(void); static int ignorep(FTSENT *); +static int linkchk(FTSENT *); +static void print_file_size(FTSENT *); +static void prthumanval(const char *, int64_t); +static void record_file_size(FTSENT *); static void siginfo(int __unused); +static void usage(void); static int nodumpflag = 0; static int Aflag, hflag; static long blocksize, cblocksize; static volatile sig_atomic_t info; +static off_t threshold, threshold_sign; -static const struct option long_options[] = -{ +static const struct option long_options[] = { { "si", no_argument, NULL, SI_OPT }, { NULL, no_argument, NULL, 0 }, }; @@ -87,8 +91,7 @@ main(int argc, char *argv[]) { FTS *fts; FTSENT *p; - off_t savednumber, curblocks; - off_t threshold, threshold_sign; + off_t savednumber; int ftsoptions; int depth; int Hflag, Lflag, aflag, sflag, dflag, cflag; @@ -185,7 +188,7 @@ main(int argc, char *argv[]) break; case 'r': /* Compatibility. */ break; - case 't' : + case 't': if (expand_number(optarg, &threshold) != 0 || threshold == 0) { xo_warnx("invalid threshold: %s", optarg); @@ -271,32 +274,15 @@ main(int argc, char *argv[]) if (ignorep(p)) fts_set(fts, p, FTS_SKIP); break; - case FTS_DP: + case FTS_DP: /* Directory files */ if (ignorep(p)) break; - curblocks = Aflag ? - howmany(p->fts_statp->st_size, cblocksize) : - howmany(p->fts_statp->st_blocks, cblocksize); - p->fts_parent->fts_bignum += p->fts_bignum += - curblocks; - - if (p->fts_level <= depth && threshold <= - threshold_sign * howmany(p->fts_bignum * - cblocksize, blocksize)) { - xo_open_instance("paths"); - if (hflag > 0) { - prthumanval("{:blocks/%4s}", - p->fts_bignum); - xo_emit("\t{:path/%s}\n", p->fts_path); - } else { - xo_emit("{:blocks/%jd}\t{:path/%s}\n", - (intmax_t)howmany(p->fts_bignum * - cblocksize, blocksize), - p->fts_path); - } - xo_close_instance("paths"); - } + record_file_size(p); + + if (p->fts_level <= depth && check_threshold(p)) + print_file_size(p); + if (info) { info = 0; (void)printf("\t%s\n", p->fts_path); @@ -310,7 +296,7 @@ main(int argc, char *argv[]) xo_warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); rval = 1; break; - default: + default: /* All other files */ if (ignorep(p)) break; @@ -318,29 +304,12 @@ main(int argc, char *argv[]) linkchk(p)) break; - curblocks = Aflag ? - howmany(p->fts_statp->st_size, cblocksize) : - howmany(p->fts_statp->st_blocks, cblocksize); - - if ((aflag || p->fts_level == 0) && threshold <= - threshold_sign * howmany(curblocks * cblocksize, - blocksize)) { - xo_open_instance("paths"); - if (hflag > 0) { - prthumanval("{:blocks/%4s}", curblocks); - xo_emit("\t{:path/%s}\n", p->fts_path); - } else { - xo_emit("{:blocks/%jd}\t{:path/%s}\n", - (intmax_t)howmany(curblocks * - cblocksize, blocksize), - p->fts_path); - } - xo_close_instance("paths"); - } + record_file_size(p); - p->fts_parent->fts_bignum += curblocks; + if ((aflag || p->fts_level == 0) && check_threshold(p)) + print_file_size(p); } - savednumber = p->fts_parent->fts_bignum; + savednumber = p->fts_parent->fts_number; } xo_close_list("paths"); @@ -441,7 +410,7 @@ linkchk(FTSENT *p) } /* Try to locate this entry in the hash table. */ - hash = ( st->st_dev ^ st->st_ino ) % number_buckets; + 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) { /* @@ -517,10 +486,10 @@ prthumanval(const char *fmt, int64_t bytes) static void usage(void) { - xo_error( - "usage: du [-Aclnx] [-H | -L | -P] [-g | -h | -k | -m] " - "[-a | -s | -d depth] [-B blocksize] [-I mask] " - "[-t threshold] [file ...]\n"); + xo_error("%s\n%s\n%s\n", + "usage: du [--libxo] [-Aclnx] [-H | -L | -P] [-g | -h | -k | -m]", + " [-a | -s | -d depth] [-B blocksize] [-I mask] [-t threshold]", + " [file ...]"); exit(EX_USAGE); } @@ -557,16 +526,52 @@ ignorep(FTSENT *ent) struct ignentry *ign; if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP)) - return 1; + return (1); SLIST_FOREACH(ign, &ignores, next) if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH) - return 1; - return 0; + return (1); + return (0); } static void siginfo(int sig __unused) { - info = 1; } + +/* + * Record the total disk/block size of the file or directory. The fts_number + * variable provided in FTSENT is used for keeping track of the total size. + * See FTS(3). + */ +static void +record_file_size(FTSENT *p) +{ + p->fts_number += Aflag ? + howmany(p->fts_statp->st_size, cblocksize) : + howmany(p->fts_statp->st_blocks, cblocksize); + + p->fts_parent->fts_number += p->fts_number; +} + +static bool +check_threshold(FTSENT *p) +{ + return (threshold <= threshold_sign * + howmany(p->fts_number * cblocksize, blocksize)); +} + +static void +print_file_size(FTSENT *p) +{ + xo_open_instance("paths"); + if (hflag > 0) { + prthumanval("{:blocks/%4s}", p->fts_number); + xo_emit("\t{:path/%s}\n", p->fts_path); + } else { + xo_emit("{:blocks/%jd}\t{:path/%s}\n", + (intmax_t)howmany(p->fts_number * cblocksize, blocksize), + p->fts_path); + } + xo_close_instance("paths"); +}