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