blob: d0841388ec06b23b11a5f786294ff00d73a513d2 [file] [log] [blame]
Darin Petkovc0b7a532010-09-29 15:18:14 -07001// Copyright (c) 2010 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>
21#include <base/string_util.h>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070022#include <bzlib.h>
Darin Petkov880335c2010-10-01 15:52:53 -070023
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070024#include "update_engine/bzip.h"
25#include "update_engine/cycle_breaker.h"
26#include "update_engine/extent_mapper.h"
Andrew de los Reyesef017552010-10-06 17:57:52 -070027#include "update_engine/extent_ranges.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070028#include "update_engine/file_writer.h"
29#include "update_engine/filesystem_iterator.h"
Darin Petkov7a22d792010-11-08 14:10:00 -080030#include "update_engine/full_update_generator.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070031#include "update_engine/graph_types.h"
32#include "update_engine/graph_utils.h"
Darin Petkov36a58222010-10-07 22:00:09 -070033#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070034#include "update_engine/payload_signer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070035#include "update_engine/subprocess.h"
36#include "update_engine/topological_sort.h"
37#include "update_engine/update_metadata.pb.h"
38#include "update_engine/utils.h"
39
40using std::make_pair;
Andrew de los Reyesef017552010-10-06 17:57:52 -070041using std::map;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070042using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070043using std::min;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -070044using std::pair;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070045using std::set;
46using std::string;
47using std::vector;
48
49namespace chromeos_update_engine {
50
51typedef DeltaDiffGenerator::Block Block;
Darin Petkov9fa7ec52010-10-18 11:45:23 -070052typedef map<const DeltaArchiveManifest_InstallOperation*,
53 const string*> OperationNameMap;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070054
55namespace {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070056const size_t kBlockSize = 4096; // bytes
Darin Petkov9eadd642010-10-14 15:20:57 -070057const size_t kRootFSPartitionSize = 1 * 1024 * 1024 * 1024; // bytes
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070058const uint64_t kVersionNumber = 1;
Darin Petkov9eadd642010-10-14 15:20:57 -070059const uint64_t kFullUpdateChunkSize = 1024 * 1024; // bytes
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070060
Darin Petkov68c10d12010-10-14 09:24:37 -070061static const char* kInstallOperationTypes[] = {
62 "REPLACE",
63 "REPLACE_BZ",
64 "MOVE",
65 "BSDIFF"
66};
67
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070068// Stores all Extents for a file into 'out'. Returns true on success.
69bool GatherExtents(const string& path,
70 google::protobuf::RepeatedPtrField<Extent>* out) {
71 vector<Extent> extents;
72 TEST_AND_RETURN_FALSE(extent_mapper::ExtentsForFileFibmap(path, &extents));
73 DeltaDiffGenerator::StoreExtents(extents, out);
74 return true;
75}
76
77// Runs the bsdiff tool on two files and returns the resulting delta in
78// 'out'. Returns true on success.
79bool BsdiffFiles(const string& old_file,
80 const string& new_file,
81 vector<char>* out) {
82 const string kPatchFile = "/tmp/delta.patchXXXXXX";
83 string patch_file_path;
84
85 TEST_AND_RETURN_FALSE(
86 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
87
88 vector<string> cmd;
89 cmd.push_back(kBsdiffPath);
90 cmd.push_back(old_file);
91 cmd.push_back(new_file);
92 cmd.push_back(patch_file_path);
93
94 int rc = 1;
95 vector<char> patch_file;
96 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc));
97 TEST_AND_RETURN_FALSE(rc == 0);
98 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
99 unlink(patch_file_path.c_str());
100 return true;
101}
102
103// The blocks vector contains a reader and writer for each block on the
104// filesystem that's being in-place updated. We populate the reader/writer
105// fields of blocks by calling this function.
106// For each block in 'operation' that is read or written, find that block
107// in 'blocks' and set the reader/writer field to the vertex passed.
108// 'graph' is not strictly necessary, but useful for printing out
109// error messages.
110bool AddInstallOpToBlocksVector(
111 const DeltaArchiveManifest_InstallOperation& operation,
112 vector<Block>* blocks,
113 const Graph& graph,
114 Vertex::Index vertex) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700115 // See if this is already present.
116 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700117
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700118 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
119 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
120 const int extents_size =
121 (field == READER) ? operation.src_extents_size() :
122 operation.dst_extents_size();
123 const char* past_participle = (field == READER) ? "read" : "written";
124 const google::protobuf::RepeatedPtrField<Extent>& extents =
125 (field == READER) ? operation.src_extents() : operation.dst_extents();
126 Vertex::Index Block::*access_type =
127 (field == READER) ? &Block::reader : &Block::writer;
128
129 for (int i = 0; i < extents_size; i++) {
130 const Extent& extent = extents.Get(i);
131 if (extent.start_block() == kSparseHole) {
132 // Hole in sparse file. skip
133 continue;
134 }
135 for (uint64_t block = extent.start_block();
136 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700137 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
138 LOG(FATAL) << "Block " << block << " is already "
139 << past_participle << " by "
140 << (*blocks)[block].*access_type << "("
141 << graph[(*blocks)[block].*access_type].file_name
142 << ") and also " << vertex << "("
143 << graph[vertex].file_name << ")";
144 }
145 (*blocks)[block].*access_type = vertex;
146 }
147 }
148 }
149 return true;
150}
151
Andrew de los Reyesef017552010-10-06 17:57:52 -0700152// For a given regular file which must exist at new_root + path, and
153// may exist at old_root + path, creates a new InstallOperation and
154// adds it to the graph. Also, populates the |blocks| array as
155// necessary, if |blocks| is non-NULL. Also, writes the data
156// necessary to send the file down to the client into data_fd, which
157// has length *data_file_size. *data_file_size is updated
158// appropriately. If |existing_vertex| is no kInvalidIndex, use that
159// rather than allocating a new vertex. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700160bool DeltaReadFile(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700161 Vertex::Index existing_vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700162 vector<Block>* blocks,
163 const string& old_root,
164 const string& new_root,
165 const string& path, // within new_root
166 int data_fd,
167 off_t* data_file_size) {
168 vector<char> data;
169 DeltaArchiveManifest_InstallOperation operation;
170
171 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_root + path,
172 new_root + path,
173 &data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700174 &operation,
175 true));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700176
177 // Write the data
178 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
179 operation.set_data_offset(*data_file_size);
180 operation.set_data_length(data.size());
181 }
182
183 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
184 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700185
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700186 // Now, insert into graph and blocks vector
Andrew de los Reyesef017552010-10-06 17:57:52 -0700187 Vertex::Index vertex = existing_vertex;
188 if (vertex == Vertex::kInvalidIndex) {
189 graph->resize(graph->size() + 1);
190 vertex = graph->size() - 1;
191 }
192 (*graph)[vertex].op = operation;
193 CHECK((*graph)[vertex].op.has_type());
194 (*graph)[vertex].file_name = path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700195
Andrew de los Reyesef017552010-10-06 17:57:52 -0700196 if (blocks)
197 TEST_AND_RETURN_FALSE(AddInstallOpToBlocksVector((*graph)[vertex].op,
198 blocks,
199 *graph,
200 vertex));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700201 return true;
202}
203
204// For each regular file within new_root, creates a node in the graph,
205// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
206// and writes any necessary data to the end of data_fd.
207bool DeltaReadFiles(Graph* graph,
208 vector<Block>* blocks,
209 const string& old_root,
210 const string& new_root,
211 int data_fd,
212 off_t* data_file_size) {
213 set<ino_t> visited_inodes;
214 for (FilesystemIterator fs_iter(new_root,
215 utils::SetWithValue<string>("/lost+found"));
216 !fs_iter.IsEnd(); fs_iter.Increment()) {
217 if (!S_ISREG(fs_iter.GetStat().st_mode))
218 continue;
219
220 // Make sure we visit each inode only once.
221 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
222 continue;
223 visited_inodes.insert(fs_iter.GetStat().st_ino);
224 if (fs_iter.GetStat().st_size == 0)
225 continue;
226
227 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700228
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700229 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700230 Vertex::kInvalidIndex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700231 blocks,
232 old_root,
233 new_root,
234 fs_iter.GetPartialPath(),
235 data_fd,
236 data_file_size));
237 }
238 return true;
239}
240
Andrew de los Reyesef017552010-10-06 17:57:52 -0700241// This class allocates non-existent temp blocks, starting from
242// kTempBlockStart. Other code is responsible for converting these
243// temp blocks into real blocks, as the client can't read or write to
244// these blocks.
245class DummyExtentAllocator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700246 public:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700247 explicit DummyExtentAllocator()
248 : next_block_(kTempBlockStart) {}
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700249 vector<Extent> Allocate(const uint64_t block_count) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700250 vector<Extent> ret(1);
251 ret[0].set_start_block(next_block_);
252 ret[0].set_num_blocks(block_count);
253 next_block_ += block_count;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700254 return ret;
255 }
256 private:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700257 uint64_t next_block_;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700258};
259
260// Reads blocks from image_path that are not yet marked as being written
261// in the blocks array. These blocks that remain are non-file-data blocks.
262// In the future we might consider intelligent diffing between this data
263// and data in the previous image, but for now we just bzip2 compress it
264// and include it in the update.
265// Creates a new node in the graph to write these blocks and writes the
266// appropriate blob to blobs_fd. Reads and updates blobs_length;
267bool ReadUnwrittenBlocks(const vector<Block>& blocks,
268 int blobs_fd,
269 off_t* blobs_length,
270 const string& image_path,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700271 Vertex* vertex) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700272 vertex->file_name = "<rootfs-non-file-data>";
273
Andrew de los Reyesef017552010-10-06 17:57:52 -0700274 DeltaArchiveManifest_InstallOperation* out_op = &vertex->op;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700275 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
276 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
277 ScopedFdCloser image_fd_closer(&image_fd);
278
279 string temp_file_path;
280 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX",
281 &temp_file_path,
282 NULL));
283
284 FILE* file = fopen(temp_file_path.c_str(), "w");
285 TEST_AND_RETURN_FALSE(file);
286 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700287
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700288 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
289 file,
290 9, // max compression
291 0, // verbosity
292 0); // default work factor
293 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700294
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700295 vector<Extent> extents;
296 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700297
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700298 LOG(INFO) << "Appending left over blocks to extents";
299 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
300 if (blocks[i].writer != Vertex::kInvalidIndex)
301 continue;
Andrew de los Reyesef017552010-10-06 17:57:52 -0700302 if (blocks[i].reader != Vertex::kInvalidIndex) {
303 graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i);
304 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700305 graph_utils::AppendBlockToExtents(&extents, i);
306 block_count++;
307 }
308
309 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
310 // so we arbitrarily set it to 1024 * kBlockSize.
311 vector<char> buf(1024 * kBlockSize);
312
313 LOG(INFO) << "Reading left over blocks";
314 vector<Block>::size_type blocks_copied_count = 0;
315
316 // For each extent in extents, write the data into BZ2_bzWrite which
317 // sends it to an output file.
318 // We use the temporary buffer 'buf' to hold the data, which may be
319 // smaller than the extent, so in that case we have to loop to get
320 // the extent's data (that's the inner while loop).
321 for (vector<Extent>::const_iterator it = extents.begin();
322 it != extents.end(); ++it) {
323 vector<Block>::size_type blocks_read = 0;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800324 float printed_progress = -1;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700325 while (blocks_read < it->num_blocks()) {
326 const int copy_block_cnt =
327 min(buf.size() / kBlockSize,
328 static_cast<vector<char>::size_type>(
329 it->num_blocks() - blocks_read));
330 ssize_t rc = pread(image_fd,
331 &buf[0],
332 copy_block_cnt * kBlockSize,
333 (it->start_block() + blocks_read) * kBlockSize);
334 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
335 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
336 copy_block_cnt * kBlockSize);
337 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
338 TEST_AND_RETURN_FALSE(err == BZ_OK);
339 blocks_read += copy_block_cnt;
340 blocks_copied_count += copy_block_cnt;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800341 float current_progress =
342 static_cast<float>(blocks_copied_count) / block_count;
343 if (printed_progress + 0.1 < current_progress ||
344 blocks_copied_count == block_count) {
345 LOG(INFO) << "progress: " << current_progress;
346 printed_progress = current_progress;
347 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700348 }
349 }
350 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
351 TEST_AND_RETURN_FALSE(err == BZ_OK);
352 bz_file = NULL;
353 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
354 file = NULL;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700355
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700356 vector<char> compressed_data;
357 LOG(INFO) << "Reading compressed data off disk";
358 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
359 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700360
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700361 // Add node to graph to write these blocks
362 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
363 out_op->set_data_offset(*blobs_length);
364 out_op->set_data_length(compressed_data.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700365 LOG(INFO) << "Rootfs non-data blocks compressed take up "
366 << compressed_data.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700367 *blobs_length += compressed_data.size();
368 out_op->set_dst_length(kBlockSize * block_count);
369 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700370
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700371 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
372 &compressed_data[0],
373 compressed_data.size()));
374 LOG(INFO) << "done with extra blocks";
375 return true;
376}
377
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700378// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700379// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700380bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
381 uint64_t value_be = htobe64(value);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700382 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)) ==
383 sizeof(value_be));
384 return true;
385}
386
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700387// Adds each operation from |graph| to |out_manifest| in the order specified by
388// |order| while building |out_op_name_map| with operation to name
389// mappings. Adds all |kernel_ops| to |out_manifest|. Filters out no-op
390// operations.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700391void InstallOperationsToManifest(
392 const Graph& graph,
393 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700394 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700395 DeltaArchiveManifest* out_manifest,
396 OperationNameMap* out_op_name_map) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700397 for (vector<Vertex::Index>::const_iterator it = order.begin();
398 it != order.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700399 const Vertex& vertex = graph[*it];
400 const DeltaArchiveManifest_InstallOperation& add_op = vertex.op;
401 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
402 continue;
403 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700404 DeltaArchiveManifest_InstallOperation* op =
405 out_manifest->add_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700406 *op = add_op;
407 (*out_op_name_map)[op] = &vertex.file_name;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700408 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700409 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
410 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700411 const DeltaArchiveManifest_InstallOperation& add_op = *it;
412 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
413 continue;
414 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700415 DeltaArchiveManifest_InstallOperation* op =
416 out_manifest->add_kernel_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700417 *op = add_op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700418 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700419}
420
421void CheckGraph(const Graph& graph) {
422 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
423 CHECK(it->op.has_type());
424 }
425}
426
Darin Petkov68c10d12010-10-14 09:24:37 -0700427// Delta compresses a kernel partition |new_kernel_part| with knowledge of the
428// old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty
429// string, generates a full update of the partition.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700430bool DeltaCompressKernelPartition(
431 const string& old_kernel_part,
432 const string& new_kernel_part,
433 vector<DeltaArchiveManifest_InstallOperation>* ops,
434 int blobs_fd,
435 off_t* blobs_length) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700436 LOG(INFO) << "Delta compressing kernel partition...";
Darin Petkov68c10d12010-10-14 09:24:37 -0700437 LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update...";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700438
439 // Add a new install operation
440 ops->resize(1);
441 DeltaArchiveManifest_InstallOperation* op = &(*ops)[0];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700442
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700443 vector<char> data;
Darin Petkov68c10d12010-10-14 09:24:37 -0700444 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_kernel_part,
445 new_kernel_part,
446 &data,
447 op,
448 false));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700449
Darin Petkov68c10d12010-10-14 09:24:37 -0700450 // Write the data
451 if (op->type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
452 op->set_data_offset(*blobs_length);
453 op->set_data_length(data.size());
454 }
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700455
Darin Petkov68c10d12010-10-14 09:24:37 -0700456 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
457 *blobs_length += data.size();
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700458
Darin Petkov68c10d12010-10-14 09:24:37 -0700459 LOG(INFO) << "Done delta compressing kernel partition: "
460 << kInstallOperationTypes[op->type()];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700461 return true;
462}
463
Darin Petkov880335c2010-10-01 15:52:53 -0700464struct DeltaObject {
465 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
466 : name(in_name),
467 type(in_type),
468 size(in_size) {}
469 bool operator <(const DeltaObject& object) const {
Darin Petkovd43d6902010-10-14 11:17:50 -0700470 return (size != object.size) ? (size < object.size) : (name < object.name);
Darin Petkov880335c2010-10-01 15:52:53 -0700471 }
472 string name;
473 int type;
474 off_t size;
475};
476
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700477void ReportPayloadUsage(const DeltaArchiveManifest& manifest,
478 const int64_t manifest_metadata_size,
479 const OperationNameMap& op_name_map) {
Darin Petkov880335c2010-10-01 15:52:53 -0700480 vector<DeltaObject> objects;
481 off_t total_size = 0;
482
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700483 // Rootfs install operations.
484 for (int i = 0; i < manifest.install_operations_size(); ++i) {
485 const DeltaArchiveManifest_InstallOperation& op =
486 manifest.install_operations(i);
487 objects.push_back(DeltaObject(*op_name_map.find(&op)->second,
488 op.type(),
489 op.data_length()));
490 total_size += op.data_length();
Darin Petkov880335c2010-10-01 15:52:53 -0700491 }
492
Darin Petkov880335c2010-10-01 15:52:53 -0700493 // Kernel install operations.
494 for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) {
495 const DeltaArchiveManifest_InstallOperation& op =
496 manifest.kernel_install_operations(i);
497 objects.push_back(DeltaObject(StringPrintf("<kernel-operation-%d>", i),
498 op.type(),
499 op.data_length()));
500 total_size += op.data_length();
501 }
502
Darin Petkov95cf01f2010-10-12 14:59:13 -0700503 objects.push_back(DeltaObject("<manifest-metadata>",
504 -1,
505 manifest_metadata_size));
506 total_size += manifest_metadata_size;
507
Darin Petkov880335c2010-10-01 15:52:53 -0700508 std::sort(objects.begin(), objects.end());
509
510 static const char kFormatString[] = "%6.2f%% %10llu %-10s %s\n";
511 for (vector<DeltaObject>::const_iterator it = objects.begin();
512 it != objects.end(); ++it) {
513 const DeltaObject& object = *it;
514 fprintf(stderr, kFormatString,
515 object.size * 100.0 / total_size,
516 object.size,
Darin Petkov95cf01f2010-10-12 14:59:13 -0700517 object.type >= 0 ? kInstallOperationTypes[object.type] : "-",
Darin Petkov880335c2010-10-01 15:52:53 -0700518 object.name.c_str());
519 }
520 fprintf(stderr, kFormatString, 100.0, total_size, "", "<total>");
521}
522
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700523} // namespace {}
524
525bool DeltaDiffGenerator::ReadFileToDiff(
526 const string& old_filename,
527 const string& new_filename,
528 vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700529 DeltaArchiveManifest_InstallOperation* out_op,
530 bool gather_extents) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700531 // Read new data in
532 vector<char> new_data;
533 TEST_AND_RETURN_FALSE(utils::ReadFile(new_filename, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700534
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700535 TEST_AND_RETURN_FALSE(!new_data.empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700536
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700537 vector<char> new_data_bz;
538 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
539 CHECK(!new_data_bz.empty());
540
541 vector<char> data; // Data blob that will be written to delta file.
542
543 DeltaArchiveManifest_InstallOperation operation;
544 size_t current_best_size = 0;
545 if (new_data.size() <= new_data_bz.size()) {
546 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
547 current_best_size = new_data.size();
548 data = new_data;
549 } else {
550 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
551 current_best_size = new_data_bz.size();
552 data = new_data_bz;
553 }
554
555 // Do we have an original file to consider?
556 struct stat old_stbuf;
Darin Petkov68c10d12010-10-14 09:24:37 -0700557 bool no_original = old_filename.empty();
558 if (!no_original && 0 != stat(old_filename.c_str(), &old_stbuf)) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700559 // If stat-ing the old file fails, it should be because it doesn't exist.
560 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
Darin Petkov68c10d12010-10-14 09:24:37 -0700561 no_original = true;
562 }
563 if (!no_original) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700564 // Read old data
565 vector<char> old_data;
566 TEST_AND_RETURN_FALSE(utils::ReadFile(old_filename, &old_data));
567 if (old_data == new_data) {
568 // No change in data.
569 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
570 current_best_size = 0;
571 data.clear();
572 } else {
573 // Try bsdiff of old to new data
574 vector<char> bsdiff_delta;
575 TEST_AND_RETURN_FALSE(
576 BsdiffFiles(old_filename, new_filename, &bsdiff_delta));
577 CHECK_GT(bsdiff_delta.size(), 0);
578 if (bsdiff_delta.size() < current_best_size) {
579 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
580 current_best_size = bsdiff_delta.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700581
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700582 data = bsdiff_delta;
583 }
584 }
585 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700586
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700587 // Set parameters of the operations
588 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700589
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700590 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
591 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Darin Petkov68c10d12010-10-14 09:24:37 -0700592 if (gather_extents) {
593 TEST_AND_RETURN_FALSE(
594 GatherExtents(old_filename, operation.mutable_src_extents()));
595 } else {
596 Extent* src_extent = operation.add_src_extents();
597 src_extent->set_start_block(0);
598 src_extent->set_num_blocks(
599 (old_stbuf.st_size + kBlockSize - 1) / kBlockSize);
600 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700601 operation.set_src_length(old_stbuf.st_size);
602 }
603
Darin Petkov68c10d12010-10-14 09:24:37 -0700604 if (gather_extents) {
605 TEST_AND_RETURN_FALSE(
606 GatherExtents(new_filename, operation.mutable_dst_extents()));
607 } else {
608 Extent* dst_extent = operation.add_dst_extents();
609 dst_extent->set_start_block(0);
610 dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize);
611 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700612 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700613
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700614 out_data->swap(data);
615 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700616
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700617 return true;
618}
619
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700620bool DeltaDiffGenerator::InitializePartitionInfo(bool is_kernel,
621 const string& partition,
622 PartitionInfo* info) {
Darin Petkov7ea32332010-10-13 10:46:11 -0700623 int64_t size = 0;
624 if (is_kernel) {
625 size = utils::FileSize(partition);
626 } else {
627 int block_count = 0, block_size = 0;
628 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition,
629 &block_count,
630 &block_size));
631 size = static_cast<int64_t>(block_count) * block_size;
632 }
633 TEST_AND_RETURN_FALSE(size > 0);
Darin Petkov36a58222010-10-07 22:00:09 -0700634 info->set_size(size);
635 OmahaHashCalculator hasher;
Darin Petkov7ea32332010-10-13 10:46:11 -0700636 TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size);
Darin Petkov36a58222010-10-07 22:00:09 -0700637 TEST_AND_RETURN_FALSE(hasher.Finalize());
638 const vector<char>& hash = hasher.raw_hash();
639 info->set_hash(hash.data(), hash.size());
Darin Petkovd43d6902010-10-14 11:17:50 -0700640 LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash();
Darin Petkov36a58222010-10-07 22:00:09 -0700641 return true;
642}
643
644bool InitializePartitionInfos(const string& old_kernel,
645 const string& new_kernel,
646 const string& old_rootfs,
647 const string& new_rootfs,
648 DeltaArchiveManifest* manifest) {
Darin Petkovd43d6902010-10-14 11:17:50 -0700649 if (!old_kernel.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700650 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
651 true,
652 old_kernel,
653 manifest->mutable_old_kernel_info()));
Darin Petkovd43d6902010-10-14 11:17:50 -0700654 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700655 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
656 true,
657 new_kernel,
658 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700659 if (!old_rootfs.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700660 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
661 false,
662 old_rootfs,
663 manifest->mutable_old_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700664 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700665 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
666 false,
667 new_rootfs,
668 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700669 return true;
670}
671
Andrew de los Reyesef017552010-10-06 17:57:52 -0700672namespace {
673
674// Takes a collection (vector or RepeatedPtrField) of Extent and
675// returns a vector of the blocks referenced, in order.
676template<typename T>
677vector<uint64_t> ExpandExtents(const T& extents) {
678 vector<uint64_t> ret;
679 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
680 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700681 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700682 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700683 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700684 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700685 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700686 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700687 }
688 }
689 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700690 return ret;
691}
692
693// Takes a vector of blocks and returns an equivalent vector of Extent
694// objects.
695vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
696 vector<Extent> new_extents;
697 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
698 it != e; ++it) {
699 graph_utils::AppendBlockToExtents(&new_extents, *it);
700 }
701 return new_extents;
702}
703
704} // namespace {}
705
706void DeltaDiffGenerator::SubstituteBlocks(
707 Vertex* vertex,
708 const vector<Extent>& remove_extents,
709 const vector<Extent>& replace_extents) {
710 // First, expand out the blocks that op reads from
711 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700712 {
713 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700714 vector<uint64_t> remove_extents_expanded =
715 ExpandExtents(remove_extents);
716 vector<uint64_t> replace_extents_expanded =
717 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700718 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700719 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700720 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700721 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700722 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
723 }
724 utils::ApplyMap(&read_blocks, conversion);
725 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
726 e = vertex->out_edges.end(); it != e; ++it) {
727 vector<uint64_t> write_before_deps_expanded =
728 ExpandExtents(it->second.write_extents);
729 utils::ApplyMap(&write_before_deps_expanded, conversion);
730 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700731 }
732 }
733 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700734 vertex->op.clear_src_extents();
735 vector<Extent> new_extents = CompressExtents(read_blocks);
736 DeltaDiffGenerator::StoreExtents(new_extents,
737 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700738}
739
740bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700741 const set<Edge>& edges,
742 vector<CutEdgeVertexes>* out_cuts) {
743 DummyExtentAllocator scratch_allocator;
744 vector<CutEdgeVertexes> cuts;
745 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700746
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700747 uint64_t scratch_blocks_used = 0;
748 for (set<Edge>::const_iterator it = edges.begin();
749 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700750 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700751 vector<Extent> old_extents =
752 (*graph)[it->first].out_edges[it->second].extents;
753 // Choose some scratch space
754 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700755 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700756 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
757 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700758 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700759 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700760 cuts.back().old_src = it->first;
761 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700762
Andrew de los Reyesef017552010-10-06 17:57:52 -0700763 EdgeProperties& cut_edge_properties =
764 (*graph)[it->first].out_edges.find(it->second)->second;
765
766 // This should never happen, as we should only be cutting edges between
767 // real file nodes, and write-before relationships are created from
768 // a real file node to a temp copy node:
769 CHECK(cut_edge_properties.write_extents.empty())
770 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700771
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700772 // make node depend on the copy operation
773 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700774 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700775
776 // Set src/dst extents and other proto variables for copy operation
777 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
778 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700779 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700780 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700781 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700782 graph->back().op.mutable_dst_extents());
783 graph->back().op.set_src_length(
784 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
785 graph->back().op.set_dst_length(graph->back().op.src_length());
786
787 // make the dest node read from the scratch space
788 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700789 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700790 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700791 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700792
793 // delete the old edge
794 CHECK_EQ(1, (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700795
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700796 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700797 EdgeProperties write_before_edge_properties;
798 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
799 (*graph)[it->second].out_edges.insert(
800 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700801 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700802 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700803 return true;
804}
805
806// Stores all Extents in 'extents' into 'out'.
807void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700808 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700809 google::protobuf::RepeatedPtrField<Extent>* out) {
810 for (vector<Extent>::const_iterator it = extents.begin();
811 it != extents.end(); ++it) {
812 Extent* new_extent = out->Add();
813 *new_extent = *it;
814 }
815}
816
817// Creates all the edges for the graph. Writers of a block point to
818// readers of the same block. This is because for an edge A->B, B
819// must complete before A executes.
820void DeltaDiffGenerator::CreateEdges(Graph* graph,
821 const vector<Block>& blocks) {
822 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
823 // Blocks with both a reader and writer get an edge
824 if (blocks[i].reader == Vertex::kInvalidIndex ||
825 blocks[i].writer == Vertex::kInvalidIndex)
826 continue;
827 // Don't have a node depend on itself
828 if (blocks[i].reader == blocks[i].writer)
829 continue;
830 // See if there's already an edge we can add onto
831 Vertex::EdgeMap::iterator edge_it =
832 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
833 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
834 // No existing edge. Create one
835 (*graph)[blocks[i].writer].out_edges.insert(
836 make_pair(blocks[i].reader, EdgeProperties()));
837 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700838 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700839 }
840 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
841 }
842}
843
Andrew de los Reyesef017552010-10-06 17:57:52 -0700844namespace {
845
846class SortCutsByTopoOrderLess {
847 public:
848 SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table)
849 : table_(table) {}
850 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
851 return table_[a.old_dst] < table_[b.old_dst];
852 }
853 private:
854 vector<vector<Vertex::Index>::size_type>& table_;
855};
856
857} // namespace {}
858
859void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
860 vector<Vertex::Index>& op_indexes,
861 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
862 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
863 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
864 i != e; ++i) {
865 Vertex::Index node = op_indexes[i];
866 if (table.size() < (node + 1)) {
867 table.resize(node + 1);
868 }
869 table[node] = i;
870 }
871 reverse_op_indexes->swap(table);
872}
873
874void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes,
875 vector<CutEdgeVertexes>* cuts) {
876 // first, make a reverse lookup table.
877 vector<vector<Vertex::Index>::size_type> table;
878 GenerateReverseTopoOrderMap(op_indexes, &table);
879 SortCutsByTopoOrderLess less(table);
880 sort(cuts->begin(), cuts->end(), less);
881}
882
883void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
884 vector<Vertex::Index>* op_indexes) {
885 vector<Vertex::Index> ret;
886 vector<Vertex::Index> full_ops;
887 ret.reserve(op_indexes->size());
888 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
889 ++i) {
890 DeltaArchiveManifest_InstallOperation_Type type =
891 (*graph)[(*op_indexes)[i]].op.type();
892 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
893 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
894 full_ops.push_back((*op_indexes)[i]);
895 } else {
896 ret.push_back((*op_indexes)[i]);
897 }
898 }
899 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
900 << (full_ops.size() + ret.size()) << " total ops.";
901 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
902 op_indexes->swap(ret);
903}
904
905namespace {
906
907template<typename T>
908bool TempBlocksExistInExtents(const T& extents) {
909 for (int i = 0, e = extents.size(); i < e; ++i) {
910 Extent extent = graph_utils::GetElement(extents, i);
911 uint64_t start = extent.start_block();
912 uint64_t num = extent.num_blocks();
913 if (start == kSparseHole)
914 continue;
915 if (start >= kTempBlockStart ||
916 (start + num) >= kTempBlockStart) {
917 LOG(ERROR) << "temp block!";
918 LOG(ERROR) << "start: " << start << ", num: " << num;
919 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
920 LOG(ERROR) << "returning true";
921 return true;
922 }
923 // check for wrap-around, which would be a bug:
924 CHECK(start <= (start + num));
925 }
926 return false;
927}
928
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700929// Convertes the cuts, which must all have the same |old_dst| member,
930// to full. It does this by converting the |old_dst| to REPLACE or
931// REPLACE_BZ, dropping all incoming edges to |old_dst|, and marking
932// all temp nodes invalid.
933bool ConvertCutsToFull(
934 Graph* graph,
935 const string& new_root,
936 int data_fd,
937 off_t* data_file_size,
938 vector<Vertex::Index>* op_indexes,
939 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
940 const vector<CutEdgeVertexes>& cuts) {
941 CHECK(!cuts.empty());
942 set<Vertex::Index> deleted_nodes;
943 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
944 e = cuts.end(); it != e; ++it) {
945 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp(
946 graph,
947 *it,
948 new_root,
949 data_fd,
950 data_file_size));
951 deleted_nodes.insert(it->new_vertex);
952 }
953 deleted_nodes.insert(cuts[0].old_dst);
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700954
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700955 vector<Vertex::Index> new_op_indexes;
956 new_op_indexes.reserve(op_indexes->size());
957 for (vector<Vertex::Index>::iterator it = op_indexes->begin(),
958 e = op_indexes->end(); it != e; ++it) {
959 if (utils::SetContainsKey(deleted_nodes, *it))
960 continue;
961 new_op_indexes.push_back(*it);
962 }
963 new_op_indexes.push_back(cuts[0].old_dst);
964 op_indexes->swap(new_op_indexes);
965 DeltaDiffGenerator::GenerateReverseTopoOrderMap(*op_indexes,
966 reverse_op_indexes);
967 return true;
968}
969
970// Tries to assign temp blocks for a collection of cuts, all of which share
971// the same old_dst member. If temp blocks can't be found, old_dst will be
972// converted to a REPLACE or REPLACE_BZ operation. Returns true on success,
973// which can happen even if blocks are converted to full. Returns false
974// on exceptional error cases.
975bool AssignBlockForAdjoiningCuts(
976 Graph* graph,
977 const string& new_root,
978 int data_fd,
979 off_t* data_file_size,
980 vector<Vertex::Index>* op_indexes,
981 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
982 const vector<CutEdgeVertexes>& cuts) {
983 CHECK(!cuts.empty());
984 const Vertex::Index old_dst = cuts[0].old_dst;
985 // Calculate # of blocks needed
986 uint64_t blocks_needed = 0;
987 map<const CutEdgeVertexes*, uint64_t> cuts_blocks_needed;
988 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
989 e = cuts.end(); it != e; ++it) {
990 uint64_t cut_blocks_needed = 0;
991 for (vector<Extent>::const_iterator jt = it->tmp_extents.begin(),
992 je = it->tmp_extents.end(); jt != je; ++jt) {
993 cut_blocks_needed += jt->num_blocks();
994 }
995 blocks_needed += cut_blocks_needed;
996 cuts_blocks_needed[&*it] = cut_blocks_needed;
997 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700998
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700999 // Find enough blocks
1000 ExtentRanges scratch_ranges;
1001 // Each block that's supplying temp blocks and the corresponding blocks:
1002 typedef vector<pair<Vertex::Index, ExtentRanges> > SupplierVector;
1003 SupplierVector block_suppliers;
1004 uint64_t scratch_blocks_found = 0;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001005 for (vector<Vertex::Index>::size_type i = (*reverse_op_indexes)[old_dst] + 1,
1006 e = op_indexes->size(); i < e; ++i) {
1007 Vertex::Index test_node = (*op_indexes)[i];
1008 if (!(*graph)[test_node].valid)
1009 continue;
1010 // See if this node has sufficient blocks
1011 ExtentRanges ranges;
1012 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
1013 ranges.SubtractExtent(ExtentForRange(
1014 kTempBlockStart, kSparseHole - kTempBlockStart));
1015 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
1016 // For now, for simplicity, subtract out all blocks in read-before
1017 // dependencies.
1018 for (Vertex::EdgeMap::const_iterator edge_i =
1019 (*graph)[test_node].out_edges.begin(),
1020 edge_e = (*graph)[test_node].out_edges.end();
1021 edge_i != edge_e; ++edge_i) {
1022 ranges.SubtractExtents(edge_i->second.extents);
1023 }
1024 if (ranges.blocks() == 0)
1025 continue;
1026
1027 if (ranges.blocks() + scratch_blocks_found > blocks_needed) {
1028 // trim down ranges
1029 vector<Extent> new_ranges = ranges.GetExtentsForBlockCount(
1030 blocks_needed - scratch_blocks_found);
1031 ranges = ExtentRanges();
1032 ranges.AddExtents(new_ranges);
1033 }
1034 scratch_ranges.AddRanges(ranges);
1035 block_suppliers.push_back(make_pair(test_node, ranges));
1036 scratch_blocks_found += ranges.blocks();
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001037 if (scratch_ranges.blocks() >= blocks_needed)
1038 break;
1039 }
1040 if (scratch_ranges.blocks() < blocks_needed) {
1041 LOG(INFO) << "Unable to find sufficient scratch";
1042 TEST_AND_RETURN_FALSE(ConvertCutsToFull(graph,
1043 new_root,
1044 data_fd,
1045 data_file_size,
1046 op_indexes,
1047 reverse_op_indexes,
1048 cuts));
1049 return true;
1050 }
1051 // Use the scratch we found
1052 TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found);
1053
1054 // Make all the suppliers depend on this node
1055 for (SupplierVector::iterator it = block_suppliers.begin(),
1056 e = block_suppliers.end(); it != e; ++it) {
1057 graph_utils::AddReadBeforeDepExtents(
1058 &(*graph)[it->first],
1059 old_dst,
1060 it->second.GetExtentsForBlockCount(it->second.blocks()));
1061 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001062
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001063 // Replace temp blocks in each cut
1064 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1065 e = cuts.end(); it != e; ++it) {
1066 vector<Extent> real_extents =
1067 scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]);
1068 scratch_ranges.SubtractExtents(real_extents);
1069
1070 // Fix the old dest node w/ the real blocks
1071 DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst],
1072 it->tmp_extents,
1073 real_extents);
1074
1075 // Fix the new node w/ the real blocks. Since the new node is just a
1076 // copy operation, we can replace all the dest extents w/ the real
1077 // blocks.
1078 DeltaArchiveManifest_InstallOperation *op =
1079 &(*graph)[it->new_vertex].op;
1080 op->clear_dst_extents();
1081 DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents());
1082 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001083 return true;
1084}
1085
Andrew de los Reyesef017552010-10-06 17:57:52 -07001086} // namespace {}
1087
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001088// Returns true if |op| is a no-op operation that doesn't do any useful work
1089// (e.g., a move operation that copies blocks onto themselves).
1090bool DeltaDiffGenerator::IsNoopOperation(
1091 const DeltaArchiveManifest_InstallOperation& op) {
1092 return (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE &&
1093 ExpandExtents(op.src_extents()) == ExpandExtents(op.dst_extents()));
1094}
1095
Andrew de los Reyesef017552010-10-06 17:57:52 -07001096bool DeltaDiffGenerator::AssignTempBlocks(
1097 Graph* graph,
1098 const string& new_root,
1099 int data_fd,
1100 off_t* data_file_size,
1101 vector<Vertex::Index>* op_indexes,
1102 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001103 const vector<CutEdgeVertexes>& cuts) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001104 CHECK(!cuts.empty());
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001105
1106 // group of cuts w/ the same old_dst:
1107 vector<CutEdgeVertexes> cuts_group;
1108
Andrew de los Reyesef017552010-10-06 17:57:52 -07001109 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
1110 true ; --i) {
1111 LOG(INFO) << "Fixing temp blocks in cut " << i
1112 << ": old dst: " << cuts[i].old_dst << " new vertex: "
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001113 << cuts[i].new_vertex << " path: "
1114 << (*graph)[cuts[i].old_dst].file_name;
1115
1116 if (cuts_group.empty() || (cuts_group[0].old_dst == cuts[i].old_dst)) {
1117 cuts_group.push_back(cuts[i]);
1118 } else {
1119 CHECK(!cuts_group.empty());
1120 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1121 new_root,
1122 data_fd,
1123 data_file_size,
1124 op_indexes,
1125 reverse_op_indexes,
1126 cuts_group));
1127 cuts_group.clear();
1128 cuts_group.push_back(cuts[i]);
Andrew de los Reyesef017552010-10-06 17:57:52 -07001129 }
Darin Petkov36a58222010-10-07 22:00:09 -07001130
Andrew de los Reyesef017552010-10-06 17:57:52 -07001131 if (i == e) {
1132 // break out of for() loop
1133 break;
1134 }
1135 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001136 CHECK(!cuts_group.empty());
1137 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1138 new_root,
1139 data_fd,
1140 data_file_size,
1141 op_indexes,
1142 reverse_op_indexes,
1143 cuts_group));
Andrew de los Reyesef017552010-10-06 17:57:52 -07001144 return true;
1145}
1146
1147bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1148 size_t idx = 0;
1149 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1150 ++it, ++idx) {
1151 if (!it->valid)
1152 continue;
1153 const DeltaArchiveManifest_InstallOperation& op = it->op;
1154 if (TempBlocksExistInExtents(op.dst_extents()) ||
1155 TempBlocksExistInExtents(op.src_extents())) {
1156 LOG(INFO) << "bad extents in node " << idx;
1157 LOG(INFO) << "so yeah";
1158 return false;
1159 }
1160
1161 // Check out-edges:
1162 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1163 je = it->out_edges.end(); jt != je; ++jt) {
1164 if (TempBlocksExistInExtents(jt->second.extents) ||
1165 TempBlocksExistInExtents(jt->second.write_extents)) {
1166 LOG(INFO) << "bad out edge in node " << idx;
1167 LOG(INFO) << "so yeah";
1168 return false;
1169 }
1170 }
1171 }
1172 return true;
1173}
1174
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001175bool DeltaDiffGenerator::ReorderDataBlobs(
1176 DeltaArchiveManifest* manifest,
1177 const std::string& data_blobs_path,
1178 const std::string& new_data_blobs_path) {
1179 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1180 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1181 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001182
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001183 DirectFileWriter writer;
1184 TEST_AND_RETURN_FALSE(
1185 writer.Open(new_data_blobs_path.c_str(),
1186 O_WRONLY | O_TRUNC | O_CREAT,
1187 0644) == 0);
1188 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001189 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001190
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001191 for (int i = 0; i < (manifest->install_operations_size() +
1192 manifest->kernel_install_operations_size()); i++) {
1193 DeltaArchiveManifest_InstallOperation* op = NULL;
1194 if (i < manifest->install_operations_size()) {
1195 op = manifest->mutable_install_operations(i);
1196 } else {
1197 op = manifest->mutable_kernel_install_operations(
1198 i - manifest->install_operations_size());
1199 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001200 if (!op->has_data_offset())
1201 continue;
1202 CHECK(op->has_data_length());
1203 vector<char> buf(op->data_length());
1204 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1205 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1206
1207 op->set_data_offset(out_file_size);
1208 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()) ==
1209 static_cast<ssize_t>(buf.size()));
1210 out_file_size += buf.size();
1211 }
1212 return true;
1213}
1214
Andrew de los Reyesef017552010-10-06 17:57:52 -07001215bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1216 const CutEdgeVertexes& cut,
1217 const string& new_root,
1218 int data_fd,
1219 off_t* data_file_size) {
1220 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001221
Andrew de los Reyesef017552010-10-06 17:57:52 -07001222 // Keep all outgoing edges
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001223 if ((*graph)[cut.old_dst].op.type() !=
1224 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ &&
1225 (*graph)[cut.old_dst].op.type() !=
1226 DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
1227 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1228 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001229
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001230 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1231 cut.old_dst,
1232 NULL,
1233 "/-!@:&*nonexistent_path",
1234 new_root,
1235 (*graph)[cut.old_dst].file_name,
1236 data_fd,
1237 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001238
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001239 (*graph)[cut.old_dst].out_edges = out_edges;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001240
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001241 // Right now we don't have doubly-linked edges, so we have to scan
1242 // the whole graph.
1243 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1244 }
Andrew de los Reyesef017552010-10-06 17:57:52 -07001245
1246 // Delete temp node
1247 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1248 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1249 (*graph)[cut.old_dst].out_edges.end());
1250 (*graph)[cut.new_vertex].valid = false;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001251 LOG(INFO) << "marked node invalid: " << cut.new_vertex;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001252 return true;
1253}
1254
1255bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1256 const string& new_root,
1257 int fd,
1258 off_t* data_file_size,
1259 vector<Vertex::Index>* final_order) {
1260 CycleBreaker cycle_breaker;
1261 LOG(INFO) << "Finding cycles...";
1262 set<Edge> cut_edges;
1263 cycle_breaker.BreakCycles(*graph, &cut_edges);
1264 LOG(INFO) << "done finding cycles";
1265 CheckGraph(*graph);
1266
1267 // Calculate number of scratch blocks needed
1268
1269 LOG(INFO) << "Cutting cycles...";
1270 vector<CutEdgeVertexes> cuts;
1271 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1272 LOG(INFO) << "done cutting cycles";
1273 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1274 CheckGraph(*graph);
1275
1276 LOG(INFO) << "Creating initial topological order...";
1277 TopologicalSort(*graph, final_order);
1278 LOG(INFO) << "done with initial topo order";
1279 CheckGraph(*graph);
1280
1281 LOG(INFO) << "Moving full ops to the back";
1282 MoveFullOpsToBack(graph, final_order);
1283 LOG(INFO) << "done moving full ops to back";
1284
1285 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1286 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1287
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001288 SortCutsByTopoOrder(*final_order, &cuts);
1289
Andrew de los Reyesef017552010-10-06 17:57:52 -07001290 if (!cuts.empty())
1291 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1292 new_root,
1293 fd,
1294 data_file_size,
1295 final_order,
1296 &inverse_final_order,
1297 cuts));
1298 LOG(INFO) << "Making sure all temp blocks have been allocated";
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001299
Andrew de los Reyesef017552010-10-06 17:57:52 -07001300 graph_utils::DumpGraph(*graph);
1301 CHECK(NoTempBlocksRemain(*graph));
1302 LOG(INFO) << "done making sure all temp blocks are allocated";
1303 return true;
1304}
1305
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001306bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1307 const string& old_root,
1308 const string& old_image,
1309 const string& new_root,
1310 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001311 const string& old_kernel_part,
1312 const string& new_kernel_part,
1313 const string& output_path,
1314 const string& private_key_path) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001315 int old_image_block_count = 0, old_image_block_size = 0;
1316 int new_image_block_count = 0, new_image_block_size = 0;
1317 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1318 &new_image_block_count,
1319 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001320 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001321 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1322 &old_image_block_count,
1323 &old_image_block_size));
1324 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1325 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1326 << "Old and new images have different block counts.";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001327 }
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001328 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001329 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1330
Darin Petkov7ea32332010-10-13 10:46:11 -07001331 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1332 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1333 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001334 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1335 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1336 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1337 }
1338 Graph graph;
1339 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001340
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001341 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
1342 string temp_file_path;
1343 off_t data_file_size = 0;
1344
1345 LOG(INFO) << "Reading files...";
1346
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001347 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1348
Andrew de los Reyesef017552010-10-06 17:57:52 -07001349 vector<Vertex::Index> final_order;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001350 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001351 int fd;
1352 TEST_AND_RETURN_FALSE(
1353 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
1354 TEST_AND_RETURN_FALSE(fd >= 0);
1355 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001356 if (!old_image.empty()) {
1357 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001358
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001359 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1360 &blocks,
1361 old_root,
1362 new_root,
1363 fd,
1364 &data_file_size));
1365 LOG(INFO) << "done reading normal files";
1366 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001367
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001368 graph.resize(graph.size() + 1);
1369 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1370 fd,
1371 &data_file_size,
1372 new_image,
1373 &graph.back()));
1374
1375 // Read kernel partition
1376 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1377 new_kernel_part,
1378 &kernel_ops,
1379 fd,
1380 &data_file_size));
1381
1382 LOG(INFO) << "done reading kernel";
1383 CheckGraph(graph);
1384
1385 LOG(INFO) << "Creating edges...";
1386 CreateEdges(&graph, blocks);
1387 LOG(INFO) << "Done creating edges";
1388 CheckGraph(graph);
1389
1390 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1391 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001392 fd,
1393 &data_file_size,
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001394 &final_order));
1395 } else {
1396 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001397 off_t new_image_size =
1398 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Darin Petkov7a22d792010-11-08 14:10:00 -08001399 TEST_AND_RETURN_FALSE(FullUpdateGenerator::Run(&graph,
1400 new_kernel_part,
1401 new_image,
1402 new_image_size,
1403 fd,
1404 &data_file_size,
1405 kFullUpdateChunkSize,
1406 kBlockSize,
1407 &kernel_ops,
1408 &final_order));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001409 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001410 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001411
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001412 // Convert to protobuf Manifest object
1413 DeltaArchiveManifest manifest;
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001414 OperationNameMap op_name_map;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001415 CheckGraph(graph);
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001416 InstallOperationsToManifest(graph,
1417 final_order,
1418 kernel_ops,
1419 &manifest,
1420 &op_name_map);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001421 CheckGraph(graph);
1422 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001423
1424 // Reorder the data blobs with the newly ordered manifest
1425 string ordered_blobs_path;
1426 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
1427 "/tmp/CrAU_temp_data.ordered.XXXXXX",
1428 &ordered_blobs_path,
1429 false));
1430 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1431 temp_file_path,
1432 ordered_blobs_path));
1433
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001434 // Check that install op blobs are in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001435 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001436 {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001437 for (int i = 0; i < (manifest.install_operations_size() +
1438 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001439 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001440 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001441 manifest.mutable_install_operations(i) :
1442 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001443 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001444 if (op->has_data_offset()) {
1445 if (op->data_offset() != next_blob_offset) {
1446 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001447 << next_blob_offset;
1448 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001449 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001450 }
1451 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001452 }
1453
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001454 // Signatures appear at the end of the blobs. Note the offset in the
1455 // manifest
1456 if (!private_key_path.empty()) {
1457 LOG(INFO) << "Making room for signature in file";
1458 manifest.set_signatures_offset(next_blob_offset);
1459 LOG(INFO) << "set? " << manifest.has_signatures_offset();
1460 // Add a dummy op at the end to appease older clients
1461 DeltaArchiveManifest_InstallOperation* dummy_op =
1462 manifest.add_kernel_install_operations();
1463 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1464 dummy_op->set_data_offset(next_blob_offset);
1465 manifest.set_signatures_offset(next_blob_offset);
1466 uint64_t signature_blob_length = 0;
1467 TEST_AND_RETURN_FALSE(
1468 PayloadSigner::SignatureBlobLength(private_key_path,
1469 &signature_blob_length));
1470 dummy_op->set_data_length(signature_blob_length);
1471 manifest.set_signatures_size(signature_blob_length);
1472 Extent* dummy_extent = dummy_op->add_dst_extents();
1473 // Tell the dummy op to write this data to a big sparse hole
1474 dummy_extent->set_start_block(kSparseHole);
1475 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1476 kBlockSize);
1477 }
1478
Darin Petkov36a58222010-10-07 22:00:09 -07001479 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1480 new_kernel_part,
1481 old_image,
1482 new_image,
1483 &manifest));
1484
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001485 // Serialize protobuf
1486 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001487
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001488 CheckGraph(graph);
1489 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1490 CheckGraph(graph);
1491
1492 LOG(INFO) << "Writing final delta file header...";
1493 DirectFileWriter writer;
1494 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1495 O_WRONLY | O_CREAT | O_TRUNC,
1496 0644) == 0);
1497 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001498
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001499 // Write header
1500 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)) ==
Andrew de los Reyes08c4e272010-04-15 14:02:17 -07001501 static_cast<ssize_t>(strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001502
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001503 // Write version number
1504 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001505
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001506 // Write protobuf length
1507 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1508 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001509
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001510 // Write protobuf
1511 LOG(INFO) << "Writing final delta file protobuf... "
1512 << serialized_manifest.size();
1513 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
1514 serialized_manifest.size()) ==
1515 static_cast<ssize_t>(serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001516
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001517 // Append the data blobs
1518 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001519 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001520 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1521 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1522 for (;;) {
1523 char buf[kBlockSize];
1524 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1525 if (0 == rc) {
1526 // EOF
1527 break;
1528 }
1529 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
1530 TEST_AND_RETURN_FALSE(writer.Write(buf, rc) == rc);
1531 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001532
1533 // Write signature blob.
1534 if (!private_key_path.empty()) {
1535 LOG(INFO) << "Signing the update...";
1536 vector<char> signature_blob;
1537 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(output_path,
1538 private_key_path,
1539 &signature_blob));
1540 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
1541 signature_blob.size()) ==
1542 static_cast<ssize_t>(signature_blob.size()));
1543 }
1544
Darin Petkov95cf01f2010-10-12 14:59:13 -07001545 int64_t manifest_metadata_size =
1546 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001547 ReportPayloadUsage(manifest, manifest_metadata_size, op_name_map);
Darin Petkov880335c2010-10-01 15:52:53 -07001548
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001549 LOG(INFO) << "All done. Successfully created delta file.";
1550 return true;
1551}
1552
Andrew de los Reyes50f36492010-11-01 13:57:12 -07001553const char* const kBsdiffPath = "bsdiff";
1554const char* const kBspatchPath = "bspatch";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001555const char* const kDeltaMagic = "CrAU";
1556
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001557}; // namespace chromeos_update_engine