blob: f5f8bec1300fdab0715a8ee94fb6855e970d0644 [file] [log] [blame]
Don Garrettf4b28742012-03-27 20:48:06 -07001// Copyright (c) 2012 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>
Darin Petkov7438a5c2011-08-29 11:56:44 -070021#include <base/memory/scoped_ptr.h>
Darin Petkov880335c2010-10-01 15:52:53 -070022#include <base/string_util.h>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070023#include <bzlib.h>
Darin Petkov880335c2010-10-01 15:52:53 -070024
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070025#include "update_engine/bzip.h"
26#include "update_engine/cycle_breaker.h"
27#include "update_engine/extent_mapper.h"
Andrew de los Reyesef017552010-10-06 17:57:52 -070028#include "update_engine/extent_ranges.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070029#include "update_engine/file_writer.h"
30#include "update_engine/filesystem_iterator.h"
Darin Petkov7a22d792010-11-08 14:10:00 -080031#include "update_engine/full_update_generator.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070032#include "update_engine/graph_types.h"
33#include "update_engine/graph_utils.h"
Thieu Le5c7d9752010-12-15 16:09:28 -080034#include "update_engine/metadata.h"
Darin Petkov36a58222010-10-07 22:00:09 -070035#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070036#include "update_engine/payload_signer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070037#include "update_engine/subprocess.h"
38#include "update_engine/topological_sort.h"
39#include "update_engine/update_metadata.pb.h"
40#include "update_engine/utils.h"
41
42using std::make_pair;
Andrew de los Reyesef017552010-10-06 17:57:52 -070043using std::map;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070044using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070045using std::min;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -070046using std::pair;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070047using std::set;
48using std::string;
49using std::vector;
50
51namespace chromeos_update_engine {
52
53typedef DeltaDiffGenerator::Block Block;
Darin Petkov9fa7ec52010-10-18 11:45:23 -070054typedef map<const DeltaArchiveManifest_InstallOperation*,
55 const string*> OperationNameMap;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070056
57namespace {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070058const size_t kBlockSize = 4096; // bytes
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -080059const string kNonexistentPath = "";
Andrew de los Reyes927179d2010-12-02 11:26:48 -080060
61// TODO(adlr): switch from 1GiB to 2GiB when we no longer care about old
62// clients:
Darin Petkov9eadd642010-10-14 15:20:57 -070063const size_t kRootFSPartitionSize = 1 * 1024 * 1024 * 1024; // bytes
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070064const uint64_t kVersionNumber = 1;
Darin Petkov9eadd642010-10-14 15:20:57 -070065const uint64_t kFullUpdateChunkSize = 1024 * 1024; // bytes
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070066
Darin Petkov68c10d12010-10-14 09:24:37 -070067static const char* kInstallOperationTypes[] = {
68 "REPLACE",
69 "REPLACE_BZ",
70 "MOVE",
71 "BSDIFF"
72};
73
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070074// Stores all Extents for a file into 'out'. Returns true on success.
75bool GatherExtents(const string& path,
76 google::protobuf::RepeatedPtrField<Extent>* out) {
77 vector<Extent> extents;
78 TEST_AND_RETURN_FALSE(extent_mapper::ExtentsForFileFibmap(path, &extents));
79 DeltaDiffGenerator::StoreExtents(extents, out);
80 return true;
81}
82
Andrew de los Reyesef017552010-10-06 17:57:52 -070083// For a given regular file which must exist at new_root + path, and
84// may exist at old_root + path, creates a new InstallOperation and
85// adds it to the graph. Also, populates the |blocks| array as
86// necessary, if |blocks| is non-NULL. Also, writes the data
87// necessary to send the file down to the client into data_fd, which
88// has length *data_file_size. *data_file_size is updated
89// appropriately. If |existing_vertex| is no kInvalidIndex, use that
90// rather than allocating a new vertex. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070091bool DeltaReadFile(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -070092 Vertex::Index existing_vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070093 vector<Block>* blocks,
94 const string& old_root,
95 const string& new_root,
96 const string& path, // within new_root
97 int data_fd,
98 off_t* data_file_size) {
99 vector<char> data;
100 DeltaArchiveManifest_InstallOperation operation;
101
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800102 string old_path = (old_root == kNonexistentPath) ? kNonexistentPath :
103 old_root + path;
104
Don Garrettf4b28742012-03-27 20:48:06 -0700105 // TODO(dgarrett): chromium-os:15274 Wire up this file all of the way to
106 // command line.
107 static const char* black_files[] = {
108 "/opt/google/chrome/pepper/libnetflixidd.so"
109 };
110
111 std::set<string> bsdiff_blacklist = std::set<string>(black_files,
112 black_files +
113 arraysize(black_files));
114
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800115 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_path,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700116 new_root + path,
Don Garrettf4b28742012-03-27 20:48:06 -0700117 bsdiff_blacklist,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700118 &data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700119 &operation,
120 true));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700121
122 // Write the data
123 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
124 operation.set_data_offset(*data_file_size);
125 operation.set_data_length(data.size());
126 }
127
128 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
129 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700130
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700131 // Now, insert into graph and blocks vector
Andrew de los Reyesef017552010-10-06 17:57:52 -0700132 Vertex::Index vertex = existing_vertex;
133 if (vertex == Vertex::kInvalidIndex) {
134 graph->resize(graph->size() + 1);
135 vertex = graph->size() - 1;
136 }
137 (*graph)[vertex].op = operation;
138 CHECK((*graph)[vertex].op.has_type());
139 (*graph)[vertex].file_name = path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700140
Andrew de los Reyesef017552010-10-06 17:57:52 -0700141 if (blocks)
Thieu Le5c7d9752010-12-15 16:09:28 -0800142 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::AddInstallOpToBlocksVector(
143 (*graph)[vertex].op,
144 *graph,
145 vertex,
146 blocks));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700147 return true;
148}
149
150// For each regular file within new_root, creates a node in the graph,
151// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
152// and writes any necessary data to the end of data_fd.
153bool DeltaReadFiles(Graph* graph,
154 vector<Block>* blocks,
155 const string& old_root,
156 const string& new_root,
157 int data_fd,
158 off_t* data_file_size) {
159 set<ino_t> visited_inodes;
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800160 set<ino_t> visited_src_inodes;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700161 for (FilesystemIterator fs_iter(new_root,
162 utils::SetWithValue<string>("/lost+found"));
163 !fs_iter.IsEnd(); fs_iter.Increment()) {
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800164 // We never diff symlinks (here, we check that dst file is not a symlink).
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700165 if (!S_ISREG(fs_iter.GetStat().st_mode))
166 continue;
167
168 // Make sure we visit each inode only once.
169 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
170 continue;
171 visited_inodes.insert(fs_iter.GetStat().st_ino);
172 if (fs_iter.GetStat().st_size == 0)
173 continue;
174
175 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700176
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800177 // We can't visit each dst image inode more than once, as that would
178 // duplicate work. Here, we avoid visiting each source image inode
179 // more than once. Technically, we could have multiple operations
180 // that read the same blocks from the source image for diffing, but
181 // we choose not to to avoid complexity. Eventually we will move away
182 // from using a graph/cycle detection/etc to generate diffs, and at that
183 // time, it will be easy (non-complex) to have many operations read
184 // from the same source blocks. At that time, this code can die. -adlr
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800185 bool should_diff_from_source = false;
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800186 string src_path = old_root + fs_iter.GetPartialPath();
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800187 struct stat src_stbuf;
188 // We never diff symlinks (here, we check that src file is not a symlink).
189 if (0 == lstat(src_path.c_str(), &src_stbuf) &&
190 S_ISREG(src_stbuf.st_mode)) {
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800191 should_diff_from_source = !utils::SetContainsKey(visited_src_inodes,
192 src_stbuf.st_ino);
193 visited_src_inodes.insert(src_stbuf.st_ino);
194 }
195
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700196 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700197 Vertex::kInvalidIndex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700198 blocks,
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800199 (should_diff_from_source ?
200 old_root :
201 kNonexistentPath),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700202 new_root,
203 fs_iter.GetPartialPath(),
204 data_fd,
205 data_file_size));
206 }
207 return true;
208}
209
Andrew de los Reyesef017552010-10-06 17:57:52 -0700210// This class allocates non-existent temp blocks, starting from
211// kTempBlockStart. Other code is responsible for converting these
212// temp blocks into real blocks, as the client can't read or write to
213// these blocks.
214class DummyExtentAllocator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700215 public:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700216 explicit DummyExtentAllocator()
217 : next_block_(kTempBlockStart) {}
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700218 vector<Extent> Allocate(const uint64_t block_count) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700219 vector<Extent> ret(1);
220 ret[0].set_start_block(next_block_);
221 ret[0].set_num_blocks(block_count);
222 next_block_ += block_count;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700223 return ret;
224 }
225 private:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700226 uint64_t next_block_;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700227};
228
229// Reads blocks from image_path that are not yet marked as being written
230// in the blocks array. These blocks that remain are non-file-data blocks.
231// In the future we might consider intelligent diffing between this data
232// and data in the previous image, but for now we just bzip2 compress it
233// and include it in the update.
234// Creates a new node in the graph to write these blocks and writes the
235// appropriate blob to blobs_fd. Reads and updates blobs_length;
236bool ReadUnwrittenBlocks(const vector<Block>& blocks,
237 int blobs_fd,
238 off_t* blobs_length,
239 const string& image_path,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700240 Vertex* vertex) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700241 vertex->file_name = "<rootfs-non-file-data>";
242
Andrew de los Reyesef017552010-10-06 17:57:52 -0700243 DeltaArchiveManifest_InstallOperation* out_op = &vertex->op;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700244 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
245 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
246 ScopedFdCloser image_fd_closer(&image_fd);
247
248 string temp_file_path;
249 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX",
250 &temp_file_path,
251 NULL));
252
253 FILE* file = fopen(temp_file_path.c_str(), "w");
254 TEST_AND_RETURN_FALSE(file);
255 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700256
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700257 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
258 file,
259 9, // max compression
260 0, // verbosity
261 0); // default work factor
262 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700263
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700264 vector<Extent> extents;
265 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700266
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700267 LOG(INFO) << "Appending left over blocks to extents";
268 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
269 if (blocks[i].writer != Vertex::kInvalidIndex)
270 continue;
Andrew de los Reyesef017552010-10-06 17:57:52 -0700271 if (blocks[i].reader != Vertex::kInvalidIndex) {
272 graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i);
273 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700274 graph_utils::AppendBlockToExtents(&extents, i);
275 block_count++;
276 }
277
278 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
279 // so we arbitrarily set it to 1024 * kBlockSize.
280 vector<char> buf(1024 * kBlockSize);
281
282 LOG(INFO) << "Reading left over blocks";
283 vector<Block>::size_type blocks_copied_count = 0;
284
285 // For each extent in extents, write the data into BZ2_bzWrite which
286 // sends it to an output file.
287 // We use the temporary buffer 'buf' to hold the data, which may be
288 // smaller than the extent, so in that case we have to loop to get
289 // the extent's data (that's the inner while loop).
290 for (vector<Extent>::const_iterator it = extents.begin();
291 it != extents.end(); ++it) {
292 vector<Block>::size_type blocks_read = 0;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800293 float printed_progress = -1;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700294 while (blocks_read < it->num_blocks()) {
295 const int copy_block_cnt =
296 min(buf.size() / kBlockSize,
297 static_cast<vector<char>::size_type>(
298 it->num_blocks() - blocks_read));
299 ssize_t rc = pread(image_fd,
300 &buf[0],
301 copy_block_cnt * kBlockSize,
302 (it->start_block() + blocks_read) * kBlockSize);
303 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
304 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
305 copy_block_cnt * kBlockSize);
306 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
307 TEST_AND_RETURN_FALSE(err == BZ_OK);
308 blocks_read += copy_block_cnt;
309 blocks_copied_count += copy_block_cnt;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800310 float current_progress =
311 static_cast<float>(blocks_copied_count) / block_count;
312 if (printed_progress + 0.1 < current_progress ||
313 blocks_copied_count == block_count) {
314 LOG(INFO) << "progress: " << current_progress;
315 printed_progress = current_progress;
316 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700317 }
318 }
319 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
320 TEST_AND_RETURN_FALSE(err == BZ_OK);
321 bz_file = NULL;
322 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
323 file = NULL;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700324
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700325 vector<char> compressed_data;
326 LOG(INFO) << "Reading compressed data off disk";
327 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
328 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700329
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700330 // Add node to graph to write these blocks
331 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
332 out_op->set_data_offset(*blobs_length);
333 out_op->set_data_length(compressed_data.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700334 LOG(INFO) << "Rootfs non-data blocks compressed take up "
335 << compressed_data.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700336 *blobs_length += compressed_data.size();
337 out_op->set_dst_length(kBlockSize * block_count);
338 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700339
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700340 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
341 &compressed_data[0],
342 compressed_data.size()));
343 LOG(INFO) << "done with extra blocks";
344 return true;
345}
346
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700347// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700348// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700349bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
350 uint64_t value_be = htobe64(value);
Don Garrette410e0f2011-11-10 15:39:01 -0800351 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700352 return true;
353}
354
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700355// Adds each operation from |graph| to |out_manifest| in the order specified by
356// |order| while building |out_op_name_map| with operation to name
357// mappings. Adds all |kernel_ops| to |out_manifest|. Filters out no-op
358// operations.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700359void InstallOperationsToManifest(
360 const Graph& graph,
361 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700362 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700363 DeltaArchiveManifest* out_manifest,
364 OperationNameMap* out_op_name_map) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700365 for (vector<Vertex::Index>::const_iterator it = order.begin();
366 it != order.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700367 const Vertex& vertex = graph[*it];
368 const DeltaArchiveManifest_InstallOperation& add_op = vertex.op;
369 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
370 continue;
371 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700372 DeltaArchiveManifest_InstallOperation* op =
373 out_manifest->add_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700374 *op = add_op;
375 (*out_op_name_map)[op] = &vertex.file_name;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700376 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700377 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
378 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700379 const DeltaArchiveManifest_InstallOperation& add_op = *it;
380 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
381 continue;
382 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700383 DeltaArchiveManifest_InstallOperation* op =
384 out_manifest->add_kernel_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700385 *op = add_op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700386 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700387}
388
389void CheckGraph(const Graph& graph) {
390 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
391 CHECK(it->op.has_type());
392 }
393}
394
Darin Petkov68c10d12010-10-14 09:24:37 -0700395// Delta compresses a kernel partition |new_kernel_part| with knowledge of the
396// old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty
397// string, generates a full update of the partition.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700398bool DeltaCompressKernelPartition(
399 const string& old_kernel_part,
400 const string& new_kernel_part,
401 vector<DeltaArchiveManifest_InstallOperation>* ops,
402 int blobs_fd,
403 off_t* blobs_length) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700404 LOG(INFO) << "Delta compressing kernel partition...";
Darin Petkov68c10d12010-10-14 09:24:37 -0700405 LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update...";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700406
407 // Add a new install operation
408 ops->resize(1);
409 DeltaArchiveManifest_InstallOperation* op = &(*ops)[0];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700410
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700411 vector<char> data;
Darin Petkov68c10d12010-10-14 09:24:37 -0700412 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_kernel_part,
413 new_kernel_part,
Don Garrettf4b28742012-03-27 20:48:06 -0700414 std::set<string>(),
Darin Petkov68c10d12010-10-14 09:24:37 -0700415 &data,
416 op,
417 false));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700418
Darin Petkov68c10d12010-10-14 09:24:37 -0700419 // Write the data
420 if (op->type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
421 op->set_data_offset(*blobs_length);
422 op->set_data_length(data.size());
423 }
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700424
Darin Petkov68c10d12010-10-14 09:24:37 -0700425 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
426 *blobs_length += data.size();
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700427
Darin Petkov68c10d12010-10-14 09:24:37 -0700428 LOG(INFO) << "Done delta compressing kernel partition: "
429 << kInstallOperationTypes[op->type()];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700430 return true;
431}
432
Darin Petkov880335c2010-10-01 15:52:53 -0700433struct DeltaObject {
434 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
435 : name(in_name),
436 type(in_type),
437 size(in_size) {}
438 bool operator <(const DeltaObject& object) const {
Darin Petkovd43d6902010-10-14 11:17:50 -0700439 return (size != object.size) ? (size < object.size) : (name < object.name);
Darin Petkov880335c2010-10-01 15:52:53 -0700440 }
441 string name;
442 int type;
443 off_t size;
444};
445
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700446void ReportPayloadUsage(const DeltaArchiveManifest& manifest,
447 const int64_t manifest_metadata_size,
448 const OperationNameMap& op_name_map) {
Darin Petkov880335c2010-10-01 15:52:53 -0700449 vector<DeltaObject> objects;
450 off_t total_size = 0;
451
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700452 // Rootfs install operations.
453 for (int i = 0; i < manifest.install_operations_size(); ++i) {
454 const DeltaArchiveManifest_InstallOperation& op =
455 manifest.install_operations(i);
456 objects.push_back(DeltaObject(*op_name_map.find(&op)->second,
457 op.type(),
458 op.data_length()));
459 total_size += op.data_length();
Darin Petkov880335c2010-10-01 15:52:53 -0700460 }
461
Darin Petkov880335c2010-10-01 15:52:53 -0700462 // Kernel install operations.
463 for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) {
464 const DeltaArchiveManifest_InstallOperation& op =
465 manifest.kernel_install_operations(i);
466 objects.push_back(DeltaObject(StringPrintf("<kernel-operation-%d>", i),
467 op.type(),
468 op.data_length()));
469 total_size += op.data_length();
470 }
471
Darin Petkov95cf01f2010-10-12 14:59:13 -0700472 objects.push_back(DeltaObject("<manifest-metadata>",
473 -1,
474 manifest_metadata_size));
475 total_size += manifest_metadata_size;
476
Darin Petkov880335c2010-10-01 15:52:53 -0700477 std::sort(objects.begin(), objects.end());
478
479 static const char kFormatString[] = "%6.2f%% %10llu %-10s %s\n";
480 for (vector<DeltaObject>::const_iterator it = objects.begin();
481 it != objects.end(); ++it) {
482 const DeltaObject& object = *it;
483 fprintf(stderr, kFormatString,
484 object.size * 100.0 / total_size,
485 object.size,
Darin Petkov95cf01f2010-10-12 14:59:13 -0700486 object.type >= 0 ? kInstallOperationTypes[object.type] : "-",
Darin Petkov880335c2010-10-01 15:52:53 -0700487 object.name.c_str());
488 }
489 fprintf(stderr, kFormatString, 100.0, total_size, "", "<total>");
490}
491
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700492} // namespace {}
493
494bool DeltaDiffGenerator::ReadFileToDiff(
495 const string& old_filename,
496 const string& new_filename,
Don Garrettf4b28742012-03-27 20:48:06 -0700497 const std::set<string>& bsdiff_blacklist,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700498 vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700499 DeltaArchiveManifest_InstallOperation* out_op,
500 bool gather_extents) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700501 // Read new data in
502 vector<char> new_data;
503 TEST_AND_RETURN_FALSE(utils::ReadFile(new_filename, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700504
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700505 TEST_AND_RETURN_FALSE(!new_data.empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700506
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700507 vector<char> new_data_bz;
508 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
509 CHECK(!new_data_bz.empty());
510
511 vector<char> data; // Data blob that will be written to delta file.
512
513 DeltaArchiveManifest_InstallOperation operation;
514 size_t current_best_size = 0;
515 if (new_data.size() <= new_data_bz.size()) {
516 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
517 current_best_size = new_data.size();
518 data = new_data;
519 } else {
520 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
521 current_best_size = new_data_bz.size();
522 data = new_data_bz;
523 }
524
525 // Do we have an original file to consider?
526 struct stat old_stbuf;
Don Garrettf4b28742012-03-27 20:48:06 -0700527 bool original = !old_filename.empty();
528 if (original && 0 != stat(old_filename.c_str(), &old_stbuf)) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700529 // If stat-ing the old file fails, it should be because it doesn't exist.
530 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
Don Garrettf4b28742012-03-27 20:48:06 -0700531 original = false;
Darin Petkov68c10d12010-10-14 09:24:37 -0700532 }
Don Garrettf4b28742012-03-27 20:48:06 -0700533
534 if (original) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700535 // Read old data
536 vector<char> old_data;
537 TEST_AND_RETURN_FALSE(utils::ReadFile(old_filename, &old_data));
538 if (old_data == new_data) {
539 // No change in data.
540 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
541 current_best_size = 0;
542 data.clear();
543 } else {
Don Garrettf4b28742012-03-27 20:48:06 -0700544 if (bsdiff_blacklist.find(old_filename) ==
545 bsdiff_blacklist.end()) {
546 // If the source file hasn't been bsdiff blacklisted, then try to see
547 // if bsdiff can find a smaller operation.
548 vector<char> bsdiff_delta;
549 TEST_AND_RETURN_FALSE(
550 BsdiffFiles(old_filename, new_filename, &bsdiff_delta));
551 CHECK_GT(bsdiff_delta.size(), static_cast<vector<char>::size_type>(0));
552 if (bsdiff_delta.size() < current_best_size) {
553 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
554 current_best_size = bsdiff_delta.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700555
Don Garrettf4b28742012-03-27 20:48:06 -0700556 data = bsdiff_delta;
557 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700558 }
559 }
560 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700561
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700562 // Set parameters of the operations
563 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700564
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700565 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
566 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Darin Petkov68c10d12010-10-14 09:24:37 -0700567 if (gather_extents) {
568 TEST_AND_RETURN_FALSE(
569 GatherExtents(old_filename, operation.mutable_src_extents()));
570 } else {
571 Extent* src_extent = operation.add_src_extents();
572 src_extent->set_start_block(0);
573 src_extent->set_num_blocks(
574 (old_stbuf.st_size + kBlockSize - 1) / kBlockSize);
575 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700576 operation.set_src_length(old_stbuf.st_size);
577 }
578
Darin Petkov68c10d12010-10-14 09:24:37 -0700579 if (gather_extents) {
580 TEST_AND_RETURN_FALSE(
581 GatherExtents(new_filename, operation.mutable_dst_extents()));
582 } else {
583 Extent* dst_extent = operation.add_dst_extents();
584 dst_extent->set_start_block(0);
585 dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize);
586 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700587 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700588
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700589 out_data->swap(data);
590 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700591
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700592 return true;
593}
594
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700595bool DeltaDiffGenerator::InitializePartitionInfo(bool is_kernel,
596 const string& partition,
597 PartitionInfo* info) {
Darin Petkov7ea32332010-10-13 10:46:11 -0700598 int64_t size = 0;
599 if (is_kernel) {
600 size = utils::FileSize(partition);
601 } else {
602 int block_count = 0, block_size = 0;
603 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition,
604 &block_count,
605 &block_size));
606 size = static_cast<int64_t>(block_count) * block_size;
607 }
608 TEST_AND_RETURN_FALSE(size > 0);
Darin Petkov36a58222010-10-07 22:00:09 -0700609 info->set_size(size);
610 OmahaHashCalculator hasher;
Darin Petkov7ea32332010-10-13 10:46:11 -0700611 TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size);
Darin Petkov36a58222010-10-07 22:00:09 -0700612 TEST_AND_RETURN_FALSE(hasher.Finalize());
613 const vector<char>& hash = hasher.raw_hash();
614 info->set_hash(hash.data(), hash.size());
Darin Petkovd43d6902010-10-14 11:17:50 -0700615 LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash();
Darin Petkov36a58222010-10-07 22:00:09 -0700616 return true;
617}
618
619bool InitializePartitionInfos(const string& old_kernel,
620 const string& new_kernel,
621 const string& old_rootfs,
622 const string& new_rootfs,
623 DeltaArchiveManifest* manifest) {
Darin Petkovd43d6902010-10-14 11:17:50 -0700624 if (!old_kernel.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700625 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
626 true,
627 old_kernel,
628 manifest->mutable_old_kernel_info()));
Darin Petkovd43d6902010-10-14 11:17:50 -0700629 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700630 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
631 true,
632 new_kernel,
633 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700634 if (!old_rootfs.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700635 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
636 false,
637 old_rootfs,
638 manifest->mutable_old_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700639 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700640 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
641 false,
642 new_rootfs,
643 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700644 return true;
645}
646
Andrew de los Reyesef017552010-10-06 17:57:52 -0700647namespace {
648
649// Takes a collection (vector or RepeatedPtrField) of Extent and
650// returns a vector of the blocks referenced, in order.
651template<typename T>
652vector<uint64_t> ExpandExtents(const T& extents) {
653 vector<uint64_t> ret;
654 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
655 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700656 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700657 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700658 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700659 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700660 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700661 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700662 }
663 }
664 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700665 return ret;
666}
667
668// Takes a vector of blocks and returns an equivalent vector of Extent
669// objects.
670vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
671 vector<Extent> new_extents;
672 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
673 it != e; ++it) {
674 graph_utils::AppendBlockToExtents(&new_extents, *it);
675 }
676 return new_extents;
677}
678
679} // namespace {}
680
681void DeltaDiffGenerator::SubstituteBlocks(
682 Vertex* vertex,
683 const vector<Extent>& remove_extents,
684 const vector<Extent>& replace_extents) {
685 // First, expand out the blocks that op reads from
686 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700687 {
688 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700689 vector<uint64_t> remove_extents_expanded =
690 ExpandExtents(remove_extents);
691 vector<uint64_t> replace_extents_expanded =
692 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700693 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700694 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700695 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700696 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700697 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
698 }
699 utils::ApplyMap(&read_blocks, conversion);
700 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
701 e = vertex->out_edges.end(); it != e; ++it) {
702 vector<uint64_t> write_before_deps_expanded =
703 ExpandExtents(it->second.write_extents);
704 utils::ApplyMap(&write_before_deps_expanded, conversion);
705 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700706 }
707 }
708 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700709 vertex->op.clear_src_extents();
710 vector<Extent> new_extents = CompressExtents(read_blocks);
711 DeltaDiffGenerator::StoreExtents(new_extents,
712 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700713}
714
715bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700716 const set<Edge>& edges,
717 vector<CutEdgeVertexes>* out_cuts) {
718 DummyExtentAllocator scratch_allocator;
719 vector<CutEdgeVertexes> cuts;
720 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700721
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700722 uint64_t scratch_blocks_used = 0;
723 for (set<Edge>::const_iterator it = edges.begin();
724 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700725 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700726 vector<Extent> old_extents =
727 (*graph)[it->first].out_edges[it->second].extents;
728 // Choose some scratch space
729 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700730 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700731 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
732 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700733 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700734 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700735 cuts.back().old_src = it->first;
736 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700737
Andrew de los Reyesef017552010-10-06 17:57:52 -0700738 EdgeProperties& cut_edge_properties =
739 (*graph)[it->first].out_edges.find(it->second)->second;
740
741 // This should never happen, as we should only be cutting edges between
742 // real file nodes, and write-before relationships are created from
743 // a real file node to a temp copy node:
744 CHECK(cut_edge_properties.write_extents.empty())
745 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700746
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700747 // make node depend on the copy operation
748 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700749 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700750
751 // Set src/dst extents and other proto variables for copy operation
752 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
753 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700754 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700755 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700756 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700757 graph->back().op.mutable_dst_extents());
758 graph->back().op.set_src_length(
759 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
760 graph->back().op.set_dst_length(graph->back().op.src_length());
761
762 // make the dest node read from the scratch space
763 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700764 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700765 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700766 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700767
768 // delete the old edge
Mike Frysinger0f9547d2012-02-16 12:11:37 -0500769 CHECK_EQ(static_cast<Graph::size_type>(1),
770 (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700771
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700772 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700773 EdgeProperties write_before_edge_properties;
774 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
775 (*graph)[it->second].out_edges.insert(
776 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700777 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700778 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700779 return true;
780}
781
782// Stores all Extents in 'extents' into 'out'.
783void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700784 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700785 google::protobuf::RepeatedPtrField<Extent>* out) {
786 for (vector<Extent>::const_iterator it = extents.begin();
787 it != extents.end(); ++it) {
788 Extent* new_extent = out->Add();
789 *new_extent = *it;
790 }
791}
792
793// Creates all the edges for the graph. Writers of a block point to
794// readers of the same block. This is because for an edge A->B, B
795// must complete before A executes.
796void DeltaDiffGenerator::CreateEdges(Graph* graph,
797 const vector<Block>& blocks) {
798 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
799 // Blocks with both a reader and writer get an edge
800 if (blocks[i].reader == Vertex::kInvalidIndex ||
801 blocks[i].writer == Vertex::kInvalidIndex)
802 continue;
803 // Don't have a node depend on itself
804 if (blocks[i].reader == blocks[i].writer)
805 continue;
806 // See if there's already an edge we can add onto
807 Vertex::EdgeMap::iterator edge_it =
808 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
809 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
810 // No existing edge. Create one
811 (*graph)[blocks[i].writer].out_edges.insert(
812 make_pair(blocks[i].reader, EdgeProperties()));
813 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700814 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700815 }
816 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
817 }
818}
819
Andrew de los Reyesef017552010-10-06 17:57:52 -0700820namespace {
821
822class SortCutsByTopoOrderLess {
823 public:
824 SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table)
825 : table_(table) {}
826 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
827 return table_[a.old_dst] < table_[b.old_dst];
828 }
829 private:
830 vector<vector<Vertex::Index>::size_type>& table_;
831};
832
833} // namespace {}
834
835void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
836 vector<Vertex::Index>& op_indexes,
837 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
838 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
839 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
840 i != e; ++i) {
841 Vertex::Index node = op_indexes[i];
842 if (table.size() < (node + 1)) {
843 table.resize(node + 1);
844 }
845 table[node] = i;
846 }
847 reverse_op_indexes->swap(table);
848}
849
850void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes,
851 vector<CutEdgeVertexes>* cuts) {
852 // first, make a reverse lookup table.
853 vector<vector<Vertex::Index>::size_type> table;
854 GenerateReverseTopoOrderMap(op_indexes, &table);
855 SortCutsByTopoOrderLess less(table);
856 sort(cuts->begin(), cuts->end(), less);
857}
858
859void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
860 vector<Vertex::Index>* op_indexes) {
861 vector<Vertex::Index> ret;
862 vector<Vertex::Index> full_ops;
863 ret.reserve(op_indexes->size());
864 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
865 ++i) {
866 DeltaArchiveManifest_InstallOperation_Type type =
867 (*graph)[(*op_indexes)[i]].op.type();
868 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
869 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
870 full_ops.push_back((*op_indexes)[i]);
871 } else {
872 ret.push_back((*op_indexes)[i]);
873 }
874 }
875 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
876 << (full_ops.size() + ret.size()) << " total ops.";
877 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
878 op_indexes->swap(ret);
879}
880
881namespace {
882
883template<typename T>
884bool TempBlocksExistInExtents(const T& extents) {
885 for (int i = 0, e = extents.size(); i < e; ++i) {
886 Extent extent = graph_utils::GetElement(extents, i);
887 uint64_t start = extent.start_block();
888 uint64_t num = extent.num_blocks();
889 if (start == kSparseHole)
890 continue;
891 if (start >= kTempBlockStart ||
892 (start + num) >= kTempBlockStart) {
893 LOG(ERROR) << "temp block!";
894 LOG(ERROR) << "start: " << start << ", num: " << num;
895 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
896 LOG(ERROR) << "returning true";
897 return true;
898 }
899 // check for wrap-around, which would be a bug:
900 CHECK(start <= (start + num));
901 }
902 return false;
903}
904
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700905// Convertes the cuts, which must all have the same |old_dst| member,
906// to full. It does this by converting the |old_dst| to REPLACE or
907// REPLACE_BZ, dropping all incoming edges to |old_dst|, and marking
908// all temp nodes invalid.
909bool ConvertCutsToFull(
910 Graph* graph,
911 const string& new_root,
912 int data_fd,
913 off_t* data_file_size,
914 vector<Vertex::Index>* op_indexes,
915 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
916 const vector<CutEdgeVertexes>& cuts) {
917 CHECK(!cuts.empty());
918 set<Vertex::Index> deleted_nodes;
919 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
920 e = cuts.end(); it != e; ++it) {
921 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp(
922 graph,
923 *it,
924 new_root,
925 data_fd,
926 data_file_size));
927 deleted_nodes.insert(it->new_vertex);
928 }
929 deleted_nodes.insert(cuts[0].old_dst);
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700930
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700931 vector<Vertex::Index> new_op_indexes;
932 new_op_indexes.reserve(op_indexes->size());
933 for (vector<Vertex::Index>::iterator it = op_indexes->begin(),
934 e = op_indexes->end(); it != e; ++it) {
935 if (utils::SetContainsKey(deleted_nodes, *it))
936 continue;
937 new_op_indexes.push_back(*it);
938 }
939 new_op_indexes.push_back(cuts[0].old_dst);
940 op_indexes->swap(new_op_indexes);
941 DeltaDiffGenerator::GenerateReverseTopoOrderMap(*op_indexes,
942 reverse_op_indexes);
943 return true;
944}
945
946// Tries to assign temp blocks for a collection of cuts, all of which share
947// the same old_dst member. If temp blocks can't be found, old_dst will be
948// converted to a REPLACE or REPLACE_BZ operation. Returns true on success,
949// which can happen even if blocks are converted to full. Returns false
950// on exceptional error cases.
951bool AssignBlockForAdjoiningCuts(
952 Graph* graph,
953 const string& new_root,
954 int data_fd,
955 off_t* data_file_size,
956 vector<Vertex::Index>* op_indexes,
957 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
958 const vector<CutEdgeVertexes>& cuts) {
959 CHECK(!cuts.empty());
960 const Vertex::Index old_dst = cuts[0].old_dst;
961 // Calculate # of blocks needed
962 uint64_t blocks_needed = 0;
963 map<const CutEdgeVertexes*, uint64_t> cuts_blocks_needed;
964 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
965 e = cuts.end(); it != e; ++it) {
966 uint64_t cut_blocks_needed = 0;
967 for (vector<Extent>::const_iterator jt = it->tmp_extents.begin(),
968 je = it->tmp_extents.end(); jt != je; ++jt) {
969 cut_blocks_needed += jt->num_blocks();
970 }
971 blocks_needed += cut_blocks_needed;
972 cuts_blocks_needed[&*it] = cut_blocks_needed;
973 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700974
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700975 // Find enough blocks
976 ExtentRanges scratch_ranges;
977 // Each block that's supplying temp blocks and the corresponding blocks:
978 typedef vector<pair<Vertex::Index, ExtentRanges> > SupplierVector;
979 SupplierVector block_suppliers;
980 uint64_t scratch_blocks_found = 0;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700981 for (vector<Vertex::Index>::size_type i = (*reverse_op_indexes)[old_dst] + 1,
982 e = op_indexes->size(); i < e; ++i) {
983 Vertex::Index test_node = (*op_indexes)[i];
984 if (!(*graph)[test_node].valid)
985 continue;
986 // See if this node has sufficient blocks
987 ExtentRanges ranges;
988 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
989 ranges.SubtractExtent(ExtentForRange(
990 kTempBlockStart, kSparseHole - kTempBlockStart));
991 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
992 // For now, for simplicity, subtract out all blocks in read-before
993 // dependencies.
994 for (Vertex::EdgeMap::const_iterator edge_i =
995 (*graph)[test_node].out_edges.begin(),
996 edge_e = (*graph)[test_node].out_edges.end();
997 edge_i != edge_e; ++edge_i) {
998 ranges.SubtractExtents(edge_i->second.extents);
999 }
1000 if (ranges.blocks() == 0)
1001 continue;
1002
1003 if (ranges.blocks() + scratch_blocks_found > blocks_needed) {
1004 // trim down ranges
1005 vector<Extent> new_ranges = ranges.GetExtentsForBlockCount(
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001006 blocks_needed - scratch_blocks_found);
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001007 ranges = ExtentRanges();
1008 ranges.AddExtents(new_ranges);
1009 }
1010 scratch_ranges.AddRanges(ranges);
1011 block_suppliers.push_back(make_pair(test_node, ranges));
1012 scratch_blocks_found += ranges.blocks();
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001013 if (scratch_ranges.blocks() >= blocks_needed)
1014 break;
1015 }
1016 if (scratch_ranges.blocks() < blocks_needed) {
1017 LOG(INFO) << "Unable to find sufficient scratch";
1018 TEST_AND_RETURN_FALSE(ConvertCutsToFull(graph,
1019 new_root,
1020 data_fd,
1021 data_file_size,
1022 op_indexes,
1023 reverse_op_indexes,
1024 cuts));
1025 return true;
1026 }
1027 // Use the scratch we found
1028 TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found);
1029
1030 // Make all the suppliers depend on this node
1031 for (SupplierVector::iterator it = block_suppliers.begin(),
1032 e = block_suppliers.end(); it != e; ++it) {
1033 graph_utils::AddReadBeforeDepExtents(
1034 &(*graph)[it->first],
1035 old_dst,
1036 it->second.GetExtentsForBlockCount(it->second.blocks()));
1037 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -07001038
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001039 // Replace temp blocks in each cut
1040 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
1041 e = cuts.end(); it != e; ++it) {
1042 vector<Extent> real_extents =
1043 scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]);
1044 scratch_ranges.SubtractExtents(real_extents);
1045
1046 // Fix the old dest node w/ the real blocks
1047 DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst],
1048 it->tmp_extents,
1049 real_extents);
1050
1051 // Fix the new node w/ the real blocks. Since the new node is just a
1052 // copy operation, we can replace all the dest extents w/ the real
1053 // blocks.
1054 DeltaArchiveManifest_InstallOperation *op =
1055 &(*graph)[it->new_vertex].op;
1056 op->clear_dst_extents();
1057 DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents());
1058 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001059 return true;
1060}
1061
Andrew de los Reyesef017552010-10-06 17:57:52 -07001062} // namespace {}
1063
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001064// Returns true if |op| is a no-op operation that doesn't do any useful work
1065// (e.g., a move operation that copies blocks onto themselves).
1066bool DeltaDiffGenerator::IsNoopOperation(
1067 const DeltaArchiveManifest_InstallOperation& op) {
1068 return (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE &&
1069 ExpandExtents(op.src_extents()) == ExpandExtents(op.dst_extents()));
1070}
1071
Andrew de los Reyesef017552010-10-06 17:57:52 -07001072bool DeltaDiffGenerator::AssignTempBlocks(
1073 Graph* graph,
1074 const string& new_root,
1075 int data_fd,
1076 off_t* data_file_size,
1077 vector<Vertex::Index>* op_indexes,
1078 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001079 const vector<CutEdgeVertexes>& cuts) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001080 CHECK(!cuts.empty());
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001081
1082 // group of cuts w/ the same old_dst:
1083 vector<CutEdgeVertexes> cuts_group;
1084
Andrew de los Reyesef017552010-10-06 17:57:52 -07001085 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
1086 true ; --i) {
1087 LOG(INFO) << "Fixing temp blocks in cut " << i
1088 << ": old dst: " << cuts[i].old_dst << " new vertex: "
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001089 << cuts[i].new_vertex << " path: "
1090 << (*graph)[cuts[i].old_dst].file_name;
1091
1092 if (cuts_group.empty() || (cuts_group[0].old_dst == cuts[i].old_dst)) {
1093 cuts_group.push_back(cuts[i]);
1094 } else {
1095 CHECK(!cuts_group.empty());
1096 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1097 new_root,
1098 data_fd,
1099 data_file_size,
1100 op_indexes,
1101 reverse_op_indexes,
1102 cuts_group));
1103 cuts_group.clear();
1104 cuts_group.push_back(cuts[i]);
Andrew de los Reyesef017552010-10-06 17:57:52 -07001105 }
Darin Petkov36a58222010-10-07 22:00:09 -07001106
Andrew de los Reyesef017552010-10-06 17:57:52 -07001107 if (i == e) {
1108 // break out of for() loop
1109 break;
1110 }
1111 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001112 CHECK(!cuts_group.empty());
1113 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1114 new_root,
1115 data_fd,
1116 data_file_size,
1117 op_indexes,
1118 reverse_op_indexes,
1119 cuts_group));
Andrew de los Reyesef017552010-10-06 17:57:52 -07001120 return true;
1121}
1122
1123bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1124 size_t idx = 0;
1125 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1126 ++it, ++idx) {
1127 if (!it->valid)
1128 continue;
1129 const DeltaArchiveManifest_InstallOperation& op = it->op;
1130 if (TempBlocksExistInExtents(op.dst_extents()) ||
1131 TempBlocksExistInExtents(op.src_extents())) {
1132 LOG(INFO) << "bad extents in node " << idx;
1133 LOG(INFO) << "so yeah";
1134 return false;
1135 }
1136
1137 // Check out-edges:
1138 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1139 je = it->out_edges.end(); jt != je; ++jt) {
1140 if (TempBlocksExistInExtents(jt->second.extents) ||
1141 TempBlocksExistInExtents(jt->second.write_extents)) {
1142 LOG(INFO) << "bad out edge in node " << idx;
1143 LOG(INFO) << "so yeah";
1144 return false;
1145 }
1146 }
1147 }
1148 return true;
1149}
1150
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001151bool DeltaDiffGenerator::ReorderDataBlobs(
1152 DeltaArchiveManifest* manifest,
1153 const std::string& data_blobs_path,
1154 const std::string& new_data_blobs_path) {
1155 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1156 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1157 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001158
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001159 DirectFileWriter writer;
1160 TEST_AND_RETURN_FALSE(
1161 writer.Open(new_data_blobs_path.c_str(),
1162 O_WRONLY | O_TRUNC | O_CREAT,
1163 0644) == 0);
1164 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001165 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001166
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001167 for (int i = 0; i < (manifest->install_operations_size() +
1168 manifest->kernel_install_operations_size()); i++) {
1169 DeltaArchiveManifest_InstallOperation* op = NULL;
1170 if (i < manifest->install_operations_size()) {
1171 op = manifest->mutable_install_operations(i);
1172 } else {
1173 op = manifest->mutable_kernel_install_operations(
1174 i - manifest->install_operations_size());
1175 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001176 if (!op->has_data_offset())
1177 continue;
1178 CHECK(op->has_data_length());
1179 vector<char> buf(op->data_length());
1180 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1181 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1182
1183 op->set_data_offset(out_file_size);
Don Garrette410e0f2011-11-10 15:39:01 -08001184 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001185 out_file_size += buf.size();
1186 }
1187 return true;
1188}
1189
Andrew de los Reyesef017552010-10-06 17:57:52 -07001190bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1191 const CutEdgeVertexes& cut,
1192 const string& new_root,
1193 int data_fd,
1194 off_t* data_file_size) {
1195 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001196
Andrew de los Reyesef017552010-10-06 17:57:52 -07001197 // Keep all outgoing edges
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001198 if ((*graph)[cut.old_dst].op.type() !=
1199 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ &&
1200 (*graph)[cut.old_dst].op.type() !=
1201 DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
1202 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1203 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001204
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001205 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1206 cut.old_dst,
1207 NULL,
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -08001208 kNonexistentPath,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001209 new_root,
1210 (*graph)[cut.old_dst].file_name,
1211 data_fd,
1212 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001213
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001214 (*graph)[cut.old_dst].out_edges = out_edges;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001215
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001216 // Right now we don't have doubly-linked edges, so we have to scan
1217 // the whole graph.
1218 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1219 }
Andrew de los Reyesef017552010-10-06 17:57:52 -07001220
1221 // Delete temp node
1222 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1223 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1224 (*graph)[cut.old_dst].out_edges.end());
1225 (*graph)[cut.new_vertex].valid = false;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001226 LOG(INFO) << "marked node invalid: " << cut.new_vertex;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001227 return true;
1228}
1229
1230bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1231 const string& new_root,
1232 int fd,
1233 off_t* data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001234 vector<Vertex::Index>* final_order,
1235 Vertex::Index scratch_vertex) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001236 CycleBreaker cycle_breaker;
1237 LOG(INFO) << "Finding cycles...";
1238 set<Edge> cut_edges;
1239 cycle_breaker.BreakCycles(*graph, &cut_edges);
1240 LOG(INFO) << "done finding cycles";
1241 CheckGraph(*graph);
1242
1243 // Calculate number of scratch blocks needed
1244
1245 LOG(INFO) << "Cutting cycles...";
1246 vector<CutEdgeVertexes> cuts;
1247 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1248 LOG(INFO) << "done cutting cycles";
1249 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1250 CheckGraph(*graph);
1251
1252 LOG(INFO) << "Creating initial topological order...";
1253 TopologicalSort(*graph, final_order);
1254 LOG(INFO) << "done with initial topo order";
1255 CheckGraph(*graph);
1256
1257 LOG(INFO) << "Moving full ops to the back";
1258 MoveFullOpsToBack(graph, final_order);
1259 LOG(INFO) << "done moving full ops to back";
1260
1261 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1262 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1263
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001264 SortCutsByTopoOrder(*final_order, &cuts);
1265
Andrew de los Reyesef017552010-10-06 17:57:52 -07001266 if (!cuts.empty())
1267 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1268 new_root,
1269 fd,
1270 data_file_size,
1271 final_order,
1272 &inverse_final_order,
1273 cuts));
1274 LOG(INFO) << "Making sure all temp blocks have been allocated";
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001275
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001276 // Remove the scratch node, if any
1277 if (scratch_vertex != Vertex::kInvalidIndex) {
1278 final_order->erase(final_order->begin() +
1279 inverse_final_order[scratch_vertex]);
1280 (*graph)[scratch_vertex].valid = false;
1281 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1282 }
1283
Andrew de los Reyesef017552010-10-06 17:57:52 -07001284 graph_utils::DumpGraph(*graph);
1285 CHECK(NoTempBlocksRemain(*graph));
1286 LOG(INFO) << "done making sure all temp blocks are allocated";
1287 return true;
1288}
1289
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001290void DeltaDiffGenerator::CreateScratchNode(uint64_t start_block,
1291 uint64_t num_blocks,
1292 Vertex* vertex) {
1293 vertex->file_name = "<scratch>";
1294 vertex->op.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
1295 vertex->op.set_data_offset(0);
1296 vertex->op.set_data_length(0);
1297 Extent* extent = vertex->op.add_dst_extents();
1298 extent->set_start_block(start_block);
1299 extent->set_num_blocks(num_blocks);
1300}
1301
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001302bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1303 const string& old_root,
1304 const string& old_image,
1305 const string& new_root,
1306 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001307 const string& old_kernel_part,
1308 const string& new_kernel_part,
1309 const string& output_path,
1310 const string& private_key_path) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001311 int old_image_block_count = 0, old_image_block_size = 0;
1312 int new_image_block_count = 0, new_image_block_size = 0;
1313 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1314 &new_image_block_count,
1315 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001316 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001317 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1318 &old_image_block_count,
1319 &old_image_block_size));
1320 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1321 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1322 << "Old and new images have different block counts.";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001323 }
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001324 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001325 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1326
Darin Petkov7ea32332010-10-13 10:46:11 -07001327 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1328 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1329 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001330 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1331 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1332 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1333 }
1334 Graph graph;
1335 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001336
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001337 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
1338 string temp_file_path;
Darin Petkov7438a5c2011-08-29 11:56:44 -07001339 scoped_ptr<ScopedPathUnlinker> temp_file_unlinker;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001340 off_t data_file_size = 0;
1341
1342 LOG(INFO) << "Reading files...";
1343
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001344 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1345
Andrew de los Reyesef017552010-10-06 17:57:52 -07001346 vector<Vertex::Index> final_order;
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001347 Vertex::Index scratch_vertex = Vertex::kInvalidIndex;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001348 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001349 int fd;
1350 TEST_AND_RETURN_FALSE(
1351 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001352 temp_file_unlinker.reset(new ScopedPathUnlinker(temp_file_path));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001353 TEST_AND_RETURN_FALSE(fd >= 0);
1354 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001355 if (!old_image.empty()) {
1356 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001357
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001358 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1359 &blocks,
1360 old_root,
1361 new_root,
1362 fd,
1363 &data_file_size));
1364 LOG(INFO) << "done reading normal files";
1365 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001366
Thieu Le5c7d9752010-12-15 16:09:28 -08001367 LOG(INFO) << "Starting metadata processing";
1368 TEST_AND_RETURN_FALSE(Metadata::DeltaReadMetadata(&graph,
1369 &blocks,
1370 old_image,
1371 new_image,
1372 fd,
1373 &data_file_size));
1374 LOG(INFO) << "Done metadata processing";
1375 CheckGraph(graph);
1376
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001377 graph.resize(graph.size() + 1);
1378 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1379 fd,
1380 &data_file_size,
1381 new_image,
1382 &graph.back()));
1383
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001384 // Final scratch block (if there's space)
1385 if (blocks.size() < (kRootFSPartitionSize / kBlockSize)) {
1386 scratch_vertex = graph.size();
1387 graph.resize(graph.size() + 1);
1388 CreateScratchNode(blocks.size(),
1389 (kRootFSPartitionSize / kBlockSize) - blocks.size(),
1390 &graph.back());
1391 }
1392
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001393 // Read kernel partition
1394 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1395 new_kernel_part,
1396 &kernel_ops,
1397 fd,
1398 &data_file_size));
1399
1400 LOG(INFO) << "done reading kernel";
1401 CheckGraph(graph);
1402
1403 LOG(INFO) << "Creating edges...";
1404 CreateEdges(&graph, blocks);
1405 LOG(INFO) << "Done creating edges";
1406 CheckGraph(graph);
1407
1408 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1409 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001410 fd,
1411 &data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001412 &final_order,
1413 scratch_vertex));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001414 } else {
1415 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001416 off_t new_image_size =
1417 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Darin Petkov7a22d792010-11-08 14:10:00 -08001418 TEST_AND_RETURN_FALSE(FullUpdateGenerator::Run(&graph,
1419 new_kernel_part,
1420 new_image,
1421 new_image_size,
1422 fd,
1423 &data_file_size,
1424 kFullUpdateChunkSize,
1425 kBlockSize,
1426 &kernel_ops,
1427 &final_order));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001428 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001429 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001430
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001431 // Convert to protobuf Manifest object
1432 DeltaArchiveManifest manifest;
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001433 OperationNameMap op_name_map;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001434 CheckGraph(graph);
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001435 InstallOperationsToManifest(graph,
1436 final_order,
1437 kernel_ops,
1438 &manifest,
1439 &op_name_map);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001440 CheckGraph(graph);
1441 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001442
1443 // Reorder the data blobs with the newly ordered manifest
1444 string ordered_blobs_path;
1445 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
1446 "/tmp/CrAU_temp_data.ordered.XXXXXX",
1447 &ordered_blobs_path,
Andrew de los Reyese05fc282011-06-02 09:50:08 -07001448 NULL));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001449 ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001450 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1451 temp_file_path,
1452 ordered_blobs_path));
Darin Petkov7438a5c2011-08-29 11:56:44 -07001453 temp_file_unlinker.reset();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001454
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001455 // Check that install op blobs are in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001456 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001457 {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001458 for (int i = 0; i < (manifest.install_operations_size() +
1459 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001460 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001461 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001462 manifest.mutable_install_operations(i) :
1463 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001464 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001465 if (op->has_data_offset()) {
1466 if (op->data_offset() != next_blob_offset) {
1467 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001468 << next_blob_offset;
1469 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001470 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001471 }
1472 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001473 }
1474
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001475 // Signatures appear at the end of the blobs. Note the offset in the
1476 // manifest
1477 if (!private_key_path.empty()) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001478 uint64_t signature_blob_length = 0;
1479 TEST_AND_RETURN_FALSE(
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -07001480 PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path),
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001481 &signature_blob_length));
Darin Petkov9574f7e2011-01-13 10:48:12 -08001482 AddSignatureOp(next_blob_offset, signature_blob_length, &manifest);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001483 }
1484
Darin Petkov36a58222010-10-07 22:00:09 -07001485 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1486 new_kernel_part,
1487 old_image,
1488 new_image,
1489 &manifest));
1490
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001491 // Serialize protobuf
1492 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001493
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001494 CheckGraph(graph);
1495 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1496 CheckGraph(graph);
1497
1498 LOG(INFO) << "Writing final delta file header...";
1499 DirectFileWriter writer;
1500 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1501 O_WRONLY | O_CREAT | O_TRUNC,
1502 0644) == 0);
1503 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001504
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001505 // Write header
Don Garrette410e0f2011-11-10 15:39:01 -08001506 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001507
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001508 // Write version number
1509 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001510
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001511 // Write protobuf length
1512 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1513 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001514
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001515 // Write protobuf
1516 LOG(INFO) << "Writing final delta file protobuf... "
1517 << serialized_manifest.size();
1518 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
Don Garrette410e0f2011-11-10 15:39:01 -08001519 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001520
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001521 // Append the data blobs
1522 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001523 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001524 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1525 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1526 for (;;) {
1527 char buf[kBlockSize];
1528 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1529 if (0 == rc) {
1530 // EOF
1531 break;
1532 }
1533 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
Don Garrette410e0f2011-11-10 15:39:01 -08001534 TEST_AND_RETURN_FALSE(writer.Write(buf, rc));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001535 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001536
1537 // Write signature blob.
1538 if (!private_key_path.empty()) {
1539 LOG(INFO) << "Signing the update...";
1540 vector<char> signature_blob;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -07001541 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
1542 output_path,
1543 vector<string>(1, private_key_path),
1544 &signature_blob));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001545 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
Don Garrette410e0f2011-11-10 15:39:01 -08001546 signature_blob.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001547 }
1548
Darin Petkov95cf01f2010-10-12 14:59:13 -07001549 int64_t manifest_metadata_size =
1550 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001551 ReportPayloadUsage(manifest, manifest_metadata_size, op_name_map);
Darin Petkov880335c2010-10-01 15:52:53 -07001552
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001553 LOG(INFO) << "All done. Successfully created delta file.";
1554 return true;
1555}
1556
Thieu Le5c7d9752010-12-15 16:09:28 -08001557// Runs the bsdiff tool on two files and returns the resulting delta in
1558// 'out'. Returns true on success.
1559bool DeltaDiffGenerator::BsdiffFiles(const string& old_file,
1560 const string& new_file,
1561 vector<char>* out) {
1562 const string kPatchFile = "/tmp/delta.patchXXXXXX";
1563 string patch_file_path;
1564
1565 TEST_AND_RETURN_FALSE(
1566 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
1567
1568 vector<string> cmd;
1569 cmd.push_back(kBsdiffPath);
1570 cmd.push_back(old_file);
1571 cmd.push_back(new_file);
1572 cmd.push_back(patch_file_path);
1573
1574 int rc = 1;
1575 vector<char> patch_file;
Darin Petkov85d02b72011-05-17 13:25:51 -07001576 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc, NULL));
Thieu Le5c7d9752010-12-15 16:09:28 -08001577 TEST_AND_RETURN_FALSE(rc == 0);
1578 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
1579 unlink(patch_file_path.c_str());
1580 return true;
1581}
1582
1583// The |blocks| vector contains a reader and writer for each block on the
1584// filesystem that's being in-place updated. We populate the reader/writer
1585// fields of |blocks| by calling this function.
1586// For each block in |operation| that is read or written, find that block
1587// in |blocks| and set the reader/writer field to the vertex passed.
1588// |graph| is not strictly necessary, but useful for printing out
1589// error messages.
1590bool DeltaDiffGenerator::AddInstallOpToBlocksVector(
1591 const DeltaArchiveManifest_InstallOperation& operation,
1592 const Graph& graph,
1593 Vertex::Index vertex,
1594 vector<Block>* blocks) {
1595 // See if this is already present.
1596 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
1597
1598 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
1599 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
1600 const int extents_size =
1601 (field == READER) ? operation.src_extents_size() :
1602 operation.dst_extents_size();
1603 const char* past_participle = (field == READER) ? "read" : "written";
1604 const google::protobuf::RepeatedPtrField<Extent>& extents =
1605 (field == READER) ? operation.src_extents() : operation.dst_extents();
1606 Vertex::Index Block::*access_type =
1607 (field == READER) ? &Block::reader : &Block::writer;
1608
1609 for (int i = 0; i < extents_size; i++) {
1610 const Extent& extent = extents.Get(i);
1611 if (extent.start_block() == kSparseHole) {
1612 // Hole in sparse file. skip
1613 continue;
1614 }
1615 for (uint64_t block = extent.start_block();
1616 block < (extent.start_block() + extent.num_blocks()); block++) {
1617 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
1618 LOG(FATAL) << "Block " << block << " is already "
1619 << past_participle << " by "
1620 << (*blocks)[block].*access_type << "("
1621 << graph[(*blocks)[block].*access_type].file_name
1622 << ") and also " << vertex << "("
1623 << graph[vertex].file_name << ")";
1624 }
1625 (*blocks)[block].*access_type = vertex;
1626 }
1627 }
1628 }
1629 return true;
1630}
1631
Darin Petkov9574f7e2011-01-13 10:48:12 -08001632void DeltaDiffGenerator::AddSignatureOp(uint64_t signature_blob_offset,
1633 uint64_t signature_blob_length,
1634 DeltaArchiveManifest* manifest) {
1635 LOG(INFO) << "Making room for signature in file";
1636 manifest->set_signatures_offset(signature_blob_offset);
1637 LOG(INFO) << "set? " << manifest->has_signatures_offset();
1638 // Add a dummy op at the end to appease older clients
1639 DeltaArchiveManifest_InstallOperation* dummy_op =
1640 manifest->add_kernel_install_operations();
1641 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1642 dummy_op->set_data_offset(signature_blob_offset);
1643 manifest->set_signatures_offset(signature_blob_offset);
1644 dummy_op->set_data_length(signature_blob_length);
1645 manifest->set_signatures_size(signature_blob_length);
1646 Extent* dummy_extent = dummy_op->add_dst_extents();
1647 // Tell the dummy op to write this data to a big sparse hole
1648 dummy_extent->set_start_block(kSparseHole);
1649 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1650 kBlockSize);
1651}
1652
Andrew de los Reyes50f36492010-11-01 13:57:12 -07001653const char* const kBsdiffPath = "bsdiff";
1654const char* const kBspatchPath = "bspatch";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001655const char* const kDeltaMagic = "CrAU";
1656
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001657}; // namespace chromeos_update_engine