blob: 3dcd2cbd2390f00944b5052116038ee64f5b1dea [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 {
449 return size < object.size;
450 }
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 Petkov698d0412010-10-13 10:59:44 -0700621 LOG(INFO) << "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 Petkov698d0412010-10-13 10:59:44 -0700630 // TODO(petkov): Generate the old kernel info when we stop generating full
631 // updates for the kernel partition.
Darin Petkov36a58222010-10-07 22:00:09 -0700632 TEST_AND_RETURN_FALSE(
Darin Petkov7ea32332010-10-13 10:46:11 -0700633 InitializePartitionInfo(true,
634 new_kernel,
635 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700636 if (!old_rootfs.empty()) {
637 TEST_AND_RETURN_FALSE(
Darin Petkov7ea32332010-10-13 10:46:11 -0700638 InitializePartitionInfo(false,
639 old_rootfs,
Darin Petkov36a58222010-10-07 22:00:09 -0700640 manifest->mutable_old_rootfs_info()));
641 }
642 TEST_AND_RETURN_FALSE(
Darin Petkov7ea32332010-10-13 10:46:11 -0700643 InitializePartitionInfo(false,
644 new_rootfs,
645 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700646 return true;
647}
648
Andrew de los Reyesef017552010-10-06 17:57:52 -0700649namespace {
650
651// Takes a collection (vector or RepeatedPtrField) of Extent and
652// returns a vector of the blocks referenced, in order.
653template<typename T>
654vector<uint64_t> ExpandExtents(const T& extents) {
655 vector<uint64_t> ret;
656 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
657 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700658 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700659 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700660 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700661 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700662 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700663 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700664 }
665 }
666 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700667 return ret;
668}
669
670// Takes a vector of blocks and returns an equivalent vector of Extent
671// objects.
672vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
673 vector<Extent> new_extents;
674 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
675 it != e; ++it) {
676 graph_utils::AppendBlockToExtents(&new_extents, *it);
677 }
678 return new_extents;
679}
680
681} // namespace {}
682
683void DeltaDiffGenerator::SubstituteBlocks(
684 Vertex* vertex,
685 const vector<Extent>& remove_extents,
686 const vector<Extent>& replace_extents) {
687 // First, expand out the blocks that op reads from
688 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700689 {
690 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700691 vector<uint64_t> remove_extents_expanded =
692 ExpandExtents(remove_extents);
693 vector<uint64_t> replace_extents_expanded =
694 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700695 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700696 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700697 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700698 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700699 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
700 }
701 utils::ApplyMap(&read_blocks, conversion);
702 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
703 e = vertex->out_edges.end(); it != e; ++it) {
704 vector<uint64_t> write_before_deps_expanded =
705 ExpandExtents(it->second.write_extents);
706 utils::ApplyMap(&write_before_deps_expanded, conversion);
707 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700708 }
709 }
710 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700711 vertex->op.clear_src_extents();
712 vector<Extent> new_extents = CompressExtents(read_blocks);
713 DeltaDiffGenerator::StoreExtents(new_extents,
714 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700715}
716
717bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700718 const set<Edge>& edges,
719 vector<CutEdgeVertexes>* out_cuts) {
720 DummyExtentAllocator scratch_allocator;
721 vector<CutEdgeVertexes> cuts;
722 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700723
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700724 uint64_t scratch_blocks_used = 0;
725 for (set<Edge>::const_iterator it = edges.begin();
726 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700727 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700728 vector<Extent> old_extents =
729 (*graph)[it->first].out_edges[it->second].extents;
730 // Choose some scratch space
731 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
732 LOG(INFO) << "using " << graph_utils::EdgeWeight(*graph, *it)
733 << " scratch blocks ("
734 << scratch_blocks_used << ")";
Andrew de los Reyesef017552010-10-06 17:57:52 -0700735 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700736 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
737 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700738 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700739 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700740 cuts.back().old_src = it->first;
741 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700742
Andrew de los Reyesef017552010-10-06 17:57:52 -0700743 EdgeProperties& cut_edge_properties =
744 (*graph)[it->first].out_edges.find(it->second)->second;
745
746 // This should never happen, as we should only be cutting edges between
747 // real file nodes, and write-before relationships are created from
748 // a real file node to a temp copy node:
749 CHECK(cut_edge_properties.write_extents.empty())
750 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700751
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700752 // make node depend on the copy operation
753 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700754 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700755
756 // Set src/dst extents and other proto variables for copy operation
757 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
758 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700759 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700760 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700761 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700762 graph->back().op.mutable_dst_extents());
763 graph->back().op.set_src_length(
764 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
765 graph->back().op.set_dst_length(graph->back().op.src_length());
766
767 // make the dest node read from the scratch space
768 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700769 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700770 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700771 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700772
773 // delete the old edge
774 CHECK_EQ(1, (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700775
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700776 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700777 EdgeProperties write_before_edge_properties;
778 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
779 (*graph)[it->second].out_edges.insert(
780 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700781 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700782 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700783 return true;
784}
785
786// Stores all Extents in 'extents' into 'out'.
787void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700788 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700789 google::protobuf::RepeatedPtrField<Extent>* out) {
790 for (vector<Extent>::const_iterator it = extents.begin();
791 it != extents.end(); ++it) {
792 Extent* new_extent = out->Add();
793 *new_extent = *it;
794 }
795}
796
797// Creates all the edges for the graph. Writers of a block point to
798// readers of the same block. This is because for an edge A->B, B
799// must complete before A executes.
800void DeltaDiffGenerator::CreateEdges(Graph* graph,
801 const vector<Block>& blocks) {
802 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
803 // Blocks with both a reader and writer get an edge
804 if (blocks[i].reader == Vertex::kInvalidIndex ||
805 blocks[i].writer == Vertex::kInvalidIndex)
806 continue;
807 // Don't have a node depend on itself
808 if (blocks[i].reader == blocks[i].writer)
809 continue;
810 // See if there's already an edge we can add onto
811 Vertex::EdgeMap::iterator edge_it =
812 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
813 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
814 // No existing edge. Create one
815 (*graph)[blocks[i].writer].out_edges.insert(
816 make_pair(blocks[i].reader, EdgeProperties()));
817 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700818 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700819 }
820 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
821 }
822}
823
Andrew de los Reyesef017552010-10-06 17:57:52 -0700824namespace {
825
826class SortCutsByTopoOrderLess {
827 public:
828 SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table)
829 : table_(table) {}
830 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
831 return table_[a.old_dst] < table_[b.old_dst];
832 }
833 private:
834 vector<vector<Vertex::Index>::size_type>& table_;
835};
836
837} // namespace {}
838
839void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
840 vector<Vertex::Index>& op_indexes,
841 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
842 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
843 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
844 i != e; ++i) {
845 Vertex::Index node = op_indexes[i];
846 if (table.size() < (node + 1)) {
847 table.resize(node + 1);
848 }
849 table[node] = i;
850 }
851 reverse_op_indexes->swap(table);
852}
853
854void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes,
855 vector<CutEdgeVertexes>* cuts) {
856 // first, make a reverse lookup table.
857 vector<vector<Vertex::Index>::size_type> table;
858 GenerateReverseTopoOrderMap(op_indexes, &table);
859 SortCutsByTopoOrderLess less(table);
860 sort(cuts->begin(), cuts->end(), less);
861}
862
863void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
864 vector<Vertex::Index>* op_indexes) {
865 vector<Vertex::Index> ret;
866 vector<Vertex::Index> full_ops;
867 ret.reserve(op_indexes->size());
868 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
869 ++i) {
870 DeltaArchiveManifest_InstallOperation_Type type =
871 (*graph)[(*op_indexes)[i]].op.type();
872 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
873 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
874 full_ops.push_back((*op_indexes)[i]);
875 } else {
876 ret.push_back((*op_indexes)[i]);
877 }
878 }
879 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
880 << (full_ops.size() + ret.size()) << " total ops.";
881 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
882 op_indexes->swap(ret);
883}
884
885namespace {
886
887template<typename T>
888bool TempBlocksExistInExtents(const T& extents) {
889 for (int i = 0, e = extents.size(); i < e; ++i) {
890 Extent extent = graph_utils::GetElement(extents, i);
891 uint64_t start = extent.start_block();
892 uint64_t num = extent.num_blocks();
893 if (start == kSparseHole)
894 continue;
895 if (start >= kTempBlockStart ||
896 (start + num) >= kTempBlockStart) {
897 LOG(ERROR) << "temp block!";
898 LOG(ERROR) << "start: " << start << ", num: " << num;
899 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
900 LOG(ERROR) << "returning true";
901 return true;
902 }
903 // check for wrap-around, which would be a bug:
904 CHECK(start <= (start + num));
905 }
906 return false;
907}
908
909} // namespace {}
910
911bool DeltaDiffGenerator::AssignTempBlocks(
912 Graph* graph,
913 const string& new_root,
914 int data_fd,
915 off_t* data_file_size,
916 vector<Vertex::Index>* op_indexes,
917 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
918 vector<CutEdgeVertexes>& cuts) {
919 CHECK(!cuts.empty());
920 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
921 true ; --i) {
922 LOG(INFO) << "Fixing temp blocks in cut " << i
923 << ": old dst: " << cuts[i].old_dst << " new vertex: "
924 << cuts[i].new_vertex;
925 const uint64_t blocks_needed =
926 graph_utils::BlocksInExtents(cuts[i].tmp_extents);
927 LOG(INFO) << "Scanning for usable blocks (" << blocks_needed << " needed)";
928 // For now, just look for a single op w/ sufficient blocks, not
929 // considering blocks from outgoing read-before deps.
930 Vertex::Index node = cuts[i].old_dst;
931 DeltaArchiveManifest_InstallOperation_Type node_type =
932 (*graph)[node].op.type();
933 if (node_type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
934 node_type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
935 LOG(INFO) << "This was already converted to full, so skipping.";
936 // Delete the temp node and pointer to it from old src
937 if (!(*graph)[cuts[i].old_src].out_edges.erase(cuts[i].new_vertex)) {
938 LOG(INFO) << "Odd. node " << cuts[i].old_src << " didn't point to "
939 << cuts[i].new_vertex;
940 }
941 (*graph)[cuts[i].new_vertex].valid = false;
942 vector<Vertex::Index>::size_type new_topo_idx =
943 (*reverse_op_indexes)[cuts[i].new_vertex];
944 op_indexes->erase(op_indexes->begin() + new_topo_idx);
945 GenerateReverseTopoOrderMap(*op_indexes, reverse_op_indexes);
946 continue;
947 }
948 bool found_node = false;
949 for (vector<Vertex::Index>::size_type j = (*reverse_op_indexes)[node] + 1,
950 je = op_indexes->size(); j < je; ++j) {
951 Vertex::Index test_node = (*op_indexes)[j];
952 // See if this node has sufficient blocks
953 ExtentRanges ranges;
954 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
955 ranges.SubtractExtent(ExtentForRange(
956 kTempBlockStart, kSparseHole - kTempBlockStart));
957 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
958 // For now, for simplicity, subtract out all blocks in read-before
959 // dependencies.
960 for (Vertex::EdgeMap::const_iterator edge_i =
961 (*graph)[test_node].out_edges.begin(),
962 edge_e = (*graph)[test_node].out_edges.end();
963 edge_i != edge_e; ++edge_i) {
964 ranges.SubtractExtents(edge_i->second.extents);
965 }
Darin Petkov36a58222010-10-07 22:00:09 -0700966
Andrew de los Reyesef017552010-10-06 17:57:52 -0700967 uint64_t blocks_found = ranges.blocks();
968 if (blocks_found < blocks_needed) {
969 if (blocks_found > 0)
970 LOG(INFO) << "insufficient blocks found in topo node " << j
971 << " (node " << (*op_indexes)[j] << "). Found only "
972 << blocks_found;
973 continue;
974 }
975 found_node = true;
976 LOG(INFO) << "Found sufficient blocks in topo node " << j
977 << " (node " << (*op_indexes)[j] << ")";
978 // Sub in the blocks, and make the node supplying the blocks
979 // depend on old_dst.
980 vector<Extent> real_extents =
981 ranges.GetExtentsForBlockCount(blocks_needed);
Darin Petkov36a58222010-10-07 22:00:09 -0700982
Andrew de los Reyesef017552010-10-06 17:57:52 -0700983 // Fix the old dest node w/ the real blocks
984 SubstituteBlocks(&(*graph)[node],
985 cuts[i].tmp_extents,
986 real_extents);
Darin Petkov36a58222010-10-07 22:00:09 -0700987
Andrew de los Reyesef017552010-10-06 17:57:52 -0700988 // Fix the new node w/ the real blocks. Since the new node is just a
989 // copy operation, we can replace all the dest extents w/ the real
990 // blocks.
991 DeltaArchiveManifest_InstallOperation *op =
992 &(*graph)[cuts[i].new_vertex].op;
993 op->clear_dst_extents();
994 StoreExtents(real_extents, op->mutable_dst_extents());
Darin Petkov36a58222010-10-07 22:00:09 -0700995
Andrew de los Reyesef017552010-10-06 17:57:52 -0700996 // Add an edge from the real-block supplier to the old dest block.
997 graph_utils::AddReadBeforeDepExtents(&(*graph)[test_node],
998 node,
999 real_extents);
1000 break;
1001 }
1002 if (!found_node) {
1003 // convert to full op
1004 LOG(WARNING) << "Failed to find enough temp blocks for cut " << i
1005 << " with old dest (graph node " << node
1006 << "). Converting to a full op, at the expense of a "
1007 << "good compression ratio.";
1008 TEST_AND_RETURN_FALSE(ConvertCutToFullOp(graph,
1009 cuts[i],
1010 new_root,
1011 data_fd,
1012 data_file_size));
1013 // move the full op to the back
1014 vector<Vertex::Index> new_op_indexes;
1015 for (vector<Vertex::Index>::const_iterator iter_i = op_indexes->begin(),
1016 iter_e = op_indexes->end(); iter_i != iter_e; ++iter_i) {
1017 if ((*iter_i == cuts[i].old_dst) || (*iter_i == cuts[i].new_vertex))
1018 continue;
1019 new_op_indexes.push_back(*iter_i);
1020 }
1021 new_op_indexes.push_back(cuts[i].old_dst);
1022 op_indexes->swap(new_op_indexes);
Darin Petkov36a58222010-10-07 22:00:09 -07001023
Andrew de los Reyesef017552010-10-06 17:57:52 -07001024 GenerateReverseTopoOrderMap(*op_indexes, reverse_op_indexes);
1025 }
1026 if (i == e) {
1027 // break out of for() loop
1028 break;
1029 }
1030 }
1031 return true;
1032}
1033
1034bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1035 size_t idx = 0;
1036 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1037 ++it, ++idx) {
1038 if (!it->valid)
1039 continue;
1040 const DeltaArchiveManifest_InstallOperation& op = it->op;
1041 if (TempBlocksExistInExtents(op.dst_extents()) ||
1042 TempBlocksExistInExtents(op.src_extents())) {
1043 LOG(INFO) << "bad extents in node " << idx;
1044 LOG(INFO) << "so yeah";
1045 return false;
1046 }
1047
1048 // Check out-edges:
1049 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1050 je = it->out_edges.end(); jt != je; ++jt) {
1051 if (TempBlocksExistInExtents(jt->second.extents) ||
1052 TempBlocksExistInExtents(jt->second.write_extents)) {
1053 LOG(INFO) << "bad out edge in node " << idx;
1054 LOG(INFO) << "so yeah";
1055 return false;
1056 }
1057 }
1058 }
1059 return true;
1060}
1061
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001062bool DeltaDiffGenerator::ReorderDataBlobs(
1063 DeltaArchiveManifest* manifest,
1064 const std::string& data_blobs_path,
1065 const std::string& new_data_blobs_path) {
1066 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1067 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1068 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001069
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001070 DirectFileWriter writer;
1071 TEST_AND_RETURN_FALSE(
1072 writer.Open(new_data_blobs_path.c_str(),
1073 O_WRONLY | O_TRUNC | O_CREAT,
1074 0644) == 0);
1075 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001076 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001077
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001078 for (int i = 0; i < (manifest->install_operations_size() +
1079 manifest->kernel_install_operations_size()); i++) {
1080 DeltaArchiveManifest_InstallOperation* op = NULL;
1081 if (i < manifest->install_operations_size()) {
1082 op = manifest->mutable_install_operations(i);
1083 } else {
1084 op = manifest->mutable_kernel_install_operations(
1085 i - manifest->install_operations_size());
1086 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001087 if (!op->has_data_offset())
1088 continue;
1089 CHECK(op->has_data_length());
1090 vector<char> buf(op->data_length());
1091 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1092 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1093
1094 op->set_data_offset(out_file_size);
1095 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()) ==
1096 static_cast<ssize_t>(buf.size()));
1097 out_file_size += buf.size();
1098 }
1099 return true;
1100}
1101
Andrew de los Reyesef017552010-10-06 17:57:52 -07001102bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1103 const CutEdgeVertexes& cut,
1104 const string& new_root,
1105 int data_fd,
1106 off_t* data_file_size) {
1107 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001108
Andrew de los Reyesef017552010-10-06 17:57:52 -07001109 // Keep all outgoing edges
1110 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1111 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001112
Andrew de los Reyesef017552010-10-06 17:57:52 -07001113 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1114 cut.old_dst,
1115 NULL,
1116 "/-!@:&*nonexistent_path",
1117 new_root,
1118 (*graph)[cut.old_dst].file_name,
1119 data_fd,
1120 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001121
Andrew de los Reyesef017552010-10-06 17:57:52 -07001122 (*graph)[cut.old_dst].out_edges = out_edges;
1123
1124 // Right now we don't have doubly-linked edges, so we have to scan
1125 // the whole graph.
1126 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1127
1128 // Delete temp node
1129 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1130 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1131 (*graph)[cut.old_dst].out_edges.end());
1132 (*graph)[cut.new_vertex].valid = false;
1133 return true;
1134}
1135
1136bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1137 const string& new_root,
1138 int fd,
1139 off_t* data_file_size,
1140 vector<Vertex::Index>* final_order) {
1141 CycleBreaker cycle_breaker;
1142 LOG(INFO) << "Finding cycles...";
1143 set<Edge> cut_edges;
1144 cycle_breaker.BreakCycles(*graph, &cut_edges);
1145 LOG(INFO) << "done finding cycles";
1146 CheckGraph(*graph);
1147
1148 // Calculate number of scratch blocks needed
1149
1150 LOG(INFO) << "Cutting cycles...";
1151 vector<CutEdgeVertexes> cuts;
1152 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1153 LOG(INFO) << "done cutting cycles";
1154 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1155 CheckGraph(*graph);
1156
1157 LOG(INFO) << "Creating initial topological order...";
1158 TopologicalSort(*graph, final_order);
1159 LOG(INFO) << "done with initial topo order";
1160 CheckGraph(*graph);
1161
1162 LOG(INFO) << "Moving full ops to the back";
1163 MoveFullOpsToBack(graph, final_order);
1164 LOG(INFO) << "done moving full ops to back";
1165
1166 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1167 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1168
1169 if (!cuts.empty())
1170 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1171 new_root,
1172 fd,
1173 data_file_size,
1174 final_order,
1175 &inverse_final_order,
1176 cuts));
1177 LOG(INFO) << "Making sure all temp blocks have been allocated";
1178 graph_utils::DumpGraph(*graph);
1179 CHECK(NoTempBlocksRemain(*graph));
1180 LOG(INFO) << "done making sure all temp blocks are allocated";
1181 return true;
1182}
1183
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001184bool DeltaDiffGenerator::ReadFullUpdateFromDisk(
1185 Graph* graph,
1186 const std::string& new_kernel_part,
1187 const std::string& new_image,
Darin Petkov7ea32332010-10-13 10:46:11 -07001188 off_t image_size,
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001189 int fd,
1190 off_t* data_file_size,
1191 off_t chunk_size,
1192 vector<DeltaArchiveManifest_InstallOperation>* kernel_ops,
1193 std::vector<Vertex::Index>* final_order) {
1194 TEST_AND_RETURN_FALSE(chunk_size > 0);
1195 TEST_AND_RETURN_FALSE((chunk_size % kBlockSize) == 0);
Darin Petkov36a58222010-10-07 22:00:09 -07001196
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001197 // Get the sizes early in the function, so we can fail fast if the user
1198 // passed us bad paths.
Darin Petkov7ea32332010-10-13 10:46:11 -07001199 TEST_AND_RETURN_FALSE(image_size >= 0 &&
1200 image_size <= utils::FileSize(new_image));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001201 const off_t kernel_size = utils::FileSize(new_kernel_part);
1202 TEST_AND_RETURN_FALSE(kernel_size >= 0);
1203
1204 off_t part_sizes[] = { image_size, kernel_size };
1205 string paths[] = { new_image, new_kernel_part };
1206
1207 for (int partition = 0; partition < 2; ++partition) {
1208 const string& path = paths[partition];
1209 LOG(INFO) << "compressing " << path;
1210
1211 int in_fd = open(path.c_str(), O_RDONLY, 0);
1212 TEST_AND_RETURN_FALSE(in_fd >= 0);
1213 ScopedFdCloser in_fd_closer(&in_fd);
1214
1215 for (off_t bytes_left = part_sizes[partition], counter = 0, offset = 0;
1216 bytes_left > 0;
1217 bytes_left -= chunk_size, ++counter, offset += chunk_size) {
1218 LOG(INFO) << "offset = " << offset;
1219 DeltaArchiveManifest_InstallOperation* op = NULL;
1220 if (partition == 0) {
1221 graph->resize(graph->size() + 1);
1222 graph->back().file_name = path + StringPrintf("-%" PRIi64, counter);
1223 op = &graph->back().op;
1224 final_order->push_back(graph->size() - 1);
1225 } else {
1226 kernel_ops->resize(kernel_ops->size() + 1);
1227 op = &kernel_ops->back();
1228 }
1229 LOG(INFO) << "have an op";
Darin Petkov36a58222010-10-07 22:00:09 -07001230
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001231 vector<char> buf(min(bytes_left, chunk_size));
1232 LOG(INFO) << "buf size: " << buf.size();
1233 ssize_t bytes_read = -1;
Darin Petkov36a58222010-10-07 22:00:09 -07001234
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001235 TEST_AND_RETURN_FALSE(utils::PReadAll(
1236 in_fd, &buf[0], buf.size(), offset, &bytes_read));
1237 TEST_AND_RETURN_FALSE(bytes_read == static_cast<ssize_t>(buf.size()));
Darin Petkov36a58222010-10-07 22:00:09 -07001238
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001239 vector<char> buf_compressed;
Darin Petkov36a58222010-10-07 22:00:09 -07001240
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001241 TEST_AND_RETURN_FALSE(BzipCompress(buf, &buf_compressed));
1242 const bool compress = buf_compressed.size() < buf.size();
1243 const vector<char>& use_buf = compress ? buf_compressed : buf;
1244 if (compress) {
1245 op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
1246 } else {
1247 op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1248 }
1249 op->set_data_offset(*data_file_size);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001250 TEST_AND_RETURN_FALSE(utils::WriteAll(fd, &use_buf[0], use_buf.size()));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001251 *data_file_size += use_buf.size();
1252 op->set_data_length(use_buf.size());
1253 Extent* dst_extent = op->add_dst_extents();
1254 dst_extent->set_start_block(offset / kBlockSize);
1255 dst_extent->set_num_blocks(chunk_size / kBlockSize);
1256 }
1257 }
1258
1259 return true;
1260}
1261
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001262bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1263 const string& old_root,
1264 const string& old_image,
1265 const string& new_root,
1266 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001267 const string& old_kernel_part,
1268 const string& new_kernel_part,
1269 const string& output_path,
1270 const string& private_key_path) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001271 int old_image_block_count = 0, old_image_block_size = 0;
1272 int new_image_block_count = 0, new_image_block_size = 0;
1273 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1274 &new_image_block_count,
1275 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001276 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001277 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1278 &old_image_block_count,
1279 &old_image_block_size));
1280 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1281 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1282 << "Old and new images have different block counts.";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001283 }
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001284 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001285 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1286
Darin Petkov7ea32332010-10-13 10:46:11 -07001287 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1288 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1289 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001290 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1291 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1292 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1293 }
1294 Graph graph;
1295 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001296
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001297 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
1298 string temp_file_path;
1299 off_t data_file_size = 0;
1300
1301 LOG(INFO) << "Reading files...";
1302
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001303 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1304
Andrew de los Reyesef017552010-10-06 17:57:52 -07001305 vector<Vertex::Index> final_order;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001306 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001307 int fd;
1308 TEST_AND_RETURN_FALSE(
1309 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
1310 TEST_AND_RETURN_FALSE(fd >= 0);
1311 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001312 if (!old_image.empty()) {
1313 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001314
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001315 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1316 &blocks,
1317 old_root,
1318 new_root,
1319 fd,
1320 &data_file_size));
1321 LOG(INFO) << "done reading normal files";
1322 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001323
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001324 graph.resize(graph.size() + 1);
1325 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1326 fd,
1327 &data_file_size,
1328 new_image,
1329 &graph.back()));
1330
1331 // Read kernel partition
1332 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1333 new_kernel_part,
1334 &kernel_ops,
1335 fd,
1336 &data_file_size));
1337
1338 LOG(INFO) << "done reading kernel";
1339 CheckGraph(graph);
1340
1341 LOG(INFO) << "Creating edges...";
1342 CreateEdges(&graph, blocks);
1343 LOG(INFO) << "Done creating edges";
1344 CheckGraph(graph);
1345
1346 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1347 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001348 fd,
1349 &data_file_size,
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001350 &final_order));
1351 } else {
1352 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001353 off_t new_image_size =
1354 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001355 TEST_AND_RETURN_FALSE(ReadFullUpdateFromDisk(&graph,
1356 new_kernel_part,
1357 new_image,
Darin Petkov7ea32332010-10-13 10:46:11 -07001358 new_image_size,
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001359 fd,
1360 &data_file_size,
1361 kFullUpdateChunkSize,
1362 &kernel_ops,
1363 &final_order));
1364 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001365 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001366
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001367 // Convert to protobuf Manifest object
1368 DeltaArchiveManifest manifest;
1369 CheckGraph(graph);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001370 InstallOperationsToManifest(graph, final_order, kernel_ops, &manifest);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001371
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001372 CheckGraph(graph);
1373 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001374
1375 // Reorder the data blobs with the newly ordered manifest
1376 string ordered_blobs_path;
1377 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
1378 "/tmp/CrAU_temp_data.ordered.XXXXXX",
1379 &ordered_blobs_path,
1380 false));
1381 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1382 temp_file_path,
1383 ordered_blobs_path));
1384
1385 // Check that install op blobs are in order and that all blocks are written.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001386 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001387 {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001388 vector<uint32_t> written_count(blocks.size(), 0);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001389 for (int i = 0; i < (manifest.install_operations_size() +
1390 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001391 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001392 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001393 manifest.mutable_install_operations(i) :
1394 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001395 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001396 for (int j = 0; j < op->dst_extents_size(); j++) {
1397 const Extent& extent = op->dst_extents(j);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001398 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001399 block < (extent.start_block() + extent.num_blocks()); block++) {
Darin Petkovc0b7a532010-09-29 15:18:14 -07001400 if (block < blocks.size())
1401 written_count[block]++;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001402 }
1403 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001404 if (op->has_data_offset()) {
1405 if (op->data_offset() != next_blob_offset) {
1406 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001407 << next_blob_offset;
1408 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001409 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001410 }
1411 }
1412 // check all blocks written to
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001413 for (vector<uint32_t>::size_type i = 0; i < written_count.size(); i++) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001414 if (written_count[i] == 0) {
1415 LOG(FATAL) << "block " << i << " not written!";
1416 }
1417 }
1418 }
1419
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001420 // Signatures appear at the end of the blobs. Note the offset in the
1421 // manifest
1422 if (!private_key_path.empty()) {
1423 LOG(INFO) << "Making room for signature in file";
1424 manifest.set_signatures_offset(next_blob_offset);
1425 LOG(INFO) << "set? " << manifest.has_signatures_offset();
1426 // Add a dummy op at the end to appease older clients
1427 DeltaArchiveManifest_InstallOperation* dummy_op =
1428 manifest.add_kernel_install_operations();
1429 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1430 dummy_op->set_data_offset(next_blob_offset);
1431 manifest.set_signatures_offset(next_blob_offset);
1432 uint64_t signature_blob_length = 0;
1433 TEST_AND_RETURN_FALSE(
1434 PayloadSigner::SignatureBlobLength(private_key_path,
1435 &signature_blob_length));
1436 dummy_op->set_data_length(signature_blob_length);
1437 manifest.set_signatures_size(signature_blob_length);
1438 Extent* dummy_extent = dummy_op->add_dst_extents();
1439 // Tell the dummy op to write this data to a big sparse hole
1440 dummy_extent->set_start_block(kSparseHole);
1441 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1442 kBlockSize);
1443 }
1444
Darin Petkov36a58222010-10-07 22:00:09 -07001445 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1446 new_kernel_part,
1447 old_image,
1448 new_image,
1449 &manifest));
1450
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001451 // Serialize protobuf
1452 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001453
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001454 CheckGraph(graph);
1455 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1456 CheckGraph(graph);
1457
1458 LOG(INFO) << "Writing final delta file header...";
1459 DirectFileWriter writer;
1460 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1461 O_WRONLY | O_CREAT | O_TRUNC,
1462 0644) == 0);
1463 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001464
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001465 // Write header
1466 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)) ==
Andrew de los Reyes08c4e272010-04-15 14:02:17 -07001467 static_cast<ssize_t>(strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001468
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001469 // Write version number
1470 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001471
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001472 // Write protobuf length
1473 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1474 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001475
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001476 // Write protobuf
1477 LOG(INFO) << "Writing final delta file protobuf... "
1478 << serialized_manifest.size();
1479 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
1480 serialized_manifest.size()) ==
1481 static_cast<ssize_t>(serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001482
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001483 // Append the data blobs
1484 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001485 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001486 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1487 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1488 for (;;) {
1489 char buf[kBlockSize];
1490 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1491 if (0 == rc) {
1492 // EOF
1493 break;
1494 }
1495 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
1496 TEST_AND_RETURN_FALSE(writer.Write(buf, rc) == rc);
1497 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001498
1499 // Write signature blob.
1500 if (!private_key_path.empty()) {
1501 LOG(INFO) << "Signing the update...";
1502 vector<char> signature_blob;
1503 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(output_path,
1504 private_key_path,
1505 &signature_blob));
1506 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
1507 signature_blob.size()) ==
1508 static_cast<ssize_t>(signature_blob.size()));
1509 }
1510
Darin Petkov95cf01f2010-10-12 14:59:13 -07001511 int64_t manifest_metadata_size =
1512 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
1513 ReportPayloadUsage(graph, manifest, manifest_metadata_size);
Darin Petkov880335c2010-10-01 15:52:53 -07001514
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001515 LOG(INFO) << "All done. Successfully created delta file.";
1516 return true;
1517}
1518
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001519const char* const kBsdiffPath = "/usr/bin/bsdiff";
1520const char* const kBspatchPath = "/usr/bin/bspatch";
1521const char* const kDeltaMagic = "CrAU";
1522
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001523}; // namespace chromeos_update_engine