Skip to content

Commit 36ef8d9

Browse files
committed
more secure use of temporary files
1 parent 7261bf4 commit 36ef8d9

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Install from GitHub::
2424
Status
2525
------
2626

27-
Highly experimental, pre-alpha. Bug reports and pull requests very welcome.
27+
Highly experimental, pre-alpha. Bug reports and pull requests welcome.
2828

2929
Design goals
3030
------------

zarr/ext.pyx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,24 @@ cdef class PersistentChunk(BaseChunk):
510510
raise RuntimeError('error during blosc compression: %d' % ret)
511511

512512
cdef void write(self, bytes data):
513+
513514
# N.B., write to a temporary file then move into place to avoid data
514-
# corruption due to errors during write leaving partially written files
515-
tmp = tempfile.mktemp(suffix='.partial', prefix=self._basename + '.',
516-
dir=self._dirname)
517-
with open(tmp, 'wb') as f:
515+
# corruption that could happen if errors during write leave partially
516+
# written files
517+
518+
temp_path = None
519+
520+
# write to temporary file
521+
with tempfile.NamedTemporaryFile(mode='wb', delete=False,
522+
dir=self._dirname,
523+
prefix=self._basename + '.',
524+
suffix='.partial') as f:
518525
f.write(data)
519-
os.replace(tmp, self._path)
526+
temp_path = f.name
527+
528+
# move temporary file into place
529+
if temp_path is not None:
530+
os.replace(temp_path, self._path)
520531

521532
cdef void put(self, char *source):
522533
cdef:

0 commit comments

Comments
 (0)