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

feat: support uid only form in NormalizeUidGid #553

Conversation

james-garner-canonical
Copy link
Contributor

@james-garner-canonical james-garner-canonical commented Jan 8, 2025

This makes it valid to supply just the uid for operations setting file ownership, like mkdir and push.

This lack came up when investigating the API for the file operations library.

This makes it valid to supply just the uid for operations setting file
ownership, like mkdir and push.
Copy link
Contributor

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks! One comment suggestion.

@@ -134,12 +130,22 @@ func NormalizeUidGid(uid, gid *int, username, group string) (*int, *int, error)
}
gid = &n
}
if gid == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

It wasn't at all obvious to me that uid was guaranteed to be non-nil here (to avoid a nil pointer panic on line 135), but I think it is, due to the previous checks. Is that your thinking too? Can we add a little comment on line 135 like // uid will be non-nil here due to checks above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah it's non-obvious isn't it, I just spent 10 minutes going through the logic again ...

The only way for uid to be nil would be if neither uid nor user were specified (we set uid based on the user name). Likewise, the only way for gid to be nil here is if neither gid nor group were specified.

Line 98 guarantees that at least one of the four were specified.

if uid == nil && username == "" && gid == nil && group == "" {
	return nil, nil, nil
}

Since we check if gid is nil here (i.e. neither gid nor group were provided), then one of uid or user must have been provided, so uid can't be nil.

So maybe comments like this?

if gid == nil { // Neither gid nor group was specified
// uid either specified or looked up from user; use user's primary group ID

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me!

@james-garner-canonical james-garner-canonical force-pushed the 2025-01/feat/support-uid-only-in-chown branch from fad1fa4 to 597029d Compare January 10, 2025 03:42
@james-garner-canonical james-garner-canonical merged commit 05ea5bc into canonical:master Jan 10, 2025
18 checks passed
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.

2 participants