Skip to content

Commit 6d49909

Browse files
committed
Set file permissions in DirectoryStorage according to umask
Fixes zarr-developers#325
1 parent 7431e47 commit 6d49909

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎zarr/storage.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ def _path_to_prefix(path):
7474
return prefix
7575

7676

77+
def _NamedTemporaryFileUmask(*args, **kargs):
78+
fdesc = tempfile.NamedTemporaryFile(*args, **kargs)
79+
umask = os.umask(0)
80+
os.umask(umask)
81+
os.chmod(fdesc.name, 0o666 & ~umask)
82+
return fdesc
83+
84+
7785
def contains_array(store, path=None):
7886
"""Return True if the store contains an array at the given logical path."""
7987
path = normalize_storage_path(path)
@@ -752,9 +760,9 @@ def __setitem__(self, key, value):
752760
# write to temporary file
753761
temp_path = None
754762
try:
755-
with tempfile.NamedTemporaryFile(mode='wb', delete=False, dir=dir_path,
756-
prefix=file_name + '.',
757-
suffix='.partial') as f:
763+
with _NamedTemporaryFileUmask(mode='wb', delete=False, dir=dir_path,
764+
prefix=file_name + '.',
765+
suffix='.partial') as f:
758766
temp_path = f.name
759767
f.write(value)
760768

‎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)