blob: b38b495a51f5ecf0f77973b3577a5f0bad6128b2 [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"
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"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070024#include "update_engine/payload_signer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070025#include "update_engine/subprocess.h"
26#include "update_engine/topological_sort.h"
27#include "update_engine/update_metadata.pb.h"
28#include "update_engine/utils.h"
29
30using std::make_pair;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070031using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070032using std::min;
33using std::set;
34using std::string;
35using std::vector;
36
37namespace chromeos_update_engine {
38
39typedef DeltaDiffGenerator::Block Block;
40
41namespace {
42const size_t kBlockSize = 4096;
Darin Petkovc0b7a532010-09-29 15:18:14 -070043const size_t kRootFSPartitionSize = 1 * 1024 * 1024 * 1024; // 1 GiB
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070044const uint64_t kVersionNumber = 1;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070045
46// Stores all Extents for a file into 'out'. Returns true on success.
47bool GatherExtents(const string& path,
48 google::protobuf::RepeatedPtrField<Extent>* out) {
49 vector<Extent> extents;
50 TEST_AND_RETURN_FALSE(extent_mapper::ExtentsForFileFibmap(path, &extents));
51 DeltaDiffGenerator::StoreExtents(extents, out);
52 return true;
53}
54
55// Runs the bsdiff tool on two files and returns the resulting delta in
56// 'out'. Returns true on success.
57bool BsdiffFiles(const string& old_file,
58 const string& new_file,
59 vector<char>* out) {
60 const string kPatchFile = "/tmp/delta.patchXXXXXX";
61 string patch_file_path;
62
63 TEST_AND_RETURN_FALSE(
64 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
65
66 vector<string> cmd;
67 cmd.push_back(kBsdiffPath);
68 cmd.push_back(old_file);
69 cmd.push_back(new_file);
70 cmd.push_back(patch_file_path);
71
72 int rc = 1;
73 vector<char> patch_file;
74 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc));
75 TEST_AND_RETURN_FALSE(rc == 0);
76 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
77 unlink(patch_file_path.c_str());
78 return true;
79}
80
81// The blocks vector contains a reader and writer for each block on the
82// filesystem that's being in-place updated. We populate the reader/writer
83// fields of blocks by calling this function.
84// For each block in 'operation' that is read or written, find that block
85// in 'blocks' and set the reader/writer field to the vertex passed.
86// 'graph' is not strictly necessary, but useful for printing out
87// error messages.
88bool AddInstallOpToBlocksVector(
89 const DeltaArchiveManifest_InstallOperation& operation,
90 vector<Block>* blocks,
91 const Graph& graph,
92 Vertex::Index vertex) {
93 LOG(INFO) << "AddInstallOpToBlocksVector(" << vertex << "), "
94 << graph[vertex].file_name;
95 // See if this is already present.
96 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070097
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070098 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
99 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
100 const int extents_size =
101 (field == READER) ? operation.src_extents_size() :
102 operation.dst_extents_size();
103 const char* past_participle = (field == READER) ? "read" : "written";
104 const google::protobuf::RepeatedPtrField<Extent>& extents =
105 (field == READER) ? operation.src_extents() : operation.dst_extents();
106 Vertex::Index Block::*access_type =
107 (field == READER) ? &Block::reader : &Block::writer;
108
109 for (int i = 0; i < extents_size; i++) {
110 const Extent& extent = extents.Get(i);
111 if (extent.start_block() == kSparseHole) {
112 // Hole in sparse file. skip
113 continue;
114 }
115 for (uint64_t block = extent.start_block();
116 block < (extent.start_block() + extent.num_blocks()); block++) {
117 LOG(INFO) << "ext: " << i << " block: " << block;
118 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
119 LOG(FATAL) << "Block " << block << " is already "
120 << past_participle << " by "
121 << (*blocks)[block].*access_type << "("
122 << graph[(*blocks)[block].*access_type].file_name
123 << ") and also " << vertex << "("
124 << graph[vertex].file_name << ")";
125 }
126 (*blocks)[block].*access_type = vertex;
127 }
128 }
129 }
130 return true;
131}
132
133// For a given regular file which must exist at new_root + path, and may
134// exist at old_root + path, creates a new InstallOperation and adds it to
135// the graph. Also, populates the 'blocks' array as necessary.
136// Also, writes the data necessary to send the file down to the client
137// into data_fd, which has length *data_file_size. *data_file_size is
138// updated appropriately.
139// Returns true on success.
140bool DeltaReadFile(Graph* graph,
141 vector<Block>* blocks,
142 const string& old_root,
143 const string& new_root,
144 const string& path, // within new_root
145 int data_fd,
146 off_t* data_file_size) {
147 vector<char> data;
148 DeltaArchiveManifest_InstallOperation operation;
149
150 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_root + path,
151 new_root + path,
152 &data,
153 &operation));
154
155 // Write the data
156 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
157 operation.set_data_offset(*data_file_size);
158 operation.set_data_length(data.size());
159 }
160
161 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
162 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700163
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700164 // Now, insert into graph and blocks vector
165 graph->resize(graph->size() + 1);
166 graph->back().op = operation;
167 CHECK(graph->back().op.has_type());
168 graph->back().file_name = path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700169
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700170 TEST_AND_RETURN_FALSE(AddInstallOpToBlocksVector(graph->back().op,
171 blocks,
172 *graph,
173 graph->size() - 1));
174 return true;
175}
176
177// For each regular file within new_root, creates a node in the graph,
178// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
179// and writes any necessary data to the end of data_fd.
180bool DeltaReadFiles(Graph* graph,
181 vector<Block>* blocks,
182 const string& old_root,
183 const string& new_root,
184 int data_fd,
185 off_t* data_file_size) {
186 set<ino_t> visited_inodes;
187 for (FilesystemIterator fs_iter(new_root,
188 utils::SetWithValue<string>("/lost+found"));
189 !fs_iter.IsEnd(); fs_iter.Increment()) {
190 if (!S_ISREG(fs_iter.GetStat().st_mode))
191 continue;
192
193 // Make sure we visit each inode only once.
194 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
195 continue;
196 visited_inodes.insert(fs_iter.GetStat().st_ino);
197 if (fs_iter.GetStat().st_size == 0)
198 continue;
199
200 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700201
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700202 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
203 blocks,
204 old_root,
205 new_root,
206 fs_iter.GetPartialPath(),
207 data_fd,
208 data_file_size));
209 }
210 return true;
211}
212
Darin Petkovc0b7a532010-09-29 15:18:14 -0700213// Attempts to find |block_count| blocks to use as scratch space. Returns true
214// on success. Right now we return exactly as many blocks as are required.
215//
216// TODO(adlr): Consider returning all scratch blocks, even if there are extras,
217// to make it easier for a scratch allocator to find contiguous regions for
218// specific scratch writes.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700219bool FindScratchSpace(const vector<Block>& blocks,
220 vector<Block>::size_type block_count,
221 vector<Extent>* out) {
Darin Petkovc0b7a532010-09-29 15:18:14 -0700222 // Scan |blocks| for blocks that are neither read, nor written. If we don't
223 // find enough of those, look past the end of |blocks| till the end of the
224 // partition. If we don't find |block_count| scratch blocks, return false.
225 //
226 // TODO(adlr): Return blocks that are written by operations that don't have
227 // incoming edges (and thus, can be deferred until all old blocks are read by
228 // other operations).
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700229 vector<Extent> ret;
230 vector<Block>::size_type blocks_found = 0;
Darin Petkovc0b7a532010-09-29 15:18:14 -0700231 const size_t kPartitionBlocks = kRootFSPartitionSize / kBlockSize;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700232 for (vector<Block>::size_type i = 0;
Darin Petkovc0b7a532010-09-29 15:18:14 -0700233 i < kPartitionBlocks && blocks_found < block_count; i++) {
234 if (i >= blocks.size() ||
235 (blocks[i].reader == Vertex::kInvalidIndex &&
236 blocks[i].writer == Vertex::kInvalidIndex)) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700237 graph_utils::AppendBlockToExtents(&ret, i);
238 blocks_found++;
239 }
240 }
Darin Petkovc0b7a532010-09-29 15:18:14 -0700241 LOG(INFO) << "found " << blocks_found << " scratch blocks";
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700242 if (blocks_found == block_count) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700243 out->swap(ret);
244 return true;
245 }
246 return false;
247}
248
249// This class takes a collection of Extents and allows the client to
250// allocate space from these extents. The client must not request more
251// space then exists in the source extents. Space is allocated from the
252// beginning of the source extents on; no consideration is paid to
253// fragmentation.
254class LinearExtentAllocator {
255 public:
256 explicit LinearExtentAllocator(const vector<Extent>& extents)
257 : extents_(extents),
258 extent_index_(0),
259 extent_blocks_allocated_(0) {}
260 vector<Extent> Allocate(const uint64_t block_count) {
261 vector<Extent> ret;
262 for (uint64_t blocks = 0; blocks < block_count; blocks++) {
263 CHECK_LT(extent_index_, extents_.size());
264 CHECK_LT(extent_blocks_allocated_, extents_[extent_index_].num_blocks());
265 graph_utils::AppendBlockToExtents(
266 &ret,
267 extents_[extent_index_].start_block() + extent_blocks_allocated_);
268 extent_blocks_allocated_++;
269 if (extent_blocks_allocated_ >= extents_[extent_index_].num_blocks()) {
270 extent_blocks_allocated_ = 0;
271 extent_index_++;
272 }
273 }
274 return ret;
275 }
276 private:
277 const vector<Extent> extents_;
278 vector<Extent>::size_type extent_index_; // current Extent
279 // number of blocks allocated from the current extent
280 uint64_t extent_blocks_allocated_;
281};
282
283// Reads blocks from image_path that are not yet marked as being written
284// in the blocks array. These blocks that remain are non-file-data blocks.
285// In the future we might consider intelligent diffing between this data
286// and data in the previous image, but for now we just bzip2 compress it
287// and include it in the update.
288// Creates a new node in the graph to write these blocks and writes the
289// appropriate blob to blobs_fd. Reads and updates blobs_length;
290bool ReadUnwrittenBlocks(const vector<Block>& blocks,
291 int blobs_fd,
292 off_t* blobs_length,
293 const string& image_path,
294 DeltaArchiveManifest_InstallOperation* out_op) {
295 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
296 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
297 ScopedFdCloser image_fd_closer(&image_fd);
298
299 string temp_file_path;
300 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX",
301 &temp_file_path,
302 NULL));
303
304 FILE* file = fopen(temp_file_path.c_str(), "w");
305 TEST_AND_RETURN_FALSE(file);
306 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700307
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700308 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
309 file,
310 9, // max compression
311 0, // verbosity
312 0); // default work factor
313 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700314
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700315 vector<Extent> extents;
316 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700317
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700318 LOG(INFO) << "Appending left over blocks to extents";
319 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
320 if (blocks[i].writer != Vertex::kInvalidIndex)
321 continue;
322 graph_utils::AppendBlockToExtents(&extents, i);
323 block_count++;
324 }
325
326 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
327 // so we arbitrarily set it to 1024 * kBlockSize.
328 vector<char> buf(1024 * kBlockSize);
329
330 LOG(INFO) << "Reading left over blocks";
331 vector<Block>::size_type blocks_copied_count = 0;
332
333 // For each extent in extents, write the data into BZ2_bzWrite which
334 // sends it to an output file.
335 // We use the temporary buffer 'buf' to hold the data, which may be
336 // smaller than the extent, so in that case we have to loop to get
337 // the extent's data (that's the inner while loop).
338 for (vector<Extent>::const_iterator it = extents.begin();
339 it != extents.end(); ++it) {
340 vector<Block>::size_type blocks_read = 0;
341 while (blocks_read < it->num_blocks()) {
342 const int copy_block_cnt =
343 min(buf.size() / kBlockSize,
344 static_cast<vector<char>::size_type>(
345 it->num_blocks() - blocks_read));
346 ssize_t rc = pread(image_fd,
347 &buf[0],
348 copy_block_cnt * kBlockSize,
349 (it->start_block() + blocks_read) * kBlockSize);
350 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
351 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
352 copy_block_cnt * kBlockSize);
353 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
354 TEST_AND_RETURN_FALSE(err == BZ_OK);
355 blocks_read += copy_block_cnt;
356 blocks_copied_count += copy_block_cnt;
357 LOG(INFO) << "progress: " << ((float)blocks_copied_count)/block_count;
358 }
359 }
360 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
361 TEST_AND_RETURN_FALSE(err == BZ_OK);
362 bz_file = NULL;
363 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
364 file = NULL;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700365
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700366 vector<char> compressed_data;
367 LOG(INFO) << "Reading compressed data off disk";
368 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
369 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700370
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700371 // Add node to graph to write these blocks
372 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
373 out_op->set_data_offset(*blobs_length);
374 out_op->set_data_length(compressed_data.size());
375 *blobs_length += compressed_data.size();
376 out_op->set_dst_length(kBlockSize * block_count);
377 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700378
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700379 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
380 &compressed_data[0],
381 compressed_data.size()));
382 LOG(INFO) << "done with extra blocks";
383 return true;
384}
385
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700386// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700387// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700388bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
389 uint64_t value_be = htobe64(value);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700390 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)) ==
391 sizeof(value_be));
392 return true;
393}
394
395// Adds each operation from the graph to the manifest in the order
396// specified by 'order'.
397void InstallOperationsToManifest(
398 const Graph& graph,
399 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700400 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700401 DeltaArchiveManifest* out_manifest) {
402 for (vector<Vertex::Index>::const_iterator it = order.begin();
403 it != order.end(); ++it) {
404 DeltaArchiveManifest_InstallOperation* op =
405 out_manifest->add_install_operations();
406 *op = graph[*it].op;
407 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700408 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
409 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
410 DeltaArchiveManifest_InstallOperation* op =
411 out_manifest->add_kernel_install_operations();
412 *op = *it;
413 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700414}
415
416void CheckGraph(const Graph& graph) {
417 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
418 CHECK(it->op.has_type());
419 }
420}
421
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700422// Delta compresses a kernel partition new_kernel_part with knowledge of
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700423// the old kernel partition old_kernel_part.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700424bool DeltaCompressKernelPartition(
425 const string& old_kernel_part,
426 const string& new_kernel_part,
427 vector<DeltaArchiveManifest_InstallOperation>* ops,
428 int blobs_fd,
429 off_t* blobs_length) {
430 // For now, just bsdiff the kernel partition as a whole.
431 // TODO(adlr): Use knowledge of how the kernel partition is laid out
432 // to more efficiently compress it.
433
434 LOG(INFO) << "Delta compressing kernel partition...";
435
436 // Add a new install operation
437 ops->resize(1);
438 DeltaArchiveManifest_InstallOperation* op = &(*ops)[0];
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700439 op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700440 op->set_data_offset(*blobs_length);
441
442 // Do the actual compression
443 vector<char> data;
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700444 TEST_AND_RETURN_FALSE(utils::ReadFile(new_kernel_part, &data));
445 TEST_AND_RETURN_FALSE(!data.empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700446
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700447 vector<char> data_bz;
448 TEST_AND_RETURN_FALSE(BzipCompress(data, &data_bz));
449 CHECK(!data_bz.empty());
450
451 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data_bz[0], data_bz.size()));
452 *blobs_length += data_bz.size();
453
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700454 off_t new_part_size = utils::FileSize(new_kernel_part);
455 TEST_AND_RETURN_FALSE(new_part_size >= 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700456
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700457 op->set_data_length(data_bz.size());
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700458
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700459 op->set_dst_length(new_part_size);
460
Andrew de los Reyes877ca8d2010-09-07 14:42:49 -0700461 // There's a single dest extent
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700462 Extent* dst_extent = op->add_dst_extents();
463 dst_extent->set_start_block(0);
464 dst_extent->set_num_blocks((new_part_size + kBlockSize - 1) / kBlockSize);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700465
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700466 LOG(INFO) << "Done compressing kernel partition.";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700467 return true;
468}
469
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700470} // namespace {}
471
472bool DeltaDiffGenerator::ReadFileToDiff(
473 const string& old_filename,
474 const string& new_filename,
475 vector<char>* out_data,
476 DeltaArchiveManifest_InstallOperation* out_op) {
477 // Read new data in
478 vector<char> new_data;
479 TEST_AND_RETURN_FALSE(utils::ReadFile(new_filename, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700480
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700481 TEST_AND_RETURN_FALSE(!new_data.empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700482
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700483 vector<char> new_data_bz;
484 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
485 CHECK(!new_data_bz.empty());
486
487 vector<char> data; // Data blob that will be written to delta file.
488
489 DeltaArchiveManifest_InstallOperation operation;
490 size_t current_best_size = 0;
491 if (new_data.size() <= new_data_bz.size()) {
492 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
493 current_best_size = new_data.size();
494 data = new_data;
495 } else {
496 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
497 current_best_size = new_data_bz.size();
498 data = new_data_bz;
499 }
500
501 // Do we have an original file to consider?
502 struct stat old_stbuf;
503 if (0 != stat(old_filename.c_str(), &old_stbuf)) {
504 // If stat-ing the old file fails, it should be because it doesn't exist.
505 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
506 } else {
507 // Read old data
508 vector<char> old_data;
509 TEST_AND_RETURN_FALSE(utils::ReadFile(old_filename, &old_data));
510 if (old_data == new_data) {
511 // No change in data.
512 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
513 current_best_size = 0;
514 data.clear();
515 } else {
516 // Try bsdiff of old to new data
517 vector<char> bsdiff_delta;
518 TEST_AND_RETURN_FALSE(
519 BsdiffFiles(old_filename, new_filename, &bsdiff_delta));
520 CHECK_GT(bsdiff_delta.size(), 0);
521 if (bsdiff_delta.size() < current_best_size) {
522 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
523 current_best_size = bsdiff_delta.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700524
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700525 data = bsdiff_delta;
526 }
527 }
528 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700529
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700530 // Set parameters of the operations
531 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700532
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700533 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
534 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
535 TEST_AND_RETURN_FALSE(
536 GatherExtents(old_filename, operation.mutable_src_extents()));
537 operation.set_src_length(old_stbuf.st_size);
538 }
539
540 TEST_AND_RETURN_FALSE(
541 GatherExtents(new_filename, operation.mutable_dst_extents()));
542 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700543
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700544 out_data->swap(data);
545 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700546
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700547 return true;
548}
549
550void DeltaDiffGenerator::SubstituteBlocks(
551 DeltaArchiveManifest_InstallOperation* op,
552 const vector<Extent>& remove_extents,
553 const vector<Extent>& replace_extents) {
554 // First, expand out the blocks that op reads from
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700555 vector<uint64_t> read_blocks;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700556 for (int i = 0; i < op->src_extents_size(); i++) {
557 const Extent& extent = op->src_extents(i);
558 if (extent.start_block() == kSparseHole) {
559 read_blocks.resize(read_blocks.size() + extent.num_blocks(), kSparseHole);
560 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700561 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700562 block < (extent.start_block() + extent.num_blocks()); block++) {
563 read_blocks.push_back(block);
564 }
565 }
566 }
567 {
568 // Expand remove_extents and replace_extents
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700569 vector<uint64_t> remove_extents_expanded;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700570 for (vector<Extent>::const_iterator it = remove_extents.begin();
571 it != remove_extents.end(); ++it) {
572 const Extent& extent = *it;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700573 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700574 block < (extent.start_block() + extent.num_blocks()); block++) {
575 remove_extents_expanded.push_back(block);
576 }
577 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700578 vector<uint64_t> replace_extents_expanded;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700579 for (vector<Extent>::const_iterator it = replace_extents.begin();
580 it != replace_extents.end(); ++it) {
581 const Extent& extent = *it;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700582 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700583 block < (extent.start_block() + extent.num_blocks()); block++) {
584 replace_extents_expanded.push_back(block);
585 }
586 }
587 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700588 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700589 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700590 vector<uint64_t>::size_type index = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700591 CHECK(utils::VectorIndexOf(read_blocks,
592 remove_extents_expanded[i],
593 &index));
594 CHECK(read_blocks[index] == remove_extents_expanded[i]);
595 read_blocks[index] = replace_extents_expanded[i];
596 }
597 }
598 // Convert read_blocks back to extents
599 op->clear_src_extents();
600 vector<Extent> new_extents;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700601 for (vector<uint64_t>::const_iterator it = read_blocks.begin();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700602 it != read_blocks.end(); ++it) {
603 graph_utils::AppendBlockToExtents(&new_extents, *it);
604 }
605 DeltaDiffGenerator::StoreExtents(new_extents, op->mutable_src_extents());
606}
607
608bool DeltaDiffGenerator::CutEdges(Graph* graph,
609 const vector<Block>& blocks,
610 const set<Edge>& edges) {
611 // First, find enough scratch space for the edges we'll be cutting.
612 vector<Block>::size_type blocks_required = 0;
613 for (set<Edge>::const_iterator it = edges.begin(); it != edges.end(); ++it) {
614 blocks_required += graph_utils::EdgeWeight(*graph, *it);
615 }
616 vector<Extent> scratch_extents;
617 LOG(INFO) << "requesting " << blocks_required << " blocks of scratch";
618 TEST_AND_RETURN_FALSE(
619 FindScratchSpace(blocks, blocks_required, &scratch_extents));
620 LinearExtentAllocator scratch_allocator(scratch_extents);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700621
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700622 uint64_t scratch_blocks_used = 0;
623 for (set<Edge>::const_iterator it = edges.begin();
624 it != edges.end(); ++it) {
625 vector<Extent> old_extents =
626 (*graph)[it->first].out_edges[it->second].extents;
627 // Choose some scratch space
628 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
629 LOG(INFO) << "using " << graph_utils::EdgeWeight(*graph, *it)
630 << " scratch blocks ("
631 << scratch_blocks_used << ")";
632 vector<Extent> scratch =
633 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
634 // create vertex to copy original->scratch
635 graph->resize(graph->size() + 1);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700636
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700637 // make node depend on the copy operation
638 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
639 EdgeProperties()));
640
641 // Set src/dst extents and other proto variables for copy operation
642 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
643 DeltaDiffGenerator::StoreExtents(
644 (*graph)[it->first].out_edges[it->second].extents,
645 graph->back().op.mutable_src_extents());
646 DeltaDiffGenerator::StoreExtents(scratch,
647 graph->back().op.mutable_dst_extents());
648 graph->back().op.set_src_length(
649 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
650 graph->back().op.set_dst_length(graph->back().op.src_length());
651
652 // make the dest node read from the scratch space
653 DeltaDiffGenerator::SubstituteBlocks(
654 &((*graph)[it->second].op),
655 (*graph)[it->first].out_edges[it->second].extents,
656 scratch);
657
658 // delete the old edge
659 CHECK_EQ(1, (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700660
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700661 // Add an edge from dst to copy operation
662 (*graph)[it->second].out_edges.insert(make_pair(graph->size() - 1,
663 EdgeProperties()));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700664 }
665 return true;
666}
667
668// Stores all Extents in 'extents' into 'out'.
669void DeltaDiffGenerator::StoreExtents(
670 vector<Extent>& extents,
671 google::protobuf::RepeatedPtrField<Extent>* out) {
672 for (vector<Extent>::const_iterator it = extents.begin();
673 it != extents.end(); ++it) {
674 Extent* new_extent = out->Add();
675 *new_extent = *it;
676 }
677}
678
679// Creates all the edges for the graph. Writers of a block point to
680// readers of the same block. This is because for an edge A->B, B
681// must complete before A executes.
682void DeltaDiffGenerator::CreateEdges(Graph* graph,
683 const vector<Block>& blocks) {
684 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
685 // Blocks with both a reader and writer get an edge
686 if (blocks[i].reader == Vertex::kInvalidIndex ||
687 blocks[i].writer == Vertex::kInvalidIndex)
688 continue;
689 // Don't have a node depend on itself
690 if (blocks[i].reader == blocks[i].writer)
691 continue;
692 // See if there's already an edge we can add onto
693 Vertex::EdgeMap::iterator edge_it =
694 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
695 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
696 // No existing edge. Create one
697 (*graph)[blocks[i].writer].out_edges.insert(
698 make_pair(blocks[i].reader, EdgeProperties()));
699 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700700 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700701 }
702 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
703 }
704}
705
706bool DeltaDiffGenerator::ReorderDataBlobs(
707 DeltaArchiveManifest* manifest,
708 const std::string& data_blobs_path,
709 const std::string& new_data_blobs_path) {
710 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
711 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
712 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -0700713
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700714 DirectFileWriter writer;
715 TEST_AND_RETURN_FALSE(
716 writer.Open(new_data_blobs_path.c_str(),
717 O_WRONLY | O_TRUNC | O_CREAT,
718 0644) == 0);
719 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700720 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -0700721
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700722 for (int i = 0; i < (manifest->install_operations_size() +
723 manifest->kernel_install_operations_size()); i++) {
724 DeltaArchiveManifest_InstallOperation* op = NULL;
725 if (i < manifest->install_operations_size()) {
726 op = manifest->mutable_install_operations(i);
727 } else {
728 op = manifest->mutable_kernel_install_operations(
729 i - manifest->install_operations_size());
730 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700731 if (!op->has_data_offset())
732 continue;
733 CHECK(op->has_data_length());
734 vector<char> buf(op->data_length());
735 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
736 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
737
738 op->set_data_offset(out_file_size);
739 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()) ==
740 static_cast<ssize_t>(buf.size()));
741 out_file_size += buf.size();
742 }
743 return true;
744}
745
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700746bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
747 const string& old_root,
748 const string& old_image,
749 const string& new_root,
750 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700751 const string& old_kernel_part,
752 const string& new_kernel_part,
753 const string& output_path,
754 const string& private_key_path) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700755 struct stat old_image_stbuf;
756 TEST_AND_RETURN_FALSE_ERRNO(stat(old_image.c_str(), &old_image_stbuf) == 0);
757 struct stat new_image_stbuf;
758 TEST_AND_RETURN_FALSE_ERRNO(stat(new_image.c_str(), &new_image_stbuf) == 0);
759 LOG_IF(WARNING, new_image_stbuf.st_size != old_image_stbuf.st_size)
760 << "Old and new images are different sizes.";
761 LOG_IF(FATAL, new_image_stbuf.st_size % kBlockSize)
762 << "New image not a multiple of block size " << kBlockSize;
763 LOG_IF(FATAL, old_image_stbuf.st_size % kBlockSize)
764 << "Old image not a multiple of block size " << kBlockSize;
765
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700766 // Sanity check kernel partition args
767 TEST_AND_RETURN_FALSE(utils::FileSize(old_kernel_part) >= 0);
768 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
769
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700770 vector<Block> blocks(max(old_image_stbuf.st_size / kBlockSize,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700771 new_image_stbuf.st_size / kBlockSize));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700772 LOG(INFO) << "invalid: " << Vertex::kInvalidIndex;
773 LOG(INFO) << "len: " << blocks.size();
774 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
775 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
776 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
777 }
778 Graph graph;
779 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700780
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700781 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
782 string temp_file_path;
783 off_t data_file_size = 0;
784
785 LOG(INFO) << "Reading files...";
786
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700787 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
788
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700789 DeltaArchiveManifest_InstallOperation final_op;
790 {
791 int fd;
792 TEST_AND_RETURN_FALSE(
793 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
794 TEST_AND_RETURN_FALSE(fd >= 0);
795 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700796
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700797 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
798 &blocks,
799 old_root,
800 new_root,
801 fd,
802 &data_file_size));
803 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700804
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700805 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
806 fd,
807 &data_file_size,
808 new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700809 &final_op));
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700810
811 // Read kernel partition
812 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
813 new_kernel_part,
814 &kernel_ops,
815 fd,
816 &data_file_size));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700817 }
818 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700819
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700820 LOG(INFO) << "Creating edges...";
821 CreateEdges(&graph, blocks);
822 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700823
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700824 CycleBreaker cycle_breaker;
825 LOG(INFO) << "Finding cycles...";
826 set<Edge> cut_edges;
827 cycle_breaker.BreakCycles(graph, &cut_edges);
828 CheckGraph(graph);
829
830 // Calculate number of scratch blocks needed
831
832 LOG(INFO) << "Cutting cycles...";
833 TEST_AND_RETURN_FALSE(CutEdges(&graph, blocks, cut_edges));
834 CheckGraph(graph);
835
836 vector<Vertex::Index> final_order;
837 LOG(INFO) << "Ordering...";
838 TopologicalSort(graph, &final_order);
839 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700840
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700841 // Convert to protobuf Manifest object
842 DeltaArchiveManifest manifest;
843 CheckGraph(graph);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700844 InstallOperationsToManifest(graph, final_order, kernel_ops, &manifest);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700845 {
846 // Write final operation
847 DeltaArchiveManifest_InstallOperation* op =
848 manifest.add_install_operations();
849 *op = final_op;
850 CHECK(op->has_type());
851 LOG(INFO) << "final op length: " << op->data_length();
852 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700853
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700854 CheckGraph(graph);
855 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700856
857 // Reorder the data blobs with the newly ordered manifest
858 string ordered_blobs_path;
859 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
860 "/tmp/CrAU_temp_data.ordered.XXXXXX",
861 &ordered_blobs_path,
862 false));
863 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
864 temp_file_path,
865 ordered_blobs_path));
866
867 // Check that install op blobs are in order and that all blocks are written.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700868 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700869 {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700870 vector<uint32_t> written_count(blocks.size(), 0);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700871 for (int i = 0; i < (manifest.install_operations_size() +
872 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700873 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700874 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700875 manifest.mutable_install_operations(i) :
876 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700877 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700878 for (int j = 0; j < op->dst_extents_size(); j++) {
879 const Extent& extent = op->dst_extents(j);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700880 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700881 block < (extent.start_block() + extent.num_blocks()); block++) {
Darin Petkovc0b7a532010-09-29 15:18:14 -0700882 if (block < blocks.size())
883 written_count[block]++;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700884 }
885 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700886 if (op->has_data_offset()) {
887 if (op->data_offset() != next_blob_offset) {
888 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700889 << next_blob_offset;
890 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700891 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700892 }
893 }
894 // check all blocks written to
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700895 for (vector<uint32_t>::size_type i = 0; i < written_count.size(); i++) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700896 if (written_count[i] == 0) {
897 LOG(FATAL) << "block " << i << " not written!";
898 }
899 }
900 }
901
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700902 // Signatures appear at the end of the blobs. Note the offset in the
903 // manifest
904 if (!private_key_path.empty()) {
905 LOG(INFO) << "Making room for signature in file";
906 manifest.set_signatures_offset(next_blob_offset);
907 LOG(INFO) << "set? " << manifest.has_signatures_offset();
908 // Add a dummy op at the end to appease older clients
909 DeltaArchiveManifest_InstallOperation* dummy_op =
910 manifest.add_kernel_install_operations();
911 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
912 dummy_op->set_data_offset(next_blob_offset);
913 manifest.set_signatures_offset(next_blob_offset);
914 uint64_t signature_blob_length = 0;
915 TEST_AND_RETURN_FALSE(
916 PayloadSigner::SignatureBlobLength(private_key_path,
917 &signature_blob_length));
918 dummy_op->set_data_length(signature_blob_length);
919 manifest.set_signatures_size(signature_blob_length);
920 Extent* dummy_extent = dummy_op->add_dst_extents();
921 // Tell the dummy op to write this data to a big sparse hole
922 dummy_extent->set_start_block(kSparseHole);
923 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
924 kBlockSize);
925 }
926
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700927 // Serialize protobuf
928 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700929
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700930 CheckGraph(graph);
931 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
932 CheckGraph(graph);
933
934 LOG(INFO) << "Writing final delta file header...";
935 DirectFileWriter writer;
936 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
937 O_WRONLY | O_CREAT | O_TRUNC,
938 0644) == 0);
939 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700940
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700941 // Write header
942 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)) ==
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700943 static_cast<ssize_t>(strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700944
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700945 // Write version number
946 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700947
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700948 // Write protobuf length
949 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
950 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700951
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700952 // Write protobuf
953 LOG(INFO) << "Writing final delta file protobuf... "
954 << serialized_manifest.size();
955 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
956 serialized_manifest.size()) ==
957 static_cast<ssize_t>(serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700958
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700959 // Append the data blobs
960 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700961 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700962 ScopedFdCloser blobs_fd_closer(&blobs_fd);
963 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
964 for (;;) {
965 char buf[kBlockSize];
966 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
967 if (0 == rc) {
968 // EOF
969 break;
970 }
971 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
972 TEST_AND_RETURN_FALSE(writer.Write(buf, rc) == rc);
973 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700974
975 // Write signature blob.
976 if (!private_key_path.empty()) {
977 LOG(INFO) << "Signing the update...";
978 vector<char> signature_blob;
979 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(output_path,
980 private_key_path,
981 &signature_blob));
982 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
983 signature_blob.size()) ==
984 static_cast<ssize_t>(signature_blob.size()));
985 }
986
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700987 LOG(INFO) << "All done. Successfully created delta file.";
988 return true;
989}
990
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700991const char* const kBsdiffPath = "/usr/bin/bsdiff";
992const char* const kBspatchPath = "/usr/bin/bspatch";
993const char* const kDeltaMagic = "CrAU";
994
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700995}; // namespace chromeos_update_engine