Skip to content

Commit 97499be

Browse files
committed
Set file permissions in DirectoryStorage according to umask
Fixes #320
1 parent 7431e47 commit 97499be

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

zarr/storage.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ def _path_to_prefix(path):
7373
prefix = ''
7474
return prefix
7575

76+
def _NamedTemporaryFileUmask(*args, **kargs):
77+
fdesc = tempfile.NamedTemporaryFile(*args, **kargs)
78+
umask = os.umask(0)
79+
os.umask(umask)
80+
os.chmod(fdesc.name, 0o666 & ~umask)
81+
return fdesc
7682

7783
def contains_array(store, path=None):
7884
"""Return True if the store contains an array at the given logical path."""
@@ -752,9 +758,9 @@ def __setitem__(self, key, value):
752758
# write to temporary file
753759
temp_path = None
754760
try:
755-
with tempfile.NamedTemporaryFile(mode='wb', delete=False, dir=dir_path,
756-
prefix=file_name + '.',
757-
suffix='.partial') as f:
761+
with _NamedTemporaryFileUmask(mode='wb', delete=False, dir=dir_path,
762+
prefix=file_name + '.',
763+
suffix='.partial') as f:
758764
temp_path = f.name
759765
f.write(value)
760766

zarr/tests/test_storage.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,14 @@ def test_filesystem_path(self):
721721
store['foo'] = b'bar'
722722
assert os.path.isdir(path)
723723

724+
# check correct permissions
725+
stat = os.stat(path)
726+
mode = stat.st_mode & 0o666
727+
umask = os.umask(0)
728+
os.umask(umask)
729+
assert mode == (0o666 & ~umask)
730+
731+
724732
# test behaviour with file path
725733
with tempfile.NamedTemporaryFile() as f:
726734
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)