Skip to content

respect umask for files / dirs (1.1) #6430

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

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/borg/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,12 @@ def prune_split(archives, pattern, n, skip=[]):
return keep


def ensure_dir(path, mode=stat.S_IRWXU, pretty_deadly=True):
def ensure_dir(path, mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO, pretty_deadly=True):
"""
Ensures that the dir exists with the right permissions.
1) Make sure the directory exists in a race-free operation
2) If mode is not None and the directory has been created, give the right
permissions to the leaf directory
permissions to the leaf directory. The current umask value is masked out first.
3) If pretty_deadly is True, catch exceptions, reraise them with a pretty
message.
Returns if the directory has been created and has the right permissions,
Expand Down
10 changes: 10 additions & 0 deletions src/borg/platform/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile
import uuid

from borg.constants import UMASK_DEFAULT
from borg.helpers import safe_unlink

"""
Expand Down Expand Up @@ -193,6 +194,15 @@ def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is not None:
safe_unlink(self.tmp_fname) # with-body has failed, clean up tmp file
return # continue processing the exception normally

# tempfile.mkstemp always uses owner-only file permissions for the temp file,
# but as we'll rename it to the non-temp permanent file now, we need to respect
# the umask and change the file mode to what a normally created file would have.
# thanks to the crappy os.umask api, we can't query the umask without setting it. :-(
umask = os.umask(UMASK_DEFAULT)
os.umask(umask)
os.chmod(self.tmp_fname, mode=0o666 & ~ umask)

try:
os.replace(self.tmp_fname, self.path) # POSIX: atomic rename
except OSError:
Expand Down