blob: 94487c1c79d8a62b9810c1e793d7c727a3cda2c9 [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
20#include <base/logging.h>
Darin Petkov7438a5c2011-08-29 11:56:44 -070021#include <base/memory/scoped_ptr.h>
Darin Petkov880335c2010-10-01 15:52:53 -070022#include <base/string_util.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040023#include <base/stringprintf.h>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070024#include <bzlib.h>
Darin Petkov880335c2010-10-01 15:52:53 -070025
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070026#include "update_engine/bzip.h"
27#include "update_engine/cycle_breaker.h"
28#include "update_engine/extent_mapper.h"
Andrew de los Reyesef017552010-10-06 17:57:52 -070029#include "update_engine/extent_ranges.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070030#include "update_engine/file_writer.h"
31#include "update_engine/filesystem_iterator.h"
Darin Petkov7a22d792010-11-08 14:10:00 -080032#include "update_engine/full_update_generator.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070033#include "update_engine/graph_types.h"
34#include "update_engine/graph_utils.h"
Thieu Le5c7d9752010-12-15 16:09:28 -080035#include "update_engine/metadata.h"
Darin Petkov36a58222010-10-07 22:00:09 -070036#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070037#include "update_engine/payload_signer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070038#include "update_engine/subprocess.h"
39#include "update_engine/topological_sort.h"
40#include "update_engine/update_metadata.pb.h"
41#include "update_engine/utils.h"
42
43using std::make_pair;
Andrew de los Reyesef017552010-10-06 17:57:52 -070044using std::map;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070045using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070046using std::min;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -070047using std::pair;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070048using std::set;
49using std::string;
50using std::vector;
51
52namespace chromeos_update_engine {
53
54typedef DeltaDiffGenerator::Block Block;
Darin Petkov9fa7ec52010-10-18 11:45:23 -070055typedef map<const DeltaArchiveManifest_InstallOperation*,
56 const string*> OperationNameMap;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070057
58namespace {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070059const size_t kBlockSize = 4096; // bytes
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -080060const string kNonexistentPath = "";
Andrew de los Reyes927179d2010-12-02 11:26:48 -080061
62// TODO(adlr): switch from 1GiB to 2GiB when we no longer care about old
63// clients:
Darin Petkov9eadd642010-10-14 15:20:57 -070064const size_t kRootFSPartitionSize = 1 * 1024 * 1024 * 1024; // bytes
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070065const uint64_t kVersionNumber = 1;
Darin Petkov9eadd642010-10-14 15:20:57 -070066const uint64_t kFullUpdateChunkSize = 1024 * 1024; // bytes
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070067
Darin Petkov68c10d12010-10-14 09:24:37 -070068static const char* kInstallOperationTypes[] = {
69 "REPLACE",
70 "REPLACE_BZ",
71 "MOVE",
72 "BSDIFF"
73};
74
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070075// Stores all Extents for a file into 'out'. Returns true on success.
76bool GatherExtents(const string& path,
77 google::protobuf::RepeatedPtrField<Extent>* out) {
78 vector<Extent> extents;
79 TEST_AND_RETURN_FALSE(extent_mapper::ExtentsForFileFibmap(path, &extents));
80 DeltaDiffGenerator::StoreExtents(extents, out);
81 return true;
82}
83
Andrew de los Reyesef017552010-10-06 17:57:52 -070084// For a given regular file which must exist at new_root + path, and
85// may exist at old_root + path, creates a new InstallOperation and
86// adds it to the graph. Also, populates the |blocks| array as
87// necessary, if |blocks| is non-NULL. Also, writes the data
88// necessary to send the file down to the client into data_fd, which
89// has length *data_file_size. *data_file_size is updated
90// appropriately. If |existing_vertex| is no kInvalidIndex, use that
91// rather than allocating a new vertex. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070092bool DeltaReadFile(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -070093 Vertex::Index existing_vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070094 vector<Block>* blocks,
95 const string& old_root,
96 const string& new_root,
97 const string& path, // within new_root
98 int data_fd,
99 off_t* data_file_size) {
100 vector<char> data;
101 DeltaArchiveManifest_InstallOperation operation;
102
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800103 string old_path = (old_root == kNonexistentPath) ? kNonexistentPath :
104 old_root + path;
105
Don Garrettf4b28742012-03-27 20:48:06 -0700106 // TODO(dgarrett): chromium-os:15274 Wire up this file all of the way to
107 // command line.
Don Garrett36e60772012-03-29 10:31:20 -0700108 //
109 // To hardcode for now, use the following instead of true.
110 // (path != "/opt/google/chrome/pepper/libnetflixidd.so");
111 bool bsdiff_allowed = true;
Don Garrettf4b28742012-03-27 20:48:06 -0700112
Don Garrett36e60772012-03-29 10:31:20 -0700113 if (!bsdiff_allowed)
114 LOG(INFO) << "bsdiff blacklisting: " << path;
Don Garrettf4b28742012-03-27 20:48:06 -0700115
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800116 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_path,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700117 new_root + path,
Don Garrett36e60772012-03-29 10:31:20 -0700118 bsdiff_allowed,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700119 &data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700120 &operation,
121 true));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700122
123 // Write the data
124 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
125 operation.set_data_offset(*data_file_size);
126 operation.set_data_length(data.size());
127 }
128
129 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
130 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700131
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700132 // Now, insert into graph and blocks vector
Andrew de los Reyesef017552010-10-06 17:57:52 -0700133 Vertex::Index vertex = existing_vertex;
134 if (vertex == Vertex::kInvalidIndex) {
135 graph->resize(graph->size() + 1);
136 vertex = graph->size() - 1;
137 }
138 (*graph)[vertex].op = operation;
139 CHECK((*graph)[vertex].op.has_type());
140 (*graph)[vertex].file_name = path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700141
Andrew de los Reyesef017552010-10-06 17:57:52 -0700142 if (blocks)
Thieu Le5c7d9752010-12-15 16:09:28 -0800143 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::AddInstallOpToBlocksVector(
144 (*graph)[vertex].op,
145 *graph,
146 vertex,
147 blocks));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700148 return true;
149}
150
151// For each regular file within new_root, creates a node in the graph,
152// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
153// and writes any necessary data to the end of data_fd.
154bool DeltaReadFiles(Graph* graph,
155 vector<Block>* blocks,
156 const string& old_root,
157 const string& new_root,
158 int data_fd,
159 off_t* data_file_size) {
160 set<ino_t> visited_inodes;
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800161 set<ino_t> visited_src_inodes;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700162 for (FilesystemIterator fs_iter(new_root,
163 utils::SetWithValue<string>("/lost+found"));
164 !fs_iter.IsEnd(); fs_iter.Increment()) {
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800165 // We never diff symlinks (here, we check that dst file is not a symlink).
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700166 if (!S_ISREG(fs_iter.GetStat().st_mode))
167 continue;
168
169 // Make sure we visit each inode only once.
170 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
171 continue;
172 visited_inodes.insert(fs_iter.GetStat().st_ino);
173 if (fs_iter.GetStat().st_size == 0)
174 continue;
175
176 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700177
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800178 // We can't visit each dst image inode more than once, as that would
179 // duplicate work. Here, we avoid visiting each source image inode
180 // more than once. Technically, we could have multiple operations
181 // that read the same blocks from the source image for diffing, but
182 // we choose not to to avoid complexity. Eventually we will move away
183 // from using a graph/cycle detection/etc to generate diffs, and at that
184 // time, it will be easy (non-complex) to have many operations read
185 // from the same source blocks. At that time, this code can die. -adlr
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800186 bool should_diff_from_source = false;
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800187 string src_path = old_root + fs_iter.GetPartialPath();
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800188 struct stat src_stbuf;
189 // We never diff symlinks (here, we check that src file is not a symlink).
190 if (0 == lstat(src_path.c_str(), &src_stbuf) &&
191 S_ISREG(src_stbuf.st_mode)) {
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800192 should_diff_from_source = !utils::SetContainsKey(visited_src_inodes,
193 src_stbuf.st_ino);
194 visited_src_inodes.insert(src_stbuf.st_ino);
195 }
196
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700197 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700198 Vertex::kInvalidIndex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700199 blocks,
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800200 (should_diff_from_source ?
201 old_root :
202 kNonexistentPath),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700203 new_root,
204 fs_iter.GetPartialPath(),
205 data_fd,
206 data_file_size));
207 }
208 return true;
209}
210
Andrew de los Reyesef017552010-10-06 17:57:52 -0700211// This class allocates non-existent temp blocks, starting from
212// kTempBlockStart. Other code is responsible for converting these
213// temp blocks into real blocks, as the client can't read or write to
214// these blocks.
215class DummyExtentAllocator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700216 public:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700217 explicit DummyExtentAllocator()
218 : next_block_(kTempBlockStart) {}
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700219 vector<Extent> Allocate(const uint64_t block_count) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700220 vector<Extent> ret(1);
221 ret[0].set_start_block(next_block_);
222 ret[0].set_num_blocks(block_count);
223 next_block_ += block_count;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700224 return ret;
225 }
226 private:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700227 uint64_t next_block_;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700228};
229
230// Reads blocks from image_path that are not yet marked as being written
231// in the blocks array. These blocks that remain are non-file-data blocks.
232// In the future we might consider intelligent diffing between this data
233// and data in the previous image, but for now we just bzip2 compress it
234// and include it in the update.
235// Creates a new node in the graph to write these blocks and writes the
236// appropriate blob to blobs_fd. Reads and updates blobs_length;
237bool ReadUnwrittenBlocks(const vector<Block>& blocks,
238 int blobs_fd,
239 off_t* blobs_length,
240 const string& image_path,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700241 Vertex* vertex) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700242 vertex->file_name = "<rootfs-non-file-data>";
243
Andrew de los Reyesef017552010-10-06 17:57:52 -0700244 DeltaArchiveManifest_InstallOperation* out_op = &vertex->op;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700245 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
246 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
247 ScopedFdCloser image_fd_closer(&image_fd);
248
249 string temp_file_path;
250 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX",
251 &temp_file_path,
252 NULL));
253
254 FILE* file = fopen(temp_file_path.c_str(), "w");
255 TEST_AND_RETURN_FALSE(file);
256 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700257
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700258 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
259 file,
260 9, // max compression
261 0, // verbosity
262 0); // default work factor
263 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700264
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700265 vector<Extent> extents;
266 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700267
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700268 LOG(INFO) << "Appending left over blocks to extents";
269 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
270 if (blocks[i].writer != Vertex::kInvalidIndex)
271 continue;
Andrew de los Reyesef017552010-10-06 17:57:52 -0700272 if (blocks[i].reader != Vertex::kInvalidIndex) {
273 graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i);
274 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700275 graph_utils::AppendBlockToExtents(&extents, i);
276 block_count++;
277 }
278
279 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
280 // so we arbitrarily set it to 1024 * kBlockSize.
281 vector<char> buf(1024 * kBlockSize);
282
283 LOG(INFO) << "Reading left over blocks";
284 vector<Block>::size_type blocks_copied_count = 0;
285
286 // For each extent in extents, write the data into BZ2_bzWrite which
287 // sends it to an output file.
288 // We use the temporary buffer 'buf' to hold the data, which may be
289 // smaller than the extent, so in that case we have to loop to get
290 // the extent's data (that's the inner while loop).
291 for (vector<Extent>::const_iterator it = extents.begin();
292 it != extents.end(); ++it) {
293 vector<Block>::size_type blocks_read = 0;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800294 float printed_progress = -1;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700295 while (blocks_read < it->num_blocks()) {
296 const int copy_block_cnt =
297 min(buf.size() / kBlockSize,
298 static_cast<vector<char>::size_type>(
299 it->num_blocks() - blocks_read));
300 ssize_t rc = pread(image_fd,
301 &buf[0],
302 copy_block_cnt * kBlockSize,
303 (it->start_block() + blocks_read) * kBlockSize);
304 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
305 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
306 copy_block_cnt * kBlockSize);
307 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
308 TEST_AND_RETURN_FALSE(err == BZ_OK);
309 blocks_read += copy_block_cnt;
310 blocks_copied_count += copy_block_cnt;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800311 float current_progress =
312 static_cast<float>(blocks_copied_count) / block_count;
313 if (printed_progress + 0.1 < current_progress ||
314 blocks_copied_count == block_count) {
315 LOG(INFO) << "progress: " << current_progress;
316 printed_progress = current_progress;
317 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700318 }
319 }
320 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
321 TEST_AND_RETURN_FALSE(err == BZ_OK);
322 bz_file = NULL;
323 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
324 file = NULL;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700325
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700326 vector<char> compressed_data;
327 LOG(INFO) << "Reading compressed data off disk";
328 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
329 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700330
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700331 // Add node to graph to write these blocks
332 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
333 out_op->set_data_offset(*blobs_length);
334 out_op->set_data_length(compressed_data.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700335 LOG(INFO) << "Rootfs non-data blocks compressed take up "
336 << compressed_data.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700337 *blobs_length += compressed_data.size();
338 out_op->set_dst_length(kBlockSize * block_count);
339 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700340
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700341 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
342 &compressed_data[0],
343 compressed_data.size()));
344 LOG(INFO) << "done with extra blocks";
345 return true;
346}
347
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700348// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700349// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700350bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
351 uint64_t value_be = htobe64(value);
Don Garrette410e0f2011-11-10 15:39:01 -0800352 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700353 return true;
354}
355
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700356// Adds each operation from |graph| to |out_manifest| in the order specified by
357// |order| while building |out_op_name_map| with operation to name
358// mappings. Adds all |kernel_ops| to |out_manifest|. Filters out no-op
359// operations.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700360void InstallOperationsToManifest(
361 const Graph& graph,
362 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700363 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700364 DeltaArchiveManifest* out_manifest,
365 OperationNameMap* out_op_name_map) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700366 for (vector<Vertex::Index>::const_iterator it = order.begin();
367 it != order.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700368 const Vertex& vertex = graph[*it];
369 const DeltaArchiveManifest_InstallOperation& add_op = vertex.op;
370 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
371 continue;
372 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700373 DeltaArchiveManifest_InstallOperation* op =
374 out_manifest->add_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700375 *op = add_op;
376 (*out_op_name_map)[op] = &vertex.file_name;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700377 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700378 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
379 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700380 const DeltaArchiveManifest_InstallOperation& add_op = *it;
381 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
382 continue;
383 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700384 DeltaArchiveManifest_InstallOperation* op =
385 out_manifest->add_kernel_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700386 *op = add_op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700387 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700388}
389
390void CheckGraph(const Graph& graph) {
391 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
392 CHECK(it->op.has_type());
393 }
394}
395
Darin Petkov68c10d12010-10-14 09:24:37 -0700396// Delta compresses a kernel partition |new_kernel_part| with knowledge of the
397// old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty
398// string, generates a full update of the partition.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700399bool DeltaCompressKernelPartition(
400 const string& old_kernel_part,
401 const string& new_kernel_part,
402 vector<DeltaArchiveManifest_InstallOperation>* ops,
403 int blobs_fd,
404 off_t* blobs_length) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700405 LOG(INFO) << "Delta compressing kernel partition...";
Darin Petkov68c10d12010-10-14 09:24:37 -0700406 LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update...";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700407
408 // Add a new install operation
409 ops->resize(1);
410 DeltaArchiveManifest_InstallOperation* op = &(*ops)[0];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700411
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700412 vector<char> data;
Don Garrett36e60772012-03-29 10:31:20 -0700413 TEST_AND_RETURN_FALSE(
414 DeltaDiffGenerator::ReadFileToDiff(old_kernel_part,
415 new_kernel_part,
416 true, // bsdiff_allowed
417 &data,
418 op,
419 false));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700420
Darin Petkov68c10d12010-10-14 09:24:37 -0700421 // Write the data
422 if (op->type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
423 op->set_data_offset(*blobs_length);
424 op->set_data_length(data.size());
425 }
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700426
Darin Petkov68c10d12010-10-14 09:24:37 -0700427 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
428 *blobs_length += data.size();
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700429
Darin Petkov68c10d12010-10-14 09:24:37 -0700430 LOG(INFO) << "Done delta compressing kernel partition: "
431 << kInstallOperationTypes[op->type()];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700432 return true;
433}
434
Darin Petkov880335c2010-10-01 15:52:53 -0700435struct DeltaObject {
436 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
437 : name(in_name),
438 type(in_type),
439 size(in_size) {}
440 bool operator <(const DeltaObject& object) const {
Darin Petkovd43d6902010-10-14 11:17:50 -0700441 return (size != object.size) ? (size < object.size) : (name < object.name);
Darin Petkov880335c2010-10-01 15:52:53 -0700442 }
443 string name;
444 int type;
445 off_t size;
446};
447
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700448void ReportPayloadUsage(const DeltaArchiveManifest& manifest,
449 const int64_t manifest_metadata_size,
450 const OperationNameMap& op_name_map) {
Darin Petkov880335c2010-10-01 15:52:53 -0700451 vector<DeltaObject> objects;
452 off_t total_size = 0;
453
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700454 // Rootfs install operations.
455 for (int i = 0; i < manifest.install_operations_size(); ++i) {
456 const DeltaArchiveManifest_InstallOperation& op =
457 manifest.install_operations(i);
458 objects.push_back(DeltaObject(*op_name_map.find(&op)->second,
459 op.type(),
460 op.data_length()));
461 total_size += op.data_length();
Darin Petkov880335c2010-10-01 15:52:53 -0700462 }
463
Darin Petkov880335c2010-10-01 15:52:53 -0700464 // Kernel install operations.
465 for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) {
466 const DeltaArchiveManifest_InstallOperation& op =
467 manifest.kernel_install_operations(i);
468 objects.push_back(DeltaObject(StringPrintf("<kernel-operation-%d>", i),
469 op.type(),
470 op.data_length()));
471 total_size += op.data_length();
472 }
473
Darin Petkov95cf01f2010-10-12 14:59:13 -0700474 objects.push_back(DeltaObject("<manifest-metadata>",
475 -1,
476 manifest_metadata_size));
477 total_size += manifest_metadata_size;
478
Darin Petkov880335c2010-10-01 15:52:53 -0700479 std::sort(objects.begin(), objects.end());
480
481 static const char kFormatString[] = "%6.2f%% %10llu %-10s %s\n";
482 for (vector<DeltaObject>::const_iterator it = objects.begin();
483 it != objects.end(); ++it) {
484 const DeltaObject& object = *it;
485 fprintf(stderr, kFormatString,
486 object.size * 100.0 / total_size,
487 object.size,
Darin Petkov95cf01f2010-10-12 14:59:13 -0700488 object.type >= 0 ? kInstallOperationTypes[object.type] : "-",
Darin Petkov880335c2010-10-01 15:52:53 -0700489 object.name.c_str());
490 }
491 fprintf(stderr, kFormatString, 100.0, total_size, "", "<total>");
492}
493
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700494} // namespace {}
495
496bool DeltaDiffGenerator::ReadFileToDiff(
497 const string& old_filename,
498 const string& new_filename,
Don Garrett36e60772012-03-29 10:31:20 -0700499 bool bsdiff_allowed,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700500 vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700501 DeltaArchiveManifest_InstallOperation* out_op,
502 bool gather_extents) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700503 // Read new data in
504 vector<char> new_data;
505 TEST_AND_RETURN_FALSE(utils::ReadFile(new_filename, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700506
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700507 TEST_AND_RETURN_FALSE(!new_data.empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700508
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700509 vector<char> new_data_bz;
510 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
511 CHECK(!new_data_bz.empty());
512
513 vector<char> data; // Data blob that will be written to delta file.
514
515 DeltaArchiveManifest_InstallOperation operation;
516 size_t current_best_size = 0;
517 if (new_data.size() <= new_data_bz.size()) {
518 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
519 current_best_size = new_data.size();
520 data = new_data;
521 } else {
522 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
523 current_best_size = new_data_bz.size();
524 data = new_data_bz;
525 }
526
527 // Do we have an original file to consider?
528 struct stat old_stbuf;
Don Garrettf4b28742012-03-27 20:48:06 -0700529 bool original = !old_filename.empty();
530 if (original && 0 != stat(old_filename.c_str(), &old_stbuf)) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700531 // If stat-ing the old file fails, it should be because it doesn't exist.
532 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
Don Garrettf4b28742012-03-27 20:48:06 -0700533 original = false;
Darin Petkov68c10d12010-10-14 09:24:37 -0700534 }
Don Garrettf4b28742012-03-27 20:48:06 -0700535
536 if (original) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700537 // Read old data
538 vector<char> old_data;
539 TEST_AND_RETURN_FALSE(utils::ReadFile(old_filename, &old_data));
540 if (old_data == new_data) {
541 // No change in data.
542 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
543 current_best_size = 0;
544 data.clear();
Don Garrett36e60772012-03-29 10:31:20 -0700545 } else if (bsdiff_allowed) {
546 // If the source file is considered bsdiff safe (no bsdiff bugs
547 // triggered), see if BSDIFF encoding is smaller.
548 vector<char> bsdiff_delta;
549 TEST_AND_RETURN_FALSE(
550 BsdiffFiles(old_filename, new_filename, &bsdiff_delta));
551 CHECK_GT(bsdiff_delta.size(), static_cast<vector<char>::size_type>(0));
552 if (bsdiff_delta.size() < current_best_size) {
553 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
554 current_best_size = bsdiff_delta.size();
555 data = bsdiff_delta;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700556 }
557 }
558 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700559
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700560 // Set parameters of the operations
561 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700562
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700563 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
564 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Darin Petkov68c10d12010-10-14 09:24:37 -0700565 if (gather_extents) {
566 TEST_AND_RETURN_FALSE(
567 GatherExtents(old_filename, operation.mutable_src_extents()));
568 } else {
569 Extent* src_extent = operation.add_src_extents();
570 src_extent->set_start_block(0);
571 src_extent->set_num_blocks(
572 (old_stbuf.st_size + kBlockSize - 1) / kBlockSize);
573 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700574 operation.set_src_length(old_stbuf.st_size);
575 }
576
Darin Petkov68c10d12010-10-14 09:24:37 -0700577 if (gather_extents) {
578 TEST_AND_RETURN_FALSE(
579 GatherExtents(new_filename, operation.mutable_dst_extents()));
580 } else {
581 Extent* dst_extent = operation.add_dst_extents();
582 dst_extent->set_start_block(0);
583 dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize);
584 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700585 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700586
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700587 out_data->swap(data);
588 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700589
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700590 return true;
591}
592
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700593bool DeltaDiffGenerator::InitializePartitionInfo(bool is_kernel,
594 const string& partition,
595 PartitionInfo* info) {
Darin Petkov7ea32332010-10-13 10:46:11 -0700596 int64_t size = 0;
597 if (is_kernel) {
598 size = utils::FileSize(partition);
599 } else {
600 int block_count = 0, block_size = 0;
601 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition,
602 &block_count,
603 &block_size));
604 size = static_cast<int64_t>(block_count) * block_size;
605 }
606 TEST_AND_RETURN_FALSE(size > 0);
Darin Petkov36a58222010-10-07 22:00:09 -0700607 info->set_size(size);
608 OmahaHashCalculator hasher;
Darin Petkov7ea32332010-10-13 10:46:11 -0700609 TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size);
Darin Petkov36a58222010-10-07 22:00:09 -0700610 TEST_AND_RETURN_FALSE(hasher.Finalize());
611 const vector<char>& hash = hasher.raw_hash();
612 info->set_hash(hash.data(), hash.size());
Darin Petkovd43d6902010-10-14 11:17:50 -0700613 LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash();
Darin Petkov36a58222010-10-07 22:00:09 -0700614 return true;
615}
616
617bool InitializePartitionInfos(const string& old_kernel,
618 const string& new_kernel,
619 const string& old_rootfs,
620 const string& new_rootfs,
621 DeltaArchiveManifest* manifest) {
Darin Petkovd43d6902010-10-14 11:17:50 -0700622 if (!old_kernel.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700623 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
624 true,
625 old_kernel,
626 manifest->mutable_old_kernel_info()));
Darin Petkovd43d6902010-10-14 11:17:50 -0700627 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700628 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
629 true,
630 new_kernel,
631 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700632 if (!old_rootfs.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700633 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
634 false,
635 old_rootfs,
636 manifest->mutable_old_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700637 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700638 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
639 false,
640 new_rootfs,
641 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700642 return true;
643}
644
Andrew de los Reyesef017552010-10-06 17:57:52 -0700645namespace {
646
647// Takes a collection (vector or RepeatedPtrField) of Extent and
648// returns a vector of the blocks referenced, in order.
649template<typename T>
650vector<uint64_t> ExpandExtents(const T& extents) {
651 vector<uint64_t> ret;
652 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
653 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700654 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700655 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700656 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700657 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700658 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700659 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700660 }
661 }
662 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700663 return ret;
664}
665
666// Takes a vector of blocks and returns an equivalent vector of Extent
667// objects.
668vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
669 vector<Extent> new_extents;
670 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
671 it != e; ++it) {
672 graph_utils::AppendBlockToExtents(&new_extents, *it);
673 }
674 return new_extents;
675}
676
677} // namespace {}
678
679void DeltaDiffGenerator::SubstituteBlocks(
680 Vertex* vertex,
681 const vector<Extent>& remove_extents,
682 const vector<Extent>& replace_extents) {
683 // First, expand out the blocks that op reads from
684 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700685 {
686 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700687 vector<uint64_t> remove_extents_expanded =
688 ExpandExtents(remove_extents);
689 vector<uint64_t> replace_extents_expanded =
690 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700691 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700692 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700693 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700694 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700695 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
696 }
697 utils::ApplyMap(&read_blocks, conversion);
698 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
699 e = vertex->out_edges.end(); it != e; ++it) {
700 vector<uint64_t> write_before_deps_expanded =
701 ExpandExtents(it->second.write_extents);
702 utils::ApplyMap(&write_before_deps_expanded, conversion);
703 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700704 }
705 }
706 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700707 vertex->op.clear_src_extents();
708 vector<Extent> new_extents = CompressExtents(read_blocks);
709 DeltaDiffGenerator::StoreExtents(new_extents,
710 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700711}
712
713bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700714 const set<Edge>& edges,
715 vector<CutEdgeVertexes>* out_cuts) {
716 DummyExtentAllocator scratch_allocator;
717 vector<CutEdgeVertexes> cuts;
718 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700719
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700720 uint64_t scratch_blocks_used = 0;
721 for (set<Edge>::const_iterator it = edges.begin();
722 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700723 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700724 vector<Extent> old_extents =
725 (*graph)[it->first].out_edges[it->second].extents;
726 // Choose some scratch space
727 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700728 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700729 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
730 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700731 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700732 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700733 cuts.back().old_src = it->first;
734 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700735
Andrew de los Reyesef017552010-10-06 17:57:52 -0700736 EdgeProperties& cut_edge_properties =
737 (*graph)[it->first].out_edges.find(it->second)->second;
738
739 // This should never happen, as we should only be cutting edges between
740 // real file nodes, and write-before relationships are created from
741 // a real file node to a temp copy node:
742 CHECK(cut_edge_properties.write_extents.empty())
743 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700744
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700745 // make node depend on the copy operation
746 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700747 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700748
749 // Set src/dst extents and other proto variables for copy operation
750 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
751 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700752 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700753 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700754 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700755 graph->back().op.mutable_dst_extents());
756 graph->back().op.set_src_length(
757 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
758 graph->back().op.set_dst_length(graph->back().op.src_length());
759
760 // make the dest node read from the scratch space
761 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700762 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700763 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700764 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700765
766 // delete the old edge
Mike Frysinger0f9547d2012-02-16 12:11:37 -0500767 CHECK_EQ(static_cast<Graph::size_type>(1),
768 (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700769
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700770 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700771 EdgeProperties write_before_edge_properties;
772 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
773 (*graph)[it->second].out_edges.insert(
774 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700775 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700776 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700777 return true;
778}
779
780// Stores all Extents in 'extents' into 'out'.
781void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700782 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700783 google::protobuf::RepeatedPtrField<Extent>* out) {
784 for (vector<Extent>::const_iterator it = extents.begin();
785 it != extents.end(); ++it) {
786 Extent* new_extent = out->Add();
787 *new_extent = *it;
788 }
789}
790
791// Creates all the edges for the graph. Writers of a block point to
792// readers of the same block. This is because for an edge A->B, B
793// must complete before A executes.
794void DeltaDiffGenerator::CreateEdges(Graph* graph,
795 const vector<Block>& blocks) {
796 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
797 // Blocks with both a reader and writer get an edge
798 if (blocks[i].reader == Vertex::kInvalidIndex ||
799 blocks[i].writer == Vertex::kInvalidIndex)
800 continue;
801 // Don't have a node depend on itself
802 if (blocks[i].reader == blocks[i].writer)
803 continue;
804 // See if there's already an edge we can add onto
805 Vertex::EdgeMap::iterator edge_it =
806 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
807 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
808 // No existing edge. Create one
809 (*graph)[blocks[i].writer].out_edges.insert(
810 make_pair(blocks[i].reader, EdgeProperties()));
811 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700812 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700813 }
814 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
815 }
816}
817
Andrew de los Reyesef017552010-10-06 17:57:52 -0700818namespace {
819
820class SortCutsByTopoOrderLess {
821 public:
822 SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table)
823 : table_(table) {}
824 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
825 return table_[a.old_dst] < table_[b.old_dst];
826 }
827 private:
828 vector<vector<Vertex::Index>::size_type>& table_;
829};
830
831} // namespace {}
832
833void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
834 vector<Vertex::Index>& op_indexes,
835 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
836 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
837 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
838 i != e; ++i) {
839 Vertex::Index node = op_indexes[i];
840 if (table.size() < (node + 1)) {
841 table.resize(node + 1);
842 }
843 table[node] = i;
844 }
845 reverse_op_indexes->swap(table);
846}
847
848void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes,
849 vector<CutEdgeVertexes>* cuts) {
850 // first, make a reverse lookup table.
851 vector<vector<Vertex::Index>::size_type> table;
852 GenerateReverseTopoOrderMap(op_indexes, &table);
853 SortCutsByTopoOrderLess less(table);
854 sort(cuts->begin(), cuts->end(), less);
855}
856
857void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
858 vector<Vertex::Index>* op_indexes) {
859 vector<Vertex::Index> ret;
860 vector<Vertex::Index> full_ops;
861 ret.reserve(op_indexes->size());
862 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
863 ++i) {
864 DeltaArchiveManifest_InstallOperation_Type type =
865 (*graph)[(*op_indexes)[i]].op.type();
866 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
867 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
868 full_ops.push_back((*op_indexes)[i]);
869 } else {
870 ret.push_back((*op_indexes)[i]);
871 }
872 }
873 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
874 << (full_ops.size() + ret.size()) << " total ops.";
875 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
876 op_indexes->swap(ret);
877}
878
879namespace {
880
881template<typename T>
882bool TempBlocksExistInExtents(const T& extents) {
883 for (int i = 0, e = extents.size(); i < e; ++i) {
884 Extent extent = graph_utils::GetElement(extents, i);
885 uint64_t start = extent.start_block();
886 uint64_t num = extent.num_blocks();
887 if (start == kSparseHole)
888 continue;
889 if (start >= kTempBlockStart ||
890 (start + num) >= kTempBlockStart) {
891 LOG(ERROR) << "temp block!";
892 LOG(ERROR) << "start: " << start << ", num: " << num;
893 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
894 LOG(ERROR) << "returning true";
895 return true;
896 }
897 // check for wrap-around, which would be a bug:
898 CHECK(start <= (start + num));
899 }
900 return false;
901}
902
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700903// Convertes the cuts, which must all have the same |old_dst| member,
904// to full. It does this by converting the |old_dst| to REPLACE or
905// REPLACE_BZ, dropping all incoming edges to |old_dst|, and marking
906// all temp nodes invalid.
907bool ConvertCutsToFull(
908 Graph* graph,
909 const string& new_root,
910 int data_fd,
911 off_t* data_file_size,
912 vector<Vertex::Index>* op_indexes,
913 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
914 const vector<CutEdgeVertexes>& cuts) {
915 CHECK(!cuts.empty());
916 set<Vertex::Index> deleted_nodes;
917 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
918 e = cuts.end(); it != e; ++it) {
919 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp(
920 graph,
921 *it,
922 new_root,
923 data_fd,
924 data_file_size));
925 deleted_nodes.insert(it->new_vertex);
926 }
927 deleted_nodes.insert(cuts[0].old_dst);
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700928
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700929 vector<Vertex::Index> new_op_indexes;
930 new_op_indexes.reserve(op_indexes->size());
931 for (vector<Vertex::Index>::iterator it = op_indexes->begin(),
932 e = op_indexes->end(); it != e; ++it) {
933 if (utils::SetContainsKey(deleted_nodes, *it))
934 continue;
935 new_op_indexes.push_back(*it);
936 }
937 new_op_indexes.push_back(cuts[0].old_dst);
938 op_indexes->swap(new_op_indexes);
939 DeltaDiffGenerator::GenerateReverseTopoOrderMap(*op_indexes,
940 reverse_op_indexes);
941 return true;
942}
943
944// Tries to assign temp blocks for a collection of cuts, all of which share
945// the same old_dst member. If temp blocks can't be found, old_dst will be
946// converted to a REPLACE or REPLACE_BZ operation. Returns true on success,
947// which can happen even if blocks are converted to full. Returns false
948// on exceptional error cases.
949bool AssignBlockForAdjoiningCuts(
950 Graph* graph,
951 const string& new_root,
952 int data_fd,
953 off_t* data_file_size,
954 vector<Vertex::Index>* op_indexes,
955 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
956 const vector<CutEdgeVertexes>& cuts) {
957 CHECK(!cuts.empty());
958 const Vertex::Index old_dst = cuts[0].old_dst;
959 // Calculate # of blocks needed
960 uint64_t blocks_needed = 0;
961 map<const CutEdgeVertexes*, uint64_t> cuts_blocks_needed;
962 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
963 e = cuts.end(); it != e; ++it) {
964 uint64_t cut_blocks_needed = 0;
965 for (vector<Extent>::const_iterator jt = it->tmp_extents.begin(),
966 je = it->tmp_extents.end(); jt != je; ++jt) {
967 cut_blocks_needed += jt->num_blocks();
968 }
969 blocks_needed += cut_blocks_needed;
970 cuts_blocks_needed[&*it] = cut_blocks_needed;
971 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700972
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700973 // Find enough blocks
974 ExtentRanges scratch_ranges;
975 // Each block that's supplying temp blocks and the corresponding blocks:
976 typedef vector<pair<Vertex::Index, ExtentRanges> > SupplierVector;
977 SupplierVector block_suppliers;
978 uint64_t scratch_blocks_found = 0;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700979 for (vector<Vertex::Index>::size_type i = (*reverse_op_indexes)[old_dst] + 1,
980 e = op_indexes->size(); i < e; ++i) {
981 Vertex::Index test_node = (*op_indexes)[i];
982 if (!(*graph)[test_node].valid)
983 continue;
984 // See if this node has sufficient blocks
985 ExtentRanges ranges;
986 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
987 ranges.SubtractExtent(ExtentForRange(
988 kTempBlockStart, kSparseHole - kTempBlockStart));
989 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
990 // For now, for simplicity, subtract out all blocks in read-before
991 // dependencies.
992 for (Vertex::EdgeMap::const_iterator edge_i =
993 (*graph)[test_node].out_edges.begin(),
994 edge_e = (*graph)[test_node].out_edges.end();
995 edge_i != edge_e; ++edge_i) {
996 ranges.SubtractExtents(edge_i->second.extents);
997 }
998 if (ranges.blocks() == 0)
999 continue;
1000
1001 if (ranges.blocks() + scratch_blocks_found > blocks_needed) {
1002 // trim down ranges
1003 vector<Extent> new_ranges = ranges.GetExtentsForBlockCount(
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001004 blocks_needed - scratch_blocks_found);
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001005 ranges = ExtentRanges();
1006 ranges.AddExtents(new_ranges);
1007 }
1008 scratch_ranges.AddRanges(ranges);
1009 block_suppliers.push_back(make_pair(test_node, ranges));
1010 scratch_blocks_found += ranges.blocks();
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001011 if (scratch_ranges.blocks() >= blocks_needed)
1012 break;
1013 }
1014 if (scratch_ranges.blocks() < blocks_needed) {
1015 LOG(INFO) << "Unable to find sufficient scratch";
1016 TEST_AND_RETURN_FALSE(ConvertCutsToFull(graph,
1017 new_root,
1018 data_fd,
1019 data_file_size,
1020 op_indexes,
1021 reverse_op_indexes,
1022 cuts));
1023 return true;
1024 }
1025 // Use the scratch we found
1026 TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found);
1027
1028 // Make all the suppliers depend on this node
1029 for (SupplierVector::iterator it = block_suppliers.begin(),
1030 e = block_suppliers.end(); it != e; ++it) {
1031 graph_utils::AddReadBeforeDepExtents(
1032 &(*graph)[it->first],
1033 old_dst,
1034 it->second.GetExtentsForBlockCount(it->second.blocks()));
1035 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001036
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001037 // Replace temp blocks in each cut
1038 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1039 e = cuts.end(); it != e; ++it) {
1040 vector<Extent> real_extents =
1041 scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]);
1042 scratch_ranges.SubtractExtents(real_extents);
1043
1044 // Fix the old dest node w/ the real blocks
1045 DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst],
1046 it->tmp_extents,
1047 real_extents);
1048
1049 // Fix the new node w/ the real blocks. Since the new node is just a
1050 // copy operation, we can replace all the dest extents w/ the real
1051 // blocks.
1052 DeltaArchiveManifest_InstallOperation *op =
1053 &(*graph)[it->new_vertex].op;
1054 op->clear_dst_extents();
1055 DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents());
1056 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001057 return true;
1058}
1059
Andrew de los Reyesef017552010-10-06 17:57:52 -07001060} // namespace {}
1061
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001062// Returns true if |op| is a no-op operation that doesn't do any useful work
1063// (e.g., a move operation that copies blocks onto themselves).
1064bool DeltaDiffGenerator::IsNoopOperation(
1065 const DeltaArchiveManifest_InstallOperation& op) {
1066 return (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE &&
1067 ExpandExtents(op.src_extents()) == ExpandExtents(op.dst_extents()));
1068}
1069
Andrew de los Reyesef017552010-10-06 17:57:52 -07001070bool DeltaDiffGenerator::AssignTempBlocks(
1071 Graph* graph,
1072 const string& new_root,
1073 int data_fd,
1074 off_t* data_file_size,
1075 vector<Vertex::Index>* op_indexes,
1076 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001077 const vector<CutEdgeVertexes>& cuts) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001078 CHECK(!cuts.empty());
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001079
1080 // group of cuts w/ the same old_dst:
1081 vector<CutEdgeVertexes> cuts_group;
1082
Andrew de los Reyesef017552010-10-06 17:57:52 -07001083 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
1084 true ; --i) {
1085 LOG(INFO) << "Fixing temp blocks in cut " << i
1086 << ": old dst: " << cuts[i].old_dst << " new vertex: "
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001087 << cuts[i].new_vertex << " path: "
1088 << (*graph)[cuts[i].old_dst].file_name;
1089
1090 if (cuts_group.empty() || (cuts_group[0].old_dst == cuts[i].old_dst)) {
1091 cuts_group.push_back(cuts[i]);
1092 } else {
1093 CHECK(!cuts_group.empty());
1094 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1095 new_root,
1096 data_fd,
1097 data_file_size,
1098 op_indexes,
1099 reverse_op_indexes,
1100 cuts_group));
1101 cuts_group.clear();
1102 cuts_group.push_back(cuts[i]);
Andrew de los Reyesef017552010-10-06 17:57:52 -07001103 }
Darin Petkov36a58222010-10-07 22:00:09 -07001104
Andrew de los Reyesef017552010-10-06 17:57:52 -07001105 if (i == e) {
1106 // break out of for() loop
1107 break;
1108 }
1109 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001110 CHECK(!cuts_group.empty());
1111 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1112 new_root,
1113 data_fd,
1114 data_file_size,
1115 op_indexes,
1116 reverse_op_indexes,
1117 cuts_group));
Andrew de los Reyesef017552010-10-06 17:57:52 -07001118 return true;
1119}
1120
1121bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1122 size_t idx = 0;
1123 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1124 ++it, ++idx) {
1125 if (!it->valid)
1126 continue;
1127 const DeltaArchiveManifest_InstallOperation& op = it->op;
1128 if (TempBlocksExistInExtents(op.dst_extents()) ||
1129 TempBlocksExistInExtents(op.src_extents())) {
1130 LOG(INFO) << "bad extents in node " << idx;
1131 LOG(INFO) << "so yeah";
1132 return false;
1133 }
1134
1135 // Check out-edges:
1136 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1137 je = it->out_edges.end(); jt != je; ++jt) {
1138 if (TempBlocksExistInExtents(jt->second.extents) ||
1139 TempBlocksExistInExtents(jt->second.write_extents)) {
1140 LOG(INFO) << "bad out edge in node " << idx;
1141 LOG(INFO) << "so yeah";
1142 return false;
1143 }
1144 }
1145 }
1146 return true;
1147}
1148
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001149bool DeltaDiffGenerator::ReorderDataBlobs(
1150 DeltaArchiveManifest* manifest,
1151 const std::string& data_blobs_path,
1152 const std::string& new_data_blobs_path) {
1153 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1154 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1155 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001156
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001157 DirectFileWriter writer;
1158 TEST_AND_RETURN_FALSE(
1159 writer.Open(new_data_blobs_path.c_str(),
1160 O_WRONLY | O_TRUNC | O_CREAT,
1161 0644) == 0);
1162 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001163 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001164
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001165 for (int i = 0; i < (manifest->install_operations_size() +
1166 manifest->kernel_install_operations_size()); i++) {
1167 DeltaArchiveManifest_InstallOperation* op = NULL;
1168 if (i < manifest->install_operations_size()) {
1169 op = manifest->mutable_install_operations(i);
1170 } else {
1171 op = manifest->mutable_kernel_install_operations(
1172 i - manifest->install_operations_size());
1173 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001174 if (!op->has_data_offset())
1175 continue;
1176 CHECK(op->has_data_length());
1177 vector<char> buf(op->data_length());
1178 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1179 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1180
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001181 // Add the hash of the data blobs for this operation
1182 TEST_AND_RETURN_FALSE(AddOperationHash(op, buf));
1183
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001184 op->set_data_offset(out_file_size);
Don Garrette410e0f2011-11-10 15:39:01 -08001185 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001186 out_file_size += buf.size();
1187 }
1188 return true;
1189}
1190
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001191bool DeltaDiffGenerator::AddOperationHash(
1192 DeltaArchiveManifest_InstallOperation* op,
1193 const vector<char>& buf) {
1194 OmahaHashCalculator hasher;
1195
1196 TEST_AND_RETURN_FALSE(hasher.Update(&buf[0], buf.size()));
1197 TEST_AND_RETURN_FALSE(hasher.Finalize());
1198
1199 const vector<char>& hash = hasher.raw_hash();
1200 op->set_data_sha256_hash(hash.data(), hash.size());
1201 return true;
1202}
1203
Andrew de los Reyesef017552010-10-06 17:57:52 -07001204bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1205 const CutEdgeVertexes& cut,
1206 const string& new_root,
1207 int data_fd,
1208 off_t* data_file_size) {
1209 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001210
Andrew de los Reyesef017552010-10-06 17:57:52 -07001211 // Keep all outgoing edges
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001212 if ((*graph)[cut.old_dst].op.type() !=
1213 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ &&
1214 (*graph)[cut.old_dst].op.type() !=
1215 DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
1216 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1217 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001218
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001219 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1220 cut.old_dst,
1221 NULL,
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -08001222 kNonexistentPath,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001223 new_root,
1224 (*graph)[cut.old_dst].file_name,
1225 data_fd,
1226 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001227
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001228 (*graph)[cut.old_dst].out_edges = out_edges;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001229
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001230 // Right now we don't have doubly-linked edges, so we have to scan
1231 // the whole graph.
1232 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1233 }
Andrew de los Reyesef017552010-10-06 17:57:52 -07001234
1235 // Delete temp node
1236 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1237 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1238 (*graph)[cut.old_dst].out_edges.end());
1239 (*graph)[cut.new_vertex].valid = false;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001240 LOG(INFO) << "marked node invalid: " << cut.new_vertex;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001241 return true;
1242}
1243
1244bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1245 const string& new_root,
1246 int fd,
1247 off_t* data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001248 vector<Vertex::Index>* final_order,
1249 Vertex::Index scratch_vertex) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001250 CycleBreaker cycle_breaker;
1251 LOG(INFO) << "Finding cycles...";
1252 set<Edge> cut_edges;
1253 cycle_breaker.BreakCycles(*graph, &cut_edges);
1254 LOG(INFO) << "done finding cycles";
1255 CheckGraph(*graph);
1256
1257 // Calculate number of scratch blocks needed
1258
1259 LOG(INFO) << "Cutting cycles...";
1260 vector<CutEdgeVertexes> cuts;
1261 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1262 LOG(INFO) << "done cutting cycles";
1263 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1264 CheckGraph(*graph);
1265
1266 LOG(INFO) << "Creating initial topological order...";
1267 TopologicalSort(*graph, final_order);
1268 LOG(INFO) << "done with initial topo order";
1269 CheckGraph(*graph);
1270
1271 LOG(INFO) << "Moving full ops to the back";
1272 MoveFullOpsToBack(graph, final_order);
1273 LOG(INFO) << "done moving full ops to back";
1274
1275 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1276 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1277
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001278 SortCutsByTopoOrder(*final_order, &cuts);
1279
Andrew de los Reyesef017552010-10-06 17:57:52 -07001280 if (!cuts.empty())
1281 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1282 new_root,
1283 fd,
1284 data_file_size,
1285 final_order,
1286 &inverse_final_order,
1287 cuts));
1288 LOG(INFO) << "Making sure all temp blocks have been allocated";
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001289
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001290 // Remove the scratch node, if any
1291 if (scratch_vertex != Vertex::kInvalidIndex) {
1292 final_order->erase(final_order->begin() +
1293 inverse_final_order[scratch_vertex]);
1294 (*graph)[scratch_vertex].valid = false;
1295 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1296 }
1297
Andrew de los Reyesef017552010-10-06 17:57:52 -07001298 graph_utils::DumpGraph(*graph);
1299 CHECK(NoTempBlocksRemain(*graph));
1300 LOG(INFO) << "done making sure all temp blocks are allocated";
1301 return true;
1302}
1303
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001304void DeltaDiffGenerator::CreateScratchNode(uint64_t start_block,
1305 uint64_t num_blocks,
1306 Vertex* vertex) {
1307 vertex->file_name = "<scratch>";
1308 vertex->op.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
1309 vertex->op.set_data_offset(0);
1310 vertex->op.set_data_length(0);
1311 Extent* extent = vertex->op.add_dst_extents();
1312 extent->set_start_block(start_block);
1313 extent->set_num_blocks(num_blocks);
1314}
1315
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001316bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1317 const string& old_root,
1318 const string& old_image,
1319 const string& new_root,
1320 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001321 const string& old_kernel_part,
1322 const string& new_kernel_part,
1323 const string& output_path,
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001324 const string& private_key_path,
1325 uint64_t* metadata_size) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001326 int old_image_block_count = 0, old_image_block_size = 0;
1327 int new_image_block_count = 0, new_image_block_size = 0;
1328 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1329 &new_image_block_count,
1330 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001331 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001332 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1333 &old_image_block_count,
1334 &old_image_block_size));
1335 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1336 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1337 << "Old and new images have different block counts.";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001338 }
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001339 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001340 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1341
Darin Petkov7ea32332010-10-13 10:46:11 -07001342 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1343 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1344 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001345 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1346 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1347 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1348 }
1349 Graph graph;
1350 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001351
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001352 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
1353 string temp_file_path;
Darin Petkov7438a5c2011-08-29 11:56:44 -07001354 scoped_ptr<ScopedPathUnlinker> temp_file_unlinker;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001355 off_t data_file_size = 0;
1356
1357 LOG(INFO) << "Reading files...";
1358
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001359 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1360
Andrew de los Reyesef017552010-10-06 17:57:52 -07001361 vector<Vertex::Index> final_order;
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001362 Vertex::Index scratch_vertex = Vertex::kInvalidIndex;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001363 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001364 int fd;
1365 TEST_AND_RETURN_FALSE(
1366 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001367 temp_file_unlinker.reset(new ScopedPathUnlinker(temp_file_path));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001368 TEST_AND_RETURN_FALSE(fd >= 0);
1369 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001370 if (!old_image.empty()) {
1371 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001372
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001373 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1374 &blocks,
1375 old_root,
1376 new_root,
1377 fd,
1378 &data_file_size));
1379 LOG(INFO) << "done reading normal files";
1380 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001381
Thieu Le5c7d9752010-12-15 16:09:28 -08001382 LOG(INFO) << "Starting metadata processing";
1383 TEST_AND_RETURN_FALSE(Metadata::DeltaReadMetadata(&graph,
1384 &blocks,
1385 old_image,
1386 new_image,
1387 fd,
1388 &data_file_size));
1389 LOG(INFO) << "Done metadata processing";
1390 CheckGraph(graph);
1391
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001392 graph.resize(graph.size() + 1);
1393 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1394 fd,
1395 &data_file_size,
1396 new_image,
1397 &graph.back()));
1398
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001399 // Final scratch block (if there's space)
1400 if (blocks.size() < (kRootFSPartitionSize / kBlockSize)) {
1401 scratch_vertex = graph.size();
1402 graph.resize(graph.size() + 1);
1403 CreateScratchNode(blocks.size(),
1404 (kRootFSPartitionSize / kBlockSize) - blocks.size(),
1405 &graph.back());
1406 }
1407
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001408 // Read kernel partition
1409 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1410 new_kernel_part,
1411 &kernel_ops,
1412 fd,
1413 &data_file_size));
1414
1415 LOG(INFO) << "done reading kernel";
1416 CheckGraph(graph);
1417
1418 LOG(INFO) << "Creating edges...";
1419 CreateEdges(&graph, blocks);
1420 LOG(INFO) << "Done creating edges";
1421 CheckGraph(graph);
1422
1423 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1424 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001425 fd,
1426 &data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001427 &final_order,
1428 scratch_vertex));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001429 } else {
1430 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001431 off_t new_image_size =
1432 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Darin Petkov7a22d792010-11-08 14:10:00 -08001433 TEST_AND_RETURN_FALSE(FullUpdateGenerator::Run(&graph,
1434 new_kernel_part,
1435 new_image,
1436 new_image_size,
1437 fd,
1438 &data_file_size,
1439 kFullUpdateChunkSize,
1440 kBlockSize,
1441 &kernel_ops,
1442 &final_order));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001443 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001444 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001445
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001446 // Convert to protobuf Manifest object
1447 DeltaArchiveManifest manifest;
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001448 OperationNameMap op_name_map;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001449 CheckGraph(graph);
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001450 InstallOperationsToManifest(graph,
1451 final_order,
1452 kernel_ops,
1453 &manifest,
1454 &op_name_map);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001455 CheckGraph(graph);
1456 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001457
1458 // Reorder the data blobs with the newly ordered manifest
1459 string ordered_blobs_path;
1460 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
1461 "/tmp/CrAU_temp_data.ordered.XXXXXX",
1462 &ordered_blobs_path,
Andrew de los Reyese05fc282011-06-02 09:50:08 -07001463 NULL));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001464 ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001465 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1466 temp_file_path,
1467 ordered_blobs_path));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001468 temp_file_unlinker.reset();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001469
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001470 // Check that install op blobs are in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001471 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001472 {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001473 for (int i = 0; i < (manifest.install_operations_size() +
1474 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001475 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001476 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001477 manifest.mutable_install_operations(i) :
1478 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001479 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001480 if (op->has_data_offset()) {
1481 if (op->data_offset() != next_blob_offset) {
1482 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001483 << next_blob_offset;
1484 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001485 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001486 }
1487 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001488 }
1489
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001490 // Signatures appear at the end of the blobs. Note the offset in the
1491 // manifest
1492 if (!private_key_path.empty()) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001493 uint64_t signature_blob_length = 0;
1494 TEST_AND_RETURN_FALSE(
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -07001495 PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path),
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001496 &signature_blob_length));
Darin Petkov9574f7e2011-01-13 10:48:12 -08001497 AddSignatureOp(next_blob_offset, signature_blob_length, &manifest);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001498 }
1499
Darin Petkov36a58222010-10-07 22:00:09 -07001500 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1501 new_kernel_part,
1502 old_image,
1503 new_image,
1504 &manifest));
1505
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001506 // Serialize protobuf
1507 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001508
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001509 CheckGraph(graph);
1510 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1511 CheckGraph(graph);
1512
1513 LOG(INFO) << "Writing final delta file header...";
1514 DirectFileWriter writer;
1515 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1516 O_WRONLY | O_CREAT | O_TRUNC,
1517 0644) == 0);
1518 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001519
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001520 // Write header
Don Garrette410e0f2011-11-10 15:39:01 -08001521 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001522
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001523 // Write version number
1524 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001525
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001526 // Write protobuf length
1527 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1528 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001529
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001530 // Write protobuf
1531 LOG(INFO) << "Writing final delta file protobuf... "
1532 << serialized_manifest.size();
1533 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
Don Garrette410e0f2011-11-10 15:39:01 -08001534 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001535
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001536 // Append the data blobs
1537 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001538 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001539 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1540 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1541 for (;;) {
1542 char buf[kBlockSize];
1543 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1544 if (0 == rc) {
1545 // EOF
1546 break;
1547 }
1548 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
Don Garrette410e0f2011-11-10 15:39:01 -08001549 TEST_AND_RETURN_FALSE(writer.Write(buf, rc));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001550 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001551
1552 // Write signature blob.
1553 if (!private_key_path.empty()) {
1554 LOG(INFO) << "Signing the update...";
1555 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -07001556 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
1557 output_path,
1558 vector<string>(1, private_key_path),
1559 &signature_blob));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001560 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
Don Garrette410e0f2011-11-10 15:39:01 -08001561 signature_blob.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001562 }
1563
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001564 *metadata_size =
Darin Petkov95cf01f2010-10-12 14:59:13 -07001565 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001566 ReportPayloadUsage(manifest, *metadata_size, op_name_map);
Darin Petkov880335c2010-10-01 15:52:53 -07001567
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001568 LOG(INFO) << "All done. Successfully created delta file with "
1569 << "metadata size = " << *metadata_size;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001570 return true;
1571}
1572
Thieu Le5c7d9752010-12-15 16:09:28 -08001573// Runs the bsdiff tool on two files and returns the resulting delta in
1574// 'out'. Returns true on success.
1575bool DeltaDiffGenerator::BsdiffFiles(const string& old_file,
1576 const string& new_file,
1577 vector<char>* out) {
1578 const string kPatchFile = "/tmp/delta.patchXXXXXX";
1579 string patch_file_path;
1580
1581 TEST_AND_RETURN_FALSE(
1582 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
1583
1584 vector<string> cmd;
1585 cmd.push_back(kBsdiffPath);
1586 cmd.push_back(old_file);
1587 cmd.push_back(new_file);
1588 cmd.push_back(patch_file_path);
1589
1590 int rc = 1;
1591 vector<char> patch_file;
Darin Petkov85d02b72011-05-17 13:25:51 -07001592 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc, NULL));
Thieu Le5c7d9752010-12-15 16:09:28 -08001593 TEST_AND_RETURN_FALSE(rc == 0);
1594 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
1595 unlink(patch_file_path.c_str());
1596 return true;
1597}
1598
1599// The |blocks| vector contains a reader and writer for each block on the
1600// filesystem that's being in-place updated. We populate the reader/writer
1601// fields of |blocks| by calling this function.
1602// For each block in |operation| that is read or written, find that block
1603// in |blocks| and set the reader/writer field to the vertex passed.
1604// |graph| is not strictly necessary, but useful for printing out
1605// error messages.
1606bool DeltaDiffGenerator::AddInstallOpToBlocksVector(
1607 const DeltaArchiveManifest_InstallOperation& operation,
1608 const Graph& graph,
1609 Vertex::Index vertex,
1610 vector<Block>* blocks) {
1611 // See if this is already present.
1612 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
1613
1614 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
1615 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
1616 const int extents_size =
1617 (field == READER) ? operation.src_extents_size() :
1618 operation.dst_extents_size();
1619 const char* past_participle = (field == READER) ? "read" : "written";
1620 const google::protobuf::RepeatedPtrField<Extent>& extents =
1621 (field == READER) ? operation.src_extents() : operation.dst_extents();
1622 Vertex::Index Block::*access_type =
1623 (field == READER) ? &Block::reader : &Block::writer;
1624
1625 for (int i = 0; i < extents_size; i++) {
1626 const Extent& extent = extents.Get(i);
1627 if (extent.start_block() == kSparseHole) {
1628 // Hole in sparse file. skip
1629 continue;
1630 }
1631 for (uint64_t block = extent.start_block();
1632 block < (extent.start_block() + extent.num_blocks()); block++) {
1633 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
1634 LOG(FATAL) << "Block " << block << " is already "
1635 << past_participle << " by "
1636 << (*blocks)[block].*access_type << "("
1637 << graph[(*blocks)[block].*access_type].file_name
1638 << ") and also " << vertex << "("
1639 << graph[vertex].file_name << ")";
1640 }
1641 (*blocks)[block].*access_type = vertex;
1642 }
1643 }
1644 }
1645 return true;
1646}
1647
Darin Petkov9574f7e2011-01-13 10:48:12 -08001648void DeltaDiffGenerator::AddSignatureOp(uint64_t signature_blob_offset,
1649 uint64_t signature_blob_length,
1650 DeltaArchiveManifest* manifest) {
1651 LOG(INFO) << "Making room for signature in file";
1652 manifest->set_signatures_offset(signature_blob_offset);
1653 LOG(INFO) << "set? " << manifest->has_signatures_offset();
1654 // Add a dummy op at the end to appease older clients
1655 DeltaArchiveManifest_InstallOperation* dummy_op =
1656 manifest->add_kernel_install_operations();
1657 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1658 dummy_op->set_data_offset(signature_blob_offset);
1659 manifest->set_signatures_offset(signature_blob_offset);
1660 dummy_op->set_data_length(signature_blob_length);
1661 manifest->set_signatures_size(signature_blob_length);
1662 Extent* dummy_extent = dummy_op->add_dst_extents();
1663 // Tell the dummy op to write this data to a big sparse hole
1664 dummy_extent->set_start_block(kSparseHole);
1665 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1666 kBlockSize);
1667}
1668
Andrew de los Reyes50f36492010-11-01 13:57:12 -07001669const char* const kBsdiffPath = "bsdiff";
1670const char* const kBspatchPath = "bspatch";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001671const char* const kDeltaMagic = "CrAU";
1672
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001673}; // namespace chromeos_update_engine