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