Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add libxo support to du #1145

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions usr.bin/du/du.1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
.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
54 changes: 34 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,17 @@ main(int argc, char *argv[])
if (p->fts_level <= depth && threshold <=
threshold_sign * howmany(p->fts_bignum *
cblocksize, blocksize)) {
xo_open_instance("paths");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless I'm missing something, and maybe I am, why are we opening this twice? Here and at line 263?

if (hflag > 0) {
prthumanval(p->fts_bignum);
(void)printf("\t%s\n", p->fts_path);
prthumanval("{:blocks/%4s}",p->fts_bignum);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs a space after the comma.

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 +302,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 +318,41 @@ 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also needs a space before saved number.

} 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this wasn't flagged with the style checker. if space ( and there needs to be a space after <.

xo_err(1, "stdout");
exit(rval);
}

Expand Down Expand Up @@ -392,7 +406,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 +472,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 +488,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 +502,13 @@ prthumanval(int64_t bytes)

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

(void)printf("%4s", buf);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The width of the string here is coupled with the size of buf, so this change isn't quite right. Should we instead have the caller pass a buffer to prthumanval()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this PR changes the behavior here. It changes from printf("%4s", buf) to an xo_emit with a format string that also always uses "%4s". There might be an issue here, but if so I think it is pre-existing.

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