Skip to content

Commit

Permalink
Add libxo support to du
Browse files Browse the repository at this point in the history
Convert du to use libxo enabling structured output.

[[ minor style fixes by imp ]]

Signed-off-by: Nathan Huff <nhuff@acm.org>
Reviewed by: imp
Pull Request: #1145
  • Loading branch information
nhuff authored and bsdimp committed Apr 29, 2024
1 parent 1ee7a8f commit b2b8fa1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
2 changes: 1 addition & 1 deletion usr.bin/du/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

PACKAGE= runtime
PROG= du
LIBADD= util
LIBADD= xo util

HAS_TESTS=
SUBDIR.${MK_TESTS}+= tests
Expand Down
12 changes: 11 additions & 1 deletion usr.bin/du/du.1
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd August 1, 2019
.Dd April 29, 2024
.Dt DU 1
.Os
.Sh NAME
.Nm du
.Nd display disk usage statistics
.Sh SYNOPSIS
.Nm
.Op Fl -libxo
.Op Fl Aclnx
.Op Fl H | L | P
.Op Fl g | h | k | m
Expand All @@ -52,6 +53,13 @@ the current directory is displayed.
.Pp
The options are as follows:
.Bl -tag -width indent
.It Fl -libxo
Generate output via
.Xr libxo 3
in a selection of different human and machine readable formats.
See
.Xr xo_parse_args 3
for details on command line arguments.
.It Fl A
Display the apparent size instead of the disk usage.
This can be helpful when operating on compressed volumes or sparse files.
Expand Down Expand Up @@ -216,6 +224,8 @@ Also display a grand total at the end:
.Xr df 1 ,
.Xr chflags 2 ,
.Xr fts 3 ,
.Xr libxo 3 ,
.Xr xo_parse_args 3 ,
.Xr symlink 7 ,
.Xr quot 8
.Sh STANDARDS
Expand Down
57 changes: 37 additions & 20 deletions usr.bin/du/du.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <libxo/xo.h>

#define SI_OPT (CHAR_MAX + 1)

