blob: 433a010819e391b1061b250ad4050afafb5cdd4a [file] [log] [blame]
Doug Zongker424296a2014-09-02 08:53:09 -07001# Copyright (C) 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Doug Zongkerfc44a512014-08-26 13:10:25 -070015from __future__ import print_function
16
Doug Zongker6ab2a502016-02-09 08:28:09 -080017import array
Tao Bao8dcf7382015-05-21 14:09:49 -070018import common
Doug Zongker6ab2a502016-02-09 08:28:09 -080019import functools
Doug Zongker62338182014-09-08 08:29:55 -070020import heapq
Doug Zongkerfc44a512014-08-26 13:10:25 -070021import itertools
22import multiprocessing
23import os
Tao Bao3a2e3502016-12-28 09:14:39 -080024import os.path
Doug Zongkerfc44a512014-08-26 13:10:25 -070025import re
26import subprocess
Doug Zongkerfc44a512014-08-26 13:10:25 -070027import threading
28import tempfile
29
Tao Bao3a2e3502016-12-28 09:14:39 -080030from collections import deque, OrderedDict
31from hashlib import sha1
Dan Albert8b72aef2015-03-23 19:13:21 -070032from rangelib import RangeSet
33
Doug Zongkerfc44a512014-08-26 13:10:25 -070034
Doug Zongkerab7ca1d2014-08-26 10:40:28 -070035__all__ = ["EmptyImage", "DataImage", "BlockImageDiff"]
36
Dan Albert8b72aef2015-03-23 19:13:21 -070037
Doug Zongkerfc44a512014-08-26 13:10:25 -070038def compute_patch(src, tgt, imgdiff=False):
39 srcfd, srcfile = tempfile.mkstemp(prefix="src-")
40 tgtfd, tgtfile = tempfile.mkstemp(prefix="tgt-")
41 patchfd, patchfile = tempfile.mkstemp(prefix="patch-")
42 os.close(patchfd)
43
44 try:
45 with os.fdopen(srcfd, "wb") as f_src:
46 for p in src:
47 f_src.write(p)
48
49 with os.fdopen(tgtfd, "wb") as f_tgt:
50 for p in tgt:
51 f_tgt.write(p)
52 try:
53 os.unlink(patchfile)
54 except OSError:
55 pass
56 if imgdiff:
57 p = subprocess.call(["imgdiff", "-z", srcfile, tgtfile, patchfile],
58 stdout=open("/dev/null", "a"),
59 stderr=subprocess.STDOUT)
60 else:
61 p = subprocess.call(["bsdiff", srcfile, tgtfile, patchfile])
62
63 if p:
64 raise ValueError("diff failed: " + str(p))
65
66 with open(patchfile, "rb") as f:
67 return f.read()
68 finally:
69 try:
70 os.unlink(srcfile)
71 os.unlink(tgtfile)
72 os.unlink(patchfile)
73 except OSError:
74 pass
75
Dan Albert8b72aef2015-03-23 19:13:21 -070076
77class Image(object):
78 def ReadRangeSet(self, ranges):
79 raise NotImplementedError
80
Tao Bao68658c02015-06-01 13:40:49 -070081 def TotalSha1(self, include_clobbered_blocks=False):
Dan Albert8b72aef2015-03-23 19:13:21 -070082 raise NotImplementedError
83
84
85class EmptyImage(Image):
Doug Zongkerfc44a512014-08-26 13:10:25 -070086 """A zero-length image."""
87 blocksize = 4096
88 care_map = RangeSet()
Tao Baoff777812015-05-12 11:42:31 -070089 clobbered_blocks = RangeSet()
Tao Baoe9b61912015-07-09 17:37:49 -070090 extended = RangeSet()
Doug Zongkerfc44a512014-08-26 13:10:25 -070091 total_blocks = 0
92 file_map = {}
93 def ReadRangeSet(self, ranges):
94 return ()
Tao Bao68658c02015-06-01 13:40:49 -070095 def TotalSha1(self, include_clobbered_blocks=False):
96 # EmptyImage always carries empty clobbered_blocks, so
97 # include_clobbered_blocks can be ignored.
98 assert self.clobbered_blocks.size() == 0
Doug Zongkerab7ca1d2014-08-26 10:40:28 -070099 return sha1().hexdigest()
100
101
Dan Albert8b72aef2015-03-23 19:13:21 -0700102class DataImage(Image):
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700103 """An image wrapped around a single string of data."""
104
105 def __init__(self, data, trim=False, pad=False):
106 self.data = data
107 self.blocksize = 4096
108
109 assert not (trim and pad)
110
111 partial = len(self.data) % self.blocksize
Tao Bao7589e962015-09-05 20:35:32 -0700112 padded = False
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700113 if partial > 0:
114 if trim:
115 self.data = self.data[:-partial]
116 elif pad:
117 self.data += '\0' * (self.blocksize - partial)
Tao Bao7589e962015-09-05 20:35:32 -0700118 padded = True
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700119 else:
120 raise ValueError(("data for DataImage must be multiple of %d bytes "
121 "unless trim or pad is specified") %
122 (self.blocksize,))
123
124 assert len(self.data) % self.blocksize == 0
125
126 self.total_blocks = len(self.data) / self.blocksize
127 self.care_map = RangeSet(data=(0, self.total_blocks))
Tao Bao7589e962015-09-05 20:35:32 -0700128 # When the last block is padded, we always write the whole block even for
129 # incremental OTAs. Because otherwise the last block may get skipped if
130 # unchanged for an incremental, but would fail the post-install
131 # verification if it has non-zero contents in the padding bytes.
132 # Bug: 23828506
133 if padded:
Tao Bao42206c32015-09-08 13:39:40 -0700134 clobbered_blocks = [self.total_blocks-1, self.total_blocks]
Tao Bao7589e962015-09-05 20:35:32 -0700135 else:
Tao Bao42206c32015-09-08 13:39:40 -0700136 clobbered_blocks = []
137 self.clobbered_blocks = clobbered_blocks
Tao Baoe9b61912015-07-09 17:37:49 -0700138 self.extended = RangeSet()
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700139
140 zero_blocks = []
141 nonzero_blocks = []
142 reference = '\0' * self.blocksize
143
Tao Bao7589e962015-09-05 20:35:32 -0700144 for i in range(self.total_blocks-1 if padded else self.total_blocks):
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700145 d = self.data[i*self.blocksize : (i+1)*self.blocksize]
146 if d == reference:
147 zero_blocks.append(i)
148 zero_blocks.append(i+1)
149 else:
150 nonzero_blocks.append(i)
151 nonzero_blocks.append(i+1)
152
Tao Bao42206c32015-09-08 13:39:40 -0700153 assert zero_blocks or nonzero_blocks or clobbered_blocks
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700154
Tao Bao42206c32015-09-08 13:39:40 -0700155 self.file_map = dict()
156 if zero_blocks:
157 self.file_map["__ZERO"] = RangeSet(data=zero_blocks)
158 if nonzero_blocks:
159 self.file_map["__NONZERO"] = RangeSet(data=nonzero_blocks)
160 if clobbered_blocks:
161 self.file_map["__COPY"] = RangeSet(data=clobbered_blocks)
Tao Bao7589e962015-09-05 20:35:32 -0700162
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700163 def ReadRangeSet(self, ranges):
164 return [self.data[s*self.blocksize:e*self.blocksize] for (s, e) in ranges]
165
Tao Bao68658c02015-06-01 13:40:49 -0700166 def TotalSha1(self, include_clobbered_blocks=False):
Tao Bao7589e962015-09-05 20:35:32 -0700167 if not include_clobbered_blocks:
168 ranges = self.care_map.subtract(self.clobbered_blocks)
169 return sha1(self.ReadRangeSet(ranges)).hexdigest()
170 else:
171 return sha1(self.data).hexdigest()
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700172
Doug Zongkerfc44a512014-08-26 13:10:25 -0700173
174class Transfer(object):
175 def __init__(self, tgt_name, src_name, tgt_ranges, src_ranges, style, by_id):
176 self.tgt_name = tgt_name
177 self.src_name = src_name
178 self.tgt_ranges = tgt_ranges
179 self.src_ranges = src_ranges
180 self.style = style
181 self.intact = (getattr(tgt_ranges, "monotonic", False) and
182 getattr(src_ranges, "monotonic", False))
Tao Baob8c87172015-03-19 19:42:12 -0700183
184 # We use OrderedDict rather than dict so that the output is repeatable;
185 # otherwise it would depend on the hash values of the Transfer objects.
186 self.goes_before = OrderedDict()
187 self.goes_after = OrderedDict()
Doug Zongkerfc44a512014-08-26 13:10:25 -0700188
Doug Zongker62338182014-09-08 08:29:55 -0700189 self.stash_before = []
190 self.use_stash = []
191
Doug Zongkerfc44a512014-08-26 13:10:25 -0700192 self.id = len(by_id)
193 by_id.append(self)
194
Doug Zongker62338182014-09-08 08:29:55 -0700195 def NetStashChange(self):
196 return (sum(sr.size() for (_, sr) in self.stash_before) -
197 sum(sr.size() for (_, sr) in self.use_stash))
198
Tao Bao82c47982015-08-17 09:45:13 -0700199 def ConvertToNew(self):
200 assert self.style != "new"
201 self.use_stash = []
202 self.style = "new"
203 self.src_ranges = RangeSet()
204
Doug Zongkerfc44a512014-08-26 13:10:25 -0700205 def __str__(self):
206 return (str(self.id) + ": <" + str(self.src_ranges) + " " + self.style +
207 " to " + str(self.tgt_ranges) + ">")
208
209
Doug Zongker6ab2a502016-02-09 08:28:09 -0800210@functools.total_ordering
211class HeapItem(object):
212 def __init__(self, item):
213 self.item = item
214 # Negate the score since python's heap is a min-heap and we want
215 # the maximum score.
216 self.score = -item.score
217 def clear(self):
218 self.item = None
219 def __bool__(self):
220 return self.item is None
221 def __eq__(self, other):
222 return self.score == other.score
223 def __le__(self, other):
224 return self.score <= other.score
225
226
Doug Zongkerfc44a512014-08-26 13:10:25 -0700227# BlockImageDiff works on two image objects. An image object is
228# anything that provides the following attributes:
229#
230# blocksize: the size in bytes of a block, currently must be 4096.
231#
232# total_blocks: the total size of the partition/image, in blocks.
233#
234# care_map: a RangeSet containing which blocks (in the range [0,
235# total_blocks) we actually care about; i.e. which blocks contain
236# data.
237#
238# file_map: a dict that partitions the blocks contained in care_map
239# into smaller domains that are useful for doing diffs on.
240# (Typically a domain is a file, and the key in file_map is the
241# pathname.)
242#
Tao Baoff777812015-05-12 11:42:31 -0700243# clobbered_blocks: a RangeSet containing which blocks contain data
244# but may be altered by the FS. They need to be excluded when
245# verifying the partition integrity.
246#
Doug Zongkerfc44a512014-08-26 13:10:25 -0700247# ReadRangeSet(): a function that takes a RangeSet and returns the
248# data contained in the image blocks of that RangeSet. The data
249# is returned as a list or tuple of strings; concatenating the
250# elements together should produce the requested data.
251# Implementations are free to break up the data into list/tuple
252# elements in any way that is convenient.
253#
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700254# TotalSha1(): a function that returns (as a hex string) the SHA-1
255# hash of all the data in the image (ie, all the blocks in the
Tao Bao68658c02015-06-01 13:40:49 -0700256# care_map minus clobbered_blocks, or including the clobbered
257# blocks if include_clobbered_blocks is True).
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700258#
Doug Zongkerfc44a512014-08-26 13:10:25 -0700259# When creating a BlockImageDiff, the src image may be None, in which
260# case the list of transfers produced will never read from the
261# original image.
262
263class BlockImageDiff(object):
Tao Bao293fd132016-06-11 12:19:23 -0700264 def __init__(self, tgt, src=None, threads=None, version=4,
265 disable_imgdiff=False):
Doug Zongkerfc44a512014-08-26 13:10:25 -0700266 if threads is None:
267 threads = multiprocessing.cpu_count() // 2
Dan Albert8b72aef2015-03-23 19:13:21 -0700268 if threads == 0:
269 threads = 1
Doug Zongkerfc44a512014-08-26 13:10:25 -0700270 self.threads = threads
Doug Zongker62338182014-09-08 08:29:55 -0700271 self.version = version
Dan Albert8b72aef2015-03-23 19:13:21 -0700272 self.transfers = []
273 self.src_basenames = {}
274 self.src_numpatterns = {}
Tao Baob4cfca52016-02-04 14:26:02 -0800275 self._max_stashed_size = 0
Tao Baod522bdc2016-04-12 15:53:16 -0700276 self.touched_src_ranges = RangeSet()
277 self.touched_src_sha1 = None
Tao Bao293fd132016-06-11 12:19:23 -0700278 self.disable_imgdiff = disable_imgdiff
Doug Zongker62338182014-09-08 08:29:55 -0700279
Tao Baoeba409c2015-10-21 13:30:43 -0700280 assert version in (1, 2, 3, 4)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700281
282 self.tgt = tgt
283 if src is None:
284 src = EmptyImage()
285 self.src = src
286
287 # The updater code that installs the patch always uses 4k blocks.
288 assert tgt.blocksize == 4096
289 assert src.blocksize == 4096
290
291 # The range sets in each filemap should comprise a partition of
292 # the care map.
293 self.AssertPartition(src.care_map, src.file_map.values())
294 self.AssertPartition(tgt.care_map, tgt.file_map.values())
295
Tao Baob4cfca52016-02-04 14:26:02 -0800296 @property
297 def max_stashed_size(self):
298 return self._max_stashed_size
299
Doug Zongkerfc44a512014-08-26 13:10:25 -0700300 def Compute(self, prefix):
301 # When looking for a source file to use as the diff input for a
302 # target file, we try:
303 # 1) an exact path match if available, otherwise
304 # 2) a exact basename match if available, otherwise
305 # 3) a basename match after all runs of digits are replaced by
306 # "#" if available, otherwise
307 # 4) we have no source for this target.
308 self.AbbreviateSourceNames()
309 self.FindTransfers()
310
311 # Find the ordering dependencies among transfers (this is O(n^2)
312 # in the number of transfers).
313 self.GenerateDigraph()
314 # Find a sequence of transfers that satisfies as many ordering
315 # dependencies as possible (heuristically).
316 self.FindVertexSequence()
317 # Fix up the ordering dependencies that the sequence didn't
318 # satisfy.
Doug Zongker62338182014-09-08 08:29:55 -0700319 if self.version == 1:
320 self.RemoveBackwardEdges()
321 else:
322 self.ReverseBackwardEdges()
323 self.ImproveVertexSequence()
324
Tao Bao82c47982015-08-17 09:45:13 -0700325 # Ensure the runtime stash size is under the limit.
326 if self.version >= 2 and common.OPTIONS.cache_size is not None:
327 self.ReviseStashSize()
328
Doug Zongkerfc44a512014-08-26 13:10:25 -0700329 # Double-check our work.
330 self.AssertSequenceGood()
331
332 self.ComputePatches(prefix)
333 self.WriteTransfers(prefix)
334
Dan Albert8b72aef2015-03-23 19:13:21 -0700335 def HashBlocks(self, source, ranges): # pylint: disable=no-self-use
Sami Tolvanendd67a292014-12-09 16:40:34 +0000336 data = source.ReadRangeSet(ranges)
337 ctx = sha1()
338
339 for p in data:
340 ctx.update(p)
341
342 return ctx.hexdigest()
343
Doug Zongkerfc44a512014-08-26 13:10:25 -0700344 def WriteTransfers(self, prefix):
Tianjie Xu37e29302016-06-23 16:10:35 -0700345 def WriteSplitTransfers(out, style, target_blocks):
346 """Limit the size of operand in command 'new' and 'zero' to 1024 blocks.
Tianjie Xubb848c52016-06-21 15:54:09 -0700347
348 This prevents the target size of one command from being too large; and
349 might help to avoid fsync errors on some devices."""
350
Tao Bao3a2e3502016-12-28 09:14:39 -0800351 assert style == "new" or style == "zero"
Tianjie Xu37e29302016-06-23 16:10:35 -0700352 blocks_limit = 1024
Tianjie Xubb848c52016-06-21 15:54:09 -0700353 total = 0
Tianjie Xu37e29302016-06-23 16:10:35 -0700354 while target_blocks:
355 blocks_to_write = target_blocks.first(blocks_limit)
356 out.append("%s %s\n" % (style, blocks_to_write.to_string_raw()))
357 total += blocks_to_write.size()
358 target_blocks = target_blocks.subtract(blocks_to_write)
Tianjie Xubb848c52016-06-21 15:54:09 -0700359 return total
360
Doug Zongkerfc44a512014-08-26 13:10:25 -0700361 out = []
Doug Zongkerfc44a512014-08-26 13:10:25 -0700362 total = 0
Doug Zongkerfc44a512014-08-26 13:10:25 -0700363
Tao Bao3a2e3502016-12-28 09:14:39 -0800364 # In BBOTA v2, 'stashes' records the map from 'stash_raw_id' to 'stash_id'
365 # (aka 'sid', which is the stash slot id). The stash in a 'stash_id' will
366 # be freed immediately after its use. So unlike 'stash_raw_id' (which
367 # uniquely identifies each pair of stashed blocks), the same 'stash_id'
368 # may be reused during the life cycle of an update (maintained by
369 # 'free_stash_ids' heap and 'next_stash_id').
370 #
371 # In BBOTA v3+, it uses the hash of the stashed blocks as the stash slot
372 # id. 'stashes' records the map from 'hash' to the ref count. The stash
373 # will be freed only if the count decrements to zero.
Doug Zongker62338182014-09-08 08:29:55 -0700374 stashes = {}
375 stashed_blocks = 0
376 max_stashed_blocks = 0
377
Tao Bao3a2e3502016-12-28 09:14:39 -0800378 if self.version == 2:
379 free_stash_ids = []
380 next_stash_id = 0
Doug Zongker62338182014-09-08 08:29:55 -0700381
Doug Zongkerfc44a512014-08-26 13:10:25 -0700382 for xf in self.transfers:
383
Doug Zongker62338182014-09-08 08:29:55 -0700384 if self.version < 2:
385 assert not xf.stash_before
386 assert not xf.use_stash
387
Tao Bao3a2e3502016-12-28 09:14:39 -0800388 for stash_raw_id, sr in xf.stash_before:
Sami Tolvanendd67a292014-12-09 16:40:34 +0000389 if self.version == 2:
Tao Bao3a2e3502016-12-28 09:14:39 -0800390 assert stash_raw_id not in stashes
391 if free_stash_ids:
392 sid = heapq.heappop(free_stash_ids)
393 else:
394 sid = next_stash_id
395 next_stash_id += 1
396 stashes[stash_raw_id] = sid
caozhiyuan21b37d82015-10-21 15:14:03 +0800397 stashed_blocks += sr.size()
Sami Tolvanendd67a292014-12-09 16:40:34 +0000398 out.append("stash %d %s\n" % (sid, sr.to_string_raw()))
399 else:
400 sh = self.HashBlocks(self.src, sr)
401 if sh in stashes:
402 stashes[sh] += 1
403 else:
404 stashes[sh] = 1
caozhiyuan21b37d82015-10-21 15:14:03 +0800405 stashed_blocks += sr.size()
Tao Baod522bdc2016-04-12 15:53:16 -0700406 self.touched_src_ranges = self.touched_src_ranges.union(sr)
Sami Tolvanendd67a292014-12-09 16:40:34 +0000407 out.append("stash %s %s\n" % (sh, sr.to_string_raw()))
Doug Zongker62338182014-09-08 08:29:55 -0700408
409 if stashed_blocks > max_stashed_blocks:
410 max_stashed_blocks = stashed_blocks
411
Jesse Zhao7b985f62015-03-02 16:53:08 -0800412 free_string = []
caozhiyuan21b37d82015-10-21 15:14:03 +0800413 free_size = 0
Jesse Zhao7b985f62015-03-02 16:53:08 -0800414
Doug Zongker62338182014-09-08 08:29:55 -0700415 if self.version == 1:
Tao Bao4fcb77e2015-10-21 13:36:01 -0700416 src_str = xf.src_ranges.to_string_raw() if xf.src_ranges else ""
Sami Tolvanendd67a292014-12-09 16:40:34 +0000417 elif self.version >= 2:
Doug Zongker62338182014-09-08 08:29:55 -0700418
419 # <# blocks> <src ranges>
420 # OR
421 # <# blocks> <src ranges> <src locs> <stash refs...>
422 # OR
423 # <# blocks> - <stash refs...>
424
425 size = xf.src_ranges.size()
Dan Albert8b72aef2015-03-23 19:13:21 -0700426 src_str = [str(size)]
Doug Zongker62338182014-09-08 08:29:55 -0700427
428 unstashed_src_ranges = xf.src_ranges
429 mapped_stashes = []
Tao Bao3a2e3502016-12-28 09:14:39 -0800430 for stash_raw_id, sr in xf.use_stash:
Doug Zongker62338182014-09-08 08:29:55 -0700431 unstashed_src_ranges = unstashed_src_ranges.subtract(sr)
Sami Tolvanendd67a292014-12-09 16:40:34 +0000432 sh = self.HashBlocks(self.src, sr)
Doug Zongker62338182014-09-08 08:29:55 -0700433 sr = xf.src_ranges.map_within(sr)
434 mapped_stashes.append(sr)
Sami Tolvanendd67a292014-12-09 16:40:34 +0000435 if self.version == 2:
Tao Bao3a2e3502016-12-28 09:14:39 -0800436 sid = stashes.pop(stash_raw_id)
Dan Albert8b72aef2015-03-23 19:13:21 -0700437 src_str.append("%d:%s" % (sid, sr.to_string_raw()))
Tao Baobb625d22015-08-13 14:44:15 -0700438 # A stash will be used only once. We need to free the stash
439 # immediately after the use, instead of waiting for the automatic
440 # clean-up at the end. Because otherwise it may take up extra space
441 # and lead to OTA failures.
442 # Bug: 23119955
443 free_string.append("free %d\n" % (sid,))
caozhiyuan21b37d82015-10-21 15:14:03 +0800444 free_size += sr.size()
Tao Bao3a2e3502016-12-28 09:14:39 -0800445 heapq.heappush(free_stash_ids, sid)
Sami Tolvanendd67a292014-12-09 16:40:34 +0000446 else:
447 assert sh in stashes
Dan Albert8b72aef2015-03-23 19:13:21 -0700448 src_str.append("%s:%s" % (sh, sr.to_string_raw()))
Sami Tolvanendd67a292014-12-09 16:40:34 +0000449 stashes[sh] -= 1
450 if stashes[sh] == 0:
Tao Baoe27acfd2016-12-16 11:13:55 -0800451 free_string.append("free %s\n" % (sh,))
Tao Bao3a2e3502016-12-28 09:14:39 -0800452 free_size += sr.size()
Sami Tolvanendd67a292014-12-09 16:40:34 +0000453 stashes.pop(sh)
Doug Zongker62338182014-09-08 08:29:55 -0700454
455 if unstashed_src_ranges:
Dan Albert8b72aef2015-03-23 19:13:21 -0700456 src_str.insert(1, unstashed_src_ranges.to_string_raw())
Doug Zongker62338182014-09-08 08:29:55 -0700457 if xf.use_stash:
458 mapped_unstashed = xf.src_ranges.map_within(unstashed_src_ranges)
Dan Albert8b72aef2015-03-23 19:13:21 -0700459 src_str.insert(2, mapped_unstashed.to_string_raw())
Doug Zongker62338182014-09-08 08:29:55 -0700460 mapped_stashes.append(mapped_unstashed)
461 self.AssertPartition(RangeSet(data=(0, size)), mapped_stashes)
462 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700463 src_str.insert(1, "-")
Doug Zongker62338182014-09-08 08:29:55 -0700464 self.AssertPartition(RangeSet(data=(0, size)), mapped_stashes)
465
Dan Albert8b72aef2015-03-23 19:13:21 -0700466 src_str = " ".join(src_str)
Doug Zongker62338182014-09-08 08:29:55 -0700467
Sami Tolvanendd67a292014-12-09 16:40:34 +0000468 # all versions:
Doug Zongker62338182014-09-08 08:29:55 -0700469 # zero <rangeset>
470 # new <rangeset>
471 # erase <rangeset>
472 #
473 # version 1:
474 # bsdiff patchstart patchlen <src rangeset> <tgt rangeset>
475 # imgdiff patchstart patchlen <src rangeset> <tgt rangeset>
476 # move <src rangeset> <tgt rangeset>
477 #
478 # version 2:
Dan Albert8b72aef2015-03-23 19:13:21 -0700479 # bsdiff patchstart patchlen <tgt rangeset> <src_str>
480 # imgdiff patchstart patchlen <tgt rangeset> <src_str>
481 # move <tgt rangeset> <src_str>
Sami Tolvanendd67a292014-12-09 16:40:34 +0000482 #
483 # version 3:
Dan Albert8b72aef2015-03-23 19:13:21 -0700484 # bsdiff patchstart patchlen srchash tgthash <tgt rangeset> <src_str>
485 # imgdiff patchstart patchlen srchash tgthash <tgt rangeset> <src_str>
486 # move hash <tgt rangeset> <src_str>
Doug Zongkerfc44a512014-08-26 13:10:25 -0700487
488 tgt_size = xf.tgt_ranges.size()
489
490 if xf.style == "new":
491 assert xf.tgt_ranges
Tianjie Xu37e29302016-06-23 16:10:35 -0700492 assert tgt_size == WriteSplitTransfers(out, xf.style, xf.tgt_ranges)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700493 total += tgt_size
494 elif xf.style == "move":
Doug Zongkerfc44a512014-08-26 13:10:25 -0700495 assert xf.tgt_ranges
496 assert xf.src_ranges.size() == tgt_size
497 if xf.src_ranges != xf.tgt_ranges:
Doug Zongker62338182014-09-08 08:29:55 -0700498 if self.version == 1:
499 out.append("%s %s %s\n" % (
500 xf.style,
501 xf.src_ranges.to_string_raw(), xf.tgt_ranges.to_string_raw()))
502 elif self.version == 2:
503 out.append("%s %s %s\n" % (
504 xf.style,
Dan Albert8b72aef2015-03-23 19:13:21 -0700505 xf.tgt_ranges.to_string_raw(), src_str))
Sami Tolvanendd67a292014-12-09 16:40:34 +0000506 elif self.version >= 3:
Sami Tolvanen29f529f2015-04-17 16:28:08 +0100507 # take into account automatic stashing of overlapping blocks
508 if xf.src_ranges.overlaps(xf.tgt_ranges):
Tao Baoe9b61912015-07-09 17:37:49 -0700509 temp_stash_usage = stashed_blocks + xf.src_ranges.size()
Sami Tolvanen29f529f2015-04-17 16:28:08 +0100510 if temp_stash_usage > max_stashed_blocks:
511 max_stashed_blocks = temp_stash_usage
512
Tao Baod522bdc2016-04-12 15:53:16 -0700513 self.touched_src_ranges = self.touched_src_ranges.union(
514 xf.src_ranges)
515
Sami Tolvanendd67a292014-12-09 16:40:34 +0000516 out.append("%s %s %s %s\n" % (
517 xf.style,
518 self.HashBlocks(self.tgt, xf.tgt_ranges),
Dan Albert8b72aef2015-03-23 19:13:21 -0700519 xf.tgt_ranges.to_string_raw(), src_str))
Doug Zongkerfc44a512014-08-26 13:10:25 -0700520 total += tgt_size
521 elif xf.style in ("bsdiff", "imgdiff"):
Doug Zongkerfc44a512014-08-26 13:10:25 -0700522 assert xf.tgt_ranges
523 assert xf.src_ranges
Doug Zongker62338182014-09-08 08:29:55 -0700524 if self.version == 1:
525 out.append("%s %d %d %s %s\n" % (
526 xf.style, xf.patch_start, xf.patch_len,
527 xf.src_ranges.to_string_raw(), xf.tgt_ranges.to_string_raw()))
528 elif self.version == 2:
529 out.append("%s %d %d %s %s\n" % (
530 xf.style, xf.patch_start, xf.patch_len,
Dan Albert8b72aef2015-03-23 19:13:21 -0700531 xf.tgt_ranges.to_string_raw(), src_str))
Sami Tolvanendd67a292014-12-09 16:40:34 +0000532 elif self.version >= 3:
Sami Tolvanen29f529f2015-04-17 16:28:08 +0100533 # take into account automatic stashing of overlapping blocks
534 if xf.src_ranges.overlaps(xf.tgt_ranges):
Tao Baoe9b61912015-07-09 17:37:49 -0700535 temp_stash_usage = stashed_blocks + xf.src_ranges.size()
Sami Tolvanen29f529f2015-04-17 16:28:08 +0100536 if temp_stash_usage > max_stashed_blocks:
537 max_stashed_blocks = temp_stash_usage
538
Tao Baod522bdc2016-04-12 15:53:16 -0700539 self.touched_src_ranges = self.touched_src_ranges.union(
540 xf.src_ranges)
541
Sami Tolvanendd67a292014-12-09 16:40:34 +0000542 out.append("%s %d %d %s %s %s %s\n" % (
543 xf.style,
544 xf.patch_start, xf.patch_len,
545 self.HashBlocks(self.src, xf.src_ranges),
546 self.HashBlocks(self.tgt, xf.tgt_ranges),
Dan Albert8b72aef2015-03-23 19:13:21 -0700547 xf.tgt_ranges.to_string_raw(), src_str))
Doug Zongkerfc44a512014-08-26 13:10:25 -0700548 total += tgt_size
549 elif xf.style == "zero":
550 assert xf.tgt_ranges
551 to_zero = xf.tgt_ranges.subtract(xf.src_ranges)
Tianjie Xu37e29302016-06-23 16:10:35 -0700552 assert WriteSplitTransfers(out, xf.style, to_zero) == to_zero.size()
Tianjie Xubb848c52016-06-21 15:54:09 -0700553 total += to_zero.size()
Doug Zongkerfc44a512014-08-26 13:10:25 -0700554 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700555 raise ValueError("unknown transfer style '%s'\n" % xf.style)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700556
Sami Tolvanendd67a292014-12-09 16:40:34 +0000557 if free_string:
558 out.append("".join(free_string))
caozhiyuan21b37d82015-10-21 15:14:03 +0800559 stashed_blocks -= free_size
Sami Tolvanendd67a292014-12-09 16:40:34 +0000560
Tao Bao575d68a2015-08-07 19:49:45 -0700561 if self.version >= 2 and common.OPTIONS.cache_size is not None:
Tao Bao8dcf7382015-05-21 14:09:49 -0700562 # Sanity check: abort if we're going to need more stash space than
563 # the allowed size (cache_size * threshold). There are two purposes
564 # of having a threshold here. a) Part of the cache may have been
565 # occupied by some recovery logs. b) It will buy us some time to deal
566 # with the oversize issue.
567 cache_size = common.OPTIONS.cache_size
568 stash_threshold = common.OPTIONS.stash_threshold
569 max_allowed = cache_size * stash_threshold
570 assert max_stashed_blocks * self.tgt.blocksize < max_allowed, \
571 'Stash size %d (%d * %d) exceeds the limit %d (%d * %.2f)' % (
572 max_stashed_blocks * self.tgt.blocksize, max_stashed_blocks,
573 self.tgt.blocksize, max_allowed, cache_size,
574 stash_threshold)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700575
Tao Baod522bdc2016-04-12 15:53:16 -0700576 if self.version >= 3:
577 self.touched_src_sha1 = self.HashBlocks(
578 self.src, self.touched_src_ranges)
579
Tao Baoe9b61912015-07-09 17:37:49 -0700580 # Zero out extended blocks as a workaround for bug 20881595.
581 if self.tgt.extended:
Tianjie Xu37e29302016-06-23 16:10:35 -0700582 assert (WriteSplitTransfers(out, "zero", self.tgt.extended) ==
Tianjie Xubb848c52016-06-21 15:54:09 -0700583 self.tgt.extended.size())
Tao Baob32d56e2015-09-09 11:55:01 -0700584 total += self.tgt.extended.size()
Tao Baoe9b61912015-07-09 17:37:49 -0700585
586 # We erase all the blocks on the partition that a) don't contain useful
Tao Bao66f1fa62016-05-03 10:02:01 -0700587 # data in the new image; b) will not be touched by dm-verity. Out of those
588 # blocks, we erase the ones that won't be used in this update at the
589 # beginning of an update. The rest would be erased at the end. This is to
590 # work around the eMMC issue observed on some devices, which may otherwise
591 # get starving for clean blocks and thus fail the update. (b/28347095)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700592 all_tgt = RangeSet(data=(0, self.tgt.total_blocks))
Tao Baoe9b61912015-07-09 17:37:49 -0700593 all_tgt_minus_extended = all_tgt.subtract(self.tgt.extended)
594 new_dontcare = all_tgt_minus_extended.subtract(self.tgt.care_map)
Tao Bao66f1fa62016-05-03 10:02:01 -0700595
596 erase_first = new_dontcare.subtract(self.touched_src_ranges)
597 if erase_first:
598 out.insert(0, "erase %s\n" % (erase_first.to_string_raw(),))
599
600 erase_last = new_dontcare.subtract(erase_first)
601 if erase_last:
602 out.append("erase %s\n" % (erase_last.to_string_raw(),))
Doug Zongkere985f6f2014-09-09 12:38:47 -0700603
604 out.insert(0, "%d\n" % (self.version,)) # format version number
Tao Baob32d56e2015-09-09 11:55:01 -0700605 out.insert(1, "%d\n" % (total,))
Tao Bao3a2e3502016-12-28 09:14:39 -0800606 if self.version == 2:
607 # v2 only: after the total block count, we give the number of stash slots
608 # needed, and the maximum size needed (in blocks).
Doug Zongkere985f6f2014-09-09 12:38:47 -0700609 out.insert(2, str(next_stash_id) + "\n")
610 out.insert(3, str(max_stashed_blocks) + "\n")
Tao Bao3a2e3502016-12-28 09:14:39 -0800611 elif self.version >= 3:
612 # v3+: the number of stash slots is unused.
613 out.insert(2, "0\n")
614 out.insert(3, str(max_stashed_blocks) + "\n")
Doug Zongkerfc44a512014-08-26 13:10:25 -0700615
616 with open(prefix + ".transfer.list", "wb") as f:
617 for i in out:
618 f.write(i)
619
Doug Zongker62338182014-09-08 08:29:55 -0700620 if self.version >= 2:
Tao Baob4cfca52016-02-04 14:26:02 -0800621 self._max_stashed_size = max_stashed_blocks * self.tgt.blocksize
Tao Bao575d68a2015-08-07 19:49:45 -0700622 OPTIONS = common.OPTIONS
623 if OPTIONS.cache_size is not None:
624 max_allowed = OPTIONS.cache_size * OPTIONS.stash_threshold
625 print("max stashed blocks: %d (%d bytes), "
626 "limit: %d bytes (%.2f%%)\n" % (
Tao Baob4cfca52016-02-04 14:26:02 -0800627 max_stashed_blocks, self._max_stashed_size, max_allowed,
628 self._max_stashed_size * 100.0 / max_allowed))
Tao Bao575d68a2015-08-07 19:49:45 -0700629 else:
630 print("max stashed blocks: %d (%d bytes), limit: <unknown>\n" % (
Tao Baob4cfca52016-02-04 14:26:02 -0800631 max_stashed_blocks, self._max_stashed_size))
Doug Zongker62338182014-09-08 08:29:55 -0700632
Tao Bao82c47982015-08-17 09:45:13 -0700633 def ReviseStashSize(self):
634 print("Revising stash size...")
Tao Baoe27acfd2016-12-16 11:13:55 -0800635 stash_map = {}
Tao Bao82c47982015-08-17 09:45:13 -0700636
637 # Create the map between a stash and its def/use points. For example, for a
Tao Bao3a2e3502016-12-28 09:14:39 -0800638 # given stash of (raw_id, sr), stashes[raw_id] = (sr, def_cmd, use_cmd).
Tao Bao82c47982015-08-17 09:45:13 -0700639 for xf in self.transfers:
640 # Command xf defines (stores) all the stashes in stash_before.
Tao Bao3a2e3502016-12-28 09:14:39 -0800641 for stash_raw_id, sr in xf.stash_before:
642 stash_map[stash_raw_id] = (sr, xf)
Tao Bao82c47982015-08-17 09:45:13 -0700643
644 # Record all the stashes command xf uses.
Tao Bao3a2e3502016-12-28 09:14:39 -0800645 for stash_raw_id, _ in xf.use_stash:
646 stash_map[stash_raw_id] += (xf,)
Tao Bao82c47982015-08-17 09:45:13 -0700647
648 # Compute the maximum blocks available for stash based on /cache size and
649 # the threshold.
650 cache_size = common.OPTIONS.cache_size
651 stash_threshold = common.OPTIONS.stash_threshold
652 max_allowed = cache_size * stash_threshold / self.tgt.blocksize
653
Tao Bao3a2e3502016-12-28 09:14:39 -0800654 # See the comments for 'stashes' in WriteTransfers().
Tao Baoe27acfd2016-12-16 11:13:55 -0800655 stashes = {}
Tao Bao82c47982015-08-17 09:45:13 -0700656 stashed_blocks = 0
Tao Bao9a5caf22015-08-25 15:10:10 -0700657 new_blocks = 0
Tao Bao82c47982015-08-17 09:45:13 -0700658
Tao Bao3a2e3502016-12-28 09:14:39 -0800659 if self.version == 2:
660 free_stash_ids = []
661 next_stash_id = 0
Tao Baoe27acfd2016-12-16 11:13:55 -0800662
Tao Bao82c47982015-08-17 09:45:13 -0700663 # Now go through all the commands. Compute the required stash size on the
664 # fly. If a command requires excess stash than available, it deletes the
665 # stash by replacing the command that uses the stash with a "new" command
666 # instead.
667 for xf in self.transfers:
668 replaced_cmds = []
669
670 # xf.stash_before generates explicit stash commands.
Tao Bao3a2e3502016-12-28 09:14:39 -0800671 for stash_raw_id, sr in xf.stash_before:
Tao Baoe27acfd2016-12-16 11:13:55 -0800672 # Check the post-command stashed_blocks.
673 stashed_blocks_after = stashed_blocks
674 if self.version == 2:
Tao Bao3a2e3502016-12-28 09:14:39 -0800675 assert stash_raw_id not in stashes
676 if free_stash_ids:
677 sid = heapq.heappop(free_stash_ids)
678 else:
679 sid = next_stash_id
680 next_stash_id += 1
681 stashes[stash_raw_id] = sid
Tao Baoe27acfd2016-12-16 11:13:55 -0800682 stashed_blocks_after += sr.size()
683 else:
684 sh = self.HashBlocks(self.src, sr)
685 if sh in stashes:
686 stashes[sh] += 1
687 else:
688 stashes[sh] = 1
689 stashed_blocks_after += sr.size()
690
691 if stashed_blocks_after > max_allowed:
Tao Bao82c47982015-08-17 09:45:13 -0700692 # We cannot stash this one for a later command. Find out the command
693 # that will use this stash and replace the command with "new".
Tao Bao3a2e3502016-12-28 09:14:39 -0800694 use_cmd = stash_map[stash_raw_id][2]
Tao Bao82c47982015-08-17 09:45:13 -0700695 replaced_cmds.append(use_cmd)
Tao Bao9a5caf22015-08-25 15:10:10 -0700696 print("%10d %9s %s" % (sr.size(), "explicit", use_cmd))
Tao Bao82c47982015-08-17 09:45:13 -0700697 else:
Tao Baoe27acfd2016-12-16 11:13:55 -0800698 stashed_blocks = stashed_blocks_after
Tao Bao82c47982015-08-17 09:45:13 -0700699
700 # "move" and "diff" may introduce implicit stashes in BBOTA v3. Prior to
701 # ComputePatches(), they both have the style of "diff".
702 if xf.style == "diff" and self.version >= 3:
703 assert xf.tgt_ranges and xf.src_ranges
704 if xf.src_ranges.overlaps(xf.tgt_ranges):
705 if stashed_blocks + xf.src_ranges.size() > max_allowed:
706 replaced_cmds.append(xf)
Tao Bao9a5caf22015-08-25 15:10:10 -0700707 print("%10d %9s %s" % (xf.src_ranges.size(), "implicit", xf))
Tao Bao82c47982015-08-17 09:45:13 -0700708
709 # Replace the commands in replaced_cmds with "new"s.
710 for cmd in replaced_cmds:
711 # It no longer uses any commands in "use_stash". Remove the def points
712 # for all those stashes.
Tao Bao3a2e3502016-12-28 09:14:39 -0800713 for stash_raw_id, sr in cmd.use_stash:
714 def_cmd = stash_map[stash_raw_id][1]
715 assert (stash_raw_id, sr) in def_cmd.stash_before
716 def_cmd.stash_before.remove((stash_raw_id, sr))
Tao Bao82c47982015-08-17 09:45:13 -0700717
Tianjie Xuebe39a02016-01-14 14:12:26 -0800718 # Add up blocks that violates space limit and print total number to
719 # screen later.
720 new_blocks += cmd.tgt_ranges.size()
Tao Bao82c47982015-08-17 09:45:13 -0700721 cmd.ConvertToNew()
722
Tao Bao3a2e3502016-12-28 09:14:39 -0800723 # xf.use_stash may generate free commands.
724 for stash_raw_id, sr in xf.use_stash:
Tao Baoe27acfd2016-12-16 11:13:55 -0800725 if self.version == 2:
Tao Bao3a2e3502016-12-28 09:14:39 -0800726 sid = stashes.pop(stash_raw_id)
Tao Baoe27acfd2016-12-16 11:13:55 -0800727 stashed_blocks -= sr.size()
Tao Bao3a2e3502016-12-28 09:14:39 -0800728 heapq.heappush(free_stash_ids, sid)
Tao Baoe27acfd2016-12-16 11:13:55 -0800729 else:
730 sh = self.HashBlocks(self.src, sr)
731 assert sh in stashes
732 stashes[sh] -= 1
733 if stashes[sh] == 0:
734 stashed_blocks -= sr.size()
735 stashes.pop(sh)
Tao Baoe27acfd2016-12-16 11:13:55 -0800736
Tianjie Xuebe39a02016-01-14 14:12:26 -0800737 num_of_bytes = new_blocks * self.tgt.blocksize
738 print(" Total %d blocks (%d bytes) are packed as new blocks due to "
739 "insufficient cache size." % (new_blocks, num_of_bytes))
Tao Bao304ee272016-12-19 11:01:38 -0800740 return new_blocks
Tao Bao9a5caf22015-08-25 15:10:10 -0700741
Doug Zongkerfc44a512014-08-26 13:10:25 -0700742 def ComputePatches(self, prefix):
743 print("Reticulating splines...")
744 diff_q = []
745 patch_num = 0
746 with open(prefix + ".new.dat", "wb") as new_f:
747 for xf in self.transfers:
748 if xf.style == "zero":
Tao Bao08c85832016-09-19 22:26:30 -0700749 tgt_size = xf.tgt_ranges.size() * self.tgt.blocksize
750 print("%10d %10d (%6.2f%%) %7s %s %s" % (
751 tgt_size, tgt_size, 100.0, xf.style, xf.tgt_name,
752 str(xf.tgt_ranges)))
753
Doug Zongkerfc44a512014-08-26 13:10:25 -0700754 elif xf.style == "new":
755 for piece in self.tgt.ReadRangeSet(xf.tgt_ranges):
756 new_f.write(piece)
Tao Bao08c85832016-09-19 22:26:30 -0700757 tgt_size = xf.tgt_ranges.size() * self.tgt.blocksize
758 print("%10d %10d (%6.2f%%) %7s %s %s" % (
759 tgt_size, tgt_size, 100.0, xf.style,
760 xf.tgt_name, str(xf.tgt_ranges)))
761
Doug Zongkerfc44a512014-08-26 13:10:25 -0700762 elif xf.style == "diff":
763 src = self.src.ReadRangeSet(xf.src_ranges)
764 tgt = self.tgt.ReadRangeSet(xf.tgt_ranges)
765
766 # We can't compare src and tgt directly because they may have
767 # the same content but be broken up into blocks differently, eg:
768 #
769 # ["he", "llo"] vs ["h", "ello"]
770 #
771 # We want those to compare equal, ideally without having to
772 # actually concatenate the strings (these may be tens of
773 # megabytes).
774
775 src_sha1 = sha1()
776 for p in src:
777 src_sha1.update(p)
778 tgt_sha1 = sha1()
779 tgt_size = 0
780 for p in tgt:
781 tgt_sha1.update(p)
782 tgt_size += len(p)
783
784 if src_sha1.digest() == tgt_sha1.digest():
785 # These are identical; we don't need to generate a patch,
786 # just issue copy commands on the device.
787 xf.style = "move"
Tao Bao08c85832016-09-19 22:26:30 -0700788 if xf.src_ranges != xf.tgt_ranges:
789 print("%10d %10d (%6.2f%%) %7s %s %s (from %s)" % (
790 tgt_size, tgt_size, 100.0, xf.style,
791 xf.tgt_name if xf.tgt_name == xf.src_name else (
792 xf.tgt_name + " (from " + xf.src_name + ")"),
793 str(xf.tgt_ranges), str(xf.src_ranges)))
Doug Zongkerfc44a512014-08-26 13:10:25 -0700794 else:
795 # For files in zip format (eg, APKs, JARs, etc.) we would
796 # like to use imgdiff -z if possible (because it usually
797 # produces significantly smaller patches than bsdiff).
798 # This is permissible if:
799 #
Tao Bao293fd132016-06-11 12:19:23 -0700800 # - imgdiff is not disabled, and
Doug Zongkerfc44a512014-08-26 13:10:25 -0700801 # - the source and target files are monotonic (ie, the
802 # data is stored with blocks in increasing order), and
803 # - we haven't removed any blocks from the source set.
804 #
805 # If these conditions are satisfied then appending all the
806 # blocks in the set together in order will produce a valid
807 # zip file (plus possibly extra zeros in the last block),
808 # which is what imgdiff needs to operate. (imgdiff is
809 # fine with extra zeros at the end of the file.)
Tao Bao293fd132016-06-11 12:19:23 -0700810 imgdiff = (not self.disable_imgdiff and xf.intact and
Doug Zongkerfc44a512014-08-26 13:10:25 -0700811 xf.tgt_name.split(".")[-1].lower()
812 in ("apk", "jar", "zip"))
813 xf.style = "imgdiff" if imgdiff else "bsdiff"
814 diff_q.append((tgt_size, src, tgt, xf, patch_num))
815 patch_num += 1
816
817 else:
818 assert False, "unknown style " + xf.style
819
820 if diff_q:
821 if self.threads > 1:
822 print("Computing patches (using %d threads)..." % (self.threads,))
823 else:
824 print("Computing patches...")
825 diff_q.sort()
826
827 patches = [None] * patch_num
828
Dan Albert8b72aef2015-03-23 19:13:21 -0700829 # TODO: Rewrite with multiprocessing.ThreadPool?
Doug Zongkerfc44a512014-08-26 13:10:25 -0700830 lock = threading.Lock()
831 def diff_worker():
832 while True:
833 with lock:
Dan Albert8b72aef2015-03-23 19:13:21 -0700834 if not diff_q:
835 return
Doug Zongkerfc44a512014-08-26 13:10:25 -0700836 tgt_size, src, tgt, xf, patchnum = diff_q.pop()
837 patch = compute_patch(src, tgt, imgdiff=(xf.style == "imgdiff"))
838 size = len(patch)
839 with lock:
840 patches[patchnum] = (patch, xf)
Tao Bao08c85832016-09-19 22:26:30 -0700841 print("%10d %10d (%6.2f%%) %7s %s %s %s" % (
Doug Zongkerfc44a512014-08-26 13:10:25 -0700842 size, tgt_size, size * 100.0 / tgt_size, xf.style,
843 xf.tgt_name if xf.tgt_name == xf.src_name else (
Tao Bao08c85832016-09-19 22:26:30 -0700844 xf.tgt_name + " (from " + xf.src_name + ")"),
845 str(xf.tgt_ranges), str(xf.src_ranges)))
Doug Zongkerfc44a512014-08-26 13:10:25 -0700846
847 threads = [threading.Thread(target=diff_worker)
Dan Albert8b72aef2015-03-23 19:13:21 -0700848 for _ in range(self.threads)]
Doug Zongkerfc44a512014-08-26 13:10:25 -0700849 for th in threads:
850 th.start()
851 while threads:
852 threads.pop().join()
853 else:
854 patches = []
855
856 p = 0
857 with open(prefix + ".patch.dat", "wb") as patch_f:
858 for patch, xf in patches:
859 xf.patch_start = p
860 xf.patch_len = len(patch)
861 patch_f.write(patch)
862 p += len(patch)
863
864 def AssertSequenceGood(self):
865 # Simulate the sequences of transfers we will output, and check that:
866 # - we never read a block after writing it, and
867 # - we write every block we care about exactly once.
868
869 # Start with no blocks having been touched yet.
Doug Zongker6ab2a502016-02-09 08:28:09 -0800870 touched = array.array("B", "\0" * self.tgt.total_blocks)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700871
872 # Imagine processing the transfers in order.
873 for xf in self.transfers:
874 # Check that the input blocks for this transfer haven't yet been touched.
Doug Zongker62338182014-09-08 08:29:55 -0700875
876 x = xf.src_ranges
877 if self.version >= 2:
878 for _, sr in xf.use_stash:
879 x = x.subtract(sr)
880
Doug Zongker6ab2a502016-02-09 08:28:09 -0800881 for s, e in x:
Tao Baoff75c232016-03-04 15:23:34 -0800882 # Source image could be larger. Don't check the blocks that are in the
883 # source image only. Since they are not in 'touched', and won't ever
884 # be touched.
885 for i in range(s, min(e, self.tgt.total_blocks)):
Doug Zongker6ab2a502016-02-09 08:28:09 -0800886 assert touched[i] == 0
887
888 # Check that the output blocks for this transfer haven't yet
889 # been touched, and touch all the blocks written by this
890 # transfer.
891 for s, e in xf.tgt_ranges:
892 for i in range(s, e):
893 assert touched[i] == 0
894 touched[i] = 1
Doug Zongkerfc44a512014-08-26 13:10:25 -0700895
896 # Check that we've written every target block.
Doug Zongker6ab2a502016-02-09 08:28:09 -0800897 for s, e in self.tgt.care_map:
898 for i in range(s, e):
899 assert touched[i] == 1
Doug Zongkerfc44a512014-08-26 13:10:25 -0700900
Doug Zongker62338182014-09-08 08:29:55 -0700901 def ImproveVertexSequence(self):
902 print("Improving vertex order...")
903
904 # At this point our digraph is acyclic; we reversed any edges that
905 # were backwards in the heuristically-generated sequence. The
906 # previously-generated order is still acceptable, but we hope to
907 # find a better order that needs less memory for stashed data.
908 # Now we do a topological sort to generate a new vertex order,
909 # using a greedy algorithm to choose which vertex goes next
910 # whenever we have a choice.
911
912 # Make a copy of the edge set; this copy will get destroyed by the
913 # algorithm.
914 for xf in self.transfers:
915 xf.incoming = xf.goes_after.copy()
916 xf.outgoing = xf.goes_before.copy()
917
918 L = [] # the new vertex order
919
920 # S is the set of sources in the remaining graph; we always choose
921 # the one that leaves the least amount of stashed data after it's
922 # executed.
923 S = [(u.NetStashChange(), u.order, u) for u in self.transfers
924 if not u.incoming]
925 heapq.heapify(S)
926
927 while S:
928 _, _, xf = heapq.heappop(S)
929 L.append(xf)
930 for u in xf.outgoing:
931 del u.incoming[xf]
932 if not u.incoming:
933 heapq.heappush(S, (u.NetStashChange(), u.order, u))
934
935 # if this fails then our graph had a cycle.
936 assert len(L) == len(self.transfers)
937
938 self.transfers = L
939 for i, xf in enumerate(L):
940 xf.order = i
941
Doug Zongkerfc44a512014-08-26 13:10:25 -0700942 def RemoveBackwardEdges(self):
943 print("Removing backward edges...")
944 in_order = 0
945 out_of_order = 0
946 lost_source = 0
947
948 for xf in self.transfers:
Doug Zongkerfc44a512014-08-26 13:10:25 -0700949 lost = 0
950 size = xf.src_ranges.size()
951 for u in xf.goes_before:
952 # xf should go before u
953 if xf.order < u.order:
954 # it does, hurray!
Doug Zongker62338182014-09-08 08:29:55 -0700955 in_order += 1
Doug Zongkerfc44a512014-08-26 13:10:25 -0700956 else:
957 # it doesn't, boo. trim the blocks that u writes from xf's
958 # source, so that xf can go after u.
Doug Zongker62338182014-09-08 08:29:55 -0700959 out_of_order += 1
Doug Zongkerfc44a512014-08-26 13:10:25 -0700960 assert xf.src_ranges.overlaps(u.tgt_ranges)
961 xf.src_ranges = xf.src_ranges.subtract(u.tgt_ranges)
962 xf.intact = False
963
964 if xf.style == "diff" and not xf.src_ranges:
965 # nothing left to diff from; treat as new data
966 xf.style = "new"
967
968 lost = size - xf.src_ranges.size()
969 lost_source += lost
Doug Zongkerfc44a512014-08-26 13:10:25 -0700970
971 print((" %d/%d dependencies (%.2f%%) were violated; "
972 "%d source blocks removed.") %
973 (out_of_order, in_order + out_of_order,
974 (out_of_order * 100.0 / (in_order + out_of_order))
975 if (in_order + out_of_order) else 0.0,
976 lost_source))
977
Doug Zongker62338182014-09-08 08:29:55 -0700978 def ReverseBackwardEdges(self):
Tao Bao3a2e3502016-12-28 09:14:39 -0800979 """Reverse unsatisfying edges and compute pairs of stashed blocks.
980
981 For each transfer, make sure it properly stashes the blocks it touches and
982 will be used by later transfers. It uses pairs of (stash_raw_id, range) to
983 record the blocks to be stashed. 'stash_raw_id' is an id that uniquely
984 identifies each pair. Note that for the same range (e.g. RangeSet("1-5")),
985 it is possible to have multiple pairs with different 'stash_raw_id's. Each
986 'stash_raw_id' will be consumed by one transfer. In BBOTA v3+, identical
987 blocks will be written to the same stash slot in WriteTransfers().
988 """
989
Doug Zongker62338182014-09-08 08:29:55 -0700990 print("Reversing backward edges...")
991 in_order = 0
992 out_of_order = 0
Tao Bao3a2e3502016-12-28 09:14:39 -0800993 stash_raw_id = 0
Doug Zongker62338182014-09-08 08:29:55 -0700994 stash_size = 0
995
996 for xf in self.transfers:
Doug Zongker62338182014-09-08 08:29:55 -0700997 for u in xf.goes_before.copy():
998 # xf should go before u
999 if xf.order < u.order:
1000 # it does, hurray!
1001 in_order += 1
1002 else:
1003 # it doesn't, boo. modify u to stash the blocks that it
1004 # writes that xf wants to read, and then require u to go
1005 # before xf.
1006 out_of_order += 1
1007
1008 overlap = xf.src_ranges.intersect(u.tgt_ranges)
1009 assert overlap
1010
Tao Bao3a2e3502016-12-28 09:14:39 -08001011 u.stash_before.append((stash_raw_id, overlap))
1012 xf.use_stash.append((stash_raw_id, overlap))
1013 stash_raw_id += 1
Doug Zongker62338182014-09-08 08:29:55 -07001014 stash_size += overlap.size()
1015
1016 # reverse the edge direction; now xf must go after u
1017 del xf.goes_before[u]
1018 del u.goes_after[xf]
1019 xf.goes_after[u] = None # value doesn't matter
1020 u.goes_before[xf] = None
1021
1022 print((" %d/%d dependencies (%.2f%%) were violated; "
1023 "%d source blocks stashed.") %
1024 (out_of_order, in_order + out_of_order,
1025 (out_of_order * 100.0 / (in_order + out_of_order))
1026 if (in_order + out_of_order) else 0.0,
1027 stash_size))
1028
Doug Zongkerfc44a512014-08-26 13:10:25 -07001029 def FindVertexSequence(self):
1030 print("Finding vertex sequence...")
1031
1032 # This is based on "A Fast & Effective Heuristic for the Feedback
1033 # Arc Set Problem" by P. Eades, X. Lin, and W.F. Smyth. Think of
1034 # it as starting with the digraph G and moving all the vertices to
1035 # be on a horizontal line in some order, trying to minimize the
1036 # number of edges that end up pointing to the left. Left-pointing
1037 # edges will get removed to turn the digraph into a DAG. In this
1038 # case each edge has a weight which is the number of source blocks
1039 # we'll lose if that edge is removed; we try to minimize the total
1040 # weight rather than just the number of edges.
1041
1042 # Make a copy of the edge set; this copy will get destroyed by the
1043 # algorithm.
1044 for xf in self.transfers:
1045 xf.incoming = xf.goes_after.copy()
1046 xf.outgoing = xf.goes_before.copy()
Doug Zongker6ab2a502016-02-09 08:28:09 -08001047 xf.score = sum(xf.outgoing.values()) - sum(xf.incoming.values())
Doug Zongkerfc44a512014-08-26 13:10:25 -07001048
1049 # We use an OrderedDict instead of just a set so that the output
1050 # is repeatable; otherwise it would depend on the hash values of
1051 # the transfer objects.
1052 G = OrderedDict()
1053 for xf in self.transfers:
1054 G[xf] = None
1055 s1 = deque() # the left side of the sequence, built from left to right
1056 s2 = deque() # the right side of the sequence, built from right to left
1057
Doug Zongker6ab2a502016-02-09 08:28:09 -08001058 heap = []
1059 for xf in self.transfers:
1060 xf.heap_item = HeapItem(xf)
1061 heap.append(xf.heap_item)
1062 heapq.heapify(heap)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001063
Tao Bao33482282016-10-24 16:49:08 -07001064 # Use OrderedDict() instead of set() to preserve the insertion order. Need
1065 # to use 'sinks[key] = None' to add key into the set. sinks will look like
1066 # { key1: None, key2: None, ... }.
1067 sinks = OrderedDict.fromkeys(u for u in G if not u.outgoing)
1068 sources = OrderedDict.fromkeys(u for u in G if not u.incoming)
Doug Zongker6ab2a502016-02-09 08:28:09 -08001069
1070 def adjust_score(iu, delta):
1071 iu.score += delta
1072 iu.heap_item.clear()
1073 iu.heap_item = HeapItem(iu)
1074 heapq.heappush(heap, iu.heap_item)
1075
1076 while G:
Doug Zongkerfc44a512014-08-26 13:10:25 -07001077 # Put all sinks at the end of the sequence.
Doug Zongker6ab2a502016-02-09 08:28:09 -08001078 while sinks:
Tao Bao33482282016-10-24 16:49:08 -07001079 new_sinks = OrderedDict()
Doug Zongkerfc44a512014-08-26 13:10:25 -07001080 for u in sinks:
Doug Zongker6ab2a502016-02-09 08:28:09 -08001081 if u not in G: continue
Doug Zongkerfc44a512014-08-26 13:10:25 -07001082 s2.appendleft(u)
1083 del G[u]
1084 for iu in u.incoming:
Doug Zongker6ab2a502016-02-09 08:28:09 -08001085 adjust_score(iu, -iu.outgoing.pop(u))
Tao Bao33482282016-10-24 16:49:08 -07001086 if not iu.outgoing:
1087 new_sinks[iu] = None
Doug Zongker6ab2a502016-02-09 08:28:09 -08001088 sinks = new_sinks
Doug Zongkerfc44a512014-08-26 13:10:25 -07001089
1090 # Put all the sources at the beginning of the sequence.
Doug Zongker6ab2a502016-02-09 08:28:09 -08001091 while sources:
Tao Bao33482282016-10-24 16:49:08 -07001092 new_sources = OrderedDict()
Doug Zongkerfc44a512014-08-26 13:10:25 -07001093 for u in sources:
Doug Zongker6ab2a502016-02-09 08:28:09 -08001094 if u not in G: continue
Doug Zongkerfc44a512014-08-26 13:10:25 -07001095 s1.append(u)
1096 del G[u]
1097 for iu in u.outgoing:
Doug Zongker6ab2a502016-02-09 08:28:09 -08001098 adjust_score(iu, +iu.incoming.pop(u))
Tao Bao33482282016-10-24 16:49:08 -07001099 if not iu.incoming:
1100 new_sources[iu] = None
Doug Zongker6ab2a502016-02-09 08:28:09 -08001101 sources = new_sources
Doug Zongkerfc44a512014-08-26 13:10:25 -07001102
Doug Zongker6ab2a502016-02-09 08:28:09 -08001103 if not G: break
Doug Zongkerfc44a512014-08-26 13:10:25 -07001104
1105 # Find the "best" vertex to put next. "Best" is the one that
1106 # maximizes the net difference in source blocks saved we get by
1107 # pretending it's a source rather than a sink.
1108
Doug Zongker6ab2a502016-02-09 08:28:09 -08001109 while True:
1110 u = heapq.heappop(heap)
1111 if u and u.item in G:
1112 u = u.item
1113 break
Doug Zongkerfc44a512014-08-26 13:10:25 -07001114
Doug Zongkerfc44a512014-08-26 13:10:25 -07001115 s1.append(u)
1116 del G[u]
1117 for iu in u.outgoing:
Doug Zongker6ab2a502016-02-09 08:28:09 -08001118 adjust_score(iu, +iu.incoming.pop(u))
Tao Bao33482282016-10-24 16:49:08 -07001119 if not iu.incoming:
1120 sources[iu] = None
Doug Zongker6ab2a502016-02-09 08:28:09 -08001121
Doug Zongkerfc44a512014-08-26 13:10:25 -07001122 for iu in u.incoming:
Doug Zongker6ab2a502016-02-09 08:28:09 -08001123 adjust_score(iu, -iu.outgoing.pop(u))
Tao Bao33482282016-10-24 16:49:08 -07001124 if not iu.outgoing:
1125 sinks[iu] = None
Doug Zongkerfc44a512014-08-26 13:10:25 -07001126
1127 # Now record the sequence in the 'order' field of each transfer,
1128 # and by rearranging self.transfers to be in the chosen sequence.
1129
1130 new_transfers = []
1131 for x in itertools.chain(s1, s2):
1132 x.order = len(new_transfers)
1133 new_transfers.append(x)
1134 del x.incoming
1135 del x.outgoing
1136
1137 self.transfers = new_transfers
1138
1139 def GenerateDigraph(self):
1140 print("Generating digraph...")
Doug Zongker6ab2a502016-02-09 08:28:09 -08001141
1142 # Each item of source_ranges will be:
1143 # - None, if that block is not used as a source,
Tao Bao33482282016-10-24 16:49:08 -07001144 # - an ordered set of transfers.
Doug Zongker6ab2a502016-02-09 08:28:09 -08001145 source_ranges = []
1146 for b in self.transfers:
1147 for s, e in b.src_ranges:
1148 if e > len(source_ranges):
1149 source_ranges.extend([None] * (e-len(source_ranges)))
1150 for i in range(s, e):
1151 if source_ranges[i] is None:
Tao Bao33482282016-10-24 16:49:08 -07001152 source_ranges[i] = OrderedDict.fromkeys([b])
Doug Zongker6ab2a502016-02-09 08:28:09 -08001153 else:
Tao Bao33482282016-10-24 16:49:08 -07001154 source_ranges[i][b] = None
Doug Zongker6ab2a502016-02-09 08:28:09 -08001155
Doug Zongkerfc44a512014-08-26 13:10:25 -07001156 for a in self.transfers:
Tao Bao33482282016-10-24 16:49:08 -07001157 intersections = OrderedDict()
Doug Zongker6ab2a502016-02-09 08:28:09 -08001158 for s, e in a.tgt_ranges:
1159 for i in range(s, e):
1160 if i >= len(source_ranges): break
Tao Bao33482282016-10-24 16:49:08 -07001161 # Add all the Transfers in source_ranges[i] to the (ordered) set.
1162 if source_ranges[i] is not None:
1163 for j in source_ranges[i]:
1164 intersections[j] = None
Doug Zongker6ab2a502016-02-09 08:28:09 -08001165
1166 for b in intersections:
1167 if a is b: continue
Doug Zongkerfc44a512014-08-26 13:10:25 -07001168
1169 # If the blocks written by A are read by B, then B needs to go before A.
1170 i = a.tgt_ranges.intersect(b.src_ranges)
1171 if i:
Doug Zongkerab7ca1d2014-08-26 10:40:28 -07001172 if b.src_name == "__ZERO":
1173 # the cost of removing source blocks for the __ZERO domain
1174 # is (nearly) zero.
1175 size = 0
1176 else:
1177 size = i.size()
Doug Zongkerfc44a512014-08-26 13:10:25 -07001178 b.goes_before[a] = size
1179 a.goes_after[b] = size
1180
1181 def FindTransfers(self):
Tao Bao9a5caf22015-08-25 15:10:10 -07001182 """Parse the file_map to generate all the transfers."""
1183
Tao Bao08c85832016-09-19 22:26:30 -07001184 def AddSplitTransfers(tgt_name, src_name, tgt_ranges, src_ranges,
1185 style, by_id):
1186 """Add one or multiple Transfer()s by splitting large files.
Tao Bao9a5caf22015-08-25 15:10:10 -07001187
1188 For BBOTA v3, we need to stash source blocks for resumable feature.
1189 However, with the growth of file size and the shrink of the cache
1190 partition source blocks are too large to be stashed. If a file occupies
Tao Bao08c85832016-09-19 22:26:30 -07001191 too many blocks, we split it into smaller pieces by getting multiple
1192 Transfer()s.
Tao Bao9a5caf22015-08-25 15:10:10 -07001193
Tianjie Xubb86e1d2016-01-13 16:14:10 -08001194 The downside is that after splitting, we may increase the package size
1195 since the split pieces don't align well. According to our experiments,
1196 1/8 of the cache size as the per-piece limit appears to be optimal.
1197 Compared to the fixed 1024-block limit, it reduces the overall package
Tao Bao08c85832016-09-19 22:26:30 -07001198 size by 30% for volantis, and 20% for angler and bullhead."""
Tao Bao9a5caf22015-08-25 15:10:10 -07001199
Tao Bao08c85832016-09-19 22:26:30 -07001200 # Possibly split large files into smaller chunks.
Tianjie Xubb86e1d2016-01-13 16:14:10 -08001201 pieces = 0
1202 cache_size = common.OPTIONS.cache_size
1203 split_threshold = 0.125
1204 max_blocks_per_transfer = int(cache_size * split_threshold /
1205 self.tgt.blocksize)
1206
Tao Bao9a5caf22015-08-25 15:10:10 -07001207 # Change nothing for small files.
Tianjie Xubb86e1d2016-01-13 16:14:10 -08001208 if (tgt_ranges.size() <= max_blocks_per_transfer and
1209 src_ranges.size() <= max_blocks_per_transfer):
Tao Bao9a5caf22015-08-25 15:10:10 -07001210 Transfer(tgt_name, src_name, tgt_ranges, src_ranges, style, by_id)
1211 return
1212
Tianjie Xubb86e1d2016-01-13 16:14:10 -08001213 while (tgt_ranges.size() > max_blocks_per_transfer and
1214 src_ranges.size() > max_blocks_per_transfer):
Tao Bao9a5caf22015-08-25 15:10:10 -07001215 tgt_split_name = "%s-%d" % (tgt_name, pieces)
1216 src_split_name = "%s-%d" % (src_name, pieces)
Tianjie Xubb86e1d2016-01-13 16:14:10 -08001217 tgt_first = tgt_ranges.first(max_blocks_per_transfer)
1218 src_first = src_ranges.first(max_blocks_per_transfer)
1219
Tao Bao9a5caf22015-08-25 15:10:10 -07001220 Transfer(tgt_split_name, src_split_name, tgt_first, src_first, style,
1221 by_id)
1222
1223 tgt_ranges = tgt_ranges.subtract(tgt_first)
1224 src_ranges = src_ranges.subtract(src_first)
1225 pieces += 1
1226
1227 # Handle remaining blocks.
1228 if tgt_ranges.size() or src_ranges.size():
1229 # Must be both non-empty.
1230 assert tgt_ranges.size() and src_ranges.size()
1231 tgt_split_name = "%s-%d" % (tgt_name, pieces)
1232 src_split_name = "%s-%d" % (src_name, pieces)
1233 Transfer(tgt_split_name, src_split_name, tgt_ranges, src_ranges, style,
1234 by_id)
1235
Tao Bao08c85832016-09-19 22:26:30 -07001236 def AddTransfer(tgt_name, src_name, tgt_ranges, src_ranges, style, by_id,
1237 split=False):
1238 """Wrapper function for adding a Transfer()."""
1239
1240 # We specialize diff transfers only (which covers bsdiff/imgdiff/move);
1241 # otherwise add the Transfer() as is.
1242 if style != "diff" or not split:
1243 Transfer(tgt_name, src_name, tgt_ranges, src_ranges, style, by_id)
1244 return
1245
1246 # Handle .odex files specially to analyze the block-wise difference. If
1247 # most of the blocks are identical with only few changes (e.g. header),
1248 # we will patch the changed blocks only. This avoids stashing unchanged
1249 # blocks while patching. We limit the analysis to files without size
1250 # changes only. This is to avoid sacrificing the OTA generation cost too
1251 # much.
1252 if (tgt_name.split(".")[-1].lower() == 'odex' and
1253 tgt_ranges.size() == src_ranges.size()):
1254
1255 # 0.5 threshold can be further tuned. The tradeoff is: if only very
1256 # few blocks remain identical, we lose the opportunity to use imgdiff
1257 # that may have better compression ratio than bsdiff.
1258 crop_threshold = 0.5
1259
1260 tgt_skipped = RangeSet()
1261 src_skipped = RangeSet()
1262 tgt_size = tgt_ranges.size()
1263 tgt_changed = 0
1264 for src_block, tgt_block in zip(src_ranges.next_item(),
1265 tgt_ranges.next_item()):
1266 src_rs = RangeSet(str(src_block))
1267 tgt_rs = RangeSet(str(tgt_block))
1268 if self.src.ReadRangeSet(src_rs) == self.tgt.ReadRangeSet(tgt_rs):
1269 tgt_skipped = tgt_skipped.union(tgt_rs)
1270 src_skipped = src_skipped.union(src_rs)
1271 else:
1272 tgt_changed += tgt_rs.size()
1273
1274 # Terminate early if no clear sign of benefits.
1275 if tgt_changed > tgt_size * crop_threshold:
1276 break
1277
1278 if tgt_changed < tgt_size * crop_threshold:
1279 assert tgt_changed + tgt_skipped.size() == tgt_size
1280 print('%10d %10d (%6.2f%%) %s' % (tgt_skipped.size(), tgt_size,
1281 tgt_skipped.size() * 100.0 / tgt_size, tgt_name))
1282 AddSplitTransfers(
1283 "%s-skipped" % (tgt_name,),
1284 "%s-skipped" % (src_name,),
1285 tgt_skipped, src_skipped, style, by_id)
1286
1287 # Intentionally change the file extension to avoid being imgdiff'd as
1288 # the files are no longer in their original format.
1289 tgt_name = "%s-cropped" % (tgt_name,)
1290 src_name = "%s-cropped" % (src_name,)
1291 tgt_ranges = tgt_ranges.subtract(tgt_skipped)
1292 src_ranges = src_ranges.subtract(src_skipped)
1293
1294 # Possibly having no changed blocks.
1295 if not tgt_ranges:
1296 return
1297
1298 # Add the transfer(s).
1299 AddSplitTransfers(
1300 tgt_name, src_name, tgt_ranges, src_ranges, style, by_id)
1301
1302 print("Finding transfers...")
1303
Doug Zongkerfc44a512014-08-26 13:10:25 -07001304 empty = RangeSet()
1305 for tgt_fn, tgt_ranges in self.tgt.file_map.items():
1306 if tgt_fn == "__ZERO":
1307 # the special "__ZERO" domain is all the blocks not contained
1308 # in any file and that are filled with zeros. We have a
1309 # special transfer style for zero blocks.
1310 src_ranges = self.src.file_map.get("__ZERO", empty)
Tao Bao9a5caf22015-08-25 15:10:10 -07001311 AddTransfer(tgt_fn, "__ZERO", tgt_ranges, src_ranges,
1312 "zero", self.transfers)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001313 continue
1314
Tao Baoff777812015-05-12 11:42:31 -07001315 elif tgt_fn == "__COPY":
1316 # "__COPY" domain includes all the blocks not contained in any
1317 # file and that need to be copied unconditionally to the target.
Tao Bao9a5caf22015-08-25 15:10:10 -07001318 AddTransfer(tgt_fn, None, tgt_ranges, empty, "new", self.transfers)
Tao Baoff777812015-05-12 11:42:31 -07001319 continue
1320
Doug Zongkerfc44a512014-08-26 13:10:25 -07001321 elif tgt_fn in self.src.file_map:
1322 # Look for an exact pathname match in the source.
Tao Bao9a5caf22015-08-25 15:10:10 -07001323 AddTransfer(tgt_fn, tgt_fn, tgt_ranges, self.src.file_map[tgt_fn],
1324 "diff", self.transfers, self.version >= 3)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001325 continue
1326
1327 b = os.path.basename(tgt_fn)
1328 if b in self.src_basenames:
1329 # Look for an exact basename match in the source.
1330 src_fn = self.src_basenames[b]
Tao Bao9a5caf22015-08-25 15:10:10 -07001331 AddTransfer(tgt_fn, src_fn, tgt_ranges, self.src.file_map[src_fn],
1332 "diff", self.transfers, self.version >= 3)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001333 continue
1334
1335 b = re.sub("[0-9]+", "#", b)
1336 if b in self.src_numpatterns:
1337 # Look for a 'number pattern' match (a basename match after
1338 # all runs of digits are replaced by "#"). (This is useful
1339 # for .so files that contain version numbers in the filename
1340 # that get bumped.)
1341 src_fn = self.src_numpatterns[b]
Tao Bao9a5caf22015-08-25 15:10:10 -07001342 AddTransfer(tgt_fn, src_fn, tgt_ranges, self.src.file_map[src_fn],
1343 "diff", self.transfers, self.version >= 3)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001344 continue
1345
Tao Bao9a5caf22015-08-25 15:10:10 -07001346 AddTransfer(tgt_fn, None, tgt_ranges, empty, "new", self.transfers)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001347
1348 def AbbreviateSourceNames(self):
Doug Zongkerfc44a512014-08-26 13:10:25 -07001349 for k in self.src.file_map.keys():
1350 b = os.path.basename(k)
1351 self.src_basenames[b] = k
1352 b = re.sub("[0-9]+", "#", b)
1353 self.src_numpatterns[b] = k
1354
1355 @staticmethod
1356 def AssertPartition(total, seq):
1357 """Assert that all the RangeSets in 'seq' form a partition of the
1358 'total' RangeSet (ie, they are nonintersecting and their union
1359 equals 'total')."""
Doug Zongker6ab2a502016-02-09 08:28:09 -08001360
Doug Zongkerfc44a512014-08-26 13:10:25 -07001361 so_far = RangeSet()
1362 for i in seq:
1363 assert not so_far.overlaps(i)
1364 so_far = so_far.union(i)
1365 assert so_far == total