blob: ee5a79b1f647cfa32d8b80ad36108c1d60747059 [file] [log] [blame]
Don Garrettf4b28742012-03-27 20:48:06 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo161c4a12014-05-16 15:56:21 -07005#include "update_engine/payload_generator/delta_diff_generator.h"
Darin Petkov880335c2010-10-01 15:52:53 -07006
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07007#include <errno.h>
8#include <fcntl.h>
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07009#include <inttypes.h>
Darin Petkov880335c2010-10-01 15:52:53 -070010#include <sys/stat.h>
11#include <sys/types.h>
12
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070013#include <algorithm>
Andrew de los Reyesef017552010-10-06 17:57:52 -070014#include <map>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070015#include <set>
16#include <string>
17#include <utility>
18#include <vector>
Darin Petkov880335c2010-10-01 15:52:53 -070019
Ben Chan06c76a42014-09-05 08:21:06 -070020#include <base/files/file_util.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070021#include <base/files/file_path.h>
Darin Petkov880335c2010-10-01 15:52:53 -070022#include <base/logging.h>
Darin Petkov7438a5c2011-08-29 11:56:44 -070023#include <base/memory/scoped_ptr.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070024#include <base/strings/string_number_conversions.h>
25#include <base/strings/string_util.h>
26#include <base/strings/stringprintf.h>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070027#include <bzlib.h>
Darin Petkov880335c2010-10-01 15:52:53 -070028
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070029#include "update_engine/bzip.h"
Don Garrettb8dd1d92013-11-22 17:40:02 -080030#include "update_engine/delta_performer.h"
Andrew de los Reyesef017552010-10-06 17:57:52 -070031#include "update_engine/extent_ranges.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070032#include "update_engine/file_writer.h"
Darin Petkov36a58222010-10-07 22:00:09 -070033#include "update_engine/omaha_hash_calculator.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070034#include "update_engine/payload_constants.h"
35#include "update_engine/payload_generator/cycle_breaker.h"
36#include "update_engine/payload_generator/extent_mapper.h"
37#include "update_engine/payload_generator/filesystem_iterator.h"
38#include "update_engine/payload_generator/full_update_generator.h"
39#include "update_engine/payload_generator/graph_types.h"
40#include "update_engine/payload_generator/graph_utils.h"
41#include "update_engine/payload_generator/metadata.h"
Alex Deymo923d8fa2014-07-15 17:58:51 -070042#include "update_engine/payload_generator/payload_signer.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070043#include "update_engine/payload_generator/topological_sort.h"
Alex Deymo923d8fa2014-07-15 17:58:51 -070044#include "update_engine/payload_verifier.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070045#include "update_engine/subprocess.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070046#include "update_engine/update_metadata.pb.h"
47#include "update_engine/utils.h"
48
49using std::make_pair;
Andrew de los Reyesef017552010-10-06 17:57:52 -070050using std::map;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070051using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070052using std::min;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -070053using std::pair;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070054using std::set;
55using std::string;
56using std::vector;
57
Alex Deymo161c4a12014-05-16 15:56:21 -070058namespace {
59
60const uint64_t kVersionNumber = 1;
61const uint64_t kFullUpdateChunkSize = 1024 * 1024; // bytes
62
63const size_t kBlockSize = 4096; // bytes
Alex Deymo923d8fa2014-07-15 17:58:51 -070064const char kEmptyPath[] = "";
Alex Deymo161c4a12014-05-16 15:56:21 -070065
66static const char* kInstallOperationTypes[] = {
67 "REPLACE",
68 "REPLACE_BZ",
69 "MOVE",
70 "BSDIFF"
71};
72
73} // namespace
74
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070075namespace chromeos_update_engine {
76
77typedef DeltaDiffGenerator::Block Block;
Darin Petkov9fa7ec52010-10-18 11:45:23 -070078typedef map<const DeltaArchiveManifest_InstallOperation*,
Darin Petkov8e447e02013-04-16 16:23:50 +020079 string> OperationNameMap;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070080
Chris Sosaf586b012013-05-21 13:33:42 -070081// bytes
82const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024;
Chris Sosad5ae1562013-04-23 13:20:18 -070083
Gilad Arnoldfa404502014-01-01 23:36:12 -080084// Needed for testing purposes, in case we can't use actual filesystem objects.
Alex Deymo923d8fa2014-07-15 17:58:51 -070085// TODO(garnold) (chromium:331965) Replace this hack with a properly injected
Gilad Arnoldfa404502014-01-01 23:36:12 -080086// parameter in form of a mockable abstract class.
87bool (*get_extents_with_chunk_func)(const std::string&, off_t, off_t,
88 std::vector<Extent>*) =
89 extent_mapper::ExtentsForFileChunkFibmap;
90
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070091namespace {
Darin Petkov68c10d12010-10-14 09:24:37 -070092
Gilad Arnoldfa404502014-01-01 23:36:12 -080093// Stores all the extents of |path| into |extents|. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070094bool GatherExtents(const string& path,
Darin Petkov8e447e02013-04-16 16:23:50 +020095 off_t chunk_offset,
96 off_t chunk_size,
Gilad Arnoldfa404502014-01-01 23:36:12 -080097 vector<Extent>* extents) {
98 extents->clear();
Darin Petkov8e447e02013-04-16 16:23:50 +020099 TEST_AND_RETURN_FALSE(
Gilad Arnoldfa404502014-01-01 23:36:12 -0800100 get_extents_with_chunk_func(
101 path, chunk_offset, chunk_size, extents));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700102 return true;
103}
104
Andrew de los Reyesef017552010-10-06 17:57:52 -0700105// For a given regular file which must exist at new_root + path, and
106// may exist at old_root + path, creates a new InstallOperation and
107// adds it to the graph. Also, populates the |blocks| array as
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700108// necessary, if |blocks| is non-null. Also, writes the data
Andrew de los Reyesef017552010-10-06 17:57:52 -0700109// necessary to send the file down to the client into data_fd, which
110// has length *data_file_size. *data_file_size is updated
111// appropriately. If |existing_vertex| is no kInvalidIndex, use that
112// rather than allocating a new vertex. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700113bool DeltaReadFile(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700114 Vertex::Index existing_vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700115 vector<Block>* blocks,
116 const string& old_root,
117 const string& new_root,
118 const string& path, // within new_root
Darin Petkov8e447e02013-04-16 16:23:50 +0200119 off_t chunk_offset,
120 off_t chunk_size,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700121 int data_fd,
122 off_t* data_file_size) {
123 vector<char> data;
124 DeltaArchiveManifest_InstallOperation operation;
125
Alex Deymo923d8fa2014-07-15 17:58:51 -0700126 string old_path = (old_root == kEmptyPath) ? kEmptyPath :
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800127 old_root + path;
128
Don Garrett1d787092013-03-11 18:07:28 -0700129 // If bsdiff breaks again, blacklist the problem file by using:
130 // bsdiff_allowed = (path != "/foo/bar")
Don Garrett36e60772012-03-29 10:31:20 -0700131 //
Don Garrett1d787092013-03-11 18:07:28 -0700132 // TODO(dgarrett): chromium-os:15274 connect this test to the command line.
Don Garrett36e60772012-03-29 10:31:20 -0700133 bool bsdiff_allowed = true;
Don Garrettf4b28742012-03-27 20:48:06 -0700134
Don Garrett36e60772012-03-29 10:31:20 -0700135 if (!bsdiff_allowed)
136 LOG(INFO) << "bsdiff blacklisting: " << path;
Don Garrettf4b28742012-03-27 20:48:06 -0700137
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800138 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_path,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700139 new_root + path,
Darin Petkov8e447e02013-04-16 16:23:50 +0200140 chunk_offset,
141 chunk_size,
Don Garrett36e60772012-03-29 10:31:20 -0700142 bsdiff_allowed,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700143 &data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700144 &operation,
145 true));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700146
Gilad Arnoldfa404502014-01-01 23:36:12 -0800147 // Check if the operation writes nothing.
148 if (operation.dst_extents_size() == 0) {
149 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
150 LOG(INFO) << "Empty MOVE operation (" << new_root + path << "), skipping";
151 return true;
152 } else {
153 LOG(ERROR) << "Empty non-MOVE operation";
154 return false;
155 }
156 }
157
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700158 // Write the data
159 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
160 operation.set_data_offset(*data_file_size);
161 operation.set_data_length(data.size());
162 }
163
164 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
165 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700166
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700167 // Now, insert into graph and blocks vector
Andrew de los Reyesef017552010-10-06 17:57:52 -0700168 Vertex::Index vertex = existing_vertex;
169 if (vertex == Vertex::kInvalidIndex) {
170 graph->resize(graph->size() + 1);
171 vertex = graph->size() - 1;
172 }
173 (*graph)[vertex].op = operation;
174 CHECK((*graph)[vertex].op.has_type());
175 (*graph)[vertex].file_name = path;
Darin Petkov8e447e02013-04-16 16:23:50 +0200176 (*graph)[vertex].chunk_offset = chunk_offset;
177 (*graph)[vertex].chunk_size = chunk_size;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700178
Andrew de los Reyesef017552010-10-06 17:57:52 -0700179 if (blocks)
Thieu Le5c7d9752010-12-15 16:09:28 -0800180 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::AddInstallOpToBlocksVector(
181 (*graph)[vertex].op,
182 *graph,
183 vertex,
184 blocks));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700185 return true;
186}
187
188// For each regular file within new_root, creates a node in the graph,
189// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
190// and writes any necessary data to the end of data_fd.
191bool DeltaReadFiles(Graph* graph,
192 vector<Block>* blocks,
193 const string& old_root,
194 const string& new_root,
Darin Petkov8e447e02013-04-16 16:23:50 +0200195 off_t chunk_size,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700196 int data_fd,
197 off_t* data_file_size) {
198 set<ino_t> visited_inodes;
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800199 set<ino_t> visited_src_inodes;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700200 for (FilesystemIterator fs_iter(new_root,
201 utils::SetWithValue<string>("/lost+found"));
202 !fs_iter.IsEnd(); fs_iter.Increment()) {
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800203 // We never diff symlinks (here, we check that dst file is not a symlink).
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700204 if (!S_ISREG(fs_iter.GetStat().st_mode))
205 continue;
206
207 // Make sure we visit each inode only once.
208 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
209 continue;
210 visited_inodes.insert(fs_iter.GetStat().st_ino);
Gabe Blacka77939e2014-09-09 23:35:08 -0700211 off_t dst_size = fs_iter.GetFileSize();
Darin Petkov8e447e02013-04-16 16:23:50 +0200212 if (dst_size == 0)
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700213 continue;
214
215 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700216
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800217 // We can't visit each dst image inode more than once, as that would
218 // duplicate work. Here, we avoid visiting each source image inode
219 // more than once. Technically, we could have multiple operations
220 // that read the same blocks from the source image for diffing, but
Alex Vakulenko072359c2014-07-18 11:41:07 -0700221 // we choose not to avoid complexity. Eventually we will move away
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800222 // from using a graph/cycle detection/etc to generate diffs, and at that
223 // time, it will be easy (non-complex) to have many operations read
224 // from the same source blocks. At that time, this code can die. -adlr
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800225 bool should_diff_from_source = false;
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800226 string src_path = old_root + fs_iter.GetPartialPath();
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800227 struct stat src_stbuf;
228 // We never diff symlinks (here, we check that src file is not a symlink).
229 if (0 == lstat(src_path.c_str(), &src_stbuf) &&
230 S_ISREG(src_stbuf.st_mode)) {
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800231 should_diff_from_source = !utils::SetContainsKey(visited_src_inodes,
232 src_stbuf.st_ino);
233 visited_src_inodes.insert(src_stbuf.st_ino);
234 }
235
Darin Petkov8e447e02013-04-16 16:23:50 +0200236 off_t size = chunk_size == -1 ? dst_size : chunk_size;
237 off_t step = size;
238 for (off_t offset = 0; offset < dst_size; offset += step) {
239 if (offset + size >= dst_size) {
240 size = -1; // Read through the end of the file.
241 }
242 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
243 Vertex::kInvalidIndex,
244 blocks,
245 (should_diff_from_source ?
246 old_root :
Alex Deymo923d8fa2014-07-15 17:58:51 -0700247 kEmptyPath),
Darin Petkov8e447e02013-04-16 16:23:50 +0200248 new_root,
249 fs_iter.GetPartialPath(),
250 offset,
251 size,
252 data_fd,
253 data_file_size));
254 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700255 }
256 return true;
257}
258
Andrew de los Reyesef017552010-10-06 17:57:52 -0700259// This class allocates non-existent temp blocks, starting from
260// kTempBlockStart. Other code is responsible for converting these
261// temp blocks into real blocks, as the client can't read or write to
262// these blocks.
263class DummyExtentAllocator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700264 public:
Alex Vakulenko157fe302014-08-11 15:59:58 -0700265 DummyExtentAllocator() : next_block_(kTempBlockStart) {}
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700266 vector<Extent> Allocate(const uint64_t block_count) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700267 vector<Extent> ret(1);
268 ret[0].set_start_block(next_block_);
269 ret[0].set_num_blocks(block_count);
270 next_block_ += block_count;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700271 return ret;
272 }
273 private:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700274 uint64_t next_block_;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700275};
276
277// Reads blocks from image_path that are not yet marked as being written
278// in the blocks array. These blocks that remain are non-file-data blocks.
279// In the future we might consider intelligent diffing between this data
280// and data in the previous image, but for now we just bzip2 compress it
281// and include it in the update.
282// Creates a new node in the graph to write these blocks and writes the
283// appropriate blob to blobs_fd. Reads and updates blobs_length;
284bool ReadUnwrittenBlocks(const vector<Block>& blocks,
285 int blobs_fd,
286 off_t* blobs_length,
287 const string& image_path,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700288 Vertex* vertex) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700289 vertex->file_name = "<rootfs-non-file-data>";
290
Andrew de los Reyesef017552010-10-06 17:57:52 -0700291 DeltaArchiveManifest_InstallOperation* out_op = &vertex->op;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700292 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
293 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
294 ScopedFdCloser image_fd_closer(&image_fd);
295
296 string temp_file_path;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800297 TEST_AND_RETURN_FALSE(utils::MakeTempFile("CrAU_temp_data.XXXXXX",
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700298 &temp_file_path,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700299 nullptr));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700300
301 FILE* file = fopen(temp_file_path.c_str(), "w");
302 TEST_AND_RETURN_FALSE(file);
303 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700304
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700305 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
306 file,
307 9, // max compression
308 0, // verbosity
309 0); // default work factor
310 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700311
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700312 vector<Extent> extents;
313 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700314
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700315 LOG(INFO) << "Appending left over blocks to extents";
316 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
317 if (blocks[i].writer != Vertex::kInvalidIndex)
318 continue;
Andrew de los Reyesef017552010-10-06 17:57:52 -0700319 if (blocks[i].reader != Vertex::kInvalidIndex) {
320 graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i);
321 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700322 graph_utils::AppendBlockToExtents(&extents, i);
323 block_count++;
324 }
325
326 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
327 // so we arbitrarily set it to 1024 * kBlockSize.
328 vector<char> buf(1024 * kBlockSize);
329
330 LOG(INFO) << "Reading left over blocks";
331 vector<Block>::size_type blocks_copied_count = 0;
332
333 // For each extent in extents, write the data into BZ2_bzWrite which
334 // sends it to an output file.
335 // We use the temporary buffer 'buf' to hold the data, which may be
336 // smaller than the extent, so in that case we have to loop to get
337 // the extent's data (that's the inner while loop).
338 for (vector<Extent>::const_iterator it = extents.begin();
339 it != extents.end(); ++it) {
340 vector<Block>::size_type blocks_read = 0;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800341 float printed_progress = -1;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700342 while (blocks_read < it->num_blocks()) {
343 const int copy_block_cnt =
344 min(buf.size() / kBlockSize,
345 static_cast<vector<char>::size_type>(
346 it->num_blocks() - blocks_read));
347 ssize_t rc = pread(image_fd,
348 &buf[0],
349 copy_block_cnt * kBlockSize,
350 (it->start_block() + blocks_read) * kBlockSize);
351 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
352 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
353 copy_block_cnt * kBlockSize);
354 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
355 TEST_AND_RETURN_FALSE(err == BZ_OK);
356 blocks_read += copy_block_cnt;
357 blocks_copied_count += copy_block_cnt;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800358 float current_progress =
359 static_cast<float>(blocks_copied_count) / block_count;
360 if (printed_progress + 0.1 < current_progress ||
361 blocks_copied_count == block_count) {
362 LOG(INFO) << "progress: " << current_progress;
363 printed_progress = current_progress;
364 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700365 }
366 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700367 BZ2_bzWriteClose(&err, bz_file, 0, nullptr, nullptr);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700368 TEST_AND_RETURN_FALSE(err == BZ_OK);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700369 bz_file = nullptr;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700370 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700371 file = nullptr;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700372
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700373 vector<char> compressed_data;
374 LOG(INFO) << "Reading compressed data off disk";
375 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
376 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700377
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700378 // Add node to graph to write these blocks
379 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
380 out_op->set_data_offset(*blobs_length);
381 out_op->set_data_length(compressed_data.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700382 LOG(INFO) << "Rootfs non-data blocks compressed take up "
383 << compressed_data.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700384 *blobs_length += compressed_data.size();
385 out_op->set_dst_length(kBlockSize * block_count);
386 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700387
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700388 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
389 &compressed_data[0],
390 compressed_data.size()));
391 LOG(INFO) << "done with extra blocks";
392 return true;
393}
394
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700395// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700396// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700397bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
398 uint64_t value_be = htobe64(value);
Don Garrette410e0f2011-11-10 15:39:01 -0800399 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700400 return true;
401}
402
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700403// Adds each operation from |graph| to |out_manifest| in the order specified by
404// |order| while building |out_op_name_map| with operation to name
405// mappings. Adds all |kernel_ops| to |out_manifest|. Filters out no-op
406// operations.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700407void InstallOperationsToManifest(
408 const Graph& graph,
409 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700410 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700411 DeltaArchiveManifest* out_manifest,
412 OperationNameMap* out_op_name_map) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700413 for (vector<Vertex::Index>::const_iterator it = order.begin();
414 it != order.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700415 const Vertex& vertex = graph[*it];
416 const DeltaArchiveManifest_InstallOperation& add_op = vertex.op;
417 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
418 continue;
419 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700420 DeltaArchiveManifest_InstallOperation* op =
421 out_manifest->add_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700422 *op = add_op;
Darin Petkov8e447e02013-04-16 16:23:50 +0200423 string name = vertex.file_name;
424 if (vertex.chunk_offset || vertex.chunk_size != -1) {
425 string offset = base::Int64ToString(vertex.chunk_offset);
426 if (vertex.chunk_size != -1) {
427 name += " [" + offset + ", " +
428 base::Int64ToString(vertex.chunk_offset + vertex.chunk_size - 1) +
429 "]";
430 } else {
431 name += " [" + offset + ", end]";
432 }
433 }
434 (*out_op_name_map)[op] = name;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700435 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700436 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
437 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700438 const DeltaArchiveManifest_InstallOperation& add_op = *it;
439 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
440 continue;
441 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700442 DeltaArchiveManifest_InstallOperation* op =
443 out_manifest->add_kernel_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700444 *op = add_op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700445 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700446}
447
448void CheckGraph(const Graph& graph) {
449 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
450 CHECK(it->op.has_type());
451 }
452}
453
Darin Petkov68c10d12010-10-14 09:24:37 -0700454// Delta compresses a kernel partition |new_kernel_part| with knowledge of the
455// old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty
456// string, generates a full update of the partition.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700457bool DeltaCompressKernelPartition(
458 const string& old_kernel_part,
459 const string& new_kernel_part,
460 vector<DeltaArchiveManifest_InstallOperation>* ops,
461 int blobs_fd,
462 off_t* blobs_length) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700463 LOG(INFO) << "Delta compressing kernel partition...";
Darin Petkov68c10d12010-10-14 09:24:37 -0700464 LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update...";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700465
Gilad Arnoldfa404502014-01-01 23:36:12 -0800466 DeltaArchiveManifest_InstallOperation op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700467 vector<char> data;
Don Garrett36e60772012-03-29 10:31:20 -0700468 TEST_AND_RETURN_FALSE(
469 DeltaDiffGenerator::ReadFileToDiff(old_kernel_part,
470 new_kernel_part,
Darin Petkov8e447e02013-04-16 16:23:50 +0200471 0, // chunk_offset
472 -1, // chunk_size
Alex Deymo923d8fa2014-07-15 17:58:51 -0700473 true, // bsdiff_allowed
Don Garrett36e60772012-03-29 10:31:20 -0700474 &data,
Gilad Arnoldfa404502014-01-01 23:36:12 -0800475 &op,
Don Garrett36e60772012-03-29 10:31:20 -0700476 false));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700477
Gilad Arnoldfa404502014-01-01 23:36:12 -0800478 // Check if the operation writes nothing.
479 if (op.dst_extents_size() == 0) {
480 if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
481 LOG(INFO) << "Empty MOVE operation, nothing to do.";
482 return true;
483 } else {
484 LOG(ERROR) << "Empty non-MOVE operation";
485 return false;
486 }
Darin Petkov68c10d12010-10-14 09:24:37 -0700487 }
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700488
Gilad Arnoldfa404502014-01-01 23:36:12 -0800489 // Write the data.
490 if (op.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
491 op.set_data_offset(*blobs_length);
492 op.set_data_length(data.size());
493 }
494
495 // Add the new install operation.
496 ops->clear();
497 ops->push_back(op);
498
Darin Petkov68c10d12010-10-14 09:24:37 -0700499 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
500 *blobs_length += data.size();
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700501
Darin Petkov68c10d12010-10-14 09:24:37 -0700502 LOG(INFO) << "Done delta compressing kernel partition: "
Gilad Arnoldfa404502014-01-01 23:36:12 -0800503 << kInstallOperationTypes[op.type()];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700504 return true;
505}
506
Darin Petkov880335c2010-10-01 15:52:53 -0700507struct DeltaObject {
508 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
509 : name(in_name),
510 type(in_type),
511 size(in_size) {}
512 bool operator <(const DeltaObject& object) const {
Darin Petkovd43d6902010-10-14 11:17:50 -0700513 return (size != object.size) ? (size < object.size) : (name < object.name);
Darin Petkov880335c2010-10-01 15:52:53 -0700514 }
515 string name;
516 int type;
517 off_t size;
518};
519
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700520void ReportPayloadUsage(const DeltaArchiveManifest& manifest,
521 const int64_t manifest_metadata_size,
522 const OperationNameMap& op_name_map) {
Darin Petkov880335c2010-10-01 15:52:53 -0700523 vector<DeltaObject> objects;
524 off_t total_size = 0;
525
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700526 // Rootfs install operations.
527 for (int i = 0; i < manifest.install_operations_size(); ++i) {
528 const DeltaArchiveManifest_InstallOperation& op =
529 manifest.install_operations(i);
Darin Petkov8e447e02013-04-16 16:23:50 +0200530 objects.push_back(DeltaObject(op_name_map.find(&op)->second,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700531 op.type(),
532 op.data_length()));
533 total_size += op.data_length();
Darin Petkov880335c2010-10-01 15:52:53 -0700534 }
535
Darin Petkov880335c2010-10-01 15:52:53 -0700536 // Kernel install operations.
537 for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) {
538 const DeltaArchiveManifest_InstallOperation& op =
539 manifest.kernel_install_operations(i);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700540 objects.push_back(DeltaObject(base::StringPrintf("<kernel-operation-%d>",
541 i),
Darin Petkov880335c2010-10-01 15:52:53 -0700542 op.type(),
543 op.data_length()));
544 total_size += op.data_length();
545 }
546
Darin Petkov95cf01f2010-10-12 14:59:13 -0700547 objects.push_back(DeltaObject("<manifest-metadata>",
548 -1,
549 manifest_metadata_size));
550 total_size += manifest_metadata_size;
551
Darin Petkov880335c2010-10-01 15:52:53 -0700552 std::sort(objects.begin(), objects.end());
553
Alex Vakulenko75039d72014-03-25 12:36:28 -0700554 static const char kFormatString[] = "%6.2f%% %10jd %-10s %s\n";
Darin Petkov880335c2010-10-01 15:52:53 -0700555 for (vector<DeltaObject>::const_iterator it = objects.begin();
556 it != objects.end(); ++it) {
557 const DeltaObject& object = *it;
558 fprintf(stderr, kFormatString,
559 object.size * 100.0 / total_size,
Alex Vakulenko75039d72014-03-25 12:36:28 -0700560 static_cast<intmax_t>(object.size),
Darin Petkov95cf01f2010-10-12 14:59:13 -0700561 object.type >= 0 ? kInstallOperationTypes[object.type] : "-",
Darin Petkov880335c2010-10-01 15:52:53 -0700562 object.name.c_str());
563 }
Alex Vakulenko75039d72014-03-25 12:36:28 -0700564 fprintf(stderr, kFormatString,
565 100.0, static_cast<intmax_t>(total_size), "", "<total>");
Darin Petkov880335c2010-10-01 15:52:53 -0700566}
567
Gilad Arnoldfa404502014-01-01 23:36:12 -0800568// Process a range of blocks from |range_start| to |range_end| in the extent at
569// position |*idx_p| of |extents|. If |do_remove| is true, this range will be
570// removed, which may cause the extent to be trimmed, split or removed entirely.
571// The value of |*idx_p| is updated to point to the next extent to be processed.
572// Returns true iff the next extent to process is a new or updated one.
573bool ProcessExtentBlockRange(vector<Extent>* extents, size_t* idx_p,
574 const bool do_remove, uint64_t range_start,
575 uint64_t range_end) {
576 size_t idx = *idx_p;
577 uint64_t start_block = (*extents)[idx].start_block();
578 uint64_t num_blocks = (*extents)[idx].num_blocks();
579 uint64_t range_size = range_end - range_start;
580
581 if (do_remove) {
582 if (range_size == num_blocks) {
583 // Remove the entire extent.
584 extents->erase(extents->begin() + idx);
585 } else if (range_end == num_blocks) {
586 // Trim the end of the extent.
587 (*extents)[idx].set_num_blocks(num_blocks - range_size);
588 idx++;
589 } else if (range_start == 0) {
590 // Trim the head of the extent.
591 (*extents)[idx].set_start_block(start_block + range_size);
592 (*extents)[idx].set_num_blocks(num_blocks - range_size);
593 } else {
594 // Trim the middle, splitting the remainder into two parts.
595 (*extents)[idx].set_num_blocks(range_start);
596 Extent e;
597 e.set_start_block(start_block + range_end);
598 e.set_num_blocks(num_blocks - range_end);
599 idx++;
600 extents->insert(extents->begin() + idx, e);
601 }
602 } else if (range_end == num_blocks) {
603 // Done with this extent.
604 idx++;
605 } else {
606 return false;
607 }
608
609 *idx_p = idx;
610 return true;
611}
612
613// Remove identical corresponding block ranges in |src_extents| and
614// |dst_extents|. Used for preventing moving of blocks onto themselves during
Gilad Arnoldebca5712014-01-10 14:26:37 -0800615// MOVE operations. The value of |total_bytes| indicates the actual length of
616// content; this may be slightly less than the total size of blocks, in which
617// case the last block is only partly occupied with data. Returns the total
618// number of bytes removed.
619size_t RemoveIdenticalBlockRanges(vector<Extent>* src_extents,
620 vector<Extent>* dst_extents,
621 const size_t total_bytes) {
Gilad Arnoldfa404502014-01-01 23:36:12 -0800622 size_t src_idx = 0;
623 size_t dst_idx = 0;
624 uint64_t src_offset = 0, dst_offset = 0;
625 bool new_src = true, new_dst = true;
Gilad Arnoldebca5712014-01-10 14:26:37 -0800626 size_t removed_bytes = 0, nonfull_block_bytes;
627 bool do_remove = false;
Gilad Arnoldfa404502014-01-01 23:36:12 -0800628 while (src_idx < src_extents->size() && dst_idx < dst_extents->size()) {
629 if (new_src) {
630 src_offset = 0;
631 new_src = false;
632 }
633 if (new_dst) {
634 dst_offset = 0;
635 new_dst = false;
636 }
637
Gilad Arnoldebca5712014-01-10 14:26:37 -0800638 do_remove = ((*src_extents)[src_idx].start_block() + src_offset ==
639 (*dst_extents)[dst_idx].start_block() + dst_offset);
Gilad Arnoldfa404502014-01-01 23:36:12 -0800640
641 uint64_t src_num_blocks = (*src_extents)[src_idx].num_blocks();
642 uint64_t dst_num_blocks = (*dst_extents)[dst_idx].num_blocks();
643 uint64_t min_num_blocks = min(src_num_blocks - src_offset,
644 dst_num_blocks - dst_offset);
645 uint64_t prev_src_offset = src_offset;
646 uint64_t prev_dst_offset = dst_offset;
647 src_offset += min_num_blocks;
648 dst_offset += min_num_blocks;
649
650 new_src = ProcessExtentBlockRange(src_extents, &src_idx, do_remove,
651 prev_src_offset, src_offset);
652 new_dst = ProcessExtentBlockRange(dst_extents, &dst_idx, do_remove,
653 prev_dst_offset, dst_offset);
Gilad Arnoldebca5712014-01-10 14:26:37 -0800654 if (do_remove)
655 removed_bytes += min_num_blocks * kBlockSize;
Gilad Arnoldfa404502014-01-01 23:36:12 -0800656 }
Gilad Arnoldebca5712014-01-10 14:26:37 -0800657
658 // If we removed the last block and this block is only partly used by file
659 // content, deduct the unused portion from the total removed byte count.
660 if (do_remove && (nonfull_block_bytes = total_bytes % kBlockSize))
661 removed_bytes -= kBlockSize - nonfull_block_bytes;
662
663 return removed_bytes;
Gilad Arnoldfa404502014-01-01 23:36:12 -0800664}
665
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700666} // namespace
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700667
668bool DeltaDiffGenerator::ReadFileToDiff(
669 const string& old_filename,
670 const string& new_filename,
Darin Petkov8e447e02013-04-16 16:23:50 +0200671 off_t chunk_offset,
672 off_t chunk_size,
Don Garrett36e60772012-03-29 10:31:20 -0700673 bool bsdiff_allowed,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700674 vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700675 DeltaArchiveManifest_InstallOperation* out_op,
676 bool gather_extents) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700677 // Read new data in
678 vector<char> new_data;
Darin Petkov8e447e02013-04-16 16:23:50 +0200679 TEST_AND_RETURN_FALSE(
680 utils::ReadFileChunk(new_filename, chunk_offset, chunk_size, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700681
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700682 TEST_AND_RETURN_FALSE(!new_data.empty());
Darin Petkov8e447e02013-04-16 16:23:50 +0200683 TEST_AND_RETURN_FALSE(chunk_size == -1 ||
684 static_cast<off_t>(new_data.size()) <= chunk_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700685
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700686 vector<char> new_data_bz;
687 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
688 CHECK(!new_data_bz.empty());
689
690 vector<char> data; // Data blob that will be written to delta file.
691
692 DeltaArchiveManifest_InstallOperation operation;
693 size_t current_best_size = 0;
694 if (new_data.size() <= new_data_bz.size()) {
695 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
696 current_best_size = new_data.size();
697 data = new_data;
698 } else {
699 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
700 current_best_size = new_data_bz.size();
701 data = new_data_bz;
702 }
703
704 // Do we have an original file to consider?
Gabe Blacka77939e2014-09-09 23:35:08 -0700705 off_t old_size = 0;
Don Garrettf4b28742012-03-27 20:48:06 -0700706 bool original = !old_filename.empty();
Gabe Blacka77939e2014-09-09 23:35:08 -0700707 if (original && (old_size = utils::FileSize(old_filename)) < 0) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700708 // If stat-ing the old file fails, it should be because it doesn't exist.
Gabe Blacka77939e2014-09-09 23:35:08 -0700709 TEST_AND_RETURN_FALSE(!utils::FileExists(old_filename.c_str()));
Don Garrettf4b28742012-03-27 20:48:06 -0700710 original = false;
Darin Petkov68c10d12010-10-14 09:24:37 -0700711 }
Don Garrettf4b28742012-03-27 20:48:06 -0700712
Darin Petkov8e447e02013-04-16 16:23:50 +0200713 vector<char> old_data;
Don Garrettf4b28742012-03-27 20:48:06 -0700714 if (original) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700715 // Read old data
Darin Petkov8e447e02013-04-16 16:23:50 +0200716 TEST_AND_RETURN_FALSE(
717 utils::ReadFileChunk(
718 old_filename, chunk_offset, chunk_size, &old_data));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700719 if (old_data == new_data) {
720 // No change in data.
721 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
722 current_best_size = 0;
723 data.clear();
Darin Petkov8e447e02013-04-16 16:23:50 +0200724 } else if (!old_data.empty() && bsdiff_allowed) {
Don Garrett36e60772012-03-29 10:31:20 -0700725 // If the source file is considered bsdiff safe (no bsdiff bugs
726 // triggered), see if BSDIFF encoding is smaller.
Alex Vakulenko75039d72014-03-25 12:36:28 -0700727 base::FilePath old_chunk;
728 TEST_AND_RETURN_FALSE(base::CreateTemporaryFile(&old_chunk));
Darin Petkov8e447e02013-04-16 16:23:50 +0200729 ScopedPathUnlinker old_unlinker(old_chunk.value());
730 TEST_AND_RETURN_FALSE(
731 utils::WriteFile(old_chunk.value().c_str(),
732 &old_data[0], old_data.size()));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700733 base::FilePath new_chunk;
734 TEST_AND_RETURN_FALSE(base::CreateTemporaryFile(&new_chunk));
Darin Petkov8e447e02013-04-16 16:23:50 +0200735 ScopedPathUnlinker new_unlinker(new_chunk.value());
736 TEST_AND_RETURN_FALSE(
737 utils::WriteFile(new_chunk.value().c_str(),
738 &new_data[0], new_data.size()));
739
Don Garrett36e60772012-03-29 10:31:20 -0700740 vector<char> bsdiff_delta;
741 TEST_AND_RETURN_FALSE(
Darin Petkov8e447e02013-04-16 16:23:50 +0200742 BsdiffFiles(old_chunk.value(), new_chunk.value(), &bsdiff_delta));
Don Garrett36e60772012-03-29 10:31:20 -0700743 CHECK_GT(bsdiff_delta.size(), static_cast<vector<char>::size_type>(0));
744 if (bsdiff_delta.size() < current_best_size) {
745 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
746 current_best_size = bsdiff_delta.size();
747 data = bsdiff_delta;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700748 }
749 }
750 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700751
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700752 // Set parameters of the operations
753 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700754
Gilad Arnoldfa404502014-01-01 23:36:12 -0800755 vector<Extent> src_extents, dst_extents;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700756 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
757 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Darin Petkov68c10d12010-10-14 09:24:37 -0700758 if (gather_extents) {
759 TEST_AND_RETURN_FALSE(
Darin Petkov8e447e02013-04-16 16:23:50 +0200760 GatherExtents(old_filename,
761 chunk_offset,
762 chunk_size,
Gilad Arnoldfa404502014-01-01 23:36:12 -0800763 &src_extents));
Darin Petkov68c10d12010-10-14 09:24:37 -0700764 } else {
765 Extent* src_extent = operation.add_src_extents();
766 src_extent->set_start_block(0);
Gabe Blacka77939e2014-09-09 23:35:08 -0700767 src_extent->set_num_blocks((old_size + kBlockSize - 1) / kBlockSize);
Darin Petkov68c10d12010-10-14 09:24:37 -0700768 }
Darin Petkov8e447e02013-04-16 16:23:50 +0200769 operation.set_src_length(old_data.size());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700770 }
771
Darin Petkov68c10d12010-10-14 09:24:37 -0700772 if (gather_extents) {
773 TEST_AND_RETURN_FALSE(
Darin Petkov8e447e02013-04-16 16:23:50 +0200774 GatherExtents(new_filename,
775 chunk_offset,
776 chunk_size,
Gilad Arnoldfa404502014-01-01 23:36:12 -0800777 &dst_extents));
Darin Petkov68c10d12010-10-14 09:24:37 -0700778 } else {
779 Extent* dst_extent = operation.add_dst_extents();
780 dst_extent->set_start_block(0);
781 dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize);
782 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700783 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700784
Gilad Arnoldfa404502014-01-01 23:36:12 -0800785 if (gather_extents) {
786 // Remove identical src/dst block ranges in MOVE operations.
Gilad Arnoldebca5712014-01-10 14:26:37 -0800787 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
788 size_t removed_bytes = RemoveIdenticalBlockRanges(
789 &src_extents, &dst_extents, new_data.size());
790
791 // Adjust the file length field accordingly.
792 if (removed_bytes) {
793 operation.set_src_length(old_data.size() - removed_bytes);
794 operation.set_dst_length(new_data.size() - removed_bytes);
795 }
796 }
Gilad Arnoldfa404502014-01-01 23:36:12 -0800797
798 // Embed extents in the operation.
799 DeltaDiffGenerator::StoreExtents(src_extents,
800 operation.mutable_src_extents());
801 DeltaDiffGenerator::StoreExtents(dst_extents,
802 operation.mutable_dst_extents());
803 }
804
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700805 out_data->swap(data);
806 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700807
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700808 return true;
809}
810
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700811bool DeltaDiffGenerator::InitializePartitionInfo(bool is_kernel,
812 const string& partition,
813 PartitionInfo* info) {
Darin Petkov7ea32332010-10-13 10:46:11 -0700814 int64_t size = 0;
815 if (is_kernel) {
816 size = utils::FileSize(partition);
817 } else {
818 int block_count = 0, block_size = 0;
819 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition,
820 &block_count,
821 &block_size));
822 size = static_cast<int64_t>(block_count) * block_size;
823 }
824 TEST_AND_RETURN_FALSE(size > 0);
Darin Petkov36a58222010-10-07 22:00:09 -0700825 info->set_size(size);
826 OmahaHashCalculator hasher;
Darin Petkov7ea32332010-10-13 10:46:11 -0700827 TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size);
Darin Petkov36a58222010-10-07 22:00:09 -0700828 TEST_AND_RETURN_FALSE(hasher.Finalize());
829 const vector<char>& hash = hasher.raw_hash();
830 info->set_hash(hash.data(), hash.size());
Darin Petkovd43d6902010-10-14 11:17:50 -0700831 LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash();
Darin Petkov36a58222010-10-07 22:00:09 -0700832 return true;
833}
834
835bool InitializePartitionInfos(const string& old_kernel,
836 const string& new_kernel,
837 const string& old_rootfs,
838 const string& new_rootfs,
839 DeltaArchiveManifest* manifest) {
Darin Petkovd43d6902010-10-14 11:17:50 -0700840 if (!old_kernel.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700841 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
842 true,
843 old_kernel,
844 manifest->mutable_old_kernel_info()));
Darin Petkovd43d6902010-10-14 11:17:50 -0700845 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700846 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
847 true,
848 new_kernel,
849 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700850 if (!old_rootfs.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700851 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
852 false,
853 old_rootfs,
854 manifest->mutable_old_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700855 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700856 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
857 false,
858 new_rootfs,
859 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700860 return true;
861}
862
Andrew de los Reyesef017552010-10-06 17:57:52 -0700863namespace {
864
865// Takes a collection (vector or RepeatedPtrField) of Extent and
866// returns a vector of the blocks referenced, in order.
867template<typename T>
868vector<uint64_t> ExpandExtents(const T& extents) {
869 vector<uint64_t> ret;
870 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
871 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700872 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700873 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700874 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700875 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700876 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700877 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700878 }
879 }
880 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700881 return ret;
882}
883
884// Takes a vector of blocks and returns an equivalent vector of Extent
885// objects.
886vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
887 vector<Extent> new_extents;
888 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
889 it != e; ++it) {
890 graph_utils::AppendBlockToExtents(&new_extents, *it);
891 }
892 return new_extents;
893}
894
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700895} // namespace
Andrew de los Reyesef017552010-10-06 17:57:52 -0700896
897void DeltaDiffGenerator::SubstituteBlocks(
898 Vertex* vertex,
899 const vector<Extent>& remove_extents,
900 const vector<Extent>& replace_extents) {
901 // First, expand out the blocks that op reads from
902 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700903 {
904 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700905 vector<uint64_t> remove_extents_expanded =
906 ExpandExtents(remove_extents);
907 vector<uint64_t> replace_extents_expanded =
908 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700909 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700910 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700911 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700912 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700913 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
914 }
915 utils::ApplyMap(&read_blocks, conversion);
916 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
917 e = vertex->out_edges.end(); it != e; ++it) {
918 vector<uint64_t> write_before_deps_expanded =
919 ExpandExtents(it->second.write_extents);
920 utils::ApplyMap(&write_before_deps_expanded, conversion);
921 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700922 }
923 }
924 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700925 vertex->op.clear_src_extents();
926 vector<Extent> new_extents = CompressExtents(read_blocks);
927 DeltaDiffGenerator::StoreExtents(new_extents,
928 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700929}
930
931bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700932 const set<Edge>& edges,
933 vector<CutEdgeVertexes>* out_cuts) {
934 DummyExtentAllocator scratch_allocator;
935 vector<CutEdgeVertexes> cuts;
936 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700937
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700938 uint64_t scratch_blocks_used = 0;
939 for (set<Edge>::const_iterator it = edges.begin();
940 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700941 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700942 vector<Extent> old_extents =
943 (*graph)[it->first].out_edges[it->second].extents;
944 // Choose some scratch space
945 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700946 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700947 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
948 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700949 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700950 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700951 cuts.back().old_src = it->first;
952 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700953
Andrew de los Reyesef017552010-10-06 17:57:52 -0700954 EdgeProperties& cut_edge_properties =
955 (*graph)[it->first].out_edges.find(it->second)->second;
956
957 // This should never happen, as we should only be cutting edges between
958 // real file nodes, and write-before relationships are created from
959 // a real file node to a temp copy node:
960 CHECK(cut_edge_properties.write_extents.empty())
961 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700962
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700963 // make node depend on the copy operation
964 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700965 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700966
967 // Set src/dst extents and other proto variables for copy operation
968 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
969 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700970 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700971 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700972 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700973 graph->back().op.mutable_dst_extents());
974 graph->back().op.set_src_length(
975 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
976 graph->back().op.set_dst_length(graph->back().op.src_length());
977
978 // make the dest node read from the scratch space
979 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700980 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700981 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700982 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700983
984 // delete the old edge
Mike Frysinger0f9547d2012-02-16 12:11:37 -0500985 CHECK_EQ(static_cast<Graph::size_type>(1),
986 (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700987
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700988 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700989 EdgeProperties write_before_edge_properties;
990 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
991 (*graph)[it->second].out_edges.insert(
992 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700993 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700994 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700995 return true;
996}
997
998// Stores all Extents in 'extents' into 'out'.
999void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -07001000 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001001 google::protobuf::RepeatedPtrField<Extent>* out) {
1002 for (vector<Extent>::const_iterator it = extents.begin();
1003 it != extents.end(); ++it) {
1004 Extent* new_extent = out->Add();
1005 *new_extent = *it;
1006 }
1007}
1008
1009// Creates all the edges for the graph. Writers of a block point to
1010// readers of the same block. This is because for an edge A->B, B
1011// must complete before A executes.
1012void DeltaDiffGenerator::CreateEdges(Graph* graph,
1013 const vector<Block>& blocks) {
1014 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1015 // Blocks with both a reader and writer get an edge
1016 if (blocks[i].reader == Vertex::kInvalidIndex ||
1017 blocks[i].writer == Vertex::kInvalidIndex)
1018 continue;
1019 // Don't have a node depend on itself
1020 if (blocks[i].reader == blocks[i].writer)
1021 continue;
1022 // See if there's already an edge we can add onto
1023 Vertex::EdgeMap::iterator edge_it =
1024 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
1025 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
1026 // No existing edge. Create one
1027 (*graph)[blocks[i].writer].out_edges.insert(
1028 make_pair(blocks[i].reader, EdgeProperties()));
1029 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -07001030 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001031 }
1032 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
1033 }
1034}
1035
Andrew de los Reyesef017552010-10-06 17:57:52 -07001036namespace {
1037
1038class SortCutsByTopoOrderLess {
1039 public:
Alex Deymo923d8fa2014-07-15 17:58:51 -07001040 explicit SortCutsByTopoOrderLess(
1041 const vector<vector<Vertex::Index>::size_type>& table)
Andrew de los Reyesef017552010-10-06 17:57:52 -07001042 : table_(table) {}
1043 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
1044 return table_[a.old_dst] < table_[b.old_dst];
1045 }
1046 private:
Alex Deymo923d8fa2014-07-15 17:58:51 -07001047 const vector<vector<Vertex::Index>::size_type>& table_;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001048};
1049
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001050} // namespace
Andrew de los Reyesef017552010-10-06 17:57:52 -07001051
1052void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
Alex Deymo923d8fa2014-07-15 17:58:51 -07001053 const vector<Vertex::Index>& op_indexes,
Andrew de los Reyesef017552010-10-06 17:57:52 -07001054 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
1055 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
1056 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
1057 i != e; ++i) {
1058 Vertex::Index node = op_indexes[i];
1059 if (table.size() < (node + 1)) {
1060 table.resize(node + 1);
1061 }
1062 table[node] = i;
1063 }
1064 reverse_op_indexes->swap(table);
1065}
1066
Alex Deymo923d8fa2014-07-15 17:58:51 -07001067void DeltaDiffGenerator::SortCutsByTopoOrder(
1068 const vector<Vertex::Index>& op_indexes,
1069 vector<CutEdgeVertexes>* cuts) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001070 // first, make a reverse lookup table.
1071 vector<vector<Vertex::Index>::size_type> table;
1072 GenerateReverseTopoOrderMap(op_indexes, &table);
1073 SortCutsByTopoOrderLess less(table);
1074 sort(cuts->begin(), cuts->end(), less);
1075}
1076
1077void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
1078 vector<Vertex::Index>* op_indexes) {
1079 vector<Vertex::Index> ret;
1080 vector<Vertex::Index> full_ops;
1081 ret.reserve(op_indexes->size());
1082 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
1083 ++i) {
1084 DeltaArchiveManifest_InstallOperation_Type type =
1085 (*graph)[(*op_indexes)[i]].op.type();
1086 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
1087 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
1088 full_ops.push_back((*op_indexes)[i]);
1089 } else {
1090 ret.push_back((*op_indexes)[i]);
1091 }
1092 }
1093 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
1094 << (full_ops.size() + ret.size()) << " total ops.";
1095 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
1096 op_indexes->swap(ret);
1097}
1098
1099namespace {
1100
1101template<typename T>
1102bool TempBlocksExistInExtents(const T& extents) {
1103 for (int i = 0, e = extents.size(); i < e; ++i) {
1104 Extent extent = graph_utils::GetElement(extents, i);
1105 uint64_t start = extent.start_block();
1106 uint64_t num = extent.num_blocks();
1107 if (start == kSparseHole)
1108 continue;
1109 if (start >= kTempBlockStart ||
1110 (start + num) >= kTempBlockStart) {
1111 LOG(ERROR) << "temp block!";
1112 LOG(ERROR) << "start: " << start << ", num: " << num;
1113 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
1114 LOG(ERROR) << "returning true";
1115 return true;
1116 }
1117 // check for wrap-around, which would be a bug:
1118 CHECK(start <= (start + num));
1119 }
1120 return false;
1121}
1122
Alex Vakulenko072359c2014-07-18 11:41:07 -07001123// Converts the cuts, which must all have the same |old_dst| member,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001124// to full. It does this by converting the |old_dst| to REPLACE or
1125// REPLACE_BZ, dropping all incoming edges to |old_dst|, and marking
1126// all temp nodes invalid.
1127bool ConvertCutsToFull(
1128 Graph* graph,
1129 const string& new_root,
1130 int data_fd,
1131 off_t* data_file_size,
1132 vector<Vertex::Index>* op_indexes,
1133 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
1134 const vector<CutEdgeVertexes>& cuts) {
1135 CHECK(!cuts.empty());
1136 set<Vertex::Index> deleted_nodes;
1137 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1138 e = cuts.end(); it != e; ++it) {
1139 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp(
1140 graph,
1141 *it,
1142 new_root,
1143 data_fd,
1144 data_file_size));
1145 deleted_nodes.insert(it->new_vertex);
1146 }
1147 deleted_nodes.insert(cuts[0].old_dst);
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001148
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001149 vector<Vertex::Index> new_op_indexes;
1150 new_op_indexes.reserve(op_indexes->size());
1151 for (vector<Vertex::Index>::iterator it = op_indexes->begin(),
1152 e = op_indexes->end(); it != e; ++it) {
1153 if (utils::SetContainsKey(deleted_nodes, *it))
1154 continue;
1155 new_op_indexes.push_back(*it);
1156 }
1157 new_op_indexes.push_back(cuts[0].old_dst);
1158 op_indexes->swap(new_op_indexes);
1159 DeltaDiffGenerator::GenerateReverseTopoOrderMap(*op_indexes,
1160 reverse_op_indexes);
1161 return true;
1162}
1163
1164// Tries to assign temp blocks for a collection of cuts, all of which share
1165// the same old_dst member. If temp blocks can't be found, old_dst will be
1166// converted to a REPLACE or REPLACE_BZ operation. Returns true on success,
1167// which can happen even if blocks are converted to full. Returns false
1168// on exceptional error cases.
1169bool AssignBlockForAdjoiningCuts(
1170 Graph* graph,
1171 const string& new_root,
1172 int data_fd,
1173 off_t* data_file_size,
1174 vector<Vertex::Index>* op_indexes,
1175 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
1176 const vector<CutEdgeVertexes>& cuts) {
1177 CHECK(!cuts.empty());
1178 const Vertex::Index old_dst = cuts[0].old_dst;
1179 // Calculate # of blocks needed
1180 uint64_t blocks_needed = 0;
1181 map<const CutEdgeVertexes*, uint64_t> cuts_blocks_needed;
1182 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1183 e = cuts.end(); it != e; ++it) {
1184 uint64_t cut_blocks_needed = 0;
1185 for (vector<Extent>::const_iterator jt = it->tmp_extents.begin(),
1186 je = it->tmp_extents.end(); jt != je; ++jt) {
1187 cut_blocks_needed += jt->num_blocks();
1188 }
1189 blocks_needed += cut_blocks_needed;
1190 cuts_blocks_needed[&*it] = cut_blocks_needed;
1191 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001192
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001193 // Find enough blocks
1194 ExtentRanges scratch_ranges;
1195 // Each block that's supplying temp blocks and the corresponding blocks:
Ben Chanf9cb98c2014-09-21 18:31:30 -07001196 typedef vector<pair<Vertex::Index, ExtentRanges>> SupplierVector;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001197 SupplierVector block_suppliers;
1198 uint64_t scratch_blocks_found = 0;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001199 for (vector<Vertex::Index>::size_type i = (*reverse_op_indexes)[old_dst] + 1,
1200 e = op_indexes->size(); i < e; ++i) {
1201 Vertex::Index test_node = (*op_indexes)[i];
1202 if (!(*graph)[test_node].valid)
1203 continue;
1204 // See if this node has sufficient blocks
1205 ExtentRanges ranges;
1206 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
1207 ranges.SubtractExtent(ExtentForRange(
1208 kTempBlockStart, kSparseHole - kTempBlockStart));
1209 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
1210 // For now, for simplicity, subtract out all blocks in read-before
1211 // dependencies.
1212 for (Vertex::EdgeMap::const_iterator edge_i =
1213 (*graph)[test_node].out_edges.begin(),
1214 edge_e = (*graph)[test_node].out_edges.end();
1215 edge_i != edge_e; ++edge_i) {
1216 ranges.SubtractExtents(edge_i->second.extents);
1217 }
1218 if (ranges.blocks() == 0)
1219 continue;
1220
1221 if (ranges.blocks() + scratch_blocks_found > blocks_needed) {
1222 // trim down ranges
1223 vector<Extent> new_ranges = ranges.GetExtentsForBlockCount(
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001224 blocks_needed - scratch_blocks_found);
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001225 ranges = ExtentRanges();
1226 ranges.AddExtents(new_ranges);
1227 }
1228 scratch_ranges.AddRanges(ranges);
1229 block_suppliers.push_back(make_pair(test_node, ranges));
1230 scratch_blocks_found += ranges.blocks();
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001231 if (scratch_ranges.blocks() >= blocks_needed)
1232 break;
1233 }
1234 if (scratch_ranges.blocks() < blocks_needed) {
1235 LOG(INFO) << "Unable to find sufficient scratch";
1236 TEST_AND_RETURN_FALSE(ConvertCutsToFull(graph,
1237 new_root,
1238 data_fd,
1239 data_file_size,
1240 op_indexes,
1241 reverse_op_indexes,
1242 cuts));
1243 return true;
1244 }
1245 // Use the scratch we found
1246 TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found);
1247
1248 // Make all the suppliers depend on this node
1249 for (SupplierVector::iterator it = block_suppliers.begin(),
1250 e = block_suppliers.end(); it != e; ++it) {
1251 graph_utils::AddReadBeforeDepExtents(
1252 &(*graph)[it->first],
1253 old_dst,
1254 it->second.GetExtentsForBlockCount(it->second.blocks()));
1255 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001256
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001257 // Replace temp blocks in each cut
1258 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1259 e = cuts.end(); it != e; ++it) {
1260 vector<Extent> real_extents =
1261 scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]);
1262 scratch_ranges.SubtractExtents(real_extents);
1263
1264 // Fix the old dest node w/ the real blocks
1265 DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst],
1266 it->tmp_extents,
1267 real_extents);
1268
1269 // Fix the new node w/ the real blocks. Since the new node is just a
1270 // copy operation, we can replace all the dest extents w/ the real
1271 // blocks.
1272 DeltaArchiveManifest_InstallOperation *op =
1273 &(*graph)[it->new_vertex].op;
1274 op->clear_dst_extents();
1275 DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents());
1276 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001277 return true;
1278}
1279
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001280} // namespace
Andrew de los Reyesef017552010-10-06 17:57:52 -07001281
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001282// Returns true if |op| is a no-op operation that doesn't do any useful work
1283// (e.g., a move operation that copies blocks onto themselves).
1284bool DeltaDiffGenerator::IsNoopOperation(
1285 const DeltaArchiveManifest_InstallOperation& op) {
1286 return (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE &&
1287 ExpandExtents(op.src_extents()) == ExpandExtents(op.dst_extents()));
1288}
1289
Andrew de los Reyesef017552010-10-06 17:57:52 -07001290bool DeltaDiffGenerator::AssignTempBlocks(
1291 Graph* graph,
1292 const string& new_root,
1293 int data_fd,
1294 off_t* data_file_size,
1295 vector<Vertex::Index>* op_indexes,
1296 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001297 const vector<CutEdgeVertexes>& cuts) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001298 CHECK(!cuts.empty());
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001299
1300 // group of cuts w/ the same old_dst:
1301 vector<CutEdgeVertexes> cuts_group;
1302
Andrew de los Reyesef017552010-10-06 17:57:52 -07001303 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
1304 true ; --i) {
1305 LOG(INFO) << "Fixing temp blocks in cut " << i
1306 << ": old dst: " << cuts[i].old_dst << " new vertex: "
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001307 << cuts[i].new_vertex << " path: "
1308 << (*graph)[cuts[i].old_dst].file_name;
1309
1310 if (cuts_group.empty() || (cuts_group[0].old_dst == cuts[i].old_dst)) {
1311 cuts_group.push_back(cuts[i]);
1312 } else {
1313 CHECK(!cuts_group.empty());
1314 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1315 new_root,
1316 data_fd,
1317 data_file_size,
1318 op_indexes,
1319 reverse_op_indexes,
1320 cuts_group));
1321 cuts_group.clear();
1322 cuts_group.push_back(cuts[i]);
Andrew de los Reyesef017552010-10-06 17:57:52 -07001323 }
Darin Petkov36a58222010-10-07 22:00:09 -07001324
Andrew de los Reyesef017552010-10-06 17:57:52 -07001325 if (i == e) {
1326 // break out of for() loop
1327 break;
1328 }
1329 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001330 CHECK(!cuts_group.empty());
1331 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1332 new_root,
1333 data_fd,
1334 data_file_size,
1335 op_indexes,
1336 reverse_op_indexes,
1337 cuts_group));
Andrew de los Reyesef017552010-10-06 17:57:52 -07001338 return true;
1339}
1340
1341bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1342 size_t idx = 0;
1343 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1344 ++it, ++idx) {
1345 if (!it->valid)
1346 continue;
1347 const DeltaArchiveManifest_InstallOperation& op = it->op;
1348 if (TempBlocksExistInExtents(op.dst_extents()) ||
1349 TempBlocksExistInExtents(op.src_extents())) {
1350 LOG(INFO) << "bad extents in node " << idx;
1351 LOG(INFO) << "so yeah";
1352 return false;
1353 }
1354
1355 // Check out-edges:
1356 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1357 je = it->out_edges.end(); jt != je; ++jt) {
1358 if (TempBlocksExistInExtents(jt->second.extents) ||
1359 TempBlocksExistInExtents(jt->second.write_extents)) {
1360 LOG(INFO) << "bad out edge in node " << idx;
1361 LOG(INFO) << "so yeah";
1362 return false;
1363 }
1364 }
1365 }
1366 return true;
1367}
1368
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001369bool DeltaDiffGenerator::ReorderDataBlobs(
1370 DeltaArchiveManifest* manifest,
1371 const std::string& data_blobs_path,
1372 const std::string& new_data_blobs_path) {
1373 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1374 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1375 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001376
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001377 DirectFileWriter writer;
1378 TEST_AND_RETURN_FALSE(
1379 writer.Open(new_data_blobs_path.c_str(),
1380 O_WRONLY | O_TRUNC | O_CREAT,
1381 0644) == 0);
1382 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001383 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001384
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001385 for (int i = 0; i < (manifest->install_operations_size() +
1386 manifest->kernel_install_operations_size()); i++) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001387 DeltaArchiveManifest_InstallOperation* op = nullptr;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001388 if (i < manifest->install_operations_size()) {
1389 op = manifest->mutable_install_operations(i);
1390 } else {
1391 op = manifest->mutable_kernel_install_operations(
1392 i - manifest->install_operations_size());
1393 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001394 if (!op->has_data_offset())
1395 continue;
1396 CHECK(op->has_data_length());
1397 vector<char> buf(op->data_length());
1398 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1399 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1400
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001401 // Add the hash of the data blobs for this operation
1402 TEST_AND_RETURN_FALSE(AddOperationHash(op, buf));
1403
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001404 op->set_data_offset(out_file_size);
Don Garrette410e0f2011-11-10 15:39:01 -08001405 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001406 out_file_size += buf.size();
1407 }
1408 return true;
1409}
1410
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001411bool DeltaDiffGenerator::AddOperationHash(
1412 DeltaArchiveManifest_InstallOperation* op,
1413 const vector<char>& buf) {
1414 OmahaHashCalculator hasher;
1415
1416 TEST_AND_RETURN_FALSE(hasher.Update(&buf[0], buf.size()));
1417 TEST_AND_RETURN_FALSE(hasher.Finalize());
1418
1419 const vector<char>& hash = hasher.raw_hash();
1420 op->set_data_sha256_hash(hash.data(), hash.size());
1421 return true;
1422}
1423
Andrew de los Reyesef017552010-10-06 17:57:52 -07001424bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1425 const CutEdgeVertexes& cut,
1426 const string& new_root,
1427 int data_fd,
1428 off_t* data_file_size) {
1429 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001430
Andrew de los Reyesef017552010-10-06 17:57:52 -07001431 // Keep all outgoing edges
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001432 if ((*graph)[cut.old_dst].op.type() !=
1433 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ &&
1434 (*graph)[cut.old_dst].op.type() !=
1435 DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
1436 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1437 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001438
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001439 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1440 cut.old_dst,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001441 nullptr,
Alex Deymo923d8fa2014-07-15 17:58:51 -07001442 kEmptyPath,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001443 new_root,
1444 (*graph)[cut.old_dst].file_name,
Darin Petkov8e447e02013-04-16 16:23:50 +02001445 (*graph)[cut.old_dst].chunk_offset,
1446 (*graph)[cut.old_dst].chunk_size,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001447 data_fd,
1448 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001449
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001450 (*graph)[cut.old_dst].out_edges = out_edges;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001451
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001452 // Right now we don't have doubly-linked edges, so we have to scan
1453 // the whole graph.
1454 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1455 }
Andrew de los Reyesef017552010-10-06 17:57:52 -07001456
1457 // Delete temp node
1458 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1459 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1460 (*graph)[cut.old_dst].out_edges.end());
1461 (*graph)[cut.new_vertex].valid = false;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001462 LOG(INFO) << "marked node invalid: " << cut.new_vertex;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001463 return true;
1464}
1465
1466bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1467 const string& new_root,
1468 int fd,
1469 off_t* data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001470 vector<Vertex::Index>* final_order,
1471 Vertex::Index scratch_vertex) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001472 CycleBreaker cycle_breaker;
1473 LOG(INFO) << "Finding cycles...";
1474 set<Edge> cut_edges;
1475 cycle_breaker.BreakCycles(*graph, &cut_edges);
1476 LOG(INFO) << "done finding cycles";
1477 CheckGraph(*graph);
1478
1479 // Calculate number of scratch blocks needed
1480
1481 LOG(INFO) << "Cutting cycles...";
1482 vector<CutEdgeVertexes> cuts;
1483 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1484 LOG(INFO) << "done cutting cycles";
1485 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1486 CheckGraph(*graph);
1487
1488 LOG(INFO) << "Creating initial topological order...";
1489 TopologicalSort(*graph, final_order);
1490 LOG(INFO) << "done with initial topo order";
1491 CheckGraph(*graph);
1492
1493 LOG(INFO) << "Moving full ops to the back";
1494 MoveFullOpsToBack(graph, final_order);
1495 LOG(INFO) << "done moving full ops to back";
1496
1497 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1498 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1499
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001500 SortCutsByTopoOrder(*final_order, &cuts);
1501
Andrew de los Reyesef017552010-10-06 17:57:52 -07001502 if (!cuts.empty())
1503 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1504 new_root,
1505 fd,
1506 data_file_size,
1507 final_order,
1508 &inverse_final_order,
1509 cuts));
1510 LOG(INFO) << "Making sure all temp blocks have been allocated";
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001511
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001512 // Remove the scratch node, if any
1513 if (scratch_vertex != Vertex::kInvalidIndex) {
1514 final_order->erase(final_order->begin() +
1515 inverse_final_order[scratch_vertex]);
1516 (*graph)[scratch_vertex].valid = false;
1517 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1518 }
1519
Andrew de los Reyesef017552010-10-06 17:57:52 -07001520 graph_utils::DumpGraph(*graph);
1521 CHECK(NoTempBlocksRemain(*graph));
1522 LOG(INFO) << "done making sure all temp blocks are allocated";
1523 return true;
1524}
1525
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001526void DeltaDiffGenerator::CreateScratchNode(uint64_t start_block,
1527 uint64_t num_blocks,
1528 Vertex* vertex) {
1529 vertex->file_name = "<scratch>";
1530 vertex->op.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
1531 vertex->op.set_data_offset(0);
1532 vertex->op.set_data_length(0);
1533 Extent* extent = vertex->op.add_dst_extents();
1534 extent->set_start_block(start_block);
1535 extent->set_num_blocks(num_blocks);
1536}
1537
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001538bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1539 const string& old_root,
1540 const string& old_image,
1541 const string& new_root,
1542 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001543 const string& old_kernel_part,
1544 const string& new_kernel_part,
1545 const string& output_path,
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001546 const string& private_key_path,
Darin Petkov8e447e02013-04-16 16:23:50 +02001547 off_t chunk_size,
Chris Sosad5ae1562013-04-23 13:20:18 -07001548 size_t rootfs_partition_size,
Don Garrett0dd39852013-04-03 16:55:42 -07001549 const ImageInfo* old_image_info,
1550 const ImageInfo* new_image_info,
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001551 uint64_t* metadata_size) {
Darin Petkov8e447e02013-04-16 16:23:50 +02001552 TEST_AND_RETURN_FALSE(chunk_size == -1 || chunk_size % kBlockSize == 0);
Darin Petkov7ea32332010-10-13 10:46:11 -07001553 int old_image_block_count = 0, old_image_block_size = 0;
1554 int new_image_block_count = 0, new_image_block_size = 0;
1555 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1556 &new_image_block_count,
1557 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001558 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001559 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1560 &old_image_block_count,
1561 &old_image_block_size));
1562 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1563 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1564 << "Old and new images have different block counts.";
Don Garrett0dd39852013-04-03 16:55:42 -07001565
Don Garrett60fc59c2013-10-18 11:43:52 -07001566 // If new_image_info is present, old_image_info must be present.
Alex Deymo923d8fa2014-07-15 17:58:51 -07001567 TEST_AND_RETURN_FALSE(!old_image_info == !new_image_info);
Don Garrett0dd39852013-04-03 16:55:42 -07001568 } else {
1569 // old_image_info must not be present for a full update.
1570 TEST_AND_RETURN_FALSE(!old_image_info);
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001571 }
Chris Sosad5ae1562013-04-23 13:20:18 -07001572
1573 // Sanity checks for the partition size.
1574 TEST_AND_RETURN_FALSE(rootfs_partition_size % kBlockSize == 0);
Chris Sosae9f5f422013-05-17 16:11:10 -07001575 size_t fs_size = static_cast<size_t>(new_image_block_size) *
1576 new_image_block_count;
Chris Sosad5ae1562013-04-23 13:20:18 -07001577 LOG(INFO) << "Rootfs partition size: " << rootfs_partition_size;
1578 LOG(INFO) << "Actual filesystem size: " << fs_size;
1579 TEST_AND_RETURN_FALSE(rootfs_partition_size >= fs_size);
1580
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001581 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001582 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1583
Darin Petkov7ea32332010-10-13 10:46:11 -07001584 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1585 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1586 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001587 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1588 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1589 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1590 }
1591 Graph graph;
1592 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001593
Gilad Arnolda6742b32014-01-11 00:18:34 -08001594 const string kTempFileTemplate("CrAU_temp_data.XXXXXX");
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001595 string temp_file_path;
Darin Petkov7438a5c2011-08-29 11:56:44 -07001596 scoped_ptr<ScopedPathUnlinker> temp_file_unlinker;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001597 off_t data_file_size = 0;
1598
1599 LOG(INFO) << "Reading files...";
1600
Don Garrettb8dd1d92013-11-22 17:40:02 -08001601 // Create empty protobuf Manifest object
1602 DeltaArchiveManifest manifest;
1603
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001604 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1605
Andrew de los Reyesef017552010-10-06 17:57:52 -07001606 vector<Vertex::Index> final_order;
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001607 Vertex::Index scratch_vertex = Vertex::kInvalidIndex;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001608 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001609 int fd;
1610 TEST_AND_RETURN_FALSE(
1611 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001612 temp_file_unlinker.reset(new ScopedPathUnlinker(temp_file_path));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001613 TEST_AND_RETURN_FALSE(fd >= 0);
1614 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001615 if (!old_image.empty()) {
1616 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001617
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001618 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1619 &blocks,
1620 old_root,
1621 new_root,
Darin Petkov8e447e02013-04-16 16:23:50 +02001622 chunk_size,
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001623 fd,
1624 &data_file_size));
1625 LOG(INFO) << "done reading normal files";
1626 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001627
Thieu Le5c7d9752010-12-15 16:09:28 -08001628 LOG(INFO) << "Starting metadata processing";
1629 TEST_AND_RETURN_FALSE(Metadata::DeltaReadMetadata(&graph,
1630 &blocks,
1631 old_image,
1632 new_image,
1633 fd,
1634 &data_file_size));
1635 LOG(INFO) << "Done metadata processing";
1636 CheckGraph(graph);
1637
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001638 graph.resize(graph.size() + 1);
1639 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1640 fd,
1641 &data_file_size,
1642 new_image,
1643 &graph.back()));
1644
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001645 // Final scratch block (if there's space)
Chris Sosad5ae1562013-04-23 13:20:18 -07001646 if (blocks.size() < (rootfs_partition_size / kBlockSize)) {
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001647 scratch_vertex = graph.size();
1648 graph.resize(graph.size() + 1);
1649 CreateScratchNode(blocks.size(),
Chris Sosad5ae1562013-04-23 13:20:18 -07001650 (rootfs_partition_size / kBlockSize) - blocks.size(),
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001651 &graph.back());
1652 }
1653
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001654 // Read kernel partition
1655 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1656 new_kernel_part,
1657 &kernel_ops,
1658 fd,
1659 &data_file_size));
1660
1661 LOG(INFO) << "done reading kernel";
1662 CheckGraph(graph);
1663
1664 LOG(INFO) << "Creating edges...";
1665 CreateEdges(&graph, blocks);
1666 LOG(INFO) << "Done creating edges";
1667 CheckGraph(graph);
1668
1669 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1670 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001671 fd,
1672 &data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001673 &final_order,
1674 scratch_vertex));
Don Garrettb8dd1d92013-11-22 17:40:02 -08001675
1676 // Set the minor version for this payload.
1677 LOG(INFO) << "Adding Delta Minor Version.";
1678 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001679 } else {
1680 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001681 off_t new_image_size =
1682 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Darin Petkov7a22d792010-11-08 14:10:00 -08001683 TEST_AND_RETURN_FALSE(FullUpdateGenerator::Run(&graph,
1684 new_kernel_part,
1685 new_image,
1686 new_image_size,
1687 fd,
1688 &data_file_size,
1689 kFullUpdateChunkSize,
1690 kBlockSize,
1691 &kernel_ops,
1692 &final_order));
Don Garrettb8dd1d92013-11-22 17:40:02 -08001693
1694 // Set the minor version for this payload.
1695 LOG(INFO) << "Adding Full Minor Version.";
1696 manifest.set_minor_version(DeltaPerformer::kFullPayloadMinorVersion);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001697 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001698 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001699
Don Garrett0dd39852013-04-03 16:55:42 -07001700 if (old_image_info)
1701 *(manifest.mutable_old_image_info()) = *old_image_info;
1702
1703 if (new_image_info)
1704 *(manifest.mutable_new_image_info()) = *new_image_info;
1705
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001706 OperationNameMap op_name_map;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001707 CheckGraph(graph);
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001708 InstallOperationsToManifest(graph,
1709 final_order,
1710 kernel_ops,
1711 &manifest,
1712 &op_name_map);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001713 CheckGraph(graph);
1714 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001715
1716 // Reorder the data blobs with the newly ordered manifest
1717 string ordered_blobs_path;
1718 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -08001719 "CrAU_temp_data.ordered.XXXXXX",
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001720 &ordered_blobs_path,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001721 nullptr));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001722 ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001723 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1724 temp_file_path,
1725 ordered_blobs_path));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001726 temp_file_unlinker.reset();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001727
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001728 // Check that install op blobs are in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001729 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001730 {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001731 for (int i = 0; i < (manifest.install_operations_size() +
1732 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001733 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001734 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001735 manifest.mutable_install_operations(i) :
1736 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001737 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001738 if (op->has_data_offset()) {
1739 if (op->data_offset() != next_blob_offset) {
1740 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001741 << next_blob_offset;
1742 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001743 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001744 }
1745 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001746 }
1747
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001748 // Signatures appear at the end of the blobs. Note the offset in the
1749 // manifest
1750 if (!private_key_path.empty()) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001751 uint64_t signature_blob_length = 0;
1752 TEST_AND_RETURN_FALSE(
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -07001753 PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path),
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001754 &signature_blob_length));
Darin Petkov9574f7e2011-01-13 10:48:12 -08001755 AddSignatureOp(next_blob_offset, signature_blob_length, &manifest);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001756 }
1757
Darin Petkov36a58222010-10-07 22:00:09 -07001758 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1759 new_kernel_part,
1760 old_image,
1761 new_image,
1762 &manifest));
1763
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001764 // Serialize protobuf
1765 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001766
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001767 CheckGraph(graph);
1768 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1769 CheckGraph(graph);
1770
1771 LOG(INFO) << "Writing final delta file header...";
1772 DirectFileWriter writer;
1773 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1774 O_WRONLY | O_CREAT | O_TRUNC,
1775 0644) == 0);
1776 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001777
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001778 // Write header
Don Garrette410e0f2011-11-10 15:39:01 -08001779 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001780
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001781 // Write version number
1782 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001783
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001784 // Write protobuf length
1785 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1786 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001787
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001788 // Write protobuf
1789 LOG(INFO) << "Writing final delta file protobuf... "
1790 << serialized_manifest.size();
1791 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
Don Garrette410e0f2011-11-10 15:39:01 -08001792 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001793
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001794 // Append the data blobs
1795 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001796 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001797 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1798 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1799 for (;;) {
1800 char buf[kBlockSize];
1801 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1802 if (0 == rc) {
1803 // EOF
1804 break;
1805 }
1806 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
Don Garrette410e0f2011-11-10 15:39:01 -08001807 TEST_AND_RETURN_FALSE(writer.Write(buf, rc));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001808 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001809
1810 // Write signature blob.
1811 if (!private_key_path.empty()) {
1812 LOG(INFO) << "Signing the update...";
1813 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -07001814 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
1815 output_path,
1816 vector<string>(1, private_key_path),
1817 &signature_blob));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001818 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
Don Garrette410e0f2011-11-10 15:39:01 -08001819 signature_blob.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001820 }
1821
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001822 *metadata_size =
Darin Petkov95cf01f2010-10-12 14:59:13 -07001823 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001824 ReportPayloadUsage(manifest, *metadata_size, op_name_map);
Darin Petkov880335c2010-10-01 15:52:53 -07001825
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001826 LOG(INFO) << "All done. Successfully created delta file with "
1827 << "metadata size = " << *metadata_size;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001828 return true;
1829}
1830
Thieu Le5c7d9752010-12-15 16:09:28 -08001831// Runs the bsdiff tool on two files and returns the resulting delta in
1832// 'out'. Returns true on success.
1833bool DeltaDiffGenerator::BsdiffFiles(const string& old_file,
1834 const string& new_file,
1835 vector<char>* out) {
Gilad Arnolda6742b32014-01-11 00:18:34 -08001836 const string kPatchFile = "delta.patchXXXXXX";
Thieu Le5c7d9752010-12-15 16:09:28 -08001837 string patch_file_path;
1838
1839 TEST_AND_RETURN_FALSE(
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001840 utils::MakeTempFile(kPatchFile, &patch_file_path, nullptr));
Thieu Le5c7d9752010-12-15 16:09:28 -08001841
1842 vector<string> cmd;
1843 cmd.push_back(kBsdiffPath);
1844 cmd.push_back(old_file);
1845 cmd.push_back(new_file);
1846 cmd.push_back(patch_file_path);
1847
1848 int rc = 1;
1849 vector<char> patch_file;
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001850 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc, nullptr));
Thieu Le5c7d9752010-12-15 16:09:28 -08001851 TEST_AND_RETURN_FALSE(rc == 0);
1852 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
1853 unlink(patch_file_path.c_str());
1854 return true;
1855}
1856
1857// The |blocks| vector contains a reader and writer for each block on the
1858// filesystem that's being in-place updated. We populate the reader/writer
1859// fields of |blocks| by calling this function.
1860// For each block in |operation| that is read or written, find that block
1861// in |blocks| and set the reader/writer field to the vertex passed.
1862// |graph| is not strictly necessary, but useful for printing out
1863// error messages.
1864bool DeltaDiffGenerator::AddInstallOpToBlocksVector(
1865 const DeltaArchiveManifest_InstallOperation& operation,
1866 const Graph& graph,
1867 Vertex::Index vertex,
1868 vector<Block>* blocks) {
1869 // See if this is already present.
1870 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
1871
1872 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
1873 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
1874 const int extents_size =
1875 (field == READER) ? operation.src_extents_size() :
1876 operation.dst_extents_size();
1877 const char* past_participle = (field == READER) ? "read" : "written";
1878 const google::protobuf::RepeatedPtrField<Extent>& extents =
1879 (field == READER) ? operation.src_extents() : operation.dst_extents();
1880 Vertex::Index Block::*access_type =
1881 (field == READER) ? &Block::reader : &Block::writer;
1882
1883 for (int i = 0; i < extents_size; i++) {
1884 const Extent& extent = extents.Get(i);
1885 if (extent.start_block() == kSparseHole) {
1886 // Hole in sparse file. skip
1887 continue;
1888 }
1889 for (uint64_t block = extent.start_block();
1890 block < (extent.start_block() + extent.num_blocks()); block++) {
1891 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
1892 LOG(FATAL) << "Block " << block << " is already "
1893 << past_participle << " by "
1894 << (*blocks)[block].*access_type << "("
1895 << graph[(*blocks)[block].*access_type].file_name
1896 << ") and also " << vertex << "("
1897 << graph[vertex].file_name << ")";
1898 }
1899 (*blocks)[block].*access_type = vertex;
1900 }
1901 }
1902 }
1903 return true;
1904}
1905
Darin Petkov9574f7e2011-01-13 10:48:12 -08001906void DeltaDiffGenerator::AddSignatureOp(uint64_t signature_blob_offset,
1907 uint64_t signature_blob_length,
1908 DeltaArchiveManifest* manifest) {
1909 LOG(INFO) << "Making room for signature in file";
1910 manifest->set_signatures_offset(signature_blob_offset);
1911 LOG(INFO) << "set? " << manifest->has_signatures_offset();
1912 // Add a dummy op at the end to appease older clients
1913 DeltaArchiveManifest_InstallOperation* dummy_op =
1914 manifest->add_kernel_install_operations();
1915 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1916 dummy_op->set_data_offset(signature_blob_offset);
1917 manifest->set_signatures_offset(signature_blob_offset);
1918 dummy_op->set_data_length(signature_blob_length);
1919 manifest->set_signatures_size(signature_blob_length);
1920 Extent* dummy_extent = dummy_op->add_dst_extents();
1921 // Tell the dummy op to write this data to a big sparse hole
1922 dummy_extent->set_start_block(kSparseHole);
1923 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1924 kBlockSize);
1925}
1926
Andrew de los Reyes50f36492010-11-01 13:57:12 -07001927const char* const kBsdiffPath = "bsdiff";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001928
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001929}; // namespace chromeos_update_engine