blob: 21a4c03ad84b98decd39afa9c7a94bfd6e4f29d1 [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"
Darin Petkov7a22d792010-11-08 14:10:00 -080030#include "update_engine/full_update_generator.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070031#include "update_engine/graph_types.h"
32#include "update_engine/graph_utils.h"
Darin Petkov36a58222010-10-07 22:00:09 -070033#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070034#include "update_engine/payload_signer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070035#include "update_engine/subprocess.h"
36#include "update_engine/topological_sort.h"
37#include "update_engine/update_metadata.pb.h"
38#include "update_engine/utils.h"
39
40using std::make_pair;
Andrew de los Reyesef017552010-10-06 17:57:52 -070041using std::map;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070042using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070043using std::min;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -070044using std::pair;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070045using std::set;
46using std::string;
47using std::vector;
48
49namespace chromeos_update_engine {
50
51typedef DeltaDiffGenerator::Block Block;
Darin Petkov9fa7ec52010-10-18 11:45:23 -070052typedef map<const DeltaArchiveManifest_InstallOperation*,
53 const string*> OperationNameMap;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070054
55namespace {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070056const size_t kBlockSize = 4096; // bytes
Andrew de los Reyes927179d2010-12-02 11:26:48 -080057
58// TODO(adlr): switch from 1GiB to 2GiB when we no longer care about old
59// clients:
Darin Petkov9eadd642010-10-14 15:20:57 -070060const size_t kRootFSPartitionSize = 1 * 1024 * 1024 * 1024; // bytes
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070061const uint64_t kVersionNumber = 1;
Darin Petkov9eadd642010-10-14 15:20:57 -070062const uint64_t kFullUpdateChunkSize = 1024 * 1024; // bytes
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070063
Darin Petkov68c10d12010-10-14 09:24:37 -070064static const char* kInstallOperationTypes[] = {
65 "REPLACE",
66 "REPLACE_BZ",
67 "MOVE",
68 "BSDIFF"
69};
70
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070071// Stores all Extents for a file into 'out'. Returns true on success.
72bool GatherExtents(const string& path,
73 google::protobuf::RepeatedPtrField<Extent>* out) {
74 vector<Extent> extents;
75 TEST_AND_RETURN_FALSE(extent_mapper::ExtentsForFileFibmap(path, &extents));
76 DeltaDiffGenerator::StoreExtents(extents, out);
77 return true;
78}
79
80// Runs the bsdiff tool on two files and returns the resulting delta in
81// 'out'. Returns true on success.
82bool BsdiffFiles(const string& old_file,
83 const string& new_file,
84 vector<char>* out) {
85 const string kPatchFile = "/tmp/delta.patchXXXXXX";
86 string patch_file_path;
87
88 TEST_AND_RETURN_FALSE(
89 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
90
91 vector<string> cmd;
92 cmd.push_back(kBsdiffPath);
93 cmd.push_back(old_file);
94 cmd.push_back(new_file);
95 cmd.push_back(patch_file_path);
96
97 int rc = 1;
98 vector<char> patch_file;
99 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc));
100 TEST_AND_RETURN_FALSE(rc == 0);
101 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
102 unlink(patch_file_path.c_str());
103 return true;
104}
105
106// The blocks vector contains a reader and writer for each block on the
107// filesystem that's being in-place updated. We populate the reader/writer
108// fields of blocks by calling this function.
109// For each block in 'operation' that is read or written, find that block
110// in 'blocks' and set the reader/writer field to the vertex passed.
111// 'graph' is not strictly necessary, but useful for printing out
112// error messages.
113bool AddInstallOpToBlocksVector(
114 const DeltaArchiveManifest_InstallOperation& operation,
115 vector<Block>* blocks,
116 const Graph& graph,
117 Vertex::Index vertex) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700118 // See if this is already present.
119 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700120
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700121 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
122 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
123 const int extents_size =
124 (field == READER) ? operation.src_extents_size() :
125 operation.dst_extents_size();
126 const char* past_participle = (field == READER) ? "read" : "written";
127 const google::protobuf::RepeatedPtrField<Extent>& extents =
128 (field == READER) ? operation.src_extents() : operation.dst_extents();
129 Vertex::Index Block::*access_type =
130 (field == READER) ? &Block::reader : &Block::writer;
131
132 for (int i = 0; i < extents_size; i++) {
133 const Extent& extent = extents.Get(i);
134 if (extent.start_block() == kSparseHole) {
135 // Hole in sparse file. skip
136 continue;
137 }
138 for (uint64_t block = extent.start_block();
139 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700140 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
141 LOG(FATAL) << "Block " << block << " is already "
142 << past_participle << " by "
143 << (*blocks)[block].*access_type << "("
144 << graph[(*blocks)[block].*access_type].file_name
145 << ") and also " << vertex << "("
146 << graph[vertex].file_name << ")";
147 }
148 (*blocks)[block].*access_type = vertex;
149 }
150 }
151 }
152 return true;
153}
154
Andrew de los Reyesef017552010-10-06 17:57:52 -0700155// For a given regular file which must exist at new_root + path, and
156// may exist at old_root + path, creates a new InstallOperation and
157// adds it to the graph. Also, populates the |blocks| array as
158// necessary, if |blocks| is non-NULL. Also, writes the data
159// necessary to send the file down to the client into data_fd, which
160// has length *data_file_size. *data_file_size is updated
161// appropriately. If |existing_vertex| is no kInvalidIndex, use that
162// rather than allocating a new vertex. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700163bool DeltaReadFile(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700164 Vertex::Index existing_vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700165 vector<Block>* blocks,
166 const string& old_root,
167 const string& new_root,
168 const string& path, // within new_root
169 int data_fd,
170 off_t* data_file_size) {
171 vector<char> data;
172 DeltaArchiveManifest_InstallOperation operation;
173
174 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_root + path,
175 new_root + path,
176 &data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700177 &operation,
178 true));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700179
180 // Write the data
181 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
182 operation.set_data_offset(*data_file_size);
183 operation.set_data_length(data.size());
184 }
185
186 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
187 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700188
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700189 // Now, insert into graph and blocks vector
Andrew de los Reyesef017552010-10-06 17:57:52 -0700190 Vertex::Index vertex = existing_vertex;
191 if (vertex == Vertex::kInvalidIndex) {
192 graph->resize(graph->size() + 1);
193 vertex = graph->size() - 1;
194 }
195 (*graph)[vertex].op = operation;
196 CHECK((*graph)[vertex].op.has_type());
197 (*graph)[vertex].file_name = path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700198
Andrew de los Reyesef017552010-10-06 17:57:52 -0700199 if (blocks)
200 TEST_AND_RETURN_FALSE(AddInstallOpToBlocksVector((*graph)[vertex].op,
201 blocks,
202 *graph,
203 vertex));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700204 return true;
205}
206
207// For each regular file within new_root, creates a node in the graph,
208// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
209// and writes any necessary data to the end of data_fd.
210bool DeltaReadFiles(Graph* graph,
211 vector<Block>* blocks,
212 const string& old_root,
213 const string& new_root,
214 int data_fd,
215 off_t* data_file_size) {
216 set<ino_t> visited_inodes;
217 for (FilesystemIterator fs_iter(new_root,
218 utils::SetWithValue<string>("/lost+found"));
219 !fs_iter.IsEnd(); fs_iter.Increment()) {
220 if (!S_ISREG(fs_iter.GetStat().st_mode))
221 continue;
222
223 // Make sure we visit each inode only once.
224 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
225 continue;
226 visited_inodes.insert(fs_iter.GetStat().st_ino);
227 if (fs_iter.GetStat().st_size == 0)
228 continue;
229
230 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700231
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700232 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700233 Vertex::kInvalidIndex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700234 blocks,
235 old_root,
236 new_root,
237 fs_iter.GetPartialPath(),
238 data_fd,
239 data_file_size));
240 }
241 return true;
242}
243
Andrew de los Reyesef017552010-10-06 17:57:52 -0700244// This class allocates non-existent temp blocks, starting from
245// kTempBlockStart. Other code is responsible for converting these
246// temp blocks into real blocks, as the client can't read or write to
247// these blocks.
248class DummyExtentAllocator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700249 public:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700250 explicit DummyExtentAllocator()
251 : next_block_(kTempBlockStart) {}
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700252 vector<Extent> Allocate(const uint64_t block_count) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700253 vector<Extent> ret(1);
254 ret[0].set_start_block(next_block_);
255 ret[0].set_num_blocks(block_count);
256 next_block_ += block_count;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700257 return ret;
258 }
259 private:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700260 uint64_t next_block_;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700261};
262
263// Reads blocks from image_path that are not yet marked as being written
264// in the blocks array. These blocks that remain are non-file-data blocks.
265// In the future we might consider intelligent diffing between this data
266// and data in the previous image, but for now we just bzip2 compress it
267// and include it in the update.
268// Creates a new node in the graph to write these blocks and writes the
269// appropriate blob to blobs_fd. Reads and updates blobs_length;
270bool ReadUnwrittenBlocks(const vector<Block>& blocks,
271 int blobs_fd,
272 off_t* blobs_length,
273 const string& image_path,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700274 Vertex* vertex) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700275 vertex->file_name = "<rootfs-non-file-data>";
276
Andrew de los Reyesef017552010-10-06 17:57:52 -0700277 DeltaArchiveManifest_InstallOperation* out_op = &vertex->op;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700278 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
279 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
280 ScopedFdCloser image_fd_closer(&image_fd);
281
282 string temp_file_path;
283 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX",
284 &temp_file_path,
285 NULL));
286
287 FILE* file = fopen(temp_file_path.c_str(), "w");
288 TEST_AND_RETURN_FALSE(file);
289 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700290
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700291 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
292 file,
293 9, // max compression
294 0, // verbosity
295 0); // default work factor
296 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700297
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700298 vector<Extent> extents;
299 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700300
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700301 LOG(INFO) << "Appending left over blocks to extents";
302 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
303 if (blocks[i].writer != Vertex::kInvalidIndex)
304 continue;
Andrew de los Reyesef017552010-10-06 17:57:52 -0700305 if (blocks[i].reader != Vertex::kInvalidIndex) {
306 graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i);
307 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700308 graph_utils::AppendBlockToExtents(&extents, i);
309 block_count++;
310 }
311
312 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
313 // so we arbitrarily set it to 1024 * kBlockSize.
314 vector<char> buf(1024 * kBlockSize);
315
316 LOG(INFO) << "Reading left over blocks";
317 vector<Block>::size_type blocks_copied_count = 0;
318
319 // For each extent in extents, write the data into BZ2_bzWrite which
320 // sends it to an output file.
321 // We use the temporary buffer 'buf' to hold the data, which may be
322 // smaller than the extent, so in that case we have to loop to get
323 // the extent's data (that's the inner while loop).
324 for (vector<Extent>::const_iterator it = extents.begin();
325 it != extents.end(); ++it) {
326 vector<Block>::size_type blocks_read = 0;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800327 float printed_progress = -1;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700328 while (blocks_read < it->num_blocks()) {
329 const int copy_block_cnt =
330 min(buf.size() / kBlockSize,
331 static_cast<vector<char>::size_type>(
332 it->num_blocks() - blocks_read));
333 ssize_t rc = pread(image_fd,
334 &buf[0],
335 copy_block_cnt * kBlockSize,
336 (it->start_block() + blocks_read) * kBlockSize);
337 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
338 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
339 copy_block_cnt * kBlockSize);
340 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
341 TEST_AND_RETURN_FALSE(err == BZ_OK);
342 blocks_read += copy_block_cnt;
343 blocks_copied_count += copy_block_cnt;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800344 float current_progress =
345 static_cast<float>(blocks_copied_count) / block_count;
346 if (printed_progress + 0.1 < current_progress ||
347 blocks_copied_count == block_count) {
348 LOG(INFO) << "progress: " << current_progress;
349 printed_progress = current_progress;
350 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700351 }
352 }
353 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
354 TEST_AND_RETURN_FALSE(err == BZ_OK);
355 bz_file = NULL;
356 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
357 file = NULL;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700358
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700359 vector<char> compressed_data;
360 LOG(INFO) << "Reading compressed data off disk";
361 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
362 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700363
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700364 // Add node to graph to write these blocks
365 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
366 out_op->set_data_offset(*blobs_length);
367 out_op->set_data_length(compressed_data.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700368 LOG(INFO) << "Rootfs non-data blocks compressed take up "
369 << compressed_data.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700370 *blobs_length += compressed_data.size();
371 out_op->set_dst_length(kBlockSize * block_count);
372 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700373
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700374 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
375 &compressed_data[0],
376 compressed_data.size()));
377 LOG(INFO) << "done with extra blocks";
378 return true;
379}
380
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700381// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700382// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700383bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
384 uint64_t value_be = htobe64(value);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700385 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)) ==
386 sizeof(value_be));
387 return true;
388}
389
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700390// Adds each operation from |graph| to |out_manifest| in the order specified by
391// |order| while building |out_op_name_map| with operation to name
392// mappings. Adds all |kernel_ops| to |out_manifest|. Filters out no-op
393// operations.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700394void InstallOperationsToManifest(
395 const Graph& graph,
396 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700397 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700398 DeltaArchiveManifest* out_manifest,
399 OperationNameMap* out_op_name_map) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700400 for (vector<Vertex::Index>::const_iterator it = order.begin();
401 it != order.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700402 const Vertex& vertex = graph[*it];
403 const DeltaArchiveManifest_InstallOperation& add_op = vertex.op;
404 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
405 continue;
406 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700407 DeltaArchiveManifest_InstallOperation* op =
408 out_manifest->add_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700409 *op = add_op;
410 (*out_op_name_map)[op] = &vertex.file_name;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700411 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700412 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
413 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700414 const DeltaArchiveManifest_InstallOperation& add_op = *it;
415 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
416 continue;
417 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700418 DeltaArchiveManifest_InstallOperation* op =
419 out_manifest->add_kernel_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700420 *op = add_op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700421 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700422}
423
424void CheckGraph(const Graph& graph) {
425 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
426 CHECK(it->op.has_type());
427 }
428}
429
Darin Petkov68c10d12010-10-14 09:24:37 -0700430// Delta compresses a kernel partition |new_kernel_part| with knowledge of the
431// old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty
432// string, generates a full update of the partition.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700433bool DeltaCompressKernelPartition(
434 const string& old_kernel_part,
435 const string& new_kernel_part,
436 vector<DeltaArchiveManifest_InstallOperation>* ops,
437 int blobs_fd,
438 off_t* blobs_length) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700439 LOG(INFO) << "Delta compressing kernel partition...";
Darin Petkov68c10d12010-10-14 09:24:37 -0700440 LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update...";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700441
442 // Add a new install operation
443 ops->resize(1);
444 DeltaArchiveManifest_InstallOperation* op = &(*ops)[0];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700445
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700446 vector<char> data;
Darin Petkov68c10d12010-10-14 09:24:37 -0700447 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_kernel_part,
448 new_kernel_part,
449 &data,
450 op,
451 false));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700452
Darin Petkov68c10d12010-10-14 09:24:37 -0700453 // Write the data
454 if (op->type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
455 op->set_data_offset(*blobs_length);
456 op->set_data_length(data.size());
457 }
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700458
Darin Petkov68c10d12010-10-14 09:24:37 -0700459 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
460 *blobs_length += data.size();
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700461
Darin Petkov68c10d12010-10-14 09:24:37 -0700462 LOG(INFO) << "Done delta compressing kernel partition: "
463 << kInstallOperationTypes[op->type()];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700464 return true;
465}
466
Darin Petkov880335c2010-10-01 15:52:53 -0700467struct DeltaObject {
468 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
469 : name(in_name),
470 type(in_type),
471 size(in_size) {}
472 bool operator <(const DeltaObject& object) const {
Darin Petkovd43d6902010-10-14 11:17:50 -0700473 return (size != object.size) ? (size < object.size) : (name < object.name);
Darin Petkov880335c2010-10-01 15:52:53 -0700474 }
475 string name;
476 int type;
477 off_t size;
478};
479
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700480void ReportPayloadUsage(const DeltaArchiveManifest& manifest,
481 const int64_t manifest_metadata_size,
482 const OperationNameMap& op_name_map) {
Darin Petkov880335c2010-10-01 15:52:53 -0700483 vector<DeltaObject> objects;
484 off_t total_size = 0;
485
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700486 // Rootfs install operations.
487 for (int i = 0; i < manifest.install_operations_size(); ++i) {
488 const DeltaArchiveManifest_InstallOperation& op =
489 manifest.install_operations(i);
490 objects.push_back(DeltaObject(*op_name_map.find(&op)->second,
491 op.type(),
492 op.data_length()));
493 total_size += op.data_length();
Darin Petkov880335c2010-10-01 15:52:53 -0700494 }
495
Darin Petkov880335c2010-10-01 15:52:53 -0700496 // Kernel install operations.
497 for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) {
498 const DeltaArchiveManifest_InstallOperation& op =
499 manifest.kernel_install_operations(i);
500 objects.push_back(DeltaObject(StringPrintf("<kernel-operation-%d>", i),
501 op.type(),
502 op.data_length()));
503 total_size += op.data_length();
504 }
505
Darin Petkov95cf01f2010-10-12 14:59:13 -0700506 objects.push_back(DeltaObject("<manifest-metadata>",
507 -1,
508 manifest_metadata_size));
509 total_size += manifest_metadata_size;
510
Darin Petkov880335c2010-10-01 15:52:53 -0700511 std::sort(objects.begin(), objects.end());
512
513 static const char kFormatString[] = "%6.2f%% %10llu %-10s %s\n";
514 for (vector<DeltaObject>::const_iterator it = objects.begin();
515 it != objects.end(); ++it) {
516 const DeltaObject& object = *it;
517 fprintf(stderr, kFormatString,
518 object.size * 100.0 / total_size,
519 object.size,
Darin Petkov95cf01f2010-10-12 14:59:13 -0700520 object.type >= 0 ? kInstallOperationTypes[object.type] : "-",
Darin Petkov880335c2010-10-01 15:52:53 -0700521 object.name.c_str());
522 }
523 fprintf(stderr, kFormatString, 100.0, total_size, "", "<total>");
524}
525
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700526} // namespace {}
527
528bool DeltaDiffGenerator::ReadFileToDiff(
529 const string& old_filename,
530 const string& new_filename,
531 vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700532 DeltaArchiveManifest_InstallOperation* out_op,
533 bool gather_extents) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700534 // Read new data in
535 vector<char> new_data;
536 TEST_AND_RETURN_FALSE(utils::ReadFile(new_filename, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700537
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700538 TEST_AND_RETURN_FALSE(!new_data.empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700539
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700540 vector<char> new_data_bz;
541 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
542 CHECK(!new_data_bz.empty());
543
544 vector<char> data; // Data blob that will be written to delta file.
545
546 DeltaArchiveManifest_InstallOperation operation;
547 size_t current_best_size = 0;
548 if (new_data.size() <= new_data_bz.size()) {
549 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
550 current_best_size = new_data.size();
551 data = new_data;
552 } else {
553 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
554 current_best_size = new_data_bz.size();
555 data = new_data_bz;
556 }
557
558 // Do we have an original file to consider?
559 struct stat old_stbuf;
Darin Petkov68c10d12010-10-14 09:24:37 -0700560 bool no_original = old_filename.empty();
561 if (!no_original && 0 != stat(old_filename.c_str(), &old_stbuf)) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700562 // If stat-ing the old file fails, it should be because it doesn't exist.
563 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
Darin Petkov68c10d12010-10-14 09:24:37 -0700564 no_original = true;
565 }
566 if (!no_original) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700567 // Read old data
568 vector<char> old_data;
569 TEST_AND_RETURN_FALSE(utils::ReadFile(old_filename, &old_data));
570 if (old_data == new_data) {
571 // No change in data.
572 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
573 current_best_size = 0;
574 data.clear();
575 } else {
576 // Try bsdiff of old to new data
577 vector<char> bsdiff_delta;
578 TEST_AND_RETURN_FALSE(
579 BsdiffFiles(old_filename, new_filename, &bsdiff_delta));
580 CHECK_GT(bsdiff_delta.size(), 0);
581 if (bsdiff_delta.size() < current_best_size) {
582 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
583 current_best_size = bsdiff_delta.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700584
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700585 data = bsdiff_delta;
586 }
587 }
588 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700589
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700590 // Set parameters of the operations
591 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700592
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700593 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
594 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Darin Petkov68c10d12010-10-14 09:24:37 -0700595 if (gather_extents) {
596 TEST_AND_RETURN_FALSE(
597 GatherExtents(old_filename, operation.mutable_src_extents()));
598 } else {
599 Extent* src_extent = operation.add_src_extents();
600 src_extent->set_start_block(0);
601 src_extent->set_num_blocks(
602 (old_stbuf.st_size + kBlockSize - 1) / kBlockSize);
603 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700604 operation.set_src_length(old_stbuf.st_size);
605 }
606
Darin Petkov68c10d12010-10-14 09:24:37 -0700607 if (gather_extents) {
608 TEST_AND_RETURN_FALSE(
609 GatherExtents(new_filename, operation.mutable_dst_extents()));
610 } else {
611 Extent* dst_extent = operation.add_dst_extents();
612 dst_extent->set_start_block(0);
613 dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize);
614 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700615 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700616
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700617 out_data->swap(data);
618 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700619
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700620 return true;
621}
622
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700623bool DeltaDiffGenerator::InitializePartitionInfo(bool is_kernel,
624 const string& partition,
625 PartitionInfo* info) {
Darin Petkov7ea32332010-10-13 10:46:11 -0700626 int64_t size = 0;
627 if (is_kernel) {
628 size = utils::FileSize(partition);
629 } else {
630 int block_count = 0, block_size = 0;
631 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition,
632 &block_count,
633 &block_size));
634 size = static_cast<int64_t>(block_count) * block_size;
635 }
636 TEST_AND_RETURN_FALSE(size > 0);
Darin Petkov36a58222010-10-07 22:00:09 -0700637 info->set_size(size);
638 OmahaHashCalculator hasher;
Darin Petkov7ea32332010-10-13 10:46:11 -0700639 TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size);
Darin Petkov36a58222010-10-07 22:00:09 -0700640 TEST_AND_RETURN_FALSE(hasher.Finalize());
641 const vector<char>& hash = hasher.raw_hash();
642 info->set_hash(hash.data(), hash.size());
Darin Petkovd43d6902010-10-14 11:17:50 -0700643 LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash();
Darin Petkov36a58222010-10-07 22:00:09 -0700644 return true;
645}
646
647bool InitializePartitionInfos(const string& old_kernel,
648 const string& new_kernel,
649 const string& old_rootfs,
650 const string& new_rootfs,
651 DeltaArchiveManifest* manifest) {
Darin Petkovd43d6902010-10-14 11:17:50 -0700652 if (!old_kernel.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700653 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
654 true,
655 old_kernel,
656 manifest->mutable_old_kernel_info()));
Darin Petkovd43d6902010-10-14 11:17:50 -0700657 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700658 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
659 true,
660 new_kernel,
661 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700662 if (!old_rootfs.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700663 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
664 false,
665 old_rootfs,
666 manifest->mutable_old_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700667 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700668 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
669 false,
670 new_rootfs,
671 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700672 return true;
673}
674
Andrew de los Reyesef017552010-10-06 17:57:52 -0700675namespace {
676
677// Takes a collection (vector or RepeatedPtrField) of Extent and
678// returns a vector of the blocks referenced, in order.
679template<typename T>
680vector<uint64_t> ExpandExtents(const T& extents) {
681 vector<uint64_t> ret;
682 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
683 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700684 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700685 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700686 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700687 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700688 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700689 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700690 }
691 }
692 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700693 return ret;
694}
695
696// Takes a vector of blocks and returns an equivalent vector of Extent
697// objects.
698vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
699 vector<Extent> new_extents;
700 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
701 it != e; ++it) {
702 graph_utils::AppendBlockToExtents(&new_extents, *it);
703 }
704 return new_extents;
705}
706
707} // namespace {}
708
709void DeltaDiffGenerator::SubstituteBlocks(
710 Vertex* vertex,
711 const vector<Extent>& remove_extents,
712 const vector<Extent>& replace_extents) {
713 // First, expand out the blocks that op reads from
714 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700715 {
716 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700717 vector<uint64_t> remove_extents_expanded =
718 ExpandExtents(remove_extents);
719 vector<uint64_t> replace_extents_expanded =
720 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700721 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700722 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700723 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700724 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700725 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
726 }
727 utils::ApplyMap(&read_blocks, conversion);
728 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
729 e = vertex->out_edges.end(); it != e; ++it) {
730 vector<uint64_t> write_before_deps_expanded =
731 ExpandExtents(it->second.write_extents);
732 utils::ApplyMap(&write_before_deps_expanded, conversion);
733 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700734 }
735 }
736 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700737 vertex->op.clear_src_extents();
738 vector<Extent> new_extents = CompressExtents(read_blocks);
739 DeltaDiffGenerator::StoreExtents(new_extents,
740 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700741}
742
743bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700744 const set<Edge>& edges,
745 vector<CutEdgeVertexes>* out_cuts) {
746 DummyExtentAllocator scratch_allocator;
747 vector<CutEdgeVertexes> cuts;
748 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700749
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700750 uint64_t scratch_blocks_used = 0;
751 for (set<Edge>::const_iterator it = edges.begin();
752 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700753 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700754 vector<Extent> old_extents =
755 (*graph)[it->first].out_edges[it->second].extents;
756 // Choose some scratch space
757 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700758 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700759 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
760 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700761 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700762 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700763 cuts.back().old_src = it->first;
764 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700765
Andrew de los Reyesef017552010-10-06 17:57:52 -0700766 EdgeProperties& cut_edge_properties =
767 (*graph)[it->first].out_edges.find(it->second)->second;
768
769 // This should never happen, as we should only be cutting edges between
770 // real file nodes, and write-before relationships are created from
771 // a real file node to a temp copy node:
772 CHECK(cut_edge_properties.write_extents.empty())
773 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700774
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700775 // make node depend on the copy operation
776 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700777 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700778
779 // Set src/dst extents and other proto variables for copy operation
780 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
781 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700782 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700783 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700784 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700785 graph->back().op.mutable_dst_extents());
786 graph->back().op.set_src_length(
787 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
788 graph->back().op.set_dst_length(graph->back().op.src_length());
789
790 // make the dest node read from the scratch space
791 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700792 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700793 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700794 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700795
796 // delete the old edge
797 CHECK_EQ(1, (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700798
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700799 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700800 EdgeProperties write_before_edge_properties;
801 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
802 (*graph)[it->second].out_edges.insert(
803 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700804 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700805 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700806 return true;
807}
808
809// Stores all Extents in 'extents' into 'out'.
810void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700811 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700812 google::protobuf::RepeatedPtrField<Extent>* out) {
813 for (vector<Extent>::const_iterator it = extents.begin();
814 it != extents.end(); ++it) {
815 Extent* new_extent = out->Add();
816 *new_extent = *it;
817 }
818}
819
820// Creates all the edges for the graph. Writers of a block point to
821// readers of the same block. This is because for an edge A->B, B
822// must complete before A executes.
823void DeltaDiffGenerator::CreateEdges(Graph* graph,
824 const vector<Block>& blocks) {
825 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
826 // Blocks with both a reader and writer get an edge
827 if (blocks[i].reader == Vertex::kInvalidIndex ||
828 blocks[i].writer == Vertex::kInvalidIndex)
829 continue;
830 // Don't have a node depend on itself
831 if (blocks[i].reader == blocks[i].writer)
832 continue;
833 // See if there's already an edge we can add onto
834 Vertex::EdgeMap::iterator edge_it =
835 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
836 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
837 // No existing edge. Create one
838 (*graph)[blocks[i].writer].out_edges.insert(
839 make_pair(blocks[i].reader, EdgeProperties()));
840 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700841 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700842 }
843 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
844 }
845}
846
Andrew de los Reyesef017552010-10-06 17:57:52 -0700847namespace {
848
849class SortCutsByTopoOrderLess {
850 public:
851 SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table)
852 : table_(table) {}
853 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
854 return table_[a.old_dst] < table_[b.old_dst];
855 }
856 private:
857 vector<vector<Vertex::Index>::size_type>& table_;
858};
859
860} // namespace {}
861
862void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
863 vector<Vertex::Index>& op_indexes,
864 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
865 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
866 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
867 i != e; ++i) {
868 Vertex::Index node = op_indexes[i];
869 if (table.size() < (node + 1)) {
870 table.resize(node + 1);
871 }
872 table[node] = i;
873 }
874 reverse_op_indexes->swap(table);
875}
876
877void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes,
878 vector<CutEdgeVertexes>* cuts) {
879 // first, make a reverse lookup table.
880 vector<vector<Vertex::Index>::size_type> table;
881 GenerateReverseTopoOrderMap(op_indexes, &table);
882 SortCutsByTopoOrderLess less(table);
883 sort(cuts->begin(), cuts->end(), less);
884}
885
886void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
887 vector<Vertex::Index>* op_indexes) {
888 vector<Vertex::Index> ret;
889 vector<Vertex::Index> full_ops;
890 ret.reserve(op_indexes->size());
891 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
892 ++i) {
893 DeltaArchiveManifest_InstallOperation_Type type =
894 (*graph)[(*op_indexes)[i]].op.type();
895 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
896 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
897 full_ops.push_back((*op_indexes)[i]);
898 } else {
899 ret.push_back((*op_indexes)[i]);
900 }
901 }
902 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
903 << (full_ops.size() + ret.size()) << " total ops.";
904 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
905 op_indexes->swap(ret);
906}
907
908namespace {
909
910template<typename T>
911bool TempBlocksExistInExtents(const T& extents) {
912 for (int i = 0, e = extents.size(); i < e; ++i) {
913 Extent extent = graph_utils::GetElement(extents, i);
914 uint64_t start = extent.start_block();
915 uint64_t num = extent.num_blocks();
916 if (start == kSparseHole)
917 continue;
918 if (start >= kTempBlockStart ||
919 (start + num) >= kTempBlockStart) {
920 LOG(ERROR) << "temp block!";
921 LOG(ERROR) << "start: " << start << ", num: " << num;
922 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
923 LOG(ERROR) << "returning true";
924 return true;
925 }
926 // check for wrap-around, which would be a bug:
927 CHECK(start <= (start + num));
928 }
929 return false;
930}
931
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700932// Convertes the cuts, which must all have the same |old_dst| member,
933// to full. It does this by converting the |old_dst| to REPLACE or
934// REPLACE_BZ, dropping all incoming edges to |old_dst|, and marking
935// all temp nodes invalid.
936bool ConvertCutsToFull(
937 Graph* graph,
938 const string& new_root,
939 int data_fd,
940 off_t* data_file_size,
941 vector<Vertex::Index>* op_indexes,
942 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
943 const vector<CutEdgeVertexes>& cuts) {
944 CHECK(!cuts.empty());
945 set<Vertex::Index> deleted_nodes;
946 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
947 e = cuts.end(); it != e; ++it) {
948 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp(
949 graph,
950 *it,
951 new_root,
952 data_fd,
953 data_file_size));
954 deleted_nodes.insert(it->new_vertex);
955 }
956 deleted_nodes.insert(cuts[0].old_dst);
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700957
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700958 vector<Vertex::Index> new_op_indexes;
959 new_op_indexes.reserve(op_indexes->size());
960 for (vector<Vertex::Index>::iterator it = op_indexes->begin(),
961 e = op_indexes->end(); it != e; ++it) {
962 if (utils::SetContainsKey(deleted_nodes, *it))
963 continue;
964 new_op_indexes.push_back(*it);
965 }
966 new_op_indexes.push_back(cuts[0].old_dst);
967 op_indexes->swap(new_op_indexes);
968 DeltaDiffGenerator::GenerateReverseTopoOrderMap(*op_indexes,
969 reverse_op_indexes);
970 return true;
971}
972
973// Tries to assign temp blocks for a collection of cuts, all of which share
974// the same old_dst member. If temp blocks can't be found, old_dst will be
975// converted to a REPLACE or REPLACE_BZ operation. Returns true on success,
976// which can happen even if blocks are converted to full. Returns false
977// on exceptional error cases.
978bool AssignBlockForAdjoiningCuts(
979 Graph* graph,
980 const string& new_root,
981 int data_fd,
982 off_t* data_file_size,
983 vector<Vertex::Index>* op_indexes,
984 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
985 const vector<CutEdgeVertexes>& cuts) {
986 CHECK(!cuts.empty());
987 const Vertex::Index old_dst = cuts[0].old_dst;
988 // Calculate # of blocks needed
989 uint64_t blocks_needed = 0;
990 map<const CutEdgeVertexes*, uint64_t> cuts_blocks_needed;
991 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
992 e = cuts.end(); it != e; ++it) {
993 uint64_t cut_blocks_needed = 0;
994 for (vector<Extent>::const_iterator jt = it->tmp_extents.begin(),
995 je = it->tmp_extents.end(); jt != je; ++jt) {
996 cut_blocks_needed += jt->num_blocks();
997 }
998 blocks_needed += cut_blocks_needed;
999 cuts_blocks_needed[&*it] = cut_blocks_needed;
1000 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001001
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001002 // Find enough blocks
1003 ExtentRanges scratch_ranges;
1004 // Each block that's supplying temp blocks and the corresponding blocks:
1005 typedef vector<pair<Vertex::Index, ExtentRanges> > SupplierVector;
1006 SupplierVector block_suppliers;
1007 uint64_t scratch_blocks_found = 0;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001008 for (vector<Vertex::Index>::size_type i = (*reverse_op_indexes)[old_dst] + 1,
1009 e = op_indexes->size(); i < e; ++i) {
1010 Vertex::Index test_node = (*op_indexes)[i];
1011 if (!(*graph)[test_node].valid)
1012 continue;
1013 // See if this node has sufficient blocks
1014 ExtentRanges ranges;
1015 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
1016 ranges.SubtractExtent(ExtentForRange(
1017 kTempBlockStart, kSparseHole - kTempBlockStart));
1018 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
1019 // For now, for simplicity, subtract out all blocks in read-before
1020 // dependencies.
1021 for (Vertex::EdgeMap::const_iterator edge_i =
1022 (*graph)[test_node].out_edges.begin(),
1023 edge_e = (*graph)[test_node].out_edges.end();
1024 edge_i != edge_e; ++edge_i) {
1025 ranges.SubtractExtents(edge_i->second.extents);
1026 }
1027 if (ranges.blocks() == 0)
1028 continue;
1029
1030 if (ranges.blocks() + scratch_blocks_found > blocks_needed) {
1031 // trim down ranges
1032 vector<Extent> new_ranges = ranges.GetExtentsForBlockCount(
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001033 blocks_needed - scratch_blocks_found);
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001034 ranges = ExtentRanges();
1035 ranges.AddExtents(new_ranges);
1036 }
1037 scratch_ranges.AddRanges(ranges);
1038 block_suppliers.push_back(make_pair(test_node, ranges));
1039 scratch_blocks_found += ranges.blocks();
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001040 if (scratch_ranges.blocks() >= blocks_needed)
1041 break;
1042 }
1043 if (scratch_ranges.blocks() < blocks_needed) {
1044 LOG(INFO) << "Unable to find sufficient scratch";
1045 TEST_AND_RETURN_FALSE(ConvertCutsToFull(graph,
1046 new_root,
1047 data_fd,
1048 data_file_size,
1049 op_indexes,
1050 reverse_op_indexes,
1051 cuts));
1052 return true;
1053 }
1054 // Use the scratch we found
1055 TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found);
1056
1057 // Make all the suppliers depend on this node
1058 for (SupplierVector::iterator it = block_suppliers.begin(),
1059 e = block_suppliers.end(); it != e; ++it) {
1060 graph_utils::AddReadBeforeDepExtents(
1061 &(*graph)[it->first],
1062 old_dst,
1063 it->second.GetExtentsForBlockCount(it->second.blocks()));
1064 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001065
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001066 // Replace temp blocks in each cut
1067 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1068 e = cuts.end(); it != e; ++it) {
1069 vector<Extent> real_extents =
1070 scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]);
1071 scratch_ranges.SubtractExtents(real_extents);
1072
1073 // Fix the old dest node w/ the real blocks
1074 DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst],
1075 it->tmp_extents,
1076 real_extents);
1077
1078 // Fix the new node w/ the real blocks. Since the new node is just a
1079 // copy operation, we can replace all the dest extents w/ the real
1080 // blocks.
1081 DeltaArchiveManifest_InstallOperation *op =
1082 &(*graph)[it->new_vertex].op;
1083 op->clear_dst_extents();
1084 DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents());
1085 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001086 return true;
1087}
1088
Andrew de los Reyesef017552010-10-06 17:57:52 -07001089} // namespace {}
1090
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001091// Returns true if |op| is a no-op operation that doesn't do any useful work
1092// (e.g., a move operation that copies blocks onto themselves).
1093bool DeltaDiffGenerator::IsNoopOperation(
1094 const DeltaArchiveManifest_InstallOperation& op) {
1095 return (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE &&
1096 ExpandExtents(op.src_extents()) == ExpandExtents(op.dst_extents()));
1097}
1098
Andrew de los Reyesef017552010-10-06 17:57:52 -07001099bool DeltaDiffGenerator::AssignTempBlocks(
1100 Graph* graph,
1101 const string& new_root,
1102 int data_fd,
1103 off_t* data_file_size,
1104 vector<Vertex::Index>* op_indexes,
1105 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001106 const vector<CutEdgeVertexes>& cuts) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001107 CHECK(!cuts.empty());
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001108
1109 // group of cuts w/ the same old_dst:
1110 vector<CutEdgeVertexes> cuts_group;
1111
Andrew de los Reyesef017552010-10-06 17:57:52 -07001112 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
1113 true ; --i) {
1114 LOG(INFO) << "Fixing temp blocks in cut " << i
1115 << ": old dst: " << cuts[i].old_dst << " new vertex: "
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001116 << cuts[i].new_vertex << " path: "
1117 << (*graph)[cuts[i].old_dst].file_name;
1118
1119 if (cuts_group.empty() || (cuts_group[0].old_dst == cuts[i].old_dst)) {
1120 cuts_group.push_back(cuts[i]);
1121 } else {
1122 CHECK(!cuts_group.empty());
1123 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1124 new_root,
1125 data_fd,
1126 data_file_size,
1127 op_indexes,
1128 reverse_op_indexes,
1129 cuts_group));
1130 cuts_group.clear();
1131 cuts_group.push_back(cuts[i]);
Andrew de los Reyesef017552010-10-06 17:57:52 -07001132 }
Darin Petkov36a58222010-10-07 22:00:09 -07001133
Andrew de los Reyesef017552010-10-06 17:57:52 -07001134 if (i == e) {
1135 // break out of for() loop
1136 break;
1137 }
1138 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001139 CHECK(!cuts_group.empty());
1140 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1141 new_root,
1142 data_fd,
1143 data_file_size,
1144 op_indexes,
1145 reverse_op_indexes,
1146 cuts_group));
Andrew de los Reyesef017552010-10-06 17:57:52 -07001147 return true;
1148}
1149
1150bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1151 size_t idx = 0;
1152 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1153 ++it, ++idx) {
1154 if (!it->valid)
1155 continue;
1156 const DeltaArchiveManifest_InstallOperation& op = it->op;
1157 if (TempBlocksExistInExtents(op.dst_extents()) ||
1158 TempBlocksExistInExtents(op.src_extents())) {
1159 LOG(INFO) << "bad extents in node " << idx;
1160 LOG(INFO) << "so yeah";
1161 return false;
1162 }
1163
1164 // Check out-edges:
1165 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1166 je = it->out_edges.end(); jt != je; ++jt) {
1167 if (TempBlocksExistInExtents(jt->second.extents) ||
1168 TempBlocksExistInExtents(jt->second.write_extents)) {
1169 LOG(INFO) << "bad out edge in node " << idx;
1170 LOG(INFO) << "so yeah";
1171 return false;
1172 }
1173 }
1174 }
1175 return true;
1176}
1177
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001178bool DeltaDiffGenerator::ReorderDataBlobs(
1179 DeltaArchiveManifest* manifest,
1180 const std::string& data_blobs_path,
1181 const std::string& new_data_blobs_path) {
1182 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1183 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1184 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001185
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001186 DirectFileWriter writer;
1187 TEST_AND_RETURN_FALSE(
1188 writer.Open(new_data_blobs_path.c_str(),
1189 O_WRONLY | O_TRUNC | O_CREAT,
1190 0644) == 0);
1191 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001192 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001193
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001194 for (int i = 0; i < (manifest->install_operations_size() +
1195 manifest->kernel_install_operations_size()); i++) {
1196 DeltaArchiveManifest_InstallOperation* op = NULL;
1197 if (i < manifest->install_operations_size()) {
1198 op = manifest->mutable_install_operations(i);
1199 } else {
1200 op = manifest->mutable_kernel_install_operations(
1201 i - manifest->install_operations_size());
1202 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001203 if (!op->has_data_offset())
1204 continue;
1205 CHECK(op->has_data_length());
1206 vector<char> buf(op->data_length());
1207 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1208 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1209
1210 op->set_data_offset(out_file_size);
1211 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()) ==
1212 static_cast<ssize_t>(buf.size()));
1213 out_file_size += buf.size();
1214 }
1215 return true;
1216}
1217
Andrew de los Reyesef017552010-10-06 17:57:52 -07001218bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1219 const CutEdgeVertexes& cut,
1220 const string& new_root,
1221 int data_fd,
1222 off_t* data_file_size) {
1223 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001224
Andrew de los Reyesef017552010-10-06 17:57:52 -07001225 // Keep all outgoing edges
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001226 if ((*graph)[cut.old_dst].op.type() !=
1227 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ &&
1228 (*graph)[cut.old_dst].op.type() !=
1229 DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
1230 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1231 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001232
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001233 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1234 cut.old_dst,
1235 NULL,
1236 "/-!@:&*nonexistent_path",
1237 new_root,
1238 (*graph)[cut.old_dst].file_name,
1239 data_fd,
1240 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001241
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001242 (*graph)[cut.old_dst].out_edges = out_edges;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001243
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001244 // Right now we don't have doubly-linked edges, so we have to scan
1245 // the whole graph.
1246 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1247 }
Andrew de los Reyesef017552010-10-06 17:57:52 -07001248
1249 // Delete temp node
1250 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1251 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1252 (*graph)[cut.old_dst].out_edges.end());
1253 (*graph)[cut.new_vertex].valid = false;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001254 LOG(INFO) << "marked node invalid: " << cut.new_vertex;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001255 return true;
1256}
1257
1258bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1259 const string& new_root,
1260 int fd,
1261 off_t* data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001262 vector<Vertex::Index>* final_order,
1263 Vertex::Index scratch_vertex) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001264 CycleBreaker cycle_breaker;
1265 LOG(INFO) << "Finding cycles...";
1266 set<Edge> cut_edges;
1267 cycle_breaker.BreakCycles(*graph, &cut_edges);
1268 LOG(INFO) << "done finding cycles";
1269 CheckGraph(*graph);
1270
1271 // Calculate number of scratch blocks needed
1272
1273 LOG(INFO) << "Cutting cycles...";
1274 vector<CutEdgeVertexes> cuts;
1275 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1276 LOG(INFO) << "done cutting cycles";
1277 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1278 CheckGraph(*graph);
1279
1280 LOG(INFO) << "Creating initial topological order...";
1281 TopologicalSort(*graph, final_order);
1282 LOG(INFO) << "done with initial topo order";
1283 CheckGraph(*graph);
1284
1285 LOG(INFO) << "Moving full ops to the back";
1286 MoveFullOpsToBack(graph, final_order);
1287 LOG(INFO) << "done moving full ops to back";
1288
1289 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1290 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1291
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001292 SortCutsByTopoOrder(*final_order, &cuts);
1293
Andrew de los Reyesef017552010-10-06 17:57:52 -07001294 if (!cuts.empty())
1295 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1296 new_root,
1297 fd,
1298 data_file_size,
1299 final_order,
1300 &inverse_final_order,
1301 cuts));
1302 LOG(INFO) << "Making sure all temp blocks have been allocated";
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001303
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001304 // Remove the scratch node, if any
1305 if (scratch_vertex != Vertex::kInvalidIndex) {
1306 final_order->erase(final_order->begin() +
1307 inverse_final_order[scratch_vertex]);
1308 (*graph)[scratch_vertex].valid = false;
1309 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1310 }
1311
Andrew de los Reyesef017552010-10-06 17:57:52 -07001312 graph_utils::DumpGraph(*graph);
1313 CHECK(NoTempBlocksRemain(*graph));
1314 LOG(INFO) << "done making sure all temp blocks are allocated";
1315 return true;
1316}
1317
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001318void DeltaDiffGenerator::CreateScratchNode(uint64_t start_block,
1319 uint64_t num_blocks,
1320 Vertex* vertex) {
1321 vertex->file_name = "<scratch>";
1322 vertex->op.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
1323 vertex->op.set_data_offset(0);
1324 vertex->op.set_data_length(0);
1325 Extent* extent = vertex->op.add_dst_extents();
1326 extent->set_start_block(start_block);
1327 extent->set_num_blocks(num_blocks);
1328}
1329
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001330bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1331 const string& old_root,
1332 const string& old_image,
1333 const string& new_root,
1334 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001335 const string& old_kernel_part,
1336 const string& new_kernel_part,
1337 const string& output_path,
1338 const string& private_key_path) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001339 int old_image_block_count = 0, old_image_block_size = 0;
1340 int new_image_block_count = 0, new_image_block_size = 0;
1341 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1342 &new_image_block_count,
1343 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001344 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001345 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1346 &old_image_block_count,
1347 &old_image_block_size));
1348 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1349 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1350 << "Old and new images have different block counts.";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001351 }
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001352 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001353 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1354
Darin Petkov7ea32332010-10-13 10:46:11 -07001355 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1356 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1357 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001358 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1359 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1360 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1361 }
1362 Graph graph;
1363 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001364
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001365 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
1366 string temp_file_path;
1367 off_t data_file_size = 0;
1368
1369 LOG(INFO) << "Reading files...";
1370
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001371 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1372
Andrew de los Reyesef017552010-10-06 17:57:52 -07001373 vector<Vertex::Index> final_order;
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001374 Vertex::Index scratch_vertex = Vertex::kInvalidIndex;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001375 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001376 int fd;
1377 TEST_AND_RETURN_FALSE(
1378 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
1379 TEST_AND_RETURN_FALSE(fd >= 0);
1380 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001381 if (!old_image.empty()) {
1382 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001383
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001384 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1385 &blocks,
1386 old_root,
1387 new_root,
1388 fd,
1389 &data_file_size));
1390 LOG(INFO) << "done reading normal files";
1391 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001392
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001393 graph.resize(graph.size() + 1);
1394 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1395 fd,
1396 &data_file_size,
1397 new_image,
1398 &graph.back()));
1399
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001400 // Final scratch block (if there's space)
1401 if (blocks.size() < (kRootFSPartitionSize / kBlockSize)) {
1402 scratch_vertex = graph.size();
1403 graph.resize(graph.size() + 1);
1404 CreateScratchNode(blocks.size(),
1405 (kRootFSPartitionSize / kBlockSize) - blocks.size(),
1406 &graph.back());
1407 }
1408
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001409 // Read kernel partition
1410 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1411 new_kernel_part,
1412 &kernel_ops,
1413 fd,
1414 &data_file_size));
1415
1416 LOG(INFO) << "done reading kernel";
1417 CheckGraph(graph);
1418
1419 LOG(INFO) << "Creating edges...";
1420 CreateEdges(&graph, blocks);
1421 LOG(INFO) << "Done creating edges";
1422 CheckGraph(graph);
1423
1424 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1425 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001426 fd,
1427 &data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001428 &final_order,
1429 scratch_vertex));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001430 } else {
1431 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001432 off_t new_image_size =
1433 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Darin Petkov7a22d792010-11-08 14:10:00 -08001434 TEST_AND_RETURN_FALSE(FullUpdateGenerator::Run(&graph,
1435 new_kernel_part,
1436 new_image,
1437 new_image_size,
1438 fd,
1439 &data_file_size,
1440 kFullUpdateChunkSize,
1441 kBlockSize,
1442 &kernel_ops,
1443 &final_order));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001444 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001445 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001446
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001447 // Convert to protobuf Manifest object
1448 DeltaArchiveManifest manifest;
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001449 OperationNameMap op_name_map;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001450 CheckGraph(graph);
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001451 InstallOperationsToManifest(graph,
1452 final_order,
1453 kernel_ops,
1454 &manifest,
1455 &op_name_map);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001456 CheckGraph(graph);
1457 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001458
1459 // Reorder the data blobs with the newly ordered manifest
1460 string ordered_blobs_path;
1461 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
1462 "/tmp/CrAU_temp_data.ordered.XXXXXX",
1463 &ordered_blobs_path,
1464 false));
1465 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1466 temp_file_path,
1467 ordered_blobs_path));
1468
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001469 // Check that install op blobs are in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001470 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001471 {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001472 for (int i = 0; i < (manifest.install_operations_size() +
1473 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001474 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001475 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001476 manifest.mutable_install_operations(i) :
1477 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001478 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001479 if (op->has_data_offset()) {
1480 if (op->data_offset() != next_blob_offset) {
1481 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001482 << next_blob_offset;
1483 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001484 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001485 }
1486 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001487 }
1488
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001489 // Signatures appear at the end of the blobs. Note the offset in the
1490 // manifest
1491 if (!private_key_path.empty()) {
1492 LOG(INFO) << "Making room for signature in file";
1493 manifest.set_signatures_offset(next_blob_offset);
1494 LOG(INFO) << "set? " << manifest.has_signatures_offset();
1495 // Add a dummy op at the end to appease older clients
1496 DeltaArchiveManifest_InstallOperation* dummy_op =
1497 manifest.add_kernel_install_operations();
1498 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1499 dummy_op->set_data_offset(next_blob_offset);
1500 manifest.set_signatures_offset(next_blob_offset);
1501 uint64_t signature_blob_length = 0;
1502 TEST_AND_RETURN_FALSE(
1503 PayloadSigner::SignatureBlobLength(private_key_path,
1504 &signature_blob_length));
1505 dummy_op->set_data_length(signature_blob_length);
1506 manifest.set_signatures_size(signature_blob_length);
1507 Extent* dummy_extent = dummy_op->add_dst_extents();
1508 // Tell the dummy op to write this data to a big sparse hole
1509 dummy_extent->set_start_block(kSparseHole);
1510 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1511 kBlockSize);
1512 }
1513
Darin Petkov36a58222010-10-07 22:00:09 -07001514 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1515 new_kernel_part,
1516 old_image,
1517 new_image,
1518 &manifest));
1519
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001520 // Serialize protobuf
1521 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001522
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001523 CheckGraph(graph);
1524 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1525 CheckGraph(graph);
1526
1527 LOG(INFO) << "Writing final delta file header...";
1528 DirectFileWriter writer;
1529 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1530 O_WRONLY | O_CREAT | O_TRUNC,
1531 0644) == 0);
1532 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001533
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001534 // Write header
1535 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)) ==
Andrew de los Reyes08c4e272010-04-15 14:02:17 -07001536 static_cast<ssize_t>(strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001537
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001538 // Write version number
1539 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001540
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001541 // Write protobuf length
1542 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1543 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001544
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001545 // Write protobuf
1546 LOG(INFO) << "Writing final delta file protobuf... "
1547 << serialized_manifest.size();
1548 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
1549 serialized_manifest.size()) ==
1550 static_cast<ssize_t>(serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001551
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001552 // Append the data blobs
1553 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001554 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001555 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1556 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1557 for (;;) {
1558 char buf[kBlockSize];
1559 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1560 if (0 == rc) {
1561 // EOF
1562 break;
1563 }
1564 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
1565 TEST_AND_RETURN_FALSE(writer.Write(buf, rc) == rc);
1566 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001567
1568 // Write signature blob.
1569 if (!private_key_path.empty()) {
1570 LOG(INFO) << "Signing the update...";
1571 vector<char> signature_blob;
1572 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(output_path,
1573 private_key_path,
1574 &signature_blob));
1575 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
1576 signature_blob.size()) ==
1577 static_cast<ssize_t>(signature_blob.size()));
1578 }
1579
Darin Petkov95cf01f2010-10-12 14:59:13 -07001580 int64_t manifest_metadata_size =
1581 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001582 ReportPayloadUsage(manifest, manifest_metadata_size, op_name_map);
Darin Petkov880335c2010-10-01 15:52:53 -07001583
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001584 LOG(INFO) << "All done. Successfully created delta file.";
1585 return true;
1586}
1587
Andrew de los Reyes50f36492010-11-01 13:57:12 -07001588const char* const kBsdiffPath = "bsdiff";
1589const char* const kBspatchPath = "bspatch";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001590const char* const kDeltaMagic = "CrAU";
1591
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001592}; // namespace chromeos_update_engine