2009-06-18 23:43:44 +08:00
|
|
|
# Copyright (C) 2009 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
import common
|
|
|
|
|
|
|
|
class EdifyGenerator(object):
|
|
|
|
"""Class to generate scripts in the 'edify' recovery script language
|
|
|
|
used from donut onwards."""
|
|
|
|
|
2015-06-23 10:17:41 +08:00
|
|
|
def __init__(self, version, info, fstab=None):
|
2009-06-18 23:43:44 +08:00
|
|
|
self.script = []
|
|
|
|
self.mounts = set()
|
2016-02-05 06:26:02 +08:00
|
|
|
self._required_cache = 0
|
2009-06-18 23:43:44 +08:00
|
|
|
self.version = version
|
2010-07-02 06:30:11 +08:00
|
|
|
self.info = info
|
2015-06-23 10:17:41 +08:00
|
|
|
if fstab is None:
|
|
|
|
self.fstab = self.info.get("fstab", None)
|
|
|
|
else:
|
|
|
|
self.fstab = fstab
|
2009-06-18 23:43:44 +08:00
|
|
|
|
|
|
|
def MakeTemporary(self):
|
|
|
|
"""Make a temporary script object whose commands can latter be
|
|
|
|
appended to the parent script with AppendScript(). Used when the
|
|
|
|
caller wants to generate script commands out-of-order."""
|
2010-07-08 04:53:32 +08:00
|
|
|
x = EdifyGenerator(self.version, self.info)
|
2009-06-18 23:43:44 +08:00
|
|
|
x.mounts = self.mounts
|
|
|
|
return x
|
|
|
|
|
2016-02-05 06:26:02 +08:00
|
|
|
@property
|
|
|
|
def required_cache(self):
|
|
|
|
"""Return the minimum cache size to apply the update."""
|
|
|
|
return self._required_cache
|
|
|
|
|
2009-06-18 23:43:44 +08:00
|
|
|
@staticmethod
|
2015-03-24 10:13:21 +08:00
|
|
|
def WordWrap(cmd, linelen=80):
|
2009-06-18 23:43:44 +08:00
|
|
|
"""'cmd' should be a function call with null characters after each
|
|
|
|
parameter (eg, "somefun(foo,\0bar,\0baz)"). This function wraps cmd
|
|
|
|
to a given line length, replacing nulls with spaces and/or newlines
|
|
|
|
to format it nicely."""
|
|
|
|
indent = cmd.index("(")+1
|
|
|
|
out = []
|
|
|
|
first = True
|
|
|
|
x = re.compile("^(.{,%d})\0" % (linelen-indent,))
|
|
|
|
while True:
|
|
|
|
if not first:
|
|
|
|
out.append(" " * indent)
|
|
|
|
first = False
|
|
|
|
m = x.search(cmd)
|
|
|
|
if not m:
|
|
|
|
parts = cmd.split("\0", 1)
|
|
|
|
out.append(parts[0]+"\n")
|
|
|
|
if len(parts) == 1:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
cmd = parts[1]
|
|
|
|
continue
|
|
|
|
out.append(m.group(1)+"\n")
|
|
|
|
cmd = cmd[m.end():]
|
|
|
|
|
|
|
|
return "".join(out).replace("\0", " ").rstrip("\n")
|
|
|
|
|
|
|
|
def AppendScript(self, other):
|
|
|
|
"""Append the contents of another script (which should be created
|
|
|
|
with temporary=True) to this one."""
|
|
|
|
self.script.extend(other.script)
|
|
|
|
|
2014-04-16 08:40:21 +08:00
|
|
|
def AssertOemProperty(self, name, value):
|
|
|
|
"""Assert that a property on the OEM paritition matches a value."""
|
|
|
|
if not name:
|
|
|
|
raise ValueError("must specify an OEM property")
|
|
|
|
if not value:
|
|
|
|
raise ValueError("must specify the OEM value")
|
2015-03-23 05:20:48 +08:00
|
|
|
cmd = ('file_getprop("/oem/oem.prop", "{name}") == "{value}" || '
|
|
|
|
'abort("This package expects the value \\"{value}\\" for '
|
|
|
|
'\\"{name}\\" on the OEM partition; this has value \\"" + '
|
2015-03-24 10:13:21 +08:00
|
|
|
'file_getprop("/oem/oem.prop", "{name}") + "\\".");').format(
|
|
|
|
name=name, value=value)
|
2014-04-16 08:40:21 +08:00
|
|
|
self.script.append(cmd)
|
|
|
|
|
2009-06-18 23:43:44 +08:00
|
|
|
def AssertSomeFingerprint(self, *fp):
|
2014-05-09 23:29:05 +08:00
|
|
|
"""Assert that the current recovery build fingerprint is one of *fp."""
|
2009-06-18 23:43:44 +08:00
|
|
|
if not fp:
|
|
|
|
raise ValueError("must specify some fingerprints")
|
2015-03-24 10:13:21 +08:00
|
|
|
cmd = (' ||\n '.join([('getprop("ro.build.fingerprint") == "%s"') % i
|
|
|
|
for i in fp]) +
|
2013-06-04 03:07:12 +08:00
|
|
|
' ||\n abort("Package expects build fingerprint of %s; this '
|
2015-03-24 10:13:21 +08:00
|
|
|
'device has " + getprop("ro.build.fingerprint") + ".");') % (
|
|
|
|
" or ".join(fp))
|
2013-06-04 03:07:12 +08:00
|
|
|
self.script.append(cmd)
|
2009-06-18 23:43:44 +08:00
|
|
|
|
2014-04-16 08:40:21 +08:00
|
|
|
def AssertSomeThumbprint(self, *fp):
|
2014-05-09 23:29:05 +08:00
|
|
|
"""Assert that the current recovery build thumbprint is one of *fp."""
|
2014-02-07 11:45:10 +08:00
|
|
|
if not fp:
|
2014-04-16 08:40:21 +08:00
|
|
|
raise ValueError("must specify some thumbprints")
|
2015-03-24 10:13:21 +08:00
|
|
|
cmd = (' ||\n '.join([('getprop("ro.build.thumbprint") == "%s"') % i
|
|
|
|
for i in fp]) +
|
2014-04-16 08:40:21 +08:00
|
|
|
' ||\n abort("Package expects build thumbprint of %s; this '
|
2015-03-24 10:13:21 +08:00
|
|
|
'device has " + getprop("ro.build.thumbprint") + ".");') % (
|
|
|
|
" or ".join(fp))
|
2014-02-07 11:45:10 +08:00
|
|
|
self.script.append(cmd)
|
|
|
|
|
2013-06-04 03:07:12 +08:00
|
|
|
def AssertOlderBuild(self, timestamp, timestamp_text):
|
2009-06-18 23:43:44 +08:00
|
|
|
"""Assert that the build on the device is older (or the same as)
|
|
|
|
the given timestamp."""
|
2013-06-04 03:07:12 +08:00
|
|
|
self.script.append(
|
|
|
|
('(!less_than_int(%s, getprop("ro.build.date.utc"))) || '
|
|
|
|
'abort("Can\'t install this package (%s) over newer '
|
2015-03-24 10:13:21 +08:00
|
|
|
'build (" + getprop("ro.build.date") + ").");') % (timestamp,
|
|
|
|
timestamp_text))
|
2009-06-18 23:43:44 +08:00
|
|
|
|
|
|
|
def AssertDevice(self, device):
|
|
|
|
"""Assert that the device identifier is the given string."""
|
2013-06-04 03:07:12 +08:00
|
|
|
cmd = ('getprop("ro.product.device") == "%s" || '
|
|
|
|
'abort("This package is for \\"%s\\" devices; '
|
2015-03-24 10:13:21 +08:00
|
|
|
'this is a \\"" + getprop("ro.product.device") + "\\".");') % (
|
|
|
|
device, device)
|
2013-06-04 03:07:12 +08:00
|
|
|
self.script.append(cmd)
|
2009-06-18 23:43:44 +08:00
|
|
|
|
|
|
|
def AssertSomeBootloader(self, *bootloaders):
|
|
|
|
"""Asert that the bootloader version is one of *bootloaders."""
|
|
|
|
cmd = ("assert(" +
|
|
|
|
" ||\0".join(['getprop("ro.bootloader") == "%s"' % (b,)
|
|
|
|
for b in bootloaders]) +
|
|
|
|
");")
|
2015-03-24 10:13:21 +08:00
|
|
|
self.script.append(self.WordWrap(cmd))
|
2009-06-18 23:43:44 +08:00
|
|
|
|
|
|
|
def ShowProgress(self, frac, dur):
|
|
|
|
"""Update the progress bar, advancing it over 'frac' over the next
|
2009-09-21 05:03:55 +08:00
|
|
|
'dur' seconds. 'dur' may be zero to advance it via SetProgress
|
|
|
|
commands instead of by time."""
|
2009-06-18 23:43:44 +08:00
|
|
|
self.script.append("show_progress(%f, %d);" % (frac, int(dur)))
|
|
|
|
|
2009-09-21 05:03:55 +08:00
|
|
|
def SetProgress(self, frac):
|
|
|
|
"""Set the position of the progress bar within the chunk defined
|
|
|
|
by the most recent ShowProgress call. 'frac' should be in
|
|
|
|
[0,1]."""
|
|
|
|
self.script.append("set_progress(%f);" % (frac,))
|
|
|
|
|
2009-06-18 23:43:44 +08:00
|
|
|
def PatchCheck(self, filename, *sha1):
|
2010-02-23 07:41:53 +08:00
|
|
|
"""Check that the given file (or MTD reference) has one of the
|
|
|
|
given *sha1 hashes, checking the version saved in cache if the
|
|
|
|
file does not match."""
|
2013-06-04 03:07:12 +08:00
|
|
|
self.script.append(
|
|
|
|
'apply_patch_check("%s"' % (filename,) +
|
|
|
|
"".join([', "%s"' % (i,) for i in sha1]) +
|
|
|
|
') || abort("\\"%s\\" has unexpected contents.");' % (filename,))
|
2010-02-23 07:41:53 +08:00
|
|
|
|
2015-11-10 08:58:28 +08:00
|
|
|
def Verify(self, filename):
|
|
|
|
"""Check that the given file (or MTD reference) has one of the
|
|
|
|
given hashes (encoded in the filename)."""
|
|
|
|
self.script.append(
|
|
|
|
'apply_patch_check("{filename}") && '
|
|
|
|
'ui_print(" Verified.") || '
|
|
|
|
'ui_print("\\"{filename}\\" has unexpected contents.");'.format(
|
|
|
|
filename=filename))
|
|
|
|
|
2010-02-23 07:41:53 +08:00
|
|
|
def FileCheck(self, filename, *sha1):
|
2009-06-18 23:43:44 +08:00
|
|
|
"""Check that the given file (or MTD reference) has one of the
|
|
|
|
given *sha1 hashes."""
|
2010-02-18 08:09:18 +08:00
|
|
|
self.script.append('assert(sha1_check(read_file("%s")' % (filename,) +
|
2009-06-18 23:43:44 +08:00
|
|
|
"".join([', "%s"' % (i,) for i in sha1]) +
|
|
|
|
'));')
|
|
|
|
|
|
|
|
def CacheFreeSpaceCheck(self, amount):
|
|
|
|
"""Check that there's at least 'amount' space that can be made
|
|
|
|
available on /cache."""
|
2016-02-05 06:26:02 +08:00
|
|
|
self._required_cache = max(self._required_cache, amount)
|
2013-06-04 03:07:12 +08:00
|
|
|
self.script.append(('apply_patch_space(%d) || abort("Not enough free space '
|
2015-06-04 00:24:08 +08:00
|
|
|
'on /cache to apply patches.");') % (amount,))
|
2009-06-18 23:43:44 +08:00
|
|
|
|
2014-10-23 08:21:48 +08:00
|
|
|
def Mount(self, mount_point, mount_options_by_format=""):
|
|
|
|
"""Mount the partition with the given mount_point.
|
|
|
|
mount_options_by_format:
|
|
|
|
[fs_type=option[,option]...[|fs_type=option[,option]...]...]
|
|
|
|
where option is optname[=optvalue]
|
|
|
|
E.g. ext4=barrier=1,nodelalloc,errors=panic|f2fs=errors=recover
|
|
|
|
"""
|
2015-06-23 10:17:41 +08:00
|
|
|
fstab = self.fstab
|
2010-09-21 09:04:41 +08:00
|
|
|
if fstab:
|
|
|
|
p = fstab[mount_point]
|
2014-10-23 08:21:48 +08:00
|
|
|
mount_dict = {}
|
|
|
|
if mount_options_by_format is not None:
|
|
|
|
for option in mount_options_by_format.split("|"):
|
|
|
|
if "=" in option:
|
|
|
|
key, value = option.split("=", 1)
|
|
|
|
mount_dict[key] = value
|
2015-06-11 03:32:41 +08:00
|
|
|
mount_flags = mount_dict.get(p.fs_type, "")
|
|
|
|
if p.context is not None:
|
|
|
|
mount_flags = p.context + ("," + mount_flags if mount_flags else "")
|
2015-03-24 10:13:21 +08:00
|
|
|
self.script.append('mount("%s", "%s", "%s", "%s", "%s");' % (
|
|
|
|
p.fs_type, common.PARTITION_TYPES[p.fs_type], p.device,
|
2015-06-11 03:32:41 +08:00
|
|
|
p.mount_point, mount_flags))
|
2010-09-21 09:04:41 +08:00
|
|
|
self.mounts.add(p.mount_point)
|
2009-06-18 23:43:44 +08:00
|
|
|
|
|
|
|
def UnpackPackageDir(self, src, dst):
|
|
|
|
"""Unpack a given directory from the OTA package into the given
|
|
|
|
destination directory."""
|
|
|
|
self.script.append('package_extract_dir("%s", "%s");' % (src, dst))
|
|
|
|
|
|
|
|
def Comment(self, comment):
|
|
|
|
"""Write a comment into the update script."""
|
|
|
|
self.script.append("")
|
|
|
|
for i in comment.split("\n"):
|
|
|
|
self.script.append("# " + i)
|
|
|
|
self.script.append("")
|
|
|
|
|
|
|
|
def Print(self, message):
|
|
|
|
"""Log a message to the screen (if the logs are visible)."""
|
|
|
|
self.script.append('ui_print("%s");' % (message,))
|
|
|
|
|
2014-11-21 16:46:03 +08:00
|
|
|
def TunePartition(self, partition, *options):
|
2015-06-23 10:17:41 +08:00
|
|
|
fstab = self.fstab
|
2014-11-21 16:46:03 +08:00
|
|
|
if fstab:
|
|
|
|
p = fstab[partition]
|
2015-03-24 10:13:21 +08:00
|
|
|
if p.fs_type not in ("ext2", "ext3", "ext4"):
|
2014-11-21 16:46:03 +08:00
|
|
|
raise ValueError("Partition %s cannot be tuned\n" % (partition,))
|
2015-03-24 10:13:21 +08:00
|
|
|
self.script.append(
|
|
|
|
'tune2fs(' + "".join(['"%s", ' % (i,) for i in options]) +
|
|
|
|
'"%s") || abort("Failed to tune partition %s");' % (
|
|
|
|
p.device, partition))
|
2014-11-21 16:46:03 +08:00
|
|
|
|
2009-06-18 23:43:44 +08:00
|
|
|
def FormatPartition(self, partition):
|
2010-09-21 09:04:41 +08:00
|
|
|
"""Format the given partition, specified by its mount point (eg,
|
|
|
|
"/system")."""
|
|
|
|
|
2015-06-23 10:17:41 +08:00
|
|
|
fstab = self.fstab
|
2010-09-21 09:04:41 +08:00
|
|
|
if fstab:
|
|
|
|
p = fstab[partition]
|
2012-04-10 03:27:43 +08:00
|
|
|
self.script.append('format("%s", "%s", "%s", "%s", "%s");' %
|
2011-02-18 07:54:20 +08:00
|
|
|
(p.fs_type, common.PARTITION_TYPES[p.fs_type],
|
2012-04-10 03:27:43 +08:00
|
|
|
p.device, p.length, p.mount_point))
|
2009-06-18 23:43:44 +08:00
|
|
|
|
2014-02-25 00:13:45 +08:00
|
|
|
def WipeBlockDevice(self, partition):
|
2014-06-17 06:16:31 +08:00
|
|
|
if partition not in ("/system", "/vendor"):
|
|
|
|
raise ValueError(("WipeBlockDevice doesn't work on %s\n") % (partition,))
|
2015-06-23 10:17:41 +08:00
|
|
|
fstab = self.fstab
|
2014-06-17 06:16:31 +08:00
|
|
|
size = self.info.get(partition.lstrip("/") + "_size", None)
|
2014-02-25 00:13:45 +08:00
|
|
|
device = fstab[partition].device
|
|
|
|
|
|
|
|
self.script.append('wipe_block_device("%s", %s);' % (device, size))
|
|
|
|
|
2009-06-18 23:43:44 +08:00
|
|
|
def DeleteFiles(self, file_list):
|
|
|
|
"""Delete all files in file_list."""
|
2015-03-24 10:13:21 +08:00
|
|
|
if not file_list:
|
|
|
|
return
|
2009-06-18 23:43:44 +08:00
|
|
|
cmd = "delete(" + ",\0".join(['"%s"' % (i,) for i in file_list]) + ");"
|
2015-03-24 10:13:21 +08:00
|
|
|
self.script.append(self.WordWrap(cmd))
|
2009-06-18 23:43:44 +08:00
|
|
|
|
2015-09-03 01:28:08 +08:00
|
|
|
def DeleteFilesIfNotMatching(self, file_list):
|
|
|
|
"""Delete the file in file_list if not matching the checksum."""
|
|
|
|
if not file_list:
|
|
|
|
return
|
|
|
|
for name, sha1 in file_list:
|
|
|
|
cmd = ('sha1_check(read_file("{name}"), "{sha1}") || '
|
|
|
|
'delete("{name}");'.format(name=name, sha1=sha1))
|
|
|
|
self.script.append(self.WordWrap(cmd))
|
|
|
|
|
2013-12-14 10:06:28 +08:00
|
|
|
def RenameFile(self, srcfile, tgtfile):
|
|
|
|
"""Moves a file from one location to another."""
|
|
|
|
if self.info.get("update_rename_support", False):
|
|
|
|
self.script.append('rename("%s", "%s");' % (srcfile, tgtfile))
|
|
|
|
else:
|
|
|
|
raise ValueError("Rename not supported by update binary")
|
|
|
|
|
|
|
|
def SkipNextActionIfTargetExists(self, tgtfile, tgtsha1):
|
|
|
|
"""Prepend an action with an apply_patch_check in order to
|
|
|
|
skip the action if the file exists. Used when a patch
|
|
|
|
is later renamed."""
|
2015-09-03 01:28:08 +08:00
|
|
|
cmd = ('sha1_check(read_file("%s"), %s) ||' % (tgtfile, tgtsha1))
|
2015-03-24 10:13:21 +08:00
|
|
|
self.script.append(self.WordWrap(cmd))
|
2013-12-14 10:06:28 +08:00
|
|
|
|
2009-06-18 23:43:44 +08:00
|
|
|
def ApplyPatch(self, srcfile, tgtfile, tgtsize, tgtsha1, *patchpairs):
|
|
|
|
"""Apply binary patches (in *patchpairs) to the given srcfile to
|
|
|
|
produce tgtfile (which may be "-" to indicate overwriting the
|
|
|
|
source file."""
|
|
|
|
if len(patchpairs) % 2 != 0 or len(patchpairs) == 0:
|
|
|
|
raise ValueError("bad patches given to ApplyPatch")
|
|
|
|
cmd = ['apply_patch("%s",\0"%s",\0%s,\0%d'
|
|
|
|
% (srcfile, tgtfile, tgtsha1, tgtsize)]
|
|
|
|
for i in range(0, len(patchpairs), 2):
|
2015-12-02 09:46:46 +08:00
|
|
|
cmd.append(',\0%s,\0package_extract_file("%s")' % patchpairs[i:i+2])
|
|
|
|
cmd.append(') ||\n abort("Failed to apply patch to %s");' % (srcfile,))
|
2009-06-18 23:43:44 +08:00
|
|
|
cmd = "".join(cmd)
|
2015-03-24 10:13:21 +08:00
|
|
|
self.script.append(self.WordWrap(cmd))
|
2009-06-18 23:43:44 +08:00
|
|
|
|
2014-02-25 00:13:45 +08:00
|
|
|
def WriteRawImage(self, mount_point, fn, mapfn=None):
|
2010-09-21 09:04:41 +08:00
|
|
|
"""Write the given package file into the partition for the given
|
|
|
|
mount point."""
|
2010-07-02 06:30:11 +08:00
|
|
|
|
2015-06-23 10:17:41 +08:00
|
|
|
fstab = self.fstab
|
2010-09-21 09:04:41 +08:00
|
|
|
if fstab:
|
|
|
|
p = fstab[mount_point]
|
2010-09-27 05:57:41 +08:00
|
|
|
partition_type = common.PARTITION_TYPES[p.fs_type]
|
2010-09-21 09:04:41 +08:00
|
|
|
args = {'device': p.device, 'fn': fn}
|
|
|
|
if partition_type == "MTD":
|
|
|
|
self.script.append(
|
2011-04-13 06:50:17 +08:00
|
|
|
'write_raw_image(package_extract_file("%(fn)s"), "%(device)s");'
|
|
|
|
% args)
|
2010-09-21 09:04:41 +08:00
|
|
|
elif partition_type == "EMMC":
|
2014-02-25 00:13:45 +08:00
|
|
|
if mapfn:
|
|
|
|
args["map"] = mapfn
|
|
|
|
self.script.append(
|
|
|
|
'package_extract_file("%(fn)s", "%(device)s", "%(map)s");' % args)
|
|
|
|
else:
|
|
|
|
self.script.append(
|
|
|
|
'package_extract_file("%(fn)s", "%(device)s");' % args)
|
2010-09-21 09:04:41 +08:00
|
|
|
else:
|
2015-03-24 10:13:21 +08:00
|
|
|
raise ValueError(
|
|
|
|
"don't know how to write \"%s\" partitions" % p.fs_type)
|
2009-06-18 23:43:44 +08:00
|
|
|
|
2013-09-08 08:10:29 +08:00
|
|
|
def SetPermissions(self, fn, uid, gid, mode, selabel, capabilities):
|
2009-06-18 23:43:44 +08:00
|
|
|
"""Set file ownership and permissions."""
|
2013-09-08 08:10:29 +08:00
|
|
|
if not self.info.get("use_set_metadata", False):
|
|
|
|
self.script.append('set_perm(%d, %d, 0%o, "%s");' % (uid, gid, mode, fn))
|
|
|
|
else:
|
2015-03-24 10:13:21 +08:00
|
|
|
if capabilities is None:
|
|
|
|
capabilities = "0x0"
|
2013-09-08 08:10:29 +08:00
|
|
|
cmd = 'set_metadata("%s", "uid", %d, "gid", %d, "mode", 0%o, ' \
|
|
|
|
'"capabilities", %s' % (fn, uid, gid, mode, capabilities)
|
|
|
|
if selabel is not None:
|
2015-03-24 10:13:21 +08:00
|
|
|
cmd += ', "selabel", "%s"' % selabel
|
2013-09-08 08:10:29 +08:00
|
|
|
cmd += ');'
|
|
|
|
self.script.append(cmd)
|
2009-06-18 23:43:44 +08:00
|
|
|
|
2015-03-24 10:13:21 +08:00
|
|
|
def SetPermissionsRecursive(self, fn, uid, gid, dmode, fmode, selabel,
|
|
|
|
capabilities):
|
2009-06-18 23:43:44 +08:00
|
|
|
"""Recursively set path ownership and permissions."""
|
2013-09-08 08:10:29 +08:00
|
|
|
if not self.info.get("use_set_metadata", False):
|
|
|
|
self.script.append('set_perm_recursive(%d, %d, 0%o, 0%o, "%s");'
|
|
|
|
% (uid, gid, dmode, fmode, fn))
|
|
|
|
else:
|
2015-03-24 10:13:21 +08:00
|
|
|
if capabilities is None:
|
|
|
|
capabilities = "0x0"
|
2013-09-08 08:10:29 +08:00
|
|
|
cmd = 'set_metadata_recursive("%s", "uid", %d, "gid", %d, ' \
|
|
|
|
'"dmode", 0%o, "fmode", 0%o, "capabilities", %s' \
|
|
|
|
% (fn, uid, gid, dmode, fmode, capabilities)
|
|
|
|
if selabel is not None:
|
2015-03-24 10:13:21 +08:00
|
|
|
cmd += ', "selabel", "%s"' % selabel
|
2013-09-08 08:10:29 +08:00
|
|
|
cmd += ');'
|
|
|
|
self.script.append(cmd)
|
2009-06-18 23:43:44 +08:00
|
|
|
|
|
|
|
def MakeSymlinks(self, symlink_list):
|
|
|
|
"""Create symlinks, given a list of (dest, link) pairs."""
|
|
|
|
by_dest = {}
|
|
|
|
for d, l in symlink_list:
|
|
|
|
by_dest.setdefault(d, []).append(l)
|
|
|
|
|
|
|
|
for dest, links in sorted(by_dest.iteritems()):
|
|
|
|
cmd = ('symlink("%s", ' % (dest,) +
|
|
|
|
",\0".join(['"' + i + '"' for i in sorted(links)]) + ");")
|
2015-03-24 10:13:21 +08:00
|
|
|
self.script.append(self.WordWrap(cmd))
|
2009-06-18 23:43:44 +08:00
|
|
|
|
|
|
|
def AppendExtra(self, extra):
|
|
|
|
"""Append text verbatim to the output script."""
|
|
|
|
self.script.append(extra)
|
|
|
|
|
2014-10-29 10:24:19 +08:00
|
|
|
def Unmount(self, mount_point):
|
2015-03-24 10:13:21 +08:00
|
|
|
self.script.append('unmount("%s");' % mount_point)
|
|
|
|
self.mounts.remove(mount_point)
|
2014-10-29 10:24:19 +08:00
|
|
|
|
2010-02-03 05:12:04 +08:00
|
|
|
def UnmountAll(self):
|
|
|
|
for p in sorted(self.mounts):
|
|
|
|
self.script.append('unmount("%s");' % (p,))
|
|
|
|
self.mounts = set()
|
|
|
|
|
2009-06-18 23:43:44 +08:00
|
|
|
def AddToZip(self, input_zip, output_zip, input_path=None):
|
|
|
|
"""Write the accumulated script to the output_zip file. input_zip
|
|
|
|
is used as the source for the 'updater' binary needed to run
|
|
|
|
script. If input_path is not None, it will be used as a local
|
|
|
|
path for the binary instead of input_zip."""
|
|
|
|
|
2010-02-03 05:12:04 +08:00
|
|
|
self.UnmountAll()
|
2009-06-18 23:43:44 +08:00
|
|
|
|
|
|
|
common.ZipWriteStr(output_zip, "META-INF/com/google/android/updater-script",
|
|
|
|
"\n".join(self.script) + "\n")
|
|
|
|
|
|
|
|
if input_path is None:
|
|
|
|
data = input_zip.read("OTA/bin/updater")
|
|
|
|
else:
|
2014-03-04 02:21:27 +08:00
|
|
|
data = open(input_path, "rb").read()
|
2009-06-18 23:43:44 +08:00
|
|
|
common.ZipWriteStr(output_zip, "META-INF/com/google/android/update-binary",
|
2015-03-24 10:13:21 +08:00
|
|
|
data, perms=0o755)
|