Expand All @@ -62,7 +63,7 @@ struct ignentry {

static int linkchk(FTSENT *);
static void usage(void);
static void prthumanval(int64_t);
static void prthumanval(const char *, int64_t);
static void ignoreadd(const char *);
static void ignoreclean(void);
static int ignorep(FTSENT *);
Expand Down Expand Up @@ -107,6 +108,10 @@ main(int argc, char *argv[])
depth = INT_MAX;
SLIST_INIT(&ignores);

argc = xo_parse_args(argc, argv);
if (argc < 0)
exit(EX_USAGE);

while ((ch = getopt_long(argc, argv, "+AB:HI:LPasd:cghklmnrt:x",
long_options, NULL)) != -1)
switch (ch) {
Expand All @@ -117,7 +122,7 @@ main(int argc, char *argv[])
errno = 0;
cblocksize = atoi(optarg);
if (errno == ERANGE || cblocksize <= 0) {
warnx("invalid argument to option B: %s",
xo_warnx("invalid argument to option B: %s",
optarg);
usage();
}
Expand Down Expand Up @@ -147,7 +152,7 @@ main(int argc, char *argv[])
errno = 0;
depth = atoi(optarg);
if (errno == ERANGE || depth < 0) {
warnx("invalid argument to option d: %s",
xo_warnx("invalid argument to option d: %s",
optarg);
usage();
}
Expand Down Expand Up @@ -181,7 +186,7 @@ main(int argc, char *argv[])
case 't' :
if (expand_number(optarg, &threshold) != 0 ||
threshold == 0) {
warnx("invalid threshold: %s", optarg);
xo_warnx("invalid threshold: %s", optarg);
usage();
} else if (threshold < 0)
threshold_sign = -1;
Expand Down Expand Up @@ -254,6 +259,8 @@ main(int argc, char *argv[])
if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
err(1, "fts_open");

xo_open_container("disk-usage-information");
xo_open_list("paths");
while (errno = 0, (p = fts_read(fts)) != NULL) {
switch (p->fts_info) {
case FTS_D: /* Ignore. */
Expand All @@ -273,15 +280,18 @@ main(int argc, char *argv[])
if (p->fts_level <= depth && threshold <=
threshold_sign * howmany(p->fts_bignum *
cblocksize, blocksize)) {
xo_open_instance("paths");
if (hflag > 0) {
prthumanval(p->fts_bignum);
(void)printf("\t%s\n", p->fts_path);
prthumanval("{:blocks/%4s}",
p->fts_bignum);
xo_emit("\t{:path/%s}\n", p->fts_path);
} else {
(void)printf("%jd\t%s\n",
xo_emit("{:blocks/%jd}\t{:path/%s}\n",
(intmax_t)howmany(p->fts_bignum *
cblocksize, blocksize),
p->fts_path);
}
xo_close_instance("paths");
}
if (info) {
info = 0;
Expand All @@ -293,7 +303,7 @@ main(int argc, char *argv[])
case FTS_DNR: /* Warn, continue. */
case FTS_ERR:
case FTS_NS:
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
xo_warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
rval = 1;
break;
default:
Expand All @@ -309,36 +319,43 @@ main(int argc, char *argv[])
howmany(p->fts_statp->st_blocks, cblocksize);

if (aflag || p->fts_level == 0) {
xo_open_instance("paths");
if (hflag > 0) {
prthumanval(curblocks);
(void)printf("\t%s\n", p->fts_path);
prthumanval("{:blocks/%4s}", curblocks);
xo_emit("\t{:path/%s}\n", p->fts_path);
} else {
(void)printf("%jd\t%s\n",
xo_emit("{:blocks/%jd}\t{:path/%s}\n",
(intmax_t)howmany(curblocks *
cblocksize, blocksize),
p->fts_path);
}
xo_close_instance("paths");
}

p->fts_parent->fts_bignum += curblocks;
}
savednumber = p->fts_parent->fts_bignum;
}
xo_close_list("paths");

if (errno)
err(1, "fts_read");
xo_err(1, "fts_read");

if (cflag) {
if (hflag > 0) {
prthumanval(savednumber);
(void)printf("\ttotal\n");
prthumanval("{:total-blocks/%4s}\ttotal\n",
savednumber);
} else {
(void)printf("%jd\ttotal\n", (intmax_t)howmany(
xo_emit("{:total-blocks/%jd}\ttotal\n",
(intmax_t)howmany(
savednumber * cblocksize, blocksize));
}
}

ignoreclean();
xo_close_container("disk-usage-information");
if (xo_finish() < 0)
xo_err(1, "stdout");
exit(rval);
}

Expand Down Expand Up @@ -392,7 +409,7 @@ linkchk(FTSENT *p)

if (new_buckets == NULL) {
stop_allocating = 1;
warnx("No more memory for tracking hard links");
xo_warnx("No more memory for tracking hard links");
} else {
for (i = 0; i < number_buckets; i++) {
while (buckets[i] != NULL) {
Expand Down Expand Up @@ -458,7 +475,7 @@ linkchk(FTSENT *p)
le = malloc(sizeof(struct links_entry));
if (le == NULL) {
stop_allocating = 1;
warnx("No more memory for tracking hard links");
xo_warnx("No more memory for tracking hard links");
return (0);
}
le->dev = st->st_dev;
Expand All @@ -474,7 +491,7 @@ linkchk(FTSENT *p)
}

static void
prthumanval(int64_t bytes)
prthumanval(const char *fmt, int64_t bytes)
{
char buf[5];
int flags;
Expand All @@ -488,13 +505,13 @@ prthumanval(int64_t bytes)

humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, flags);

(void)printf("%4s", buf);
xo_emit(fmt, buf);
}

static void
usage(void)
{
(void)fprintf(stderr,
xo_error(
"usage: du [-Aclnx] [-H | -L | -P] [-g | -h | -k | -m] "
"[-a | -s | -d depth] [-B blocksize] [-I mask] "
"[-t threshold] [file ...]\n");
Expand Down

0 comments on commit b2b8fa1

Please sign in to comment.