@@ -21,6 +21,8 @@ class RepositoryReplay(Error):
2121 class CacheInitAbortedError (Error ):
2222 """Cache initialization aborted"""
2323
24+ class RepositoryAccessAborted (Error ):
25+ """Repository access aborted"""
2426
2527 class EncryptionMethodMismatch (Error ):
2628 """Repository encryption method changed since last acccess, refusing to continue
@@ -34,15 +36,20 @@ def __init__(self, repository, key, manifest, path=None, sync=True, warn_if_unen
3436 self .key = key
3537 self .manifest = manifest
3638 self .path = path or os .path .join (get_cache_dir (), hexlify (repository .id ).decode ('ascii' ))
39+ # Warn user before sending data to a never seen before unencrypted repository
3740 if not os .path .exists (self .path ):
3841 if warn_if_unencrypted and isinstance (key , PlaintextKey ):
39- if 'ATTIC_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK' not in os .environ :
40- print ("""Warning: Attempting to access a previously unknown unencrypted repository\n """ , file = sys .stderr )
41- answer = input ('Do you want to continue? [yN] ' )
42- if not (answer and answer in 'Yy' ):
43- raise self .CacheInitAbortedError ()
42+ if not self ._confirm ('Warning: Attempting to access a previously unknown unencrypted repository' ,
43+ 'ATTIC_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK' ):
44+ raise self .CacheInitAbortedError ()
4445 self .create ()
4546 self .open ()
47+ # Warn user before sending data to a relocated repository
48+ if self .previous_location and self .previous_location != repository ._location .canonical_path ():
49+ msg = 'Warning: The repository at location {} was previously located at {}' .format (repository ._location .canonical_path (), self .previous_location )
50+ if not self ._confirm (msg , 'ATTIC_RELOCATED_REPO_ACCESS_IS_OK' ):
51+ raise self .RepositoryAccessAborted ()
52+
4653 if sync and self .manifest .id != self .manifest_id :
4754 # If repository is older than the cache something fishy is going on
4855 if self .timestamp and self .timestamp > manifest .timestamp :
@@ -56,6 +63,16 @@ def __init__(self, repository, key, manifest, path=None, sync=True, warn_if_unen
5663 def __del__ (self ):
5764 self .close ()
5865
66+ def _confirm (self , message , env_var_override = None ):
67+ print (message , file = sys .stderr )
68+ if env_var_override and os .environ .get (env_var_override ):
69+ print ("Yes (From {})" .format (env_var_override ))
70+ return True
71+ if sys .stdin .isatty ():
72+ return False
73+ answer = input ('Do you want to continue? [yN] ' )
74+ return answer and answer in 'Yy'
75+
5976 def create (self ):
6077 """Create a new empty cache at `path`
6178 """
@@ -86,12 +103,14 @@ def open(self):
86103 self .manifest_id = unhexlify (self .config .get ('cache' , 'manifest' ))
87104 self .timestamp = self .config .get ('cache' , 'timestamp' , fallback = None )
88105 self .key_type = self .config .get ('cache' , 'key_type' , fallback = None )
106+ self .previous_location = self .config .get ('cache' , 'previous_location' , fallback = None )
89107 self .chunks = ChunkIndex .read (os .path .join (self .path , 'chunks' ).encode ('utf-8' ))
90108 self .files = None
91109
92110 def close (self ):
93111 if self .lock :
94112 self .lock .release ()
113+ self .lock = None
95114
96115 def _read_files (self ):
97116 self .files = {}
@@ -134,6 +153,7 @@ def commit(self):
134153 self .config .set ('cache' , 'manifest' , hexlify (self .manifest .id ).decode ('ascii' ))
135154 self .config .set ('cache' , 'timestamp' , self .manifest .timestamp )
136155 self .config .set ('cache' , 'key_type' , str (self .key .TYPE ))
156+ self .config .set ('cache' , 'previous_location' , self .repository ._location .canonical_path ())
137157 with open (os .path .join (self .path , 'config' ), 'w' ) as fd :
138158 self .config .write (fd )
139159 self .chunks .write (os .path .join (self .path , 'chunks' ).encode ('utf-8' ))
0 commit comments