1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00

use the backup file in case of problems

This commit is contained in:
Joël Schaerer 2009-09-16 16:06:20 +02:00
parent 99d82be473
commit eb0f287eb9

View File

@ -56,12 +56,12 @@ def save(path_dict,dic_file):
f.close()
try:
os.rename(dic_file+".tmp",dic_file) #cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
import time #backup file
if not os.path.exists(dic_file+".bak") or time.time()-os.path.getmtime(dic_file+".bak")>86400:
import shutil
shutil.copy(dic_file,dic_file+".bak")
except OSError:
pass #Fail quietly, this usually means a concurrent autojump process already did the job
import time #backup file
if not os.path.exists(dic_file+".bak") or time.time()-os.path.getmtime(dic_file+".bak")>86400:
import shutil
shutil.copy(dic_file,dic_file+".bak")
def forget(path_dict,dic_file):
"""Gradually forget about directories. Only call from the actual jump since it can take time"""
@ -78,6 +78,19 @@ def find_matches(dirs,pattern,path_dict,result_list,re_flags,max_matches):
if match(path,pattern,path_dict,re_flags):
uniqadd(result_list,path)
def open_dic(dic_file,error_recovery=False):
try:
aj_file=open(dic_file)
path_dict=cPickle.load(aj_file)
aj_file.close()
return path_dict
except (IOError,EOFError):
if not error_recovery and os.path.exists(dic_file+".bak"):
import shutil
shutil.copy(dic_file+".bak",dic_file)
return open_dic(dic_file,True)
else: return {} #if everything fails, return an empty file
#Main code
try:
optlist, args = getopt.getopt(argv[1:], 'a',['stat','import','completion'])
@ -86,13 +99,7 @@ except getopt.GetoptError, e:
exit(1)
dic_file=os.path.expanduser("~/.autojump_py")
try:
aj_file=open(dic_file)
path_dict=cPickle.load(aj_file)
aj_file.close()
except IOError:
path_dict={}
path_dict=open_dic(dic_file)
if ('-a','') in optlist:
if(args[-1] != os.path.expanduser("~")): # home dir can be reached quickly by "cd" and may interfere with other directory
dicadd(path_dict,args[-1])