blob: 4f71c498283a2d170ffaa57b6d7fa1b29cfd1413 [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"
Thieu Le5c7d9752010-12-15 16:09:28 -080033#include "update_engine/metadata.h"
Darin Petkov36a58222010-10-07 22:00:09 -070034#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070035#include "update_engine/payload_signer.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070036#include "update_engine/subprocess.h"
37#include "update_engine/topological_sort.h"
38#include "update_engine/update_metadata.pb.h"
39#include "update_engine/utils.h"
40
41using std::make_pair;
Andrew de los Reyesef017552010-10-06 17:57:52 -070042using std::map;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070043using std::max;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070044using std::min;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -070045using std::pair;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070046using std::set;
47using std::string;
48using std::vector;
49
50namespace chromeos_update_engine {
51
52typedef DeltaDiffGenerator::Block Block;
Darin Petkov9fa7ec52010-10-18 11:45:23 -070053typedef map<const DeltaArchiveManifest_InstallOperation*,
54 const string*> OperationNameMap;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070055
56namespace {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070057const size_t kBlockSize = 4096; // bytes
Andrew de los Reyes927179d2010-12-02 11:26:48 -080058
59// TODO(adlr): switch from 1GiB to 2GiB when we no longer care about old
60// clients:
Darin Petkov9eadd642010-10-14 15:20:57 -070061const size_t kRootFSPartitionSize = 1 * 1024 * 1024 * 1024; // bytes
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070062const uint64_t kVersionNumber = 1;
Darin Petkov9eadd642010-10-14 15:20:57 -070063const uint64_t kFullUpdateChunkSize = 1024 * 1024; // bytes
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070064
Darin Petkov68c10d12010-10-14 09:24:37 -070065static const char* kInstallOperationTypes[] = {
66 "REPLACE",
67 "REPLACE_BZ",
68 "MOVE",
69 "BSDIFF"
70};
71
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070072// Stores all Extents for a file into 'out'. Returns true on success.
73bool GatherExtents(const string& path,
74 google::protobuf::RepeatedPtrField<Extent>* out) {
75 vector<Extent> extents;
76 TEST_AND_RETURN_FALSE(extent_mapper::ExtentsForFileFibmap(path, &extents));
77 DeltaDiffGenerator::StoreExtents(extents, out);
78 return true;
79}
80
Andrew de los Reyesef017552010-10-06 17:57:52 -070081// For a given regular file which must exist at new_root + path, and
82// may exist at old_root + path, creates a new InstallOperation and
83// adds it to the graph. Also, populates the |blocks| array as
84// necessary, if |blocks| is non-NULL. Also, writes the data
85// necessary to send the file down to the client into data_fd, which
86// has length *data_file_size. *data_file_size is updated
87// appropriately. If |existing_vertex| is no kInvalidIndex, use that
88// rather than allocating a new vertex. Returns true on success.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070089bool DeltaReadFile(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -070090 Vertex::Index existing_vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070091 vector<Block>* blocks,
92 const string& old_root,
93 const string& new_root,
94 const string& path, // within new_root
95 int data_fd,
96 off_t* data_file_size) {
97 vector<char> data;
98 DeltaArchiveManifest_InstallOperation operation;
99
100 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_root + path,
101 new_root + path,
102 &data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700103 &operation,
104 true));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700105
106 // Write the data
107 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
108 operation.set_data_offset(*data_file_size);
109 operation.set_data_length(data.size());
110 }
111
112 TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size()));
113 *data_file_size += data.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700114
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700115 // Now, insert into graph and blocks vector
Andrew de los Reyesef017552010-10-06 17:57:52 -0700116 Vertex::Index vertex = existing_vertex;
117 if (vertex == Vertex::kInvalidIndex) {
118 graph->resize(graph->size() + 1);
119 vertex = graph->size() - 1;
120 }
121 (*graph)[vertex].op = operation;
122 CHECK((*graph)[vertex].op.has_type());
123 (*graph)[vertex].file_name = path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700124
Andrew de los Reyesef017552010-10-06 17:57:52 -0700125 if (blocks)
Thieu Le5c7d9752010-12-15 16:09:28 -0800126 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::AddInstallOpToBlocksVector(
127 (*graph)[vertex].op,
128 *graph,
129 vertex,
130 blocks));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700131 return true;
132}
133
134// For each regular file within new_root, creates a node in the graph,
135// determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF),
136// and writes any necessary data to the end of data_fd.
137bool DeltaReadFiles(Graph* graph,
138 vector<Block>* blocks,
139 const string& old_root,
140 const string& new_root,
141 int data_fd,
142 off_t* data_file_size) {
143 set<ino_t> visited_inodes;
144 for (FilesystemIterator fs_iter(new_root,
145 utils::SetWithValue<string>("/lost+found"));
146 !fs_iter.IsEnd(); fs_iter.Increment()) {
147 if (!S_ISREG(fs_iter.GetStat().st_mode))
148 continue;
149
150 // Make sure we visit each inode only once.
151 if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino))
152 continue;
153 visited_inodes.insert(fs_iter.GetStat().st_ino);
154 if (fs_iter.GetStat().st_size == 0)
155 continue;
156
157 LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700158
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700159 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700160 Vertex::kInvalidIndex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700161 blocks,
162 old_root,
163 new_root,
164 fs_iter.GetPartialPath(),
165 data_fd,
166 data_file_size));
167 }
168 return true;
169}
170
Andrew de los Reyesef017552010-10-06 17:57:52 -0700171// This class allocates non-existent temp blocks, starting from
172// kTempBlockStart. Other code is responsible for converting these
173// temp blocks into real blocks, as the client can't read or write to
174// these blocks.
175class DummyExtentAllocator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700176 public:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700177 explicit DummyExtentAllocator()
178 : next_block_(kTempBlockStart) {}
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700179 vector<Extent> Allocate(const uint64_t block_count) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700180 vector<Extent> ret(1);
181 ret[0].set_start_block(next_block_);
182 ret[0].set_num_blocks(block_count);
183 next_block_ += block_count;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700184 return ret;
185 }
186 private:
Andrew de los Reyesef017552010-10-06 17:57:52 -0700187 uint64_t next_block_;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700188};
189
190// Reads blocks from image_path that are not yet marked as being written
191// in the blocks array. These blocks that remain are non-file-data blocks.
192// In the future we might consider intelligent diffing between this data
193// and data in the previous image, but for now we just bzip2 compress it
194// and include it in the update.
195// Creates a new node in the graph to write these blocks and writes the
196// appropriate blob to blobs_fd. Reads and updates blobs_length;
197bool ReadUnwrittenBlocks(const vector<Block>& blocks,
198 int blobs_fd,
199 off_t* blobs_length,
200 const string& image_path,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700201 Vertex* vertex) {
Darin Petkovabe7cc92010-10-08 12:29:32 -0700202 vertex->file_name = "<rootfs-non-file-data>";
203
Andrew de los Reyesef017552010-10-06 17:57:52 -0700204 DeltaArchiveManifest_InstallOperation* out_op = &vertex->op;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700205 int image_fd = open(image_path.c_str(), O_RDONLY, 000);
206 TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0);
207 ScopedFdCloser image_fd_closer(&image_fd);
208
209 string temp_file_path;
210 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX",
211 &temp_file_path,
212 NULL));
213
214 FILE* file = fopen(temp_file_path.c_str(), "w");
215 TEST_AND_RETURN_FALSE(file);
216 int err = BZ_OK;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700217
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700218 BZFILE* bz_file = BZ2_bzWriteOpen(&err,
219 file,
220 9, // max compression
221 0, // verbosity
222 0); // default work factor
223 TEST_AND_RETURN_FALSE(err == BZ_OK);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700224
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700225 vector<Extent> extents;
226 vector<Block>::size_type block_count = 0;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700227
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700228 LOG(INFO) << "Appending left over blocks to extents";
229 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
230 if (blocks[i].writer != Vertex::kInvalidIndex)
231 continue;
Andrew de los Reyesef017552010-10-06 17:57:52 -0700232 if (blocks[i].reader != Vertex::kInvalidIndex) {
233 graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i);
234 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700235 graph_utils::AppendBlockToExtents(&extents, i);
236 block_count++;
237 }
238
239 // Code will handle 'buf' at any size that's a multiple of kBlockSize,
240 // so we arbitrarily set it to 1024 * kBlockSize.
241 vector<char> buf(1024 * kBlockSize);
242
243 LOG(INFO) << "Reading left over blocks";
244 vector<Block>::size_type blocks_copied_count = 0;
245
246 // For each extent in extents, write the data into BZ2_bzWrite which
247 // sends it to an output file.
248 // We use the temporary buffer 'buf' to hold the data, which may be
249 // smaller than the extent, so in that case we have to loop to get
250 // the extent's data (that's the inner while loop).
251 for (vector<Extent>::const_iterator it = extents.begin();
252 it != extents.end(); ++it) {
253 vector<Block>::size_type blocks_read = 0;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800254 float printed_progress = -1;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700255 while (blocks_read < it->num_blocks()) {
256 const int copy_block_cnt =
257 min(buf.size() / kBlockSize,
258 static_cast<vector<char>::size_type>(
259 it->num_blocks() - blocks_read));
260 ssize_t rc = pread(image_fd,
261 &buf[0],
262 copy_block_cnt * kBlockSize,
263 (it->start_block() + blocks_read) * kBlockSize);
264 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
265 TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) ==
266 copy_block_cnt * kBlockSize);
267 BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize);
268 TEST_AND_RETURN_FALSE(err == BZ_OK);
269 blocks_read += copy_block_cnt;
270 blocks_copied_count += copy_block_cnt;
Andrew de los Reyes4b8740f2010-11-08 17:09:11 -0800271 float current_progress =
272 static_cast<float>(blocks_copied_count) / block_count;
273 if (printed_progress + 0.1 < current_progress ||
274 blocks_copied_count == block_count) {
275 LOG(INFO) << "progress: " << current_progress;
276 printed_progress = current_progress;
277 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700278 }
279 }
280 BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL);
281 TEST_AND_RETURN_FALSE(err == BZ_OK);
282 bz_file = NULL;
283 TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file));
284 file = NULL;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700285
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700286 vector<char> compressed_data;
287 LOG(INFO) << "Reading compressed data off disk";
288 TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data));
289 TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700290
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700291 // Add node to graph to write these blocks
292 out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
293 out_op->set_data_offset(*blobs_length);
294 out_op->set_data_length(compressed_data.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700295 LOG(INFO) << "Rootfs non-data blocks compressed take up "
296 << compressed_data.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700297 *blobs_length += compressed_data.size();
298 out_op->set_dst_length(kBlockSize * block_count);
299 DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700300
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700301 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd,
302 &compressed_data[0],
303 compressed_data.size()));
304 LOG(INFO) << "done with extra blocks";
305 return true;
306}
307
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700308// Writes the uint64_t passed in in host-endian to the file as big-endian.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700309// Returns true on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700310bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
311 uint64_t value_be = htobe64(value);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700312 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)) ==
313 sizeof(value_be));
314 return true;
315}
316
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700317// Adds each operation from |graph| to |out_manifest| in the order specified by
318// |order| while building |out_op_name_map| with operation to name
319// mappings. Adds all |kernel_ops| to |out_manifest|. Filters out no-op
320// operations.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700321void InstallOperationsToManifest(
322 const Graph& graph,
323 const vector<Vertex::Index>& order,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700324 const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops,
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700325 DeltaArchiveManifest* out_manifest,
326 OperationNameMap* out_op_name_map) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700327 for (vector<Vertex::Index>::const_iterator it = order.begin();
328 it != order.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700329 const Vertex& vertex = graph[*it];
330 const DeltaArchiveManifest_InstallOperation& add_op = vertex.op;
331 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
332 continue;
333 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700334 DeltaArchiveManifest_InstallOperation* op =
335 out_manifest->add_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700336 *op = add_op;
337 (*out_op_name_map)[op] = &vertex.file_name;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700338 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700339 for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it =
340 kernel_ops.begin(); it != kernel_ops.end(); ++it) {
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700341 const DeltaArchiveManifest_InstallOperation& add_op = *it;
342 if (DeltaDiffGenerator::IsNoopOperation(add_op)) {
343 continue;
344 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700345 DeltaArchiveManifest_InstallOperation* op =
346 out_manifest->add_kernel_install_operations();
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700347 *op = add_op;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700348 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700349}
350
351void CheckGraph(const Graph& graph) {
352 for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) {
353 CHECK(it->op.has_type());
354 }
355}
356
Darin Petkov68c10d12010-10-14 09:24:37 -0700357// Delta compresses a kernel partition |new_kernel_part| with knowledge of the
358// old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty
359// string, generates a full update of the partition.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700360bool DeltaCompressKernelPartition(
361 const string& old_kernel_part,
362 const string& new_kernel_part,
363 vector<DeltaArchiveManifest_InstallOperation>* ops,
364 int blobs_fd,
365 off_t* blobs_length) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700366 LOG(INFO) << "Delta compressing kernel partition...";
Darin Petkov68c10d12010-10-14 09:24:37 -0700367 LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update...";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700368
369 // Add a new install operation
370 ops->resize(1);
371 DeltaArchiveManifest_InstallOperation* op = &(*ops)[0];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700372
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700373 vector<char> data;
Darin Petkov68c10d12010-10-14 09:24:37 -0700374 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_kernel_part,
375 new_kernel_part,
376 &data,
377 op,
378 false));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700379
Darin Petkov68c10d12010-10-14 09:24:37 -0700380 // Write the data
381 if (op->type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) {
382 op->set_data_offset(*blobs_length);
383 op->set_data_length(data.size());
384 }
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700385
Darin Petkov68c10d12010-10-14 09:24:37 -0700386 TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size()));
387 *blobs_length += data.size();
Andrew de los Reyes36f37362010-09-03 09:20:04 -0700388
Darin Petkov68c10d12010-10-14 09:24:37 -0700389 LOG(INFO) << "Done delta compressing kernel partition: "
390 << kInstallOperationTypes[op->type()];
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700391 return true;
392}
393
Darin Petkov880335c2010-10-01 15:52:53 -0700394struct DeltaObject {
395 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
396 : name(in_name),
397 type(in_type),
398 size(in_size) {}
399 bool operator <(const DeltaObject& object) const {
Darin Petkovd43d6902010-10-14 11:17:50 -0700400 return (size != object.size) ? (size < object.size) : (name < object.name);
Darin Petkov880335c2010-10-01 15:52:53 -0700401 }
402 string name;
403 int type;
404 off_t size;
405};
406
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700407void ReportPayloadUsage(const DeltaArchiveManifest& manifest,
408 const int64_t manifest_metadata_size,
409 const OperationNameMap& op_name_map) {
Darin Petkov880335c2010-10-01 15:52:53 -0700410 vector<DeltaObject> objects;
411 off_t total_size = 0;
412
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700413 // Rootfs install operations.
414 for (int i = 0; i < manifest.install_operations_size(); ++i) {
415 const DeltaArchiveManifest_InstallOperation& op =
416 manifest.install_operations(i);
417 objects.push_back(DeltaObject(*op_name_map.find(&op)->second,
418 op.type(),
419 op.data_length()));
420 total_size += op.data_length();
Darin Petkov880335c2010-10-01 15:52:53 -0700421 }
422
Darin Petkov880335c2010-10-01 15:52:53 -0700423 // Kernel install operations.
424 for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) {
425 const DeltaArchiveManifest_InstallOperation& op =
426 manifest.kernel_install_operations(i);
427 objects.push_back(DeltaObject(StringPrintf("<kernel-operation-%d>", i),
428 op.type(),
429 op.data_length()));
430 total_size += op.data_length();
431 }
432
Darin Petkov95cf01f2010-10-12 14:59:13 -0700433 objects.push_back(DeltaObject("<manifest-metadata>",
434 -1,
435 manifest_metadata_size));
436 total_size += manifest_metadata_size;
437
Darin Petkov880335c2010-10-01 15:52:53 -0700438 std::sort(objects.begin(), objects.end());
439
440 static const char kFormatString[] = "%6.2f%% %10llu %-10s %s\n";
441 for (vector<DeltaObject>::const_iterator it = objects.begin();
442 it != objects.end(); ++it) {
443 const DeltaObject& object = *it;
444 fprintf(stderr, kFormatString,
445 object.size * 100.0 / total_size,
446 object.size,
Darin Petkov95cf01f2010-10-12 14:59:13 -0700447 object.type >= 0 ? kInstallOperationTypes[object.type] : "-",
Darin Petkov880335c2010-10-01 15:52:53 -0700448 object.name.c_str());
449 }
450 fprintf(stderr, kFormatString, 100.0, total_size, "", "<total>");
451}
452
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700453} // namespace {}
454
455bool DeltaDiffGenerator::ReadFileToDiff(
456 const string& old_filename,
457 const string& new_filename,
458 vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700459 DeltaArchiveManifest_InstallOperation* out_op,
460 bool gather_extents) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700461 // Read new data in
462 vector<char> new_data;
463 TEST_AND_RETURN_FALSE(utils::ReadFile(new_filename, &new_data));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700464
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700465 TEST_AND_RETURN_FALSE(!new_data.empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700466
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700467 vector<char> new_data_bz;
468 TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz));
469 CHECK(!new_data_bz.empty());
470
471 vector<char> data; // Data blob that will be written to delta file.
472
473 DeltaArchiveManifest_InstallOperation operation;
474 size_t current_best_size = 0;
475 if (new_data.size() <= new_data_bz.size()) {
476 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
477 current_best_size = new_data.size();
478 data = new_data;
479 } else {
480 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
481 current_best_size = new_data_bz.size();
482 data = new_data_bz;
483 }
484
485 // Do we have an original file to consider?
486 struct stat old_stbuf;
Darin Petkov68c10d12010-10-14 09:24:37 -0700487 bool no_original = old_filename.empty();
488 if (!no_original && 0 != stat(old_filename.c_str(), &old_stbuf)) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700489 // If stat-ing the old file fails, it should be because it doesn't exist.
490 TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT);
Darin Petkov68c10d12010-10-14 09:24:37 -0700491 no_original = true;
492 }
493 if (!no_original) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700494 // Read old data
495 vector<char> old_data;
496 TEST_AND_RETURN_FALSE(utils::ReadFile(old_filename, &old_data));
497 if (old_data == new_data) {
498 // No change in data.
499 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
500 current_best_size = 0;
501 data.clear();
502 } else {
503 // Try bsdiff of old to new data
504 vector<char> bsdiff_delta;
505 TEST_AND_RETURN_FALSE(
506 BsdiffFiles(old_filename, new_filename, &bsdiff_delta));
507 CHECK_GT(bsdiff_delta.size(), 0);
508 if (bsdiff_delta.size() < current_best_size) {
509 operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF);
510 current_best_size = bsdiff_delta.size();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700511
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700512 data = bsdiff_delta;
513 }
514 }
515 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700516
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700517 // Set parameters of the operations
518 CHECK_EQ(data.size(), current_best_size);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700519
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700520 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE ||
521 operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Darin Petkov68c10d12010-10-14 09:24:37 -0700522 if (gather_extents) {
523 TEST_AND_RETURN_FALSE(
524 GatherExtents(old_filename, operation.mutable_src_extents()));
525 } else {
526 Extent* src_extent = operation.add_src_extents();
527 src_extent->set_start_block(0);
528 src_extent->set_num_blocks(
529 (old_stbuf.st_size + kBlockSize - 1) / kBlockSize);
530 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700531 operation.set_src_length(old_stbuf.st_size);
532 }
533
Darin Petkov68c10d12010-10-14 09:24:37 -0700534 if (gather_extents) {
535 TEST_AND_RETURN_FALSE(
536 GatherExtents(new_filename, operation.mutable_dst_extents()));
537 } else {
538 Extent* dst_extent = operation.add_dst_extents();
539 dst_extent->set_start_block(0);
540 dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize);
541 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700542 operation.set_dst_length(new_data.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700543
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700544 out_data->swap(data);
545 *out_op = operation;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700546
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700547 return true;
548}
549
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700550bool DeltaDiffGenerator::InitializePartitionInfo(bool is_kernel,
551 const string& partition,
552 PartitionInfo* info) {
Darin Petkov7ea32332010-10-13 10:46:11 -0700553 int64_t size = 0;
554 if (is_kernel) {
555 size = utils::FileSize(partition);
556 } else {
557 int block_count = 0, block_size = 0;
558 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition,
559 &block_count,
560 &block_size));
561 size = static_cast<int64_t>(block_count) * block_size;
562 }
563 TEST_AND_RETURN_FALSE(size > 0);
Darin Petkov36a58222010-10-07 22:00:09 -0700564 info->set_size(size);
565 OmahaHashCalculator hasher;
Darin Petkov7ea32332010-10-13 10:46:11 -0700566 TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size);
Darin Petkov36a58222010-10-07 22:00:09 -0700567 TEST_AND_RETURN_FALSE(hasher.Finalize());
568 const vector<char>& hash = hasher.raw_hash();
569 info->set_hash(hash.data(), hash.size());
Darin Petkovd43d6902010-10-14 11:17:50 -0700570 LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash();
Darin Petkov36a58222010-10-07 22:00:09 -0700571 return true;
572}
573
574bool InitializePartitionInfos(const string& old_kernel,
575 const string& new_kernel,
576 const string& old_rootfs,
577 const string& new_rootfs,
578 DeltaArchiveManifest* manifest) {
Darin Petkovd43d6902010-10-14 11:17:50 -0700579 if (!old_kernel.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700580 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
581 true,
582 old_kernel,
583 manifest->mutable_old_kernel_info()));
Darin Petkovd43d6902010-10-14 11:17:50 -0700584 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700585 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
586 true,
587 new_kernel,
588 manifest->mutable_new_kernel_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700589 if (!old_rootfs.empty()) {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700590 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
591 false,
592 old_rootfs,
593 manifest->mutable_old_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700594 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700595 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo(
596 false,
597 new_rootfs,
598 manifest->mutable_new_rootfs_info()));
Darin Petkov36a58222010-10-07 22:00:09 -0700599 return true;
600}
601
Andrew de los Reyesef017552010-10-06 17:57:52 -0700602namespace {
603
604// Takes a collection (vector or RepeatedPtrField) of Extent and
605// returns a vector of the blocks referenced, in order.
606template<typename T>
607vector<uint64_t> ExpandExtents(const T& extents) {
608 vector<uint64_t> ret;
609 for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) {
610 const Extent extent = graph_utils::GetElement(extents, i);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700611 if (extent.start_block() == kSparseHole) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700612 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700613 } else {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700614 for (uint64_t block = extent.start_block();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700615 block < (extent.start_block() + extent.num_blocks()); block++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700616 ret.push_back(block);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700617 }
618 }
619 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700620 return ret;
621}
622
623// Takes a vector of blocks and returns an equivalent vector of Extent
624// objects.
625vector<Extent> CompressExtents(const vector<uint64_t>& blocks) {
626 vector<Extent> new_extents;
627 for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end();
628 it != e; ++it) {
629 graph_utils::AppendBlockToExtents(&new_extents, *it);
630 }
631 return new_extents;
632}
633
634} // namespace {}
635
636void DeltaDiffGenerator::SubstituteBlocks(
637 Vertex* vertex,
638 const vector<Extent>& remove_extents,
639 const vector<Extent>& replace_extents) {
640 // First, expand out the blocks that op reads from
641 vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700642 {
643 // Expand remove_extents and replace_extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700644 vector<uint64_t> remove_extents_expanded =
645 ExpandExtents(remove_extents);
646 vector<uint64_t> replace_extents_expanded =
647 ExpandExtents(replace_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700648 CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700649 map<uint64_t, uint64_t> conversion;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700650 for (vector<uint64_t>::size_type i = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700651 i < replace_extents_expanded.size(); i++) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700652 conversion[remove_extents_expanded[i]] = replace_extents_expanded[i];
653 }
654 utils::ApplyMap(&read_blocks, conversion);
655 for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(),
656 e = vertex->out_edges.end(); it != e; ++it) {
657 vector<uint64_t> write_before_deps_expanded =
658 ExpandExtents(it->second.write_extents);
659 utils::ApplyMap(&write_before_deps_expanded, conversion);
660 it->second.write_extents = CompressExtents(write_before_deps_expanded);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700661 }
662 }
663 // Convert read_blocks back to extents
Andrew de los Reyesef017552010-10-06 17:57:52 -0700664 vertex->op.clear_src_extents();
665 vector<Extent> new_extents = CompressExtents(read_blocks);
666 DeltaDiffGenerator::StoreExtents(new_extents,
667 vertex->op.mutable_src_extents());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700668}
669
670bool DeltaDiffGenerator::CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700671 const set<Edge>& edges,
672 vector<CutEdgeVertexes>* out_cuts) {
673 DummyExtentAllocator scratch_allocator;
674 vector<CutEdgeVertexes> cuts;
675 cuts.reserve(edges.size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700676
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700677 uint64_t scratch_blocks_used = 0;
678 for (set<Edge>::const_iterator it = edges.begin();
679 it != edges.end(); ++it) {
Andrew de los Reyesef017552010-10-06 17:57:52 -0700680 cuts.resize(cuts.size() + 1);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700681 vector<Extent> old_extents =
682 (*graph)[it->first].out_edges[it->second].extents;
683 // Choose some scratch space
684 scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700685 cuts.back().tmp_extents =
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700686 scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it));
687 // create vertex to copy original->scratch
Andrew de los Reyesef017552010-10-06 17:57:52 -0700688 cuts.back().new_vertex = graph->size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700689 graph->resize(graph->size() + 1);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700690 cuts.back().old_src = it->first;
691 cuts.back().old_dst = it->second;
Darin Petkov36a58222010-10-07 22:00:09 -0700692
Andrew de los Reyesef017552010-10-06 17:57:52 -0700693 EdgeProperties& cut_edge_properties =
694 (*graph)[it->first].out_edges.find(it->second)->second;
695
696 // This should never happen, as we should only be cutting edges between
697 // real file nodes, and write-before relationships are created from
698 // a real file node to a temp copy node:
699 CHECK(cut_edge_properties.write_extents.empty())
700 << "Can't cut edge that has write-before relationship.";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700701
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700702 // make node depend on the copy operation
703 (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700704 cut_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700705
706 // Set src/dst extents and other proto variables for copy operation
707 graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE);
708 DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700709 cut_edge_properties.extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700710 graph->back().op.mutable_src_extents());
Andrew de los Reyesef017552010-10-06 17:57:52 -0700711 DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700712 graph->back().op.mutable_dst_extents());
713 graph->back().op.set_src_length(
714 graph_utils::EdgeWeight(*graph, *it) * kBlockSize);
715 graph->back().op.set_dst_length(graph->back().op.src_length());
716
717 // make the dest node read from the scratch space
718 DeltaDiffGenerator::SubstituteBlocks(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700719 &((*graph)[it->second]),
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700720 (*graph)[it->first].out_edges[it->second].extents,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700721 cuts.back().tmp_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700722
723 // delete the old edge
724 CHECK_EQ(1, (*graph)[it->first].out_edges.erase(it->second));
Chris Masone790e62e2010-08-12 10:41:18 -0700725
Andrew de los Reyesd12784c2010-07-26 13:55:14 -0700726 // Add an edge from dst to copy operation
Andrew de los Reyesef017552010-10-06 17:57:52 -0700727 EdgeProperties write_before_edge_properties;
728 write_before_edge_properties.write_extents = cuts.back().tmp_extents;
729 (*graph)[it->second].out_edges.insert(
730 make_pair(graph->size() - 1, write_before_edge_properties));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700731 }
Andrew de los Reyesef017552010-10-06 17:57:52 -0700732 out_cuts->swap(cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700733 return true;
734}
735
736// Stores all Extents in 'extents' into 'out'.
737void DeltaDiffGenerator::StoreExtents(
Andrew de los Reyesef017552010-10-06 17:57:52 -0700738 const vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700739 google::protobuf::RepeatedPtrField<Extent>* out) {
740 for (vector<Extent>::const_iterator it = extents.begin();
741 it != extents.end(); ++it) {
742 Extent* new_extent = out->Add();
743 *new_extent = *it;
744 }
745}
746
747// Creates all the edges for the graph. Writers of a block point to
748// readers of the same block. This is because for an edge A->B, B
749// must complete before A executes.
750void DeltaDiffGenerator::CreateEdges(Graph* graph,
751 const vector<Block>& blocks) {
752 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
753 // Blocks with both a reader and writer get an edge
754 if (blocks[i].reader == Vertex::kInvalidIndex ||
755 blocks[i].writer == Vertex::kInvalidIndex)
756 continue;
757 // Don't have a node depend on itself
758 if (blocks[i].reader == blocks[i].writer)
759 continue;
760 // See if there's already an edge we can add onto
761 Vertex::EdgeMap::iterator edge_it =
762 (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
763 if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) {
764 // No existing edge. Create one
765 (*graph)[blocks[i].writer].out_edges.insert(
766 make_pair(blocks[i].reader, EdgeProperties()));
767 edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader);
Chris Masone790e62e2010-08-12 10:41:18 -0700768 CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700769 }
770 graph_utils::AppendBlockToExtents(&edge_it->second.extents, i);
771 }
772}
773
Andrew de los Reyesef017552010-10-06 17:57:52 -0700774namespace {
775
776class SortCutsByTopoOrderLess {
777 public:
778 SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table)
779 : table_(table) {}
780 bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) {
781 return table_[a.old_dst] < table_[b.old_dst];
782 }
783 private:
784 vector<vector<Vertex::Index>::size_type>& table_;
785};
786
787} // namespace {}
788
789void DeltaDiffGenerator::GenerateReverseTopoOrderMap(
790 vector<Vertex::Index>& op_indexes,
791 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) {
792 vector<vector<Vertex::Index>::size_type> table(op_indexes.size());
793 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size();
794 i != e; ++i) {
795 Vertex::Index node = op_indexes[i];
796 if (table.size() < (node + 1)) {
797 table.resize(node + 1);
798 }
799 table[node] = i;
800 }
801 reverse_op_indexes->swap(table);
802}
803
804void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes,
805 vector<CutEdgeVertexes>* cuts) {
806 // first, make a reverse lookup table.
807 vector<vector<Vertex::Index>::size_type> table;
808 GenerateReverseTopoOrderMap(op_indexes, &table);
809 SortCutsByTopoOrderLess less(table);
810 sort(cuts->begin(), cuts->end(), less);
811}
812
813void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph,
814 vector<Vertex::Index>* op_indexes) {
815 vector<Vertex::Index> ret;
816 vector<Vertex::Index> full_ops;
817 ret.reserve(op_indexes->size());
818 for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e;
819 ++i) {
820 DeltaArchiveManifest_InstallOperation_Type type =
821 (*graph)[(*op_indexes)[i]].op.type();
822 if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
823 type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
824 full_ops.push_back((*op_indexes)[i]);
825 } else {
826 ret.push_back((*op_indexes)[i]);
827 }
828 }
829 LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of "
830 << (full_ops.size() + ret.size()) << " total ops.";
831 ret.insert(ret.end(), full_ops.begin(), full_ops.end());
832 op_indexes->swap(ret);
833}
834
835namespace {
836
837template<typename T>
838bool TempBlocksExistInExtents(const T& extents) {
839 for (int i = 0, e = extents.size(); i < e; ++i) {
840 Extent extent = graph_utils::GetElement(extents, i);
841 uint64_t start = extent.start_block();
842 uint64_t num = extent.num_blocks();
843 if (start == kSparseHole)
844 continue;
845 if (start >= kTempBlockStart ||
846 (start + num) >= kTempBlockStart) {
847 LOG(ERROR) << "temp block!";
848 LOG(ERROR) << "start: " << start << ", num: " << num;
849 LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart;
850 LOG(ERROR) << "returning true";
851 return true;
852 }
853 // check for wrap-around, which would be a bug:
854 CHECK(start <= (start + num));
855 }
856 return false;
857}
858
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700859// Convertes the cuts, which must all have the same |old_dst| member,
860// to full. It does this by converting the |old_dst| to REPLACE or
861// REPLACE_BZ, dropping all incoming edges to |old_dst|, and marking
862// all temp nodes invalid.
863bool ConvertCutsToFull(
864 Graph* graph,
865 const string& new_root,
866 int data_fd,
867 off_t* data_file_size,
868 vector<Vertex::Index>* op_indexes,
869 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
870 const vector<CutEdgeVertexes>& cuts) {
871 CHECK(!cuts.empty());
872 set<Vertex::Index> deleted_nodes;
873 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
874 e = cuts.end(); it != e; ++it) {
875 TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp(
876 graph,
877 *it,
878 new_root,
879 data_fd,
880 data_file_size));
881 deleted_nodes.insert(it->new_vertex);
882 }
883 deleted_nodes.insert(cuts[0].old_dst);
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700884
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700885 vector<Vertex::Index> new_op_indexes;
886 new_op_indexes.reserve(op_indexes->size());
887 for (vector<Vertex::Index>::iterator it = op_indexes->begin(),
888 e = op_indexes->end(); it != e; ++it) {
889 if (utils::SetContainsKey(deleted_nodes, *it))
890 continue;
891 new_op_indexes.push_back(*it);
892 }
893 new_op_indexes.push_back(cuts[0].old_dst);
894 op_indexes->swap(new_op_indexes);
895 DeltaDiffGenerator::GenerateReverseTopoOrderMap(*op_indexes,
896 reverse_op_indexes);
897 return true;
898}
899
900// Tries to assign temp blocks for a collection of cuts, all of which share
901// the same old_dst member. If temp blocks can't be found, old_dst will be
902// converted to a REPLACE or REPLACE_BZ operation. Returns true on success,
903// which can happen even if blocks are converted to full. Returns false
904// on exceptional error cases.
905bool AssignBlockForAdjoiningCuts(
906 Graph* graph,
907 const string& new_root,
908 int data_fd,
909 off_t* data_file_size,
910 vector<Vertex::Index>* op_indexes,
911 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
912 const vector<CutEdgeVertexes>& cuts) {
913 CHECK(!cuts.empty());
914 const Vertex::Index old_dst = cuts[0].old_dst;
915 // Calculate # of blocks needed
916 uint64_t blocks_needed = 0;
917 map<const CutEdgeVertexes*, uint64_t> cuts_blocks_needed;
918 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
919 e = cuts.end(); it != e; ++it) {
920 uint64_t cut_blocks_needed = 0;
921 for (vector<Extent>::const_iterator jt = it->tmp_extents.begin(),
922 je = it->tmp_extents.end(); jt != je; ++jt) {
923 cut_blocks_needed += jt->num_blocks();
924 }
925 blocks_needed += cut_blocks_needed;
926 cuts_blocks_needed[&*it] = cut_blocks_needed;
927 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700928
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700929 // Find enough blocks
930 ExtentRanges scratch_ranges;
931 // Each block that's supplying temp blocks and the corresponding blocks:
932 typedef vector<pair<Vertex::Index, ExtentRanges> > SupplierVector;
933 SupplierVector block_suppliers;
934 uint64_t scratch_blocks_found = 0;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700935 for (vector<Vertex::Index>::size_type i = (*reverse_op_indexes)[old_dst] + 1,
936 e = op_indexes->size(); i < e; ++i) {
937 Vertex::Index test_node = (*op_indexes)[i];
938 if (!(*graph)[test_node].valid)
939 continue;
940 // See if this node has sufficient blocks
941 ExtentRanges ranges;
942 ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents());
943 ranges.SubtractExtent(ExtentForRange(
944 kTempBlockStart, kSparseHole - kTempBlockStart));
945 ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents());
946 // For now, for simplicity, subtract out all blocks in read-before
947 // dependencies.
948 for (Vertex::EdgeMap::const_iterator edge_i =
949 (*graph)[test_node].out_edges.begin(),
950 edge_e = (*graph)[test_node].out_edges.end();
951 edge_i != edge_e; ++edge_i) {
952 ranges.SubtractExtents(edge_i->second.extents);
953 }
954 if (ranges.blocks() == 0)
955 continue;
956
957 if (ranges.blocks() + scratch_blocks_found > blocks_needed) {
958 // trim down ranges
959 vector<Extent> new_ranges = ranges.GetExtentsForBlockCount(
Andrew de los Reyes927179d2010-12-02 11:26:48 -0800960 blocks_needed - scratch_blocks_found);
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700961 ranges = ExtentRanges();
962 ranges.AddExtents(new_ranges);
963 }
964 scratch_ranges.AddRanges(ranges);
965 block_suppliers.push_back(make_pair(test_node, ranges));
966 scratch_blocks_found += ranges.blocks();
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700967 if (scratch_ranges.blocks() >= blocks_needed)
968 break;
969 }
970 if (scratch_ranges.blocks() < blocks_needed) {
971 LOG(INFO) << "Unable to find sufficient scratch";
972 TEST_AND_RETURN_FALSE(ConvertCutsToFull(graph,
973 new_root,
974 data_fd,
975 data_file_size,
976 op_indexes,
977 reverse_op_indexes,
978 cuts));
979 return true;
980 }
981 // Use the scratch we found
982 TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found);
983
984 // Make all the suppliers depend on this node
985 for (SupplierVector::iterator it = block_suppliers.begin(),
986 e = block_suppliers.end(); it != e; ++it) {
987 graph_utils::AddReadBeforeDepExtents(
988 &(*graph)[it->first],
989 old_dst,
990 it->second.GetExtentsForBlockCount(it->second.blocks()));
991 }
Darin Petkovbc58a7b2010-11-03 11:52:53 -0700992
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -0700993 // Replace temp blocks in each cut
994 for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(),
995 e = cuts.end(); it != e; ++it) {
996 vector<Extent> real_extents =
997 scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]);
998 scratch_ranges.SubtractExtents(real_extents);
999
1000 // Fix the old dest node w/ the real blocks
1001 DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst],
1002 it->tmp_extents,
1003 real_extents);
1004
1005 // Fix the new node w/ the real blocks. Since the new node is just a
1006 // copy operation, we can replace all the dest extents w/ the real
1007 // blocks.
1008 DeltaArchiveManifest_InstallOperation *op =
1009 &(*graph)[it->new_vertex].op;
1010 op->clear_dst_extents();
1011 DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents());
1012 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001013 return true;
1014}
1015
Andrew de los Reyesef017552010-10-06 17:57:52 -07001016} // namespace {}
1017
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001018// Returns true if |op| is a no-op operation that doesn't do any useful work
1019// (e.g., a move operation that copies blocks onto themselves).
1020bool DeltaDiffGenerator::IsNoopOperation(
1021 const DeltaArchiveManifest_InstallOperation& op) {
1022 return (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE &&
1023 ExpandExtents(op.src_extents()) == ExpandExtents(op.dst_extents()));
1024}
1025
Andrew de los Reyesef017552010-10-06 17:57:52 -07001026bool DeltaDiffGenerator::AssignTempBlocks(
1027 Graph* graph,
1028 const string& new_root,
1029 int data_fd,
1030 off_t* data_file_size,
1031 vector<Vertex::Index>* op_indexes,
1032 vector<vector<Vertex::Index>::size_type>* reverse_op_indexes,
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001033 const vector<CutEdgeVertexes>& cuts) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001034 CHECK(!cuts.empty());
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001035
1036 // group of cuts w/ the same old_dst:
1037 vector<CutEdgeVertexes> cuts_group;
1038
Andrew de los Reyesef017552010-10-06 17:57:52 -07001039 for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0;
1040 true ; --i) {
1041 LOG(INFO) << "Fixing temp blocks in cut " << i
1042 << ": old dst: " << cuts[i].old_dst << " new vertex: "
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001043 << cuts[i].new_vertex << " path: "
1044 << (*graph)[cuts[i].old_dst].file_name;
1045
1046 if (cuts_group.empty() || (cuts_group[0].old_dst == cuts[i].old_dst)) {
1047 cuts_group.push_back(cuts[i]);
1048 } else {
1049 CHECK(!cuts_group.empty());
1050 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1051 new_root,
1052 data_fd,
1053 data_file_size,
1054 op_indexes,
1055 reverse_op_indexes,
1056 cuts_group));
1057 cuts_group.clear();
1058 cuts_group.push_back(cuts[i]);
Andrew de los Reyesef017552010-10-06 17:57:52 -07001059 }
Darin Petkov36a58222010-10-07 22:00:09 -07001060
Andrew de los Reyesef017552010-10-06 17:57:52 -07001061 if (i == e) {
1062 // break out of for() loop
1063 break;
1064 }
1065 }
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001066 CHECK(!cuts_group.empty());
1067 TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph,
1068 new_root,
1069 data_fd,
1070 data_file_size,
1071 op_indexes,
1072 reverse_op_indexes,
1073 cuts_group));
Andrew de los Reyesef017552010-10-06 17:57:52 -07001074 return true;
1075}
1076
1077bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) {
1078 size_t idx = 0;
1079 for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e;
1080 ++it, ++idx) {
1081 if (!it->valid)
1082 continue;
1083 const DeltaArchiveManifest_InstallOperation& op = it->op;
1084 if (TempBlocksExistInExtents(op.dst_extents()) ||
1085 TempBlocksExistInExtents(op.src_extents())) {
1086 LOG(INFO) << "bad extents in node " << idx;
1087 LOG(INFO) << "so yeah";
1088 return false;
1089 }
1090
1091 // Check out-edges:
1092 for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(),
1093 je = it->out_edges.end(); jt != je; ++jt) {
1094 if (TempBlocksExistInExtents(jt->second.extents) ||
1095 TempBlocksExistInExtents(jt->second.write_extents)) {
1096 LOG(INFO) << "bad out edge in node " << idx;
1097 LOG(INFO) << "so yeah";
1098 return false;
1099 }
1100 }
1101 }
1102 return true;
1103}
1104
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001105bool DeltaDiffGenerator::ReorderDataBlobs(
1106 DeltaArchiveManifest* manifest,
1107 const std::string& data_blobs_path,
1108 const std::string& new_data_blobs_path) {
1109 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
1110 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
1111 ScopedFdCloser in_fd_closer(&in_fd);
Chris Masone790e62e2010-08-12 10:41:18 -07001112
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001113 DirectFileWriter writer;
1114 TEST_AND_RETURN_FALSE(
1115 writer.Open(new_data_blobs_path.c_str(),
1116 O_WRONLY | O_TRUNC | O_CREAT,
1117 0644) == 0);
1118 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001119 uint64_t out_file_size = 0;
Chris Masone790e62e2010-08-12 10:41:18 -07001120
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001121 for (int i = 0; i < (manifest->install_operations_size() +
1122 manifest->kernel_install_operations_size()); i++) {
1123 DeltaArchiveManifest_InstallOperation* op = NULL;
1124 if (i < manifest->install_operations_size()) {
1125 op = manifest->mutable_install_operations(i);
1126 } else {
1127 op = manifest->mutable_kernel_install_operations(
1128 i - manifest->install_operations_size());
1129 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001130 if (!op->has_data_offset())
1131 continue;
1132 CHECK(op->has_data_length());
1133 vector<char> buf(op->data_length());
1134 ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset());
1135 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
1136
1137 op->set_data_offset(out_file_size);
1138 TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()) ==
1139 static_cast<ssize_t>(buf.size()));
1140 out_file_size += buf.size();
1141 }
1142 return true;
1143}
1144
Andrew de los Reyesef017552010-10-06 17:57:52 -07001145bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph,
1146 const CutEdgeVertexes& cut,
1147 const string& new_root,
1148 int data_fd,
1149 off_t* data_file_size) {
1150 // Drop all incoming edges, keep all outgoing edges
Darin Petkov36a58222010-10-07 22:00:09 -07001151
Andrew de los Reyesef017552010-10-06 17:57:52 -07001152 // Keep all outgoing edges
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001153 if ((*graph)[cut.old_dst].op.type() !=
1154 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ &&
1155 (*graph)[cut.old_dst].op.type() !=
1156 DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
1157 Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges;
1158 graph_utils::DropWriteBeforeDeps(&out_edges);
Darin Petkov36a58222010-10-07 22:00:09 -07001159
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001160 TEST_AND_RETURN_FALSE(DeltaReadFile(graph,
1161 cut.old_dst,
1162 NULL,
1163 "/-!@:&*nonexistent_path",
1164 new_root,
1165 (*graph)[cut.old_dst].file_name,
1166 data_fd,
1167 data_file_size));
Darin Petkov36a58222010-10-07 22:00:09 -07001168
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001169 (*graph)[cut.old_dst].out_edges = out_edges;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001170
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001171 // Right now we don't have doubly-linked edges, so we have to scan
1172 // the whole graph.
1173 graph_utils::DropIncomingEdgesTo(graph, cut.old_dst);
1174 }
Andrew de los Reyesef017552010-10-06 17:57:52 -07001175
1176 // Delete temp node
1177 (*graph)[cut.old_src].out_edges.erase(cut.new_vertex);
1178 CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) ==
1179 (*graph)[cut.old_dst].out_edges.end());
1180 (*graph)[cut.new_vertex].valid = false;
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001181 LOG(INFO) << "marked node invalid: " << cut.new_vertex;
Andrew de los Reyesef017552010-10-06 17:57:52 -07001182 return true;
1183}
1184
1185bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph,
1186 const string& new_root,
1187 int fd,
1188 off_t* data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001189 vector<Vertex::Index>* final_order,
1190 Vertex::Index scratch_vertex) {
Andrew de los Reyesef017552010-10-06 17:57:52 -07001191 CycleBreaker cycle_breaker;
1192 LOG(INFO) << "Finding cycles...";
1193 set<Edge> cut_edges;
1194 cycle_breaker.BreakCycles(*graph, &cut_edges);
1195 LOG(INFO) << "done finding cycles";
1196 CheckGraph(*graph);
1197
1198 // Calculate number of scratch blocks needed
1199
1200 LOG(INFO) << "Cutting cycles...";
1201 vector<CutEdgeVertexes> cuts;
1202 TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts));
1203 LOG(INFO) << "done cutting cycles";
1204 LOG(INFO) << "There are " << cuts.size() << " cuts.";
1205 CheckGraph(*graph);
1206
1207 LOG(INFO) << "Creating initial topological order...";
1208 TopologicalSort(*graph, final_order);
1209 LOG(INFO) << "done with initial topo order";
1210 CheckGraph(*graph);
1211
1212 LOG(INFO) << "Moving full ops to the back";
1213 MoveFullOpsToBack(graph, final_order);
1214 LOG(INFO) << "done moving full ops to back";
1215
1216 vector<vector<Vertex::Index>::size_type> inverse_final_order;
1217 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1218
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001219 SortCutsByTopoOrder(*final_order, &cuts);
1220
Andrew de los Reyesef017552010-10-06 17:57:52 -07001221 if (!cuts.empty())
1222 TEST_AND_RETURN_FALSE(AssignTempBlocks(graph,
1223 new_root,
1224 fd,
1225 data_file_size,
1226 final_order,
1227 &inverse_final_order,
1228 cuts));
1229 LOG(INFO) << "Making sure all temp blocks have been allocated";
Andrew de los Reyes4ba850d2010-10-25 12:12:40 -07001230
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001231 // Remove the scratch node, if any
1232 if (scratch_vertex != Vertex::kInvalidIndex) {
1233 final_order->erase(final_order->begin() +
1234 inverse_final_order[scratch_vertex]);
1235 (*graph)[scratch_vertex].valid = false;
1236 GenerateReverseTopoOrderMap(*final_order, &inverse_final_order);
1237 }
1238
Andrew de los Reyesef017552010-10-06 17:57:52 -07001239 graph_utils::DumpGraph(*graph);
1240 CHECK(NoTempBlocksRemain(*graph));
1241 LOG(INFO) << "done making sure all temp blocks are allocated";
1242 return true;
1243}
1244
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001245void DeltaDiffGenerator::CreateScratchNode(uint64_t start_block,
1246 uint64_t num_blocks,
1247 Vertex* vertex) {
1248 vertex->file_name = "<scratch>";
1249 vertex->op.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
1250 vertex->op.set_data_offset(0);
1251 vertex->op.set_data_length(0);
1252 Extent* extent = vertex->op.add_dst_extents();
1253 extent->set_start_block(start_block);
1254 extent->set_num_blocks(num_blocks);
1255}
1256
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001257bool DeltaDiffGenerator::GenerateDeltaUpdateFile(
1258 const string& old_root,
1259 const string& old_image,
1260 const string& new_root,
1261 const string& new_image,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001262 const string& old_kernel_part,
1263 const string& new_kernel_part,
1264 const string& output_path,
1265 const string& private_key_path) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001266 int old_image_block_count = 0, old_image_block_size = 0;
1267 int new_image_block_count = 0, new_image_block_size = 0;
1268 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image,
1269 &new_image_block_count,
1270 &new_image_block_size));
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001271 if (!old_image.empty()) {
Darin Petkov7ea32332010-10-13 10:46:11 -07001272 TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image,
1273 &old_image_block_count,
1274 &old_image_block_size));
1275 TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size);
1276 LOG_IF(WARNING, old_image_block_count != new_image_block_count)
1277 << "Old and new images have different block counts.";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001278 }
Andrew de los Reyes27f7d372010-10-07 11:26:07 -07001279 // Sanity check kernel partition arg
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001280 TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0);
1281
Darin Petkov7ea32332010-10-13 10:46:11 -07001282 vector<Block> blocks(max(old_image_block_count, new_image_block_count));
1283 LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex;
1284 LOG(INFO) << "Block count: " << blocks.size();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001285 for (vector<Block>::size_type i = 0; i < blocks.size(); i++) {
1286 CHECK(blocks[i].reader == Vertex::kInvalidIndex);
1287 CHECK(blocks[i].writer == Vertex::kInvalidIndex);
1288 }
1289 Graph graph;
1290 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001291
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001292 const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX");
1293 string temp_file_path;
1294 off_t data_file_size = 0;
1295
1296 LOG(INFO) << "Reading files...";
1297
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001298 vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
1299
Andrew de los Reyesef017552010-10-06 17:57:52 -07001300 vector<Vertex::Index> final_order;
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001301 Vertex::Index scratch_vertex = Vertex::kInvalidIndex;
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001302 {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001303 int fd;
1304 TEST_AND_RETURN_FALSE(
1305 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd));
1306 TEST_AND_RETURN_FALSE(fd >= 0);
1307 ScopedFdCloser fd_closer(&fd);
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001308 if (!old_image.empty()) {
1309 // Delta update
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001310
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001311 TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph,
1312 &blocks,
1313 old_root,
1314 new_root,
1315 fd,
1316 &data_file_size));
1317 LOG(INFO) << "done reading normal files";
1318 CheckGraph(graph);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001319
Thieu Le5c7d9752010-12-15 16:09:28 -08001320 LOG(INFO) << "Starting metadata processing";
1321 TEST_AND_RETURN_FALSE(Metadata::DeltaReadMetadata(&graph,
1322 &blocks,
1323 old_image,
1324 new_image,
1325 fd,
1326 &data_file_size));
1327 LOG(INFO) << "Done metadata processing";
1328 CheckGraph(graph);
1329
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001330 graph.resize(graph.size() + 1);
1331 TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks,
1332 fd,
1333 &data_file_size,
1334 new_image,
1335 &graph.back()));
1336
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001337 // Final scratch block (if there's space)
1338 if (blocks.size() < (kRootFSPartitionSize / kBlockSize)) {
1339 scratch_vertex = graph.size();
1340 graph.resize(graph.size() + 1);
1341 CreateScratchNode(blocks.size(),
1342 (kRootFSPartitionSize / kBlockSize) - blocks.size(),
1343 &graph.back());
1344 }
1345
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001346 // Read kernel partition
1347 TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part,
1348 new_kernel_part,
1349 &kernel_ops,
1350 fd,
1351 &data_file_size));
1352
1353 LOG(INFO) << "done reading kernel";
1354 CheckGraph(graph);
1355
1356 LOG(INFO) << "Creating edges...";
1357 CreateEdges(&graph, blocks);
1358 LOG(INFO) << "Done creating edges";
1359 CheckGraph(graph);
1360
1361 TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph,
1362 new_root,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001363 fd,
1364 &data_file_size,
Andrew de los Reyes927179d2010-12-02 11:26:48 -08001365 &final_order,
1366 scratch_vertex));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001367 } else {
1368 // Full update
Darin Petkov7ea32332010-10-13 10:46:11 -07001369 off_t new_image_size =
1370 static_cast<off_t>(new_image_block_count) * new_image_block_size;
Darin Petkov7a22d792010-11-08 14:10:00 -08001371 TEST_AND_RETURN_FALSE(FullUpdateGenerator::Run(&graph,
1372 new_kernel_part,
1373 new_image,
1374 new_image_size,
1375 fd,
1376 &data_file_size,
1377 kFullUpdateChunkSize,
1378 kBlockSize,
1379 &kernel_ops,
1380 &final_order));
Andrew de los Reyesf88144f2010-10-11 10:32:59 -07001381 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001382 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001383
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001384 // Convert to protobuf Manifest object
1385 DeltaArchiveManifest manifest;
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001386 OperationNameMap op_name_map;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001387 CheckGraph(graph);
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001388 InstallOperationsToManifest(graph,
1389 final_order,
1390 kernel_ops,
1391 &manifest,
1392 &op_name_map);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001393 CheckGraph(graph);
1394 manifest.set_block_size(kBlockSize);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001395
1396 // Reorder the data blobs with the newly ordered manifest
1397 string ordered_blobs_path;
1398 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
1399 "/tmp/CrAU_temp_data.ordered.XXXXXX",
1400 &ordered_blobs_path,
1401 false));
1402 TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest,
1403 temp_file_path,
1404 ordered_blobs_path));
1405
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001406 // Check that install op blobs are in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001407 uint64_t next_blob_offset = 0;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001408 {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001409 for (int i = 0; i < (manifest.install_operations_size() +
1410 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001411 DeltaArchiveManifest_InstallOperation* op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001412 i < manifest.install_operations_size() ?
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001413 manifest.mutable_install_operations(i) :
1414 manifest.mutable_kernel_install_operations(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -07001415 i - manifest.install_operations_size());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001416 if (op->has_data_offset()) {
1417 if (op->data_offset() != next_blob_offset) {
1418 LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != "
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001419 << next_blob_offset;
1420 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001421 next_blob_offset += op->data_length();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001422 }
1423 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001424 }
1425
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001426 // Signatures appear at the end of the blobs. Note the offset in the
1427 // manifest
1428 if (!private_key_path.empty()) {
1429 LOG(INFO) << "Making room for signature in file";
1430 manifest.set_signatures_offset(next_blob_offset);
1431 LOG(INFO) << "set? " << manifest.has_signatures_offset();
1432 // Add a dummy op at the end to appease older clients
1433 DeltaArchiveManifest_InstallOperation* dummy_op =
1434 manifest.add_kernel_install_operations();
1435 dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
1436 dummy_op->set_data_offset(next_blob_offset);
1437 manifest.set_signatures_offset(next_blob_offset);
1438 uint64_t signature_blob_length = 0;
1439 TEST_AND_RETURN_FALSE(
1440 PayloadSigner::SignatureBlobLength(private_key_path,
1441 &signature_blob_length));
1442 dummy_op->set_data_length(signature_blob_length);
1443 manifest.set_signatures_size(signature_blob_length);
1444 Extent* dummy_extent = dummy_op->add_dst_extents();
1445 // Tell the dummy op to write this data to a big sparse hole
1446 dummy_extent->set_start_block(kSparseHole);
1447 dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
1448 kBlockSize);
1449 }
1450
Darin Petkov36a58222010-10-07 22:00:09 -07001451 TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
1452 new_kernel_part,
1453 old_image,
1454 new_image,
1455 &manifest));
1456
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001457 // Serialize protobuf
1458 string serialized_manifest;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001459
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001460 CheckGraph(graph);
1461 TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest));
1462 CheckGraph(graph);
1463
1464 LOG(INFO) << "Writing final delta file header...";
1465 DirectFileWriter writer;
1466 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(),
1467 O_WRONLY | O_CREAT | O_TRUNC,
1468 0644) == 0);
1469 ScopedFileWriterCloser writer_closer(&writer);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001470
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001471 // Write header
1472 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)) ==
Andrew de los Reyes08c4e272010-04-15 14:02:17 -07001473 static_cast<ssize_t>(strlen(kDeltaMagic)));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001474
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001475 // Write version number
1476 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001477
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001478 // Write protobuf length
1479 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
1480 serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001481
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001482 // Write protobuf
1483 LOG(INFO) << "Writing final delta file protobuf... "
1484 << serialized_manifest.size();
1485 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
1486 serialized_manifest.size()) ==
1487 static_cast<ssize_t>(serialized_manifest.size()));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001488
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001489 // Append the data blobs
1490 LOG(INFO) << "Writing final delta file data blobs...";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001491 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001492 ScopedFdCloser blobs_fd_closer(&blobs_fd);
1493 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
1494 for (;;) {
1495 char buf[kBlockSize];
1496 ssize_t rc = read(blobs_fd, buf, sizeof(buf));
1497 if (0 == rc) {
1498 // EOF
1499 break;
1500 }
1501 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
1502 TEST_AND_RETURN_FALSE(writer.Write(buf, rc) == rc);
1503 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07001504
1505 // Write signature blob.
1506 if (!private_key_path.empty()) {
1507 LOG(INFO) << "Signing the update...";
1508 vector<char> signature_blob;
1509 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(output_path,
1510 private_key_path,
1511 &signature_blob));
1512 TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0],
1513 signature_blob.size()) ==
1514 static_cast<ssize_t>(signature_blob.size()));
1515 }
1516
Darin Petkov95cf01f2010-10-12 14:59:13 -07001517 int64_t manifest_metadata_size =
1518 strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
Darin Petkov9fa7ec52010-10-18 11:45:23 -07001519 ReportPayloadUsage(manifest, manifest_metadata_size, op_name_map);
Darin Petkov880335c2010-10-01 15:52:53 -07001520
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001521 LOG(INFO) << "All done. Successfully created delta file.";
1522 return true;
1523}
1524
Thieu Le5c7d9752010-12-15 16:09:28 -08001525// Runs the bsdiff tool on two files and returns the resulting delta in
1526// 'out'. Returns true on success.
1527bool DeltaDiffGenerator::BsdiffFiles(const string& old_file,
1528 const string& new_file,
1529 vector<char>* out) {
1530 const string kPatchFile = "/tmp/delta.patchXXXXXX";
1531 string patch_file_path;
1532
1533 TEST_AND_RETURN_FALSE(
1534 utils::MakeTempFile(kPatchFile, &patch_file_path, NULL));
1535
1536 vector<string> cmd;
1537 cmd.push_back(kBsdiffPath);
1538 cmd.push_back(old_file);
1539 cmd.push_back(new_file);
1540 cmd.push_back(patch_file_path);
1541
1542 int rc = 1;
1543 vector<char> patch_file;
1544 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc));
1545 TEST_AND_RETURN_FALSE(rc == 0);
1546 TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out));
1547 unlink(patch_file_path.c_str());
1548 return true;
1549}
1550
1551// The |blocks| vector contains a reader and writer for each block on the
1552// filesystem that's being in-place updated. We populate the reader/writer
1553// fields of |blocks| by calling this function.
1554// For each block in |operation| that is read or written, find that block
1555// in |blocks| and set the reader/writer field to the vertex passed.
1556// |graph| is not strictly necessary, but useful for printing out
1557// error messages.
1558bool DeltaDiffGenerator::AddInstallOpToBlocksVector(
1559 const DeltaArchiveManifest_InstallOperation& operation,
1560 const Graph& graph,
1561 Vertex::Index vertex,
1562 vector<Block>* blocks) {
1563 // See if this is already present.
1564 TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0);
1565
1566 enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT };
1567 for (int field = READER; field < BLOCK_FIELD_COUNT; field++) {
1568 const int extents_size =
1569 (field == READER) ? operation.src_extents_size() :
1570 operation.dst_extents_size();
1571 const char* past_participle = (field == READER) ? "read" : "written";
1572 const google::protobuf::RepeatedPtrField<Extent>& extents =
1573 (field == READER) ? operation.src_extents() : operation.dst_extents();
1574 Vertex::Index Block::*access_type =
1575 (field == READER) ? &Block::reader : &Block::writer;
1576
1577 for (int i = 0; i < extents_size; i++) {
1578 const Extent& extent = extents.Get(i);
1579 if (extent.start_block() == kSparseHole) {
1580 // Hole in sparse file. skip
1581 continue;
1582 }
1583 for (uint64_t block = extent.start_block();
1584 block < (extent.start_block() + extent.num_blocks()); block++) {
1585 if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) {
1586 LOG(FATAL) << "Block " << block << " is already "
1587 << past_participle << " by "
1588 << (*blocks)[block].*access_type << "("
1589 << graph[(*blocks)[block].*access_type].file_name
1590 << ") and also " << vertex << "("
1591 << graph[vertex].file_name << ")";
1592 }
1593 (*blocks)[block].*access_type = vertex;
1594 }
1595 }
1596 }
1597 return true;
1598}
1599
Andrew de los Reyes50f36492010-11-01 13:57:12 -07001600const char* const kBsdiffPath = "bsdiff";
1601const char* const kBspatchPath = "bspatch";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001602const char* const kDeltaMagic = "CrAU";
1603
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001604}; // namespace chromeos_update_engine