Skip to content

Set file permissions in DirectoryStorage according to umask #445

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

Closed
Closed
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
14 changes: 11 additions & 3 deletions zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def _path_to_prefix(path):
return prefix


def _NamedTemporaryFileUmask(*args, **kargs):
fdesc = tempfile.NamedTemporaryFile(*args, **kargs)
umask = os.umask(0)
os.umask(umask)
os.chmod(fdesc.name, 0o666 & ~umask)
return fdesc


def contains_array(store, path=None):
"""Return True if the store contains an array at the given logical path."""
path = normalize_storage_path(path)
Expand Down Expand Up @@ -752,9 +760,9 @@ def __setitem__(self, key, value):
# write to temporary file
temp_path = None
try:
with tempfile.NamedTemporaryFile(mode='wb', delete=False, dir=dir_path,
prefix=file_name + '.',
suffix='.partial') as f:
with _NamedTemporaryFileUmask(mode='wb', delete=False, dir=dir_path,
prefix=file_name + '.',
suffix='.partial') as f:
temp_path = f.name
f.write(value)

Expand Down
7 changes: 7 additions & 0 deletions zarr/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,13 @@ def test_filesystem_path(self):
store['foo'] = b'bar'
assert os.path.isdir(path)

# check correct permissions
stat = os.stat(path)
mode = stat.st_mode & 0o666
umask = os.umask(0)
os.umask(umask)
assert mode == (0o666 & ~umask)

# test behaviour with file path
with tempfile.NamedTemporaryFile() as f:
with pytest.raises(ValueError):
Expand Down