blob: ef7fdeee406a3e3d623f74380fc74652a50f5ecd [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
5#include "update_engine/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
Darin Petkov8e447e02013-04-16 16:23:50 +020020#include <base/file_path.h>
21#include <base/file_util.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>
Darin Petkov8e447e02013-04-16 16:23:50 +020024#include <base/string_number_conversions.h>
Darin Petkov880335c2010-10-01 15:52:53 -070025#include <base/string_util.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040026#include <base/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"
30#include "update_engine/cycle_breaker.h"
Don Garrettb8dd1d92013-11-22 17:40:02 -080031#include "update_engine/delta_performer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070032#include "update_engine/extent_mapper.h"
Andrew de los Reyesef017552010-10-06 17:57:52 -070033#include "update_engine/extent_ranges.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070034#include "update_engine/file_writer.h"
35#include "update_engine/filesystem_iterator.h"
Darin Petkov7a22d792010-11-08 14:10:00 -080036#include "update_engine/full_update_generator.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070037#include "update_engine/graph_types.h"
38#include "update_engine/graph_utils.h"
Thieu Le5c7d9752010-12-15 16:09:28 -080039#include "update_engine/metadata.h"
Darin Petkov36a58222010-10-07 22:00:09 -070040#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070041#include "update_engine/payload_signer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070042#include "update_engine/subprocess.h"
43#include "update_engine/topological_sort.h"
44#include "update_engine/update_metadata.pb.h"
45#include "update_engine/utils.h"
46
47using std::make_pair;
Andrew de los Reyesef017552010-10-06 17:57:52 -070048using std::map;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070049using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070050using std::min;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -070051using std::pair;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070052using std::set;
53using std::string;
54using std::vector;
55
56namespace chromeos_update_engine {
57
58typedef DeltaDiffGenerator::Block Block;
Darin Petkov9fa7ec52010-10-18 11:45:23 -070059typedef map<const DeltaArchiveManifest_InstallOperation*,
Darin Petkov8e447e02013-04-16 16:23:50 +020060 string> OperationNameMap;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070061
Chris Sosaf586b012013-05-21 13:33:42 -070062// bytes
63const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024;
Chris Sosad5ae1562013-04-23 13:20:18 -070064const uint64_t kVersionNumber = 1;
65const uint64_t kFullUpdateChunkSize = 1024 * 1024; // bytes
66
Gilad Arnoldfa404502014-01-01 23:36:12 -080067// Needed for testing purposes, in case we can't use actual filesystem objects.
68// TODO(garnold)(chromium:331965) Replace this hack with a properly injected
69// parameter in form of a mockable abstract class.
70bool (*get_extents_with_chunk_func)(const std::string&, off_t, off_t,
71 std::vector<Extent>*) =
72 extent_mapper::ExtentsForFileChunkFibmap;
73
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070074namespace {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070075const size_t kBlockSize = 4096; // bytes
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -080076const string kNonexistentPath = "";
Andrew de los Reyes927179d2010-12-02 11:26:48 -080077
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070078
Darin Petkov68c10d12010-10-14 09:24:37 -070079static const char* kInstallOperationTypes[] = {
80 "REPLACE",
81 "REPLACE_BZ",
82 "MOVE",
83 "BSDIFF"
84};
85
Gilad Arnoldfa404502014-01-01 23:36:12 -080086// Stores all the extents of |path| into |extents|. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070087bool GatherExtents(const string& path,
Darin Petkov8e447e02013-04-16 16:23:50 +020088 off_t chunk_offset,
89 off_t chunk_size,
Gilad Arnoldfa404502014-01-01 23:36:12 -080090 vector<Extent>* extents) {
91 extents->clear();
Darin Petkov8e447e02013-04-16 16:23:50 +020092 TEST_AND_RETURN_FALSE(
Gilad Arnoldfa404502014-01-01 23:36:12 -080093 get_extents_with_chunk_func(
94 path, chunk_offset, chunk_size, extents));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070095 return true;
96}
97
Andrew de los Reyesef017552010-10-06 17:57:52 -070098// For a given regular file which must exist at new_root + path, and
99// may exist at old_root + path, creates a new InstallOperation and
100// adds it to the graph. Also, populates the |blocks| array as
101// necessary, if |blocks| is non-NULL. Also, writes the data
102// necessary to send the file down to the client into data_fd, which
103// has length *data_file_size. *data_file_size is updated
104// appropriately. If |existing_vertex| is no kInvalidIndex, use that
105// rather than allocating a new vertex. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700106bool DeltaReadFile(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700107 Vertex::Index existing_vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700108 vector<Block>* blocks,
109 const string& old_root,
110 const string& new_root,
111 const string& path, // within new_root
Darin Petkov8e447e02013-04-16 16:23:50 +0200112 off_t chunk_offset,
113 off_t chunk_size,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700114 int data_fd,
115 off_t* data_file_size) {
116 vector<char> data;
117 DeltaArchiveManifest_InstallOperation operation;
118
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800119 string old_path = (old_root == kNonexistentPath) ? kNonexistentPath :
120 old_root + path;
121
Don Garrett1d787092013-03-11 18:07:28 -0700122 // If bsdiff breaks again, blacklist the problem file by using:
123 // bsdiff_allowed = (path != "/foo/bar")
Don Garrett36e60772012-03-29 10:31:20 -0700124 //
Don Garrett1d787092013-03-11 18:07:28 -0700125 // TODO(dgarrett): chromium-os:15274 connect this test to the command line.
Don Garrett36e60772012-03-29 10:31:20 -0700126 bool bsdiff_allowed = true;
Don Garrettf4b28742012-03-27 20:48:06 -0700127
Don Garrett36e60772012-03-29 10:31:20 -0700128 if (!bsdiff_allowed)
129 LOG(INFO) << "bsdiff blacklisting: " << path;
Don Garrettf4b28742012-03-27 20:48:06 -0700130
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800131 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_path,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700132 new_root + path,
Darin Petkov8e447e02013-04-16 16:23:50 +0200133 chunk_offset,
134 chunk_size,
Don Garrett36e60772012-03-29 10:31:20 -0700135 bsdiff_allowed,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700136 &data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700137 &operation,
138 true));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700139
Gilad Arnoldfa404502014-01-01 23:36:12 -0800140 // Check if the operation writes nothing.
141 if (operation.dst_extents_size() == 0) {
142 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
143 LOG(INFO) << "Empty MOVE operation (" << new_root + path << "), skipping";
144 return true;
145 } else {
146 LOG(ERROR) << "Empty non-MOVE operation";
147 return false;
148 }
149 }
150
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700151 // Write the data
152 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
153 operation.set_data_offset(*data_file_size);
154 operation.set_data_length(data.size());
155 }
156
157 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
158 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700159
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700160 // Now, insert into graph and blocks vector
Andrew de los Reyesef017552010-10-06 17:57:52 -0700161 Vertex::Index vertex = existing_vertex;
162 if (vertex == Vertex::kInvalidIndex) {
163 graph->resize(graph->size() + 1);
164 vertex = graph->size() - 1;
165 }
166 (*graph)[vertex].op = operation;
167 CHECK((*graph)[vertex].op.has_type());
168 (*graph)[vertex].file_name = path;
Darin Petkov8e447e02013-04-16 16:23:50 +0200169 (*graph)[vertex].chunk_offset = chunk_offset;
170 (*graph)[vertex].chunk_size = chunk_size;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700171
Andrew de los Reyesef017552010-10-06 17:57:52 -0700172 if (blocks)
Thieu Le5c7d9752010-12-15 16:09:28 -0800173 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::AddInstallOpToBlocksVector(
174 (*graph)[vertex].op,
175 *graph,
176 vertex,
177 blocks));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700178 return true;
179}
180
181// For each regular file within new_root, creates a node in the graph,
182// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
183// and writes any necessary data to the end of data_fd.
184bool DeltaReadFiles(Graph* graph,
185 vector<Block>* blocks,
186 const string& old_root,
187 const string& new_root,
Darin Petkov8e447e02013-04-16 16:23:50 +0200188 off_t chunk_size,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700189 int data_fd,
190 off_t* data_file_size) {
191 set<ino_t> visited_inodes;
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800192 set<ino_t> visited_src_inodes;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700193 for (FilesystemIterator fs_iter(new_root,
194 utils::SetWithValue<string>("/lost+found"));
195 !fs_iter.IsEnd(); fs_iter.Increment()) {
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800196 // We never diff symlinks (here, we check that dst file is not a symlink).
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700197 if (!S_ISREG(fs_iter.GetStat().st_mode))
198 continue;
199
200 // Make sure we visit each inode only once.
201 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
202 continue;
203 visited_inodes.insert(fs_iter.GetStat().st_ino);
Darin Petkov8e447e02013-04-16 16:23:50 +0200204 off_t dst_size = fs_iter.GetStat().st_size;
205 if (dst_size == 0)
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700206 continue;
207
208 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700209
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800210 // We can't visit each dst image inode more than once, as that would
211 // duplicate work. Here, we avoid visiting each source image inode
212 // more than once. Technically, we could have multiple operations
213 // that read the same blocks from the source image for diffing, but
214 // we choose not to to avoid complexity. Eventually we will move away
215 // from using a graph/cycle detection/etc to generate diffs, and at that
216 // time, it will be easy (non-complex) to have many operations read
217 // from the same source blocks. At that time, this code can die. -adlr
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800218 bool should_diff_from_source = false;
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800219 string src_path = old_root + fs_iter.GetPartialPath();
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800220 struct stat src_stbuf;
221 // We never diff symlinks (here, we check that src file is not a symlink).
222 if (0 == lstat(src_path.c_str(), &src_stbuf) &&
223 S_ISREG(src_stbuf.st_mode)) {
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800224 should_diff_from_source = !utils::SetContainsKey(visited_src_inodes,
225 src_stbuf.st_ino);
226 visited_src_inodes.insert(src_stbuf.st_ino);
227 }
228
Darin Petkov8e447e02013-04-16 16:23:50 +0200229 off_t size = chunk_size == -1 ? dst_size : chunk_size;
230 off_t step = size;
231 for (off_t offset = 0; offset < dst_size; offset += step) {
232 if (offset + size >= dst_size) {
233 size = -1; // Read through the end of the file.
234 }
235 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
236 Vertex::kInvalidIndex,
237 blocks,
238 (should_diff_from_source ?
239 old_root :
240 kNonexistentPath),
241 new_root,
242 fs_iter.GetPartialPath(),
243 offset,
244 size,
245 data_fd,
246 data_file_size));
247 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700248 }
249 return true;
250}
251
Andrew de los Reyesef017552010-10-06 17:57:52 -0700252// This class allocates non-existent temp blocks, starting from
253// kTempBlockStart. Other code is responsible for converting these
254// temp blocks into real blocks, as the client can't read or write to
255// these blocks.
256class DummyExtentAllocator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700257 public:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700258 explicit DummyExtentAllocator()
259 : next_block_(kTempBlockStart) {}
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700260 vector<Extent> Allocate(const uint64_t block_count) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700261 vector<Extent> ret(1);
262 ret[0].set_start_block(next_block_);
263 ret[0].set_num_blocks(block_count);
264 next_block_ += block_count;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700265 return ret;
266 }
267 private:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700268 uint64_t next_block_;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700269};
270
271// Reads blocks from image_path that are not yet marked as being written
272// in the blocks array. These blocks that remain are non-file-data blocks.
273// In the future we might consider intelligent diffing between this data
274// and data in the previous image, but for now we just bzip2 compress it
275// and include it in the update.
276// Creates a new node in the graph to write these blocks and writes the
277// appropriate blob to blobs_fd. Reads and updates blobs_length;
278bool ReadUnwrittenBlocks(const vector<Block>& blocks,
279 int blobs_fd,
280 off_t* blobs_length,
281 const string& image_path,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700282 Vertex* vertex) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700283 vertex->file_name = "<rootfs-non-file-data>";
284
Andrew de los Reyesef017552010-10-06 17:57:52 -0700285 DeltaArchiveManifest_InstallOperation* out_op = &vertex->op;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700286 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
287 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
288 ScopedFdCloser image_fd_closer(&image_fd);
289
290 string temp_file_path;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800291 TEST_AND_RETURN_FALSE(utils::MakeTempFile("CrAU_temp_data.XXXXXX",
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700292 &temp_file_path,
293 NULL));
294
295 FILE* file = fopen(temp_file_path.c_str(), "w");
296 TEST_AND_RETURN_FALSE(file);
297 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700298
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700299 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
300 file,
301 9, // max compression
302 0, // verbosity
303 0); // default work factor
304 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700305
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700306 vector<Extent> extents;
307 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700308
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700309 LOG(INFO) << "Appending left over blocks to extents";
310 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
311 if (blocks[i].writer != Vertex::kInvalidIndex)
312 continue;
Andrew de los Reyesef017552010-10-06 17:57:52 -0700313 if (blocks[i].reader != Vertex::kInvalidIndex) {
314 graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i);
315 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700316 graph_utils::AppendBlockToExtents(&extents, i);
317 block_count++;
318 }
319
320 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
321 // so we arbitrarily set it to 1024 * kBlockSize.
322 vector<char> buf(1024 * kBlockSize);
323
324 LOG(INFO) << "Reading left over blocks";
325 vector<Block>::size_type blocks_copied_count = 0;
326
327 // For each extent in extents, write the data into BZ2_bzWrite which
328 // sends it to an output file.
329 // We use the temporary buffer 'buf' to hold the data, which may be
330 // smaller than the extent, so in that case we have to loop to get
331 // the extent's data (that's the inner while loop).
332 for (vector<Extent>::const_iterator it = extents.begin();
333 it != extents.end(); ++it) {
334 vector<Block>::size_type blocks_read = 0;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800335 float printed_progress = -1;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700336 while (blocks_read < it->num_blocks()) {
337 const int copy_block_cnt =
338 min(buf.size() / kBlockSize,
339 static_cast<vector<char>::size_type>(
340 it->num_blocks() - blocks_read));
341 ssize_t rc = pread(image_fd,
342 &buf[0],
343 copy_block_cnt * kBlockSize,
344 (it->start_block() + blocks_read) * kBlockSize);
345 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
346 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
347 copy_block_cnt * kBlockSize);
348 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
349 TEST_AND_RETURN_FALSE(err == BZ_OK);
350 blocks_read += copy_block_cnt;
351 blocks_copied_count += copy_block_cnt;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800352 float current_progress =
353 static_cast<float>(blocks_copied_count) / block_count;
354 if (printed_progress + 0.1 < current_progress ||
355 blocks_copied_count == block_count) {
356 LOG(INFO) << "progress: " << current_progress;
357 printed_progress = current_progress;
358 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700359 }
360 }
361 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
362 TEST_AND_RETURN_FALSE(err == BZ_OK);
363 bz_file = NULL;
364 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
365 file = NULL;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700366
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700367 vector<char> compressed_data;
368 LOG(INFO) << "Reading compressed data off disk";
369 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
370 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700371
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700372 // Add node to graph to write these blocks
373 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
374 out_op->set_data_offset(*blobs_length);
375 out_op->set_data_length(compressed_data.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700376 LOG(INFO) << "Rootfs non-data blocks compressed take up "
377 << compressed_data.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700378 *blobs_length += compressed_data.size();
379 out_op->set_dst_length(kBlockSize * block_count);
380 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700381
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700382 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
383 &compressed_data[0],
384 compressed_data.size()));
385 LOG(INFO) << "done with extra blocks";
386 return true;
387}
388
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700389// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700390// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700391bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
392 uint64_t value_be = htobe64(value);
Don Garrette410e0f2011-11-10 15:39:01 -0800393 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700394 return true;
395}
396
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700397// Adds each operation from |graph| to |out_manifest| in the order specified by
398// |order| while building |out_op_name_map| with operation to name
399// mappings. Adds all |kernel_ops| to |out_manifest|. Filters out no-op
400// operations.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700401void InstallOperationsToManifest(
402 const Graph& graph,
403 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700404 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700405 DeltaArchiveManifest* out_manifest,
406 OperationNameMap* out_op_name_map) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700407 for (vector<Vertex::Index>::const_iterator it = order.begin();
408 it != order.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700409 const Vertex& vertex = graph[*it];
410 const DeltaArchiveManifest_InstallOperation& add_op = vertex.op;
411 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
412 continue;
413 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700414 DeltaArchiveManifest_InstallOperation* op =
415 out_manifest->add_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700416 *op = add_op;
Darin Petkov8e447e02013-04-16 16:23:50 +0200417 string name = vertex.file_name;
418 if (vertex.chunk_offset || vertex.chunk_size != -1) {
419 string offset = base::Int64ToString(vertex.chunk_offset);
420 if (vertex.chunk_size != -1) {
421 name += " [" + offset + ", " +
422 base::Int64ToString(vertex.chunk_offset + vertex.chunk_size - 1) +
423 "]";
424 } else {
425 name += " [" + offset + ", end]";
426 }
427 }
428 (*out_op_name_map)[op] = name;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700429 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700430 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
431 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700432 const DeltaArchiveManifest_InstallOperation& add_op = *it;
433 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
434 continue;
435 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700436 DeltaArchiveManifest_InstallOperation* op =
437 out_manifest->add_kernel_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700438 *op = add_op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700439 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700440}
441
442void CheckGraph(const Graph& graph) {
443 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
444 CHECK(it->op.has_type());
445 }
446}
447
Darin Petkov68c10d12010-10-14 09:24:37 -0700448// Delta compresses a kernel partition |new_kernel_part| with knowledge of the
449// old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty
450// string, generates a full update of the partition.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700451bool DeltaCompressKernelPartition(
452 const string& old_kernel_part,
453 const string& new_kernel_part,
454 vector<DeltaArchiveManifest_InstallOperation>* ops,
455 int blobs_fd,
456 off_t* blobs_length) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700457 LOG(INFO) << "Delta compressing kernel partition...";
Darin Petkov68c10d12010-10-14 09:24:37 -0700458 LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update...";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700459
Gilad Arnoldfa404502014-01-01 23:36:12 -0800460 DeltaArchiveManifest_InstallOperation op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700461 vector<char> data;
Don Garrett36e60772012-03-29 10:31:20 -0700462 TEST_AND_RETURN_FALSE(
463 DeltaDiffGenerator::ReadFileToDiff(old_kernel_part,
464 new_kernel_part,
Darin Petkov8e447e02013-04-16 16:23:50 +0200465 0, // chunk_offset
466 -1, // chunk_size
Don Garrett36e60772012-03-29 10:31:20 -0700467 true, // bsdiff_allowed
468 &data,
Gilad Arnoldfa404502014-01-01 23:36:12 -0800469 &op,
Don Garrett36e60772012-03-29 10:31:20 -0700470 false));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700471
Gilad Arnoldfa404502014-01-01 23:36:12 -0800472 // Check if the operation writes nothing.
473 if (op.dst_extents_size() == 0) {
474 if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
475 LOG(INFO) << "Empty MOVE operation, nothing to do.";
476 return true;
477 } else {
478 LOG(ERROR) << "Empty non-MOVE operation";
479 return false;
480 }
Darin Petkov68c10d12010-10-14 09:24:37 -0700481 }
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700482
Gilad Arnoldfa404502014-01-01 23:36:12 -0800483 // Write the data.
484 if (op.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
485 op.set_data_offset(*blobs_length);
486 op.set_data_length(data.size());
487 }
488
489 // Add the new install operation.
490 ops->clear();
491 ops->push_back(op);
492
Darin Petkov68c10d12010-10-14 09:24:37 -0700493 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
494 *blobs_length += data.size();
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700495
Darin Petkov68c10d12010-10-14 09:24:37 -0700496 LOG(INFO) << "Done delta compressing kernel partition: "
Gilad Arnoldfa404502014-01-01 23:36:12 -0800497 << kInstallOperationTypes[op.type()];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700498 return true;
499}
500
Darin Petkov880335c2010-10-01 15:52:53 -0700501struct DeltaObject {
502 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
503 : name(in_name),
504 type(in_type),
505 size(in_size) {}
506 bool operator <(const DeltaObject& object) const {
Darin Petkovd43d6902010-10-14 11:17:50 -0700507 return (size != object.size) ? (size < object.size) : (name < object.name);
Darin Petkov880335c2010-10-01 15:52:53 -0700508 }
509 string name;
510 int type;
511 off_t size;
512};
513
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700514void ReportPayloadUsage(const DeltaArchiveManifest& manifest,
515 const int64_t manifest_metadata_size,
516 const OperationNameMap& op_name_map) {
Darin Petkov880335c2010-10-01 15:52:53 -0700517 vector<DeltaObject> objects;
518 off_t total_size = 0;
519
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700520 // Rootfs install operations.
521 for (int i = 0; i < manifest.install_operations_size(); ++i) {
522 const DeltaArchiveManifest_InstallOperation& op =
523 manifest.install_operations(i);
Darin Petkov8e447e02013-04-16 16:23:50 +0200524 objects.push_back(DeltaObject(op_name_map.find(&op)->second,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700525 op.type(),
526 op.data_length()));
527 total_size += op.data_length();
Darin Petkov880335c2010-10-01 15:52:53 -0700528 }
529
Darin Petkov880335c2010-10-01 15:52:53 -0700530 // Kernel install operations.
531 for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) {
532 const DeltaArchiveManifest_InstallOperation& op =
533 manifest.kernel_install_operations(i);
534 objects.push_back(DeltaObject(StringPrintf("<kernel-operation-%d>", i),
535 op.type(),
536 op.data_length()));
537 total_size += op.data_length();
538 }
539
Darin Petkov95cf01f2010-10-12 14:59:13 -0700540 objects.push_back(DeltaObject("<manifest-metadata>",
541 -1,
542 manifest_metadata_size));
543 total_size += manifest_metadata_size;
544
Darin Petkov880335c2010-10-01 15:52:53 -0700545 std::sort(objects.begin(), objects.end());
546
547 static const char kFormatString[] = "%6.2f%% %10llu %-10s %s\n";
548 for (vector<DeltaObject>::const_iterator it = objects.begin();
549 it != objects.end(); ++it) {
550 const DeltaObject& object = *it;
551 fprintf(stderr, kFormatString,
552 object.size * 100.0 / total_size,
553 object.size,
Darin Petkov95cf01f2010-10-12 14:59:13 -0700554 object.type >= 0 ? kInstallOperationTypes[object.type] : "-",
Darin Petkov880335c2010-10-01 15:52:53 -0700555 object.name.c_str());
556 }
557 fprintf(stderr, kFormatString, 100.0, total_size, "", "<total>");
558}
559
Gilad Arnoldfa404502014-01-01 23:36:12 -0800560// Process a range of blocks from |range_start| to |range_end| in the extent at
561// position |*idx_p| of |extents|. If |do_remove| is true, this range will be
562// removed, which may cause the extent to be trimmed, split or removed entirely.
563// The value of |*idx_p| is updated to point to the next extent to be processed.
564// Returns true iff the next extent to process is a new or updated one.
565bool ProcessExtentBlockRange(vector<Extent>* extents, size_t* idx_p,
566 const bool do_remove, uint64_t range_start,
567 uint64_t range_end) {
568 size_t idx = *idx_p;
569 uint64_t start_block = (*extents)[idx].start_block();
570 uint64_t num_blocks = (*extents)[idx].num_blocks();
571 uint64_t range_size = range_end - range_start;
572
573 if (do_remove) {
574 if (range_size == num_blocks) {
575 // Remove the entire extent.
576 extents->erase(extents->begin() + idx);
577 } else if (range_end == num_blocks) {
578 // Trim the end of the extent.
579 (*extents)[idx].set_num_blocks(num_blocks - range_size);
580 idx++;
581 } else if (range_start == 0) {
582 // Trim the head of the extent.
583 (*extents)[idx].set_start_block(start_block + range_size);
584 (*extents)[idx].set_num_blocks(num_blocks - range_size);
585 } else {
586 // Trim the middle, splitting the remainder into two parts.
587 (*extents)[idx].set_num_blocks(range_start);
588 Extent e;
589 e.set_start_block(start_block + range_end);
590 e.set_num_blocks(num_blocks - range_end);
591 idx++;
592 extents->insert(extents->begin() + idx, e);
593 }
594 } else if (range_end == num_blocks) {
595 // Done with this extent.
596 idx++;
597 } else {
598 return false;
599 }
600
601 *idx_p = idx;
602 return true;
603}
604
605// Remove identical corresponding block ranges in |src_extents| and
606// |dst_extents|. Used for preventing moving of blocks onto themselves during
Gilad Arnoldebca5712014-01-10 14:26:37 -0800607// MOVE operations. The value of |total_bytes| indicates the actual length of
608// content; this may be slightly less than the total size of blocks, in which
609// case the last block is only partly occupied with data. Returns the total
610// number of bytes removed.
611size_t RemoveIdenticalBlockRanges(vector<Extent>* src_extents,
612 vector<Extent>* dst_extents,
613 const size_t total_bytes) {
Gilad Arnoldfa404502014-01-01 23:36:12 -0800614 size_t src_idx = 0;
615 size_t dst_idx = 0;
616 uint64_t src_offset = 0, dst_offset = 0;
617 bool new_src = true, new_dst = true;
Gilad Arnoldebca5712014-01-10 14:26:37 -0800618 size_t removed_bytes = 0, nonfull_block_bytes;
619 bool do_remove = false;
Gilad Arnoldfa404502014-01-01 23:36:12 -0800620 while (src_idx < src_extents->size() && dst_idx < dst_extents->size()) {
621 if (new_src) {
622 src_offset = 0;
623 new_src = false;
624 }
625 if (new_dst) {
626 dst_offset = 0;
627 new_dst = false;
628 }
629
Gilad Arnoldebca5712014-01-10 14:26:37 -0800630 do_remove = ((*src_extents)[src_idx].start_block() + src_offset ==
631 (*dst_extents)[dst_idx].start_block() + dst_offset);
Gilad Arnoldfa404502014-01-01 23:36:12 -0800632
633 uint64_t src_num_blocks = (*src_extents)[src_idx].num_blocks();
634 uint64_t dst_num_blocks = (*dst_extents)[dst_idx].num_blocks();
635 uint64_t min_num_blocks = min(src_num_blocks - src_offset,
636 dst_num_blocks - dst_offset);
637 uint64_t prev_src_offset = src_offset;
638 uint64_t prev_dst_offset = dst_offset;
639 src_offset += min_num_blocks;
640 dst_offset += min_num_blocks;
641
642 new_src = ProcessExtentBlockRange(src_extents, &src_idx, do_remove,
643 prev_src_offset, src_offset);
644 new_dst = ProcessExtentBlockRange(dst_extents, &dst_idx, do_remove,
645 prev_dst_offset, dst_offset);
Gilad Arnoldebca5712014-01-10 14:26:37 -0800646 if (do_remove)
647 removed_bytes += min_num_blocks * kBlockSize;
Gilad Arnoldfa404502014-01-01 23:36:12 -0800648 }
Gilad Arnoldebca5712014-01-10 14:26:37 -0800649
650 // If we removed the last block and this block is only partly used by file
651 // content, deduct the unused portion from the total removed byte count.
652 if (do_remove && (nonfull_block_bytes = total_bytes % kBlockSize))
653 removed_bytes -= kBlockSize - nonfull_block_bytes;
654
655 return removed_bytes;
Gilad Arnoldfa404502014-01-01 23:36:12 -0800656}
657
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700658} // namespace {}
659
660bool DeltaDiffGenerator::ReadFileToDiff(
661 const string& old_filename,
662 const string& new_filename,
Darin Petkov8e447e02013-04-16 16:23:50 +0200663 off_t chunk_offset,
664 off_t chunk_size,
Don Garrett36e60772012-03-29 10:31:20 -0700665 bool bsdiff_allowed,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700666 vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700667 DeltaArchiveManifest_InstallOperation* out_op,
668 bool gather_extents) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700669 // Read new data in
670 vector<char> new_data;
Darin Petkov8e447e02013-04-16 16:23:50 +0200671 TEST_AND_RETURN_FALSE(
672 utils::ReadFileChunk(new_filename, chunk_offset, chunk_size, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700673
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700674 TEST_AND_RETURN_FALSE(!new_data.empty());
Darin Petkov8e447e02013-04-16 16:23:50 +0200675 TEST_AND_RETURN_FALSE(chunk_size == -1 ||
676 static_cast<off_t>(new_data.size()) <= chunk_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700677
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700678 vector<char> new_data_bz;
679 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
680 CHECK(!new_data_bz.empty());
681
682 vector<char> data; // Data blob that will be written to delta file.
683
684 DeltaArchiveManifest_InstallOperation operation;
685 size_t current_best_size = 0;
686 if (new_data.size() <= new_data_bz.size()) {
687 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
688 current_best_size = new_data.size();
689 data = new_data;
690 } else {
691 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
692 current_best_size = new_data_bz.size();
693 data = new_data_bz;
694 }
695
696 // Do we have an original file to consider?
697 struct stat old_stbuf;
Don Garrettf4b28742012-03-27 20:48:06 -0700698 bool original = !old_filename.empty();
699 if (original && 0 != stat(old_filename.c_str(), &old_stbuf)) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700700 // If stat-ing the old file fails, it should be because it doesn't exist.
701 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
Don Garrettf4b28742012-03-27 20:48:06 -0700702 original = false;
Darin Petkov68c10d12010-10-14 09:24:37 -0700703 }
Don Garrettf4b28742012-03-27 20:48:06 -0700704
Darin Petkov8e447e02013-04-16 16:23:50 +0200705 vector<char> old_data;
Don Garrettf4b28742012-03-27 20:48:06 -0700706 if (original) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700707 // Read old data
Darin Petkov8e447e02013-04-16 16:23:50 +0200708 TEST_AND_RETURN_FALSE(
709 utils::ReadFileChunk(
710 old_filename, chunk_offset, chunk_size, &old_data));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700711 if (old_data == new_data) {
712 // No change in data.
713 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
714 current_best_size = 0;
715 data.clear();
Darin Petkov8e447e02013-04-16 16:23:50 +0200716 } else if (!old_data.empty() && bsdiff_allowed) {
Don Garrett36e60772012-03-29 10:31:20 -0700717 // If the source file is considered bsdiff safe (no bsdiff bugs
718 // triggered), see if BSDIFF encoding is smaller.
Darin Petkov8e447e02013-04-16 16:23:50 +0200719 FilePath old_chunk;
720 TEST_AND_RETURN_FALSE(file_util::CreateTemporaryFile(&old_chunk));
721 ScopedPathUnlinker old_unlinker(old_chunk.value());
722 TEST_AND_RETURN_FALSE(
723 utils::WriteFile(old_chunk.value().c_str(),
724 &old_data[0], old_data.size()));
725 FilePath new_chunk;
726 TEST_AND_RETURN_FALSE(file_util::CreateTemporaryFile(&new_chunk));
727 ScopedPathUnlinker new_unlinker(new_chunk.value());
728 TEST_AND_RETURN_FALSE(
729 utils::WriteFile(new_chunk.value().c_str(),
730 &new_data[0], new_data.size()));
731
Don Garrett36e60772012-03-29 10:31:20 -0700732 vector<char> bsdiff_delta;
733 TEST_AND_RETURN_FALSE(
Darin Petkov8e447e02013-04-16 16:23:50 +0200734 BsdiffFiles(old_chunk.value(), new_chunk.value(), &bsdiff_delta));
Don Garrett36e60772012-03-29 10:31:20 -0700735 CHECK_GT(bsdiff_delta.size(), static_cast<vector<char>::size_type>(0));
736 if (bsdiff_delta.size() < current_best_size) {
737 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
738 current_best_size = bsdiff_delta.size();
739 data = bsdiff_delta;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700740 }
741 }
742 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700743
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700744 // Set parameters of the operations
745 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700746
Gilad Arnoldfa404502014-01-01 23:36:12 -0800747 vector<Extent> src_extents, dst_extents;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700748 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
749 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Darin Petkov68c10d12010-10-14 09:24:37 -0700750 if (gather_extents) {
751 TEST_AND_RETURN_FALSE(
Darin Petkov8e447e02013-04-16 16:23:50 +0200752 GatherExtents(old_filename,
753 chunk_offset,
754 chunk_size,
Gilad Arnoldfa404502014-01-01 23:36:12 -0800755 &src_extents));
Darin Petkov68c10d12010-10-14 09:24:37 -0700756 } else {
757 Extent* src_extent = operation.add_src_extents();
758 src_extent->set_start_block(0);
759 src_extent->set_num_blocks(
760 (old_stbuf.st_size + kBlockSize - 1) / kBlockSize);
761 }
Darin Petkov8e447e02013-04-16 16:23:50 +0200762 operation.set_src_length(old_data.size());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700763 }
764
Darin Petkov68c10d12010-10-14 09:24:37 -0700765 if (gather_extents) {
766 TEST_AND_RETURN_FALSE(
Darin Petkov8e447e02013-04-16 16:23:50 +0200767 GatherExtents(new_filename,
768 chunk_offset,
769 chunk_size,
Gilad Arnoldfa404502014-01-01 23:36:12 -0800770 &dst_extents));
Darin Petkov68c10d12010-10-14 09:24:37 -0700771 } else {
772 Extent* dst_extent = operation.add_dst_extents();
773 dst_extent->set_start_block(0);
774 dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize);
775 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700776 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700777
Gilad Arnoldfa404502014-01-01 23:36:12 -0800778 if (gather_extents) {
779 // Remove identical src/dst block ranges in MOVE operations.
Gilad Arnoldebca5712014-01-10 14:26:37 -0800780 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
781 size_t removed_bytes = RemoveIdenticalBlockRanges(
782 &src_extents, &dst_extents, new_data.size());
783
784 // Adjust the file length field accordingly.
785 if (removed_bytes) {
786 operation.set_src_length(old_data.size() - removed_bytes);
787 operation.set_dst_length(new_data.size() - removed_bytes);
788 }
789 }
Gilad Arnoldfa404502014-01-01 23:36:12 -0800790
791 // Embed extents in the operation.
792 DeltaDiffGenerator::StoreExtents(src_extents,
793 operation.mutable_src_extents());
794 DeltaDiffGenerator::StoreExtents(dst_extents,
795 operation.mutable_dst_extents());
796 }
797
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700798 out_data->swap(data);
799 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700800
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700801 return true;
802}
803
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700804bool DeltaDiffGenerator::InitializePartitionInfo(bool is_kernel,
805 const string& partition,
806 PartitionInfo* info) {
Darin Petkov7ea32332010-10-13 10:46:11 -0700807 int64_t size = 0;
808 if (is_kernel) {
809 size = utils::FileSize(partition);
810 } else {
811 int block_count = 0, block_size = 0;
812 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition,
813 &block_count,
814 &block_size));
815 size = static_cast<int64_t>(block_count) * block_size;
816 }
817 TEST_AND_RETURN_FALSE(size > 0);
Darin Petkov36a58222010-10-07 22:00:09 -0700818 info->set_size(size);
819 OmahaHashCalculator hasher;
Darin Petkov7ea32332010-10-13 10:46:11 -0700820 TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size);
Darin Petkov36a58222010-10-07 22:00:09 -0700821 TEST_AND_RETURN_FALSE(hasher.Finalize());
822 const vector<char>& hash = hasher.raw_hash();
823 info->set_hash(hash.data(), hash.size());
Darin Petkovd43d6902010-10-14 11:17:50 -0700824 LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash();
Darin Petkov36a58222010-10-07 22:00:09 -0700825 return true;
826}
827
828bool InitializePartitionInfos(const string& old_kernel,
829 const string& new_kernel,
830 const string& old_rootfs,
831 const string& new_rootfs,
832 DeltaArchiveManifest* manifest) {
Darin Petkovd43d6902010-10-14 11:17:50 -0700833 if (!old_kernel.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700834 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
835 true,
836 old_kernel,
837 manifest->mutable_old_kernel_info()));
Darin Petkovd43d6902010-10-14 11:17:50 -0700838 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700839 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
840 true,
841 new_kernel,
842 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700843 if (!old_rootfs.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700844 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
845 false,
846 old_rootfs,
847 manifest->mutable_old_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700848 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700849 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
850 false,
851 new_rootfs,
852 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700853 return true;
854}
855
Andrew de los Reyesef017552010-10-06 17:57:52 -0700856namespace {
857
858// Takes a collection (vector or RepeatedPtrField) of Extent and
859// returns a vector of the blocks referenced, in order.
860template<typename T>
861vector<uint64_t> ExpandExtents(const T& extents) {
862 vector<uint64_t> ret;
863 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
864 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700865 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700866 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700867 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700868 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700869 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700870 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700871 }
872 }
873 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700874 return ret;
875}
876
877// Takes a vector of blocks and returns an equivalent vector of Extent
878// objects.
879vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
880 vector<Extent> new_extents;
881 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
882 it != e; ++it) {
883 graph_utils::AppendBlockToExtents(&new_extents, *it);
884 }
885 return new_extents;
886}
887
888} // namespace {}
889
890void DeltaDiffGenerator::SubstituteBlocks(
891 Vertex* vertex,
892 const vector<Extent>& remove_extents,
893 const vector<Extent>& replace_extents) {
894 // First, expand out the blocks that op reads from
895 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700896 {
897 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700898 vector<uint64_t> remove_extents_expanded =
899 ExpandExtents(remove_extents);
900 vector<uint64_t> replace_extents_expanded =
901 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700902 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700903 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700904 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700905 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700906 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
907 }
908 utils::ApplyMap(&read_blocks, conversion);
909 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
910 e = vertex->out_edges.end(); it != e; ++it) {
911 vector<uint64_t> write_before_deps_expanded =
912 ExpandExtents(it->second.write_extents);
913 utils::ApplyMap(&write_before_deps_expanded, conversion);
914 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700915 }
916 }
917 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700918 vertex->op.clear_src_extents();
919 vector<Extent> new_extents = CompressExtents(read_blocks);
920 DeltaDiffGenerator::StoreExtents(new_extents,
921 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700922}
923
924bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700925 const set<Edge>& edges,
926 vector<CutEdgeVertexes>* out_cuts) {
927 DummyExtentAllocator scratch_allocator;
928 vector<CutEdgeVertexes> cuts;
929 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700930
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700931 uint64_t scratch_blocks_used = 0;
932 for (set<Edge>::const_iterator it = edges.begin();
933 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700934 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700935 vector<Extent> old_extents =
936 (*graph)[it->first].out_edges[it->second].extents;
937 // Choose some scratch space
938 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700939 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700940 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
941 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700942 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700943 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700944 cuts.back().old_src = it->first;
945 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700946
Andrew de los Reyesef017552010-10-06 17:57:52 -0700947 EdgeProperties& cut_edge_properties =
948 (*graph)[it->first].out_edges.find(it->second)->second;
949
950 // This should never happen, as we should only be cutting edges between
951 // real file nodes, and write-before relationships are created from
952 // a real file node to a temp copy node:
953 CHECK(cut_edge_properties.write_extents.empty())
954 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700955
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700956 // make node depend on the copy operation
957 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700958 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700959
960 // Set src/dst extents and other proto variables for copy operation
961 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
962 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700963 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700964 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700965 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700966 graph->back().op.mutable_dst_extents());
967 graph->back().op.set_src_length(
968 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
969 graph->back().op.set_dst_length(graph->back().op.src_length());
970
971 // make the dest node read from the scratch space
972 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700973 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700974 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700975 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700976
977 // delete the old edge
Mike Frysinger0f9547d2012-02-16 12:11:37 -0500978 CHECK_EQ(static_cast<Graph::size_type>(1),
979 (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700980
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700981 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700982 EdgeProperties write_before_edge_properties;
983 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
984 (*graph)[it->second].out_edges.insert(
985 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700986 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700987 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700988 return true;
989}
990
991// Stores all Extents in 'extents' into 'out'.
992void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700993 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700994 google::protobuf::RepeatedPtrField<Extent>* out) {
995 for (vector<Extent>::const_iterator it = extents.begin();
996 it != extents.end(); ++it) {
997 Extent* new_extent = out->Add();
998 *new_extent = *it;
999 }
1000}
1001
1002// Creates all the edges for the graph. Writers of a block point to
1003// readers of the same block. This is because for an edge A->B, B
1004// must complete before A executes.
1005void DeltaDiffGenerator::CreateEdges(Graph* graph,
1006 const vector<Block>& blocks) {
1007 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1008 // Blocks with both a reader and writer get an edge
1009 if (blocks[i].reader == Vertex::kInvalidIndex ||
1010 blocks[i].writer == Vertex::kInvalidIndex)
1011 continue;
1012 // Don't have a node depend on itself
1013 if (blocks[i].reader == blocks[i].writer)
1014 continue;
1015 // See if there's already an edge we can add onto
1016 Vertex::EdgeMap::iterator edge_it =
1017 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
1018 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
1019 // No existing edge. Create one
1020 (*graph)[blocks[i].writer].out_edges.insert(
1021 make_pair(blocks[i].reader, EdgeProperties()));
1022 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -07001023 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001024 }
1025 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
1026 }
1027}
1028
Andrew de los Reyesef017552010-10-06 17:57:52 -07001029namespace {
1030
1031class SortCutsByTopoOrderLess {
1032 public:
1033 SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table)
1034 : table_(table) {}
1035 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
1036 return table_[a.old_dst] < table_[b.old_dst];
1037 }
1038 private:
1039 vector<vector<Vertex::Index>::size_type>& table_;
1040};
1041
1042} // namespace {}
1043
1044void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
1045 vector<Vertex::Index>& op_indexes,
1046 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
1047 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
1048 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
1049 i != e; ++i) {
1050 Vertex::Index node = op_indexes[i];
1051 if (table.size() < (node + 1)) {
1052 table.resize(node + 1);
1053 }
1054 table[node] = i;
1055 }
1056 reverse_op_indexes->swap(table);
1057}
1058
1059void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes,
1060 vector<CutEdgeVertexes>* cuts) {
1061 // first, make a reverse lookup table.
1062 vector<vector<Vertex::Index>::size_type> table;
1063 GenerateReverseTopoOrderMap(op_indexes, &table);
1064 SortCutsByTopoOrderLess less(table);
1065 sort(cuts->begin(), cuts->end(), less);
1066}
1067
1068void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
1069 vector<Vertex::Index>* op_indexes) {
1070 vector<Vertex::Index> ret;
1071 vector<Vertex::Index> full_ops;
1072 ret.reserve(op_indexes->size());
1073 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
1074 ++i) {
1075 DeltaArchiveManifest_InstallOperation_Type type =
1076 (*graph)[(*op_indexes)[i]].op.type();
1077 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
1078 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
1079 full_ops.push_back((*op_indexes)[i]);
1080 } else {
1081 ret.push_back((*op_indexes)[i]);
1082 }
1083 }
1084 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
1085 << (full_ops.size() + ret.size()) << " total ops.";
1086 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
1087 op_indexes->swap(ret);
1088}
1089
1090namespace {
1091
1092template<typename T>
1093bool TempBlocksExistInExtents(const T& extents) {
1094 for (int i = 0, e = extents.size(); i < e; ++i) {
1095 Extent extent = graph_utils::GetElement(extents, i);
1096 uint64_t start = extent.start_block();
1097 uint64_t num = extent.num_blocks();
1098 if (start == kSparseHole)
1099 continue;
1100 if (start >= kTempBlockStart ||
1101 (start + num) >= kTempBlockStart) {
1102 LOG(ERROR) << "temp block!";
1103 LOG(ERROR) << "start: " << start << ", num: " << num;
1104 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
1105 LOG(ERROR) << "returning true";
1106 return true;
1107 }
1108 // check for wrap-around, which would be a bug:
1109 CHECK(start <= (start + num));
1110 }
1111 return false;
1112}
1113
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001114// Convertes the cuts, which must all have the same |old_dst| member,
1115// to full. It does this by converting the |old_dst| to REPLACE or
1116// REPLACE_BZ, dropping all incoming edges to |old_dst|, and marking
1117// all temp nodes invalid.
1118bool ConvertCutsToFull(
1119 Graph* graph,
1120 const string& new_root,
1121 int data_fd,
1122 off_t* data_file_size,
1123 vector<Vertex::Index>* op_indexes,
1124 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
1125 const vector<CutEdgeVertexes>& cuts) {
1126 CHECK(!cuts.empty());
1127 set<Vertex::Index> deleted_nodes;
1128 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1129 e = cuts.end(); it != e; ++it) {
1130 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp(
1131 graph,
1132 *it,
1133 new_root,
1134 data_fd,
1135 data_file_size));
1136 deleted_nodes.insert(it->new_vertex);
1137 }
1138 deleted_nodes.insert(cuts[0].old_dst);
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001139
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001140 vector<Vertex::Index> new_op_indexes;
1141 new_op_indexes.reserve(op_indexes->size());
1142 for (vector<Vertex::Index>::iterator it = op_indexes->begin(),
1143 e = op_indexes->end(); it != e; ++it) {
1144 if (utils::SetContainsKey(deleted_nodes, *it))
1145 continue;
1146 new_op_indexes.push_back(*it);
1147 }
1148 new_op_indexes.push_back(cuts[0].old_dst);
1149 op_indexes->swap(new_op_indexes);
1150 DeltaDiffGenerator::GenerateReverseTopoOrderMap(*op_indexes,
1151 reverse_op_indexes);
1152 return true;
1153}
1154
1155// Tries to assign temp blocks for a collection of cuts, all of which share
1156// the same old_dst member. If temp blocks can't be found, old_dst will be
1157// converted to a REPLACE or REPLACE_BZ operation. Returns true on success,
1158// which can happen even if blocks are converted to full. Returns false
1159// on exceptional error cases.
1160bool AssignBlockForAdjoiningCuts(
1161 Graph* graph,
1162 const string& new_root,
1163 int data_fd,
1164 off_t* data_file_size,
1165 vector<Vertex::Index>* op_indexes,
1166 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
1167 const vector<CutEdgeVertexes>& cuts) {
1168 CHECK(!cuts.empty());
1169 const Vertex::Index old_dst = cuts[0].old_dst;
1170 // Calculate # of blocks needed
1171 uint64_t blocks_needed = 0;
1172 map<const CutEdgeVertexes*, uint64_t> cuts_blocks_needed;
1173 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1174 e = cuts.end(); it != e; ++it) {
1175 uint64_t cut_blocks_needed = 0;
1176 for (vector<Extent>::const_iterator jt = it->tmp_extents.begin(),
1177 je = it->tmp_extents.end(); jt != je; ++jt) {
1178 cut_blocks_needed += jt->num_blocks();
1179 }
1180 blocks_needed += cut_blocks_needed;
1181 cuts_blocks_needed[&*it] = cut_blocks_needed;
1182 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001183
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001184 // Find enough blocks
1185 ExtentRanges scratch_ranges;
1186 // Each block that's supplying temp blocks and the corresponding blocks:
1187 typedef vector<pair<Vertex::Index, ExtentRanges> > SupplierVector;
1188 SupplierVector block_suppliers;
1189 uint64_t scratch_blocks_found = 0;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001190 for (vector<Vertex::Index>::size_type i = (*reverse_op_indexes)[old_dst] + 1,
1191 e = op_indexes->size(); i < e; ++i) {
1192 Vertex::Index test_node = (*op_indexes)[i];
1193 if (!(*graph)[test_node].valid)
1194 continue;
1195 // See if this node has sufficient blocks
1196 ExtentRanges ranges;
1197 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
1198 ranges.SubtractExtent(ExtentForRange(
1199 kTempBlockStart, kSparseHole - kTempBlockStart));
1200 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
1201 // For now, for simplicity, subtract out all blocks in read-before
1202 // dependencies.
1203 for (Vertex::EdgeMap::const_iterator edge_i =
1204 (*graph)[test_node].out_edges.begin(),
1205 edge_e = (*graph)[test_node].out_edges.end();
1206 edge_i != edge_e; ++edge_i) {
1207 ranges.SubtractExtents(edge_i->second.extents);
1208 }
1209 if (ranges.blocks() == 0)
1210 continue;
1211
1212 if (ranges.blocks() + scratch_blocks_found > blocks_needed) {
1213 // trim down ranges
1214 vector<Extent> new_ranges = ranges.GetExtentsForBlockCount(
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001215 blocks_needed - scratch_blocks_found);
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001216 ranges = ExtentRanges();
1217 ranges.AddExtents(new_ranges);
1218 }
1219 scratch_ranges.AddRanges(ranges);
1220 block_suppliers.push_back(make_pair(test_node, ranges));
1221 scratch_blocks_found += ranges.blocks();
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001222 if (scratch_ranges.blocks() >= blocks_needed)
1223 break;
1224 }
1225 if (scratch_ranges.blocks() < blocks_needed) {
1226 LOG(INFO) << "Unable to find sufficient scratch";
1227 TEST_AND_RETURN_FALSE(ConvertCutsToFull(graph,
1228 new_root,
1229 data_fd,
1230 data_file_size,
1231 op_indexes,
1232 reverse_op_indexes,
1233 cuts));
1234 return true;
1235 }
1236 // Use the scratch we found
1237 TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found);
1238
1239 // Make all the suppliers depend on this node
1240 for (SupplierVector::iterator it = block_suppliers.begin(),
1241 e = block_suppliers.end(); it != e; ++it) {
1242 graph_utils::AddReadBeforeDepExtents(
1243 &(*graph)[it->first],
1244 old_dst,
1245 it->second.GetExtentsForBlockCount(it->second.blocks()));
1246 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001247
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001248 // Replace temp blocks in each cut
1249 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1250 e = cuts.end(); it != e; ++it) {
1251 vector<Extent> real_extents =
1252 scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]);
1253 scratch_ranges.SubtractExtents(real_extents);
1254
1255 // Fix the old dest node w/ the real blocks
1256 DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst],
1257 it->tmp_extents,
1258 real_extents);
1259
1260 // Fix the new node w/ the real blocks. Since the new node is just a
1261 // copy operation, we can replace all the dest extents w/ the real
1262 // blocks.
1263 DeltaArchiveManifest_InstallOperation *op =
1264 &(*graph)[it->new_vertex].op;
1265 op->clear_dst_extents();
1266 DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents());
1267 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001268 return true;
1269}
1270
Andrew de los Reyesef017552010-10-06 17:57:52 -07001271} // namespace {}
1272
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001273// Returns true if |op| is a no-op operation that doesn't do any useful work
1274// (e.g., a move operation that copies blocks onto themselves).
1275bool DeltaDiffGenerator::IsNoopOperation(
1276 const DeltaArchiveManifest_InstallOperation& op) {
1277 return (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE &&
1278 ExpandExtents(op.src_extents()) == ExpandExtents(op.dst_extents()));
1279}
1280
Andrew de los Reyesef017552010-10-06 17:57:52 -07001281bool DeltaDiffGenerator::AssignTempBlocks(
1282 Graph* graph,
1283 const string& new_root,
1284 int data_fd,
1285 off_t* data_file_size,
1286 vector<Vertex::Index>* op_indexes,
1287 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001288 const vector<CutEdgeVertexes>& cuts) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001289 CHECK(!cuts.empty());
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001290
1291 // group of cuts w/ the same old_dst:
1292 vector<CutEdgeVertexes> cuts_group;
1293
Andrew de los Reyesef017552010-10-06 17:57:52 -07001294 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
1295 true ; --i) {
1296 LOG(INFO) << "Fixing temp blocks in cut " << i
1297 << ": old dst: " << cuts[i].old_dst << " new vertex: "
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001298 << cuts[i].new_vertex << " path: "
1299 << (*graph)[cuts[i].old_dst].file_name;
1300
1301 if (cuts_group.empty() || (cuts_group[0].old_dst == cuts[i].old_dst)) {
1302 cuts_group.push_back(cuts[i]);
1303 } else {
1304 CHECK(!cuts_group.empty());
1305 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1306 new_root,
1307 data_fd,
1308 data_file_size,
1309 op_indexes,
1310 reverse_op_indexes,
1311 cuts_group));
1312 cuts_group.clear();
1313 cuts_group.push_back(cuts[i]);
Andrew de los Reyesef017552010-10-06 17:57:52 -07001314 }
Darin Petkov36a58222010-10-07 22:00:09 -07001315
Andrew de los Reyesef017552010-10-06 17:57:52 -07001316 if (i == e) {
1317 // break out of for() loop
1318 break;
1319 }
1320 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001321 CHECK(!cuts_group.empty());
1322 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1323 new_root,
1324 data_fd,
1325 data_file_size,
1326 op_indexes,
1327 reverse_op_indexes,
1328 cuts_group));
Andrew de los Reyesef017552010-10-06 17:57:52 -07001329 return true;
1330}
1331
1332bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1333 size_t idx = 0;
1334 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1335 ++it, ++idx) {
1336 if (!it->valid)
1337 continue;
1338 const DeltaArchiveManifest_InstallOperation& op = it->op;
1339 if (TempBlocksExistInExtents(op.dst_extents()) ||
1340 TempBlocksExistInExtents(op.src_extents())) {
1341 LOG(INFO) << "bad extents in node " << idx;
1342 LOG(INFO) << "so yeah";
1343 return false;
1344 }
1345
1346 // Check out-edges:
1347 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1348 je = it->out_edges.end(); jt != je; ++jt) {
1349 if (TempBlocksExistInExtents(jt->second.extents) ||
1350 TempBlocksExistInExtents(jt->second.write_extents)) {
1351 LOG(INFO) << "bad out edge in node " << idx;
1352 LOG(INFO) << "so yeah";
1353 return false;
1354 }
1355 }
1356 }
1357 return true;
1358}
1359
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001360bool DeltaDiffGenerator::ReorderDataBlobs(
1361 DeltaArchiveManifest* manifest,
1362 const std::string& data_blobs_path,
1363 const std::string& new_data_blobs_path) {
1364 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1365 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1366 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001367
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001368 DirectFileWriter writer;
1369 TEST_AND_RETURN_FALSE(
1370 writer.Open(new_data_blobs_path.c_str(),
1371 O_WRONLY | O_TRUNC | O_CREAT,
1372 0644) == 0);
1373 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001374 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001375
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001376 for (int i = 0; i < (manifest->install_operations_size() +
1377 manifest->kernel_install_operations_size()); i++) {
1378 DeltaArchiveManifest_InstallOperation* op = NULL;
1379 if (i < manifest->install_operations_size()) {
1380 op = manifest->mutable_install_operations(i);
1381 } else {
1382 op = manifest->mutable_kernel_install_operations(
1383 i - manifest->install_operations_size());
1384 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001385 if (!op->has_data_offset())
1386 continue;
1387 CHECK(op->has_data_length());
1388 vector<char> buf(op->data_length());
1389 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1390 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1391
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001392 // Add the hash of the data blobs for this operation
1393 TEST_AND_RETURN_FALSE(AddOperationHash(op, buf));
1394
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001395 op->set_data_offset(out_file_size);
Don Garrette410e0f2011-11-10 15:39:01 -08001396 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001397 out_file_size += buf.size();
1398 }
1399 return true;
1400}
1401
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001402bool DeltaDiffGenerator::AddOperationHash(
1403 DeltaArchiveManifest_InstallOperation* op,
1404 const vector<char>& buf) {
1405 OmahaHashCalculator hasher;
1406
1407 TEST_AND_RETURN_FALSE(hasher.Update(&buf[0], buf.size()));
1408 TEST_AND_RETURN_FALSE(hasher.Finalize());
1409
1410 const vector<char>& hash = hasher.raw_hash();
1411 op->set_data_sha256_hash(hash.data(), hash.size());
1412 return true;
1413}
1414
Andrew de los Reyesef017552010-10-06 17:57:52 -07001415bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1416 const CutEdgeVertexes& cut,
1417 const string& new_root,
1418 int data_fd,
1419 off_t* data_file_size) {
1420 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001421
Andrew de los Reyesef017552010-10-06 17:57:52 -07001422 // Keep all outgoing edges
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001423 if ((*graph)[cut.old_dst].op.type() !=
1424 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ &&
1425 (*graph)[cut.old_dst].op.type() !=
1426 DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
1427 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1428 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001429
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001430 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1431 cut.old_dst,
1432 NULL,
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -08001433 kNonexistentPath,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001434 new_root,
1435 (*graph)[cut.old_dst].file_name,
Darin Petkov8e447e02013-04-16 16:23:50 +02001436 (*graph)[cut.old_dst].chunk_offset,
1437 (*graph)[cut.old_dst].chunk_size,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001438 data_fd,
1439 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001440
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001441 (*graph)[cut.old_dst].out_edges = out_edges;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001442
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001443 // Right now we don't have doubly-linked edges, so we have to scan
1444 // the whole graph.
1445 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1446 }
Andrew de los Reyesef017552010-10-06 17:57:52 -07001447
1448 // Delete temp node
1449 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1450 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1451 (*graph)[cut.old_dst].out_edges.end());
1452 (*graph)[cut.new_vertex].valid = false;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001453 LOG(INFO) << "marked node invalid: " << cut.new_vertex;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001454 return true;
1455}
1456
1457bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1458 const string& new_root,
1459 int fd,
1460 off_t* data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001461 vector<Vertex::Index>* final_order,
1462 Vertex::Index scratch_vertex) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001463 CycleBreaker cycle_breaker;
1464 LOG(INFO) << "Finding cycles...";
1465 set<Edge> cut_edges;
1466 cycle_breaker.BreakCycles(*graph, &cut_edges);
1467 LOG(INFO) << "done finding cycles";
1468 CheckGraph(*graph);
1469
1470 // Calculate number of scratch blocks needed
1471
1472 LOG(INFO) << "Cutting cycles...";
1473 vector<CutEdgeVertexes> cuts;
1474 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1475 LOG(INFO) << "done cutting cycles";
1476 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1477 CheckGraph(*graph);
1478
1479 LOG(INFO) << "Creating initial topological order...";
1480 TopologicalSort(*graph, final_order);
1481 LOG(INFO) << "done with initial topo order";
1482 CheckGraph(*graph);
1483
1484 LOG(INFO) << "Moving full ops to the back";
1485 MoveFullOpsToBack(graph, final_order);
1486 LOG(INFO) << "done moving full ops to back";
1487
1488 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1489 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1490
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001491 SortCutsByTopoOrder(*final_order, &cuts);
1492
Andrew de los Reyesef017552010-10-06 17:57:52 -07001493 if (!cuts.empty())
1494 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1495 new_root,
1496 fd,
1497 data_file_size,
1498 final_order,
1499 &inverse_final_order,
1500 cuts));
1501 LOG(INFO) << "Making sure all temp blocks have been allocated";
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001502
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001503 // Remove the scratch node, if any
1504 if (scratch_vertex != Vertex::kInvalidIndex) {
1505 final_order->erase(final_order->begin() +
1506 inverse_final_order[scratch_vertex]);
1507 (*graph)[scratch_vertex].valid = false;
1508 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1509 }
1510
Andrew de los Reyesef017552010-10-06 17:57:52 -07001511 graph_utils::DumpGraph(*graph);
1512 CHECK(NoTempBlocksRemain(*graph));
1513 LOG(INFO) << "done making sure all temp blocks are allocated";
1514 return true;
1515}
1516
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001517void DeltaDiffGenerator::CreateScratchNode(uint64_t start_block,
1518 uint64_t num_blocks,
1519 Vertex* vertex) {
1520 vertex->file_name = "<scratch>";
1521 vertex->op.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
1522 vertex->op.set_data_offset(0);
1523 vertex->op.set_data_length(0);
1524 Extent* extent = vertex->op.add_dst_extents();
1525 extent->set_start_block(start_block);
1526 extent->set_num_blocks(num_blocks);
1527}
1528
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001529bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1530 const string& old_root,
1531 const string& old_image,
1532 const string& new_root,
1533 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001534 const string& old_kernel_part,
1535 const string& new_kernel_part,
1536 const string& output_path,
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001537 const string& private_key_path,
Darin Petkov8e447e02013-04-16 16:23:50 +02001538 off_t chunk_size,
Chris Sosad5ae1562013-04-23 13:20:18 -07001539 size_t rootfs_partition_size,
Don Garrett0dd39852013-04-03 16:55:42 -07001540 const ImageInfo* old_image_info,
1541 const ImageInfo* new_image_info,
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001542 uint64_t* metadata_size) {
Darin Petkov8e447e02013-04-16 16:23:50 +02001543 TEST_AND_RETURN_FALSE(chunk_size == -1 || chunk_size % kBlockSize == 0);
Darin Petkov7ea32332010-10-13 10:46:11 -07001544 int old_image_block_count = 0, old_image_block_size = 0;
1545 int new_image_block_count = 0, new_image_block_size = 0;
1546 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1547 &new_image_block_count,
1548 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001549 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001550 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1551 &old_image_block_count,
1552 &old_image_block_size));
1553 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1554 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1555 << "Old and new images have different block counts.";
Don Garrett0dd39852013-04-03 16:55:42 -07001556
Don Garrett60fc59c2013-10-18 11:43:52 -07001557 // If new_image_info is present, old_image_info must be present.
Don Garrett0dd39852013-04-03 16:55:42 -07001558 TEST_AND_RETURN_FALSE((bool)old_image_info == (bool)new_image_info);
1559 } else {
1560 // old_image_info must not be present for a full update.
1561 TEST_AND_RETURN_FALSE(!old_image_info);
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001562 }
Chris Sosad5ae1562013-04-23 13:20:18 -07001563
1564 // Sanity checks for the partition size.
1565 TEST_AND_RETURN_FALSE(rootfs_partition_size % kBlockSize == 0);
Chris Sosae9f5f422013-05-17 16:11:10 -07001566 size_t fs_size = static_cast<size_t>(new_image_block_size) *
1567 new_image_block_count;
Chris Sosad5ae1562013-04-23 13:20:18 -07001568 LOG(INFO) << "Rootfs partition size: " << rootfs_partition_size;
1569 LOG(INFO) << "Actual filesystem size: " << fs_size;
1570 TEST_AND_RETURN_FALSE(rootfs_partition_size >= fs_size);
1571
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001572 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001573 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1574
Darin Petkov7ea32332010-10-13 10:46:11 -07001575 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1576 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1577 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001578 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1579 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1580 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1581 }
1582 Graph graph;
1583 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001584
Gilad Arnolda6742b32014-01-11 00:18:34 -08001585 const string kTempFileTemplate("CrAU_temp_data.XXXXXX");
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001586 string temp_file_path;
Darin Petkov7438a5c2011-08-29 11:56:44 -07001587 scoped_ptr<ScopedPathUnlinker> temp_file_unlinker;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001588 off_t data_file_size = 0;
1589
1590 LOG(INFO) << "Reading files...";
1591
Don Garrettb8dd1d92013-11-22 17:40:02 -08001592 // Create empty protobuf Manifest object
1593 DeltaArchiveManifest manifest;
1594
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001595 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1596
Andrew de los Reyesef017552010-10-06 17:57:52 -07001597 vector<Vertex::Index> final_order;
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001598 Vertex::Index scratch_vertex = Vertex::kInvalidIndex;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001599 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001600 int fd;
1601 TEST_AND_RETURN_FALSE(
1602 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001603 temp_file_unlinker.reset(new ScopedPathUnlinker(temp_file_path));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001604 TEST_AND_RETURN_FALSE(fd >= 0);
1605 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001606 if (!old_image.empty()) {
1607 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001608
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001609 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1610 &blocks,
1611 old_root,
1612 new_root,
Darin Petkov8e447e02013-04-16 16:23:50 +02001613 chunk_size,
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001614 fd,
1615 &data_file_size));
1616 LOG(INFO) << "done reading normal files";
1617 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001618
Thieu Le5c7d9752010-12-15 16:09:28 -08001619 LOG(INFO) << "Starting metadata processing";
1620 TEST_AND_RETURN_FALSE(Metadata::DeltaReadMetadata(&graph,
1621 &blocks,
1622 old_image,
1623 new_image,
1624 fd,
1625 &data_file_size));
1626 LOG(INFO) << "Done metadata processing";
1627 CheckGraph(graph);
1628
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001629 graph.resize(graph.size() + 1);
1630 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1631 fd,
1632 &data_file_size,
1633 new_image,
1634 &graph.back()));
1635
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001636 // Final scratch block (if there's space)
Chris Sosad5ae1562013-04-23 13:20:18 -07001637 if (blocks.size() < (rootfs_partition_size / kBlockSize)) {
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001638 scratch_vertex = graph.size();
1639 graph.resize(graph.size() + 1);
1640 CreateScratchNode(blocks.size(),
Chris Sosad5ae1562013-04-23 13:20:18 -07001641 (rootfs_partition_size / kBlockSize) - blocks.size(),
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001642 &graph.back());
1643 }
1644
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001645 // Read kernel partition
1646 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1647 new_kernel_part,
1648 &kernel_ops,
1649 fd,
1650 &data_file_size));
1651
1652 LOG(INFO) << "done reading kernel";
1653 CheckGraph(graph);
1654
1655 LOG(INFO) << "Creating edges...";
1656 CreateEdges(&graph, blocks);
1657 LOG(INFO) << "Done creating edges";
1658 CheckGraph(graph);
1659
1660 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1661 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001662 fd,
1663 &data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001664 &final_order,
1665 scratch_vertex));
Don Garrettb8dd1d92013-11-22 17:40:02 -08001666
1667 // Set the minor version for this payload.
1668 LOG(INFO) << "Adding Delta Minor Version.";
1669 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001670 } else {
1671 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001672 off_t new_image_size =
1673 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Darin Petkov7a22d792010-11-08 14:10:00 -08001674 TEST_AND_RETURN_FALSE(FullUpdateGenerator::Run(&graph,
1675 new_kernel_part,
1676 new_image,
1677 new_image_size,
1678 fd,
1679 &data_file_size,
1680 kFullUpdateChunkSize,
1681 kBlockSize,
1682 &kernel_ops,
1683 &final_order));
Don Garrettb8dd1d92013-11-22 17:40:02 -08001684
1685 // Set the minor version for this payload.
1686 LOG(INFO) << "Adding Full Minor Version.";
1687 manifest.set_minor_version(DeltaPerformer::kFullPayloadMinorVersion);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001688 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001689 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001690
Don Garrett0dd39852013-04-03 16:55:42 -07001691 if (old_image_info)
1692 *(manifest.mutable_old_image_info()) = *old_image_info;
1693
1694 if (new_image_info)
1695 *(manifest.mutable_new_image_info()) = *new_image_info;
1696
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001697 OperationNameMap op_name_map;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001698 CheckGraph(graph);
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001699 InstallOperationsToManifest(graph,
1700 final_order,
1701 kernel_ops,
1702 &manifest,
1703 &op_name_map);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001704 CheckGraph(graph);
1705 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001706
1707 // Reorder the data blobs with the newly ordered manifest
1708 string ordered_blobs_path;
1709 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -08001710 "CrAU_temp_data.ordered.XXXXXX",
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001711 &ordered_blobs_path,
Andrew de los Reyese05fc282011-06-02 09:50:08 -07001712 NULL));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001713 ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001714 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1715 temp_file_path,
1716 ordered_blobs_path));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001717 temp_file_unlinker.reset();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001718
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001719 // Check that install op blobs are in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001720 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001721 {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001722 for (int i = 0; i < (manifest.install_operations_size() +
1723 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001724 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001725 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001726 manifest.mutable_install_operations(i) :
1727 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001728 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001729 if (op->has_data_offset()) {
1730 if (op->data_offset() != next_blob_offset) {
1731 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001732 << next_blob_offset;
1733 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001734 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001735 }
1736 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001737 }
1738
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001739 // Signatures appear at the end of the blobs. Note the offset in the
1740 // manifest
1741 if (!private_key_path.empty()) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001742 uint64_t signature_blob_length = 0;
1743 TEST_AND_RETURN_FALSE(
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -07001744 PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path),
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001745 &signature_blob_length));
Darin Petkov9574f7e2011-01-13 10:48:12 -08001746 AddSignatureOp(next_blob_offset, signature_blob_length, &manifest);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001747 }
1748
Darin Petkov36a58222010-10-07 22:00:09 -07001749 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1750 new_kernel_part,
1751 old_image,
1752 new_image,
1753 &manifest));
1754
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001755 // Serialize protobuf
1756 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001757
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001758 CheckGraph(graph);
1759 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1760 CheckGraph(graph);
1761
1762 LOG(INFO) << "Writing final delta file header...";
1763 DirectFileWriter writer;
1764 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1765 O_WRONLY | O_CREAT | O_TRUNC,
1766 0644) == 0);
1767 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001768
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001769 // Write header
Don Garrette410e0f2011-11-10 15:39:01 -08001770 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001771
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001772 // Write version number
1773 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001774
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001775 // Write protobuf length
1776 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1777 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001778
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001779 // Write protobuf
1780 LOG(INFO) << "Writing final delta file protobuf... "
1781 << serialized_manifest.size();
1782 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
Don Garrette410e0f2011-11-10 15:39:01 -08001783 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001784
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001785 // Append the data blobs
1786 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001787 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001788 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1789 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1790 for (;;) {
1791 char buf[kBlockSize];
1792 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1793 if (0 == rc) {
1794 // EOF
1795 break;
1796 }
1797 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
Don Garrette410e0f2011-11-10 15:39:01 -08001798 TEST_AND_RETURN_FALSE(writer.Write(buf, rc));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001799 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001800
1801 // Write signature blob.
1802 if (!private_key_path.empty()) {
1803 LOG(INFO) << "Signing the update...";
1804 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -07001805 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
1806 output_path,
1807 vector<string>(1, private_key_path),
1808 &signature_blob));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001809 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
Don Garrette410e0f2011-11-10 15:39:01 -08001810 signature_blob.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001811 }
1812
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001813 *metadata_size =
Darin Petkov95cf01f2010-10-12 14:59:13 -07001814 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001815 ReportPayloadUsage(manifest, *metadata_size, op_name_map);
Darin Petkov880335c2010-10-01 15:52:53 -07001816
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001817 LOG(INFO) << "All done. Successfully created delta file with "
1818 << "metadata size = " << *metadata_size;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001819 return true;
1820}
1821
Thieu Le5c7d9752010-12-15 16:09:28 -08001822// Runs the bsdiff tool on two files and returns the resulting delta in
1823// 'out'. Returns true on success.
1824bool DeltaDiffGenerator::BsdiffFiles(const string& old_file,
1825 const string& new_file,
1826 vector<char>* out) {
Gilad Arnolda6742b32014-01-11 00:18:34 -08001827 const string kPatchFile = "delta.patchXXXXXX";
Thieu Le5c7d9752010-12-15 16:09:28 -08001828 string patch_file_path;
1829
1830 TEST_AND_RETURN_FALSE(
1831 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
1832
1833 vector<string> cmd;
1834 cmd.push_back(kBsdiffPath);
1835 cmd.push_back(old_file);
1836 cmd.push_back(new_file);
1837 cmd.push_back(patch_file_path);
1838
1839 int rc = 1;
1840 vector<char> patch_file;
Darin Petkov85d02b72011-05-17 13:25:51 -07001841 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc, NULL));
Thieu Le5c7d9752010-12-15 16:09:28 -08001842 TEST_AND_RETURN_FALSE(rc == 0);
1843 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
1844 unlink(patch_file_path.c_str());
1845 return true;
1846}
1847
1848// The |blocks| vector contains a reader and writer for each block on the
1849// filesystem that's being in-place updated. We populate the reader/writer
1850// fields of |blocks| by calling this function.
1851// For each block in |operation| that is read or written, find that block
1852// in |blocks| and set the reader/writer field to the vertex passed.
1853// |graph| is not strictly necessary, but useful for printing out
1854// error messages.
1855bool DeltaDiffGenerator::AddInstallOpToBlocksVector(
1856 const DeltaArchiveManifest_InstallOperation& operation,
1857 const Graph& graph,
1858 Vertex::Index vertex,
1859 vector<Block>* blocks) {
1860 // See if this is already present.
1861 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
1862
1863 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
1864 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
1865 const int extents_size =
1866 (field == READER) ? operation.src_extents_size() :
1867 operation.dst_extents_size();
1868 const char* past_participle = (field == READER) ? "read" : "written";
1869 const google::protobuf::RepeatedPtrField<Extent>& extents =
1870 (field == READER) ? operation.src_extents() : operation.dst_extents();
1871 Vertex::Index Block::*access_type =
1872 (field == READER) ? &Block::reader : &Block::writer;
1873
1874 for (int i = 0; i < extents_size; i++) {
1875 const Extent& extent = extents.Get(i);
1876 if (extent.start_block() == kSparseHole) {
1877 // Hole in sparse file. skip
1878 continue;
1879 }
1880 for (uint64_t block = extent.start_block();
1881 block < (extent.start_block() + extent.num_blocks()); block++) {
1882 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
1883 LOG(FATAL) << "Block " << block << " is already "
1884 << past_participle << " by "
1885 << (*blocks)[block].*access_type << "("
1886 << graph[(*blocks)[block].*access_type].file_name
1887 << ") and also " << vertex << "("
1888 << graph[vertex].file_name << ")";
1889 }
1890 (*blocks)[block].*access_type = vertex;
1891 }
1892 }
1893 }
1894 return true;
1895}
1896
Darin Petkov9574f7e2011-01-13 10:48:12 -08001897void DeltaDiffGenerator::AddSignatureOp(uint64_t signature_blob_offset,
1898 uint64_t signature_blob_length,
1899 DeltaArchiveManifest* manifest) {
1900 LOG(INFO) << "Making room for signature in file";
1901 manifest->set_signatures_offset(signature_blob_offset);
1902 LOG(INFO) << "set? " << manifest->has_signatures_offset();
1903 // Add a dummy op at the end to appease older clients
1904 DeltaArchiveManifest_InstallOperation* dummy_op =
1905 manifest->add_kernel_install_operations();
1906 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1907 dummy_op->set_data_offset(signature_blob_offset);
1908 manifest->set_signatures_offset(signature_blob_offset);
1909 dummy_op->set_data_length(signature_blob_length);
1910 manifest->set_signatures_size(signature_blob_length);
1911 Extent* dummy_extent = dummy_op->add_dst_extents();
1912 // Tell the dummy op to write this data to a big sparse hole
1913 dummy_extent->set_start_block(kSparseHole);
1914 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1915 kBlockSize);
1916}
1917
Andrew de los Reyes50f36492010-11-01 13:57:12 -07001918const char* const kBsdiffPath = "bsdiff";
1919const char* const kBspatchPath = "bspatch";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001920const char* const kDeltaMagic = "CrAU";
1921
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001922}; // namespace chromeos_update_engine