Skip to content

Commit 3ea7407

Browse files
committed
Allow callers to specify desired uid/gid
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.
1 parent e4659ba commit 3ea7407

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

atomicwrites/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ def get_fileobject(self, suffix="", prefix=tempfile.gettempprefix(),
182182
dir = os.path.normpath(os.path.dirname(self._path))
183183
descriptor, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
184184
dir=dir)
185+
186+
# If specified, change user and group.
187+
uid = kwargs.get('uid', default=-1)
188+
gid = kwargs.get('gid', default=-1)
189+
if uid != -1 or gid != -1:
190+
os.fchown(descriptor, uid, gid)
191+
185192
# io.open() will take either the descriptor or the name, but we need
186193
# the name later for commit()/replace_atomic() and couldn't find a way
187194
# to get the filename from the descriptor.

0 commit comments

Comments
 (0)