Don Garrett | f4b2874 | 2012-03-27 20:48:06 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // 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 Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 6 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 9 | #include <inttypes.h> |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 10 | #include <sys/stat.h> |
| 11 | #include <sys/types.h> |
| 12 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 13 | #include <algorithm> |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 14 | #include <map> |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 15 | #include <set> |
| 16 | #include <string> |
| 17 | #include <utility> |
| 18 | #include <vector> |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 19 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 20 | #include <base/file_path.h> |
| 21 | #include <base/file_util.h> |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 22 | #include <base/logging.h> |
Darin Petkov | 7438a5c | 2011-08-29 11:56:44 -0700 | [diff] [blame] | 23 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 24 | #include <base/string_number_conversions.h> |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 25 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 26 | #include <base/stringprintf.h> |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 27 | #include <bzlib.h> |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 28 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 29 | #include "update_engine/bzip.h" |
| 30 | #include "update_engine/cycle_breaker.h" |
| 31 | #include "update_engine/extent_mapper.h" |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 32 | #include "update_engine/extent_ranges.h" |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 33 | #include "update_engine/file_writer.h" |
| 34 | #include "update_engine/filesystem_iterator.h" |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 35 | #include "update_engine/full_update_generator.h" |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 36 | #include "update_engine/graph_types.h" |
| 37 | #include "update_engine/graph_utils.h" |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 38 | #include "update_engine/metadata.h" |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 39 | #include "update_engine/omaha_hash_calculator.h" |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 40 | #include "update_engine/payload_signer.h" |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 41 | #include "update_engine/subprocess.h" |
| 42 | #include "update_engine/topological_sort.h" |
| 43 | #include "update_engine/update_metadata.pb.h" |
| 44 | #include "update_engine/utils.h" |
| 45 | |
| 46 | using std::make_pair; |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 47 | using std::map; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 48 | using std::max; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 49 | using std::min; |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 50 | using std::pair; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 51 | using std::set; |
| 52 | using std::string; |
| 53 | using std::vector; |
| 54 | |
| 55 | namespace chromeos_update_engine { |
| 56 | |
| 57 | typedef DeltaDiffGenerator::Block Block; |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 58 | typedef map<const DeltaArchiveManifest_InstallOperation*, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 59 | string> OperationNameMap; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 60 | |
Chris Sosa | f586b01 | 2013-05-21 13:33:42 -0700 | [diff] [blame^] | 61 | // bytes |
| 62 | const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024; |
Chris Sosa | d5ae156 | 2013-04-23 13:20:18 -0700 | [diff] [blame] | 63 | const uint64_t kVersionNumber = 1; |
| 64 | const uint64_t kFullUpdateChunkSize = 1024 * 1024; // bytes |
| 65 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 66 | namespace { |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 67 | const size_t kBlockSize = 4096; // bytes |
Andrew de los Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame] | 68 | const string kNonexistentPath = ""; |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 69 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 70 | |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 71 | static const char* kInstallOperationTypes[] = { |
| 72 | "REPLACE", |
| 73 | "REPLACE_BZ", |
| 74 | "MOVE", |
| 75 | "BSDIFF" |
| 76 | }; |
| 77 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 78 | // Stores all Extents for a file into 'out'. Returns true on success. |
| 79 | bool GatherExtents(const string& path, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 80 | off_t chunk_offset, |
| 81 | off_t chunk_size, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 82 | google::protobuf::RepeatedPtrField<Extent>* out) { |
| 83 | vector<Extent> extents; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 84 | TEST_AND_RETURN_FALSE( |
| 85 | extent_mapper::ExtentsForFileChunkFibmap( |
| 86 | path, chunk_offset, chunk_size, &extents)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 87 | DeltaDiffGenerator::StoreExtents(extents, out); |
| 88 | return true; |
| 89 | } |
| 90 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 91 | // For a given regular file which must exist at new_root + path, and |
| 92 | // may exist at old_root + path, creates a new InstallOperation and |
| 93 | // adds it to the graph. Also, populates the |blocks| array as |
| 94 | // necessary, if |blocks| is non-NULL. Also, writes the data |
| 95 | // necessary to send the file down to the client into data_fd, which |
| 96 | // has length *data_file_size. *data_file_size is updated |
| 97 | // appropriately. If |existing_vertex| is no kInvalidIndex, use that |
| 98 | // rather than allocating a new vertex. Returns true on success. |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 99 | bool DeltaReadFile(Graph* graph, |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 100 | Vertex::Index existing_vertex, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 101 | vector<Block>* blocks, |
| 102 | const string& old_root, |
| 103 | const string& new_root, |
| 104 | const string& path, // within new_root |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 105 | off_t chunk_offset, |
| 106 | off_t chunk_size, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 107 | int data_fd, |
| 108 | off_t* data_file_size) { |
| 109 | vector<char> data; |
| 110 | DeltaArchiveManifest_InstallOperation operation; |
| 111 | |
Andrew de los Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame] | 112 | string old_path = (old_root == kNonexistentPath) ? kNonexistentPath : |
| 113 | old_root + path; |
| 114 | |
Don Garrett | 1d78709 | 2013-03-11 18:07:28 -0700 | [diff] [blame] | 115 | // If bsdiff breaks again, blacklist the problem file by using: |
| 116 | // bsdiff_allowed = (path != "/foo/bar") |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 117 | // |
Don Garrett | 1d78709 | 2013-03-11 18:07:28 -0700 | [diff] [blame] | 118 | // TODO(dgarrett): chromium-os:15274 connect this test to the command line. |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 119 | bool bsdiff_allowed = true; |
Don Garrett | f4b2874 | 2012-03-27 20:48:06 -0700 | [diff] [blame] | 120 | |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 121 | if (!bsdiff_allowed) |
| 122 | LOG(INFO) << "bsdiff blacklisting: " << path; |
Don Garrett | f4b2874 | 2012-03-27 20:48:06 -0700 | [diff] [blame] | 123 | |
Andrew de los Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame] | 124 | TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_path, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 125 | new_root + path, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 126 | chunk_offset, |
| 127 | chunk_size, |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 128 | bsdiff_allowed, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 129 | &data, |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 130 | &operation, |
| 131 | true)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 132 | |
| 133 | // Write the data |
| 134 | if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) { |
| 135 | operation.set_data_offset(*data_file_size); |
| 136 | operation.set_data_length(data.size()); |
| 137 | } |
| 138 | |
| 139 | TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size())); |
| 140 | *data_file_size += data.size(); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 141 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 142 | // Now, insert into graph and blocks vector |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 143 | Vertex::Index vertex = existing_vertex; |
| 144 | if (vertex == Vertex::kInvalidIndex) { |
| 145 | graph->resize(graph->size() + 1); |
| 146 | vertex = graph->size() - 1; |
| 147 | } |
| 148 | (*graph)[vertex].op = operation; |
| 149 | CHECK((*graph)[vertex].op.has_type()); |
| 150 | (*graph)[vertex].file_name = path; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 151 | (*graph)[vertex].chunk_offset = chunk_offset; |
| 152 | (*graph)[vertex].chunk_size = chunk_size; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 153 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 154 | if (blocks) |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 155 | TEST_AND_RETURN_FALSE(DeltaDiffGenerator::AddInstallOpToBlocksVector( |
| 156 | (*graph)[vertex].op, |
| 157 | *graph, |
| 158 | vertex, |
| 159 | blocks)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
| 163 | // For each regular file within new_root, creates a node in the graph, |
| 164 | // determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF), |
| 165 | // and writes any necessary data to the end of data_fd. |
| 166 | bool DeltaReadFiles(Graph* graph, |
| 167 | vector<Block>* blocks, |
| 168 | const string& old_root, |
| 169 | const string& new_root, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 170 | off_t chunk_size, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 171 | int data_fd, |
| 172 | off_t* data_file_size) { |
| 173 | set<ino_t> visited_inodes; |
Andrew de los Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame] | 174 | set<ino_t> visited_src_inodes; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 175 | for (FilesystemIterator fs_iter(new_root, |
| 176 | utils::SetWithValue<string>("/lost+found")); |
| 177 | !fs_iter.IsEnd(); fs_iter.Increment()) { |
Andrew de los Reyes | 48a0a48 | 2011-02-22 15:32:11 -0800 | [diff] [blame] | 178 | // We never diff symlinks (here, we check that dst file is not a symlink). |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 179 | if (!S_ISREG(fs_iter.GetStat().st_mode)) |
| 180 | continue; |
| 181 | |
| 182 | // Make sure we visit each inode only once. |
| 183 | if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino)) |
| 184 | continue; |
| 185 | visited_inodes.insert(fs_iter.GetStat().st_ino); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 186 | off_t dst_size = fs_iter.GetStat().st_size; |
| 187 | if (dst_size == 0) |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 188 | continue; |
| 189 | |
| 190 | LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath(); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 191 | |
Andrew de los Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame] | 192 | // We can't visit each dst image inode more than once, as that would |
| 193 | // duplicate work. Here, we avoid visiting each source image inode |
| 194 | // more than once. Technically, we could have multiple operations |
| 195 | // that read the same blocks from the source image for diffing, but |
| 196 | // we choose not to to avoid complexity. Eventually we will move away |
| 197 | // from using a graph/cycle detection/etc to generate diffs, and at that |
| 198 | // time, it will be easy (non-complex) to have many operations read |
| 199 | // from the same source blocks. At that time, this code can die. -adlr |
Andrew de los Reyes | 48a0a48 | 2011-02-22 15:32:11 -0800 | [diff] [blame] | 200 | bool should_diff_from_source = false; |
Andrew de los Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame] | 201 | string src_path = old_root + fs_iter.GetPartialPath(); |
Andrew de los Reyes | 48a0a48 | 2011-02-22 15:32:11 -0800 | [diff] [blame] | 202 | struct stat src_stbuf; |
| 203 | // We never diff symlinks (here, we check that src file is not a symlink). |
| 204 | if (0 == lstat(src_path.c_str(), &src_stbuf) && |
| 205 | S_ISREG(src_stbuf.st_mode)) { |
Andrew de los Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame] | 206 | should_diff_from_source = !utils::SetContainsKey(visited_src_inodes, |
| 207 | src_stbuf.st_ino); |
| 208 | visited_src_inodes.insert(src_stbuf.st_ino); |
| 209 | } |
| 210 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 211 | off_t size = chunk_size == -1 ? dst_size : chunk_size; |
| 212 | off_t step = size; |
| 213 | for (off_t offset = 0; offset < dst_size; offset += step) { |
| 214 | if (offset + size >= dst_size) { |
| 215 | size = -1; // Read through the end of the file. |
| 216 | } |
| 217 | TEST_AND_RETURN_FALSE(DeltaReadFile(graph, |
| 218 | Vertex::kInvalidIndex, |
| 219 | blocks, |
| 220 | (should_diff_from_source ? |
| 221 | old_root : |
| 222 | kNonexistentPath), |
| 223 | new_root, |
| 224 | fs_iter.GetPartialPath(), |
| 225 | offset, |
| 226 | size, |
| 227 | data_fd, |
| 228 | data_file_size)); |
| 229 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 234 | // This class allocates non-existent temp blocks, starting from |
| 235 | // kTempBlockStart. Other code is responsible for converting these |
| 236 | // temp blocks into real blocks, as the client can't read or write to |
| 237 | // these blocks. |
| 238 | class DummyExtentAllocator { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 239 | public: |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 240 | explicit DummyExtentAllocator() |
| 241 | : next_block_(kTempBlockStart) {} |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 242 | vector<Extent> Allocate(const uint64_t block_count) { |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 243 | vector<Extent> ret(1); |
| 244 | ret[0].set_start_block(next_block_); |
| 245 | ret[0].set_num_blocks(block_count); |
| 246 | next_block_ += block_count; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 247 | return ret; |
| 248 | } |
| 249 | private: |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 250 | uint64_t next_block_; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | // Reads blocks from image_path that are not yet marked as being written |
| 254 | // in the blocks array. These blocks that remain are non-file-data blocks. |
| 255 | // In the future we might consider intelligent diffing between this data |
| 256 | // and data in the previous image, but for now we just bzip2 compress it |
| 257 | // and include it in the update. |
| 258 | // Creates a new node in the graph to write these blocks and writes the |
| 259 | // appropriate blob to blobs_fd. Reads and updates blobs_length; |
| 260 | bool ReadUnwrittenBlocks(const vector<Block>& blocks, |
| 261 | int blobs_fd, |
| 262 | off_t* blobs_length, |
| 263 | const string& image_path, |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 264 | Vertex* vertex) { |
Darin Petkov | abe7cc9 | 2010-10-08 12:29:32 -0700 | [diff] [blame] | 265 | vertex->file_name = "<rootfs-non-file-data>"; |
| 266 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 267 | DeltaArchiveManifest_InstallOperation* out_op = &vertex->op; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 268 | int image_fd = open(image_path.c_str(), O_RDONLY, 000); |
| 269 | TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0); |
| 270 | ScopedFdCloser image_fd_closer(&image_fd); |
| 271 | |
| 272 | string temp_file_path; |
| 273 | TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX", |
| 274 | &temp_file_path, |
| 275 | NULL)); |
| 276 | |
| 277 | FILE* file = fopen(temp_file_path.c_str(), "w"); |
| 278 | TEST_AND_RETURN_FALSE(file); |
| 279 | int err = BZ_OK; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 280 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 281 | BZFILE* bz_file = BZ2_bzWriteOpen(&err, |
| 282 | file, |
| 283 | 9, // max compression |
| 284 | 0, // verbosity |
| 285 | 0); // default work factor |
| 286 | TEST_AND_RETURN_FALSE(err == BZ_OK); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 287 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 288 | vector<Extent> extents; |
| 289 | vector<Block>::size_type block_count = 0; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 290 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 291 | LOG(INFO) << "Appending left over blocks to extents"; |
| 292 | for (vector<Block>::size_type i = 0; i < blocks.size(); i++) { |
| 293 | if (blocks[i].writer != Vertex::kInvalidIndex) |
| 294 | continue; |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 295 | if (blocks[i].reader != Vertex::kInvalidIndex) { |
| 296 | graph_utils::AddReadBeforeDep(vertex, blocks[i].reader, i); |
| 297 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 298 | graph_utils::AppendBlockToExtents(&extents, i); |
| 299 | block_count++; |
| 300 | } |
| 301 | |
| 302 | // Code will handle 'buf' at any size that's a multiple of kBlockSize, |
| 303 | // so we arbitrarily set it to 1024 * kBlockSize. |
| 304 | vector<char> buf(1024 * kBlockSize); |
| 305 | |
| 306 | LOG(INFO) << "Reading left over blocks"; |
| 307 | vector<Block>::size_type blocks_copied_count = 0; |
| 308 | |
| 309 | // For each extent in extents, write the data into BZ2_bzWrite which |
| 310 | // sends it to an output file. |
| 311 | // We use the temporary buffer 'buf' to hold the data, which may be |
| 312 | // smaller than the extent, so in that case we have to loop to get |
| 313 | // the extent's data (that's the inner while loop). |
| 314 | for (vector<Extent>::const_iterator it = extents.begin(); |
| 315 | it != extents.end(); ++it) { |
| 316 | vector<Block>::size_type blocks_read = 0; |
Andrew de los Reyes | 4b8740f | 2010-11-08 17:09:11 -0800 | [diff] [blame] | 317 | float printed_progress = -1; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 318 | while (blocks_read < it->num_blocks()) { |
| 319 | const int copy_block_cnt = |
| 320 | min(buf.size() / kBlockSize, |
| 321 | static_cast<vector<char>::size_type>( |
| 322 | it->num_blocks() - blocks_read)); |
| 323 | ssize_t rc = pread(image_fd, |
| 324 | &buf[0], |
| 325 | copy_block_cnt * kBlockSize, |
| 326 | (it->start_block() + blocks_read) * kBlockSize); |
| 327 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 328 | TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) == |
| 329 | copy_block_cnt * kBlockSize); |
| 330 | BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize); |
| 331 | TEST_AND_RETURN_FALSE(err == BZ_OK); |
| 332 | blocks_read += copy_block_cnt; |
| 333 | blocks_copied_count += copy_block_cnt; |
Andrew de los Reyes | 4b8740f | 2010-11-08 17:09:11 -0800 | [diff] [blame] | 334 | float current_progress = |
| 335 | static_cast<float>(blocks_copied_count) / block_count; |
| 336 | if (printed_progress + 0.1 < current_progress || |
| 337 | blocks_copied_count == block_count) { |
| 338 | LOG(INFO) << "progress: " << current_progress; |
| 339 | printed_progress = current_progress; |
| 340 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL); |
| 344 | TEST_AND_RETURN_FALSE(err == BZ_OK); |
| 345 | bz_file = NULL; |
| 346 | TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file)); |
| 347 | file = NULL; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 348 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 349 | vector<char> compressed_data; |
| 350 | LOG(INFO) << "Reading compressed data off disk"; |
| 351 | TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data)); |
| 352 | TEST_AND_RETURN_FALSE(unlink(temp_file_path.c_str()) == 0); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 353 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 354 | // Add node to graph to write these blocks |
| 355 | out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ); |
| 356 | out_op->set_data_offset(*blobs_length); |
| 357 | out_op->set_data_length(compressed_data.size()); |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 358 | LOG(INFO) << "Rootfs non-data blocks compressed take up " |
| 359 | << compressed_data.size(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 360 | *blobs_length += compressed_data.size(); |
| 361 | out_op->set_dst_length(kBlockSize * block_count); |
| 362 | DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 363 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 364 | TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, |
| 365 | &compressed_data[0], |
| 366 | compressed_data.size())); |
| 367 | LOG(INFO) << "done with extra blocks"; |
| 368 | return true; |
| 369 | } |
| 370 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 371 | // Writes the uint64_t passed in in host-endian to the file as big-endian. |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 372 | // Returns true on success. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 373 | bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) { |
| 374 | uint64_t value_be = htobe64(value); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 375 | TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be))); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 376 | return true; |
| 377 | } |
| 378 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 379 | // Adds each operation from |graph| to |out_manifest| in the order specified by |
| 380 | // |order| while building |out_op_name_map| with operation to name |
| 381 | // mappings. Adds all |kernel_ops| to |out_manifest|. Filters out no-op |
| 382 | // operations. |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 383 | void InstallOperationsToManifest( |
| 384 | const Graph& graph, |
| 385 | const vector<Vertex::Index>& order, |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 386 | const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops, |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 387 | DeltaArchiveManifest* out_manifest, |
| 388 | OperationNameMap* out_op_name_map) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 389 | for (vector<Vertex::Index>::const_iterator it = order.begin(); |
| 390 | it != order.end(); ++it) { |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 391 | const Vertex& vertex = graph[*it]; |
| 392 | const DeltaArchiveManifest_InstallOperation& add_op = vertex.op; |
| 393 | if (DeltaDiffGenerator::IsNoopOperation(add_op)) { |
| 394 | continue; |
| 395 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 396 | DeltaArchiveManifest_InstallOperation* op = |
| 397 | out_manifest->add_install_operations(); |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 398 | *op = add_op; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 399 | string name = vertex.file_name; |
| 400 | if (vertex.chunk_offset || vertex.chunk_size != -1) { |
| 401 | string offset = base::Int64ToString(vertex.chunk_offset); |
| 402 | if (vertex.chunk_size != -1) { |
| 403 | name += " [" + offset + ", " + |
| 404 | base::Int64ToString(vertex.chunk_offset + vertex.chunk_size - 1) + |
| 405 | "]"; |
| 406 | } else { |
| 407 | name += " [" + offset + ", end]"; |
| 408 | } |
| 409 | } |
| 410 | (*out_op_name_map)[op] = name; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 411 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 412 | for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it = |
| 413 | kernel_ops.begin(); it != kernel_ops.end(); ++it) { |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 414 | const DeltaArchiveManifest_InstallOperation& add_op = *it; |
| 415 | if (DeltaDiffGenerator::IsNoopOperation(add_op)) { |
| 416 | continue; |
| 417 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 418 | DeltaArchiveManifest_InstallOperation* op = |
| 419 | out_manifest->add_kernel_install_operations(); |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 420 | *op = add_op; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 421 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void CheckGraph(const Graph& graph) { |
| 425 | for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) { |
| 426 | CHECK(it->op.has_type()); |
| 427 | } |
| 428 | } |
| 429 | |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 430 | // Delta compresses a kernel partition |new_kernel_part| with knowledge of the |
| 431 | // old kernel partition |old_kernel_part|. If |old_kernel_part| is an empty |
| 432 | // string, generates a full update of the partition. |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 433 | bool DeltaCompressKernelPartition( |
| 434 | const string& old_kernel_part, |
| 435 | const string& new_kernel_part, |
| 436 | vector<DeltaArchiveManifest_InstallOperation>* ops, |
| 437 | int blobs_fd, |
| 438 | off_t* blobs_length) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 439 | LOG(INFO) << "Delta compressing kernel partition..."; |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 440 | LOG_IF(INFO, old_kernel_part.empty()) << "Generating full kernel update..."; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 441 | |
| 442 | // Add a new install operation |
| 443 | ops->resize(1); |
| 444 | DeltaArchiveManifest_InstallOperation* op = &(*ops)[0]; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 445 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 446 | vector<char> data; |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 447 | TEST_AND_RETURN_FALSE( |
| 448 | DeltaDiffGenerator::ReadFileToDiff(old_kernel_part, |
| 449 | new_kernel_part, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 450 | 0, // chunk_offset |
| 451 | -1, // chunk_size |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 452 | true, // bsdiff_allowed |
| 453 | &data, |
| 454 | op, |
| 455 | false)); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 456 | |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 457 | // Write the data |
| 458 | if (op->type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) { |
| 459 | op->set_data_offset(*blobs_length); |
| 460 | op->set_data_length(data.size()); |
| 461 | } |
Andrew de los Reyes | 36f3736 | 2010-09-03 09:20:04 -0700 | [diff] [blame] | 462 | |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 463 | TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data[0], data.size())); |
| 464 | *blobs_length += data.size(); |
Andrew de los Reyes | 36f3736 | 2010-09-03 09:20:04 -0700 | [diff] [blame] | 465 | |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 466 | LOG(INFO) << "Done delta compressing kernel partition: " |
| 467 | << kInstallOperationTypes[op->type()]; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 468 | return true; |
| 469 | } |
| 470 | |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 471 | struct DeltaObject { |
| 472 | DeltaObject(const string& in_name, const int in_type, const off_t in_size) |
| 473 | : name(in_name), |
| 474 | type(in_type), |
| 475 | size(in_size) {} |
| 476 | bool operator <(const DeltaObject& object) const { |
Darin Petkov | d43d690 | 2010-10-14 11:17:50 -0700 | [diff] [blame] | 477 | return (size != object.size) ? (size < object.size) : (name < object.name); |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 478 | } |
| 479 | string name; |
| 480 | int type; |
| 481 | off_t size; |
| 482 | }; |
| 483 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 484 | void ReportPayloadUsage(const DeltaArchiveManifest& manifest, |
| 485 | const int64_t manifest_metadata_size, |
| 486 | const OperationNameMap& op_name_map) { |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 487 | vector<DeltaObject> objects; |
| 488 | off_t total_size = 0; |
| 489 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 490 | // Rootfs install operations. |
| 491 | for (int i = 0; i < manifest.install_operations_size(); ++i) { |
| 492 | const DeltaArchiveManifest_InstallOperation& op = |
| 493 | manifest.install_operations(i); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 494 | objects.push_back(DeltaObject(op_name_map.find(&op)->second, |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 495 | op.type(), |
| 496 | op.data_length())); |
| 497 | total_size += op.data_length(); |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 500 | // Kernel install operations. |
| 501 | for (int i = 0; i < manifest.kernel_install_operations_size(); ++i) { |
| 502 | const DeltaArchiveManifest_InstallOperation& op = |
| 503 | manifest.kernel_install_operations(i); |
| 504 | objects.push_back(DeltaObject(StringPrintf("<kernel-operation-%d>", i), |
| 505 | op.type(), |
| 506 | op.data_length())); |
| 507 | total_size += op.data_length(); |
| 508 | } |
| 509 | |
Darin Petkov | 95cf01f | 2010-10-12 14:59:13 -0700 | [diff] [blame] | 510 | objects.push_back(DeltaObject("<manifest-metadata>", |
| 511 | -1, |
| 512 | manifest_metadata_size)); |
| 513 | total_size += manifest_metadata_size; |
| 514 | |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 515 | std::sort(objects.begin(), objects.end()); |
| 516 | |
| 517 | static const char kFormatString[] = "%6.2f%% %10llu %-10s %s\n"; |
| 518 | for (vector<DeltaObject>::const_iterator it = objects.begin(); |
| 519 | it != objects.end(); ++it) { |
| 520 | const DeltaObject& object = *it; |
| 521 | fprintf(stderr, kFormatString, |
| 522 | object.size * 100.0 / total_size, |
| 523 | object.size, |
Darin Petkov | 95cf01f | 2010-10-12 14:59:13 -0700 | [diff] [blame] | 524 | object.type >= 0 ? kInstallOperationTypes[object.type] : "-", |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 525 | object.name.c_str()); |
| 526 | } |
| 527 | fprintf(stderr, kFormatString, 100.0, total_size, "", "<total>"); |
| 528 | } |
| 529 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 530 | } // namespace {} |
| 531 | |
| 532 | bool DeltaDiffGenerator::ReadFileToDiff( |
| 533 | const string& old_filename, |
| 534 | const string& new_filename, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 535 | off_t chunk_offset, |
| 536 | off_t chunk_size, |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 537 | bool bsdiff_allowed, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 538 | vector<char>* out_data, |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 539 | DeltaArchiveManifest_InstallOperation* out_op, |
| 540 | bool gather_extents) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 541 | // Read new data in |
| 542 | vector<char> new_data; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 543 | TEST_AND_RETURN_FALSE( |
| 544 | utils::ReadFileChunk(new_filename, chunk_offset, chunk_size, &new_data)); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 545 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 546 | TEST_AND_RETURN_FALSE(!new_data.empty()); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 547 | TEST_AND_RETURN_FALSE(chunk_size == -1 || |
| 548 | static_cast<off_t>(new_data.size()) <= chunk_size); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 549 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 550 | vector<char> new_data_bz; |
| 551 | TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz)); |
| 552 | CHECK(!new_data_bz.empty()); |
| 553 | |
| 554 | vector<char> data; // Data blob that will be written to delta file. |
| 555 | |
| 556 | DeltaArchiveManifest_InstallOperation operation; |
| 557 | size_t current_best_size = 0; |
| 558 | if (new_data.size() <= new_data_bz.size()) { |
| 559 | operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE); |
| 560 | current_best_size = new_data.size(); |
| 561 | data = new_data; |
| 562 | } else { |
| 563 | operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ); |
| 564 | current_best_size = new_data_bz.size(); |
| 565 | data = new_data_bz; |
| 566 | } |
| 567 | |
| 568 | // Do we have an original file to consider? |
| 569 | struct stat old_stbuf; |
Don Garrett | f4b2874 | 2012-03-27 20:48:06 -0700 | [diff] [blame] | 570 | bool original = !old_filename.empty(); |
| 571 | if (original && 0 != stat(old_filename.c_str(), &old_stbuf)) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 572 | // If stat-ing the old file fails, it should be because it doesn't exist. |
| 573 | TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT); |
Don Garrett | f4b2874 | 2012-03-27 20:48:06 -0700 | [diff] [blame] | 574 | original = false; |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 575 | } |
Don Garrett | f4b2874 | 2012-03-27 20:48:06 -0700 | [diff] [blame] | 576 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 577 | vector<char> old_data; |
Don Garrett | f4b2874 | 2012-03-27 20:48:06 -0700 | [diff] [blame] | 578 | if (original) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 579 | // Read old data |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 580 | TEST_AND_RETURN_FALSE( |
| 581 | utils::ReadFileChunk( |
| 582 | old_filename, chunk_offset, chunk_size, &old_data)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 583 | if (old_data == new_data) { |
| 584 | // No change in data. |
| 585 | operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE); |
| 586 | current_best_size = 0; |
| 587 | data.clear(); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 588 | } else if (!old_data.empty() && bsdiff_allowed) { |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 589 | // If the source file is considered bsdiff safe (no bsdiff bugs |
| 590 | // triggered), see if BSDIFF encoding is smaller. |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 591 | FilePath old_chunk; |
| 592 | TEST_AND_RETURN_FALSE(file_util::CreateTemporaryFile(&old_chunk)); |
| 593 | ScopedPathUnlinker old_unlinker(old_chunk.value()); |
| 594 | TEST_AND_RETURN_FALSE( |
| 595 | utils::WriteFile(old_chunk.value().c_str(), |
| 596 | &old_data[0], old_data.size())); |
| 597 | FilePath new_chunk; |
| 598 | TEST_AND_RETURN_FALSE(file_util::CreateTemporaryFile(&new_chunk)); |
| 599 | ScopedPathUnlinker new_unlinker(new_chunk.value()); |
| 600 | TEST_AND_RETURN_FALSE( |
| 601 | utils::WriteFile(new_chunk.value().c_str(), |
| 602 | &new_data[0], new_data.size())); |
| 603 | |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 604 | vector<char> bsdiff_delta; |
| 605 | TEST_AND_RETURN_FALSE( |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 606 | BsdiffFiles(old_chunk.value(), new_chunk.value(), &bsdiff_delta)); |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 607 | CHECK_GT(bsdiff_delta.size(), static_cast<vector<char>::size_type>(0)); |
| 608 | if (bsdiff_delta.size() < current_best_size) { |
| 609 | operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF); |
| 610 | current_best_size = bsdiff_delta.size(); |
| 611 | data = bsdiff_delta; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | } |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 615 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 616 | // Set parameters of the operations |
| 617 | CHECK_EQ(data.size(), current_best_size); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 618 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 619 | if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE || |
| 620 | operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) { |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 621 | if (gather_extents) { |
| 622 | TEST_AND_RETURN_FALSE( |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 623 | GatherExtents(old_filename, |
| 624 | chunk_offset, |
| 625 | chunk_size, |
| 626 | operation.mutable_src_extents())); |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 627 | } else { |
| 628 | Extent* src_extent = operation.add_src_extents(); |
| 629 | src_extent->set_start_block(0); |
| 630 | src_extent->set_num_blocks( |
| 631 | (old_stbuf.st_size + kBlockSize - 1) / kBlockSize); |
| 632 | } |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 633 | operation.set_src_length(old_data.size()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 636 | if (gather_extents) { |
| 637 | TEST_AND_RETURN_FALSE( |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 638 | GatherExtents(new_filename, |
| 639 | chunk_offset, |
| 640 | chunk_size, |
| 641 | operation.mutable_dst_extents())); |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 642 | } else { |
| 643 | Extent* dst_extent = operation.add_dst_extents(); |
| 644 | dst_extent->set_start_block(0); |
| 645 | dst_extent->set_num_blocks((new_data.size() + kBlockSize - 1) / kBlockSize); |
| 646 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 647 | operation.set_dst_length(new_data.size()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 648 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 649 | out_data->swap(data); |
| 650 | *out_op = operation; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 651 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 652 | return true; |
| 653 | } |
| 654 | |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 655 | bool DeltaDiffGenerator::InitializePartitionInfo(bool is_kernel, |
| 656 | const string& partition, |
| 657 | PartitionInfo* info) { |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 658 | int64_t size = 0; |
| 659 | if (is_kernel) { |
| 660 | size = utils::FileSize(partition); |
| 661 | } else { |
| 662 | int block_count = 0, block_size = 0; |
| 663 | TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(partition, |
| 664 | &block_count, |
| 665 | &block_size)); |
| 666 | size = static_cast<int64_t>(block_count) * block_size; |
| 667 | } |
| 668 | TEST_AND_RETURN_FALSE(size > 0); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 669 | info->set_size(size); |
| 670 | OmahaHashCalculator hasher; |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 671 | TEST_AND_RETURN_FALSE(hasher.UpdateFile(partition, size) == size); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 672 | TEST_AND_RETURN_FALSE(hasher.Finalize()); |
| 673 | const vector<char>& hash = hasher.raw_hash(); |
| 674 | info->set_hash(hash.data(), hash.size()); |
Darin Petkov | d43d690 | 2010-10-14 11:17:50 -0700 | [diff] [blame] | 675 | LOG(INFO) << partition << ": size=" << size << " hash=" << hasher.hash(); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 676 | return true; |
| 677 | } |
| 678 | |
| 679 | bool InitializePartitionInfos(const string& old_kernel, |
| 680 | const string& new_kernel, |
| 681 | const string& old_rootfs, |
| 682 | const string& new_rootfs, |
| 683 | DeltaArchiveManifest* manifest) { |
Darin Petkov | d43d690 | 2010-10-14 11:17:50 -0700 | [diff] [blame] | 684 | if (!old_kernel.empty()) { |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 685 | TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo( |
| 686 | true, |
| 687 | old_kernel, |
| 688 | manifest->mutable_old_kernel_info())); |
Darin Petkov | d43d690 | 2010-10-14 11:17:50 -0700 | [diff] [blame] | 689 | } |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 690 | TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo( |
| 691 | true, |
| 692 | new_kernel, |
| 693 | manifest->mutable_new_kernel_info())); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 694 | if (!old_rootfs.empty()) { |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 695 | TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo( |
| 696 | false, |
| 697 | old_rootfs, |
| 698 | manifest->mutable_old_rootfs_info())); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 699 | } |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 700 | TEST_AND_RETURN_FALSE(DeltaDiffGenerator::InitializePartitionInfo( |
| 701 | false, |
| 702 | new_rootfs, |
| 703 | manifest->mutable_new_rootfs_info())); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 704 | return true; |
| 705 | } |
| 706 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 707 | namespace { |
| 708 | |
| 709 | // Takes a collection (vector or RepeatedPtrField) of Extent and |
| 710 | // returns a vector of the blocks referenced, in order. |
| 711 | template<typename T> |
| 712 | vector<uint64_t> ExpandExtents(const T& extents) { |
| 713 | vector<uint64_t> ret; |
| 714 | for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) { |
| 715 | const Extent extent = graph_utils::GetElement(extents, i); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 716 | if (extent.start_block() == kSparseHole) { |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 717 | ret.resize(ret.size() + extent.num_blocks(), kSparseHole); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 718 | } else { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 719 | for (uint64_t block = extent.start_block(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 720 | block < (extent.start_block() + extent.num_blocks()); block++) { |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 721 | ret.push_back(block); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | } |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | // Takes a vector of blocks and returns an equivalent vector of Extent |
| 729 | // objects. |
| 730 | vector<Extent> CompressExtents(const vector<uint64_t>& blocks) { |
| 731 | vector<Extent> new_extents; |
| 732 | for (vector<uint64_t>::const_iterator it = blocks.begin(), e = blocks.end(); |
| 733 | it != e; ++it) { |
| 734 | graph_utils::AppendBlockToExtents(&new_extents, *it); |
| 735 | } |
| 736 | return new_extents; |
| 737 | } |
| 738 | |
| 739 | } // namespace {} |
| 740 | |
| 741 | void DeltaDiffGenerator::SubstituteBlocks( |
| 742 | Vertex* vertex, |
| 743 | const vector<Extent>& remove_extents, |
| 744 | const vector<Extent>& replace_extents) { |
| 745 | // First, expand out the blocks that op reads from |
| 746 | vector<uint64_t> read_blocks = ExpandExtents(vertex->op.src_extents()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 747 | { |
| 748 | // Expand remove_extents and replace_extents |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 749 | vector<uint64_t> remove_extents_expanded = |
| 750 | ExpandExtents(remove_extents); |
| 751 | vector<uint64_t> replace_extents_expanded = |
| 752 | ExpandExtents(replace_extents); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 753 | CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size()); |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 754 | map<uint64_t, uint64_t> conversion; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 755 | for (vector<uint64_t>::size_type i = 0; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 756 | i < replace_extents_expanded.size(); i++) { |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 757 | conversion[remove_extents_expanded[i]] = replace_extents_expanded[i]; |
| 758 | } |
| 759 | utils::ApplyMap(&read_blocks, conversion); |
| 760 | for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(), |
| 761 | e = vertex->out_edges.end(); it != e; ++it) { |
| 762 | vector<uint64_t> write_before_deps_expanded = |
| 763 | ExpandExtents(it->second.write_extents); |
| 764 | utils::ApplyMap(&write_before_deps_expanded, conversion); |
| 765 | it->second.write_extents = CompressExtents(write_before_deps_expanded); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | // Convert read_blocks back to extents |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 769 | vertex->op.clear_src_extents(); |
| 770 | vector<Extent> new_extents = CompressExtents(read_blocks); |
| 771 | DeltaDiffGenerator::StoreExtents(new_extents, |
| 772 | vertex->op.mutable_src_extents()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | bool DeltaDiffGenerator::CutEdges(Graph* graph, |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 776 | const set<Edge>& edges, |
| 777 | vector<CutEdgeVertexes>* out_cuts) { |
| 778 | DummyExtentAllocator scratch_allocator; |
| 779 | vector<CutEdgeVertexes> cuts; |
| 780 | cuts.reserve(edges.size()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 781 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 782 | uint64_t scratch_blocks_used = 0; |
| 783 | for (set<Edge>::const_iterator it = edges.begin(); |
| 784 | it != edges.end(); ++it) { |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 785 | cuts.resize(cuts.size() + 1); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 786 | vector<Extent> old_extents = |
| 787 | (*graph)[it->first].out_edges[it->second].extents; |
| 788 | // Choose some scratch space |
| 789 | scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it); |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 790 | cuts.back().tmp_extents = |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 791 | scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it)); |
| 792 | // create vertex to copy original->scratch |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 793 | cuts.back().new_vertex = graph->size(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 794 | graph->resize(graph->size() + 1); |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 795 | cuts.back().old_src = it->first; |
| 796 | cuts.back().old_dst = it->second; |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 797 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 798 | EdgeProperties& cut_edge_properties = |
| 799 | (*graph)[it->first].out_edges.find(it->second)->second; |
| 800 | |
| 801 | // This should never happen, as we should only be cutting edges between |
| 802 | // real file nodes, and write-before relationships are created from |
| 803 | // a real file node to a temp copy node: |
| 804 | CHECK(cut_edge_properties.write_extents.empty()) |
| 805 | << "Can't cut edge that has write-before relationship."; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 806 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 807 | // make node depend on the copy operation |
| 808 | (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1, |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 809 | cut_edge_properties)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 810 | |
| 811 | // Set src/dst extents and other proto variables for copy operation |
| 812 | graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE); |
| 813 | DeltaDiffGenerator::StoreExtents( |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 814 | cut_edge_properties.extents, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 815 | graph->back().op.mutable_src_extents()); |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 816 | DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 817 | graph->back().op.mutable_dst_extents()); |
| 818 | graph->back().op.set_src_length( |
| 819 | graph_utils::EdgeWeight(*graph, *it) * kBlockSize); |
| 820 | graph->back().op.set_dst_length(graph->back().op.src_length()); |
| 821 | |
| 822 | // make the dest node read from the scratch space |
| 823 | DeltaDiffGenerator::SubstituteBlocks( |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 824 | &((*graph)[it->second]), |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 825 | (*graph)[it->first].out_edges[it->second].extents, |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 826 | cuts.back().tmp_extents); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 827 | |
| 828 | // delete the old edge |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 829 | CHECK_EQ(static_cast<Graph::size_type>(1), |
| 830 | (*graph)[it->first].out_edges.erase(it->second)); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 831 | |
Andrew de los Reyes | d12784c | 2010-07-26 13:55:14 -0700 | [diff] [blame] | 832 | // Add an edge from dst to copy operation |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 833 | EdgeProperties write_before_edge_properties; |
| 834 | write_before_edge_properties.write_extents = cuts.back().tmp_extents; |
| 835 | (*graph)[it->second].out_edges.insert( |
| 836 | make_pair(graph->size() - 1, write_before_edge_properties)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 837 | } |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 838 | out_cuts->swap(cuts); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 839 | return true; |
| 840 | } |
| 841 | |
| 842 | // Stores all Extents in 'extents' into 'out'. |
| 843 | void DeltaDiffGenerator::StoreExtents( |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 844 | const vector<Extent>& extents, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 845 | google::protobuf::RepeatedPtrField<Extent>* out) { |
| 846 | for (vector<Extent>::const_iterator it = extents.begin(); |
| 847 | it != extents.end(); ++it) { |
| 848 | Extent* new_extent = out->Add(); |
| 849 | *new_extent = *it; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | // Creates all the edges for the graph. Writers of a block point to |
| 854 | // readers of the same block. This is because for an edge A->B, B |
| 855 | // must complete before A executes. |
| 856 | void DeltaDiffGenerator::CreateEdges(Graph* graph, |
| 857 | const vector<Block>& blocks) { |
| 858 | for (vector<Block>::size_type i = 0; i < blocks.size(); i++) { |
| 859 | // Blocks with both a reader and writer get an edge |
| 860 | if (blocks[i].reader == Vertex::kInvalidIndex || |
| 861 | blocks[i].writer == Vertex::kInvalidIndex) |
| 862 | continue; |
| 863 | // Don't have a node depend on itself |
| 864 | if (blocks[i].reader == blocks[i].writer) |
| 865 | continue; |
| 866 | // See if there's already an edge we can add onto |
| 867 | Vertex::EdgeMap::iterator edge_it = |
| 868 | (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader); |
| 869 | if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) { |
| 870 | // No existing edge. Create one |
| 871 | (*graph)[blocks[i].writer].out_edges.insert( |
| 872 | make_pair(blocks[i].reader, EdgeProperties())); |
| 873 | edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 874 | CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 875 | } |
| 876 | graph_utils::AppendBlockToExtents(&edge_it->second.extents, i); |
| 877 | } |
| 878 | } |
| 879 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 880 | namespace { |
| 881 | |
| 882 | class SortCutsByTopoOrderLess { |
| 883 | public: |
| 884 | SortCutsByTopoOrderLess(vector<vector<Vertex::Index>::size_type>& table) |
| 885 | : table_(table) {} |
| 886 | bool operator()(const CutEdgeVertexes& a, const CutEdgeVertexes& b) { |
| 887 | return table_[a.old_dst] < table_[b.old_dst]; |
| 888 | } |
| 889 | private: |
| 890 | vector<vector<Vertex::Index>::size_type>& table_; |
| 891 | }; |
| 892 | |
| 893 | } // namespace {} |
| 894 | |
| 895 | void DeltaDiffGenerator::GenerateReverseTopoOrderMap( |
| 896 | vector<Vertex::Index>& op_indexes, |
| 897 | vector<vector<Vertex::Index>::size_type>* reverse_op_indexes) { |
| 898 | vector<vector<Vertex::Index>::size_type> table(op_indexes.size()); |
| 899 | for (vector<Vertex::Index>::size_type i = 0, e = op_indexes.size(); |
| 900 | i != e; ++i) { |
| 901 | Vertex::Index node = op_indexes[i]; |
| 902 | if (table.size() < (node + 1)) { |
| 903 | table.resize(node + 1); |
| 904 | } |
| 905 | table[node] = i; |
| 906 | } |
| 907 | reverse_op_indexes->swap(table); |
| 908 | } |
| 909 | |
| 910 | void DeltaDiffGenerator::SortCutsByTopoOrder(vector<Vertex::Index>& op_indexes, |
| 911 | vector<CutEdgeVertexes>* cuts) { |
| 912 | // first, make a reverse lookup table. |
| 913 | vector<vector<Vertex::Index>::size_type> table; |
| 914 | GenerateReverseTopoOrderMap(op_indexes, &table); |
| 915 | SortCutsByTopoOrderLess less(table); |
| 916 | sort(cuts->begin(), cuts->end(), less); |
| 917 | } |
| 918 | |
| 919 | void DeltaDiffGenerator::MoveFullOpsToBack(Graph* graph, |
| 920 | vector<Vertex::Index>* op_indexes) { |
| 921 | vector<Vertex::Index> ret; |
| 922 | vector<Vertex::Index> full_ops; |
| 923 | ret.reserve(op_indexes->size()); |
| 924 | for (vector<Vertex::Index>::size_type i = 0, e = op_indexes->size(); i != e; |
| 925 | ++i) { |
| 926 | DeltaArchiveManifest_InstallOperation_Type type = |
| 927 | (*graph)[(*op_indexes)[i]].op.type(); |
| 928 | if (type == DeltaArchiveManifest_InstallOperation_Type_REPLACE || |
| 929 | type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) { |
| 930 | full_ops.push_back((*op_indexes)[i]); |
| 931 | } else { |
| 932 | ret.push_back((*op_indexes)[i]); |
| 933 | } |
| 934 | } |
| 935 | LOG(INFO) << "Stats: " << full_ops.size() << " full ops out of " |
| 936 | << (full_ops.size() + ret.size()) << " total ops."; |
| 937 | ret.insert(ret.end(), full_ops.begin(), full_ops.end()); |
| 938 | op_indexes->swap(ret); |
| 939 | } |
| 940 | |
| 941 | namespace { |
| 942 | |
| 943 | template<typename T> |
| 944 | bool TempBlocksExistInExtents(const T& extents) { |
| 945 | for (int i = 0, e = extents.size(); i < e; ++i) { |
| 946 | Extent extent = graph_utils::GetElement(extents, i); |
| 947 | uint64_t start = extent.start_block(); |
| 948 | uint64_t num = extent.num_blocks(); |
| 949 | if (start == kSparseHole) |
| 950 | continue; |
| 951 | if (start >= kTempBlockStart || |
| 952 | (start + num) >= kTempBlockStart) { |
| 953 | LOG(ERROR) << "temp block!"; |
| 954 | LOG(ERROR) << "start: " << start << ", num: " << num; |
| 955 | LOG(ERROR) << "kTempBlockStart: " << kTempBlockStart; |
| 956 | LOG(ERROR) << "returning true"; |
| 957 | return true; |
| 958 | } |
| 959 | // check for wrap-around, which would be a bug: |
| 960 | CHECK(start <= (start + num)); |
| 961 | } |
| 962 | return false; |
| 963 | } |
| 964 | |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 965 | // Convertes the cuts, which must all have the same |old_dst| member, |
| 966 | // to full. It does this by converting the |old_dst| to REPLACE or |
| 967 | // REPLACE_BZ, dropping all incoming edges to |old_dst|, and marking |
| 968 | // all temp nodes invalid. |
| 969 | bool ConvertCutsToFull( |
| 970 | Graph* graph, |
| 971 | const string& new_root, |
| 972 | int data_fd, |
| 973 | off_t* data_file_size, |
| 974 | vector<Vertex::Index>* op_indexes, |
| 975 | vector<vector<Vertex::Index>::size_type>* reverse_op_indexes, |
| 976 | const vector<CutEdgeVertexes>& cuts) { |
| 977 | CHECK(!cuts.empty()); |
| 978 | set<Vertex::Index> deleted_nodes; |
| 979 | for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(), |
| 980 | e = cuts.end(); it != e; ++it) { |
| 981 | TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp( |
| 982 | graph, |
| 983 | *it, |
| 984 | new_root, |
| 985 | data_fd, |
| 986 | data_file_size)); |
| 987 | deleted_nodes.insert(it->new_vertex); |
| 988 | } |
| 989 | deleted_nodes.insert(cuts[0].old_dst); |
Darin Petkov | bc58a7b | 2010-11-03 11:52:53 -0700 | [diff] [blame] | 990 | |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 991 | vector<Vertex::Index> new_op_indexes; |
| 992 | new_op_indexes.reserve(op_indexes->size()); |
| 993 | for (vector<Vertex::Index>::iterator it = op_indexes->begin(), |
| 994 | e = op_indexes->end(); it != e; ++it) { |
| 995 | if (utils::SetContainsKey(deleted_nodes, *it)) |
| 996 | continue; |
| 997 | new_op_indexes.push_back(*it); |
| 998 | } |
| 999 | new_op_indexes.push_back(cuts[0].old_dst); |
| 1000 | op_indexes->swap(new_op_indexes); |
| 1001 | DeltaDiffGenerator::GenerateReverseTopoOrderMap(*op_indexes, |
| 1002 | reverse_op_indexes); |
| 1003 | return true; |
| 1004 | } |
| 1005 | |
| 1006 | // Tries to assign temp blocks for a collection of cuts, all of which share |
| 1007 | // the same old_dst member. If temp blocks can't be found, old_dst will be |
| 1008 | // converted to a REPLACE or REPLACE_BZ operation. Returns true on success, |
| 1009 | // which can happen even if blocks are converted to full. Returns false |
| 1010 | // on exceptional error cases. |
| 1011 | bool AssignBlockForAdjoiningCuts( |
| 1012 | Graph* graph, |
| 1013 | const string& new_root, |
| 1014 | int data_fd, |
| 1015 | off_t* data_file_size, |
| 1016 | vector<Vertex::Index>* op_indexes, |
| 1017 | vector<vector<Vertex::Index>::size_type>* reverse_op_indexes, |
| 1018 | const vector<CutEdgeVertexes>& cuts) { |
| 1019 | CHECK(!cuts.empty()); |
| 1020 | const Vertex::Index old_dst = cuts[0].old_dst; |
| 1021 | // Calculate # of blocks needed |
| 1022 | uint64_t blocks_needed = 0; |
| 1023 | map<const CutEdgeVertexes*, uint64_t> cuts_blocks_needed; |
| 1024 | for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(), |
| 1025 | e = cuts.end(); it != e; ++it) { |
| 1026 | uint64_t cut_blocks_needed = 0; |
| 1027 | for (vector<Extent>::const_iterator jt = it->tmp_extents.begin(), |
| 1028 | je = it->tmp_extents.end(); jt != je; ++jt) { |
| 1029 | cut_blocks_needed += jt->num_blocks(); |
| 1030 | } |
| 1031 | blocks_needed += cut_blocks_needed; |
| 1032 | cuts_blocks_needed[&*it] = cut_blocks_needed; |
| 1033 | } |
Darin Petkov | bc58a7b | 2010-11-03 11:52:53 -0700 | [diff] [blame] | 1034 | |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1035 | // Find enough blocks |
| 1036 | ExtentRanges scratch_ranges; |
| 1037 | // Each block that's supplying temp blocks and the corresponding blocks: |
| 1038 | typedef vector<pair<Vertex::Index, ExtentRanges> > SupplierVector; |
| 1039 | SupplierVector block_suppliers; |
| 1040 | uint64_t scratch_blocks_found = 0; |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1041 | for (vector<Vertex::Index>::size_type i = (*reverse_op_indexes)[old_dst] + 1, |
| 1042 | e = op_indexes->size(); i < e; ++i) { |
| 1043 | Vertex::Index test_node = (*op_indexes)[i]; |
| 1044 | if (!(*graph)[test_node].valid) |
| 1045 | continue; |
| 1046 | // See if this node has sufficient blocks |
| 1047 | ExtentRanges ranges; |
| 1048 | ranges.AddRepeatedExtents((*graph)[test_node].op.dst_extents()); |
| 1049 | ranges.SubtractExtent(ExtentForRange( |
| 1050 | kTempBlockStart, kSparseHole - kTempBlockStart)); |
| 1051 | ranges.SubtractRepeatedExtents((*graph)[test_node].op.src_extents()); |
| 1052 | // For now, for simplicity, subtract out all blocks in read-before |
| 1053 | // dependencies. |
| 1054 | for (Vertex::EdgeMap::const_iterator edge_i = |
| 1055 | (*graph)[test_node].out_edges.begin(), |
| 1056 | edge_e = (*graph)[test_node].out_edges.end(); |
| 1057 | edge_i != edge_e; ++edge_i) { |
| 1058 | ranges.SubtractExtents(edge_i->second.extents); |
| 1059 | } |
| 1060 | if (ranges.blocks() == 0) |
| 1061 | continue; |
| 1062 | |
| 1063 | if (ranges.blocks() + scratch_blocks_found > blocks_needed) { |
| 1064 | // trim down ranges |
| 1065 | vector<Extent> new_ranges = ranges.GetExtentsForBlockCount( |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 1066 | blocks_needed - scratch_blocks_found); |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1067 | ranges = ExtentRanges(); |
| 1068 | ranges.AddExtents(new_ranges); |
| 1069 | } |
| 1070 | scratch_ranges.AddRanges(ranges); |
| 1071 | block_suppliers.push_back(make_pair(test_node, ranges)); |
| 1072 | scratch_blocks_found += ranges.blocks(); |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1073 | if (scratch_ranges.blocks() >= blocks_needed) |
| 1074 | break; |
| 1075 | } |
| 1076 | if (scratch_ranges.blocks() < blocks_needed) { |
| 1077 | LOG(INFO) << "Unable to find sufficient scratch"; |
| 1078 | TEST_AND_RETURN_FALSE(ConvertCutsToFull(graph, |
| 1079 | new_root, |
| 1080 | data_fd, |
| 1081 | data_file_size, |
| 1082 | op_indexes, |
| 1083 | reverse_op_indexes, |
| 1084 | cuts)); |
| 1085 | return true; |
| 1086 | } |
| 1087 | // Use the scratch we found |
| 1088 | TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found); |
| 1089 | |
| 1090 | // Make all the suppliers depend on this node |
| 1091 | for (SupplierVector::iterator it = block_suppliers.begin(), |
| 1092 | e = block_suppliers.end(); it != e; ++it) { |
| 1093 | graph_utils::AddReadBeforeDepExtents( |
| 1094 | &(*graph)[it->first], |
| 1095 | old_dst, |
| 1096 | it->second.GetExtentsForBlockCount(it->second.blocks())); |
| 1097 | } |
Darin Petkov | bc58a7b | 2010-11-03 11:52:53 -0700 | [diff] [blame] | 1098 | |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1099 | // Replace temp blocks in each cut |
| 1100 | for (vector<CutEdgeVertexes>::const_iterator it = cuts.begin(), |
| 1101 | e = cuts.end(); it != e; ++it) { |
| 1102 | vector<Extent> real_extents = |
| 1103 | scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]); |
| 1104 | scratch_ranges.SubtractExtents(real_extents); |
| 1105 | |
| 1106 | // Fix the old dest node w/ the real blocks |
| 1107 | DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst], |
| 1108 | it->tmp_extents, |
| 1109 | real_extents); |
| 1110 | |
| 1111 | // Fix the new node w/ the real blocks. Since the new node is just a |
| 1112 | // copy operation, we can replace all the dest extents w/ the real |
| 1113 | // blocks. |
| 1114 | DeltaArchiveManifest_InstallOperation *op = |
| 1115 | &(*graph)[it->new_vertex].op; |
| 1116 | op->clear_dst_extents(); |
| 1117 | DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents()); |
| 1118 | } |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1119 | return true; |
| 1120 | } |
| 1121 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1122 | } // namespace {} |
| 1123 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 1124 | // Returns true if |op| is a no-op operation that doesn't do any useful work |
| 1125 | // (e.g., a move operation that copies blocks onto themselves). |
| 1126 | bool DeltaDiffGenerator::IsNoopOperation( |
| 1127 | const DeltaArchiveManifest_InstallOperation& op) { |
| 1128 | return (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE && |
| 1129 | ExpandExtents(op.src_extents()) == ExpandExtents(op.dst_extents())); |
| 1130 | } |
| 1131 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1132 | bool DeltaDiffGenerator::AssignTempBlocks( |
| 1133 | Graph* graph, |
| 1134 | const string& new_root, |
| 1135 | int data_fd, |
| 1136 | off_t* data_file_size, |
| 1137 | vector<Vertex::Index>* op_indexes, |
| 1138 | vector<vector<Vertex::Index>::size_type>* reverse_op_indexes, |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1139 | const vector<CutEdgeVertexes>& cuts) { |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1140 | CHECK(!cuts.empty()); |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1141 | |
| 1142 | // group of cuts w/ the same old_dst: |
| 1143 | vector<CutEdgeVertexes> cuts_group; |
| 1144 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1145 | for (vector<CutEdgeVertexes>::size_type i = cuts.size() - 1, e = 0; |
| 1146 | true ; --i) { |
| 1147 | LOG(INFO) << "Fixing temp blocks in cut " << i |
| 1148 | << ": old dst: " << cuts[i].old_dst << " new vertex: " |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1149 | << cuts[i].new_vertex << " path: " |
| 1150 | << (*graph)[cuts[i].old_dst].file_name; |
| 1151 | |
| 1152 | if (cuts_group.empty() || (cuts_group[0].old_dst == cuts[i].old_dst)) { |
| 1153 | cuts_group.push_back(cuts[i]); |
| 1154 | } else { |
| 1155 | CHECK(!cuts_group.empty()); |
| 1156 | TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph, |
| 1157 | new_root, |
| 1158 | data_fd, |
| 1159 | data_file_size, |
| 1160 | op_indexes, |
| 1161 | reverse_op_indexes, |
| 1162 | cuts_group)); |
| 1163 | cuts_group.clear(); |
| 1164 | cuts_group.push_back(cuts[i]); |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1165 | } |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 1166 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1167 | if (i == e) { |
| 1168 | // break out of for() loop |
| 1169 | break; |
| 1170 | } |
| 1171 | } |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1172 | CHECK(!cuts_group.empty()); |
| 1173 | TEST_AND_RETURN_FALSE(AssignBlockForAdjoiningCuts(graph, |
| 1174 | new_root, |
| 1175 | data_fd, |
| 1176 | data_file_size, |
| 1177 | op_indexes, |
| 1178 | reverse_op_indexes, |
| 1179 | cuts_group)); |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1180 | return true; |
| 1181 | } |
| 1182 | |
| 1183 | bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) { |
| 1184 | size_t idx = 0; |
| 1185 | for (Graph::const_iterator it = graph.begin(), e = graph.end(); it != e; |
| 1186 | ++it, ++idx) { |
| 1187 | if (!it->valid) |
| 1188 | continue; |
| 1189 | const DeltaArchiveManifest_InstallOperation& op = it->op; |
| 1190 | if (TempBlocksExistInExtents(op.dst_extents()) || |
| 1191 | TempBlocksExistInExtents(op.src_extents())) { |
| 1192 | LOG(INFO) << "bad extents in node " << idx; |
| 1193 | LOG(INFO) << "so yeah"; |
| 1194 | return false; |
| 1195 | } |
| 1196 | |
| 1197 | // Check out-edges: |
| 1198 | for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(), |
| 1199 | je = it->out_edges.end(); jt != je; ++jt) { |
| 1200 | if (TempBlocksExistInExtents(jt->second.extents) || |
| 1201 | TempBlocksExistInExtents(jt->second.write_extents)) { |
| 1202 | LOG(INFO) << "bad out edge in node " << idx; |
| 1203 | LOG(INFO) << "so yeah"; |
| 1204 | return false; |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | return true; |
| 1209 | } |
| 1210 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1211 | bool DeltaDiffGenerator::ReorderDataBlobs( |
| 1212 | DeltaArchiveManifest* manifest, |
| 1213 | const std::string& data_blobs_path, |
| 1214 | const std::string& new_data_blobs_path) { |
| 1215 | int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0); |
| 1216 | TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0); |
| 1217 | ScopedFdCloser in_fd_closer(&in_fd); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 1218 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1219 | DirectFileWriter writer; |
| 1220 | TEST_AND_RETURN_FALSE( |
| 1221 | writer.Open(new_data_blobs_path.c_str(), |
| 1222 | O_WRONLY | O_TRUNC | O_CREAT, |
| 1223 | 0644) == 0); |
| 1224 | ScopedFileWriterCloser writer_closer(&writer); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1225 | uint64_t out_file_size = 0; |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 1226 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 1227 | for (int i = 0; i < (manifest->install_operations_size() + |
| 1228 | manifest->kernel_install_operations_size()); i++) { |
| 1229 | DeltaArchiveManifest_InstallOperation* op = NULL; |
| 1230 | if (i < manifest->install_operations_size()) { |
| 1231 | op = manifest->mutable_install_operations(i); |
| 1232 | } else { |
| 1233 | op = manifest->mutable_kernel_install_operations( |
| 1234 | i - manifest->install_operations_size()); |
| 1235 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1236 | if (!op->has_data_offset()) |
| 1237 | continue; |
| 1238 | CHECK(op->has_data_length()); |
| 1239 | vector<char> buf(op->data_length()); |
| 1240 | ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset()); |
| 1241 | TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size())); |
| 1242 | |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1243 | // Add the hash of the data blobs for this operation |
| 1244 | TEST_AND_RETURN_FALSE(AddOperationHash(op, buf)); |
| 1245 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1246 | op->set_data_offset(out_file_size); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 1247 | TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size())); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1248 | out_file_size += buf.size(); |
| 1249 | } |
| 1250 | return true; |
| 1251 | } |
| 1252 | |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1253 | bool DeltaDiffGenerator::AddOperationHash( |
| 1254 | DeltaArchiveManifest_InstallOperation* op, |
| 1255 | const vector<char>& buf) { |
| 1256 | OmahaHashCalculator hasher; |
| 1257 | |
| 1258 | TEST_AND_RETURN_FALSE(hasher.Update(&buf[0], buf.size())); |
| 1259 | TEST_AND_RETURN_FALSE(hasher.Finalize()); |
| 1260 | |
| 1261 | const vector<char>& hash = hasher.raw_hash(); |
| 1262 | op->set_data_sha256_hash(hash.data(), hash.size()); |
| 1263 | return true; |
| 1264 | } |
| 1265 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1266 | bool DeltaDiffGenerator::ConvertCutToFullOp(Graph* graph, |
| 1267 | const CutEdgeVertexes& cut, |
| 1268 | const string& new_root, |
| 1269 | int data_fd, |
| 1270 | off_t* data_file_size) { |
| 1271 | // Drop all incoming edges, keep all outgoing edges |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 1272 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1273 | // Keep all outgoing edges |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1274 | if ((*graph)[cut.old_dst].op.type() != |
| 1275 | DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ && |
| 1276 | (*graph)[cut.old_dst].op.type() != |
| 1277 | DeltaArchiveManifest_InstallOperation_Type_REPLACE) { |
| 1278 | Vertex::EdgeMap out_edges = (*graph)[cut.old_dst].out_edges; |
| 1279 | graph_utils::DropWriteBeforeDeps(&out_edges); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 1280 | |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1281 | TEST_AND_RETURN_FALSE(DeltaReadFile(graph, |
| 1282 | cut.old_dst, |
| 1283 | NULL, |
Andrew de los Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame] | 1284 | kNonexistentPath, |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1285 | new_root, |
| 1286 | (*graph)[cut.old_dst].file_name, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 1287 | (*graph)[cut.old_dst].chunk_offset, |
| 1288 | (*graph)[cut.old_dst].chunk_size, |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1289 | data_fd, |
| 1290 | data_file_size)); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 1291 | |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1292 | (*graph)[cut.old_dst].out_edges = out_edges; |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1293 | |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1294 | // Right now we don't have doubly-linked edges, so we have to scan |
| 1295 | // the whole graph. |
| 1296 | graph_utils::DropIncomingEdgesTo(graph, cut.old_dst); |
| 1297 | } |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1298 | |
| 1299 | // Delete temp node |
| 1300 | (*graph)[cut.old_src].out_edges.erase(cut.new_vertex); |
| 1301 | CHECK((*graph)[cut.old_dst].out_edges.find(cut.new_vertex) == |
| 1302 | (*graph)[cut.old_dst].out_edges.end()); |
| 1303 | (*graph)[cut.new_vertex].valid = false; |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1304 | LOG(INFO) << "marked node invalid: " << cut.new_vertex; |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1305 | return true; |
| 1306 | } |
| 1307 | |
| 1308 | bool DeltaDiffGenerator::ConvertGraphToDag(Graph* graph, |
| 1309 | const string& new_root, |
| 1310 | int fd, |
| 1311 | off_t* data_file_size, |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 1312 | vector<Vertex::Index>* final_order, |
| 1313 | Vertex::Index scratch_vertex) { |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1314 | CycleBreaker cycle_breaker; |
| 1315 | LOG(INFO) << "Finding cycles..."; |
| 1316 | set<Edge> cut_edges; |
| 1317 | cycle_breaker.BreakCycles(*graph, &cut_edges); |
| 1318 | LOG(INFO) << "done finding cycles"; |
| 1319 | CheckGraph(*graph); |
| 1320 | |
| 1321 | // Calculate number of scratch blocks needed |
| 1322 | |
| 1323 | LOG(INFO) << "Cutting cycles..."; |
| 1324 | vector<CutEdgeVertexes> cuts; |
| 1325 | TEST_AND_RETURN_FALSE(CutEdges(graph, cut_edges, &cuts)); |
| 1326 | LOG(INFO) << "done cutting cycles"; |
| 1327 | LOG(INFO) << "There are " << cuts.size() << " cuts."; |
| 1328 | CheckGraph(*graph); |
| 1329 | |
| 1330 | LOG(INFO) << "Creating initial topological order..."; |
| 1331 | TopologicalSort(*graph, final_order); |
| 1332 | LOG(INFO) << "done with initial topo order"; |
| 1333 | CheckGraph(*graph); |
| 1334 | |
| 1335 | LOG(INFO) << "Moving full ops to the back"; |
| 1336 | MoveFullOpsToBack(graph, final_order); |
| 1337 | LOG(INFO) << "done moving full ops to back"; |
| 1338 | |
| 1339 | vector<vector<Vertex::Index>::size_type> inverse_final_order; |
| 1340 | GenerateReverseTopoOrderMap(*final_order, &inverse_final_order); |
| 1341 | |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1342 | SortCutsByTopoOrder(*final_order, &cuts); |
| 1343 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1344 | if (!cuts.empty()) |
| 1345 | TEST_AND_RETURN_FALSE(AssignTempBlocks(graph, |
| 1346 | new_root, |
| 1347 | fd, |
| 1348 | data_file_size, |
| 1349 | final_order, |
| 1350 | &inverse_final_order, |
| 1351 | cuts)); |
| 1352 | LOG(INFO) << "Making sure all temp blocks have been allocated"; |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 1353 | |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 1354 | // Remove the scratch node, if any |
| 1355 | if (scratch_vertex != Vertex::kInvalidIndex) { |
| 1356 | final_order->erase(final_order->begin() + |
| 1357 | inverse_final_order[scratch_vertex]); |
| 1358 | (*graph)[scratch_vertex].valid = false; |
| 1359 | GenerateReverseTopoOrderMap(*final_order, &inverse_final_order); |
| 1360 | } |
| 1361 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1362 | graph_utils::DumpGraph(*graph); |
| 1363 | CHECK(NoTempBlocksRemain(*graph)); |
| 1364 | LOG(INFO) << "done making sure all temp blocks are allocated"; |
| 1365 | return true; |
| 1366 | } |
| 1367 | |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 1368 | void DeltaDiffGenerator::CreateScratchNode(uint64_t start_block, |
| 1369 | uint64_t num_blocks, |
| 1370 | Vertex* vertex) { |
| 1371 | vertex->file_name = "<scratch>"; |
| 1372 | vertex->op.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ); |
| 1373 | vertex->op.set_data_offset(0); |
| 1374 | vertex->op.set_data_length(0); |
| 1375 | Extent* extent = vertex->op.add_dst_extents(); |
| 1376 | extent->set_start_block(start_block); |
| 1377 | extent->set_num_blocks(num_blocks); |
| 1378 | } |
| 1379 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 1380 | bool DeltaDiffGenerator::GenerateDeltaUpdateFile( |
| 1381 | const string& old_root, |
| 1382 | const string& old_image, |
| 1383 | const string& new_root, |
| 1384 | const string& new_image, |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1385 | const string& old_kernel_part, |
| 1386 | const string& new_kernel_part, |
| 1387 | const string& output_path, |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1388 | const string& private_key_path, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 1389 | off_t chunk_size, |
Chris Sosa | d5ae156 | 2013-04-23 13:20:18 -0700 | [diff] [blame] | 1390 | size_t rootfs_partition_size, |
Don Garrett | 0dd3985 | 2013-04-03 16:55:42 -0700 | [diff] [blame] | 1391 | const ImageInfo* old_image_info, |
| 1392 | const ImageInfo* new_image_info, |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1393 | uint64_t* metadata_size) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 1394 | TEST_AND_RETURN_FALSE(chunk_size == -1 || chunk_size % kBlockSize == 0); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 1395 | int old_image_block_count = 0, old_image_block_size = 0; |
| 1396 | int new_image_block_count = 0, new_image_block_size = 0; |
| 1397 | TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(new_image, |
| 1398 | &new_image_block_count, |
| 1399 | &new_image_block_size)); |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 1400 | if (!old_image.empty()) { |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 1401 | TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(old_image, |
| 1402 | &old_image_block_count, |
| 1403 | &old_image_block_size)); |
| 1404 | TEST_AND_RETURN_FALSE(old_image_block_size == new_image_block_size); |
| 1405 | LOG_IF(WARNING, old_image_block_count != new_image_block_count) |
| 1406 | << "Old and new images have different block counts."; |
Don Garrett | 0dd3985 | 2013-04-03 16:55:42 -0700 | [diff] [blame] | 1407 | |
| 1408 | // If new_image_info is present, old_image_info must but be present. |
| 1409 | TEST_AND_RETURN_FALSE((bool)old_image_info == (bool)new_image_info); |
| 1410 | } else { |
| 1411 | // old_image_info must not be present for a full update. |
| 1412 | TEST_AND_RETURN_FALSE(!old_image_info); |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 1413 | } |
Chris Sosa | d5ae156 | 2013-04-23 13:20:18 -0700 | [diff] [blame] | 1414 | |
| 1415 | // Sanity checks for the partition size. |
| 1416 | TEST_AND_RETURN_FALSE(rootfs_partition_size % kBlockSize == 0); |
Chris Sosa | e9f5f42 | 2013-05-17 16:11:10 -0700 | [diff] [blame] | 1417 | size_t fs_size = static_cast<size_t>(new_image_block_size) * |
| 1418 | new_image_block_count; |
Chris Sosa | d5ae156 | 2013-04-23 13:20:18 -0700 | [diff] [blame] | 1419 | LOG(INFO) << "Rootfs partition size: " << rootfs_partition_size; |
| 1420 | LOG(INFO) << "Actual filesystem size: " << fs_size; |
| 1421 | TEST_AND_RETURN_FALSE(rootfs_partition_size >= fs_size); |
| 1422 | |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 1423 | // Sanity check kernel partition arg |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 1424 | TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0); |
| 1425 | |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 1426 | vector<Block> blocks(max(old_image_block_count, new_image_block_count)); |
| 1427 | LOG(INFO) << "Invalid block index: " << Vertex::kInvalidIndex; |
| 1428 | LOG(INFO) << "Block count: " << blocks.size(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1429 | for (vector<Block>::size_type i = 0; i < blocks.size(); i++) { |
| 1430 | CHECK(blocks[i].reader == Vertex::kInvalidIndex); |
| 1431 | CHECK(blocks[i].writer == Vertex::kInvalidIndex); |
| 1432 | } |
| 1433 | Graph graph; |
| 1434 | CheckGraph(graph); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1435 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1436 | const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX"); |
| 1437 | string temp_file_path; |
Darin Petkov | 7438a5c | 2011-08-29 11:56:44 -0700 | [diff] [blame] | 1438 | scoped_ptr<ScopedPathUnlinker> temp_file_unlinker; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1439 | off_t data_file_size = 0; |
| 1440 | |
| 1441 | LOG(INFO) << "Reading files..."; |
| 1442 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 1443 | vector<DeltaArchiveManifest_InstallOperation> kernel_ops; |
| 1444 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 1445 | vector<Vertex::Index> final_order; |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 1446 | Vertex::Index scratch_vertex = Vertex::kInvalidIndex; |
Andrew de los Reyes | f88144f | 2010-10-11 10:32:59 -0700 | [diff] [blame] | 1447 | { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1448 | int fd; |
| 1449 | TEST_AND_RETURN_FALSE( |
| 1450 | utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd)); |
Darin Petkov | 7438a5c | 2011-08-29 11:56:44 -0700 | [diff] [blame] | 1451 | temp_file_unlinker.reset(new ScopedPathUnlinker(temp_file_path)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1452 | TEST_AND_RETURN_FALSE(fd >= 0); |
| 1453 | ScopedFdCloser fd_closer(&fd); |
Andrew de los Reyes | f88144f | 2010-10-11 10:32:59 -0700 | [diff] [blame] | 1454 | if (!old_image.empty()) { |
| 1455 | // Delta update |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1456 | |
Andrew de los Reyes | f88144f | 2010-10-11 10:32:59 -0700 | [diff] [blame] | 1457 | TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph, |
| 1458 | &blocks, |
| 1459 | old_root, |
| 1460 | new_root, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 1461 | chunk_size, |
Andrew de los Reyes | f88144f | 2010-10-11 10:32:59 -0700 | [diff] [blame] | 1462 | fd, |
| 1463 | &data_file_size)); |
| 1464 | LOG(INFO) << "done reading normal files"; |
| 1465 | CheckGraph(graph); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1466 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 1467 | LOG(INFO) << "Starting metadata processing"; |
| 1468 | TEST_AND_RETURN_FALSE(Metadata::DeltaReadMetadata(&graph, |
| 1469 | &blocks, |
| 1470 | old_image, |
| 1471 | new_image, |
| 1472 | fd, |
| 1473 | &data_file_size)); |
| 1474 | LOG(INFO) << "Done metadata processing"; |
| 1475 | CheckGraph(graph); |
| 1476 | |
Andrew de los Reyes | f88144f | 2010-10-11 10:32:59 -0700 | [diff] [blame] | 1477 | graph.resize(graph.size() + 1); |
| 1478 | TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks, |
| 1479 | fd, |
| 1480 | &data_file_size, |
| 1481 | new_image, |
| 1482 | &graph.back())); |
| 1483 | |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 1484 | // Final scratch block (if there's space) |
Chris Sosa | d5ae156 | 2013-04-23 13:20:18 -0700 | [diff] [blame] | 1485 | if (blocks.size() < (rootfs_partition_size / kBlockSize)) { |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 1486 | scratch_vertex = graph.size(); |
| 1487 | graph.resize(graph.size() + 1); |
| 1488 | CreateScratchNode(blocks.size(), |
Chris Sosa | d5ae156 | 2013-04-23 13:20:18 -0700 | [diff] [blame] | 1489 | (rootfs_partition_size / kBlockSize) - blocks.size(), |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 1490 | &graph.back()); |
| 1491 | } |
| 1492 | |
Andrew de los Reyes | f88144f | 2010-10-11 10:32:59 -0700 | [diff] [blame] | 1493 | // Read kernel partition |
| 1494 | TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part, |
| 1495 | new_kernel_part, |
| 1496 | &kernel_ops, |
| 1497 | fd, |
| 1498 | &data_file_size)); |
| 1499 | |
| 1500 | LOG(INFO) << "done reading kernel"; |
| 1501 | CheckGraph(graph); |
| 1502 | |
| 1503 | LOG(INFO) << "Creating edges..."; |
| 1504 | CreateEdges(&graph, blocks); |
| 1505 | LOG(INFO) << "Done creating edges"; |
| 1506 | CheckGraph(graph); |
| 1507 | |
| 1508 | TEST_AND_RETURN_FALSE(ConvertGraphToDag(&graph, |
| 1509 | new_root, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1510 | fd, |
| 1511 | &data_file_size, |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 1512 | &final_order, |
| 1513 | scratch_vertex)); |
Andrew de los Reyes | f88144f | 2010-10-11 10:32:59 -0700 | [diff] [blame] | 1514 | } else { |
| 1515 | // Full update |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 1516 | off_t new_image_size = |
| 1517 | static_cast<off_t>(new_image_block_count) * new_image_block_size; |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 1518 | TEST_AND_RETURN_FALSE(FullUpdateGenerator::Run(&graph, |
| 1519 | new_kernel_part, |
| 1520 | new_image, |
| 1521 | new_image_size, |
| 1522 | fd, |
| 1523 | &data_file_size, |
| 1524 | kFullUpdateChunkSize, |
| 1525 | kBlockSize, |
| 1526 | &kernel_ops, |
| 1527 | &final_order)); |
Andrew de los Reyes | f88144f | 2010-10-11 10:32:59 -0700 | [diff] [blame] | 1528 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1529 | } |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1530 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1531 | // Convert to protobuf Manifest object |
| 1532 | DeltaArchiveManifest manifest; |
Don Garrett | 0dd3985 | 2013-04-03 16:55:42 -0700 | [diff] [blame] | 1533 | |
| 1534 | if (old_image_info) |
| 1535 | *(manifest.mutable_old_image_info()) = *old_image_info; |
| 1536 | |
| 1537 | if (new_image_info) |
| 1538 | *(manifest.mutable_new_image_info()) = *new_image_info; |
| 1539 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 1540 | OperationNameMap op_name_map; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1541 | CheckGraph(graph); |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 1542 | InstallOperationsToManifest(graph, |
| 1543 | final_order, |
| 1544 | kernel_ops, |
| 1545 | &manifest, |
| 1546 | &op_name_map); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1547 | CheckGraph(graph); |
| 1548 | manifest.set_block_size(kBlockSize); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1549 | |
| 1550 | // Reorder the data blobs with the newly ordered manifest |
| 1551 | string ordered_blobs_path; |
| 1552 | TEST_AND_RETURN_FALSE(utils::MakeTempFile( |
| 1553 | "/tmp/CrAU_temp_data.ordered.XXXXXX", |
| 1554 | &ordered_blobs_path, |
Andrew de los Reyes | e05fc28 | 2011-06-02 09:50:08 -0700 | [diff] [blame] | 1555 | NULL)); |
Darin Petkov | 7438a5c | 2011-08-29 11:56:44 -0700 | [diff] [blame] | 1556 | ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1557 | TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest, |
| 1558 | temp_file_path, |
| 1559 | ordered_blobs_path)); |
Darin Petkov | 7438a5c | 2011-08-29 11:56:44 -0700 | [diff] [blame] | 1560 | temp_file_unlinker.reset(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1561 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 1562 | // Check that install op blobs are in order. |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1563 | uint64_t next_blob_offset = 0; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1564 | { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 1565 | for (int i = 0; i < (manifest.install_operations_size() + |
| 1566 | manifest.kernel_install_operations_size()); i++) { |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1567 | DeltaArchiveManifest_InstallOperation* op = |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 1568 | i < manifest.install_operations_size() ? |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1569 | manifest.mutable_install_operations(i) : |
| 1570 | manifest.mutable_kernel_install_operations( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 1571 | i - manifest.install_operations_size()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1572 | if (op->has_data_offset()) { |
| 1573 | if (op->data_offset() != next_blob_offset) { |
| 1574 | LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != " |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1575 | << next_blob_offset; |
| 1576 | } |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1577 | next_blob_offset += op->data_length(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1578 | } |
| 1579 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1582 | // Signatures appear at the end of the blobs. Note the offset in the |
| 1583 | // manifest |
| 1584 | if (!private_key_path.empty()) { |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1585 | uint64_t signature_blob_length = 0; |
| 1586 | TEST_AND_RETURN_FALSE( |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 1587 | PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path), |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1588 | &signature_blob_length)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 1589 | AddSignatureOp(next_blob_offset, signature_blob_length, &manifest); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 1592 | TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part, |
| 1593 | new_kernel_part, |
| 1594 | old_image, |
| 1595 | new_image, |
| 1596 | &manifest)); |
| 1597 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1598 | // Serialize protobuf |
| 1599 | string serialized_manifest; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1600 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1601 | CheckGraph(graph); |
| 1602 | TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest)); |
| 1603 | CheckGraph(graph); |
| 1604 | |
| 1605 | LOG(INFO) << "Writing final delta file header..."; |
| 1606 | DirectFileWriter writer; |
| 1607 | TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(), |
| 1608 | O_WRONLY | O_CREAT | O_TRUNC, |
| 1609 | 0644) == 0); |
| 1610 | ScopedFileWriterCloser writer_closer(&writer); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1611 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1612 | // Write header |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 1613 | TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic))); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1614 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1615 | // Write version number |
| 1616 | TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber)); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1617 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1618 | // Write protobuf length |
| 1619 | TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, |
| 1620 | serialized_manifest.size())); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1621 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1622 | // Write protobuf |
| 1623 | LOG(INFO) << "Writing final delta file protobuf... " |
| 1624 | << serialized_manifest.size(); |
| 1625 | TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(), |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 1626 | serialized_manifest.size())); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1627 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1628 | // Append the data blobs |
| 1629 | LOG(INFO) << "Writing final delta file data blobs..."; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1630 | int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1631 | ScopedFdCloser blobs_fd_closer(&blobs_fd); |
| 1632 | TEST_AND_RETURN_FALSE(blobs_fd >= 0); |
| 1633 | for (;;) { |
| 1634 | char buf[kBlockSize]; |
| 1635 | ssize_t rc = read(blobs_fd, buf, sizeof(buf)); |
| 1636 | if (0 == rc) { |
| 1637 | // EOF |
| 1638 | break; |
| 1639 | } |
| 1640 | TEST_AND_RETURN_FALSE_ERRNO(rc > 0); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 1641 | TEST_AND_RETURN_FALSE(writer.Write(buf, rc)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1642 | } |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1643 | |
| 1644 | // Write signature blob. |
| 1645 | if (!private_key_path.empty()) { |
| 1646 | LOG(INFO) << "Signing the update..."; |
| 1647 | vector<char> signature_blob; |
Andrew de los Reyes | c24e3f3 | 2011-08-30 15:45:20 -0700 | [diff] [blame] | 1648 | TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload( |
| 1649 | output_path, |
| 1650 | vector<string>(1, private_key_path), |
| 1651 | &signature_blob)); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1652 | TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0], |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 1653 | signature_blob.size())); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1656 | *metadata_size = |
Darin Petkov | 95cf01f | 2010-10-12 14:59:13 -0700 | [diff] [blame] | 1657 | strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size(); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1658 | ReportPayloadUsage(manifest, *metadata_size, op_name_map); |
Darin Petkov | 880335c | 2010-10-01 15:52:53 -0700 | [diff] [blame] | 1659 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1660 | LOG(INFO) << "All done. Successfully created delta file with " |
| 1661 | << "metadata size = " << *metadata_size; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1662 | return true; |
| 1663 | } |
| 1664 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 1665 | // Runs the bsdiff tool on two files and returns the resulting delta in |
| 1666 | // 'out'. Returns true on success. |
| 1667 | bool DeltaDiffGenerator::BsdiffFiles(const string& old_file, |
| 1668 | const string& new_file, |
| 1669 | vector<char>* out) { |
| 1670 | const string kPatchFile = "/tmp/delta.patchXXXXXX"; |
| 1671 | string patch_file_path; |
| 1672 | |
| 1673 | TEST_AND_RETURN_FALSE( |
| 1674 | utils::MakeTempFile(kPatchFile, &patch_file_path, NULL)); |
| 1675 | |
| 1676 | vector<string> cmd; |
| 1677 | cmd.push_back(kBsdiffPath); |
| 1678 | cmd.push_back(old_file); |
| 1679 | cmd.push_back(new_file); |
| 1680 | cmd.push_back(patch_file_path); |
| 1681 | |
| 1682 | int rc = 1; |
| 1683 | vector<char> patch_file; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 1684 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc, NULL)); |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 1685 | TEST_AND_RETURN_FALSE(rc == 0); |
| 1686 | TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out)); |
| 1687 | unlink(patch_file_path.c_str()); |
| 1688 | return true; |
| 1689 | } |
| 1690 | |
| 1691 | // The |blocks| vector contains a reader and writer for each block on the |
| 1692 | // filesystem that's being in-place updated. We populate the reader/writer |
| 1693 | // fields of |blocks| by calling this function. |
| 1694 | // For each block in |operation| that is read or written, find that block |
| 1695 | // in |blocks| and set the reader/writer field to the vertex passed. |
| 1696 | // |graph| is not strictly necessary, but useful for printing out |
| 1697 | // error messages. |
| 1698 | bool DeltaDiffGenerator::AddInstallOpToBlocksVector( |
| 1699 | const DeltaArchiveManifest_InstallOperation& operation, |
| 1700 | const Graph& graph, |
| 1701 | Vertex::Index vertex, |
| 1702 | vector<Block>* blocks) { |
| 1703 | // See if this is already present. |
| 1704 | TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0); |
| 1705 | |
| 1706 | enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT }; |
| 1707 | for (int field = READER; field < BLOCK_FIELD_COUNT; field++) { |
| 1708 | const int extents_size = |
| 1709 | (field == READER) ? operation.src_extents_size() : |
| 1710 | operation.dst_extents_size(); |
| 1711 | const char* past_participle = (field == READER) ? "read" : "written"; |
| 1712 | const google::protobuf::RepeatedPtrField<Extent>& extents = |
| 1713 | (field == READER) ? operation.src_extents() : operation.dst_extents(); |
| 1714 | Vertex::Index Block::*access_type = |
| 1715 | (field == READER) ? &Block::reader : &Block::writer; |
| 1716 | |
| 1717 | for (int i = 0; i < extents_size; i++) { |
| 1718 | const Extent& extent = extents.Get(i); |
| 1719 | if (extent.start_block() == kSparseHole) { |
| 1720 | // Hole in sparse file. skip |
| 1721 | continue; |
| 1722 | } |
| 1723 | for (uint64_t block = extent.start_block(); |
| 1724 | block < (extent.start_block() + extent.num_blocks()); block++) { |
| 1725 | if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) { |
| 1726 | LOG(FATAL) << "Block " << block << " is already " |
| 1727 | << past_participle << " by " |
| 1728 | << (*blocks)[block].*access_type << "(" |
| 1729 | << graph[(*blocks)[block].*access_type].file_name |
| 1730 | << ") and also " << vertex << "(" |
| 1731 | << graph[vertex].file_name << ")"; |
| 1732 | } |
| 1733 | (*blocks)[block].*access_type = vertex; |
| 1734 | } |
| 1735 | } |
| 1736 | } |
| 1737 | return true; |
| 1738 | } |
| 1739 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 1740 | void DeltaDiffGenerator::AddSignatureOp(uint64_t signature_blob_offset, |
| 1741 | uint64_t signature_blob_length, |
| 1742 | DeltaArchiveManifest* manifest) { |
| 1743 | LOG(INFO) << "Making room for signature in file"; |
| 1744 | manifest->set_signatures_offset(signature_blob_offset); |
| 1745 | LOG(INFO) << "set? " << manifest->has_signatures_offset(); |
| 1746 | // Add a dummy op at the end to appease older clients |
| 1747 | DeltaArchiveManifest_InstallOperation* dummy_op = |
| 1748 | manifest->add_kernel_install_operations(); |
| 1749 | dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE); |
| 1750 | dummy_op->set_data_offset(signature_blob_offset); |
| 1751 | manifest->set_signatures_offset(signature_blob_offset); |
| 1752 | dummy_op->set_data_length(signature_blob_length); |
| 1753 | manifest->set_signatures_size(signature_blob_length); |
| 1754 | Extent* dummy_extent = dummy_op->add_dst_extents(); |
| 1755 | // Tell the dummy op to write this data to a big sparse hole |
| 1756 | dummy_extent->set_start_block(kSparseHole); |
| 1757 | dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) / |
| 1758 | kBlockSize); |
| 1759 | } |
| 1760 | |
Andrew de los Reyes | 50f3649 | 2010-11-01 13:57:12 -0700 | [diff] [blame] | 1761 | const char* const kBsdiffPath = "bsdiff"; |
| 1762 | const char* const kBspatchPath = "bspatch"; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1763 | const char* const kDeltaMagic = "CrAU"; |
| 1764 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 1765 | }; // namespace chromeos_update_engine |