diff --git a/zarr/storage.py b/zarr/storage.py index d30b0da6df..50306e2c3a 100644 --- a/zarr/storage.py +++ b/zarr/storage.py @@ -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) @@ -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) diff --git a/zarr/tests/test_storage.py b/zarr/tests/test_storage.py index 90461b9db4..20b47e22bf 100644 --- a/zarr/tests/test_storage.py +++ b/zarr/tests/test_storage.py @@ -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):