-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add libxo support to du #1145
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
PACKAGE= runtime | ||
PROG= du | ||
LIBADD= util | ||
LIBADD= xo util | ||
|
||
HAS_TESTS= | ||
SUBDIR.${MK_TESTS}+= tests | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
#include <string.h> | ||
#include <sysexits.h> | ||
#include <unistd.h> | ||
#include <libxo/xo.h> | ||
|
||
#define SI_OPT (CHAR_MAX + 1) | ||
|
||
|
@@ -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 *); | ||
|
@@ -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) { | ||
|
@@ -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(); | ||
} | ||
|
@@ -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(); | ||
} | ||
|
@@ -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; | ||
|
@@ -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. */ | ||
|
@@ -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"); | ||
if (hflag > 0) { | ||
prthumanval(p->fts_bignum); | ||
(void)printf("\t%s\n", p->fts_path); | ||
prthumanval("{:blocks/%4s}",p->fts_bignum); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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: | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
|
@@ -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) { | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -488,13 +502,13 @@ prthumanval(int64_t bytes) | |
|
||
humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, flags); | ||
|
||
(void)printf("%4s", buf); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The width of the string here is coupled with the size of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
There was a problem hiding this comment.
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?