1
0
mirror of https://github.com/wting/autojump synced 2024-09-29 22:40:46 +00:00

Merge pull request #97 from wting/issue_92-no_space

Abort save if error writing to disk (issue 92)
This commit is contained in:
Joel Schaerer 2012-02-07 23:00:54 -08:00
commit faf0854602

View File

@ -85,10 +85,18 @@ def save(path_dict, dic_file):
for path,weight in sorted(path_dict.items(),key=itemgetter(1),reverse=True): for path,weight in sorted(path_dict.items(),key=itemgetter(1),reverse=True):
# the db is stored in utf-8 # the db is stored in utf-8
temp.write((unico("%s\t%s\n")%(weight,path)).encode("utf-8")) temp.write((unico("%s\t%s\n")%(weight,path)).encode("utf-8"))
#cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
temp.flush() # Catching disk errors and skipping save since file handle can't be closed.
os.fsync(temp) try:
temp.close() #cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
temp.flush()
os.fsync(temp)
temp.close()
except IOError as ex:
print("I/O error writing to temporary file. (%s)" %
ex, file=stderr)
return
# Use shutil.move instead of os.rename because windows doesn't support # Use shutil.move instead of os.rename because windows doesn't support
# using rename to overwrite files # using rename to overwrite files
shutil.move(temp.name, dic_file) shutil.move(temp.name, dic_file)