Skip to content

Allow callers to specify desired uid/gid#1

Open
jdegges wants to merge 1 commit intomasterfrom
jd/atomic-chown
Open

Allow callers to specify desired uid/gid#1
jdegges wants to merge 1 commit intomasterfrom
jd/atomic-chown

Conversation

@jdegges
Copy link
Copy Markdown
Collaborator

@jdegges jdegges commented Aug 19, 2021

It can be useful for a file to be created atomically with the desired
user and group. Let's allow callers to specify their desired uid/gid and
change ownership of the intermediate temp file before it is moved into
place.

It can be useful for a file to be created atomically with the desired
user and group. Let's allow callers to specify their desired uid/gid and
change ownership of the intermediate temp file before it is moved into
place.
Copy link
Copy Markdown

@mookerji mookerji 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, but see question.

Comment thread atomicwrites/__init__.py
uid = kwargs.pop('uid', -1)
gid = kwargs.pop('gid', -1)
if uid != -1 or gid != -1:
os.fchown(descriptor, uid, gid)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we need check for whether sys.platform is on Unix?

https://docs.python.org/3/library/os.html#os.fchown

Copy link
Copy Markdown

@borgel borgel Aug 19, 2021

Choose a reason for hiding this comment

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

Maybe an assert around it. Though obviously not if we're upstreaming the patch

Copy link
Copy Markdown

@borgel borgel Aug 19, 2021

Choose a reason for hiding this comment

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

May need to ensure that if one is specified and the other isn't, we don't attempt to set either UID or GID to -1 (unless I misunderstand the logic here)

Copy link
Copy Markdown
Collaborator Author

@jdegges jdegges Aug 19, 2021

Choose a reason for hiding this comment

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

@mookerji yes I was concerned about this as well. Do you know of a way to test for "unix"? The method that comes to mind is doing something like hasattr(os, 'fchown'), but that assumes that on "non-unix" platforms the function doesn't exist in the os module.

Copy link
Copy Markdown
Collaborator Author

@jdegges jdegges Aug 19, 2021

Choose a reason for hiding this comment

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

@borgel, According to the docs -1 will leave that field unchanged, so it is always a safe default. The if clause here was only added so we don't run the system call unnecessarily.

https://linux.die.net/man/2/fchown

https://docs.python.org/3/library/os.html#os.fchown

Copy link
Copy Markdown
Member

@JoshM1994 JoshM1994 left a comment

Choose a reason for hiding this comment

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

Can see this being really valuable for replacing some of the manual chowning that occurs inside mqtt-bridge as well :)

Comment thread atomicwrites/__init__.py
uid = kwargs.pop('uid', -1)
gid = kwargs.pop('gid', -1)
if uid != -1 or gid != -1:
os.fchown(descriptor, uid, gid)
Copy link
Copy Markdown

@borgel borgel Aug 19, 2021

Choose a reason for hiding this comment

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

May need to ensure that if one is specified and the other isn't, we don't attempt to set either UID or GID to -1 (unless I misunderstand the logic here)

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.

6 participants