blob: ea65c1a7160c11a4c94ee01d89a309046361ed6c [file] [log] [blame]
Darin Petkovc0b7a532010-09-29 15:18:14 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/delta_diff_generator.h"
Darin Petkov880335c2010-10-01 15:52:53 -07006
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07007#include <errno.h>
8#include <fcntl.h>
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07009#include <inttypes.h>
Darin Petkov880335c2010-10-01 15:52:53 -070010#include <sys/stat.h>
11#include <sys/types.h>
12
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070013#include <algorithm>
Andrew de los Reyesef017552010-10-06 17:57:52 -070014#include <map>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070015#include <set>
16#include <string>
17#include <utility>
18#include <vector>
Darin Petkov880335c2010-10-01 15:52:53 -070019
20#include <base/logging.h>
21#include <base/string_util.h>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070022#include <bzlib.h>
Darin Petkov880335c2010-10-01 15:52:53 -070023
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070024#include "update_engine/bzip.h"
25#include "update_engine/cycle_breaker.h"
26#include "update_engine/extent_mapper.h"
Andrew de los Reyesef017552010-10-06 17:57:52 -070027#include "update_engine/extent_ranges.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070028#include "update_engine/file_writer.h"
29#include "update_engine/filesystem_iterator.h"
30#include "update_engine/graph_types.h"
31#include "update_engine/graph_utils.h"
Darin Petkov36a58222010-10-07 22:00:09 -070032#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070033#include "update_engine/payload_signer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070034#include "update_engine/subprocess.h"
35#include "update_engine/topological_sort.h"
36#include "update_engine/update_metadata.pb.h"
37#include "update_engine/utils.h"
38
39using std::make_pair;
Andrew de los Reyesef017552010-10-06 17:57:52 -070040using std::map;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070041using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070042using std::min;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -070043using std::pair;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070044using std::set;
45using std::string;
46using std::vector;
47
48namespace chromeos_update_engine {
49
50typedef DeltaDiffGenerator::Block Block;
Darin Petkov9fa7ec52010-10-18 11:45:23 -070051typedef map<const DeltaArchiveManifest_InstallOperation*,
52 const string*> OperationNameMap;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070053
54namespace {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070055const size_t kBlockSize = 4096; // bytes
Darin Petkov9eadd642010-10-14 15:20:57 -070056const size_t kRootFSPartitionSize = 1 * 1024 * 1024 * 1024; // bytes
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070057const uint64_t kVersionNumber = 1;
Darin Petkov9eadd642010-10-14 15:20:57 -070058const uint64_t kFullUpdateChunkSize = 1024 * 1024; // bytes
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070059
Darin Petkov68c10d12010-10-14 09:24:37 -070060static const char* kInstallOperationTypes[] = {
61 "REPLACE",
62 "REPLACE_BZ",
63 "MOVE",
64 "BSDIFF"
65};
66
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070067// Stores all Extents for a file into 'out'. Returns true on success.
68bool GatherExtents(const string& path,
69 google::protobuf::RepeatedPtrField<Extent>* out) {
70 vector<Extent> extents;
71 TEST_AND_RETURN_FALSE(extent_mapper::ExtentsForFileFibmap(path, &extents));
72 DeltaDiffGenerator::StoreExtents(extents, out);
73 return true;
74}
75
76// Runs the bsdiff tool on two files and returns the resulting delta in
77// 'out'. Returns true on success.
78bool BsdiffFiles(const string& old_file,
79 const string& new_file,
80 vector<char>* out) {
81 const string kPatchFile = "/tmp/delta.patchXXXXXX";
82 string patch_file_path;
83
84 TEST_AND_RETURN_FALSE(
85 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
86
87 vector<string> cmd;
88 cmd.push_back(kBsdiffPath);
89 cmd.push_back(old_file);
90 cmd.push_back(new_file);
91 cmd.push_back(patch_file_path);
92
93 int rc = 1;
94 vector<char> patch_file;
95 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc));
96 TEST_AND_RETURN_FALSE(rc == 0);
97 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
98 unlink(patch_file_path.c_str());
99 return true;
100}
101
102// The blocks vector contains a reader and writer for each block on the
103// filesystem that's being in-place updated. We populate the reader/writer
104// fields of blocks by calling this function.
105// For each block in 'operation' that is read or written, find that block
106// in 'blocks' and set the reader/writer field to the vertex passed.
107// 'graph' is not strictly necessary, but useful for printing out
108// error messages.
109bool AddInstallOpToBlocksVector(
110 const DeltaArchiveManifest_InstallOperation& operation,
111 vector<Block>* blocks,
112 const Graph& graph,
113 Vertex::Index vertex) {
114 LOG(INFO) << "AddInstallOpToBlocksVector(" << vertex << "), "
115 << graph[vertex].file_name;
116 // See if this is already present.
117 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700118
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700119 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
120 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
121 const int extents_size =
122 (field == READER) ? operation.src_extents_size() :
123 operation.dst_extents_size();
124 const char* past_participle = (field == READER) ? "read" : "written";
125 const google::protobuf::RepeatedPtrField<Extent>& extents =
126 (field == READER) ? operation.src_extents() : operation.dst_extents();
127 Vertex::Index Block::*access_type =
128 (field == READER) ? &Block::reader : &Block::writer;
129
130 for (int i = 0; i < extents_size; i++) {
131 const Extent& extent = extents.Get(i);
132 if (extent.start_block() == kSparseHole) {
133 // Hole in sparse file. skip
134 continue;
135 }
136 for (uint64_t block = extent.start_block();
137 block < (extent.start_block() + extent.num_blocks()); block++) {
138 LOG(INFO) << "ext: " << i << " block: " << block;
139 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
140 LOG(FATAL) << "Block " << block << " is already "
141 << past_participle << " by "
142 << (*blocks)[block].*access_type << "("
143 << graph[(*blocks)[block].*access_type].file_name
144 << ") and also " << vertex << "("
145 << graph[vertex].file_name << ")";
146 }
147 (*blocks)[block].*access_type = vertex;
148 }
149 }
150 }
151 return true;
152}
153
Andrew de los Reyesef017552010-10-06 17:57:52 -0700154// For a given regular file which must exist at new_root + path, and
155// may exist at old_root + path, creates a new InstallOperation and
156// adds it to the graph. Also, populates the |blocks| array as
157// necessary, if |blocks| is non-NULL. Also, writes the data
158// necessary to send the file down to the client into data_fd, which
159// has length *data_file_size. *data_file_size is updated
160// appropriately. If |existing_vertex| is no kInvalidIndex, use that
161// rather than allocating a new vertex. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700162bool DeltaReadFile(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700163 Vertex::Index existing_vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700164 vector<Block>* blocks,
165 const string& old_root,
166 const string& new_root,
167 const string& path, // within new_root
168 int data_fd,
169 off_t* data_file_size) {
170 vector<char> data;
171 DeltaArchiveManifest_InstallOperation operation;
172
173 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_root + path,
174 new_root + path,
175 &data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700176 &operation,
177 true));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700178
179 // Write the data
180 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
181 operation.set_data_offset(*data_file_size);
182 operation.set_data_length(data.size());
183 }
184
185 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
186 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700187
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700188 // Now, insert into graph and blocks vector
Andrew de los Reyesef017552010-10-06 17:57:52 -0700189 Vertex::Index vertex = existing_vertex;
190 if (vertex == Vertex::kInvalidIndex) {
191 graph->resize(graph->size() + 1);
192 vertex = graph->size() - 1;
193 }
194 (*graph)[vertex].op = operation;
195 CHECK((*graph)[vertex].op.has_type());
196 (*graph)[vertex].file_name = path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700197
Andrew de los Reyesef017552010-10-06 17:57:52 -0700198 if (blocks)
199 TEST_AND_RETURN_FALSE(AddInstallOpToBlocksVector((*graph)[vertex].op,
200 blocks,
201 *graph,
202 vertex));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700203 return true;
204}
205
206// For each regular file within new_root, creates a node in the graph,
207// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
208// and writes any necessary data to the end of data_fd.
209bool DeltaReadFiles(Graph* graph,
210 vector<Block>* blocks,
211 const string& old_root,
212 const string& new_root,
213 int data_fd,
214 off_t* data_file_size) {
215 set<ino_t> visited_inodes;
216 for (FilesystemIterator fs_iter(new_root,
217 utils::SetWithValue<string>("/lost+found"));
218 !fs_iter.IsEnd(); fs_iter.Increment()) {
219 if (!S_ISREG(fs_iter.GetStat().st_mode))
220 continue;
221
222 // Make sure we visit each inode only once.
223 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
224 continue;
225 visited_inodes.insert(fs_iter.GetStat().st_ino);
226 if (fs_iter.GetStat().st_size == 0)
227 continue;
228
229 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700230
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700231 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700232 Vertex::kInvalidIndex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700233 blocks,
234 old_root,
235 new_root,
236 fs_iter.GetPartialPath(),
237 data_fd,
238 data_file_size));
239 }
240 return true;
241}
242
Andrew de los Reyesef017552010-10-06 17:57:52 -0700243// This class allocates non-existent temp blocks, starting from
244// kTempBlockStart. Other code is responsible for converting these
245// temp blocks into real blocks, as the client can't read or write to
246// these blocks.
247class DummyExtentAllocator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700248 public:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700249 explicit DummyExtentAllocator()
250 : next_block_(kTempBlockStart) {}
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700251 vector<Extent> Allocate(const uint64_t block_count) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700252 vector<Extent> ret(1);
253 ret[0].set_start_block(next_block_);
254 ret[0].set_num_blocks(block_count);
255 next_block_ += block_count;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700256 return ret;
257 }
258 private:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700259 uint64_t next_block_;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700260};
261
262// Reads blocks from image_path that are not yet marked as being written
263// in the blocks array. These blocks that remain are non-file-data blocks.
264// In the future we might consider intelligent diffing between this data
265// and data in the previous image, but for now we just bzip2 compress it
266// and include it in the update.
267// Creates a new node in the graph to write these blocks and writes the
268// appropriate blob to blobs_fd. Reads and updates blobs_length;
269bool ReadUnwrittenBlocks(const vector<Block>& blocks,
270 int blobs_fd,
271 off_t* blobs_length,
272 const string& image_path,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700273 Vertex* vertex) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700274 vertex->file_name = "<rootfs-non-file-data>";
275
Andrew de los Reyesef017552010-10-06 17:57:52 -0700276 DeltaArchiveManifest_InstallOperation* out_op = &vertex->op;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700277 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
278 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
279 ScopedFdCloser image_fd_closer(&image_fd);
280
281 string temp_file_path;
282 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX",
283 &temp_file_path,
284 NULL));
285
286 FILE* file = fopen(temp_file_path.c_str(), "w");
287 TEST_AND_RETURN_FALSE(file);
288 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700289
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700290 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
291 file,
292 9, // max compression
293 0, // verbosity
294 0); // default work factor
295 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700296
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700297 vector<Extent> extents;
298 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700299
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700300 LOG(INFO) << "Appending left over blocks to extents";
301 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
302 if (blocks[i].writer != Vertex::kInvalidIndex)
303 continue;
Andrew de los Reyesef017552010-10-06 17:57:52 -0700304 if (blocks[i].reader != Vertex::kInvalidIndex) {
305 graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i);
306 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700307 graph_utils::AppendBlockToExtents(&extents, i);
308 block_count++;
309 }
310
311 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
312 // so we arbitrarily set it to 1024 * kBlockSize.
313 vector<char> buf(1024 * kBlockSize);
314
315 LOG(INFO) << "Reading left over blocks";
316 vector<Block>::size_type blocks_copied_count = 0;
317
318 // For each extent in extents, write the data into BZ2_bzWrite which
319 // sends it to an output file.
320 // We use the temporary buffer 'buf' to hold the data, which may be
321 // smaller than the extent, so in that case we have to loop to get
322 // the extent's data (that's the inner while loop).
323 for (vector<Extent>::const_iterator it = extents.begin();
324 it != extents.end(); ++it) {
325 vector<Block>::size_type blocks_read = 0;
326 while (blocks_read < it->num_blocks()) {
327 const int copy_block_cnt =
328 min(buf.size() / kBlockSize,
329 static_cast<vector<char>::size_type>(
330 it->num_blocks() - blocks_read));
331 ssize_t rc = pread(image_fd,
332 &buf[0],
333 copy_block_cnt * kBlockSize,
334 (it->start_block() + blocks_read) * kBlockSize);
335 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
336 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
337 copy_block_cnt * kBlockSize);
338 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
339 TEST_AND_RETURN_FALSE(err == BZ_OK);
340 blocks_read += copy_block_cnt;
341 blocks_copied_count += copy_block_cnt;
342 LOG(INFO) << "progress: " << ((float)blocks_copied_count)/block_count;
343 }
344 }
345 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
346 TEST_AND_RETURN_FALSE(err == BZ_OK);
347 bz_file = NULL;
348 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
349 file = NULL;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700350
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700351 vector<char> compressed_data;
352 LOG(INFO) << "Reading compressed data off disk";
353 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
354 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700355
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700356 // Add node to graph to write these blocks
357 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
358 out_op->set_data_offset(*blobs_length);
359 out_op->set_data_length(compressed_data.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700360 LOG(INFO) << "Rootfs non-data blocks compressed take up "
361 << compressed_data.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700362 *blobs_length += compressed_data.size();
363 out_op->set_dst_length(kBlockSize * block_count);
364 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700365
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700366 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
367 &compressed_data[0],
368 compressed_data.size()));
369 LOG(INFO) << "done with extra blocks";
370 return true;
371}
372
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700373// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700374// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700375bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
376 uint64_t value_be = htobe64(value);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700377 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)) ==
378 sizeof(value_be));
379 return true;
380}
381
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700382// Adds each operation from |graph| to |out_manifest| in the order specified by
383// |order| while building |out_op_name_map| with operation to name
384// mappings. Adds all |kernel_ops| to |out_manifest|. Filters out no-op
385// operations.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700386void InstallOperationsToManifest(
387 const Graph& graph,
388 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700389 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700390 DeltaArchiveManifest* out_manifest,
391 OperationNameMap* out_op_name_map) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700392 for (vector<Vertex::Index>::const_iterator it = order.begin();
393 it != order.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700394 const Vertex& vertex = graph[*it];
395 const DeltaArchiveManifest_InstallOperation& add_op = vertex.op;
396 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
397 continue;
398 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700399 DeltaArchiveManifest_InstallOperation* op =
400 out_manifest->add_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700401 *op = add_op;
402 (*out_op_name_map)[op] = &vertex.file_name;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700403 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700404 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
405 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700406 const DeltaArchiveManifest_InstallOperation& add_op = *it;
407 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
408 continue;
409 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700410 DeltaArchiveManifest_InstallOperation* op =
411 out_manifest->add_kernel_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700412 *op = add_op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700413 }
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
Darin Petkov68c10d12010-10-14 09:24:37 -0700422// Delta compresses a kernel partition |new_kernel_part| with knowledge of the
423// old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty
424// string, generates a full update of the partition.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700425bool DeltaCompressKernelPartition(
426 const string& old_kernel_part,
427 const string& new_kernel_part,
428 vector<DeltaArchiveManifest_InstallOperation>* ops,
429 int blobs_fd,
430 off_t* blobs_length) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700431 LOG(INFO) << "Delta compressing kernel partition...";
Darin Petkov68c10d12010-10-14 09:24:37 -0700432 LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update...";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700433
434 // Add a new install operation
435 ops->resize(1);
436 DeltaArchiveManifest_InstallOperation* op = &(*ops)[0];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700437
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700438 vector<char> data;
Darin Petkov68c10d12010-10-14 09:24:37 -0700439 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_kernel_part,
440 new_kernel_part,
441 &data,
442 op,
443 false));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700444
Darin Petkov68c10d12010-10-14 09:24:37 -0700445 // Write the data
446 if (op->type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
447 op->set_data_offset(*blobs_length);
448 op->set_data_length(data.size());
449 }
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700450
Darin Petkov68c10d12010-10-14 09:24:37 -0700451 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
452 *blobs_length += data.size();
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700453
Darin Petkov68c10d12010-10-14 09:24:37 -0700454 LOG(INFO) << "Done delta compressing kernel partition: "
455 << kInstallOperationTypes[op->type()];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700456 return true;
457}
458
Darin Petkov880335c2010-10-01 15:52:53 -0700459struct DeltaObject {
460 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
461 : name(in_name),
462 type(in_type),
463 size(in_size) {}
464 bool operator <(const DeltaObject& object) const {
Darin Petkovd43d6902010-10-14 11:17:50 -0700465 return (size != object.size) ? (size < object.size) : (name < object.name);
Darin Petkov880335c2010-10-01 15:52:53 -0700466 }
467 string name;
468 int type;
469 off_t size;
470};
471
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700472void ReportPayloadUsage(const DeltaArchiveManifest& manifest,
473 const int64_t manifest_metadata_size,
474 const OperationNameMap& op_name_map) {
Darin Petkov880335c2010-10-01 15:52:53 -0700475 vector<DeltaObject> objects;
476 off_t total_size = 0;
477
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700478 // Rootfs install operations.
479 for (int i = 0; i < manifest.install_operations_size(); ++i) {
480 const DeltaArchiveManifest_InstallOperation& op =
481 manifest.install_operations(i);
482 objects.push_back(DeltaObject(*op_name_map.find(&op)->second,
483 op.type(),
484 op.data_length()));
485 total_size += op.data_length();
Darin Petkov880335c2010-10-01 15:52:53 -0700486 }
487
Darin Petkov880335c2010-10-01 15:52:53 -0700488 // Kernel install operations.
489 for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) {
490 const DeltaArchiveManifest_InstallOperation& op =
491 manifest.kernel_install_operations(i);
492 objects.push_back(DeltaObject(StringPrintf("<kernel-operation-%d>", i),
493 op.type(),
494 op.data_length()));
495 total_size += op.data_length();
496 }
497
Darin Petkov95cf01f2010-10-12 14:59:13 -0700498 objects.push_back(DeltaObject("<manifest-metadata>",
499 -1,
500 manifest_metadata_size));
501 total_size += manifest_metadata_size;
502
Darin Petkov880335c2010-10-01 15:52:53 -0700503 std::sort(objects.begin(), objects.end());
504
505 static const char kFormatString[] = "%6.2f%% %10llu %-10s %s\n";
506 for (vector<DeltaObject>::const_iterator it = objects.begin();
507 it != objects.end(); ++it) {
508 const DeltaObject& object = *it;
509 fprintf(stderr, kFormatString,
510 object.size * 100.0 / total_size,
511 object.size,
Darin Petkov95cf01f2010-10-12 14:59:13 -0700512 object.type >= 0 ? kInstallOperationTypes[object.type] : "-",
Darin Petkov880335c2010-10-01 15:52:53 -0700513 object.name.c_str());
514 }
515 fprintf(stderr, kFormatString, 100.0, total_size, "", "<total>");
516}
517
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700518} // namespace {}
519
520bool DeltaDiffGenerator::ReadFileToDiff(
521 const string& old_filename,
522 const string& new_filename,
523 vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700524 DeltaArchiveManifest_InstallOperation* out_op,
525 bool gather_extents) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700526 // Read new data in
527 vector<char> new_data;
528 TEST_AND_RETURN_FALSE(utils::ReadFile(new_filename, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700529
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700530 TEST_AND_RETURN_FALSE(!new_data.empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700531
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700532 vector<char> new_data_bz;
533 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
534 CHECK(!new_data_bz.empty());
535
536 vector<char> data; // Data blob that will be written to delta file.
537
538 DeltaArchiveManifest_InstallOperation operation;
539 size_t current_best_size = 0;
540 if (new_data.size() <= new_data_bz.size()) {
541 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
542 current_best_size = new_data.size();
543 data = new_data;
544 } else {
545 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
546 current_best_size = new_data_bz.size();
547 data = new_data_bz;
548 }
549
550 // Do we have an original file to consider?
551 struct stat old_stbuf;
Darin Petkov68c10d12010-10-14 09:24:37 -0700552 bool no_original = old_filename.empty();
553 if (!no_original && 0 != stat(old_filename.c_str(), &old_stbuf)) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700554 // If stat-ing the old file fails, it should be because it doesn't exist.
555 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
Darin Petkov68c10d12010-10-14 09:24:37 -0700556 no_original = true;
557 }
558 if (!no_original) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700559 // Read old data
560 vector<char> old_data;
561 TEST_AND_RETURN_FALSE(utils::ReadFile(old_filename, &old_data));
562 if (old_data == new_data) {
563 // No change in data.
564 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
565 current_best_size = 0;
566 data.clear();
567 } else {
568 // Try bsdiff of old to new data
569 vector<char> bsdiff_delta;
570 TEST_AND_RETURN_FALSE(
571 BsdiffFiles(old_filename, new_filename, &bsdiff_delta));
572 CHECK_GT(bsdiff_delta.size(), 0);
573 if (bsdiff_delta.size() < current_best_size) {
574 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
575 current_best_size = bsdiff_delta.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700576
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700577 data = bsdiff_delta;
578 }
579 }
580 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700581
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700582 // Set parameters of the operations
583 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700584
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700585 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
586 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Darin Petkov68c10d12010-10-14 09:24:37 -0700587 if (gather_extents) {
588 TEST_AND_RETURN_FALSE(
589 GatherExtents(old_filename, operation.mutable_src_extents()));
590 } else {
591 Extent* src_extent = operation.add_src_extents();
592 src_extent->set_start_block(0);
593 src_extent->set_num_blocks(
594 (old_stbuf.st_size + kBlockSize - 1) / kBlockSize);
595 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700596 operation.set_src_length(old_stbuf.st_size);
597 }
598
Darin Petkov68c10d12010-10-14 09:24:37 -0700599 if (gather_extents) {
600 TEST_AND_RETURN_FALSE(
601 GatherExtents(new_filename, operation.mutable_dst_extents()));
602 } else {
603 Extent* dst_extent = operation.add_dst_extents();
604 dst_extent->set_start_block(0);
605 dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize);
606 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700607 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700608
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700609 out_data->swap(data);
610 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700611
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700612 return true;
613}
614
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700615bool DeltaDiffGenerator::InitializePartitionInfo(bool is_kernel,
616 const string& partition,
617 PartitionInfo* info) {
Darin Petkov7ea32332010-10-13 10:46:11 -0700618 int64_t size = 0;
619 if (is_kernel) {
620 size = utils::FileSize(partition);
621 } else {
622 int block_count = 0, block_size = 0;
623 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition,
624 &block_count,
625 &block_size));
626 size = static_cast<int64_t>(block_count) * block_size;
627 }
628 TEST_AND_RETURN_FALSE(size > 0);
Darin Petkov36a58222010-10-07 22:00:09 -0700629 info->set_size(size);
630 OmahaHashCalculator hasher;
Darin Petkov7ea32332010-10-13 10:46:11 -0700631 TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size);
Darin Petkov36a58222010-10-07 22:00:09 -0700632 TEST_AND_RETURN_FALSE(hasher.Finalize());
633 const vector<char>& hash = hasher.raw_hash();
634 info->set_hash(hash.data(), hash.size());
Darin Petkovd43d6902010-10-14 11:17:50 -0700635 LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash();
Darin Petkov36a58222010-10-07 22:00:09 -0700636 return true;
637}
638
639bool InitializePartitionInfos(const string& old_kernel,
640 const string& new_kernel,
641 const string& old_rootfs,
642 const string& new_rootfs,
643 DeltaArchiveManifest* manifest) {
Darin Petkovd43d6902010-10-14 11:17:50 -0700644 if (!old_kernel.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700645 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
646 true,
647 old_kernel,
648 manifest->mutable_old_kernel_info()));
Darin Petkovd43d6902010-10-14 11:17:50 -0700649 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700650 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
651 true,
652 new_kernel,
653 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700654 if (!old_rootfs.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700655 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
656 false,
657 old_rootfs,
658 manifest->mutable_old_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700659 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700660 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
661 false,
662 new_rootfs,
663 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700664 return true;
665}
666
Andrew de los Reyesef017552010-10-06 17:57:52 -0700667namespace {
668
669// Takes a collection (vector or RepeatedPtrField) of Extent and
670// returns a vector of the blocks referenced, in order.
671template<typename T>
672vector<uint64_t> ExpandExtents(const T& extents) {
673 vector<uint64_t> ret;
674 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
675 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700676 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700677 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700678 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700679 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700680 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700681 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700682 }
683 }
684 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700685 return ret;
686}
687
688// Takes a vector of blocks and returns an equivalent vector of Extent
689// objects.
690vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
691 vector<Extent> new_extents;
692 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
693 it != e; ++it) {
694 graph_utils::AppendBlockToExtents(&new_extents, *it);
695 }
696 return new_extents;
697}
698
699} // namespace {}
700
701void DeltaDiffGenerator::SubstituteBlocks(
702 Vertex* vertex,
703 const vector<Extent>& remove_extents,
704 const vector<Extent>& replace_extents) {
705 // First, expand out the blocks that op reads from
706 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700707 {
708 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700709 vector<uint64_t> remove_extents_expanded =
710 ExpandExtents(remove_extents);
711 vector<uint64_t> replace_extents_expanded =
712 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700713 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700714 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700715 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700716 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700717 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
718 }
719 utils::ApplyMap(&read_blocks, conversion);
720 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
721 e = vertex->out_edges.end(); it != e; ++it) {
722 vector<uint64_t> write_before_deps_expanded =
723 ExpandExtents(it->second.write_extents);
724 utils::ApplyMap(&write_before_deps_expanded, conversion);
725 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700726 }
727 }
728 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700729 vertex->op.clear_src_extents();
730 vector<Extent> new_extents = CompressExtents(read_blocks);
731 DeltaDiffGenerator::StoreExtents(new_extents,
732 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700733}
734
735bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700736 const set<Edge>& edges,
737 vector<CutEdgeVertexes>* out_cuts) {
738 DummyExtentAllocator scratch_allocator;
739 vector<CutEdgeVertexes> cuts;
740 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700741
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700742 uint64_t scratch_blocks_used = 0;
743 for (set<Edge>::const_iterator it = edges.begin();
744 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700745 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700746 vector<Extent> old_extents =
747 (*graph)[it->first].out_edges[it->second].extents;
748 // Choose some scratch space
749 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
750 LOG(INFO) << "using " << graph_utils::EdgeWeight(*graph, *it)
751 << " scratch blocks ("
752 << scratch_blocks_used << ")";
Andrew de los Reyesef017552010-10-06 17:57:52 -0700753 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700754 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
755 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700756 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700757 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700758 cuts.back().old_src = it->first;
759 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700760
Andrew de los Reyesef017552010-10-06 17:57:52 -0700761 EdgeProperties& cut_edge_properties =
762 (*graph)[it->first].out_edges.find(it->second)->second;
763
764 // This should never happen, as we should only be cutting edges between
765 // real file nodes, and write-before relationships are created from
766 // a real file node to a temp copy node:
767 CHECK(cut_edge_properties.write_extents.empty())
768 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700769
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700770 // make node depend on the copy operation
771 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700772 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700773
774 // Set src/dst extents and other proto variables for copy operation
775 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
776 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700777 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700778 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700779 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700780 graph->back().op.mutable_dst_extents());
781 graph->back().op.set_src_length(
782 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
783 graph->back().op.set_dst_length(graph->back().op.src_length());
784
785 // make the dest node read from the scratch space
786 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700787 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700788 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700789 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700790
791 // delete the old edge
792 CHECK_EQ(1, (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700793
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700794 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700795 EdgeProperties write_before_edge_properties;
796 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
797 (*graph)[it->second].out_edges.insert(
798 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700799 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700800 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700801 return true;
802}
803
804// Stores all Extents in 'extents' into 'out'.
805void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700806 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700807 google::protobuf::RepeatedPtrField<Extent>* out) {
808 for (vector<Extent>::const_iterator it = extents.begin();
809 it != extents.end(); ++it) {
810 Extent* new_extent = out->Add();
811 *new_extent = *it;
812 }
813}
814
815// Creates all the edges for the graph. Writers of a block point to
816// readers of the same block. This is because for an edge A->B, B
817// must complete before A executes.
818void DeltaDiffGenerator::CreateEdges(Graph* graph,
819 const vector<Block>& blocks) {
820 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
821 // Blocks with both a reader and writer get an edge
822 if (blocks[i].reader == Vertex::kInvalidIndex ||
823 blocks[i].writer == Vertex::kInvalidIndex)
824 continue;
825 // Don't have a node depend on itself
826 if (blocks[i].reader == blocks[i].writer)
827 continue;
828 // See if there's already an edge we can add onto
829 Vertex::EdgeMap::iterator edge_it =
830 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
831 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
832 // No existing edge. Create one
833 (*graph)[blocks[i].writer].out_edges.insert(
834 make_pair(blocks[i].reader, EdgeProperties()));
835 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700836 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700837 }
838 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
839 }
840}
841
Andrew de los Reyesef017552010-10-06 17:57:52 -0700842namespace {
843
844class SortCutsByTopoOrderLess {
845 public:
846 SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table)
847 : table_(table) {}
848 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
849 return table_[a.old_dst] < table_[b.old_dst];
850 }
851 private:
852 vector<vector<Vertex::Index>::size_type>& table_;
853};
854
855} // namespace {}
856
857void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
858 vector<Vertex::Index>& op_indexes,
859 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
860 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
861 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
862 i != e; ++i) {
863 Vertex::Index node = op_indexes[i];
864 if (table.size() < (node + 1)) {
865 table.resize(node + 1);
866 }
867 table[node] = i;
868 }
869 reverse_op_indexes->swap(table);
870}
871
872void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes,
873 vector<CutEdgeVertexes>* cuts) {
874 // first, make a reverse lookup table.
875 vector<vector<Vertex::Index>::size_type> table;
876 GenerateReverseTopoOrderMap(op_indexes, &table);
877 SortCutsByTopoOrderLess less(table);
878 sort(cuts->begin(), cuts->end(), less);
879}
880
881void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
882 vector<Vertex::Index>* op_indexes) {
883 vector<Vertex::Index> ret;
884 vector<Vertex::Index> full_ops;
885 ret.reserve(op_indexes->size());
886 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
887 ++i) {
888 DeltaArchiveManifest_InstallOperation_Type type =
889 (*graph)[(*op_indexes)[i]].op.type();
890 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
891 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
892 full_ops.push_back((*op_indexes)[i]);
893 } else {
894 ret.push_back((*op_indexes)[i]);
895 }
896 }
897 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
898 << (full_ops.size() + ret.size()) << " total ops.";
899 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
900 op_indexes->swap(ret);
901}
902
903namespace {
904
905template<typename T>
906bool TempBlocksExistInExtents(const T& extents) {
907 for (int i = 0, e = extents.size(); i < e; ++i) {
908 Extent extent = graph_utils::GetElement(extents, i);
909 uint64_t start = extent.start_block();
910 uint64_t num = extent.num_blocks();
911 if (start == kSparseHole)
912 continue;
913 if (start >= kTempBlockStart ||
914 (start + num) >= kTempBlockStart) {
915 LOG(ERROR) << "temp block!";
916 LOG(ERROR) << "start: " << start << ", num: " << num;
917 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
918 LOG(ERROR) << "returning true";
919 return true;
920 }
921 // check for wrap-around, which would be a bug:
922 CHECK(start <= (start + num));
923 }
924 return false;
925}
926
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700927// Convertes the cuts, which must all have the same |old_dst| member,
928// to full. It does this by converting the |old_dst| to REPLACE or
929// REPLACE_BZ, dropping all incoming edges to |old_dst|, and marking
930// all temp nodes invalid.
931bool ConvertCutsToFull(
932 Graph* graph,
933 const string& new_root,
934 int data_fd,
935 off_t* data_file_size,
936 vector<Vertex::Index>* op_indexes,
937 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
938 const vector<CutEdgeVertexes>& cuts) {
939 CHECK(!cuts.empty());
940 set<Vertex::Index> deleted_nodes;
941 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
942 e = cuts.end(); it != e; ++it) {
943 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp(
944 graph,
945 *it,
946 new_root,
947 data_fd,
948 data_file_size));
949 deleted_nodes.insert(it->new_vertex);
950 }
951 deleted_nodes.insert(cuts[0].old_dst);
952
953 vector<Vertex::Index> new_op_indexes;
954 new_op_indexes.reserve(op_indexes->size());
955 for (vector<Vertex::Index>::iterator it = op_indexes->begin(),
956 e = op_indexes->end(); it != e; ++it) {
957 if (utils::SetContainsKey(deleted_nodes, *it))
958 continue;
959 new_op_indexes.push_back(*it);
960 }
961 new_op_indexes.push_back(cuts[0].old_dst);
962 op_indexes->swap(new_op_indexes);
963 DeltaDiffGenerator::GenerateReverseTopoOrderMap(*op_indexes,
964 reverse_op_indexes);
965 return true;
966}
967
968// Tries to assign temp blocks for a collection of cuts, all of which share
969// the same old_dst member. If temp blocks can't be found, old_dst will be
970// converted to a REPLACE or REPLACE_BZ operation. Returns true on success,
971// which can happen even if blocks are converted to full. Returns false
972// on exceptional error cases.
973bool AssignBlockForAdjoiningCuts(
974 Graph* graph,
975 const string& new_root,
976 int data_fd,
977 off_t* data_file_size,
978 vector<Vertex::Index>* op_indexes,
979 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
980 const vector<CutEdgeVertexes>& cuts) {
981 CHECK(!cuts.empty());
982 const Vertex::Index old_dst = cuts[0].old_dst;
983 // Calculate # of blocks needed
984 uint64_t blocks_needed = 0;
985 map<const CutEdgeVertexes*, uint64_t> cuts_blocks_needed;
986 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
987 e = cuts.end(); it != e; ++it) {
988 uint64_t cut_blocks_needed = 0;
989 for (vector<Extent>::const_iterator jt = it->tmp_extents.begin(),
990 je = it->tmp_extents.end(); jt != je; ++jt) {
991 cut_blocks_needed += jt->num_blocks();
992 }
993 blocks_needed += cut_blocks_needed;
994 cuts_blocks_needed[&*it] = cut_blocks_needed;
995 }
996 LOG(INFO) << "Need to find " << blocks_needed << " blocks for "
997 << cuts.size() << " cuts";
998
999 // Find enough blocks
1000 ExtentRanges scratch_ranges;
1001 // Each block that's supplying temp blocks and the corresponding blocks:
1002 typedef vector<pair<Vertex::Index, ExtentRanges> > SupplierVector;
1003 SupplierVector block_suppliers;
1004 uint64_t scratch_blocks_found = 0;
1005 LOG(INFO) << "scan from " << (*reverse_op_indexes)[old_dst] + 1
1006 << " to " << op_indexes->size();
1007 for (vector<Vertex::Index>::size_type i = (*reverse_op_indexes)[old_dst] + 1,
1008 e = op_indexes->size(); i < e; ++i) {
1009 Vertex::Index test_node = (*op_indexes)[i];
1010 if (!(*graph)[test_node].valid)
1011 continue;
1012 // See if this node has sufficient blocks
1013 ExtentRanges ranges;
1014 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
1015 ranges.SubtractExtent(ExtentForRange(
1016 kTempBlockStart, kSparseHole - kTempBlockStart));
1017 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
1018 // For now, for simplicity, subtract out all blocks in read-before
1019 // dependencies.
1020 for (Vertex::EdgeMap::const_iterator edge_i =
1021 (*graph)[test_node].out_edges.begin(),
1022 edge_e = (*graph)[test_node].out_edges.end();
1023 edge_i != edge_e; ++edge_i) {
1024 ranges.SubtractExtents(edge_i->second.extents);
1025 }
1026 if (ranges.blocks() == 0)
1027 continue;
1028
1029 if (ranges.blocks() + scratch_blocks_found > blocks_needed) {
1030 // trim down ranges
1031 vector<Extent> new_ranges = ranges.GetExtentsForBlockCount(
1032 blocks_needed - scratch_blocks_found);
1033 ranges = ExtentRanges();
1034 ranges.AddExtents(new_ranges);
1035 }
1036 scratch_ranges.AddRanges(ranges);
1037 block_suppliers.push_back(make_pair(test_node, ranges));
1038 scratch_blocks_found += ranges.blocks();
1039 LOG(INFO) << "Adding " << ranges.blocks() << " blocks. Now have "
1040 << scratch_ranges.blocks();
1041 if (scratch_ranges.blocks() >= blocks_needed)
1042 break;
1043 }
1044 if (scratch_ranges.blocks() < blocks_needed) {
1045 LOG(INFO) << "Unable to find sufficient scratch";
1046 TEST_AND_RETURN_FALSE(ConvertCutsToFull(graph,
1047 new_root,
1048 data_fd,
1049 data_file_size,
1050 op_indexes,
1051 reverse_op_indexes,
1052 cuts));
1053 return true;
1054 }
1055 // Use the scratch we found
1056 TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found);
1057
1058 // Make all the suppliers depend on this node
1059 for (SupplierVector::iterator it = block_suppliers.begin(),
1060 e = block_suppliers.end(); it != e; ++it) {
1061 graph_utils::AddReadBeforeDepExtents(
1062 &(*graph)[it->first],
1063 old_dst,
1064 it->second.GetExtentsForBlockCount(it->second.blocks()));
1065 }
1066
1067 // Replace temp blocks in each cut
1068 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1069 e = cuts.end(); it != e; ++it) {
1070 vector<Extent> real_extents =
1071 scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]);
1072 scratch_ranges.SubtractExtents(real_extents);
1073
1074 // Fix the old dest node w/ the real blocks
1075 DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst],
1076 it->tmp_extents,
1077 real_extents);
1078
1079 // Fix the new node w/ the real blocks. Since the new node is just a
1080 // copy operation, we can replace all the dest extents w/ the real
1081 // blocks.
1082 DeltaArchiveManifest_InstallOperation *op =
1083 &(*graph)[it->new_vertex].op;
1084 op->clear_dst_extents();
1085 DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents());
1086 }
1087 LOG(INFO) << "Done subbing in for cut set";
1088 return true;
1089}
1090
Andrew de los Reyesef017552010-10-06 17:57:52 -07001091} // namespace {}
1092
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001093// Returns true if |op| is a no-op operation that doesn't do any useful work
1094// (e.g., a move operation that copies blocks onto themselves).
1095bool DeltaDiffGenerator::IsNoopOperation(
1096 const DeltaArchiveManifest_InstallOperation& op) {
1097 return (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE &&
1098 ExpandExtents(op.src_extents()) == ExpandExtents(op.dst_extents()));
1099}
1100
Andrew de los Reyesef017552010-10-06 17:57:52 -07001101bool DeltaDiffGenerator::AssignTempBlocks(
1102 Graph* graph,
1103 const string& new_root,
1104 int data_fd,
1105 off_t* data_file_size,
1106 vector<Vertex::Index>* op_indexes,
1107 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001108 const vector<CutEdgeVertexes>& cuts) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001109 CHECK(!cuts.empty());
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001110
1111 // group of cuts w/ the same old_dst:
1112 vector<CutEdgeVertexes> cuts_group;
1113
Andrew de los Reyesef017552010-10-06 17:57:52 -07001114 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
1115 true ; --i) {
1116 LOG(INFO) << "Fixing temp blocks in cut " << i
1117 << ": old dst: " << cuts[i].old_dst << " new vertex: "
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001118 << cuts[i].new_vertex << " path: "
1119 << (*graph)[cuts[i].old_dst].file_name;
1120
1121 if (cuts_group.empty() || (cuts_group[0].old_dst == cuts[i].old_dst)) {
1122 cuts_group.push_back(cuts[i]);
1123 } else {
1124 CHECK(!cuts_group.empty());
1125 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1126 new_root,
1127 data_fd,
1128 data_file_size,
1129 op_indexes,
1130 reverse_op_indexes,
1131 cuts_group));
1132 cuts_group.clear();
1133 cuts_group.push_back(cuts[i]);
Andrew de los Reyesef017552010-10-06 17:57:52 -07001134 }
Darin Petkov36a58222010-10-07 22:00:09 -07001135
Andrew de los Reyesef017552010-10-06 17:57:52 -07001136 if (i == e) {
1137 // break out of for() loop
1138 break;
1139 }
1140 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001141 CHECK(!cuts_group.empty());
1142 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1143 new_root,
1144 data_fd,
1145 data_file_size,
1146 op_indexes,
1147 reverse_op_indexes,
1148 cuts_group));
Andrew de los Reyesef017552010-10-06 17:57:52 -07001149 return true;
1150}
1151
1152bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1153 size_t idx = 0;
1154 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1155 ++it, ++idx) {
1156 if (!it->valid)
1157 continue;
1158 const DeltaArchiveManifest_InstallOperation& op = it->op;
1159 if (TempBlocksExistInExtents(op.dst_extents()) ||
1160 TempBlocksExistInExtents(op.src_extents())) {
1161 LOG(INFO) << "bad extents in node " << idx;
1162 LOG(INFO) << "so yeah";
1163 return false;
1164 }
1165
1166 // Check out-edges:
1167 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1168 je = it->out_edges.end(); jt != je; ++jt) {
1169 if (TempBlocksExistInExtents(jt->second.extents) ||
1170 TempBlocksExistInExtents(jt->second.write_extents)) {
1171 LOG(INFO) << "bad out edge in node " << idx;
1172 LOG(INFO) << "so yeah";
1173 return false;
1174 }
1175 }
1176 }
1177 return true;
1178}
1179
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001180bool DeltaDiffGenerator::ReorderDataBlobs(
1181 DeltaArchiveManifest* manifest,
1182 const std::string& data_blobs_path,
1183 const std::string& new_data_blobs_path) {
1184 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1185 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1186 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001187
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001188 DirectFileWriter writer;
1189 TEST_AND_RETURN_FALSE(
1190 writer.Open(new_data_blobs_path.c_str(),
1191 O_WRONLY | O_TRUNC | O_CREAT,
1192 0644) == 0);
1193 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001194 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001195
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001196 for (int i = 0; i < (manifest->install_operations_size() +
1197 manifest->kernel_install_operations_size()); i++) {
1198 DeltaArchiveManifest_InstallOperation* op = NULL;
1199 if (i < manifest->install_operations_size()) {
1200 op = manifest->mutable_install_operations(i);
1201 } else {
1202 op = manifest->mutable_kernel_install_operations(
1203 i - manifest->install_operations_size());
1204 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001205 if (!op->has_data_offset())
1206 continue;
1207 CHECK(op->has_data_length());
1208 vector<char> buf(op->data_length());
1209 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1210 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1211
1212 op->set_data_offset(out_file_size);
1213 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()) ==
1214 static_cast<ssize_t>(buf.size()));
1215 out_file_size += buf.size();
1216 }
1217 return true;
1218}
1219
Andrew de los Reyesef017552010-10-06 17:57:52 -07001220bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1221 const CutEdgeVertexes& cut,
1222 const string& new_root,
1223 int data_fd,
1224 off_t* data_file_size) {
1225 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001226
Andrew de los Reyesef017552010-10-06 17:57:52 -07001227 // Keep all outgoing edges
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001228 if ((*graph)[cut.old_dst].op.type() !=
1229 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ &&
1230 (*graph)[cut.old_dst].op.type() !=
1231 DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
1232 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1233 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001234
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001235 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1236 cut.old_dst,
1237 NULL,
1238 "/-!@:&*nonexistent_path",
1239 new_root,
1240 (*graph)[cut.old_dst].file_name,
1241 data_fd,
1242 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001243
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001244 (*graph)[cut.old_dst].out_edges = out_edges;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001245
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001246 // Right now we don't have doubly-linked edges, so we have to scan
1247 // the whole graph.
1248 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1249 }
Andrew de los Reyesef017552010-10-06 17:57:52 -07001250
1251 // Delete temp node
1252 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1253 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1254 (*graph)[cut.old_dst].out_edges.end());
1255 (*graph)[cut.new_vertex].valid = false;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001256 LOG(INFO) << "marked node invalid: " << cut.new_vertex;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001257 return true;
1258}
1259
1260bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1261 const string& new_root,
1262 int fd,
1263 off_t* data_file_size,
1264 vector<Vertex::Index>* final_order) {
1265 CycleBreaker cycle_breaker;
1266 LOG(INFO) << "Finding cycles...";
1267 set<Edge> cut_edges;
1268 cycle_breaker.BreakCycles(*graph, &cut_edges);
1269 LOG(INFO) << "done finding cycles";
1270 CheckGraph(*graph);
1271
1272 // Calculate number of scratch blocks needed
1273
1274 LOG(INFO) << "Cutting cycles...";
1275 vector<CutEdgeVertexes> cuts;
1276 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1277 LOG(INFO) << "done cutting cycles";
1278 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1279 CheckGraph(*graph);
1280
1281 LOG(INFO) << "Creating initial topological order...";
1282 TopologicalSort(*graph, final_order);
1283 LOG(INFO) << "done with initial topo order";
1284 CheckGraph(*graph);
1285
1286 LOG(INFO) << "Moving full ops to the back";
1287 MoveFullOpsToBack(graph, final_order);
1288 LOG(INFO) << "done moving full ops to back";
1289
1290 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1291 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1292
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001293 SortCutsByTopoOrder(*final_order, &cuts);
1294
Andrew de los Reyesef017552010-10-06 17:57:52 -07001295 if (!cuts.empty())
1296 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1297 new_root,
1298 fd,
1299 data_file_size,
1300 final_order,
1301 &inverse_final_order,
1302 cuts));
1303 LOG(INFO) << "Making sure all temp blocks have been allocated";
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001304
Andrew de los Reyesef017552010-10-06 17:57:52 -07001305 graph_utils::DumpGraph(*graph);
1306 CHECK(NoTempBlocksRemain(*graph));
1307 LOG(INFO) << "done making sure all temp blocks are allocated";
1308 return true;
1309}
1310
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001311bool DeltaDiffGenerator::ReadFullUpdateFromDisk(
1312 Graph* graph,
1313 const std::string& new_kernel_part,
1314 const std::string& new_image,
Darin Petkov7ea32332010-10-13 10:46:11 -07001315 off_t image_size,
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001316 int fd,
1317 off_t* data_file_size,
1318 off_t chunk_size,
1319 vector<DeltaArchiveManifest_InstallOperation>* kernel_ops,
1320 std::vector<Vertex::Index>* final_order) {
1321 TEST_AND_RETURN_FALSE(chunk_size > 0);
1322 TEST_AND_RETURN_FALSE((chunk_size % kBlockSize) == 0);
Darin Petkov36a58222010-10-07 22:00:09 -07001323
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001324 // Get the sizes early in the function, so we can fail fast if the user
1325 // passed us bad paths.
Darin Petkov7ea32332010-10-13 10:46:11 -07001326 TEST_AND_RETURN_FALSE(image_size >= 0 &&
1327 image_size <= utils::FileSize(new_image));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001328 const off_t kernel_size = utils::FileSize(new_kernel_part);
1329 TEST_AND_RETURN_FALSE(kernel_size >= 0);
1330
1331 off_t part_sizes[] = { image_size, kernel_size };
1332 string paths[] = { new_image, new_kernel_part };
1333
1334 for (int partition = 0; partition < 2; ++partition) {
1335 const string& path = paths[partition];
1336 LOG(INFO) << "compressing " << path;
1337
1338 int in_fd = open(path.c_str(), O_RDONLY, 0);
1339 TEST_AND_RETURN_FALSE(in_fd >= 0);
1340 ScopedFdCloser in_fd_closer(&in_fd);
1341
1342 for (off_t bytes_left = part_sizes[partition], counter = 0, offset = 0;
1343 bytes_left > 0;
1344 bytes_left -= chunk_size, ++counter, offset += chunk_size) {
1345 LOG(INFO) << "offset = " << offset;
1346 DeltaArchiveManifest_InstallOperation* op = NULL;
1347 if (partition == 0) {
1348 graph->resize(graph->size() + 1);
1349 graph->back().file_name = path + StringPrintf("-%" PRIi64, counter);
1350 op = &graph->back().op;
1351 final_order->push_back(graph->size() - 1);
1352 } else {
1353 kernel_ops->resize(kernel_ops->size() + 1);
1354 op = &kernel_ops->back();
1355 }
1356 LOG(INFO) << "have an op";
Darin Petkov36a58222010-10-07 22:00:09 -07001357
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001358 vector<char> buf(min(bytes_left, chunk_size));
1359 LOG(INFO) << "buf size: " << buf.size();
1360 ssize_t bytes_read = -1;
Darin Petkov36a58222010-10-07 22:00:09 -07001361
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001362 TEST_AND_RETURN_FALSE(utils::PReadAll(
1363 in_fd, &buf[0], buf.size(), offset, &bytes_read));
1364 TEST_AND_RETURN_FALSE(bytes_read == static_cast<ssize_t>(buf.size()));
Darin Petkov36a58222010-10-07 22:00:09 -07001365
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001366 vector<char> buf_compressed;
Darin Petkov36a58222010-10-07 22:00:09 -07001367
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001368 TEST_AND_RETURN_FALSE(BzipCompress(buf, &buf_compressed));
1369 const bool compress = buf_compressed.size() < buf.size();
1370 const vector<char>& use_buf = compress ? buf_compressed : buf;
1371 if (compress) {
1372 op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
1373 } else {
1374 op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1375 }
1376 op->set_data_offset(*data_file_size);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001377 TEST_AND_RETURN_FALSE(utils::WriteAll(fd, &use_buf[0], use_buf.size()));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001378 *data_file_size += use_buf.size();
1379 op->set_data_length(use_buf.size());
1380 Extent* dst_extent = op->add_dst_extents();
1381 dst_extent->set_start_block(offset / kBlockSize);
1382 dst_extent->set_num_blocks(chunk_size / kBlockSize);
1383 }
1384 }
1385
1386 return true;
1387}
1388
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001389bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1390 const string& old_root,
1391 const string& old_image,
1392 const string& new_root,
1393 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001394 const string& old_kernel_part,
1395 const string& new_kernel_part,
1396 const string& output_path,
1397 const string& private_key_path) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001398 int old_image_block_count = 0, old_image_block_size = 0;
1399 int new_image_block_count = 0, new_image_block_size = 0;
1400 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1401 &new_image_block_count,
1402 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001403 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001404 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1405 &old_image_block_count,
1406 &old_image_block_size));
1407 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1408 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1409 << "Old and new images have different block counts.";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001410 }
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001411 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001412 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1413
Darin Petkov7ea32332010-10-13 10:46:11 -07001414 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1415 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1416 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001417 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1418 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1419 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1420 }
1421 Graph graph;
1422 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001423
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001424 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
1425 string temp_file_path;
1426 off_t data_file_size = 0;
1427
1428 LOG(INFO) << "Reading files...";
1429
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001430 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1431
Andrew de los Reyesef017552010-10-06 17:57:52 -07001432 vector<Vertex::Index> final_order;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001433 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001434 int fd;
1435 TEST_AND_RETURN_FALSE(
1436 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
1437 TEST_AND_RETURN_FALSE(fd >= 0);
1438 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001439 if (!old_image.empty()) {
1440 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001441
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001442 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1443 &blocks,
1444 old_root,
1445 new_root,
1446 fd,
1447 &data_file_size));
1448 LOG(INFO) << "done reading normal files";
1449 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001450
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001451 graph.resize(graph.size() + 1);
1452 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1453 fd,
1454 &data_file_size,
1455 new_image,
1456 &graph.back()));
1457
1458 // Read kernel partition
1459 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1460 new_kernel_part,
1461 &kernel_ops,
1462 fd,
1463 &data_file_size));
1464
1465 LOG(INFO) << "done reading kernel";
1466 CheckGraph(graph);
1467
1468 LOG(INFO) << "Creating edges...";
1469 CreateEdges(&graph, blocks);
1470 LOG(INFO) << "Done creating edges";
1471 CheckGraph(graph);
1472
1473 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1474 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001475 fd,
1476 &data_file_size,
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001477 &final_order));
1478 } else {
1479 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001480 off_t new_image_size =
1481 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001482 TEST_AND_RETURN_FALSE(ReadFullUpdateFromDisk(&graph,
1483 new_kernel_part,
1484 new_image,
Darin Petkov7ea32332010-10-13 10:46:11 -07001485 new_image_size,
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001486 fd,
1487 &data_file_size,
1488 kFullUpdateChunkSize,
1489 &kernel_ops,
1490 &final_order));
1491 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001492 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001493
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001494 // Convert to protobuf Manifest object
1495 DeltaArchiveManifest manifest;
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001496 OperationNameMap op_name_map;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001497 CheckGraph(graph);
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001498 InstallOperationsToManifest(graph,
1499 final_order,
1500 kernel_ops,
1501 &manifest,
1502 &op_name_map);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001503 CheckGraph(graph);
1504 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001505
1506 // Reorder the data blobs with the newly ordered manifest
1507 string ordered_blobs_path;
1508 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
1509 "/tmp/CrAU_temp_data.ordered.XXXXXX",
1510 &ordered_blobs_path,
1511 false));
1512 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1513 temp_file_path,
1514 ordered_blobs_path));
1515
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001516 // Check that install op blobs are in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001517 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001518 {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001519 for (int i = 0; i < (manifest.install_operations_size() +
1520 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001521 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001522 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001523 manifest.mutable_install_operations(i) :
1524 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001525 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001526 if (op->has_data_offset()) {
1527 if (op->data_offset() != next_blob_offset) {
1528 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001529 << next_blob_offset;
1530 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001531 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001532 }
1533 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001534 }
1535
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001536 // Signatures appear at the end of the blobs. Note the offset in the
1537 // manifest
1538 if (!private_key_path.empty()) {
1539 LOG(INFO) << "Making room for signature in file";
1540 manifest.set_signatures_offset(next_blob_offset);
1541 LOG(INFO) << "set? " << manifest.has_signatures_offset();
1542 // Add a dummy op at the end to appease older clients
1543 DeltaArchiveManifest_InstallOperation* dummy_op =
1544 manifest.add_kernel_install_operations();
1545 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1546 dummy_op->set_data_offset(next_blob_offset);
1547 manifest.set_signatures_offset(next_blob_offset);
1548 uint64_t signature_blob_length = 0;
1549 TEST_AND_RETURN_FALSE(
1550 PayloadSigner::SignatureBlobLength(private_key_path,
1551 &signature_blob_length));
1552 dummy_op->set_data_length(signature_blob_length);
1553 manifest.set_signatures_size(signature_blob_length);
1554 Extent* dummy_extent = dummy_op->add_dst_extents();
1555 // Tell the dummy op to write this data to a big sparse hole
1556 dummy_extent->set_start_block(kSparseHole);
1557 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1558 kBlockSize);
1559 }
1560
Darin Petkov36a58222010-10-07 22:00:09 -07001561 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1562 new_kernel_part,
1563 old_image,
1564 new_image,
1565 &manifest));
1566
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001567 // Serialize protobuf
1568 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001569
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001570 CheckGraph(graph);
1571 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1572 CheckGraph(graph);
1573
1574 LOG(INFO) << "Writing final delta file header...";
1575 DirectFileWriter writer;
1576 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1577 O_WRONLY | O_CREAT | O_TRUNC,
1578 0644) == 0);
1579 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001580
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001581 // Write header
1582 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)) ==
Andrew de los Reyes08c4e272010-04-15 14:02:17 -07001583 static_cast<ssize_t>(strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001584
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001585 // Write version number
1586 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001587
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001588 // Write protobuf length
1589 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1590 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001591
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001592 // Write protobuf
1593 LOG(INFO) << "Writing final delta file protobuf... "
1594 << serialized_manifest.size();
1595 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
1596 serialized_manifest.size()) ==
1597 static_cast<ssize_t>(serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001598
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001599 // Append the data blobs
1600 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001601 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001602 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1603 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1604 for (;;) {
1605 char buf[kBlockSize];
1606 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1607 if (0 == rc) {
1608 // EOF
1609 break;
1610 }
1611 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
1612 TEST_AND_RETURN_FALSE(writer.Write(buf, rc) == rc);
1613 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001614
1615 // Write signature blob.
1616 if (!private_key_path.empty()) {
1617 LOG(INFO) << "Signing the update...";
1618 vector<char> signature_blob;
1619 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(output_path,
1620 private_key_path,
1621 &signature_blob));
1622 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
1623 signature_blob.size()) ==
1624 static_cast<ssize_t>(signature_blob.size()));
1625 }
1626
Darin Petkov95cf01f2010-10-12 14:59:13 -07001627 int64_t manifest_metadata_size =
1628 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001629 ReportPayloadUsage(manifest, manifest_metadata_size, op_name_map);
Darin Petkov880335c2010-10-01 15:52:53 -07001630
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001631 LOG(INFO) << "All done. Successfully created delta file.";
1632 return true;
1633}
1634
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001635const char* const kBsdiffPath = "/usr/bin/bsdiff";
1636const char* const kBspatchPath = "/usr/bin/bspatch";
1637const char* const kDeltaMagic = "CrAU";
1638
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001639}; // namespace chromeos_update_engine