Fix a bug in computing streaming property of payload.bin
When computing the data offset of an entry in zip file, we used length
of extra field from central directory. That is correct most of the time
but wrong if the extra field in central directory has different length
than the one in local file directory. Since python's zipfile doesn't
provide an API to access local file header, we need to parse local file
header ourselves and extract length of extra field.
An incorrect offset will cause magic mismatch error from update_engine,
as update_engine expects to find uncompressed payload at the recorded
offset.
Test: th, partner verification
Bug: 191443484
Change-Id: Id670cd79b0bd65adffaaa5224ae4f8065d66b358
diff --git a/tools/releasetools/ota_utils.py b/tools/releasetools/ota_utils.py
index 28c246b..5737009 100644
--- a/tools/releasetools/ota_utils.py
+++ b/tools/releasetools/ota_utils.py
@@ -16,6 +16,7 @@
import itertools
import logging
import os
+import struct
import zipfile
import ota_metadata_pb2
@@ -399,6 +400,35 @@
return device_names, fingerprints
+def GetZipEntryOffset(zfp, entry_info):
+ """Get offset to a beginning of a particular zip entry
+ Args:
+ fp: zipfile.ZipFile
+ entry_info: zipfile.ZipInfo
+
+ Returns:
+ (offset, size) tuple
+ """
+ # Don't use len(entry_info.extra). Because that returns size of extra
+ # fields in central directory. We need to look at local file directory,
+ # as these two might have different sizes.
+
+ # We cannot work with zipfile.ZipFile instances, we need a |fp| for the underlying file.
+ zfp = zfp.fp
+ zfp.seek(entry_info.header_offset)
+ data = zfp.read(zipfile.sizeFileHeader)
+ fheader = struct.unpack(zipfile.structFileHeader, data)
+ # Last two fields of local file header are filename length and
+ # extra length
+ filename_len = fheader[-2]
+ extra_len = fheader[-1]
+ offset = entry_info.header_offset
+ offset += zipfile.sizeFileHeader
+ offset += filename_len + extra_len
+ size = entry_info.file_size
+ return (offset, size)
+
+
class PropertyFiles(object):
"""A class that computes the property-files string for an OTA package.
@@ -517,10 +547,7 @@
def ComputeEntryOffsetSize(name):
"""Computes the zip entry offset and size."""
info = zip_file.getinfo(name)
- offset = info.header_offset
- offset += zipfile.sizeFileHeader
- offset += len(info.extra) + len(info.filename)
- size = info.file_size
+ (offset, size) = GetZipEntryOffset(zip_file, info)
return '%s:%d:%d' % (os.path.basename(name), offset, size)
tokens = []