simplify control flow in RestoreHelper methods
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Wed, 2 Nov 2016 16:42:33 +0000 (17:42 +0100)
committerPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 3 Nov 2016 09:57:22 +0000 (10:57 +0100)
Make the control flow more obvious. The code in question was
introduced with commit ea6d3c3e… but did not make sense back then
either because cur_index which is the constant $1$ was compared
to the literal constant $0$:

+        cur_index = 1
+        while cur_index < len(self._data):
+            data = self._data[cur_index]

+                if cur_index == 0:

This bogus test was since removed but the convoluted ``while``
loop survived. Instead, access index 1 only once using an integer
literal.

deltatar/deltatar.py

index 5c3d9b5..508048e 100644 (file)
@@ -1481,7 +1481,7 @@ class RestoreHelper(object):
         # if path is found in the first index as to be snapshotted, deal with it
         # and finish
         if path.startswith('snapshot://'):
-            self.restore_file(itpath, data, path, l_no, self._deltatar.unprefixed(path))
+            self.restore_file(itpath, data, path, l_no, upath)
 
             # now we restore parent_directory mtime
             os.utime(parent_dir, (parent_dir_mtime, parent_dir_mtime))
@@ -1489,28 +1489,27 @@ class RestoreHelper(object):
 
         # we go from index to index, finding the path in the index, then finding
         # the index with the most recent snapshot of the file being restored
-        cur_index = 1
-        while cur_index < len(self._data):
-            data = self._data[cur_index]
-            d, l_no, dpath = self.find_path_in_index(data, upath)
-            if not d:
-                self._deltatar.logger.warn('Error restoring file %s from '
-                    'index, not found in index %s' % (path, data['path']))
-                return
-
-            if d.get('path', '').startswith('delete://'):
-                self._deltatar.logger.warn(('Strange thing happened, file '
-                    '%s was listed in first index but deleted by another '
-                    'one. Path was ignored and untouched.') % path)
-                return
-            elif d.get('path', '').startswith('snapshot://'):
-                self.restore_file(d, data, path, l_no, dpath)
-
-                # now we restore parent_directory mtime
-                os.utime(parent_dir, (parent_dir_mtime, parent_dir_mtime))
-                return
-            elif d.get('path', '').startswith('list://'):
-                continue
+        if len(self._data) > 1:
+            data = self._data[1]
+            while True:
+                d, l_no, dpath = self.find_path_in_index(data, upath)
+                if not d:
+                    self._deltatar.logger.warn('Error restoring file %s from '
+                                               'index, not found in index %s' % (path, data['path']))
+                    return
+
+                cur_path = d.get('path', '')
+                if cur_path.startswith('delete://'):
+                    self._deltatar.logger.warn(('Strange thing happened, file '
+                                                '%s was listed in first index but deleted by another '
+                                                'one. Path was ignored and untouched.') % path)
+                    return
+                elif cur_path.startswith('snapshot://'):
+                    self.restore_file(d, data, path, l_no, dpath)
+
+                    # now we restore parent_directory mtime
+                    os.utime(parent_dir, (parent_dir_mtime, parent_dir_mtime))
+                    return
 
         self._deltatar.logger.warn(('Error restoring file %s from index, '
                                     'snapshot not found in any index') % path)
@@ -1519,17 +1518,16 @@ class RestoreHelper(object):
         # NOTE: we restart the iterator sometimes because the iterator can be
         # walked over completely multiple times, for example if one path if not
         # found in one index and we have to go to the next index.
-        if data['iterator'] is None:
+        it = data['iterator']
+        if it is None:
             it = data['iterator'] = self._deltatar.iterate_index_path(data["path"])
             d, l_no = it.__next__()
         else:
-            it = data['iterator']
             d = data['last_itelement']
             l_no = data['last_lno']
 
-        dpath = self._deltatar.unprefixed(d.get('path', ''))
-
         while True:
+            dpath = self._deltatar.unprefixed(d.get('path', ''))
             if upath == dpath:
                 data['last_itelement'] = d
                 data['last_lno'] = l_no
@@ -1549,7 +1547,6 @@ class RestoreHelper(object):
                 data['last_itelement'] = d
                 data['last_lno'] = l_no
                 return None, 0, ''
-            dpath = self._deltatar.unprefixed(d.get('path', ''))
 
     def restore_directories_permissions(self):
         '''