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

Add libxo support to du #1145

wants to merge 1 commit into from

Conversation

nhuff
Copy link
Contributor

@nhuff nhuff commented Mar 24, 2024

Convert du to use libxo enabling structured output.

Signed-off-by: Nathan Huff nhuff@acm.org

usr.bin/du/du.c Outdated

#define SI_OPT (CHAR_MAX + 1)

#define UNITS_2 1
#define UNITS_2 1
Copy link
Member

Choose a reason for hiding this comment

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

This seems gratuitous.

usr.bin/du/du.c Outdated
@@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

FreeBSD's style is const char * with a space after the char.

@@ -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?

@@ -309,36 +318,40 @@ 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.

usr.bin/du/du.c Outdated
/* Malloc one if we have to. */
le = malloc(sizeof(struct links_entry));
}
Copy link
Member

Choose a reason for hiding this comment

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

This is a good change, but it's gratuitous since you're not changing adjacent lines.

Copy link
Member

@bsdimp bsdimp left a comment

Choose a reason for hiding this comment

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

two minor nits that I see, and either a double push of paths, or something that I don't understand.

@nhuff
Copy link
Contributor Author

nhuff commented Apr 12, 2024

I've updated the pull request to address the style issues.

The two xo_open_instance("paths") calls happen in two different code paths during the filesystem traversal. One path is taken if you are reporting on a file and one path if you are reporting on a directory.

Copy link
Member

@markjdb markjdb left a comment

Choose a reason for hiding this comment

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

Sorry, I had left these comments last week, but apparently failed to click submit. :(

usr.bin/du/du.c Outdated
@@ -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(-1);
Copy link
Member

Choose a reason for hiding this comment

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

It'd be more consistent to use EX_USAGE like usage() does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you have an in tree example of what you would like to see here? I'm trying to be consistent and all of the examples I'm finding just exit in this case.

Copy link
Member

@markjdb markjdb Apr 12, 2024

Choose a reason for hiding this comment

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

I meant, this line should be exit(EX_USAGE). It's unusual to exit with status -1.

usr.bin/du/du.c Outdated
/* Malloc one if we have to. */
le = malloc(sizeof(struct links_entry));
}
if (le == NULL) {
stop_allocating = 1;
warnx("No more memory for tracking hard links");
Copy link
Member

Choose a reason for hiding this comment

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

Looks like some warnx() calls were not converted?

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'm using df as a template. It didn't look like they converted the warnx on malloc failures. Does xo_warnx allocate? I figured it might and that is why df does it the way it does, but maybe I should check for sure.

Copy link
Member

Choose a reason for hiding this comment

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

By default I believe that xo_warnx() etc. just output unstructured data to standard error, and they don't appear to allocate memory. (And libxo will print an error if it hits an internal memory allocation failure.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In tree examples seem somewhat inconsistent here so I don't have any issues converting the rest of the calls.

usr.bin/du/du.c Outdated
savednumber * cblocksize, blocksize));
}
}

ignoreclean();
xo_close_container("disk-usage-information");
xo_finish();
Copy link
Member

Choose a reason for hiding this comment

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

We're not very consistent about it in the tree yet, but we should check for errors from xo_finish():

if (xo_finish() < 0)
    xo_err(1, "stdout");

@@ -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.

Convert du to use libxo enabling structured output.

Signed-off-by: Nathan Huff <nhuff@acm.org>
@nhuff
Copy link
Contributor Author

nhuff commented Apr 12, 2024

I've updated the pull request to address most of the comments from @markjdb. I didn't address the last comment since I don't think the behavior differs from the current implementation in any relevant way.

@bsdimp
Copy link
Member

bsdimp commented Apr 23, 2024

my eyeballs say that this is ready. Will run some tests and either give feedback or push.

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 <.

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.


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.

Copy link
Member

@bsdimp bsdimp left a comment

Choose a reason for hiding this comment

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

Please fix the style issues tagged.

@bsdimp
Copy link
Member

bsdimp commented Apr 29, 2024

this looks like it will work... and spot checking shows that it does. Just a couple style issue to fix.

freebsd-git pushed a commit that referenced this pull request Apr 29, 2024
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
@bsdimp
Copy link
Member

bsdimp commented Apr 29, 2024

On second thought, the style issues were relatively minor and easy to fix while staging.
I also bumped the date on the man page, but that's OK: many man pages are active enough that dates conflict a lot.
I went ahead and fixed them this time, but next time please use checkstyle9.pl to make future submissions better.
Thanks for this new functionality.

@bsdimp bsdimp closed this Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants