blob: 48fe1775beeeced22861bfb995572663e0c168aa [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"
31#include "update_engine/extent_mapper.h"
Andrew de los Reyesef017552010-10-06 17:57:52 -070032#include "update_engine/extent_ranges.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070033#include "update_engine/file_writer.h"
34#include "update_engine/filesystem_iterator.h"
Darin Petkov7a22d792010-11-08 14:10:00 -080035#include "update_engine/full_update_generator.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070036#include "update_engine/graph_types.h"
37#include "update_engine/graph_utils.h"
Thieu Le5c7d9752010-12-15 16:09:28 -080038#include "update_engine/metadata.h"
Darin Petkov36a58222010-10-07 22:00:09 -070039#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070040#include "update_engine/payload_signer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070041#include "update_engine/subprocess.h"
42#include "update_engine/topological_sort.h"
43#include "update_engine/update_metadata.pb.h"
44#include "update_engine/utils.h"
45
46using std::make_pair;
Andrew de los Reyesef017552010-10-06 17:57:52 -070047using std::map;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070048using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070049using std::min;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -070050using std::pair;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070051using std::set;
52using std::string;
53using std::vector;
54
55namespace chromeos_update_engine {
56
57typedef DeltaDiffGenerator::Block Block;
Darin Petkov9fa7ec52010-10-18 11:45:23 -070058typedef map<const DeltaArchiveManifest_InstallOperation*,
Darin Petkov8e447e02013-04-16 16:23:50 +020059 string> OperationNameMap;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070060
Chris Sosaf586b012013-05-21 13:33:42 -070061// bytes
62const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024;
Chris Sosad5ae1562013-04-23 13:20:18 -070063const uint64_t kVersionNumber = 1;
64const uint64_t kFullUpdateChunkSize = 1024 * 1024; // bytes
65
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070066namespace {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070067const size_t kBlockSize = 4096; // bytes
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -080068const string kNonexistentPath = "";
Andrew de los Reyes927179d2010-12-02 11:26:48 -080069
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070070
Darin Petkov68c10d12010-10-14 09:24:37 -070071static const char* kInstallOperationTypes[] = {
72 "REPLACE",
73 "REPLACE_BZ",
74 "MOVE",
75 "BSDIFF"
76};
77
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070078// Stores all Extents for a file into 'out'. Returns true on success.
79bool GatherExtents(const string& path,
Darin Petkov8e447e02013-04-16 16:23:50 +020080 off_t chunk_offset,
81 off_t chunk_size,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070082 google::protobuf::RepeatedPtrField<Extent>* out) {
83 vector<Extent> extents;
Darin Petkov8e447e02013-04-16 16:23:50 +020084 TEST_AND_RETURN_FALSE(
85 extent_mapper::ExtentsForFileChunkFibmap(
86 path, chunk_offset, chunk_size, &extents));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070087 DeltaDiffGenerator::StoreExtents(extents, out);
88 return true;
89}
90
Andrew de los Reyesef017552010-10-06 17:57:52 -070091// For a given regular file which must exist at new_root + path, and
92// may exist at old_root + path, creates a new InstallOperation and
93// adds it to the graph. Also, populates the |blocks| array as
94// necessary, if |blocks| is non-NULL. Also, writes the data
95// necessary to send the file down to the client into data_fd, which
96// has length *data_file_size. *data_file_size is updated
97// appropriately. If |existing_vertex| is no kInvalidIndex, use that
98// rather than allocating a new vertex. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070099bool DeltaReadFile(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700100 Vertex::Index existing_vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700101 vector<Block>* blocks,
102 const string& old_root,
103 const string& new_root,
104 const string& path, // within new_root
Darin Petkov8e447e02013-04-16 16:23:50 +0200105 off_t chunk_offset,
106 off_t chunk_size,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700107 int data_fd,
108 off_t* data_file_size) {
109 vector<char> data;
110 DeltaArchiveManifest_InstallOperation operation;
111
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800112 string old_path = (old_root == kNonexistentPath) ? kNonexistentPath :
113 old_root + path;
114
Don Garrett1d787092013-03-11 18:07:28 -0700115 // If bsdiff breaks again, blacklist the problem file by using:
116 // bsdiff_allowed = (path != "/foo/bar")
Don Garrett36e60772012-03-29 10:31:20 -0700117 //
Don Garrett1d787092013-03-11 18:07:28 -0700118 // TODO(dgarrett): chromium-os:15274 connect this test to the command line.
Don Garrett36e60772012-03-29 10:31:20 -0700119 bool bsdiff_allowed = true;
Don Garrettf4b28742012-03-27 20:48:06 -0700120
Don Garrett36e60772012-03-29 10:31:20 -0700121 if (!bsdiff_allowed)
122 LOG(INFO) << "bsdiff blacklisting: " << path;
Don Garrettf4b28742012-03-27 20:48:06 -0700123
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800124 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_path,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700125 new_root + path,
Darin Petkov8e447e02013-04-16 16:23:50 +0200126 chunk_offset,
127 chunk_size,
Don Garrett36e60772012-03-29 10:31:20 -0700128 bsdiff_allowed,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700129 &data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700130 &operation,
131 true));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700132
133 // Write the data
134 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
135 operation.set_data_offset(*data_file_size);
136 operation.set_data_length(data.size());
137 }
138
139 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
140 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700141
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700142 // Now, insert into graph and blocks vector
Andrew de los Reyesef017552010-10-06 17:57:52 -0700143 Vertex::Index vertex = existing_vertex;
144 if (vertex == Vertex::kInvalidIndex) {
145 graph->resize(graph->size() + 1);
146 vertex = graph->size() - 1;
147 }
148 (*graph)[vertex].op = operation;
149 CHECK((*graph)[vertex].op.has_type());
150 (*graph)[vertex].file_name = path;
Darin Petkov8e447e02013-04-16 16:23:50 +0200151 (*graph)[vertex].chunk_offset = chunk_offset;
152 (*graph)[vertex].chunk_size = chunk_size;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700153
Andrew de los Reyesef017552010-10-06 17:57:52 -0700154 if (blocks)
Thieu Le5c7d9752010-12-15 16:09:28 -0800155 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::AddInstallOpToBlocksVector(
156 (*graph)[vertex].op,
157 *graph,
158 vertex,
159 blocks));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700160 return true;
161}
162
163// For each regular file within new_root, creates a node in the graph,
164// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
165// and writes any necessary data to the end of data_fd.
166bool DeltaReadFiles(Graph* graph,
167 vector<Block>* blocks,
168 const string& old_root,
169 const string& new_root,
Darin Petkov8e447e02013-04-16 16:23:50 +0200170 off_t chunk_size,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700171 int data_fd,
172 off_t* data_file_size) {
173 set<ino_t> visited_inodes;
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800174 set<ino_t> visited_src_inodes;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700175 for (FilesystemIterator fs_iter(new_root,
176 utils::SetWithValue<string>("/lost+found"));
177 !fs_iter.IsEnd(); fs_iter.Increment()) {
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800178 // We never diff symlinks (here, we check that dst file is not a symlink).
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700179 if (!S_ISREG(fs_iter.GetStat().st_mode))
180 continue;
181
182 // Make sure we visit each inode only once.
183 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
184 continue;
185 visited_inodes.insert(fs_iter.GetStat().st_ino);
Darin Petkov8e447e02013-04-16 16:23:50 +0200186 off_t dst_size = fs_iter.GetStat().st_size;
187 if (dst_size == 0)
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700188 continue;
189
190 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700191
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800192 // We can't visit each dst image inode more than once, as that would
193 // duplicate work. Here, we avoid visiting each source image inode
194 // more than once. Technically, we could have multiple operations
195 // that read the same blocks from the source image for diffing, but
196 // we choose not to to avoid complexity. Eventually we will move away
197 // from using a graph/cycle detection/etc to generate diffs, and at that
198 // time, it will be easy (non-complex) to have many operations read
199 // from the same source blocks. At that time, this code can die. -adlr
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800200 bool should_diff_from_source = false;
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800201 string src_path = old_root + fs_iter.GetPartialPath();
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800202 struct stat src_stbuf;
203 // We never diff symlinks (here, we check that src file is not a symlink).
204 if (0 == lstat(src_path.c_str(), &src_stbuf) &&
205 S_ISREG(src_stbuf.st_mode)) {
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800206 should_diff_from_source = !utils::SetContainsKey(visited_src_inodes,
207 src_stbuf.st_ino);
208 visited_src_inodes.insert(src_stbuf.st_ino);
209 }
210
Darin Petkov8e447e02013-04-16 16:23:50 +0200211 off_t size = chunk_size == -1 ? dst_size : chunk_size;
212 off_t step = size;
213 for (off_t offset = 0; offset < dst_size; offset += step) {
214 if (offset + size >= dst_size) {
215 size = -1; // Read through the end of the file.
216 }
217 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
218 Vertex::kInvalidIndex,
219 blocks,
220 (should_diff_from_source ?
221 old_root :
222 kNonexistentPath),
223 new_root,
224 fs_iter.GetPartialPath(),
225 offset,
226 size,
227 data_fd,
228 data_file_size));
229 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700230 }
231 return true;
232}
233
Andrew de los Reyesef017552010-10-06 17:57:52 -0700234// This class allocates non-existent temp blocks, starting from
235// kTempBlockStart. Other code is responsible for converting these
236// temp blocks into real blocks, as the client can't read or write to
237// these blocks.
238class DummyExtentAllocator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700239 public:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700240 explicit DummyExtentAllocator()
241 : next_block_(kTempBlockStart) {}
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700242 vector<Extent> Allocate(const uint64_t block_count) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700243 vector<Extent> ret(1);
244 ret[0].set_start_block(next_block_);
245 ret[0].set_num_blocks(block_count);
246 next_block_ += block_count;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700247 return ret;
248 }
249 private:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700250 uint64_t next_block_;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700251};
252
253// Reads blocks from image_path that are not yet marked as being written
254// in the blocks array. These blocks that remain are non-file-data blocks.
255// In the future we might consider intelligent diffing between this data
256// and data in the previous image, but for now we just bzip2 compress it
257// and include it in the update.
258// Creates a new node in the graph to write these blocks and writes the
259// appropriate blob to blobs_fd. Reads and updates blobs_length;
260bool ReadUnwrittenBlocks(const vector<Block>& blocks,
261 int blobs_fd,
262 off_t* blobs_length,
263 const string& image_path,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700264 Vertex* vertex) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700265 vertex->file_name = "<rootfs-non-file-data>";
266
Andrew de los Reyesef017552010-10-06 17:57:52 -0700267 DeltaArchiveManifest_InstallOperation* out_op = &vertex->op;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700268 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
269 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
270 ScopedFdCloser image_fd_closer(&image_fd);
271
272 string temp_file_path;
273 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX",
274 &temp_file_path,
275 NULL));
276
277 FILE* file = fopen(temp_file_path.c_str(), "w");
278 TEST_AND_RETURN_FALSE(file);
279 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700280
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700281 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
282 file,
283 9, // max compression
284 0, // verbosity
285 0); // default work factor
286 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700287
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700288 vector<Extent> extents;
289 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700290
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700291 LOG(INFO) << "Appending left over blocks to extents";
292 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
293 if (blocks[i].writer != Vertex::kInvalidIndex)
294 continue;
Andrew de los Reyesef017552010-10-06 17:57:52 -0700295 if (blocks[i].reader != Vertex::kInvalidIndex) {
296 graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i);
297 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700298 graph_utils::AppendBlockToExtents(&extents, i);
299 block_count++;
300 }
301
302 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
303 // so we arbitrarily set it to 1024 * kBlockSize.
304 vector<char> buf(1024 * kBlockSize);
305
306 LOG(INFO) << "Reading left over blocks";
307 vector<Block>::size_type blocks_copied_count = 0;
308
309 // For each extent in extents, write the data into BZ2_bzWrite which
310 // sends it to an output file.
311 // We use the temporary buffer 'buf' to hold the data, which may be
312 // smaller than the extent, so in that case we have to loop to get
313 // the extent's data (that's the inner while loop).
314 for (vector<Extent>::const_iterator it = extents.begin();
315 it != extents.end(); ++it) {
316 vector<Block>::size_type blocks_read = 0;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800317 float printed_progress = -1;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700318 while (blocks_read < it->num_blocks()) {
319 const int copy_block_cnt =
320 min(buf.size() / kBlockSize,
321 static_cast<vector<char>::size_type>(
322 it->num_blocks() - blocks_read));
323 ssize_t rc = pread(image_fd,
324 &buf[0],
325 copy_block_cnt * kBlockSize,
326 (it->start_block() + blocks_read) * kBlockSize);
327 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
328 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
329 copy_block_cnt * kBlockSize);
330 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
331 TEST_AND_RETURN_FALSE(err == BZ_OK);
332 blocks_read += copy_block_cnt;
333 blocks_copied_count += copy_block_cnt;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800334 float current_progress =
335 static_cast<float>(blocks_copied_count) / block_count;
336 if (printed_progress + 0.1 < current_progress ||
337 blocks_copied_count == block_count) {
338 LOG(INFO) << "progress: " << current_progress;
339 printed_progress = current_progress;
340 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700341 }
342 }
343 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
344 TEST_AND_RETURN_FALSE(err == BZ_OK);
345 bz_file = NULL;
346 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
347 file = NULL;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700348
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700349 vector<char> compressed_data;
350 LOG(INFO) << "Reading compressed data off disk";
351 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
352 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700353
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700354 // Add node to graph to write these blocks
355 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
356 out_op->set_data_offset(*blobs_length);
357 out_op->set_data_length(compressed_data.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700358 LOG(INFO) << "Rootfs non-data blocks compressed take up "
359 << compressed_data.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700360 *blobs_length += compressed_data.size();
361 out_op->set_dst_length(kBlockSize * block_count);
362 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700363
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700364 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
365 &compressed_data[0],
366 compressed_data.size()));
367 LOG(INFO) << "done with extra blocks";
368 return true;
369}
370
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700371// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700372// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700373bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
374 uint64_t value_be = htobe64(value);
Don Garrette410e0f2011-11-10 15:39:01 -0800375 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700376 return true;
377}
378
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700379// Adds each operation from |graph| to |out_manifest| in the order specified by
380// |order| while building |out_op_name_map| with operation to name
381// mappings. Adds all |kernel_ops| to |out_manifest|. Filters out no-op
382// operations.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700383void InstallOperationsToManifest(
384 const Graph& graph,
385 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700386 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700387 DeltaArchiveManifest* out_manifest,
388 OperationNameMap* out_op_name_map) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700389 for (vector<Vertex::Index>::const_iterator it = order.begin();
390 it != order.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700391 const Vertex& vertex = graph[*it];
392 const DeltaArchiveManifest_InstallOperation& add_op = vertex.op;
393 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
394 continue;
395 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700396 DeltaArchiveManifest_InstallOperation* op =
397 out_manifest->add_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700398 *op = add_op;
Darin Petkov8e447e02013-04-16 16:23:50 +0200399 string name = vertex.file_name;
400 if (vertex.chunk_offset || vertex.chunk_size != -1) {
401 string offset = base::Int64ToString(vertex.chunk_offset);
402 if (vertex.chunk_size != -1) {
403 name += " [" + offset + ", " +
404 base::Int64ToString(vertex.chunk_offset + vertex.chunk_size - 1) +
405 "]";
406 } else {
407 name += " [" + offset + ", end]";
408 }
409 }
410 (*out_op_name_map)[op] = name;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700411 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700412 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
413 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700414 const DeltaArchiveManifest_InstallOperation& add_op = *it;
415 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
416 continue;
417 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700418 DeltaArchiveManifest_InstallOperation* op =
419 out_manifest->add_kernel_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700420 *op = add_op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700421 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700422}
423
424void CheckGraph(const Graph& graph) {
425 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
426 CHECK(it->op.has_type());
427 }
428}
429
Darin Petkov68c10d12010-10-14 09:24:37 -0700430// Delta compresses a kernel partition |new_kernel_part| with knowledge of the
431// old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty
432// string, generates a full update of the partition.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700433bool DeltaCompressKernelPartition(
434 const string& old_kernel_part,
435 const string& new_kernel_part,
436 vector<DeltaArchiveManifest_InstallOperation>* ops,
437 int blobs_fd,
438 off_t* blobs_length) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700439 LOG(INFO) << "Delta compressing kernel partition...";
Darin Petkov68c10d12010-10-14 09:24:37 -0700440 LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update...";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700441
442 // Add a new install operation
443 ops->resize(1);
444 DeltaArchiveManifest_InstallOperation* op = &(*ops)[0];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700445
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700446 vector<char> data;
Don Garrett36e60772012-03-29 10:31:20 -0700447 TEST_AND_RETURN_FALSE(
448 DeltaDiffGenerator::ReadFileToDiff(old_kernel_part,
449 new_kernel_part,
Darin Petkov8e447e02013-04-16 16:23:50 +0200450 0, // chunk_offset
451 -1, // chunk_size
Don Garrett36e60772012-03-29 10:31:20 -0700452 true, // bsdiff_allowed
453 &data,
454 op,
455 false));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700456
Darin Petkov68c10d12010-10-14 09:24:37 -0700457 // Write the data
458 if (op->type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
459 op->set_data_offset(*blobs_length);
460 op->set_data_length(data.size());
461 }
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700462
Darin Petkov68c10d12010-10-14 09:24:37 -0700463 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
464 *blobs_length += data.size();
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700465
Darin Petkov68c10d12010-10-14 09:24:37 -0700466 LOG(INFO) << "Done delta compressing kernel partition: "
467 << kInstallOperationTypes[op->type()];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700468 return true;
469}
470
Darin Petkov880335c2010-10-01 15:52:53 -0700471struct DeltaObject {
472 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
473 : name(in_name),
474 type(in_type),
475 size(in_size) {}
476 bool operator <(const DeltaObject& object) const {
Darin Petkovd43d6902010-10-14 11:17:50 -0700477 return (size != object.size) ? (size < object.size) : (name < object.name);
Darin Petkov880335c2010-10-01 15:52:53 -0700478 }
479 string name;
480 int type;
481 off_t size;
482};
483
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700484void ReportPayloadUsage(const DeltaArchiveManifest& manifest,
485 const int64_t manifest_metadata_size,
486 const OperationNameMap& op_name_map) {
Darin Petkov880335c2010-10-01 15:52:53 -0700487 vector<DeltaObject> objects;
488 off_t total_size = 0;
489
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700490 // Rootfs install operations.
491 for (int i = 0; i < manifest.install_operations_size(); ++i) {
492 const DeltaArchiveManifest_InstallOperation& op =
493 manifest.install_operations(i);
Darin Petkov8e447e02013-04-16 16:23:50 +0200494 objects.push_back(DeltaObject(op_name_map.find(&op)->second,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700495 op.type(),
496 op.data_length()));
497 total_size += op.data_length();
Darin Petkov880335c2010-10-01 15:52:53 -0700498 }
499
Darin Petkov880335c2010-10-01 15:52:53 -0700500 // Kernel install operations.
501 for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) {
502 const DeltaArchiveManifest_InstallOperation& op =
503 manifest.kernel_install_operations(i);
504 objects.push_back(DeltaObject(StringPrintf("<kernel-operation-%d>", i),
505 op.type(),
506 op.data_length()));
507 total_size += op.data_length();
508 }
509
Darin Petkov95cf01f2010-10-12 14:59:13 -0700510 objects.push_back(DeltaObject("<manifest-metadata>",
511 -1,
512 manifest_metadata_size));
513 total_size += manifest_metadata_size;
514
Darin Petkov880335c2010-10-01 15:52:53 -0700515 std::sort(objects.begin(), objects.end());
516
517 static const char kFormatString[] = "%6.2f%% %10llu %-10s %s\n";
518 for (vector<DeltaObject>::const_iterator it = objects.begin();
519 it != objects.end(); ++it) {
520 const DeltaObject& object = *it;
521 fprintf(stderr, kFormatString,
522 object.size * 100.0 / total_size,
523 object.size,
Darin Petkov95cf01f2010-10-12 14:59:13 -0700524 object.type >= 0 ? kInstallOperationTypes[object.type] : "-",
Darin Petkov880335c2010-10-01 15:52:53 -0700525 object.name.c_str());
526 }
527 fprintf(stderr, kFormatString, 100.0, total_size, "", "<total>");
528}
529
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700530} // namespace {}
531
532bool DeltaDiffGenerator::ReadFileToDiff(
533 const string& old_filename,
534 const string& new_filename,
Darin Petkov8e447e02013-04-16 16:23:50 +0200535 off_t chunk_offset,
536 off_t chunk_size,
Don Garrett36e60772012-03-29 10:31:20 -0700537 bool bsdiff_allowed,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700538 vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700539 DeltaArchiveManifest_InstallOperation* out_op,
540 bool gather_extents) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700541 // Read new data in
542 vector<char> new_data;
Darin Petkov8e447e02013-04-16 16:23:50 +0200543 TEST_AND_RETURN_FALSE(
544 utils::ReadFileChunk(new_filename, chunk_offset, chunk_size, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700545
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700546 TEST_AND_RETURN_FALSE(!new_data.empty());
Darin Petkov8e447e02013-04-16 16:23:50 +0200547 TEST_AND_RETURN_FALSE(chunk_size == -1 ||
548 static_cast<off_t>(new_data.size()) <= chunk_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700549
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700550 vector<char> new_data_bz;
551 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
552 CHECK(!new_data_bz.empty());
553
554 vector<char> data; // Data blob that will be written to delta file.
555
556 DeltaArchiveManifest_InstallOperation operation;
557 size_t current_best_size = 0;
558 if (new_data.size() <= new_data_bz.size()) {
559 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
560 current_best_size = new_data.size();
561 data = new_data;
562 } else {
563 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
564 current_best_size = new_data_bz.size();
565 data = new_data_bz;
566 }
567
568 // Do we have an original file to consider?
569 struct stat old_stbuf;
Don Garrettf4b28742012-03-27 20:48:06 -0700570 bool original = !old_filename.empty();
571 if (original && 0 != stat(old_filename.c_str(), &old_stbuf)) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700572 // If stat-ing the old file fails, it should be because it doesn't exist.
573 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
Don Garrettf4b28742012-03-27 20:48:06 -0700574 original = false;
Darin Petkov68c10d12010-10-14 09:24:37 -0700575 }
Don Garrettf4b28742012-03-27 20:48:06 -0700576
Darin Petkov8e447e02013-04-16 16:23:50 +0200577 vector<char> old_data;
Don Garrettf4b28742012-03-27 20:48:06 -0700578 if (original) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700579 // Read old data
Darin Petkov8e447e02013-04-16 16:23:50 +0200580 TEST_AND_RETURN_FALSE(
581 utils::ReadFileChunk(
582 old_filename, chunk_offset, chunk_size, &old_data));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700583 if (old_data == new_data) {
584 // No change in data.
585 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
586 current_best_size = 0;
587 data.clear();
Darin Petkov8e447e02013-04-16 16:23:50 +0200588 } else if (!old_data.empty() && bsdiff_allowed) {
Don Garrett36e60772012-03-29 10:31:20 -0700589 // If the source file is considered bsdiff safe (no bsdiff bugs
590 // triggered), see if BSDIFF encoding is smaller.
Darin Petkov8e447e02013-04-16 16:23:50 +0200591 FilePath old_chunk;
592 TEST_AND_RETURN_FALSE(file_util::CreateTemporaryFile(&old_chunk));
593 ScopedPathUnlinker old_unlinker(old_chunk.value());
594 TEST_AND_RETURN_FALSE(
595 utils::WriteFile(old_chunk.value().c_str(),
596 &old_data[0], old_data.size()));
597 FilePath new_chunk;
598 TEST_AND_RETURN_FALSE(file_util::CreateTemporaryFile(&new_chunk));
599 ScopedPathUnlinker new_unlinker(new_chunk.value());
600 TEST_AND_RETURN_FALSE(
601 utils::WriteFile(new_chunk.value().c_str(),
602 &new_data[0], new_data.size()));
603
Don Garrett36e60772012-03-29 10:31:20 -0700604 vector<char> bsdiff_delta;
605 TEST_AND_RETURN_FALSE(
Darin Petkov8e447e02013-04-16 16:23:50 +0200606 BsdiffFiles(old_chunk.value(), new_chunk.value(), &bsdiff_delta));
Don Garrett36e60772012-03-29 10:31:20 -0700607 CHECK_GT(bsdiff_delta.size(), static_cast<vector<char>::size_type>(0));
608 if (bsdiff_delta.size() < current_best_size) {
609 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
610 current_best_size = bsdiff_delta.size();
611 data = bsdiff_delta;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700612 }
613 }
614 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700615
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700616 // Set parameters of the operations
617 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700618
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700619 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
620 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Darin Petkov68c10d12010-10-14 09:24:37 -0700621 if (gather_extents) {
622 TEST_AND_RETURN_FALSE(
Darin Petkov8e447e02013-04-16 16:23:50 +0200623 GatherExtents(old_filename,
624 chunk_offset,
625 chunk_size,
626 operation.mutable_src_extents()));
Darin Petkov68c10d12010-10-14 09:24:37 -0700627 } else {
628 Extent* src_extent = operation.add_src_extents();
629 src_extent->set_start_block(0);
630 src_extent->set_num_blocks(
631 (old_stbuf.st_size + kBlockSize - 1) / kBlockSize);
632 }
Darin Petkov8e447e02013-04-16 16:23:50 +0200633 operation.set_src_length(old_data.size());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700634 }
635
Darin Petkov68c10d12010-10-14 09:24:37 -0700636 if (gather_extents) {
637 TEST_AND_RETURN_FALSE(
Darin Petkov8e447e02013-04-16 16:23:50 +0200638 GatherExtents(new_filename,
639 chunk_offset,
640 chunk_size,
641 operation.mutable_dst_extents()));
Darin Petkov68c10d12010-10-14 09:24:37 -0700642 } else {
643 Extent* dst_extent = operation.add_dst_extents();
644 dst_extent->set_start_block(0);
645 dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize);
646 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700647 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700648
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700649 out_data->swap(data);
650 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700651
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700652 return true;
653}
654
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700655bool DeltaDiffGenerator::InitializePartitionInfo(bool is_kernel,
656 const string& partition,
657 PartitionInfo* info) {
Darin Petkov7ea32332010-10-13 10:46:11 -0700658 int64_t size = 0;
659 if (is_kernel) {
660 size = utils::FileSize(partition);
661 } else {
662 int block_count = 0, block_size = 0;
663 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition,
664 &block_count,
665 &block_size));
666 size = static_cast<int64_t>(block_count) * block_size;
667 }
668 TEST_AND_RETURN_FALSE(size > 0);
Darin Petkov36a58222010-10-07 22:00:09 -0700669 info->set_size(size);
670 OmahaHashCalculator hasher;
Darin Petkov7ea32332010-10-13 10:46:11 -0700671 TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size);
Darin Petkov36a58222010-10-07 22:00:09 -0700672 TEST_AND_RETURN_FALSE(hasher.Finalize());
673 const vector<char>& hash = hasher.raw_hash();
674 info->set_hash(hash.data(), hash.size());
Darin Petkovd43d6902010-10-14 11:17:50 -0700675 LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash();
Darin Petkov36a58222010-10-07 22:00:09 -0700676 return true;
677}
678
679bool InitializePartitionInfos(const string& old_kernel,
680 const string& new_kernel,
681 const string& old_rootfs,
682 const string& new_rootfs,
683 DeltaArchiveManifest* manifest) {
Darin Petkovd43d6902010-10-14 11:17:50 -0700684 if (!old_kernel.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700685 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
686 true,
687 old_kernel,
688 manifest->mutable_old_kernel_info()));
Darin Petkovd43d6902010-10-14 11:17:50 -0700689 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700690 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
691 true,
692 new_kernel,
693 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700694 if (!old_rootfs.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700695 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
696 false,
697 old_rootfs,
698 manifest->mutable_old_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700699 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700700 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
701 false,
702 new_rootfs,
703 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700704 return true;
705}
706
Andrew de los Reyesef017552010-10-06 17:57:52 -0700707namespace {
708
709// Takes a collection (vector or RepeatedPtrField) of Extent and
710// returns a vector of the blocks referenced, in order.
711template<typename T>
712vector<uint64_t> ExpandExtents(const T& extents) {
713 vector<uint64_t> ret;
714 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
715 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700716 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700717 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700718 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700719 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700720 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700721 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700722 }
723 }
724 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700725 return ret;
726}
727
728// Takes a vector of blocks and returns an equivalent vector of Extent
729// objects.
730vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
731 vector<Extent> new_extents;
732 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
733 it != e; ++it) {
734 graph_utils::AppendBlockToExtents(&new_extents, *it);
735 }
736 return new_extents;
737}
738
739} // namespace {}
740
741void DeltaDiffGenerator::SubstituteBlocks(
742 Vertex* vertex,
743 const vector<Extent>& remove_extents,
744 const vector<Extent>& replace_extents) {
745 // First, expand out the blocks that op reads from
746 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700747 {
748 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700749 vector<uint64_t> remove_extents_expanded =
750 ExpandExtents(remove_extents);
751 vector<uint64_t> replace_extents_expanded =
752 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700753 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700754 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700755 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700756 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700757 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
758 }
759 utils::ApplyMap(&read_blocks, conversion);
760 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
761 e = vertex->out_edges.end(); it != e; ++it) {
762 vector<uint64_t> write_before_deps_expanded =
763 ExpandExtents(it->second.write_extents);
764 utils::ApplyMap(&write_before_deps_expanded, conversion);
765 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700766 }
767 }
768 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700769 vertex->op.clear_src_extents();
770 vector<Extent> new_extents = CompressExtents(read_blocks);
771 DeltaDiffGenerator::StoreExtents(new_extents,
772 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700773}
774
775bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700776 const set<Edge>& edges,
777 vector<CutEdgeVertexes>* out_cuts) {
778 DummyExtentAllocator scratch_allocator;
779 vector<CutEdgeVertexes> cuts;
780 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700781
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700782 uint64_t scratch_blocks_used = 0;
783 for (set<Edge>::const_iterator it = edges.begin();
784 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700785 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700786 vector<Extent> old_extents =
787 (*graph)[it->first].out_edges[it->second].extents;
788 // Choose some scratch space
789 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700790 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700791 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
792 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700793 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700794 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700795 cuts.back().old_src = it->first;
796 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700797
Andrew de los Reyesef017552010-10-06 17:57:52 -0700798 EdgeProperties& cut_edge_properties =
799 (*graph)[it->first].out_edges.find(it->second)->second;
800
801 // This should never happen, as we should only be cutting edges between
802 // real file nodes, and write-before relationships are created from
803 // a real file node to a temp copy node:
804 CHECK(cut_edge_properties.write_extents.empty())
805 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700806
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700807 // make node depend on the copy operation
808 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700809 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700810
811 // Set src/dst extents and other proto variables for copy operation
812 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
813 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700814 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700815 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700816 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700817 graph->back().op.mutable_dst_extents());
818 graph->back().op.set_src_length(
819 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
820 graph->back().op.set_dst_length(graph->back().op.src_length());
821
822 // make the dest node read from the scratch space
823 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700824 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700825 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700826 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700827
828 // delete the old edge
Mike Frysinger0f9547d2012-02-16 12:11:37 -0500829 CHECK_EQ(static_cast<Graph::size_type>(1),
830 (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700831
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700832 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700833 EdgeProperties write_before_edge_properties;
834 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
835 (*graph)[it->second].out_edges.insert(
836 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700837 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700838 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700839 return true;
840}
841
842// Stores all Extents in 'extents' into 'out'.
843void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700844 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700845 google::protobuf::RepeatedPtrField<Extent>* out) {
846 for (vector<Extent>::const_iterator it = extents.begin();
847 it != extents.end(); ++it) {
848 Extent* new_extent = out->Add();
849 *new_extent = *it;
850 }
851}
852
853// Creates all the edges for the graph. Writers of a block point to
854// readers of the same block. This is because for an edge A->B, B
855// must complete before A executes.
856void DeltaDiffGenerator::CreateEdges(Graph* graph,
857 const vector<Block>& blocks) {
858 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
859 // Blocks with both a reader and writer get an edge
860 if (blocks[i].reader == Vertex::kInvalidIndex ||
861 blocks[i].writer == Vertex::kInvalidIndex)
862 continue;
863 // Don't have a node depend on itself
864 if (blocks[i].reader == blocks[i].writer)
865 continue;
866 // See if there's already an edge we can add onto
867 Vertex::EdgeMap::iterator edge_it =
868 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
869 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
870 // No existing edge. Create one
871 (*graph)[blocks[i].writer].out_edges.insert(
872 make_pair(blocks[i].reader, EdgeProperties()));
873 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700874 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700875 }
876 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
877 }
878}
879
Andrew de los Reyesef017552010-10-06 17:57:52 -0700880namespace {
881
882class SortCutsByTopoOrderLess {
883 public:
884 SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table)
885 : table_(table) {}
886 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
887 return table_[a.old_dst] < table_[b.old_dst];
888 }
889 private:
890 vector<vector<Vertex::Index>::size_type>& table_;
891};
892
893} // namespace {}
894
895void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
896 vector<Vertex::Index>& op_indexes,
897 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
898 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
899 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
900 i != e; ++i) {
901 Vertex::Index node = op_indexes[i];
902 if (table.size() < (node + 1)) {
903 table.resize(node + 1);
904 }
905 table[node] = i;
906 }
907 reverse_op_indexes->swap(table);
908}
909
910void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes,
911 vector<CutEdgeVertexes>* cuts) {
912 // first, make a reverse lookup table.
913 vector<vector<Vertex::Index>::size_type> table;
914 GenerateReverseTopoOrderMap(op_indexes, &table);
915 SortCutsByTopoOrderLess less(table);
916 sort(cuts->begin(), cuts->end(), less);
917}
918
919void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
920 vector<Vertex::Index>* op_indexes) {
921 vector<Vertex::Index> ret;
922 vector<Vertex::Index> full_ops;
923 ret.reserve(op_indexes->size());
924 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
925 ++i) {
926 DeltaArchiveManifest_InstallOperation_Type type =
927 (*graph)[(*op_indexes)[i]].op.type();
928 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
929 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
930 full_ops.push_back((*op_indexes)[i]);
931 } else {
932 ret.push_back((*op_indexes)[i]);
933 }
934 }
935 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
936 << (full_ops.size() + ret.size()) << " total ops.";
937 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
938 op_indexes->swap(ret);
939}
940
941namespace {
942
943template<typename T>
944bool TempBlocksExistInExtents(const T& extents) {
945 for (int i = 0, e = extents.size(); i < e; ++i) {
946 Extent extent = graph_utils::GetElement(extents, i);
947 uint64_t start = extent.start_block();
948 uint64_t num = extent.num_blocks();
949 if (start == kSparseHole)
950 continue;
951 if (start >= kTempBlockStart ||
952 (start + num) >= kTempBlockStart) {
953 LOG(ERROR) << "temp block!";
954 LOG(ERROR) << "start: " << start << ", num: " << num;
955 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
956 LOG(ERROR) << "returning true";
957 return true;
958 }
959 // check for wrap-around, which would be a bug:
960 CHECK(start <= (start + num));
961 }
962 return false;
963}
964
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700965// Convertes the cuts, which must all have the same |old_dst| member,
966// to full. It does this by converting the |old_dst| to REPLACE or
967// REPLACE_BZ, dropping all incoming edges to |old_dst|, and marking
968// all temp nodes invalid.
969bool ConvertCutsToFull(
970 Graph* graph,
971 const string& new_root,
972 int data_fd,
973 off_t* data_file_size,
974 vector<Vertex::Index>* op_indexes,
975 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
976 const vector<CutEdgeVertexes>& cuts) {
977 CHECK(!cuts.empty());
978 set<Vertex::Index> deleted_nodes;
979 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
980 e = cuts.end(); it != e; ++it) {
981 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp(
982 graph,
983 *it,
984 new_root,
985 data_fd,
986 data_file_size));
987 deleted_nodes.insert(it->new_vertex);
988 }
989 deleted_nodes.insert(cuts[0].old_dst);
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700990
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700991 vector<Vertex::Index> new_op_indexes;
992 new_op_indexes.reserve(op_indexes->size());
993 for (vector<Vertex::Index>::iterator it = op_indexes->begin(),
994 e = op_indexes->end(); it != e; ++it) {
995 if (utils::SetContainsKey(deleted_nodes, *it))
996 continue;
997 new_op_indexes.push_back(*it);
998 }
999 new_op_indexes.push_back(cuts[0].old_dst);
1000 op_indexes->swap(new_op_indexes);
1001 DeltaDiffGenerator::GenerateReverseTopoOrderMap(*op_indexes,
1002 reverse_op_indexes);
1003 return true;
1004}
1005
1006// Tries to assign temp blocks for a collection of cuts, all of which share
1007// the same old_dst member. If temp blocks can't be found, old_dst will be
1008// converted to a REPLACE or REPLACE_BZ operation. Returns true on success,
1009// which can happen even if blocks are converted to full. Returns false
1010// on exceptional error cases.
1011bool AssignBlockForAdjoiningCuts(
1012 Graph* graph,
1013 const string& new_root,
1014 int data_fd,
1015 off_t* data_file_size,
1016 vector<Vertex::Index>* op_indexes,
1017 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
1018 const vector<CutEdgeVertexes>& cuts) {
1019 CHECK(!cuts.empty());
1020 const Vertex::Index old_dst = cuts[0].old_dst;
1021 // Calculate # of blocks needed
1022 uint64_t blocks_needed = 0;
1023 map<const CutEdgeVertexes*, uint64_t> cuts_blocks_needed;
1024 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1025 e = cuts.end(); it != e; ++it) {
1026 uint64_t cut_blocks_needed = 0;
1027 for (vector<Extent>::const_iterator jt = it->tmp_extents.begin(),
1028 je = it->tmp_extents.end(); jt != je; ++jt) {
1029 cut_blocks_needed += jt->num_blocks();
1030 }
1031 blocks_needed += cut_blocks_needed;
1032 cuts_blocks_needed[&*it] = cut_blocks_needed;
1033 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001034
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001035 // Find enough blocks
1036 ExtentRanges scratch_ranges;
1037 // Each block that's supplying temp blocks and the corresponding blocks:
1038 typedef vector<pair<Vertex::Index, ExtentRanges> > SupplierVector;
1039 SupplierVector block_suppliers;
1040 uint64_t scratch_blocks_found = 0;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001041 for (vector<Vertex::Index>::size_type i = (*reverse_op_indexes)[old_dst] + 1,
1042 e = op_indexes->size(); i < e; ++i) {
1043 Vertex::Index test_node = (*op_indexes)[i];
1044 if (!(*graph)[test_node].valid)
1045 continue;
1046 // See if this node has sufficient blocks
1047 ExtentRanges ranges;
1048 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
1049 ranges.SubtractExtent(ExtentForRange(
1050 kTempBlockStart, kSparseHole - kTempBlockStart));
1051 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
1052 // For now, for simplicity, subtract out all blocks in read-before
1053 // dependencies.
1054 for (Vertex::EdgeMap::const_iterator edge_i =
1055 (*graph)[test_node].out_edges.begin(),
1056 edge_e = (*graph)[test_node].out_edges.end();
1057 edge_i != edge_e; ++edge_i) {
1058 ranges.SubtractExtents(edge_i->second.extents);
1059 }
1060 if (ranges.blocks() == 0)
1061 continue;
1062
1063 if (ranges.blocks() + scratch_blocks_found > blocks_needed) {
1064 // trim down ranges
1065 vector<Extent> new_ranges = ranges.GetExtentsForBlockCount(
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001066 blocks_needed - scratch_blocks_found);
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001067 ranges = ExtentRanges();
1068 ranges.AddExtents(new_ranges);
1069 }
1070 scratch_ranges.AddRanges(ranges);
1071 block_suppliers.push_back(make_pair(test_node, ranges));
1072 scratch_blocks_found += ranges.blocks();
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001073 if (scratch_ranges.blocks() >= blocks_needed)
1074 break;
1075 }
1076 if (scratch_ranges.blocks() < blocks_needed) {
1077 LOG(INFO) << "Unable to find sufficient scratch";
1078 TEST_AND_RETURN_FALSE(ConvertCutsToFull(graph,
1079 new_root,
1080 data_fd,
1081 data_file_size,
1082 op_indexes,
1083 reverse_op_indexes,
1084 cuts));
1085 return true;
1086 }
1087 // Use the scratch we found
1088 TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found);
1089
1090 // Make all the suppliers depend on this node
1091 for (SupplierVector::iterator it = block_suppliers.begin(),
1092 e = block_suppliers.end(); it != e; ++it) {
1093 graph_utils::AddReadBeforeDepExtents(
1094 &(*graph)[it->first],
1095 old_dst,
1096 it->second.GetExtentsForBlockCount(it->second.blocks()));
1097 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001098
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001099 // Replace temp blocks in each cut
1100 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1101 e = cuts.end(); it != e; ++it) {
1102 vector<Extent> real_extents =
1103 scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]);
1104 scratch_ranges.SubtractExtents(real_extents);
1105
1106 // Fix the old dest node w/ the real blocks
1107 DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst],
1108 it->tmp_extents,
1109 real_extents);
1110
1111 // Fix the new node w/ the real blocks. Since the new node is just a
1112 // copy operation, we can replace all the dest extents w/ the real
1113 // blocks.
1114 DeltaArchiveManifest_InstallOperation *op =
1115 &(*graph)[it->new_vertex].op;
1116 op->clear_dst_extents();
1117 DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents());
1118 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001119 return true;
1120}
1121
Andrew de los Reyesef017552010-10-06 17:57:52 -07001122} // namespace {}
1123
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001124// Returns true if |op| is a no-op operation that doesn't do any useful work
1125// (e.g., a move operation that copies blocks onto themselves).
1126bool DeltaDiffGenerator::IsNoopOperation(
1127 const DeltaArchiveManifest_InstallOperation& op) {
1128 return (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE &&
1129 ExpandExtents(op.src_extents()) == ExpandExtents(op.dst_extents()));
1130}
1131
Andrew de los Reyesef017552010-10-06 17:57:52 -07001132bool DeltaDiffGenerator::AssignTempBlocks(
1133 Graph* graph,
1134 const string& new_root,
1135 int data_fd,
1136 off_t* data_file_size,
1137 vector<Vertex::Index>* op_indexes,
1138 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001139 const vector<CutEdgeVertexes>& cuts) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001140 CHECK(!cuts.empty());
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001141
1142 // group of cuts w/ the same old_dst:
1143 vector<CutEdgeVertexes> cuts_group;
1144
Andrew de los Reyesef017552010-10-06 17:57:52 -07001145 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
1146 true ; --i) {
1147 LOG(INFO) << "Fixing temp blocks in cut " << i
1148 << ": old dst: " << cuts[i].old_dst << " new vertex: "
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001149 << cuts[i].new_vertex << " path: "
1150 << (*graph)[cuts[i].old_dst].file_name;
1151
1152 if (cuts_group.empty() || (cuts_group[0].old_dst == cuts[i].old_dst)) {
1153 cuts_group.push_back(cuts[i]);
1154 } else {
1155 CHECK(!cuts_group.empty());
1156 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1157 new_root,
1158 data_fd,
1159 data_file_size,
1160 op_indexes,
1161 reverse_op_indexes,
1162 cuts_group));
1163 cuts_group.clear();
1164 cuts_group.push_back(cuts[i]);
Andrew de los Reyesef017552010-10-06 17:57:52 -07001165 }
Darin Petkov36a58222010-10-07 22:00:09 -07001166
Andrew de los Reyesef017552010-10-06 17:57:52 -07001167 if (i == e) {
1168 // break out of for() loop
1169 break;
1170 }
1171 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001172 CHECK(!cuts_group.empty());
1173 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1174 new_root,
1175 data_fd,
1176 data_file_size,
1177 op_indexes,
1178 reverse_op_indexes,
1179 cuts_group));
Andrew de los Reyesef017552010-10-06 17:57:52 -07001180 return true;
1181}
1182
1183bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1184 size_t idx = 0;
1185 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1186 ++it, ++idx) {
1187 if (!it->valid)
1188 continue;
1189 const DeltaArchiveManifest_InstallOperation& op = it->op;
1190 if (TempBlocksExistInExtents(op.dst_extents()) ||
1191 TempBlocksExistInExtents(op.src_extents())) {
1192 LOG(INFO) << "bad extents in node " << idx;
1193 LOG(INFO) << "so yeah";
1194 return false;
1195 }
1196
1197 // Check out-edges:
1198 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1199 je = it->out_edges.end(); jt != je; ++jt) {
1200 if (TempBlocksExistInExtents(jt->second.extents) ||
1201 TempBlocksExistInExtents(jt->second.write_extents)) {
1202 LOG(INFO) << "bad out edge in node " << idx;
1203 LOG(INFO) << "so yeah";
1204 return false;
1205 }
1206 }
1207 }
1208 return true;
1209}
1210
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001211bool DeltaDiffGenerator::ReorderDataBlobs(
1212 DeltaArchiveManifest* manifest,
1213 const std::string& data_blobs_path,
1214 const std::string& new_data_blobs_path) {
1215 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1216 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1217 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001218
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001219 DirectFileWriter writer;
1220 TEST_AND_RETURN_FALSE(
1221 writer.Open(new_data_blobs_path.c_str(),
1222 O_WRONLY | O_TRUNC | O_CREAT,
1223 0644) == 0);
1224 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001225 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001226
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001227 for (int i = 0; i < (manifest->install_operations_size() +
1228 manifest->kernel_install_operations_size()); i++) {
1229 DeltaArchiveManifest_InstallOperation* op = NULL;
1230 if (i < manifest->install_operations_size()) {
1231 op = manifest->mutable_install_operations(i);
1232 } else {
1233 op = manifest->mutable_kernel_install_operations(
1234 i - manifest->install_operations_size());
1235 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001236 if (!op->has_data_offset())
1237 continue;
1238 CHECK(op->has_data_length());
1239 vector<char> buf(op->data_length());
1240 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1241 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1242
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001243 // Add the hash of the data blobs for this operation
1244 TEST_AND_RETURN_FALSE(AddOperationHash(op, buf));
1245
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001246 op->set_data_offset(out_file_size);
Don Garrette410e0f2011-11-10 15:39:01 -08001247 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001248 out_file_size += buf.size();
1249 }
1250 return true;
1251}
1252
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001253bool DeltaDiffGenerator::AddOperationHash(
1254 DeltaArchiveManifest_InstallOperation* op,
1255 const vector<char>& buf) {
1256 OmahaHashCalculator hasher;
1257
1258 TEST_AND_RETURN_FALSE(hasher.Update(&buf[0], buf.size()));
1259 TEST_AND_RETURN_FALSE(hasher.Finalize());
1260
1261 const vector<char>& hash = hasher.raw_hash();
1262 op->set_data_sha256_hash(hash.data(), hash.size());
1263 return true;
1264}
1265
Andrew de los Reyesef017552010-10-06 17:57:52 -07001266bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1267 const CutEdgeVertexes& cut,
1268 const string& new_root,
1269 int data_fd,
1270 off_t* data_file_size) {
1271 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001272
Andrew de los Reyesef017552010-10-06 17:57:52 -07001273 // Keep all outgoing edges
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001274 if ((*graph)[cut.old_dst].op.type() !=
1275 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ &&
1276 (*graph)[cut.old_dst].op.type() !=
1277 DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
1278 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1279 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001280
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001281 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1282 cut.old_dst,
1283 NULL,
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -08001284 kNonexistentPath,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001285 new_root,
1286 (*graph)[cut.old_dst].file_name,
Darin Petkov8e447e02013-04-16 16:23:50 +02001287 (*graph)[cut.old_dst].chunk_offset,
1288 (*graph)[cut.old_dst].chunk_size,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001289 data_fd,
1290 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001291
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001292 (*graph)[cut.old_dst].out_edges = out_edges;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001293
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001294 // Right now we don't have doubly-linked edges, so we have to scan
1295 // the whole graph.
1296 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1297 }
Andrew de los Reyesef017552010-10-06 17:57:52 -07001298
1299 // Delete temp node
1300 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1301 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1302 (*graph)[cut.old_dst].out_edges.end());
1303 (*graph)[cut.new_vertex].valid = false;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001304 LOG(INFO) << "marked node invalid: " << cut.new_vertex;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001305 return true;
1306}
1307
1308bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1309 const string& new_root,
1310 int fd,
1311 off_t* data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001312 vector<Vertex::Index>* final_order,
1313 Vertex::Index scratch_vertex) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001314 CycleBreaker cycle_breaker;
1315 LOG(INFO) << "Finding cycles...";
1316 set<Edge> cut_edges;
1317 cycle_breaker.BreakCycles(*graph, &cut_edges);
1318 LOG(INFO) << "done finding cycles";
1319 CheckGraph(*graph);
1320
1321 // Calculate number of scratch blocks needed
1322
1323 LOG(INFO) << "Cutting cycles...";
1324 vector<CutEdgeVertexes> cuts;
1325 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1326 LOG(INFO) << "done cutting cycles";
1327 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1328 CheckGraph(*graph);
1329
1330 LOG(INFO) << "Creating initial topological order...";
1331 TopologicalSort(*graph, final_order);
1332 LOG(INFO) << "done with initial topo order";
1333 CheckGraph(*graph);
1334
1335 LOG(INFO) << "Moving full ops to the back";
1336 MoveFullOpsToBack(graph, final_order);
1337 LOG(INFO) << "done moving full ops to back";
1338
1339 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1340 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1341
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001342 SortCutsByTopoOrder(*final_order, &cuts);
1343
Andrew de los Reyesef017552010-10-06 17:57:52 -07001344 if (!cuts.empty())
1345 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1346 new_root,
1347 fd,
1348 data_file_size,
1349 final_order,
1350 &inverse_final_order,
1351 cuts));
1352 LOG(INFO) << "Making sure all temp blocks have been allocated";
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001353
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001354 // Remove the scratch node, if any
1355 if (scratch_vertex != Vertex::kInvalidIndex) {
1356 final_order->erase(final_order->begin() +
1357 inverse_final_order[scratch_vertex]);
1358 (*graph)[scratch_vertex].valid = false;
1359 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1360 }
1361
Andrew de los Reyesef017552010-10-06 17:57:52 -07001362 graph_utils::DumpGraph(*graph);
1363 CHECK(NoTempBlocksRemain(*graph));
1364 LOG(INFO) << "done making sure all temp blocks are allocated";
1365 return true;
1366}
1367
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001368void DeltaDiffGenerator::CreateScratchNode(uint64_t start_block,
1369 uint64_t num_blocks,
1370 Vertex* vertex) {
1371 vertex->file_name = "<scratch>";
1372 vertex->op.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
1373 vertex->op.set_data_offset(0);
1374 vertex->op.set_data_length(0);
1375 Extent* extent = vertex->op.add_dst_extents();
1376 extent->set_start_block(start_block);
1377 extent->set_num_blocks(num_blocks);
1378}
1379
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001380bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1381 const string& old_root,
1382 const string& old_image,
1383 const string& new_root,
1384 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001385 const string& old_kernel_part,
1386 const string& new_kernel_part,
1387 const string& output_path,
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001388 const string& private_key_path,
Darin Petkov8e447e02013-04-16 16:23:50 +02001389 off_t chunk_size,
Chris Sosad5ae1562013-04-23 13:20:18 -07001390 size_t rootfs_partition_size,
Don Garrett0dd39852013-04-03 16:55:42 -07001391 const ImageInfo* old_image_info,
1392 const ImageInfo* new_image_info,
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001393 uint64_t* metadata_size) {
Darin Petkov8e447e02013-04-16 16:23:50 +02001394 TEST_AND_RETURN_FALSE(chunk_size == -1 || chunk_size % kBlockSize == 0);
Darin Petkov7ea32332010-10-13 10:46:11 -07001395 int old_image_block_count = 0, old_image_block_size = 0;
1396 int new_image_block_count = 0, new_image_block_size = 0;
1397 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1398 &new_image_block_count,
1399 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001400 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001401 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1402 &old_image_block_count,
1403 &old_image_block_size));
1404 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1405 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1406 << "Old and new images have different block counts.";
Don Garrett0dd39852013-04-03 16:55:42 -07001407
1408 // If new_image_info is present, old_image_info must but be present.
1409 TEST_AND_RETURN_FALSE((bool)old_image_info == (bool)new_image_info);
1410 } else {
1411 // old_image_info must not be present for a full update.
1412 TEST_AND_RETURN_FALSE(!old_image_info);
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001413 }
Chris Sosad5ae1562013-04-23 13:20:18 -07001414
1415 // Sanity checks for the partition size.
1416 TEST_AND_RETURN_FALSE(rootfs_partition_size % kBlockSize == 0);
Chris Sosae9f5f422013-05-17 16:11:10 -07001417 size_t fs_size = static_cast<size_t>(new_image_block_size) *
1418 new_image_block_count;
Chris Sosad5ae1562013-04-23 13:20:18 -07001419 LOG(INFO) << "Rootfs partition size: " << rootfs_partition_size;
1420 LOG(INFO) << "Actual filesystem size: " << fs_size;
1421 TEST_AND_RETURN_FALSE(rootfs_partition_size >= fs_size);
1422
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001423 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001424 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1425
Darin Petkov7ea32332010-10-13 10:46:11 -07001426 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1427 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1428 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001429 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1430 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1431 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1432 }
1433 Graph graph;
1434 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001435
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001436 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
1437 string temp_file_path;
Darin Petkov7438a5c2011-08-29 11:56:44 -07001438 scoped_ptr<ScopedPathUnlinker> temp_file_unlinker;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001439 off_t data_file_size = 0;
1440
1441 LOG(INFO) << "Reading files...";
1442
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001443 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1444
Andrew de los Reyesef017552010-10-06 17:57:52 -07001445 vector<Vertex::Index> final_order;
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001446 Vertex::Index scratch_vertex = Vertex::kInvalidIndex;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001447 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001448 int fd;
1449 TEST_AND_RETURN_FALSE(
1450 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001451 temp_file_unlinker.reset(new ScopedPathUnlinker(temp_file_path));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001452 TEST_AND_RETURN_FALSE(fd >= 0);
1453 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001454 if (!old_image.empty()) {
1455 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001456
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001457 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1458 &blocks,
1459 old_root,
1460 new_root,
Darin Petkov8e447e02013-04-16 16:23:50 +02001461 chunk_size,
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001462 fd,
1463 &data_file_size));
1464 LOG(INFO) << "done reading normal files";
1465 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001466
Thieu Le5c7d9752010-12-15 16:09:28 -08001467 LOG(INFO) << "Starting metadata processing";
1468 TEST_AND_RETURN_FALSE(Metadata::DeltaReadMetadata(&graph,
1469 &blocks,
1470 old_image,
1471 new_image,
1472 fd,
1473 &data_file_size));
1474 LOG(INFO) << "Done metadata processing";
1475 CheckGraph(graph);
1476
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001477 graph.resize(graph.size() + 1);
1478 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1479 fd,
1480 &data_file_size,
1481 new_image,
1482 &graph.back()));
1483
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001484 // Final scratch block (if there's space)
Chris Sosad5ae1562013-04-23 13:20:18 -07001485 if (blocks.size() < (rootfs_partition_size / kBlockSize)) {
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001486 scratch_vertex = graph.size();
1487 graph.resize(graph.size() + 1);
1488 CreateScratchNode(blocks.size(),
Chris Sosad5ae1562013-04-23 13:20:18 -07001489 (rootfs_partition_size / kBlockSize) - blocks.size(),
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001490 &graph.back());
1491 }
1492
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001493 // Read kernel partition
1494 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1495 new_kernel_part,
1496 &kernel_ops,
1497 fd,
1498 &data_file_size));
1499
1500 LOG(INFO) << "done reading kernel";
1501 CheckGraph(graph);
1502
1503 LOG(INFO) << "Creating edges...";
1504 CreateEdges(&graph, blocks);
1505 LOG(INFO) << "Done creating edges";
1506 CheckGraph(graph);
1507
1508 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1509 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001510 fd,
1511 &data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001512 &final_order,
1513 scratch_vertex));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001514 } else {
1515 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001516 off_t new_image_size =
1517 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Darin Petkov7a22d792010-11-08 14:10:00 -08001518 TEST_AND_RETURN_FALSE(FullUpdateGenerator::Run(&graph,
1519 new_kernel_part,
1520 new_image,
1521 new_image_size,
1522 fd,
1523 &data_file_size,
1524 kFullUpdateChunkSize,
1525 kBlockSize,
1526 &kernel_ops,
1527 &final_order));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001528 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001529 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001530
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001531 // Convert to protobuf Manifest object
1532 DeltaArchiveManifest manifest;
Don Garrett0dd39852013-04-03 16:55:42 -07001533
1534 if (old_image_info)
1535 *(manifest.mutable_old_image_info()) = *old_image_info;
1536
1537 if (new_image_info)
1538 *(manifest.mutable_new_image_info()) = *new_image_info;
1539
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001540 OperationNameMap op_name_map;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001541 CheckGraph(graph);
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001542 InstallOperationsToManifest(graph,
1543 final_order,
1544 kernel_ops,
1545 &manifest,
1546 &op_name_map);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001547 CheckGraph(graph);
1548 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001549
1550 // Reorder the data blobs with the newly ordered manifest
1551 string ordered_blobs_path;
1552 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
1553 "/tmp/CrAU_temp_data.ordered.XXXXXX",
1554 &ordered_blobs_path,
Andrew de los Reyese05fc282011-06-02 09:50:08 -07001555 NULL));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001556 ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001557 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1558 temp_file_path,
1559 ordered_blobs_path));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001560 temp_file_unlinker.reset();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001561
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001562 // Check that install op blobs are in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001563 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001564 {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001565 for (int i = 0; i < (manifest.install_operations_size() +
1566 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001567 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001568 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001569 manifest.mutable_install_operations(i) :
1570 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001571 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001572 if (op->has_data_offset()) {
1573 if (op->data_offset() != next_blob_offset) {
1574 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001575 << next_blob_offset;
1576 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001577 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001578 }
1579 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001580 }
1581
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001582 // Signatures appear at the end of the blobs. Note the offset in the
1583 // manifest
1584 if (!private_key_path.empty()) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001585 uint64_t signature_blob_length = 0;
1586 TEST_AND_RETURN_FALSE(
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -07001587 PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path),
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001588 &signature_blob_length));
Darin Petkov9574f7e2011-01-13 10:48:12 -08001589 AddSignatureOp(next_blob_offset, signature_blob_length, &manifest);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001590 }
1591
Darin Petkov36a58222010-10-07 22:00:09 -07001592 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1593 new_kernel_part,
1594 old_image,
1595 new_image,
1596 &manifest));
1597
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001598 // Serialize protobuf
1599 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001600
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001601 CheckGraph(graph);
1602 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1603 CheckGraph(graph);
1604
1605 LOG(INFO) << "Writing final delta file header...";
1606 DirectFileWriter writer;
1607 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1608 O_WRONLY | O_CREAT | O_TRUNC,
1609 0644) == 0);
1610 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001611
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001612 // Write header
Don Garrette410e0f2011-11-10 15:39:01 -08001613 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001614
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001615 // Write version number
1616 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001617
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001618 // Write protobuf length
1619 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1620 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001621
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001622 // Write protobuf
1623 LOG(INFO) << "Writing final delta file protobuf... "
1624 << serialized_manifest.size();
1625 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
Don Garrette410e0f2011-11-10 15:39:01 -08001626 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001627
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001628 // Append the data blobs
1629 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001630 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001631 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1632 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1633 for (;;) {
1634 char buf[kBlockSize];
1635 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1636 if (0 == rc) {
1637 // EOF
1638 break;
1639 }
1640 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
Don Garrette410e0f2011-11-10 15:39:01 -08001641 TEST_AND_RETURN_FALSE(writer.Write(buf, rc));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001642 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001643
1644 // Write signature blob.
1645 if (!private_key_path.empty()) {
1646 LOG(INFO) << "Signing the update...";
1647 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -07001648 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
1649 output_path,
1650 vector<string>(1, private_key_path),
1651 &signature_blob));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001652 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
Don Garrette410e0f2011-11-10 15:39:01 -08001653 signature_blob.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001654 }
1655
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001656 *metadata_size =
Darin Petkov95cf01f2010-10-12 14:59:13 -07001657 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001658 ReportPayloadUsage(manifest, *metadata_size, op_name_map);
Darin Petkov880335c2010-10-01 15:52:53 -07001659
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001660 LOG(INFO) << "All done. Successfully created delta file with "
1661 << "metadata size = " << *metadata_size;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001662 return true;
1663}
1664
Thieu Le5c7d9752010-12-15 16:09:28 -08001665// Runs the bsdiff tool on two files and returns the resulting delta in
1666// 'out'. Returns true on success.
1667bool DeltaDiffGenerator::BsdiffFiles(const string& old_file,
1668 const string& new_file,
1669 vector<char>* out) {
1670 const string kPatchFile = "/tmp/delta.patchXXXXXX";
1671 string patch_file_path;
1672
1673 TEST_AND_RETURN_FALSE(
1674 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
1675
1676 vector<string> cmd;
1677 cmd.push_back(kBsdiffPath);
1678 cmd.push_back(old_file);
1679 cmd.push_back(new_file);
1680 cmd.push_back(patch_file_path);
1681
1682 int rc = 1;
1683 vector<char> patch_file;
Darin Petkov85d02b72011-05-17 13:25:51 -07001684 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc, NULL));
Thieu Le5c7d9752010-12-15 16:09:28 -08001685 TEST_AND_RETURN_FALSE(rc == 0);
1686 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
1687 unlink(patch_file_path.c_str());
1688 return true;
1689}
1690
1691// The |blocks| vector contains a reader and writer for each block on the
1692// filesystem that's being in-place updated. We populate the reader/writer
1693// fields of |blocks| by calling this function.
1694// For each block in |operation| that is read or written, find that block
1695// in |blocks| and set the reader/writer field to the vertex passed.
1696// |graph| is not strictly necessary, but useful for printing out
1697// error messages.
1698bool DeltaDiffGenerator::AddInstallOpToBlocksVector(
1699 const DeltaArchiveManifest_InstallOperation& operation,
1700 const Graph& graph,
1701 Vertex::Index vertex,
1702 vector<Block>* blocks) {
1703 // See if this is already present.
1704 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
1705
1706 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
1707 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
1708 const int extents_size =
1709 (field == READER) ? operation.src_extents_size() :
1710 operation.dst_extents_size();
1711 const char* past_participle = (field == READER) ? "read" : "written";
1712 const google::protobuf::RepeatedPtrField<Extent>& extents =
1713 (field == READER) ? operation.src_extents() : operation.dst_extents();
1714 Vertex::Index Block::*access_type =
1715 (field == READER) ? &Block::reader : &Block::writer;
1716
1717 for (int i = 0; i < extents_size; i++) {
1718 const Extent& extent = extents.Get(i);
1719 if (extent.start_block() == kSparseHole) {
1720 // Hole in sparse file. skip
1721 continue;
1722 }
1723 for (uint64_t block = extent.start_block();
1724 block < (extent.start_block() + extent.num_blocks()); block++) {
1725 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
1726 LOG(FATAL) << "Block " << block << " is already "
1727 << past_participle << " by "
1728 << (*blocks)[block].*access_type << "("
1729 << graph[(*blocks)[block].*access_type].file_name
1730 << ") and also " << vertex << "("
1731 << graph[vertex].file_name << ")";
1732 }
1733 (*blocks)[block].*access_type = vertex;
1734 }
1735 }
1736 }
1737 return true;
1738}
1739
Darin Petkov9574f7e2011-01-13 10:48:12 -08001740void DeltaDiffGenerator::AddSignatureOp(uint64_t signature_blob_offset,
1741 uint64_t signature_blob_length,
1742 DeltaArchiveManifest* manifest) {
1743 LOG(INFO) << "Making room for signature in file";
1744 manifest->set_signatures_offset(signature_blob_offset);
1745 LOG(INFO) << "set? " << manifest->has_signatures_offset();
1746 // Add a dummy op at the end to appease older clients
1747 DeltaArchiveManifest_InstallOperation* dummy_op =
1748 manifest->add_kernel_install_operations();
1749 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1750 dummy_op->set_data_offset(signature_blob_offset);
1751 manifest->set_signatures_offset(signature_blob_offset);
1752 dummy_op->set_data_length(signature_blob_length);
1753 manifest->set_signatures_size(signature_blob_length);
1754 Extent* dummy_extent = dummy_op->add_dst_extents();
1755 // Tell the dummy op to write this data to a big sparse hole
1756 dummy_extent->set_start_block(kSparseHole);
1757 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1758 kBlockSize);
1759}
1760
Andrew de los Reyes50f36492010-11-01 13:57:12 -07001761const char* const kBsdiffPath = "bsdiff";
1762const char* const kBspatchPath = "bspatch";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001763const char* const kDeltaMagic = "CrAU";
1764
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001765}; // namespace chromeos_update_engine