avoid overwriting another user's database

Should fix issue #39
pull/40/head release-v15
Joel Schaerer 13 years ago
parent 36daeb1e32
commit 1bc3440968

@ -49,22 +49,25 @@ def dicadd(dic, key, increment=1):
def save(path_dict, dic_file): def save(path_dict, dic_file):
"""Save the database in an atomic way, and preserve """Save the database in an atomic way, and preserve
a backup file.""" a backup file."""
temp = NamedTemporaryFile(dir=CONFIG_DIR, delete=False) # If the dic_file exists, check that it belongs to us
pickle.dump(path_dict, temp, -1) # Otherwise, fail quietly
temp.flush() if (not os.path.exists(dic_file)) or os.getuid() == os.stat(dic_file)[4]:
os.fsync(temp) temp = NamedTemporaryFile(dir=CONFIG_DIR, delete=False)
temp.close() pickle.dump(path_dict, temp, -1)
#cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/ temp.flush()
os.rename(temp.name, dic_file) os.fsync(temp)
try: #backup file temp.close()
import time #cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
if (not os.path.exists(dic_file+".bak") or os.rename(temp.name, dic_file)
time.time()-os.path.getmtime(dic_file+".bak")>86400): try: #backup file
import shutil import time
shutil.copy(dic_file, dic_file+".bak") if (not os.path.exists(dic_file+".bak") or
except OSError as ex: time.time()-os.path.getmtime(dic_file+".bak")>86400):
print("Error while creating backup autojump file. (%s)" % import shutil
ex, file=stderr) shutil.copy(dic_file, dic_file+".bak")
except OSError as ex:
print("Error while creating backup autojump file. (%s)" %
ex, file=stderr)
def forget(path_dict, dic_file): def forget(path_dict, dic_file):
"""Gradually forget about directories. Only call """Gradually forget about directories. Only call

Loading…
Cancel
Save