call fsync on saving

This commit is contained in:
lilydjwg 2017-07-05 13:11:03 +08:00 committed by Felix Yan
parent 84df2716b5
commit 3489e4e2d1
1 changed files with 3 additions and 0 deletions

View File

@ -41,6 +41,9 @@ def safe_overwrite(fname, data, *, method='write', mode='w', encoding=None):
# if not using "with", write can fail without exception
with open(tmpname, mode, encoding=encoding) as f:
getattr(f, method)(data)
# see also: https://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
f.flush()
os.fsync(f.fileno())
# if the above write failed (because disk is full etc), the old data should be kept
os.rename(tmpname, fname)