From eb0f287eb9fb015d3cff0024078f74b551120b23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Schaerer?= Date: Wed, 16 Sep 2009 16:06:20 +0200 Subject: [PATCH] use the backup file in case of problems --- autojump | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/autojump b/autojump index 3ee1404..6ba8dbf 100755 --- a/autojump +++ b/autojump @@ -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])