Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions atomicwrites/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ def get_fileobject(self, suffix="", prefix=tempfile.gettempprefix(),
dir = os.path.normpath(os.path.dirname(self._path))
descriptor, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
dir=dir)

# If specified, change user and group.
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


# io.open() will take either the descriptor or the name, but we need
# the name later for commit()/replace_atomic() and couldn't find a way
# to get the filename from the descriptor.
Expand Down