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 CLI command 'preview' and API 'aug_preview' to preview file contents #690

Merged
merged 3 commits into from
Sep 3, 2020

Conversation

georgehansper
Copy link
Member

This Pull Requests adds a new command preview to augtool, and a new function aug_preview() to the API.

The preview command accepts a path-name as an argument.

For the file corresponding to the path-name, the contents of the file are written to stdout, with the current
nodes and values from the /files tree applied.

This corresponds to the contents of the file that would be written by a subsequent save operation.

The path-expression must evaluate to a single node only, and the source-file is determined from this node.

The API call aug_preview() places the resulting file-contents into char *OUT, allocating the required memory.

    int aug_preview(augeas *aug, const char *path, char **out);

@raphink
Copy link
Member

raphink commented Aug 12, 2020

That looks very interesting!

At the moment, this feature is achieved in various tools by leveraging the new mode and diffing the files. Doing it in memory would be much nicer.

@raphink raphink requested a review from lutter August 12, 2020 09:16
@georgehansper
Copy link
Member Author

The preview function has to read the original file (again) in order to apply the transform to it, so it has to do a 2nd read of the original file.

It does save having to write the file out and having to read the resulting file back off the disk (by whatever is wants to do the diff), so it saves IO operations.

Copy link
Member

@lutter lutter left a comment

Choose a reason for hiding this comment

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

Nice! I really like this functionality.

src/augeas.c Outdated

ERR_THROW(file_path == NULL, aug, AUG_EBADARG, "Path %s is not associated with a file", path);

if ( file_path == NULL ) {
Copy link
Member

Choose a reason for hiding this comment

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

This if is unnecessary since you know from the ERR_THROW that file_path != NULL

Copy link
Member Author

Choose a reason for hiding this comment

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

if removed

src/augeas.c Outdated
}

r = xasprintf(&lens_path, "%s%s/%s", AUGEAS_META_TREE, file_path, s_lens);
if ( r>= 0 ) {
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 super nitpicky, but I generally write if statements with now spaces after the parentheses, but spaces around the operator, i.e., if (r >= 0) { .. - I wish there was some easy-to-use code formatter that we could just run to make sure these things are consistent. In any event, I would prefer to have these things unfirom as much as possible, but it's a preference, not a "I would never merge a PR formatted like this" ;)

Copy link
Member

Choose a reason for hiding this comment

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

Instead of the if statement, it would be better to write

ERR_NOMEM(lens_path == NULL, aug)

Copy link
Member Author

Choose a reason for hiding this comment

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

Replaced if (r >= 0) with ERR_NOMEM macro

src/augeas.c Outdated

r = xasprintf(&lens_path, "%s%s/%s", AUGEAS_META_TREE, file_path, s_lens);
if ( r>= 0 ) {
r = aug_get(aug,lens_path,(const char **) &lens_name);
Copy link
Member

Choose a reason for hiding this comment

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

You don't need the assignment to r in what follows - better to leave that out so the reader doesn't wonder why it's assigned to.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed unused assignment

src/augeas.c Outdated

ERR_THROW(lens_name == NULL, aug, AUG_ENOLENS, "No lens found for path %s", path);

if ( STREQLEN(file_path, AUGEAS_FILES_TREE, strlen(AUGEAS_FILES_TREE) ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

This if does not seem necessary - we already constructed file_path to have the 'right' content, so we should be able to perform the body of the if unconditionally.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed if ( STREQLEN

src/augeas.c Outdated
ERR_NOMEM(source_filename == NULL, aug);
source_text = xread_file(source_filename);
}
if ( source_text == NULL ) {
Copy link
Member

Choose a reason for hiding this comment

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

I think a NULL here should be reported as an error to the caller - they told us to read a file and we couldn't.

src/augeas.c Outdated
free(lens_path);
free(source_filename);
free(source_text);
api_exit(aug);
Copy link
Member

Choose a reason for hiding this comment

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

Since it's easy to only fix up all the free calls in the good path or the error path when this code changes in the future, I usually write functions like this as

int something(..) {
  int result = -1;
  .. do things ..
  /* everything worked out */
 result = 0;
error:
  /* free all the things */
 return result;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that's a better structure. Updated.

tests/test-api.c Outdated
if ( hosts_fp ) {
hosts_txt = calloc(sizeof(char),4096);
if ( hosts_txt ) {
readsz = fread(hosts_txt,sizeof(char),4096,hosts_fp);
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 there's some errant whitespace

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@lutter lutter merged commit 78e39b7 into hercules-team:master Sep 3, 2020
@georgehansper georgehansper deleted the cmd_preview branch October 1, 2021 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants