Skip to content

Commit 111a420

Browse files
Merge pull request #6430 from ThomasWaldmann/fix-savefile-mode-1.1
respect umask for files / dirs (1.1)
2 parents 70a3c14 + 988a27c commit 111a420

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/borg/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,12 @@ def prune_split(archives, pattern, n, skip=[]):
513513
return keep
514514

515515

516-
def ensure_dir(path, mode=stat.S_IRWXU, pretty_deadly=True):
516+
def ensure_dir(path, mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO, pretty_deadly=True):
517517
"""
518518
Ensures that the dir exists with the right permissions.
519519
1) Make sure the directory exists in a race-free operation
520520
2) If mode is not None and the directory has been created, give the right
521-
permissions to the leaf directory
521+
permissions to the leaf directory. The current umask value is masked out first.
522522
3) If pretty_deadly is True, catch exceptions, reraise them with a pretty
523523
message.
524524
Returns if the directory has been created and has the right permissions,

src/borg/platform/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import tempfile
55
import uuid
66

7+
from borg.constants import UMASK_DEFAULT
78
from borg.helpers import safe_unlink
89

910
"""
@@ -193,6 +194,15 @@ def __exit__(self, exc_type, exc_val, exc_tb):
193194
if exc_type is not None:
194195
safe_unlink(self.tmp_fname) # with-body has failed, clean up tmp file
195196
return # continue processing the exception normally
197+
198+
# tempfile.mkstemp always uses owner-only file permissions for the temp file,
199+
# but as we'll rename it to the non-temp permanent file now, we need to respect
200+
# the umask and change the file mode to what a normally created file would have.
201+
# thanks to the crappy os.umask api, we can't query the umask without setting it. :-(
202+
umask = os.umask(UMASK_DEFAULT)
203+
os.umask(umask)
204+
os.chmod(self.tmp_fname, mode=0o666 & ~ umask)
205+
196206
try:
197207
os.replace(self.tmp_fname, self.path) # POSIX: atomic rename
198208
except OSError:

0 commit comments

Comments
 (0)