Fiemap: improve the API
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Fri, 23 Nov 2012 11:56:16 +0000 (13:56 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Fri, 23 Nov 2012 12:12:52 +0000 (14:12 +0200)
Improve the API and allow to specify the area of the file to generate ranges
for, so that it would be possible to generate ranges only for part of the file.

Change-Id: I1e8455be739d9835bc3f750709fbe7d8e9374ee7
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/BmapCreate.py
bmaptools/Fiemap.py
tests/test_api_base.py

index e48642c..6e53c81 100644 (file)
@@ -219,7 +219,7 @@ class BmapCreate:
         # Generate the block map and write it to the XML block map
         # file as we go.
         self.mapped_cnt = 0
-        for first, last in self.fiemap.get_mapped_ranges():
+        for first, last in self.fiemap.get_mapped_ranges(0, self.blocks_cnt):
             self.mapped_cnt += last - first + 1
             if include_checksums:
                 sha1 = self._calculate_sha1(first, last)
index 00d602a..a7748e4 100644 (file)
@@ -118,12 +118,13 @@ class Fiemap:
 
         return not self.block_is_mapped(block)
 
-    def _get_ranges(self, test_func):
-        """ Internal helper generator which produces list of mapped or unmapped
-        blocks. The 'test_func' is a function object which tests whether a
-        block is mapped or unmapped. """
+    @staticmethod
+    def _get_ranges(start, count, test_func):
+        """ Internal helper function which implements 'get_mapped_ranges()' and
+        'get_unmapped_ranges()', depending whethier 'test_func' is a
+        'block_is_mapped()' or 'block_is_unmapped()' object. """
 
-        iterator = xrange(self.blocks_cnt)
+        iterator = xrange(start, count)
         for key, group in itertools.groupby(iterator, test_func):
             if key:
                 # Find the first and the last elements of the group
@@ -133,12 +134,18 @@ class Fiemap:
                     pass
                 yield first, last
 
-    def get_mapped_ranges(self):
-        """ Generate ranges of mapped blocks in the file. """
+    def get_mapped_ranges(self, start, count):
+        """ Generate ranges of mapped blocks in the file. The ranges are tuples
+        of 2 elements: [first, last], where 'first' is the first mapped block
+        and 'last' is the last mapped block.
 
-        return self._get_ranges(self.block_is_mapped)
+        The ranges are generated for the area for the file starting from block
+        'start' and 'count' blocks in size. """
 
-    def get_unmapped_ranges(self):
-        """ Generate ranges of unmapped blocks in the file. """
+        return self._get_ranges(start, count, self.block_is_mapped)
 
-        return self._get_ranges(self.block_is_unmapped)
+    def get_unmapped_ranges(self, start, count):
+        """ Just like 'get_mapped_ranges()', but for un-mapped blocks
+        (holes). """
+
+        return self._get_ranges(start, count, self.block_is_unmapped)
index 6976ff1..a218870 100644 (file)
@@ -62,7 +62,7 @@ def create_random_sparse_file(file_obj, size):
 
     # Make sure the Fiemap module reports holes correctly
     fiemap = Fiemap.Fiemap(file_obj)
-    fiemap_iterator = fiemap.get_unmapped_ranges()
+    fiemap_iterator = fiemap.get_unmapped_ranges(0, blocks_cnt)
     iterator = itertools.izip_longest(holes, fiemap_iterator)
     for ours, fiemaps in iterator:
         if ours != fiemaps: