BmapFlasher: do not postpone size initialization for uncompressed images
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 6 Nov 2012 12:39:11 +0000 (14:39 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 7 Nov 2012 08:50:45 +0000 (10:50 +0200)
When we are flashing images without bmap, we cannot initialize various
size-related attributes until we flash the image if the image is compressed.
However, we can do this for uncompressed images.

Change-Id: I8b8e604e68f680acbd059c7a44f92c654a9b4755
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/BmapFlasher.py

index 12125b2..54060df 100644 (file)
@@ -150,6 +150,7 @@ class BmapFlasher:
                 import bz2
                 self._f_image = bz2.BZ2File(self._image_path, 'rb')
             else:
+                self._image_is_compressed = False
                 self._f_image = open(self._image_path, 'rb')
         except IOError as err:
             raise Error("cannot open image file '%s': %s" \
@@ -191,6 +192,8 @@ class BmapFlasher:
         self._f_bmap  = None
 
         self._xml = None
+        self._image_is_compressed = True
+
         self.bmap_version = None
         self.bmap_block_size = None
         self.bmap_blocks_cnt = None
@@ -212,14 +215,18 @@ class BmapFlasher:
                             % (bmap_path, err.strerror))
             self._parse_bmap()
         else:
-            # There is no bmap. Initialize user-visible variables to something
+            # There is no bmap. Initialize user-visible attributes to something
             # sensible with an assumption that we just have all blocks mapped.
-            # Note, we do not know image size before we read it (thing about
-            # compressed image), so we only initialize some of the variables.
             self.bmap_version = 0
             self.bmap_block_size = 4096
             self.bmap_mapped_percent = 100
 
+            # We can initialize size-related attributes only if we the image is
+            # uncompressed.
+            if not self._image_is_compressed:
+                image_size = os.fstat(self._f_image.fileno()).st_size
+                self._initialize_sizes(image_size)
+
     def __del__(self):
         """ The class destructor which closes the opened files. """
 
@@ -311,7 +318,8 @@ class BmapFlasher:
 
             image_size += len(chunk)
 
-        self._initialize_sizes(image_size)
+        if self._image_is_compressed:
+            self._initialize_sizes(image_size)
 
         if sync:
             self.sync()