blob: 48730a83229e0436860805f093314ad4e7330534 [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// 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"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07006#include <sys/stat.h>
7#include <sys/types.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <algorithm>
11#include <set>
12#include <string>
13#include <utility>
14#include <vector>
15#include <bzlib.h>
Chris Masone790e62e2010-08-12 10:41:18 -070016#include "base/logging.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070017#include "update_engine/bzip.h"
18#include "update_engine/cycle_breaker.h"
19#include "update_engine/extent_mapper.h"
20#include "update_engine/file_writer.h"
21#include "update_engine/filesystem_iterator.h"
22#include "update_engine/graph_types.h"
23#include "update_engine/graph_utils.h"
24#include "update_engine/subprocess.h"
25#include "update_engine/topological_sort.h"
26#include "update_engine/update_metadata.pb.h"
27#include "update_engine/utils.h"
28
29using std::make_pair;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070030using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070031using std::min;
32using std::set;
33using std::string;
34using std::vector;
35
36namespace chromeos_update_engine {
37
38typedef DeltaDiffGenerator::Block Block;
39
40namespace {
41const size_t kBlockSize = 4096;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070042const uint64_t kVersionNumber = 1;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070043
44// Stores all Extents for a file into 'out'. Returns true on success.
45bool GatherExtents(const string& path,
46 google::protobuf::RepeatedPtrField<Extent>* out) {
47 vector<Extent> extents;
48 TEST_AND_RETURN_FALSE(extent_mapper::ExtentsForFileFibmap(path, &extents));
49 DeltaDiffGenerator::StoreExtents(extents, out);
50 return true;
51}
52
53// Runs the bsdiff tool on two files and returns the resulting delta in
54// 'out'. Returns true on success.
55bool BsdiffFiles(const string& old_file,
56 const string& new_file,
57 vector<char>* out) {
58 const string kPatchFile = "/tmp/delta.patchXXXXXX";
59 string patch_file_path;
60
61 TEST_AND_RETURN_FALSE(
62 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
63
64 vector<string> cmd;
65 cmd.push_back(kBsdiffPath);
66 cmd.push_back(old_file);
67 cmd.push_back(new_file);
68 cmd.push_back(patch_file_path);
69
70 int rc = 1;
71 vector<char> patch_file;
72 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc));
73 TEST_AND_RETURN_FALSE(rc == 0);
74 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
75 unlink(patch_file_path.c_str());
76 return true;
77}
78
79// The blocks vector contains a reader and writer for each block on the
80// filesystem that's being in-place updated. We populate the reader/writer
81// fields of blocks by calling this function.
82// For each block in 'operation' that is read or written, find that block
83// in 'blocks' and set the reader/writer field to the vertex passed.
84// 'graph' is not strictly necessary, but useful for printing out
85// error messages.
86bool AddInstallOpToBlocksVector(
87 const DeltaArchiveManifest_InstallOperation& operation,
88 vector<Block>* blocks,
89 const Graph& graph,
90 Vertex::Index vertex) {
91 LOG(INFO) << "AddInstallOpToBlocksVector(" << vertex << "), "
92 << graph[vertex].file_name;
93 // See if this is already present.
94 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
95
96 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
97 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
98 const int extents_size =
99 (field == READER) ? operation.src_extents_size() :
100 operation.dst_extents_size();
101 const char* past_participle = (field == READER) ? "read" : "written";
102 const google::protobuf::RepeatedPtrField<Extent>& extents =
103 (field == READER) ? operation.src_extents() : operation.dst_extents();
104 Vertex::Index Block::*access_type =
105 (field == READER) ? &Block::reader : &Block::writer;
106
107 for (int i = 0; i < extents_size; i++) {
108 const Extent& extent = extents.Get(i);
109 if (extent.start_block() == kSparseHole) {
110 // Hole in sparse file. skip
111 continue;
112 }
113 for (uint64_t block = extent.start_block();
114 block < (extent.start_block() + extent.num_blocks()); block++) {
115 LOG(INFO) << "ext: " << i << " block: " << block;
116 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
117 LOG(FATAL) << "Block " << block << " is already "
118 << past_participle << " by "
119 << (*blocks)[block].*access_type << "("
120 << graph[(*blocks)[block].*access_type].file_name
121 << ") and also " << vertex << "("
122 << graph[vertex].file_name << ")";
123 }
124 (*blocks)[block].*access_type = vertex;
125 }
126 }
127 }
128 return true;
129}
130
131// For a given regular file which must exist at new_root + path, and may
132// exist at old_root + path, creates a new InstallOperation and adds it to
133// the graph. Also, populates the 'blocks' array as necessary.
134// Also, writes the data necessary to send the file down to the client
135// into data_fd, which has length *data_file_size. *data_file_size is
136// updated appropriately.
137// Returns true on success.
138bool DeltaReadFile(Graph* graph,
139 vector<Block>* blocks,
140 const string& old_root,
141 const string& new_root,
142 const string& path, // within new_root
143 int data_fd,
144 off_t* data_file_size) {
145 vector<char> data;
146 DeltaArchiveManifest_InstallOperation operation;
147
148 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_root + path,
149 new_root + path,
150 &data,
151 &operation));
152
153 // Write the data
154 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
155 operation.set_data_offset(*data_file_size);
156 operation.set_data_length(data.size());
157 }
158
159 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
160 *data_file_size += data.size();
161
162 // Now, insert into graph and blocks vector
163 graph->resize(graph->size() + 1);
164 graph->back().op = operation;
165 CHECK(graph->back().op.has_type());
166 graph->back().file_name = path;
167
168 TEST_AND_RETURN_FALSE(AddInstallOpToBlocksVector(graph->back().op,
169 blocks,
170 *graph,
171 graph->size() - 1));
172 return true;
173}
174
175// For each regular file within new_root, creates a node in the graph,
176// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
177// and writes any necessary data to the end of data_fd.
178bool DeltaReadFiles(Graph* graph,
179 vector<Block>* blocks,
180 const string& old_root,
181 const string& new_root,
182 int data_fd,
183 off_t* data_file_size) {
184 set<ino_t> visited_inodes;
185 for (FilesystemIterator fs_iter(new_root,
186 utils::SetWithValue<string>("/lost+found"));
187 !fs_iter.IsEnd(); fs_iter.Increment()) {
188 if (!S_ISREG(fs_iter.GetStat().st_mode))
189 continue;
190
191 // Make sure we visit each inode only once.
192 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
193 continue;
194 visited_inodes.insert(fs_iter.GetStat().st_ino);
195 if (fs_iter.GetStat().st_size == 0)
196 continue;
197
198 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
199
200 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
201 blocks,
202 old_root,
203 new_root,
204 fs_iter.GetPartialPath(),
205 data_fd,
206 data_file_size));
207 }
208 return true;
209}
210
211// Attempts to find block_count blocks to use as scratch space.
212// Returns true on success.
213// Right now we return exactly as many blocks as are required.
214// TODO(adlr): consider returning all scratch blocks,
215// even if there are extras, to make it easier for a scratch allocator
216// to find contiguous regions for specific scratch writes.
217bool FindScratchSpace(const vector<Block>& blocks,
218 vector<Block>::size_type block_count,
219 vector<Extent>* out) {
220 // Scan blocks for blocks that are neither read nor written.
221 // If we don't find enough of those, return false.
222 // TODO(adlr): return blocks that are written by
223 // operations that don't have incoming edges (and thus, can be
224 // deferred until all old blocks are read by other operations).
225 vector<Extent> ret;
226 vector<Block>::size_type blocks_found = 0;
227 for (vector<Block>::size_type i = 0;
228 i < blocks.size() && blocks_found < block_count; i++) {
229 if (blocks[i].reader == Vertex::kInvalidIndex &&
230 blocks[i].writer == Vertex::kInvalidIndex) {
231 graph_utils::AppendBlockToExtents(&ret, i);
232 blocks_found++;
233 }
234 }
235 if (blocks_found == block_count) {
236 LOG(INFO) << "returning " << blocks_found << " scratch blocks";
237 out->swap(ret);
238 return true;
239 }
240 return false;
241}
242
243// This class takes a collection of Extents and allows the client to
244// allocate space from these extents. The client must not request more
245// space then exists in the source extents. Space is allocated from the
246// beginning of the source extents on; no consideration is paid to
247// fragmentation.
248class LinearExtentAllocator {
249 public:
250 explicit LinearExtentAllocator(const vector<Extent>& extents)
251 : extents_(extents),
252 extent_index_(0),
253 extent_blocks_allocated_(0) {}
254 vector<Extent> Allocate(const uint64_t block_count) {
255 vector<Extent> ret;
256 for (uint64_t blocks = 0; blocks < block_count; blocks++) {
257 CHECK_LT(extent_index_, extents_.size());
258 CHECK_LT(extent_blocks_allocated_, extents_[extent_index_].num_blocks());
259 graph_utils::AppendBlockToExtents(
260 &ret,
261 extents_[extent_index_].start_block() + extent_blocks_allocated_);
262 extent_blocks_allocated_++;
263 if (extent_blocks_allocated_ >= extents_[extent_index_].num_blocks()) {
264 extent_blocks_allocated_ = 0;
265 extent_index_++;
266 }
267 }
268 return ret;
269 }
270 private:
271 const vector<Extent> extents_;
272 vector<Extent>::size_type extent_index_; // current Extent
273 // number of blocks allocated from the current extent
274 uint64_t extent_blocks_allocated_;
275};
276
277// Reads blocks from image_path that are not yet marked as being written
278// in the blocks array. These blocks that remain are non-file-data blocks.
279// In the future we might consider intelligent diffing between this data
280// and data in the previous image, but for now we just bzip2 compress it
281// and include it in the update.
282// Creates a new node in the graph to write these blocks and writes the
283// appropriate blob to blobs_fd. Reads and updates blobs_length;
284bool ReadUnwrittenBlocks(const vector<Block>& blocks,
285 int blobs_fd,
286 off_t* blobs_length,
287 const string& image_path,
288 DeltaArchiveManifest_InstallOperation* out_op) {
289 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
290 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
291 ScopedFdCloser image_fd_closer(&image_fd);
292
293 string temp_file_path;
294 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX",
295 &temp_file_path,
296 NULL));
297
298 FILE* file = fopen(temp_file_path.c_str(), "w");
299 TEST_AND_RETURN_FALSE(file);
300 int err = BZ_OK;
301
302 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
303 file,
304 9, // max compression
305 0, // verbosity
306 0); // default work factor
307 TEST_AND_RETURN_FALSE(err == BZ_OK);
308
309 vector<Extent> extents;
310 vector<Block>::size_type block_count = 0;
311
312 LOG(INFO) << "Appending left over blocks to extents";
313 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
314 if (blocks[i].writer != Vertex::kInvalidIndex)
315 continue;
316 graph_utils::AppendBlockToExtents(&extents, i);
317 block_count++;
318 }
319
320 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
321 // so we arbitrarily set it to 1024 * kBlockSize.
322 vector<char> buf(1024 * kBlockSize);
323
324 LOG(INFO) << "Reading left over blocks";
325 vector<Block>::size_type blocks_copied_count = 0;
326
327 // For each extent in extents, write the data into BZ2_bzWrite which
328 // sends it to an output file.
329 // We use the temporary buffer 'buf' to hold the data, which may be
330 // smaller than the extent, so in that case we have to loop to get
331 // the extent's data (that's the inner while loop).
332 for (vector<Extent>::const_iterator it = extents.begin();
333 it != extents.end(); ++it) {
334 vector<Block>::size_type blocks_read = 0;
335 while (blocks_read < it->num_blocks()) {
336 const int copy_block_cnt =
337 min(buf.size() / kBlockSize,
338 static_cast<vector<char>::size_type>(
339 it->num_blocks() - blocks_read));
340 ssize_t rc = pread(image_fd,
341 &buf[0],
342 copy_block_cnt * kBlockSize,
343 (it->start_block() + blocks_read) * kBlockSize);
344 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
345 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
346 copy_block_cnt * kBlockSize);
347 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
348 TEST_AND_RETURN_FALSE(err == BZ_OK);
349 blocks_read += copy_block_cnt;
350 blocks_copied_count += copy_block_cnt;
351 LOG(INFO) << "progress: " << ((float)blocks_copied_count)/block_count;
352 }
353 }
354 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
355 TEST_AND_RETURN_FALSE(err == BZ_OK);
356 bz_file = NULL;
357 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
358 file = NULL;
359
360 vector<char> compressed_data;
361 LOG(INFO) << "Reading compressed data off disk";
362 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
363 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
364
365 // Add node to graph to write these blocks
366 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
367 out_op->set_data_offset(*blobs_length);
368 out_op->set_data_length(compressed_data.size());
369 *blobs_length += compressed_data.size();
370 out_op->set_dst_length(kBlockSize * block_count);
371 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
372
373 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
374 &compressed_data[0],
375 compressed_data.size()));
376 LOG(INFO) << "done with extra blocks";
377 return true;
378}
379
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700380// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700381// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700382bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
383 uint64_t value_be = htobe64(value);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700384 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)) ==
385 sizeof(value_be));
386 return true;
387}
388
389// Adds each operation from the graph to the manifest in the order
390// specified by 'order'.
391void InstallOperationsToManifest(
392 const Graph& graph,
393 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700394 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700395 DeltaArchiveManifest* out_manifest) {
396 for (vector<Vertex::Index>::const_iterator it = order.begin();
397 it != order.end(); ++it) {
398 DeltaArchiveManifest_InstallOperation* op =
399 out_manifest->add_install_operations();
400 *op = graph[*it].op;
401 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700402 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
403 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
404 DeltaArchiveManifest_InstallOperation* op =
405 out_manifest->add_kernel_install_operations();
406 *op = *it;
407 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700408}
409
410void CheckGraph(const Graph& graph) {
411 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
412 CHECK(it->op.has_type());
413 }
414}
415
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700416// Delta compresses a kernel partition new_kernel_part with knowledge of
417// the old kernel partition old_kernel_part.
418bool DeltaCompressKernelPartition(
419 const string& old_kernel_part,
420 const string& new_kernel_part,
421 vector<DeltaArchiveManifest_InstallOperation>* ops,
422 int blobs_fd,
423 off_t* blobs_length) {
424 // For now, just bsdiff the kernel partition as a whole.
425 // TODO(adlr): Use knowledge of how the kernel partition is laid out
426 // to more efficiently compress it.
427
428 LOG(INFO) << "Delta compressing kernel partition...";
429
430 // Add a new install operation
431 ops->resize(1);
432 DeltaArchiveManifest_InstallOperation* op = &(*ops)[0];
433 op->set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
434 op->set_data_offset(*blobs_length);
435
436 // Do the actual compression
437 vector<char> data;
438 TEST_AND_RETURN_FALSE(BsdiffFiles(old_kernel_part, new_kernel_part, &data));
439 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
440 *blobs_length += data.size();
441
442 off_t old_part_size = utils::FileSize(old_kernel_part);
443 TEST_AND_RETURN_FALSE(old_part_size >= 0);
444 off_t new_part_size = utils::FileSize(new_kernel_part);
445 TEST_AND_RETURN_FALSE(new_part_size >= 0);
446
447 op->set_data_length(data.size());
448
449 op->set_src_length(old_part_size);
450 op->set_dst_length(new_part_size);
451
452 // Theres a single src/dest extent for each
453 Extent* src_extent = op->add_src_extents();
454 src_extent->set_start_block(0);
455 src_extent->set_num_blocks((old_part_size + kBlockSize - 1) / kBlockSize);
456
457 Extent* dst_extent = op->add_dst_extents();
458 dst_extent->set_start_block(0);
459 dst_extent->set_num_blocks((new_part_size + kBlockSize - 1) / kBlockSize);
460
461 LOG(INFO) << "Done delta compressing kernel partition.";
462 return true;
463}
464
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700465} // namespace {}
466
467bool DeltaDiffGenerator::ReadFileToDiff(
468 const string& old_filename,
469 const string& new_filename,
470 vector<char>* out_data,
471 DeltaArchiveManifest_InstallOperation* out_op) {
472 // Read new data in
473 vector<char> new_data;
474 TEST_AND_RETURN_FALSE(utils::ReadFile(new_filename, &new_data));
475
476 TEST_AND_RETURN_FALSE(!new_data.empty());
477
478 vector<char> new_data_bz;
479 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
480 CHECK(!new_data_bz.empty());
481
482 vector<char> data; // Data blob that will be written to delta file.
483
484 DeltaArchiveManifest_InstallOperation operation;
485 size_t current_best_size = 0;
486 if (new_data.size() <= new_data_bz.size()) {
487 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
488 current_best_size = new_data.size();
489 data = new_data;
490 } else {
491 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
492 current_best_size = new_data_bz.size();
493 data = new_data_bz;
494 }
495
496 // Do we have an original file to consider?
497 struct stat old_stbuf;
498 if (0 != stat(old_filename.c_str(), &old_stbuf)) {
499 // If stat-ing the old file fails, it should be because it doesn't exist.
500 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
501 } else {
502 // Read old data
503 vector<char> old_data;
504 TEST_AND_RETURN_FALSE(utils::ReadFile(old_filename, &old_data));
505 if (old_data == new_data) {
506 // No change in data.
507 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
508 current_best_size = 0;
509 data.clear();
510 } else {
511 // Try bsdiff of old to new data
512 vector<char> bsdiff_delta;
513 TEST_AND_RETURN_FALSE(
514 BsdiffFiles(old_filename, new_filename, &bsdiff_delta));
515 CHECK_GT(bsdiff_delta.size(), 0);
516 if (bsdiff_delta.size() < current_best_size) {
517 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
518 current_best_size = bsdiff_delta.size();
519
520 data = bsdiff_delta;
521 }
522 }
523 }
524
525 // Set parameters of the operations
526 CHECK_EQ(data.size(), current_best_size);
527
528 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
529 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
530 TEST_AND_RETURN_FALSE(
531 GatherExtents(old_filename, operation.mutable_src_extents()));
532 operation.set_src_length(old_stbuf.st_size);
533 }
534
535 TEST_AND_RETURN_FALSE(
536 GatherExtents(new_filename, operation.mutable_dst_extents()));
537 operation.set_dst_length(new_data.size());
538
539 out_data->swap(data);
540 *out_op = operation;
541
542 return true;
543}
544
545void DeltaDiffGenerator::SubstituteBlocks(
546 DeltaArchiveManifest_InstallOperation* op,
547 const vector<Extent>& remove_extents,
548 const vector<Extent>& replace_extents) {
549 // First, expand out the blocks that op reads from
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700550 vector<uint64_t> read_blocks;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700551 for (int i = 0; i < op->src_extents_size(); i++) {
552 const Extent& extent = op->src_extents(i);
553 if (extent.start_block() == kSparseHole) {
554 read_blocks.resize(read_blocks.size() + extent.num_blocks(), kSparseHole);
555 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700556 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700557 block < (extent.start_block() + extent.num_blocks()); block++) {
558 read_blocks.push_back(block);
559 }
560 }
561 }
562 {
563 // Expand remove_extents and replace_extents
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700564 vector<uint64_t> remove_extents_expanded;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700565 for (vector<Extent>::const_iterator it = remove_extents.begin();
566 it != remove_extents.end(); ++it) {
567 const Extent& extent = *it;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700568 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700569 block < (extent.start_block() + extent.num_blocks()); block++) {
570 remove_extents_expanded.push_back(block);
571 }
572 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700573 vector<uint64_t> replace_extents_expanded;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700574 for (vector<Extent>::const_iterator it = replace_extents.begin();
575 it != replace_extents.end(); ++it) {
576 const Extent& extent = *it;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700577 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700578 block < (extent.start_block() + extent.num_blocks()); block++) {
579 replace_extents_expanded.push_back(block);
580 }
581 }
582 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700583 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700584 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700585 vector<uint64_t>::size_type index = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700586 CHECK(utils::VectorIndexOf(read_blocks,
587 remove_extents_expanded[i],
588 &index));
589 CHECK(read_blocks[index] == remove_extents_expanded[i]);
590 read_blocks[index] = replace_extents_expanded[i];
591 }
592 }
593 // Convert read_blocks back to extents
594 op->clear_src_extents();
595 vector<Extent> new_extents;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700596 for (vector<uint64_t>::const_iterator it = read_blocks.begin();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700597 it != read_blocks.end(); ++it) {
598 graph_utils::AppendBlockToExtents(&new_extents, *it);
599 }
600 DeltaDiffGenerator::StoreExtents(new_extents, op->mutable_src_extents());
601}
602
603bool DeltaDiffGenerator::CutEdges(Graph* graph,
604 const vector<Block>& blocks,
605 const set<Edge>& edges) {
606 // First, find enough scratch space for the edges we'll be cutting.
607 vector<Block>::size_type blocks_required = 0;
608 for (set<Edge>::const_iterator it = edges.begin(); it != edges.end(); ++it) {
609 blocks_required += graph_utils::EdgeWeight(*graph, *it);
610 }
611 vector<Extent> scratch_extents;
612 LOG(INFO) << "requesting " << blocks_required << " blocks of scratch";
613 TEST_AND_RETURN_FALSE(
614 FindScratchSpace(blocks, blocks_required, &scratch_extents));
615 LinearExtentAllocator scratch_allocator(scratch_extents);
616
617 uint64_t scratch_blocks_used = 0;
618 for (set<Edge>::const_iterator it = edges.begin();
619 it != edges.end(); ++it) {
620 vector<Extent> old_extents =
621 (*graph)[it->first].out_edges[it->second].extents;
622 // Choose some scratch space
623 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
624 LOG(INFO) << "using " << graph_utils::EdgeWeight(*graph, *it)
625 << " scratch blocks ("
626 << scratch_blocks_used << ")";
627 vector<Extent> scratch =
628 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
629 // create vertex to copy original->scratch
630 graph->resize(graph->size() + 1);
631
632 // make node depend on the copy operation
633 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
634 EdgeProperties()));
635
636 // Set src/dst extents and other proto variables for copy operation
637 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
638 DeltaDiffGenerator::StoreExtents(
639 (*graph)[it->first].out_edges[it->second].extents,
640 graph->back().op.mutable_src_extents());
641 DeltaDiffGenerator::StoreExtents(scratch,
642 graph->back().op.mutable_dst_extents());
643 graph->back().op.set_src_length(
644 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
645 graph->back().op.set_dst_length(graph->back().op.src_length());
646
647 // make the dest node read from the scratch space
648 DeltaDiffGenerator::SubstituteBlocks(
649 &((*graph)[it->second].op),
650 (*graph)[it->first].out_edges[it->second].extents,
651 scratch);
652
653 // delete the old edge
654 CHECK_EQ(1, (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700655
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700656 // Add an edge from dst to copy operation
657 (*graph)[it->second].out_edges.insert(make_pair(graph->size() - 1,
658 EdgeProperties()));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700659 }
660 return true;
661}
662
663// Stores all Extents in 'extents' into 'out'.
664void DeltaDiffGenerator::StoreExtents(
665 vector<Extent>& extents,
666 google::protobuf::RepeatedPtrField<Extent>* out) {
667 for (vector<Extent>::const_iterator it = extents.begin();
668 it != extents.end(); ++it) {
669 Extent* new_extent = out->Add();
670 *new_extent = *it;
671 }
672}
673
674// Creates all the edges for the graph. Writers of a block point to
675// readers of the same block. This is because for an edge A->B, B
676// must complete before A executes.
677void DeltaDiffGenerator::CreateEdges(Graph* graph,
678 const vector<Block>& blocks) {
679 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
680 // Blocks with both a reader and writer get an edge
681 if (blocks[i].reader == Vertex::kInvalidIndex ||
682 blocks[i].writer == Vertex::kInvalidIndex)
683 continue;
684 // Don't have a node depend on itself
685 if (blocks[i].reader == blocks[i].writer)
686 continue;
687 // See if there's already an edge we can add onto
688 Vertex::EdgeMap::iterator edge_it =
689 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
690 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
691 // No existing edge. Create one
692 (*graph)[blocks[i].writer].out_edges.insert(
693 make_pair(blocks[i].reader, EdgeProperties()));
694 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700695 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700696 }
697 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
698 }
699}
700
701bool DeltaDiffGenerator::ReorderDataBlobs(
702 DeltaArchiveManifest* manifest,
703 const std::string& data_blobs_path,
704 const std::string& new_data_blobs_path) {
705 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
706 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
707 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -0700708
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700709 DirectFileWriter writer;
710 TEST_AND_RETURN_FALSE(
711 writer.Open(new_data_blobs_path.c_str(),
712 O_WRONLY | O_TRUNC | O_CREAT,
713 0644) == 0);
714 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700715 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -0700716
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700717 for (int i = 0; i < (manifest->install_operations_size() +
718 manifest->kernel_install_operations_size()); i++) {
719 DeltaArchiveManifest_InstallOperation* op = NULL;
720 if (i < manifest->install_operations_size()) {
721 op = manifest->mutable_install_operations(i);
722 } else {
723 op = manifest->mutable_kernel_install_operations(
724 i - manifest->install_operations_size());
725 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700726 if (!op->has_data_offset())
727 continue;
728 CHECK(op->has_data_length());
729 vector<char> buf(op->data_length());
730 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
731 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
732
733 op->set_data_offset(out_file_size);
734 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()) ==
735 static_cast<ssize_t>(buf.size()));
736 out_file_size += buf.size();
737 }
738 return true;
739}
740
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700741bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
742 const string& old_root,
743 const string& old_image,
744 const string& new_root,
745 const string& new_image,
746 const std::string& old_kernel_part,
747 const std::string& new_kernel_part,
748 const string& output_path) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700749 struct stat old_image_stbuf;
750 TEST_AND_RETURN_FALSE_ERRNO(stat(old_image.c_str(), &old_image_stbuf) == 0);
751 struct stat new_image_stbuf;
752 TEST_AND_RETURN_FALSE_ERRNO(stat(new_image.c_str(), &new_image_stbuf) == 0);
753 LOG_IF(WARNING, new_image_stbuf.st_size != old_image_stbuf.st_size)
754 << "Old and new images are different sizes.";
755 LOG_IF(FATAL, new_image_stbuf.st_size % kBlockSize)
756 << "New image not a multiple of block size " << kBlockSize;
757 LOG_IF(FATAL, old_image_stbuf.st_size % kBlockSize)
758 << "Old image not a multiple of block size " << kBlockSize;
759
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700760 // Sanity check kernel partition args
761 TEST_AND_RETURN_FALSE(utils::FileSize(old_kernel_part) >= 0);
762 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
763
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700764 vector<Block> blocks(max(old_image_stbuf.st_size / kBlockSize,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700765 new_image_stbuf.st_size / kBlockSize));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700766 LOG(INFO) << "invalid: " << Vertex::kInvalidIndex;
767 LOG(INFO) << "len: " << blocks.size();
768 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
769 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
770 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
771 }
772 Graph graph;
773 CheckGraph(graph);
774
775 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
776 string temp_file_path;
777 off_t data_file_size = 0;
778
779 LOG(INFO) << "Reading files...";
780
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700781 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
782
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700783 DeltaArchiveManifest_InstallOperation final_op;
784 {
785 int fd;
786 TEST_AND_RETURN_FALSE(
787 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
788 TEST_AND_RETURN_FALSE(fd >= 0);
789 ScopedFdCloser fd_closer(&fd);
790
791 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
792 &blocks,
793 old_root,
794 new_root,
795 fd,
796 &data_file_size));
797 CheckGraph(graph);
798
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700799 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
800 fd,
801 &data_file_size,
802 new_image,
803 &final_op));
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700804
805 // Read kernel partition
806 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
807 new_kernel_part,
808 &kernel_ops,
809 fd,
810 &data_file_size));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700811 }
812 CheckGraph(graph);
813
814 LOG(INFO) << "Creating edges...";
815 CreateEdges(&graph, blocks);
816 CheckGraph(graph);
817
818 CycleBreaker cycle_breaker;
819 LOG(INFO) << "Finding cycles...";
820 set<Edge> cut_edges;
821 cycle_breaker.BreakCycles(graph, &cut_edges);
822 CheckGraph(graph);
823
824 // Calculate number of scratch blocks needed
825
826 LOG(INFO) << "Cutting cycles...";
827 TEST_AND_RETURN_FALSE(CutEdges(&graph, blocks, cut_edges));
828 CheckGraph(graph);
829
830 vector<Vertex::Index> final_order;
831 LOG(INFO) << "Ordering...";
832 TopologicalSort(graph, &final_order);
833 CheckGraph(graph);
834
835 // Convert to protobuf Manifest object
836 DeltaArchiveManifest manifest;
837 CheckGraph(graph);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700838 InstallOperationsToManifest(graph, final_order, kernel_ops, &manifest);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700839 {
840 // Write final operation
841 DeltaArchiveManifest_InstallOperation* op =
842 manifest.add_install_operations();
843 *op = final_op;
844 CHECK(op->has_type());
845 LOG(INFO) << "final op length: " << op->data_length();
846 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700847
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700848 CheckGraph(graph);
849 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700850
851 // Reorder the data blobs with the newly ordered manifest
852 string ordered_blobs_path;
853 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
854 "/tmp/CrAU_temp_data.ordered.XXXXXX",
855 &ordered_blobs_path,
856 false));
857 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
858 temp_file_path,
859 ordered_blobs_path));
860
861 // Check that install op blobs are in order and that all blocks are written.
862 {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700863 vector<uint32_t> written_count(blocks.size(), 0);
864 uint64_t next_blob_offset = 0;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700865 for (int i = 0; i < (manifest.install_operations_size() +
866 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700867 const DeltaArchiveManifest_InstallOperation& op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700868 i < manifest.install_operations_size() ?
869 manifest.install_operations(i) :
870 manifest.kernel_install_operations(
871 i - manifest.install_operations_size());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700872 for (int j = 0; j < op.dst_extents_size(); j++) {
873 const Extent& extent = op.dst_extents(j);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700874 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700875 block < (extent.start_block() + extent.num_blocks()); block++) {
876 written_count[block]++;
877 }
878 }
879 if (op.has_data_offset()) {
880 if (op.data_offset() != next_blob_offset) {
881 LOG(FATAL) << "bad blob offset! " << op.data_offset() << " != "
882 << next_blob_offset;
883 }
884 next_blob_offset += op.data_length();
885 }
886 }
887 // check all blocks written to
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700888 for (vector<uint32_t>::size_type i = 0; i < written_count.size(); i++) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700889 if (written_count[i] == 0) {
890 LOG(FATAL) << "block " << i << " not written!";
891 }
892 }
893 }
894
895 // Serialize protobuf
896 string serialized_manifest;
897
898 CheckGraph(graph);
899 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
900 CheckGraph(graph);
901
902 LOG(INFO) << "Writing final delta file header...";
903 DirectFileWriter writer;
904 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
905 O_WRONLY | O_CREAT | O_TRUNC,
906 0644) == 0);
907 ScopedFileWriterCloser writer_closer(&writer);
908
909 // Write header
910 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)) ==
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700911 static_cast<ssize_t>(strlen(kDeltaMagic)));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700912
913 // Write version number
914 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
915
916 // Write protobuf length
917 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
918 serialized_manifest.size()));
919
920 // Write protobuf
921 LOG(INFO) << "Writing final delta file protobuf... "
922 << serialized_manifest.size();
923 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
924 serialized_manifest.size()) ==
925 static_cast<ssize_t>(serialized_manifest.size()));
926
927 // Append the data blobs
928 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700929 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700930 ScopedFdCloser blobs_fd_closer(&blobs_fd);
931 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
932 for (;;) {
933 char buf[kBlockSize];
934 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
935 if (0 == rc) {
936 // EOF
937 break;
938 }
939 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
940 TEST_AND_RETURN_FALSE(writer.Write(buf, rc) == rc);
941 }
942
943 LOG(INFO) << "All done. Successfully created delta file.";
944 return true;
945}
946
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700947const char* const kBsdiffPath = "/usr/bin/bsdiff";
948const char* const kBspatchPath = "/usr/bin/bspatch";
949const char* const kDeltaMagic = "CrAU";
950
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700951}; // namespace chromeos_update_engine