Skip to content
Myr

Preserve expected permissions in atomic open_file

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #1376.

authored by Anders Kaseorg · committed by David Lord · parent b24c51f · Browse files · 2 files+34 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Anders Kaseorg <andersk@mit.edu>

Diff stats

2 files+34 added−3 removed

  • other:+34 added−3 removed

2 files+34 added−3 removed

modified:click/_compat.py+32 added-3 removed
502 unmodified lines
503 # Atomic writes are more complicated. They work by opening a file504 # as a proxy in the same folder and then using the fdopen505 # functionality to wrap it in a Python file. Then we wrap it in an506 # atomic file that moves the file over on close.507 import tempfile508 fd, tmp_filename = tempfile.mkstemp(dir=os.path.dirname(filename),509 prefix='.__atomic-write')507 import errno508 import random509510 try:511 perm = os.stat(filename).st_mode512 except OSError:513 perm = None514515 flags = os.O_RDWR | os.O_CREAT | os.O_EXCL516 if 'b' in mode:517 flags |= getattr(os, 'O_BINARY', 0)518519 while True:520 tmp_filename = os.path.join(521 os.path.dirname(filename),522 '.__atomic-write%08x' % (random.randrange(1 << 32),),523 )524 try:525 fd = os.open(tmp_filename, flags, 0o666 if perm is None else perm)526 break527 except OSError as e:528 if e.errno == errno.EEXIST or (529 os.name == "nt"530 and e.errno == errno.EACCES531 and os.path.isdir(dir)532 and os.access(dir, os.W_OK)533 ):534 continue535 raise536537 if perm is not None:538 os.chmod(tmp_filename, perm) # in case perm includes bits in umask539540 if encoding is not None:541 f = io.open(fd, mode, encoding=encoding, errors=errors)542 else:543 f = os.fdopen(fd, mode)
201 unmodified lines
modified:CHANGES.rst+2 added
27 unmodified lines
28- Handle ``env MYPATH=''`` as though the option were not passed.29 :issue:`1196`30- It is once again possible to call ``next(bar)`` on an active31 progress bar instance. :issue:`1125`32- ``open_file`` with ``atomic=True`` retains permissions of existing33 files and respects the current umask for new files. :issue:`1376`343536Version 7.037-----------
540 unmodified lines