when saving a snapshots file, copy it to a temporary place and then do a mv. This makes it easier to use this program in conjunction with an inotify-based service that will act on newly-created snapshost (doing a mv is atomic, whereas a create + copy is not)

This commit is contained in:
Jude Nelson
2018-10-08 11:55:40 -04:00
parent 0524a21836
commit cc645be855

View File

@@ -360,8 +360,11 @@ def make_snapshot(working_dir, snapshots_dir, private_key, block_number):
if os.path.exists('snapshot.bsk'):
os.unlink('snapshot.bsk')
# copy it in
shutil.copy('snapshot.bsk.{}'.format(block_number), 'snapshot.bsk')
# copy it in.
# do in two steps, so we can watch the directory and be assured that when the move completes,
# the file has been completely written.
shutil.copy('snapshot.bsk.{}'.format(block_number), '.snapshot.bsk')
shutil.move('.snapshot.bsk', 'snapshot.bsk')
except Exception as e:
log.exception(e)
return {'error': 'Failed to copy {} to {}'.format('snapshot.bsk.{}'.format(block_number), 'snapshot.bsk')}