blob: 30110261c69f64df2705ae7c6c98aada6e4b6de6 [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"
30#include "update_engine/graph_types.h"
31#include "update_engine/graph_utils.h"
Darin Petkov36a58222010-10-07 22:00:09 -070032#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070033#include "update_engine/payload_signer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070034#include "update_engine/subprocess.h"
35#include "update_engine/topological_sort.h"
36#include "update_engine/update_metadata.pb.h"
37#include "update_engine/utils.h"
38
39using std::make_pair;
Andrew de los Reyesef017552010-10-06 17:57:52 -070040using std::map;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070041using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070042using std::min;
43using std::set;
44using std::string;
45using std::vector;
46
47namespace chromeos_update_engine {
48
49typedef DeltaDiffGenerator::Block Block;
50
51namespace {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070052const size_t kBlockSize = 4096; // bytes
Darin Petkovc0b7a532010-09-29 15:18:14 -070053const size_t kRootFSPartitionSize = 1 * 1024 * 1024 * 1024; // 1 GiB
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070054const uint64_t kVersionNumber = 1;
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070055const uint64_t kFullUpdateChunkSize = 128 * 1024; // bytes
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070056
Darin Petkov68c10d12010-10-14 09:24:37 -070057static const char* kInstallOperationTypes[] = {
58 "REPLACE",
59 "REPLACE_BZ",
60 "MOVE",
61 "BSDIFF"
62};
63
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070064// Stores all Extents for a file into 'out'. Returns true on success.
65bool GatherExtents(const string& path,
66 google::protobuf::RepeatedPtrField<Extent>* out) {
67 vector<Extent> extents;
68 TEST_AND_RETURN_FALSE(extent_mapper::ExtentsForFileFibmap(path, &extents));
69 DeltaDiffGenerator::StoreExtents(extents, out);
70 return true;
71}
72
73// Runs the bsdiff tool on two files and returns the resulting delta in
74// 'out'. Returns true on success.
75bool BsdiffFiles(const string& old_file,
76 const string& new_file,
77 vector<char>* out) {
78 const string kPatchFile = "/tmp/delta.patchXXXXXX";
79 string patch_file_path;
80
81 TEST_AND_RETURN_FALSE(
82 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
83
84 vector<string> cmd;
85 cmd.push_back(kBsdiffPath);
86 cmd.push_back(old_file);
87 cmd.push_back(new_file);
88 cmd.push_back(patch_file_path);
89
90 int rc = 1;
91 vector<char> patch_file;
92 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc));
93 TEST_AND_RETURN_FALSE(rc == 0);
94 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
95 unlink(patch_file_path.c_str());
96 return true;
97}
98
99// The blocks vector contains a reader and writer for each block on the
100// filesystem that's being in-place updated. We populate the reader/writer
101// fields of blocks by calling this function.
102// For each block in 'operation' that is read or written, find that block
103// in 'blocks' and set the reader/writer field to the vertex passed.
104// 'graph' is not strictly necessary, but useful for printing out
105// error messages.
106bool AddInstallOpToBlocksVector(
107 const DeltaArchiveManifest_InstallOperation& operation,
108 vector<Block>* blocks,
109 const Graph& graph,
110 Vertex::Index vertex) {
111 LOG(INFO) << "AddInstallOpToBlocksVector(" << vertex << "), "
112 << graph[vertex].file_name;
113 // See if this is already present.
114 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700115
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700116 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
117 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
118 const int extents_size =
119 (field == READER) ? operation.src_extents_size() :
120 operation.dst_extents_size();
121 const char* past_participle = (field == READER) ? "read" : "written";
122 const google::protobuf::RepeatedPtrField<Extent>& extents =
123 (field == READER) ? operation.src_extents() : operation.dst_extents();
124 Vertex::Index Block::*access_type =
125 (field == READER) ? &Block::reader : &Block::writer;
126
127 for (int i = 0; i < extents_size; i++) {
128 const Extent& extent = extents.Get(i);
129 if (extent.start_block() == kSparseHole) {
130 // Hole in sparse file. skip
131 continue;
132 }
133 for (uint64_t block = extent.start_block();
134 block < (extent.start_block() + extent.num_blocks()); block++) {
135 LOG(INFO) << "ext: " << i << " block: " << block;
136 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
137 LOG(FATAL) << "Block " << block << " is already "
138 << past_participle << " by "
139 << (*blocks)[block].*access_type << "("
140 << graph[(*blocks)[block].*access_type].file_name
141 << ") and also " << vertex << "("
142 << graph[vertex].file_name << ")";
143 }
144 (*blocks)[block].*access_type = vertex;
145 }
146 }
147 }
148 return true;
149}
150
Andrew de los Reyesef017552010-10-06 17:57:52 -0700151// For a given regular file which must exist at new_root + path, and
152// may exist at old_root + path, creates a new InstallOperation and
153// adds it to the graph. Also, populates the |blocks| array as
154// necessary, if |blocks| is non-NULL. Also, writes the data
155// necessary to send the file down to the client into data_fd, which
156// has length *data_file_size. *data_file_size is updated
157// appropriately. If |existing_vertex| is no kInvalidIndex, use that
158// rather than allocating a new vertex. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700159bool DeltaReadFile(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700160 Vertex::Index existing_vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700161 vector<Block>* blocks,
162 const string& old_root,
163 const string& new_root,
164 const string& path, // within new_root
165 int data_fd,
166 off_t* data_file_size) {
167 vector<char> data;
168 DeltaArchiveManifest_InstallOperation operation;
169
170 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_root + path,
171 new_root + path,
172 &data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700173 &operation,
174 true));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700175
176 // Write the data
177 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
178 operation.set_data_offset(*data_file_size);
179 operation.set_data_length(data.size());
180 }
181
182 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
183 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700184
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700185 // Now, insert into graph and blocks vector
Andrew de los Reyesef017552010-10-06 17:57:52 -0700186 Vertex::Index vertex = existing_vertex;
187 if (vertex == Vertex::kInvalidIndex) {
188 graph->resize(graph->size() + 1);
189 vertex = graph->size() - 1;
190 }
191 (*graph)[vertex].op = operation;
192 CHECK((*graph)[vertex].op.has_type());
193 (*graph)[vertex].file_name = path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700194
Andrew de los Reyesef017552010-10-06 17:57:52 -0700195 if (blocks)
196 TEST_AND_RETURN_FALSE(AddInstallOpToBlocksVector((*graph)[vertex].op,
197 blocks,
198 *graph,
199 vertex));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700200 return true;
201}
202
203// For each regular file within new_root, creates a node in the graph,
204// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
205// and writes any necessary data to the end of data_fd.
206bool DeltaReadFiles(Graph* graph,
207 vector<Block>* blocks,
208 const string& old_root,
209 const string& new_root,
210 int data_fd,
211 off_t* data_file_size) {
212 set<ino_t> visited_inodes;
213 for (FilesystemIterator fs_iter(new_root,
214 utils::SetWithValue<string>("/lost+found"));
215 !fs_iter.IsEnd(); fs_iter.Increment()) {
216 if (!S_ISREG(fs_iter.GetStat().st_mode))
217 continue;
218
219 // Make sure we visit each inode only once.
220 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
221 continue;
222 visited_inodes.insert(fs_iter.GetStat().st_ino);
223 if (fs_iter.GetStat().st_size == 0)
224 continue;
225
226 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700227
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700228 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700229 Vertex::kInvalidIndex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700230 blocks,
231 old_root,
232 new_root,
233 fs_iter.GetPartialPath(),
234 data_fd,
235 data_file_size));
236 }
237 return true;
238}
239
Andrew de los Reyesef017552010-10-06 17:57:52 -0700240// This class allocates non-existent temp blocks, starting from
241// kTempBlockStart. Other code is responsible for converting these
242// temp blocks into real blocks, as the client can't read or write to
243// these blocks.
244class DummyExtentAllocator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700245 public:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700246 explicit DummyExtentAllocator()
247 : next_block_(kTempBlockStart) {}
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700248 vector<Extent> Allocate(const uint64_t block_count) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700249 vector<Extent> ret(1);
250 ret[0].set_start_block(next_block_);
251 ret[0].set_num_blocks(block_count);
252 next_block_ += block_count;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700253 return ret;
254 }
255 private:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700256 uint64_t next_block_;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700257};
258
259// Reads blocks from image_path that are not yet marked as being written
260// in the blocks array. These blocks that remain are non-file-data blocks.
261// In the future we might consider intelligent diffing between this data
262// and data in the previous image, but for now we just bzip2 compress it
263// and include it in the update.
264// Creates a new node in the graph to write these blocks and writes the
265// appropriate blob to blobs_fd. Reads and updates blobs_length;
266bool ReadUnwrittenBlocks(const vector<Block>& blocks,
267 int blobs_fd,
268 off_t* blobs_length,
269 const string& image_path,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700270 Vertex* vertex) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700271 vertex->file_name = "<rootfs-non-file-data>";
272
Andrew de los Reyesef017552010-10-06 17:57:52 -0700273 DeltaArchiveManifest_InstallOperation* out_op = &vertex->op;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700274 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
275 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
276 ScopedFdCloser image_fd_closer(&image_fd);
277
278 string temp_file_path;
279 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX",
280 &temp_file_path,
281 NULL));
282
283 FILE* file = fopen(temp_file_path.c_str(), "w");
284 TEST_AND_RETURN_FALSE(file);
285 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700286
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700287 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
288 file,
289 9, // max compression
290 0, // verbosity
291 0); // default work factor
292 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700293
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700294 vector<Extent> extents;
295 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700296
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700297 LOG(INFO) << "Appending left over blocks to extents";
298 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
299 if (blocks[i].writer != Vertex::kInvalidIndex)
300 continue;
Andrew de los Reyesef017552010-10-06 17:57:52 -0700301 if (blocks[i].reader != Vertex::kInvalidIndex) {
302 graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i);
303 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700304 graph_utils::AppendBlockToExtents(&extents, i);
305 block_count++;
306 }
307
308 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
309 // so we arbitrarily set it to 1024 * kBlockSize.
310 vector<char> buf(1024 * kBlockSize);
311
312 LOG(INFO) << "Reading left over blocks";
313 vector<Block>::size_type blocks_copied_count = 0;
314
315 // For each extent in extents, write the data into BZ2_bzWrite which
316 // sends it to an output file.
317 // We use the temporary buffer 'buf' to hold the data, which may be
318 // smaller than the extent, so in that case we have to loop to get
319 // the extent's data (that's the inner while loop).
320 for (vector<Extent>::const_iterator it = extents.begin();
321 it != extents.end(); ++it) {
322 vector<Block>::size_type blocks_read = 0;
323 while (blocks_read < it->num_blocks()) {
324 const int copy_block_cnt =
325 min(buf.size() / kBlockSize,
326 static_cast<vector<char>::size_type>(
327 it->num_blocks() - blocks_read));
328 ssize_t rc = pread(image_fd,
329 &buf[0],
330 copy_block_cnt * kBlockSize,
331 (it->start_block() + blocks_read) * kBlockSize);
332 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
333 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
334 copy_block_cnt * kBlockSize);
335 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
336 TEST_AND_RETURN_FALSE(err == BZ_OK);
337 blocks_read += copy_block_cnt;
338 blocks_copied_count += copy_block_cnt;
339 LOG(INFO) << "progress: " << ((float)blocks_copied_count)/block_count;
340 }
341 }
342 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
343 TEST_AND_RETURN_FALSE(err == BZ_OK);
344 bz_file = NULL;
345 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
346 file = NULL;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700347
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700348 vector<char> compressed_data;
349 LOG(INFO) << "Reading compressed data off disk";
350 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
351 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700352
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700353 // Add node to graph to write these blocks
354 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
355 out_op->set_data_offset(*blobs_length);
356 out_op->set_data_length(compressed_data.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700357 LOG(INFO) << "Rootfs non-data blocks compressed take up "
358 << compressed_data.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700359 *blobs_length += compressed_data.size();
360 out_op->set_dst_length(kBlockSize * block_count);
361 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700362
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700363 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
364 &compressed_data[0],
365 compressed_data.size()));
366 LOG(INFO) << "done with extra blocks";
367 return true;
368}
369
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700370// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700371// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700372bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
373 uint64_t value_be = htobe64(value);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700374 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)) ==
375 sizeof(value_be));
376 return true;
377}
378
379// Adds each operation from the graph to the manifest in the order
380// specified by 'order'.
381void InstallOperationsToManifest(
382 const Graph& graph,
383 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700384 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700385 DeltaArchiveManifest* out_manifest) {
386 for (vector<Vertex::Index>::const_iterator it = order.begin();
387 it != order.end(); ++it) {
388 DeltaArchiveManifest_InstallOperation* op =
389 out_manifest->add_install_operations();
390 *op = graph[*it].op;
391 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700392 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
393 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
394 DeltaArchiveManifest_InstallOperation* op =
395 out_manifest->add_kernel_install_operations();
396 *op = *it;
397 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700398}
399
400void CheckGraph(const Graph& graph) {
401 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
402 CHECK(it->op.has_type());
403 }
404}
405
Darin Petkov68c10d12010-10-14 09:24:37 -0700406// Delta compresses a kernel partition |new_kernel_part| with knowledge of the
407// old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty
408// string, generates a full update of the partition.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700409bool DeltaCompressKernelPartition(
410 const string& old_kernel_part,
411 const string& new_kernel_part,
412 vector<DeltaArchiveManifest_InstallOperation>* ops,
413 int blobs_fd,
414 off_t* blobs_length) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700415 LOG(INFO) << "Delta compressing kernel partition...";
Darin Petkov68c10d12010-10-14 09:24:37 -0700416 LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update...";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700417
418 // Add a new install operation
419 ops->resize(1);
420 DeltaArchiveManifest_InstallOperation* op = &(*ops)[0];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700421
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700422 vector<char> data;
Darin Petkov68c10d12010-10-14 09:24:37 -0700423 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_kernel_part,
424 new_kernel_part,
425 &data,
426 op,
427 false));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700428
Darin Petkov68c10d12010-10-14 09:24:37 -0700429 // Write the data
430 if (op->type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
431 op->set_data_offset(*blobs_length);
432 op->set_data_length(data.size());
433 }
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700434
Darin Petkov68c10d12010-10-14 09:24:37 -0700435 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
436 *blobs_length += data.size();
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700437
Darin Petkov68c10d12010-10-14 09:24:37 -0700438 LOG(INFO) << "Done delta compressing kernel partition: "
439 << kInstallOperationTypes[op->type()];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700440 return true;
441}
442
Darin Petkov880335c2010-10-01 15:52:53 -0700443struct DeltaObject {
444 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
445 : name(in_name),
446 type(in_type),
447 size(in_size) {}
448 bool operator <(const DeltaObject& object) const {
Darin Petkovd43d6902010-10-14 11:17:50 -0700449 return (size != object.size) ? (size < object.size) : (name < object.name);
Darin Petkov880335c2010-10-01 15:52:53 -0700450 }
451 string name;
452 int type;
453 off_t size;
454};
455
Darin Petkov880335c2010-10-01 15:52:53 -0700456void ReportPayloadUsage(const Graph& graph,
Darin Petkov95cf01f2010-10-12 14:59:13 -0700457 const DeltaArchiveManifest& manifest,
458 const int64_t manifest_metadata_size) {
Darin Petkov880335c2010-10-01 15:52:53 -0700459 vector<DeltaObject> objects;
460 off_t total_size = 0;
461
462 // Graph nodes with information about file names.
463 for (Vertex::Index node = 0; node < graph.size(); node++) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700464 const Vertex& vertex = graph[node];
465 if (!vertex.valid) {
466 continue;
467 }
468 objects.push_back(DeltaObject(vertex.file_name,
469 vertex.op.type(),
470 vertex.op.data_length()));
471 total_size += vertex.op.data_length();
Darin Petkov880335c2010-10-01 15:52:53 -0700472 }
473
Darin Petkov880335c2010-10-01 15:52:53 -0700474 // Kernel install operations.
475 for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) {
476 const DeltaArchiveManifest_InstallOperation& op =
477 manifest.kernel_install_operations(i);
478 objects.push_back(DeltaObject(StringPrintf("<kernel-operation-%d>", i),
479 op.type(),
480 op.data_length()));
481 total_size += op.data_length();
482 }
483
Darin Petkov95cf01f2010-10-12 14:59:13 -0700484 objects.push_back(DeltaObject("<manifest-metadata>",
485 -1,
486 manifest_metadata_size));
487 total_size += manifest_metadata_size;
488
Darin Petkov880335c2010-10-01 15:52:53 -0700489 std::sort(objects.begin(), objects.end());
490
491 static const char kFormatString[] = "%6.2f%% %10llu %-10s %s\n";
492 for (vector<DeltaObject>::const_iterator it = objects.begin();
493 it != objects.end(); ++it) {
494 const DeltaObject& object = *it;
495 fprintf(stderr, kFormatString,
496 object.size * 100.0 / total_size,
497 object.size,
Darin Petkov95cf01f2010-10-12 14:59:13 -0700498 object.type >= 0 ? kInstallOperationTypes[object.type] : "-",
Darin Petkov880335c2010-10-01 15:52:53 -0700499 object.name.c_str());
500 }
501 fprintf(stderr, kFormatString, 100.0, total_size, "", "<total>");
502}
503
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700504} // namespace {}
505
506bool DeltaDiffGenerator::ReadFileToDiff(
507 const string& old_filename,
508 const string& new_filename,
509 vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700510 DeltaArchiveManifest_InstallOperation* out_op,
511 bool gather_extents) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700512 // Read new data in
513 vector<char> new_data;
514 TEST_AND_RETURN_FALSE(utils::ReadFile(new_filename, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700515
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700516 TEST_AND_RETURN_FALSE(!new_data.empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700517
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700518 vector<char> new_data_bz;
519 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
520 CHECK(!new_data_bz.empty());
521
522 vector<char> data; // Data blob that will be written to delta file.
523
524 DeltaArchiveManifest_InstallOperation operation;
525 size_t current_best_size = 0;
526 if (new_data.size() <= new_data_bz.size()) {
527 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
528 current_best_size = new_data.size();
529 data = new_data;
530 } else {
531 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
532 current_best_size = new_data_bz.size();
533 data = new_data_bz;
534 }
535
536 // Do we have an original file to consider?
537 struct stat old_stbuf;
Darin Petkov68c10d12010-10-14 09:24:37 -0700538 bool no_original = old_filename.empty();
539 if (!no_original && 0 != stat(old_filename.c_str(), &old_stbuf)) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700540 // If stat-ing the old file fails, it should be because it doesn't exist.
541 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
Darin Petkov68c10d12010-10-14 09:24:37 -0700542 no_original = true;
543 }
544 if (!no_original) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700545 // Read old data
546 vector<char> old_data;
547 TEST_AND_RETURN_FALSE(utils::ReadFile(old_filename, &old_data));
548 if (old_data == new_data) {
549 // No change in data.
550 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
551 current_best_size = 0;
552 data.clear();
553 } else {
554 // Try bsdiff of old to new data
555 vector<char> bsdiff_delta;
556 TEST_AND_RETURN_FALSE(
557 BsdiffFiles(old_filename, new_filename, &bsdiff_delta));
558 CHECK_GT(bsdiff_delta.size(), 0);
559 if (bsdiff_delta.size() < current_best_size) {
560 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
561 current_best_size = bsdiff_delta.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700562
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700563 data = bsdiff_delta;
564 }
565 }
566 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700567
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700568 // Set parameters of the operations
569 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700570
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700571 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
572 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Darin Petkov68c10d12010-10-14 09:24:37 -0700573 if (gather_extents) {
574 TEST_AND_RETURN_FALSE(
575 GatherExtents(old_filename, operation.mutable_src_extents()));
576 } else {
577 Extent* src_extent = operation.add_src_extents();
578 src_extent->set_start_block(0);
579 src_extent->set_num_blocks(
580 (old_stbuf.st_size + kBlockSize - 1) / kBlockSize);
581 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700582 operation.set_src_length(old_stbuf.st_size);
583 }
584
Darin Petkov68c10d12010-10-14 09:24:37 -0700585 if (gather_extents) {
586 TEST_AND_RETURN_FALSE(
587 GatherExtents(new_filename, operation.mutable_dst_extents()));
588 } else {
589 Extent* dst_extent = operation.add_dst_extents();
590 dst_extent->set_start_block(0);
591 dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize);
592 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700593 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700594
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700595 out_data->swap(data);
596 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700597
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700598 return true;
599}
600
Darin Petkov7ea32332010-10-13 10:46:11 -0700601bool InitializePartitionInfo(bool is_kernel,
602 const string& partition,
603 PartitionInfo* info) {
604 int64_t size = 0;
605 if (is_kernel) {
606 size = utils::FileSize(partition);
607 } else {
608 int block_count = 0, block_size = 0;
609 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition,
610 &block_count,
611 &block_size));
612 size = static_cast<int64_t>(block_count) * block_size;
613 }
614 TEST_AND_RETURN_FALSE(size > 0);
Darin Petkov36a58222010-10-07 22:00:09 -0700615 info->set_size(size);
616 OmahaHashCalculator hasher;
Darin Petkov7ea32332010-10-13 10:46:11 -0700617 TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size);
Darin Petkov36a58222010-10-07 22:00:09 -0700618 TEST_AND_RETURN_FALSE(hasher.Finalize());
619 const vector<char>& hash = hasher.raw_hash();
620 info->set_hash(hash.data(), hash.size());
Darin Petkovd43d6902010-10-14 11:17:50 -0700621 LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash();
Darin Petkov36a58222010-10-07 22:00:09 -0700622 return true;
623}
624
625bool InitializePartitionInfos(const string& old_kernel,
626 const string& new_kernel,
627 const string& old_rootfs,
628 const string& new_rootfs,
629 DeltaArchiveManifest* manifest) {
Darin Petkovd43d6902010-10-14 11:17:50 -0700630 if (!old_kernel.empty()) {
631 TEST_AND_RETURN_FALSE(
632 InitializePartitionInfo(true,
633 old_kernel,
634 manifest->mutable_old_kernel_info()));
635 }
Darin Petkov36a58222010-10-07 22:00:09 -0700636 TEST_AND_RETURN_FALSE(
Darin Petkov7ea32332010-10-13 10:46:11 -0700637 InitializePartitionInfo(true,
638 new_kernel,
639 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700640 if (!old_rootfs.empty()) {
641 TEST_AND_RETURN_FALSE(
Darin Petkov7ea32332010-10-13 10:46:11 -0700642 InitializePartitionInfo(false,
643 old_rootfs,
Darin Petkov36a58222010-10-07 22:00:09 -0700644 manifest->mutable_old_rootfs_info()));
645 }
646 TEST_AND_RETURN_FALSE(
Darin Petkov7ea32332010-10-13 10:46:11 -0700647 InitializePartitionInfo(false,
648 new_rootfs,
649 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700650 return true;
651}
652
Andrew de los Reyesef017552010-10-06 17:57:52 -0700653namespace {
654
655// Takes a collection (vector or RepeatedPtrField) of Extent and
656// returns a vector of the blocks referenced, in order.
657template<typename T>
658vector<uint64_t> ExpandExtents(const T& extents) {
659 vector<uint64_t> ret;
660 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
661 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700662 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700663 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700664 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700665 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700666 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700667 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700668 }
669 }
670 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700671 return ret;
672}
673
674// Takes a vector of blocks and returns an equivalent vector of Extent
675// objects.
676vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
677 vector<Extent> new_extents;
678 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
679 it != e; ++it) {
680 graph_utils::AppendBlockToExtents(&new_extents, *it);
681 }
682 return new_extents;
683}
684
685} // namespace {}
686
687void DeltaDiffGenerator::SubstituteBlocks(
688 Vertex* vertex,
689 const vector<Extent>& remove_extents,
690 const vector<Extent>& replace_extents) {
691 // First, expand out the blocks that op reads from
692 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700693 {
694 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700695 vector<uint64_t> remove_extents_expanded =
696 ExpandExtents(remove_extents);
697 vector<uint64_t> replace_extents_expanded =
698 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700699 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700700 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700701 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700702 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700703 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
704 }
705 utils::ApplyMap(&read_blocks, conversion);
706 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
707 e = vertex->out_edges.end(); it != e; ++it) {
708 vector<uint64_t> write_before_deps_expanded =
709 ExpandExtents(it->second.write_extents);
710 utils::ApplyMap(&write_before_deps_expanded, conversion);
711 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700712 }
713 }
714 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700715 vertex->op.clear_src_extents();
716 vector<Extent> new_extents = CompressExtents(read_blocks);
717 DeltaDiffGenerator::StoreExtents(new_extents,
718 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700719}
720
721bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700722 const set<Edge>& edges,
723 vector<CutEdgeVertexes>* out_cuts) {
724 DummyExtentAllocator scratch_allocator;
725 vector<CutEdgeVertexes> cuts;
726 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700727
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700728 uint64_t scratch_blocks_used = 0;
729 for (set<Edge>::const_iterator it = edges.begin();
730 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700731 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700732 vector<Extent> old_extents =
733 (*graph)[it->first].out_edges[it->second].extents;
734 // Choose some scratch space
735 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
736 LOG(INFO) << "using " << graph_utils::EdgeWeight(*graph, *it)
737 << " scratch blocks ("
738 << scratch_blocks_used << ")";
Andrew de los Reyesef017552010-10-06 17:57:52 -0700739 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700740 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
741 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700742 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700743 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700744 cuts.back().old_src = it->first;
745 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700746
Andrew de los Reyesef017552010-10-06 17:57:52 -0700747 EdgeProperties& cut_edge_properties =
748 (*graph)[it->first].out_edges.find(it->second)->second;
749
750 // This should never happen, as we should only be cutting edges between
751 // real file nodes, and write-before relationships are created from
752 // a real file node to a temp copy node:
753 CHECK(cut_edge_properties.write_extents.empty())
754 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700755
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700756 // make node depend on the copy operation
757 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700758 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700759
760 // Set src/dst extents and other proto variables for copy operation
761 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
762 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700763 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700764 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700765 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700766 graph->back().op.mutable_dst_extents());
767 graph->back().op.set_src_length(
768 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
769 graph->back().op.set_dst_length(graph->back().op.src_length());
770
771 // make the dest node read from the scratch space
772 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700773 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700774 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700775 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700776
777 // delete the old edge
778 CHECK_EQ(1, (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700779
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700780 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700781 EdgeProperties write_before_edge_properties;
782 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
783 (*graph)[it->second].out_edges.insert(
784 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700785 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700786 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700787 return true;
788}
789
790// Stores all Extents in 'extents' into 'out'.
791void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700792 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700793 google::protobuf::RepeatedPtrField<Extent>* out) {
794 for (vector<Extent>::const_iterator it = extents.begin();
795 it != extents.end(); ++it) {
796 Extent* new_extent = out->Add();
797 *new_extent = *it;
798 }
799}
800
801// Creates all the edges for the graph. Writers of a block point to
802// readers of the same block. This is because for an edge A->B, B
803// must complete before A executes.
804void DeltaDiffGenerator::CreateEdges(Graph* graph,
805 const vector<Block>& blocks) {
806 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
807 // Blocks with both a reader and writer get an edge
808 if (blocks[i].reader == Vertex::kInvalidIndex ||
809 blocks[i].writer == Vertex::kInvalidIndex)
810 continue;
811 // Don't have a node depend on itself
812 if (blocks[i].reader == blocks[i].writer)
813 continue;
814 // See if there's already an edge we can add onto
815 Vertex::EdgeMap::iterator edge_it =
816 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
817 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
818 // No existing edge. Create one
819 (*graph)[blocks[i].writer].out_edges.insert(
820 make_pair(blocks[i].reader, EdgeProperties()));
821 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700822 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700823 }
824 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
825 }
826}
827
Andrew de los Reyesef017552010-10-06 17:57:52 -0700828namespace {
829
830class SortCutsByTopoOrderLess {
831 public:
832 SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table)
833 : table_(table) {}
834 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
835 return table_[a.old_dst] < table_[b.old_dst];
836 }
837 private:
838 vector<vector<Vertex::Index>::size_type>& table_;
839};
840
841} // namespace {}
842
843void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
844 vector<Vertex::Index>& op_indexes,
845 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
846 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
847 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
848 i != e; ++i) {
849 Vertex::Index node = op_indexes[i];
850 if (table.size() < (node + 1)) {
851 table.resize(node + 1);
852 }
853 table[node] = i;
854 }
855 reverse_op_indexes->swap(table);
856}
857
858void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes,
859 vector<CutEdgeVertexes>* cuts) {
860 // first, make a reverse lookup table.
861 vector<vector<Vertex::Index>::size_type> table;
862 GenerateReverseTopoOrderMap(op_indexes, &table);
863 SortCutsByTopoOrderLess less(table);
864 sort(cuts->begin(), cuts->end(), less);
865}
866
867void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
868 vector<Vertex::Index>* op_indexes) {
869 vector<Vertex::Index> ret;
870 vector<Vertex::Index> full_ops;
871 ret.reserve(op_indexes->size());
872 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
873 ++i) {
874 DeltaArchiveManifest_InstallOperation_Type type =
875 (*graph)[(*op_indexes)[i]].op.type();
876 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
877 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
878 full_ops.push_back((*op_indexes)[i]);
879 } else {
880 ret.push_back((*op_indexes)[i]);
881 }
882 }
883 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
884 << (full_ops.size() + ret.size()) << " total ops.";
885 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
886 op_indexes->swap(ret);
887}
888
889namespace {
890
891template<typename T>
892bool TempBlocksExistInExtents(const T& extents) {
893 for (int i = 0, e = extents.size(); i < e; ++i) {
894 Extent extent = graph_utils::GetElement(extents, i);
895 uint64_t start = extent.start_block();
896 uint64_t num = extent.num_blocks();
897 if (start == kSparseHole)
898 continue;
899 if (start >= kTempBlockStart ||
900 (start + num) >= kTempBlockStart) {
901 LOG(ERROR) << "temp block!";
902 LOG(ERROR) << "start: " << start << ", num: " << num;
903 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
904 LOG(ERROR) << "returning true";
905 return true;
906 }
907 // check for wrap-around, which would be a bug:
908 CHECK(start <= (start + num));
909 }
910 return false;
911}
912
913} // namespace {}
914
915bool DeltaDiffGenerator::AssignTempBlocks(
916 Graph* graph,
917 const string& new_root,
918 int data_fd,
919 off_t* data_file_size,
920 vector<Vertex::Index>* op_indexes,
921 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
922 vector<CutEdgeVertexes>& cuts) {
923 CHECK(!cuts.empty());
924 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
925 true ; --i) {
926 LOG(INFO) << "Fixing temp blocks in cut " << i
927 << ": old dst: " << cuts[i].old_dst << " new vertex: "
928 << cuts[i].new_vertex;
929 const uint64_t blocks_needed =
930 graph_utils::BlocksInExtents(cuts[i].tmp_extents);
931 LOG(INFO) << "Scanning for usable blocks (" << blocks_needed << " needed)";
932 // For now, just look for a single op w/ sufficient blocks, not
933 // considering blocks from outgoing read-before deps.
934 Vertex::Index node = cuts[i].old_dst;
935 DeltaArchiveManifest_InstallOperation_Type node_type =
936 (*graph)[node].op.type();
937 if (node_type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
938 node_type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
939 LOG(INFO) << "This was already converted to full, so skipping.";
940 // Delete the temp node and pointer to it from old src
941 if (!(*graph)[cuts[i].old_src].out_edges.erase(cuts[i].new_vertex)) {
942 LOG(INFO) << "Odd. node " << cuts[i].old_src << " didn't point to "
943 << cuts[i].new_vertex;
944 }
945 (*graph)[cuts[i].new_vertex].valid = false;
946 vector<Vertex::Index>::size_type new_topo_idx =
947 (*reverse_op_indexes)[cuts[i].new_vertex];
948 op_indexes->erase(op_indexes->begin() + new_topo_idx);
949 GenerateReverseTopoOrderMap(*op_indexes, reverse_op_indexes);
950 continue;
951 }
952 bool found_node = false;
953 for (vector<Vertex::Index>::size_type j = (*reverse_op_indexes)[node] + 1,
954 je = op_indexes->size(); j < je; ++j) {
955 Vertex::Index test_node = (*op_indexes)[j];
956 // See if this node has sufficient blocks
957 ExtentRanges ranges;
958 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
959 ranges.SubtractExtent(ExtentForRange(
960 kTempBlockStart, kSparseHole - kTempBlockStart));
961 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
962 // For now, for simplicity, subtract out all blocks in read-before
963 // dependencies.
964 for (Vertex::EdgeMap::const_iterator edge_i =
965 (*graph)[test_node].out_edges.begin(),
966 edge_e = (*graph)[test_node].out_edges.end();
967 edge_i != edge_e; ++edge_i) {
968 ranges.SubtractExtents(edge_i->second.extents);
969 }
Darin Petkov36a58222010-10-07 22:00:09 -0700970
Andrew de los Reyesef017552010-10-06 17:57:52 -0700971 uint64_t blocks_found = ranges.blocks();
972 if (blocks_found < blocks_needed) {
973 if (blocks_found > 0)
974 LOG(INFO) << "insufficient blocks found in topo node " << j
975 << " (node " << (*op_indexes)[j] << "). Found only "
976 << blocks_found;
977 continue;
978 }
979 found_node = true;
980 LOG(INFO) << "Found sufficient blocks in topo node " << j
981 << " (node " << (*op_indexes)[j] << ")";
982 // Sub in the blocks, and make the node supplying the blocks
983 // depend on old_dst.
984 vector<Extent> real_extents =
985 ranges.GetExtentsForBlockCount(blocks_needed);
Darin Petkov36a58222010-10-07 22:00:09 -0700986
Andrew de los Reyesef017552010-10-06 17:57:52 -0700987 // Fix the old dest node w/ the real blocks
988 SubstituteBlocks(&(*graph)[node],
989 cuts[i].tmp_extents,
990 real_extents);
Darin Petkov36a58222010-10-07 22:00:09 -0700991
Andrew de los Reyesef017552010-10-06 17:57:52 -0700992 // Fix the new node w/ the real blocks. Since the new node is just a
993 // copy operation, we can replace all the dest extents w/ the real
994 // blocks.
995 DeltaArchiveManifest_InstallOperation *op =
996 &(*graph)[cuts[i].new_vertex].op;
997 op->clear_dst_extents();
998 StoreExtents(real_extents, op->mutable_dst_extents());
Darin Petkov36a58222010-10-07 22:00:09 -0700999
Andrew de los Reyesef017552010-10-06 17:57:52 -07001000 // Add an edge from the real-block supplier to the old dest block.
1001 graph_utils::AddReadBeforeDepExtents(&(*graph)[test_node],
1002 node,
1003 real_extents);
1004 break;
1005 }
1006 if (!found_node) {
1007 // convert to full op
1008 LOG(WARNING) << "Failed to find enough temp blocks for cut " << i
1009 << " with old dest (graph node " << node
1010 << "). Converting to a full op, at the expense of a "
1011 << "good compression ratio.";
1012 TEST_AND_RETURN_FALSE(ConvertCutToFullOp(graph,
1013 cuts[i],
1014 new_root,
1015 data_fd,
1016 data_file_size));
1017 // move the full op to the back
1018 vector<Vertex::Index> new_op_indexes;
1019 for (vector<Vertex::Index>::const_iterator iter_i = op_indexes->begin(),
1020 iter_e = op_indexes->end(); iter_i != iter_e; ++iter_i) {
1021 if ((*iter_i == cuts[i].old_dst) || (*iter_i == cuts[i].new_vertex))
1022 continue;
1023 new_op_indexes.push_back(*iter_i);
1024 }
1025 new_op_indexes.push_back(cuts[i].old_dst);
1026 op_indexes->swap(new_op_indexes);
Darin Petkov36a58222010-10-07 22:00:09 -07001027
Andrew de los Reyesef017552010-10-06 17:57:52 -07001028 GenerateReverseTopoOrderMap(*op_indexes, reverse_op_indexes);
1029 }
1030 if (i == e) {
1031 // break out of for() loop
1032 break;
1033 }
1034 }
1035 return true;
1036}
1037
1038bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1039 size_t idx = 0;
1040 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1041 ++it, ++idx) {
1042 if (!it->valid)
1043 continue;
1044 const DeltaArchiveManifest_InstallOperation& op = it->op;
1045 if (TempBlocksExistInExtents(op.dst_extents()) ||
1046 TempBlocksExistInExtents(op.src_extents())) {
1047 LOG(INFO) << "bad extents in node " << idx;
1048 LOG(INFO) << "so yeah";
1049 return false;
1050 }
1051
1052 // Check out-edges:
1053 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1054 je = it->out_edges.end(); jt != je; ++jt) {
1055 if (TempBlocksExistInExtents(jt->second.extents) ||
1056 TempBlocksExistInExtents(jt->second.write_extents)) {
1057 LOG(INFO) << "bad out edge in node " << idx;
1058 LOG(INFO) << "so yeah";
1059 return false;
1060 }
1061 }
1062 }
1063 return true;
1064}
1065
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001066bool DeltaDiffGenerator::ReorderDataBlobs(
1067 DeltaArchiveManifest* manifest,
1068 const std::string& data_blobs_path,
1069 const std::string& new_data_blobs_path) {
1070 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1071 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1072 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001073
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001074 DirectFileWriter writer;
1075 TEST_AND_RETURN_FALSE(
1076 writer.Open(new_data_blobs_path.c_str(),
1077 O_WRONLY | O_TRUNC | O_CREAT,
1078 0644) == 0);
1079 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001080 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001081
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001082 for (int i = 0; i < (manifest->install_operations_size() +
1083 manifest->kernel_install_operations_size()); i++) {
1084 DeltaArchiveManifest_InstallOperation* op = NULL;
1085 if (i < manifest->install_operations_size()) {
1086 op = manifest->mutable_install_operations(i);
1087 } else {
1088 op = manifest->mutable_kernel_install_operations(
1089 i - manifest->install_operations_size());
1090 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001091 if (!op->has_data_offset())
1092 continue;
1093 CHECK(op->has_data_length());
1094 vector<char> buf(op->data_length());
1095 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1096 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1097
1098 op->set_data_offset(out_file_size);
1099 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()) ==
1100 static_cast<ssize_t>(buf.size()));
1101 out_file_size += buf.size();
1102 }
1103 return true;
1104}
1105
Andrew de los Reyesef017552010-10-06 17:57:52 -07001106bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1107 const CutEdgeVertexes& cut,
1108 const string& new_root,
1109 int data_fd,
1110 off_t* data_file_size) {
1111 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001112
Andrew de los Reyesef017552010-10-06 17:57:52 -07001113 // Keep all outgoing edges
1114 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1115 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001116
Andrew de los Reyesef017552010-10-06 17:57:52 -07001117 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1118 cut.old_dst,
1119 NULL,
1120 "/-!@:&*nonexistent_path",
1121 new_root,
1122 (*graph)[cut.old_dst].file_name,
1123 data_fd,
1124 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001125
Andrew de los Reyesef017552010-10-06 17:57:52 -07001126 (*graph)[cut.old_dst].out_edges = out_edges;
1127
1128 // Right now we don't have doubly-linked edges, so we have to scan
1129 // the whole graph.
1130 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1131
1132 // Delete temp node
1133 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1134 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1135 (*graph)[cut.old_dst].out_edges.end());
1136 (*graph)[cut.new_vertex].valid = false;
1137 return true;
1138}
1139
1140bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1141 const string& new_root,
1142 int fd,
1143 off_t* data_file_size,
1144 vector<Vertex::Index>* final_order) {
1145 CycleBreaker cycle_breaker;
1146 LOG(INFO) << "Finding cycles...";
1147 set<Edge> cut_edges;
1148 cycle_breaker.BreakCycles(*graph, &cut_edges);
1149 LOG(INFO) << "done finding cycles";
1150 CheckGraph(*graph);
1151
1152 // Calculate number of scratch blocks needed
1153
1154 LOG(INFO) << "Cutting cycles...";
1155 vector<CutEdgeVertexes> cuts;
1156 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1157 LOG(INFO) << "done cutting cycles";
1158 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1159 CheckGraph(*graph);
1160
1161 LOG(INFO) << "Creating initial topological order...";
1162 TopologicalSort(*graph, final_order);
1163 LOG(INFO) << "done with initial topo order";
1164 CheckGraph(*graph);
1165
1166 LOG(INFO) << "Moving full ops to the back";
1167 MoveFullOpsToBack(graph, final_order);
1168 LOG(INFO) << "done moving full ops to back";
1169
1170 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1171 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1172
1173 if (!cuts.empty())
1174 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1175 new_root,
1176 fd,
1177 data_file_size,
1178 final_order,
1179 &inverse_final_order,
1180 cuts));
1181 LOG(INFO) << "Making sure all temp blocks have been allocated";
1182 graph_utils::DumpGraph(*graph);
1183 CHECK(NoTempBlocksRemain(*graph));
1184 LOG(INFO) << "done making sure all temp blocks are allocated";
1185 return true;
1186}
1187
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001188bool DeltaDiffGenerator::ReadFullUpdateFromDisk(
1189 Graph* graph,
1190 const std::string& new_kernel_part,
1191 const std::string& new_image,
Darin Petkov7ea32332010-10-13 10:46:11 -07001192 off_t image_size,
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001193 int fd,
1194 off_t* data_file_size,
1195 off_t chunk_size,
1196 vector<DeltaArchiveManifest_InstallOperation>* kernel_ops,
1197 std::vector<Vertex::Index>* final_order) {
1198 TEST_AND_RETURN_FALSE(chunk_size > 0);
1199 TEST_AND_RETURN_FALSE((chunk_size % kBlockSize) == 0);
Darin Petkov36a58222010-10-07 22:00:09 -07001200
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001201 // Get the sizes early in the function, so we can fail fast if the user
1202 // passed us bad paths.
Darin Petkov7ea32332010-10-13 10:46:11 -07001203 TEST_AND_RETURN_FALSE(image_size >= 0 &&
1204 image_size <= utils::FileSize(new_image));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001205 const off_t kernel_size = utils::FileSize(new_kernel_part);
1206 TEST_AND_RETURN_FALSE(kernel_size >= 0);
1207
1208 off_t part_sizes[] = { image_size, kernel_size };
1209 string paths[] = { new_image, new_kernel_part };
1210
1211 for (int partition = 0; partition < 2; ++partition) {
1212 const string& path = paths[partition];
1213 LOG(INFO) << "compressing " << path;
1214
1215 int in_fd = open(path.c_str(), O_RDONLY, 0);
1216 TEST_AND_RETURN_FALSE(in_fd >= 0);
1217 ScopedFdCloser in_fd_closer(&in_fd);
1218
1219 for (off_t bytes_left = part_sizes[partition], counter = 0, offset = 0;
1220 bytes_left > 0;
1221 bytes_left -= chunk_size, ++counter, offset += chunk_size) {
1222 LOG(INFO) << "offset = " << offset;
1223 DeltaArchiveManifest_InstallOperation* op = NULL;
1224 if (partition == 0) {
1225 graph->resize(graph->size() + 1);
1226 graph->back().file_name = path + StringPrintf("-%" PRIi64, counter);
1227 op = &graph->back().op;
1228 final_order->push_back(graph->size() - 1);
1229 } else {
1230 kernel_ops->resize(kernel_ops->size() + 1);
1231 op = &kernel_ops->back();
1232 }
1233 LOG(INFO) << "have an op";
Darin Petkov36a58222010-10-07 22:00:09 -07001234
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001235 vector<char> buf(min(bytes_left, chunk_size));
1236 LOG(INFO) << "buf size: " << buf.size();
1237 ssize_t bytes_read = -1;
Darin Petkov36a58222010-10-07 22:00:09 -07001238
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001239 TEST_AND_RETURN_FALSE(utils::PReadAll(
1240 in_fd, &buf[0], buf.size(), offset, &bytes_read));
1241 TEST_AND_RETURN_FALSE(bytes_read == static_cast<ssize_t>(buf.size()));
Darin Petkov36a58222010-10-07 22:00:09 -07001242
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001243 vector<char> buf_compressed;
Darin Petkov36a58222010-10-07 22:00:09 -07001244
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001245 TEST_AND_RETURN_FALSE(BzipCompress(buf, &buf_compressed));
1246 const bool compress = buf_compressed.size() < buf.size();
1247 const vector<char>& use_buf = compress ? buf_compressed : buf;
1248 if (compress) {
1249 op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
1250 } else {
1251 op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1252 }
1253 op->set_data_offset(*data_file_size);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001254 TEST_AND_RETURN_FALSE(utils::WriteAll(fd, &use_buf[0], use_buf.size()));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001255 *data_file_size += use_buf.size();
1256 op->set_data_length(use_buf.size());
1257 Extent* dst_extent = op->add_dst_extents();
1258 dst_extent->set_start_block(offset / kBlockSize);
1259 dst_extent->set_num_blocks(chunk_size / kBlockSize);
1260 }
1261 }
1262
1263 return true;
1264}
1265
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001266bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1267 const string& old_root,
1268 const string& old_image,
1269 const string& new_root,
1270 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001271 const string& old_kernel_part,
1272 const string& new_kernel_part,
1273 const string& output_path,
1274 const string& private_key_path) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001275 int old_image_block_count = 0, old_image_block_size = 0;
1276 int new_image_block_count = 0, new_image_block_size = 0;
1277 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1278 &new_image_block_count,
1279 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001280 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001281 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1282 &old_image_block_count,
1283 &old_image_block_size));
1284 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1285 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1286 << "Old and new images have different block counts.";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001287 }
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001288 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001289 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1290
Darin Petkov7ea32332010-10-13 10:46:11 -07001291 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1292 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1293 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001294 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1295 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1296 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1297 }
1298 Graph graph;
1299 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001300
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001301 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
1302 string temp_file_path;
1303 off_t data_file_size = 0;
1304
1305 LOG(INFO) << "Reading files...";
1306
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001307 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1308
Andrew de los Reyesef017552010-10-06 17:57:52 -07001309 vector<Vertex::Index> final_order;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001310 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001311 int fd;
1312 TEST_AND_RETURN_FALSE(
1313 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
1314 TEST_AND_RETURN_FALSE(fd >= 0);
1315 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001316 if (!old_image.empty()) {
1317 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001318
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001319 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1320 &blocks,
1321 old_root,
1322 new_root,
1323 fd,
1324 &data_file_size));
1325 LOG(INFO) << "done reading normal files";
1326 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001327
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001328 graph.resize(graph.size() + 1);
1329 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1330 fd,
1331 &data_file_size,
1332 new_image,
1333 &graph.back()));
1334
1335 // Read kernel partition
1336 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1337 new_kernel_part,
1338 &kernel_ops,
1339 fd,
1340 &data_file_size));
1341
1342 LOG(INFO) << "done reading kernel";
1343 CheckGraph(graph);
1344
1345 LOG(INFO) << "Creating edges...";
1346 CreateEdges(&graph, blocks);
1347 LOG(INFO) << "Done creating edges";
1348 CheckGraph(graph);
1349
1350 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1351 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001352 fd,
1353 &data_file_size,
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001354 &final_order));
1355 } else {
1356 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001357 off_t new_image_size =
1358 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001359 TEST_AND_RETURN_FALSE(ReadFullUpdateFromDisk(&graph,
1360 new_kernel_part,
1361 new_image,
Darin Petkov7ea32332010-10-13 10:46:11 -07001362 new_image_size,
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001363 fd,
1364 &data_file_size,
1365 kFullUpdateChunkSize,
1366 &kernel_ops,
1367 &final_order));
1368 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001369 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001370
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001371 // Convert to protobuf Manifest object
1372 DeltaArchiveManifest manifest;
1373 CheckGraph(graph);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001374 InstallOperationsToManifest(graph, final_order, kernel_ops, &manifest);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001375
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001376 CheckGraph(graph);
1377 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001378
1379 // Reorder the data blobs with the newly ordered manifest
1380 string ordered_blobs_path;
1381 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
1382 "/tmp/CrAU_temp_data.ordered.XXXXXX",
1383 &ordered_blobs_path,
1384 false));
1385 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1386 temp_file_path,
1387 ordered_blobs_path));
1388
1389 // Check that install op blobs are in order and that all blocks are written.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001390 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001391 {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001392 vector<uint32_t> written_count(blocks.size(), 0);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001393 for (int i = 0; i < (manifest.install_operations_size() +
1394 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001395 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001396 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001397 manifest.mutable_install_operations(i) :
1398 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001399 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001400 for (int j = 0; j < op->dst_extents_size(); j++) {
1401 const Extent& extent = op->dst_extents(j);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001402 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001403 block < (extent.start_block() + extent.num_blocks()); block++) {
Darin Petkovc0b7a532010-09-29 15:18:14 -07001404 if (block < blocks.size())
1405 written_count[block]++;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001406 }
1407 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001408 if (op->has_data_offset()) {
1409 if (op->data_offset() != next_blob_offset) {
1410 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001411 << next_blob_offset;
1412 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001413 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001414 }
1415 }
1416 // check all blocks written to
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001417 for (vector<uint32_t>::size_type i = 0; i < written_count.size(); i++) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001418 if (written_count[i] == 0) {
1419 LOG(FATAL) << "block " << i << " not written!";
1420 }
1421 }
1422 }
1423
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001424 // Signatures appear at the end of the blobs. Note the offset in the
1425 // manifest
1426 if (!private_key_path.empty()) {
1427 LOG(INFO) << "Making room for signature in file";
1428 manifest.set_signatures_offset(next_blob_offset);
1429 LOG(INFO) << "set? " << manifest.has_signatures_offset();
1430 // Add a dummy op at the end to appease older clients
1431 DeltaArchiveManifest_InstallOperation* dummy_op =
1432 manifest.add_kernel_install_operations();
1433 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1434 dummy_op->set_data_offset(next_blob_offset);
1435 manifest.set_signatures_offset(next_blob_offset);
1436 uint64_t signature_blob_length = 0;
1437 TEST_AND_RETURN_FALSE(
1438 PayloadSigner::SignatureBlobLength(private_key_path,
1439 &signature_blob_length));
1440 dummy_op->set_data_length(signature_blob_length);
1441 manifest.set_signatures_size(signature_blob_length);
1442 Extent* dummy_extent = dummy_op->add_dst_extents();
1443 // Tell the dummy op to write this data to a big sparse hole
1444 dummy_extent->set_start_block(kSparseHole);
1445 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1446 kBlockSize);
1447 }
1448
Darin Petkov36a58222010-10-07 22:00:09 -07001449 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1450 new_kernel_part,
1451 old_image,
1452 new_image,
1453 &manifest));
1454
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001455 // Serialize protobuf
1456 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001457
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001458 CheckGraph(graph);
1459 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1460 CheckGraph(graph);
1461
1462 LOG(INFO) << "Writing final delta file header...";
1463 DirectFileWriter writer;
1464 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1465 O_WRONLY | O_CREAT | O_TRUNC,
1466 0644) == 0);
1467 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001468
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001469 // Write header
1470 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)) ==
Andrew de los Reyes08c4e272010-04-15 14:02:17 -07001471 static_cast<ssize_t>(strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001472
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001473 // Write version number
1474 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001475
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001476 // Write protobuf length
1477 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1478 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001479
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001480 // Write protobuf
1481 LOG(INFO) << "Writing final delta file protobuf... "
1482 << serialized_manifest.size();
1483 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
1484 serialized_manifest.size()) ==
1485 static_cast<ssize_t>(serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001486
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001487 // Append the data blobs
1488 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001489 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001490 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1491 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1492 for (;;) {
1493 char buf[kBlockSize];
1494 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1495 if (0 == rc) {
1496 // EOF
1497 break;
1498 }
1499 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
1500 TEST_AND_RETURN_FALSE(writer.Write(buf, rc) == rc);
1501 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001502
1503 // Write signature blob.
1504 if (!private_key_path.empty()) {
1505 LOG(INFO) << "Signing the update...";
1506 vector<char> signature_blob;
1507 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(output_path,
1508 private_key_path,
1509 &signature_blob));
1510 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
1511 signature_blob.size()) ==
1512 static_cast<ssize_t>(signature_blob.size()));
1513 }
1514
Darin Petkov95cf01f2010-10-12 14:59:13 -07001515 int64_t manifest_metadata_size =
1516 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
1517 ReportPayloadUsage(graph, manifest, manifest_metadata_size);
Darin Petkov880335c2010-10-01 15:52:53 -07001518
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001519 LOG(INFO) << "All done. Successfully created delta file.";
1520 return true;
1521}
1522
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001523const char* const kBsdiffPath = "/usr/bin/bsdiff";
1524const char* const kBspatchPath = "/usr/bin/bspatch";
1525const char* const kDeltaMagic = "CrAU";
1526
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001527}; // namespace chromeos_update_engine