adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 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" |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 6 | #include <sys/stat.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <errno.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <algorithm> |
| 11 | #include <set> |
| 12 | #include <string> |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | #include <bzlib.h> |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 16 | #include "base/logging.h" |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 17 | #include "update_engine/bzip.h" |
| 18 | #include "update_engine/cycle_breaker.h" |
| 19 | #include "update_engine/extent_mapper.h" |
| 20 | #include "update_engine/file_writer.h" |
| 21 | #include "update_engine/filesystem_iterator.h" |
| 22 | #include "update_engine/graph_types.h" |
| 23 | #include "update_engine/graph_utils.h" |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 24 | #include "update_engine/payload_signer.h" |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 25 | #include "update_engine/subprocess.h" |
| 26 | #include "update_engine/topological_sort.h" |
| 27 | #include "update_engine/update_metadata.pb.h" |
| 28 | #include "update_engine/utils.h" |
| 29 | |
| 30 | using std::make_pair; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 31 | using std::max; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 32 | using std::min; |
| 33 | using std::set; |
| 34 | using std::string; |
| 35 | using std::vector; |
| 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
| 39 | typedef DeltaDiffGenerator::Block Block; |
| 40 | |
| 41 | namespace { |
| 42 | const size_t kBlockSize = 4096; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 43 | const uint64_t kVersionNumber = 1; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 44 | |
| 45 | // Stores all Extents for a file into 'out'. Returns true on success. |
| 46 | bool GatherExtents(const string& path, |
| 47 | google::protobuf::RepeatedPtrField<Extent>* out) { |
| 48 | vector<Extent> extents; |
| 49 | TEST_AND_RETURN_FALSE(extent_mapper::ExtentsForFileFibmap(path, &extents)); |
| 50 | DeltaDiffGenerator::StoreExtents(extents, out); |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | // Runs the bsdiff tool on two files and returns the resulting delta in |
| 55 | // 'out'. Returns true on success. |
| 56 | bool BsdiffFiles(const string& old_file, |
| 57 | const string& new_file, |
| 58 | vector<char>* out) { |
| 59 | const string kPatchFile = "/tmp/delta.patchXXXXXX"; |
| 60 | string patch_file_path; |
| 61 | |
| 62 | TEST_AND_RETURN_FALSE( |
| 63 | utils::MakeTempFile(kPatchFile, &patch_file_path, NULL)); |
| 64 | |
| 65 | vector<string> cmd; |
| 66 | cmd.push_back(kBsdiffPath); |
| 67 | cmd.push_back(old_file); |
| 68 | cmd.push_back(new_file); |
| 69 | cmd.push_back(patch_file_path); |
| 70 | |
| 71 | int rc = 1; |
| 72 | vector<char> patch_file; |
| 73 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &rc)); |
| 74 | TEST_AND_RETURN_FALSE(rc == 0); |
| 75 | TEST_AND_RETURN_FALSE(utils::ReadFile(patch_file_path, out)); |
| 76 | unlink(patch_file_path.c_str()); |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | // The blocks vector contains a reader and writer for each block on the |
| 81 | // filesystem that's being in-place updated. We populate the reader/writer |
| 82 | // fields of blocks by calling this function. |
| 83 | // For each block in 'operation' that is read or written, find that block |
| 84 | // in 'blocks' and set the reader/writer field to the vertex passed. |
| 85 | // 'graph' is not strictly necessary, but useful for printing out |
| 86 | // error messages. |
| 87 | bool AddInstallOpToBlocksVector( |
| 88 | const DeltaArchiveManifest_InstallOperation& operation, |
| 89 | vector<Block>* blocks, |
| 90 | const Graph& graph, |
| 91 | Vertex::Index vertex) { |
| 92 | LOG(INFO) << "AddInstallOpToBlocksVector(" << vertex << "), " |
| 93 | << graph[vertex].file_name; |
| 94 | // See if this is already present. |
| 95 | TEST_AND_RETURN_FALSE(operation.dst_extents_size() > 0); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 96 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 97 | enum BlockField { READER = 0, WRITER, BLOCK_FIELD_COUNT }; |
| 98 | for (int field = READER; field < BLOCK_FIELD_COUNT; field++) { |
| 99 | const int extents_size = |
| 100 | (field == READER) ? operation.src_extents_size() : |
| 101 | operation.dst_extents_size(); |
| 102 | const char* past_participle = (field == READER) ? "read" : "written"; |
| 103 | const google::protobuf::RepeatedPtrField<Extent>& extents = |
| 104 | (field == READER) ? operation.src_extents() : operation.dst_extents(); |
| 105 | Vertex::Index Block::*access_type = |
| 106 | (field == READER) ? &Block::reader : &Block::writer; |
| 107 | |
| 108 | for (int i = 0; i < extents_size; i++) { |
| 109 | const Extent& extent = extents.Get(i); |
| 110 | if (extent.start_block() == kSparseHole) { |
| 111 | // Hole in sparse file. skip |
| 112 | continue; |
| 113 | } |
| 114 | for (uint64_t block = extent.start_block(); |
| 115 | block < (extent.start_block() + extent.num_blocks()); block++) { |
| 116 | LOG(INFO) << "ext: " << i << " block: " << block; |
| 117 | if ((*blocks)[block].*access_type != Vertex::kInvalidIndex) { |
| 118 | LOG(FATAL) << "Block " << block << " is already " |
| 119 | << past_participle << " by " |
| 120 | << (*blocks)[block].*access_type << "(" |
| 121 | << graph[(*blocks)[block].*access_type].file_name |
| 122 | << ") and also " << vertex << "(" |
| 123 | << graph[vertex].file_name << ")"; |
| 124 | } |
| 125 | (*blocks)[block].*access_type = vertex; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | // For a given regular file which must exist at new_root + path, and may |
| 133 | // exist at old_root + path, creates a new InstallOperation and adds it to |
| 134 | // the graph. Also, populates the 'blocks' array as necessary. |
| 135 | // Also, writes the data necessary to send the file down to the client |
| 136 | // into data_fd, which has length *data_file_size. *data_file_size is |
| 137 | // updated appropriately. |
| 138 | // Returns true on success. |
| 139 | bool DeltaReadFile(Graph* graph, |
| 140 | vector<Block>* blocks, |
| 141 | const string& old_root, |
| 142 | const string& new_root, |
| 143 | const string& path, // within new_root |
| 144 | int data_fd, |
| 145 | off_t* data_file_size) { |
| 146 | vector<char> data; |
| 147 | DeltaArchiveManifest_InstallOperation operation; |
| 148 | |
| 149 | TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ReadFileToDiff(old_root + path, |
| 150 | new_root + path, |
| 151 | &data, |
| 152 | &operation)); |
| 153 | |
| 154 | // Write the data |
| 155 | if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_MOVE) { |
| 156 | operation.set_data_offset(*data_file_size); |
| 157 | operation.set_data_length(data.size()); |
| 158 | } |
| 159 | |
| 160 | TEST_AND_RETURN_FALSE(utils::WriteAll(data_fd, &data[0], data.size())); |
| 161 | *data_file_size += data.size(); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 162 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 163 | // Now, insert into graph and blocks vector |
| 164 | graph->resize(graph->size() + 1); |
| 165 | graph->back().op = operation; |
| 166 | CHECK(graph->back().op.has_type()); |
| 167 | graph->back().file_name = path; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 168 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 169 | TEST_AND_RETURN_FALSE(AddInstallOpToBlocksVector(graph->back().op, |
| 170 | blocks, |
| 171 | *graph, |
| 172 | graph->size() - 1)); |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | // For each regular file within new_root, creates a node in the graph, |
| 177 | // determines the best way to compress it (REPLACE, REPLACE_BZ, COPY, BSDIFF), |
| 178 | // and writes any necessary data to the end of data_fd. |
| 179 | bool DeltaReadFiles(Graph* graph, |
| 180 | vector<Block>* blocks, |
| 181 | const string& old_root, |
| 182 | const string& new_root, |
| 183 | int data_fd, |
| 184 | off_t* data_file_size) { |
| 185 | set<ino_t> visited_inodes; |
| 186 | for (FilesystemIterator fs_iter(new_root, |
| 187 | utils::SetWithValue<string>("/lost+found")); |
| 188 | !fs_iter.IsEnd(); fs_iter.Increment()) { |
| 189 | if (!S_ISREG(fs_iter.GetStat().st_mode)) |
| 190 | continue; |
| 191 | |
| 192 | // Make sure we visit each inode only once. |
| 193 | if (utils::SetContainsKey(visited_inodes, fs_iter.GetStat().st_ino)) |
| 194 | continue; |
| 195 | visited_inodes.insert(fs_iter.GetStat().st_ino); |
| 196 | if (fs_iter.GetStat().st_size == 0) |
| 197 | continue; |
| 198 | |
| 199 | LOG(INFO) << "Encoding file " << fs_iter.GetPartialPath(); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 200 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 201 | TEST_AND_RETURN_FALSE(DeltaReadFile(graph, |
| 202 | blocks, |
| 203 | old_root, |
| 204 | new_root, |
| 205 | fs_iter.GetPartialPath(), |
| 206 | data_fd, |
| 207 | data_file_size)); |
| 208 | } |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | // Attempts to find block_count blocks to use as scratch space. |
| 213 | // Returns true on success. |
| 214 | // Right now we return exactly as many blocks as are required. |
| 215 | // TODO(adlr): consider returning all scratch blocks, |
| 216 | // even if there are extras, to make it easier for a scratch allocator |
| 217 | // to find contiguous regions for specific scratch writes. |
| 218 | bool FindScratchSpace(const vector<Block>& blocks, |
| 219 | vector<Block>::size_type block_count, |
| 220 | vector<Extent>* out) { |
| 221 | // Scan blocks for blocks that are neither read nor written. |
| 222 | // If we don't find enough of those, return false. |
| 223 | // TODO(adlr): return blocks that are written by |
| 224 | // operations that don't have incoming edges (and thus, can be |
| 225 | // deferred until all old blocks are read by other operations). |
| 226 | vector<Extent> ret; |
| 227 | vector<Block>::size_type blocks_found = 0; |
| 228 | for (vector<Block>::size_type i = 0; |
| 229 | i < blocks.size() && blocks_found < block_count; i++) { |
| 230 | if (blocks[i].reader == Vertex::kInvalidIndex && |
| 231 | blocks[i].writer == Vertex::kInvalidIndex) { |
| 232 | graph_utils::AppendBlockToExtents(&ret, i); |
| 233 | blocks_found++; |
| 234 | } |
| 235 | } |
| 236 | if (blocks_found == block_count) { |
| 237 | LOG(INFO) << "returning " << blocks_found << " scratch blocks"; |
| 238 | out->swap(ret); |
| 239 | return true; |
| 240 | } |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | // This class takes a collection of Extents and allows the client to |
| 245 | // allocate space from these extents. The client must not request more |
| 246 | // space then exists in the source extents. Space is allocated from the |
| 247 | // beginning of the source extents on; no consideration is paid to |
| 248 | // fragmentation. |
| 249 | class LinearExtentAllocator { |
| 250 | public: |
| 251 | explicit LinearExtentAllocator(const vector<Extent>& extents) |
| 252 | : extents_(extents), |
| 253 | extent_index_(0), |
| 254 | extent_blocks_allocated_(0) {} |
| 255 | vector<Extent> Allocate(const uint64_t block_count) { |
| 256 | vector<Extent> ret; |
| 257 | for (uint64_t blocks = 0; blocks < block_count; blocks++) { |
| 258 | CHECK_LT(extent_index_, extents_.size()); |
| 259 | CHECK_LT(extent_blocks_allocated_, extents_[extent_index_].num_blocks()); |
| 260 | graph_utils::AppendBlockToExtents( |
| 261 | &ret, |
| 262 | extents_[extent_index_].start_block() + extent_blocks_allocated_); |
| 263 | extent_blocks_allocated_++; |
| 264 | if (extent_blocks_allocated_ >= extents_[extent_index_].num_blocks()) { |
| 265 | extent_blocks_allocated_ = 0; |
| 266 | extent_index_++; |
| 267 | } |
| 268 | } |
| 269 | return ret; |
| 270 | } |
| 271 | private: |
| 272 | const vector<Extent> extents_; |
| 273 | vector<Extent>::size_type extent_index_; // current Extent |
| 274 | // number of blocks allocated from the current extent |
| 275 | uint64_t extent_blocks_allocated_; |
| 276 | }; |
| 277 | |
| 278 | // Reads blocks from image_path that are not yet marked as being written |
| 279 | // in the blocks array. These blocks that remain are non-file-data blocks. |
| 280 | // In the future we might consider intelligent diffing between this data |
| 281 | // and data in the previous image, but for now we just bzip2 compress it |
| 282 | // and include it in the update. |
| 283 | // Creates a new node in the graph to write these blocks and writes the |
| 284 | // appropriate blob to blobs_fd. Reads and updates blobs_length; |
| 285 | bool ReadUnwrittenBlocks(const vector<Block>& blocks, |
| 286 | int blobs_fd, |
| 287 | off_t* blobs_length, |
| 288 | const string& image_path, |
| 289 | DeltaArchiveManifest_InstallOperation* out_op) { |
| 290 | int image_fd = open(image_path.c_str(), O_RDONLY, 000); |
| 291 | TEST_AND_RETURN_FALSE_ERRNO(image_fd >= 0); |
| 292 | ScopedFdCloser image_fd_closer(&image_fd); |
| 293 | |
| 294 | string temp_file_path; |
| 295 | TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/CrAU_temp_data.XXXXXX", |
| 296 | &temp_file_path, |
| 297 | NULL)); |
| 298 | |
| 299 | FILE* file = fopen(temp_file_path.c_str(), "w"); |
| 300 | TEST_AND_RETURN_FALSE(file); |
| 301 | int err = BZ_OK; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 302 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 303 | BZFILE* bz_file = BZ2_bzWriteOpen(&err, |
| 304 | file, |
| 305 | 9, // max compression |
| 306 | 0, // verbosity |
| 307 | 0); // default work factor |
| 308 | TEST_AND_RETURN_FALSE(err == BZ_OK); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 309 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 310 | vector<Extent> extents; |
| 311 | vector<Block>::size_type block_count = 0; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 312 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 313 | LOG(INFO) << "Appending left over blocks to extents"; |
| 314 | for (vector<Block>::size_type i = 0; i < blocks.size(); i++) { |
| 315 | if (blocks[i].writer != Vertex::kInvalidIndex) |
| 316 | continue; |
| 317 | graph_utils::AppendBlockToExtents(&extents, i); |
| 318 | block_count++; |
| 319 | } |
| 320 | |
| 321 | // Code will handle 'buf' at any size that's a multiple of kBlockSize, |
| 322 | // so we arbitrarily set it to 1024 * kBlockSize. |
| 323 | vector<char> buf(1024 * kBlockSize); |
| 324 | |
| 325 | LOG(INFO) << "Reading left over blocks"; |
| 326 | vector<Block>::size_type blocks_copied_count = 0; |
| 327 | |
| 328 | // For each extent in extents, write the data into BZ2_bzWrite which |
| 329 | // sends it to an output file. |
| 330 | // We use the temporary buffer 'buf' to hold the data, which may be |
| 331 | // smaller than the extent, so in that case we have to loop to get |
| 332 | // the extent's data (that's the inner while loop). |
| 333 | for (vector<Extent>::const_iterator it = extents.begin(); |
| 334 | it != extents.end(); ++it) { |
| 335 | vector<Block>::size_type blocks_read = 0; |
| 336 | while (blocks_read < it->num_blocks()) { |
| 337 | const int copy_block_cnt = |
| 338 | min(buf.size() / kBlockSize, |
| 339 | static_cast<vector<char>::size_type>( |
| 340 | it->num_blocks() - blocks_read)); |
| 341 | ssize_t rc = pread(image_fd, |
| 342 | &buf[0], |
| 343 | copy_block_cnt * kBlockSize, |
| 344 | (it->start_block() + blocks_read) * kBlockSize); |
| 345 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 346 | TEST_AND_RETURN_FALSE(static_cast<size_t>(rc) == |
| 347 | copy_block_cnt * kBlockSize); |
| 348 | BZ2_bzWrite(&err, bz_file, &buf[0], copy_block_cnt * kBlockSize); |
| 349 | TEST_AND_RETURN_FALSE(err == BZ_OK); |
| 350 | blocks_read += copy_block_cnt; |
| 351 | blocks_copied_count += copy_block_cnt; |
| 352 | LOG(INFO) << "progress: " << ((float)blocks_copied_count)/block_count; |
| 353 | } |
| 354 | } |
| 355 | BZ2_bzWriteClose(&err, bz_file, 0, NULL, NULL); |
| 356 | TEST_AND_RETURN_FALSE(err == BZ_OK); |
| 357 | bz_file = NULL; |
| 358 | TEST_AND_RETURN_FALSE_ERRNO(0 == fclose(file)); |
| 359 | file = NULL; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 360 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 361 | vector<char> compressed_data; |
| 362 | LOG(INFO) << "Reading compressed data off disk"; |
| 363 | TEST_AND_RETURN_FALSE(utils::ReadFile(temp_file_path, &compressed_data)); |
| 364 | 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] | 365 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 366 | // Add node to graph to write these blocks |
| 367 | out_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ); |
| 368 | out_op->set_data_offset(*blobs_length); |
| 369 | out_op->set_data_length(compressed_data.size()); |
| 370 | *blobs_length += compressed_data.size(); |
| 371 | out_op->set_dst_length(kBlockSize * block_count); |
| 372 | DeltaDiffGenerator::StoreExtents(extents, out_op->mutable_dst_extents()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 373 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 374 | TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, |
| 375 | &compressed_data[0], |
| 376 | compressed_data.size())); |
| 377 | LOG(INFO) << "done with extra blocks"; |
| 378 | return true; |
| 379 | } |
| 380 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 381 | // 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] | 382 | // Returns true on success. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 383 | bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) { |
| 384 | uint64_t value_be = htobe64(value); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 385 | TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)) == |
| 386 | sizeof(value_be)); |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | // Adds each operation from the graph to the manifest in the order |
| 391 | // specified by 'order'. |
| 392 | void InstallOperationsToManifest( |
| 393 | const Graph& graph, |
| 394 | const vector<Vertex::Index>& order, |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 395 | const vector<DeltaArchiveManifest_InstallOperation>& kernel_ops, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 396 | DeltaArchiveManifest* out_manifest) { |
| 397 | for (vector<Vertex::Index>::const_iterator it = order.begin(); |
| 398 | it != order.end(); ++it) { |
| 399 | DeltaArchiveManifest_InstallOperation* op = |
| 400 | out_manifest->add_install_operations(); |
| 401 | *op = graph[*it].op; |
| 402 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 403 | for (vector<DeltaArchiveManifest_InstallOperation>::const_iterator it = |
| 404 | kernel_ops.begin(); it != kernel_ops.end(); ++it) { |
| 405 | DeltaArchiveManifest_InstallOperation* op = |
| 406 | out_manifest->add_kernel_install_operations(); |
| 407 | *op = *it; |
| 408 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void CheckGraph(const Graph& graph) { |
| 412 | for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) { |
| 413 | CHECK(it->op.has_type()); |
| 414 | } |
| 415 | } |
| 416 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 417 | // Delta compresses a kernel partition new_kernel_part with knowledge of |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 418 | // the old kernel partition old_kernel_part. |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 419 | bool DeltaCompressKernelPartition( |
| 420 | const string& old_kernel_part, |
| 421 | const string& new_kernel_part, |
| 422 | vector<DeltaArchiveManifest_InstallOperation>* ops, |
| 423 | int blobs_fd, |
| 424 | off_t* blobs_length) { |
| 425 | // For now, just bsdiff the kernel partition as a whole. |
| 426 | // TODO(adlr): Use knowledge of how the kernel partition is laid out |
| 427 | // to more efficiently compress it. |
| 428 | |
| 429 | LOG(INFO) << "Delta compressing kernel partition..."; |
| 430 | |
| 431 | // Add a new install operation |
| 432 | ops->resize(1); |
| 433 | DeltaArchiveManifest_InstallOperation* op = &(*ops)[0]; |
Andrew de los Reyes | 36f3736 | 2010-09-03 09:20:04 -0700 | [diff] [blame^] | 434 | op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 435 | op->set_data_offset(*blobs_length); |
| 436 | |
| 437 | // Do the actual compression |
| 438 | vector<char> data; |
Andrew de los Reyes | 36f3736 | 2010-09-03 09:20:04 -0700 | [diff] [blame^] | 439 | TEST_AND_RETURN_FALSE(utils::ReadFile(new_kernel_part, &data)); |
| 440 | TEST_AND_RETURN_FALSE(!data.empty()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 441 | |
Andrew de los Reyes | 36f3736 | 2010-09-03 09:20:04 -0700 | [diff] [blame^] | 442 | vector<char> data_bz; |
| 443 | TEST_AND_RETURN_FALSE(BzipCompress(data, &data_bz)); |
| 444 | CHECK(!data_bz.empty()); |
| 445 | |
| 446 | TEST_AND_RETURN_FALSE(utils::WriteAll(blobs_fd, &data_bz[0], data_bz.size())); |
| 447 | *blobs_length += data_bz.size(); |
| 448 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 449 | off_t new_part_size = utils::FileSize(new_kernel_part); |
| 450 | TEST_AND_RETURN_FALSE(new_part_size >= 0); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 451 | |
Andrew de los Reyes | 36f3736 | 2010-09-03 09:20:04 -0700 | [diff] [blame^] | 452 | op->set_data_length(data_bz.size()); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 453 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 454 | op->set_dst_length(new_part_size); |
| 455 | |
Andrew de los Reyes | 36f3736 | 2010-09-03 09:20:04 -0700 | [diff] [blame^] | 456 | // Theres a single dest extent |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 457 | Extent* dst_extent = op->add_dst_extents(); |
| 458 | dst_extent->set_start_block(0); |
| 459 | dst_extent->set_num_blocks((new_part_size + kBlockSize - 1) / kBlockSize); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 460 | |
Andrew de los Reyes | 36f3736 | 2010-09-03 09:20:04 -0700 | [diff] [blame^] | 461 | LOG(INFO) << "Done compressing kernel partition."; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 462 | return true; |
| 463 | } |
| 464 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 465 | } // namespace {} |
| 466 | |
| 467 | bool DeltaDiffGenerator::ReadFileToDiff( |
| 468 | const string& old_filename, |
| 469 | const string& new_filename, |
| 470 | vector<char>* out_data, |
| 471 | DeltaArchiveManifest_InstallOperation* out_op) { |
| 472 | // Read new data in |
| 473 | vector<char> new_data; |
| 474 | TEST_AND_RETURN_FALSE(utils::ReadFile(new_filename, &new_data)); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 475 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 476 | TEST_AND_RETURN_FALSE(!new_data.empty()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 477 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 478 | vector<char> new_data_bz; |
| 479 | TEST_AND_RETURN_FALSE(BzipCompress(new_data, &new_data_bz)); |
| 480 | CHECK(!new_data_bz.empty()); |
| 481 | |
| 482 | vector<char> data; // Data blob that will be written to delta file. |
| 483 | |
| 484 | DeltaArchiveManifest_InstallOperation operation; |
| 485 | size_t current_best_size = 0; |
| 486 | if (new_data.size() <= new_data_bz.size()) { |
| 487 | operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE); |
| 488 | current_best_size = new_data.size(); |
| 489 | data = new_data; |
| 490 | } else { |
| 491 | operation.set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ); |
| 492 | current_best_size = new_data_bz.size(); |
| 493 | data = new_data_bz; |
| 494 | } |
| 495 | |
| 496 | // Do we have an original file to consider? |
| 497 | struct stat old_stbuf; |
| 498 | if (0 != stat(old_filename.c_str(), &old_stbuf)) { |
| 499 | // If stat-ing the old file fails, it should be because it doesn't exist. |
| 500 | TEST_AND_RETURN_FALSE(errno == ENOTDIR || errno == ENOENT); |
| 501 | } else { |
| 502 | // Read old data |
| 503 | vector<char> old_data; |
| 504 | TEST_AND_RETURN_FALSE(utils::ReadFile(old_filename, &old_data)); |
| 505 | if (old_data == new_data) { |
| 506 | // No change in data. |
| 507 | operation.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE); |
| 508 | current_best_size = 0; |
| 509 | data.clear(); |
| 510 | } else { |
| 511 | // Try bsdiff of old to new data |
| 512 | vector<char> bsdiff_delta; |
| 513 | TEST_AND_RETURN_FALSE( |
| 514 | BsdiffFiles(old_filename, new_filename, &bsdiff_delta)); |
| 515 | CHECK_GT(bsdiff_delta.size(), 0); |
| 516 | if (bsdiff_delta.size() < current_best_size) { |
| 517 | operation.set_type(DeltaArchiveManifest_InstallOperation_Type_BSDIFF); |
| 518 | current_best_size = bsdiff_delta.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 | data = bsdiff_delta; |
| 521 | } |
| 522 | } |
| 523 | } |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 524 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 525 | // Set parameters of the operations |
| 526 | CHECK_EQ(data.size(), current_best_size); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 527 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 528 | if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE || |
| 529 | operation.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) { |
| 530 | TEST_AND_RETURN_FALSE( |
| 531 | GatherExtents(old_filename, operation.mutable_src_extents())); |
| 532 | operation.set_src_length(old_stbuf.st_size); |
| 533 | } |
| 534 | |
| 535 | TEST_AND_RETURN_FALSE( |
| 536 | GatherExtents(new_filename, operation.mutable_dst_extents())); |
| 537 | operation.set_dst_length(new_data.size()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 538 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 539 | out_data->swap(data); |
| 540 | *out_op = operation; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 541 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 542 | return true; |
| 543 | } |
| 544 | |
| 545 | void DeltaDiffGenerator::SubstituteBlocks( |
| 546 | DeltaArchiveManifest_InstallOperation* op, |
| 547 | const vector<Extent>& remove_extents, |
| 548 | const vector<Extent>& replace_extents) { |
| 549 | // First, expand out the blocks that op reads from |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 550 | vector<uint64_t> read_blocks; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 551 | for (int i = 0; i < op->src_extents_size(); i++) { |
| 552 | const Extent& extent = op->src_extents(i); |
| 553 | if (extent.start_block() == kSparseHole) { |
| 554 | read_blocks.resize(read_blocks.size() + extent.num_blocks(), kSparseHole); |
| 555 | } else { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 556 | for (uint64_t block = extent.start_block(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 557 | block < (extent.start_block() + extent.num_blocks()); block++) { |
| 558 | read_blocks.push_back(block); |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | { |
| 563 | // Expand remove_extents and replace_extents |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 564 | vector<uint64_t> remove_extents_expanded; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 565 | for (vector<Extent>::const_iterator it = remove_extents.begin(); |
| 566 | it != remove_extents.end(); ++it) { |
| 567 | const Extent& extent = *it; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 568 | for (uint64_t block = extent.start_block(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 569 | block < (extent.start_block() + extent.num_blocks()); block++) { |
| 570 | remove_extents_expanded.push_back(block); |
| 571 | } |
| 572 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 573 | vector<uint64_t> replace_extents_expanded; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 574 | for (vector<Extent>::const_iterator it = replace_extents.begin(); |
| 575 | it != replace_extents.end(); ++it) { |
| 576 | const Extent& extent = *it; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 577 | for (uint64_t block = extent.start_block(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 578 | block < (extent.start_block() + extent.num_blocks()); block++) { |
| 579 | replace_extents_expanded.push_back(block); |
| 580 | } |
| 581 | } |
| 582 | CHECK_EQ(remove_extents_expanded.size(), replace_extents_expanded.size()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 583 | for (vector<uint64_t>::size_type i = 0; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 584 | i < replace_extents_expanded.size(); i++) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 585 | vector<uint64_t>::size_type index = 0; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 586 | CHECK(utils::VectorIndexOf(read_blocks, |
| 587 | remove_extents_expanded[i], |
| 588 | &index)); |
| 589 | CHECK(read_blocks[index] == remove_extents_expanded[i]); |
| 590 | read_blocks[index] = replace_extents_expanded[i]; |
| 591 | } |
| 592 | } |
| 593 | // Convert read_blocks back to extents |
| 594 | op->clear_src_extents(); |
| 595 | vector<Extent> new_extents; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 596 | for (vector<uint64_t>::const_iterator it = read_blocks.begin(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 597 | it != read_blocks.end(); ++it) { |
| 598 | graph_utils::AppendBlockToExtents(&new_extents, *it); |
| 599 | } |
| 600 | DeltaDiffGenerator::StoreExtents(new_extents, op->mutable_src_extents()); |
| 601 | } |
| 602 | |
| 603 | bool DeltaDiffGenerator::CutEdges(Graph* graph, |
| 604 | const vector<Block>& blocks, |
| 605 | const set<Edge>& edges) { |
| 606 | // First, find enough scratch space for the edges we'll be cutting. |
| 607 | vector<Block>::size_type blocks_required = 0; |
| 608 | for (set<Edge>::const_iterator it = edges.begin(); it != edges.end(); ++it) { |
| 609 | blocks_required += graph_utils::EdgeWeight(*graph, *it); |
| 610 | } |
| 611 | vector<Extent> scratch_extents; |
| 612 | LOG(INFO) << "requesting " << blocks_required << " blocks of scratch"; |
| 613 | TEST_AND_RETURN_FALSE( |
| 614 | FindScratchSpace(blocks, blocks_required, &scratch_extents)); |
| 615 | LinearExtentAllocator scratch_allocator(scratch_extents); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 616 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 617 | uint64_t scratch_blocks_used = 0; |
| 618 | for (set<Edge>::const_iterator it = edges.begin(); |
| 619 | it != edges.end(); ++it) { |
| 620 | vector<Extent> old_extents = |
| 621 | (*graph)[it->first].out_edges[it->second].extents; |
| 622 | // Choose some scratch space |
| 623 | scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it); |
| 624 | LOG(INFO) << "using " << graph_utils::EdgeWeight(*graph, *it) |
| 625 | << " scratch blocks (" |
| 626 | << scratch_blocks_used << ")"; |
| 627 | vector<Extent> scratch = |
| 628 | scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it)); |
| 629 | // create vertex to copy original->scratch |
| 630 | graph->resize(graph->size() + 1); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 631 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 632 | // make node depend on the copy operation |
| 633 | (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1, |
| 634 | EdgeProperties())); |
| 635 | |
| 636 | // Set src/dst extents and other proto variables for copy operation |
| 637 | graph->back().op.set_type(DeltaArchiveManifest_InstallOperation_Type_MOVE); |
| 638 | DeltaDiffGenerator::StoreExtents( |
| 639 | (*graph)[it->first].out_edges[it->second].extents, |
| 640 | graph->back().op.mutable_src_extents()); |
| 641 | DeltaDiffGenerator::StoreExtents(scratch, |
| 642 | graph->back().op.mutable_dst_extents()); |
| 643 | graph->back().op.set_src_length( |
| 644 | graph_utils::EdgeWeight(*graph, *it) * kBlockSize); |
| 645 | graph->back().op.set_dst_length(graph->back().op.src_length()); |
| 646 | |
| 647 | // make the dest node read from the scratch space |
| 648 | DeltaDiffGenerator::SubstituteBlocks( |
| 649 | &((*graph)[it->second].op), |
| 650 | (*graph)[it->first].out_edges[it->second].extents, |
| 651 | scratch); |
| 652 | |
| 653 | // delete the old edge |
| 654 | CHECK_EQ(1, (*graph)[it->first].out_edges.erase(it->second)); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 655 | |
Andrew de los Reyes | d12784c | 2010-07-26 13:55:14 -0700 | [diff] [blame] | 656 | // Add an edge from dst to copy operation |
| 657 | (*graph)[it->second].out_edges.insert(make_pair(graph->size() - 1, |
| 658 | EdgeProperties())); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 659 | } |
| 660 | return true; |
| 661 | } |
| 662 | |
| 663 | // Stores all Extents in 'extents' into 'out'. |
| 664 | void DeltaDiffGenerator::StoreExtents( |
| 665 | vector<Extent>& extents, |
| 666 | google::protobuf::RepeatedPtrField<Extent>* out) { |
| 667 | for (vector<Extent>::const_iterator it = extents.begin(); |
| 668 | it != extents.end(); ++it) { |
| 669 | Extent* new_extent = out->Add(); |
| 670 | *new_extent = *it; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | // Creates all the edges for the graph. Writers of a block point to |
| 675 | // readers of the same block. This is because for an edge A->B, B |
| 676 | // must complete before A executes. |
| 677 | void DeltaDiffGenerator::CreateEdges(Graph* graph, |
| 678 | const vector<Block>& blocks) { |
| 679 | for (vector<Block>::size_type i = 0; i < blocks.size(); i++) { |
| 680 | // Blocks with both a reader and writer get an edge |
| 681 | if (blocks[i].reader == Vertex::kInvalidIndex || |
| 682 | blocks[i].writer == Vertex::kInvalidIndex) |
| 683 | continue; |
| 684 | // Don't have a node depend on itself |
| 685 | if (blocks[i].reader == blocks[i].writer) |
| 686 | continue; |
| 687 | // See if there's already an edge we can add onto |
| 688 | Vertex::EdgeMap::iterator edge_it = |
| 689 | (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader); |
| 690 | if (edge_it == (*graph)[blocks[i].writer].out_edges.end()) { |
| 691 | // No existing edge. Create one |
| 692 | (*graph)[blocks[i].writer].out_edges.insert( |
| 693 | make_pair(blocks[i].reader, EdgeProperties())); |
| 694 | edge_it = (*graph)[blocks[i].writer].out_edges.find(blocks[i].reader); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 695 | CHECK(edge_it != (*graph)[blocks[i].writer].out_edges.end()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 696 | } |
| 697 | graph_utils::AppendBlockToExtents(&edge_it->second.extents, i); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | bool DeltaDiffGenerator::ReorderDataBlobs( |
| 702 | DeltaArchiveManifest* manifest, |
| 703 | const std::string& data_blobs_path, |
| 704 | const std::string& new_data_blobs_path) { |
| 705 | int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0); |
| 706 | TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0); |
| 707 | ScopedFdCloser in_fd_closer(&in_fd); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 708 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 709 | DirectFileWriter writer; |
| 710 | TEST_AND_RETURN_FALSE( |
| 711 | writer.Open(new_data_blobs_path.c_str(), |
| 712 | O_WRONLY | O_TRUNC | O_CREAT, |
| 713 | 0644) == 0); |
| 714 | ScopedFileWriterCloser writer_closer(&writer); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 715 | uint64_t out_file_size = 0; |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 716 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 717 | for (int i = 0; i < (manifest->install_operations_size() + |
| 718 | manifest->kernel_install_operations_size()); i++) { |
| 719 | DeltaArchiveManifest_InstallOperation* op = NULL; |
| 720 | if (i < manifest->install_operations_size()) { |
| 721 | op = manifest->mutable_install_operations(i); |
| 722 | } else { |
| 723 | op = manifest->mutable_kernel_install_operations( |
| 724 | i - manifest->install_operations_size()); |
| 725 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 726 | if (!op->has_data_offset()) |
| 727 | continue; |
| 728 | CHECK(op->has_data_length()); |
| 729 | vector<char> buf(op->data_length()); |
| 730 | ssize_t rc = pread(in_fd, &buf[0], buf.size(), op->data_offset()); |
| 731 | TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size())); |
| 732 | |
| 733 | op->set_data_offset(out_file_size); |
| 734 | TEST_AND_RETURN_FALSE(writer.Write(&buf[0], buf.size()) == |
| 735 | static_cast<ssize_t>(buf.size())); |
| 736 | out_file_size += buf.size(); |
| 737 | } |
| 738 | return true; |
| 739 | } |
| 740 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 741 | bool DeltaDiffGenerator::GenerateDeltaUpdateFile( |
| 742 | const string& old_root, |
| 743 | const string& old_image, |
| 744 | const string& new_root, |
| 745 | const string& new_image, |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 746 | const string& old_kernel_part, |
| 747 | const string& new_kernel_part, |
| 748 | const string& output_path, |
| 749 | const string& private_key_path) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 750 | struct stat old_image_stbuf; |
| 751 | TEST_AND_RETURN_FALSE_ERRNO(stat(old_image.c_str(), &old_image_stbuf) == 0); |
| 752 | struct stat new_image_stbuf; |
| 753 | TEST_AND_RETURN_FALSE_ERRNO(stat(new_image.c_str(), &new_image_stbuf) == 0); |
| 754 | LOG_IF(WARNING, new_image_stbuf.st_size != old_image_stbuf.st_size) |
| 755 | << "Old and new images are different sizes."; |
| 756 | LOG_IF(FATAL, new_image_stbuf.st_size % kBlockSize) |
| 757 | << "New image not a multiple of block size " << kBlockSize; |
| 758 | LOG_IF(FATAL, old_image_stbuf.st_size % kBlockSize) |
| 759 | << "Old image not a multiple of block size " << kBlockSize; |
| 760 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 761 | // Sanity check kernel partition args |
| 762 | TEST_AND_RETURN_FALSE(utils::FileSize(old_kernel_part) >= 0); |
| 763 | TEST_AND_RETURN_FALSE(utils::FileSize(new_kernel_part) >= 0); |
| 764 | |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 765 | vector<Block> blocks(max(old_image_stbuf.st_size / kBlockSize, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 766 | new_image_stbuf.st_size / kBlockSize)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 767 | LOG(INFO) << "invalid: " << Vertex::kInvalidIndex; |
| 768 | LOG(INFO) << "len: " << blocks.size(); |
| 769 | for (vector<Block>::size_type i = 0; i < blocks.size(); i++) { |
| 770 | CHECK(blocks[i].reader == Vertex::kInvalidIndex); |
| 771 | CHECK(blocks[i].writer == Vertex::kInvalidIndex); |
| 772 | } |
| 773 | Graph graph; |
| 774 | CheckGraph(graph); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 775 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 776 | const string kTempFileTemplate("/tmp/CrAU_temp_data.XXXXXX"); |
| 777 | string temp_file_path; |
| 778 | off_t data_file_size = 0; |
| 779 | |
| 780 | LOG(INFO) << "Reading files..."; |
| 781 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 782 | vector<DeltaArchiveManifest_InstallOperation> kernel_ops; |
| 783 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 784 | DeltaArchiveManifest_InstallOperation final_op; |
| 785 | { |
| 786 | int fd; |
| 787 | TEST_AND_RETURN_FALSE( |
| 788 | utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &fd)); |
| 789 | TEST_AND_RETURN_FALSE(fd >= 0); |
| 790 | ScopedFdCloser fd_closer(&fd); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 791 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 792 | TEST_AND_RETURN_FALSE(DeltaReadFiles(&graph, |
| 793 | &blocks, |
| 794 | old_root, |
| 795 | new_root, |
| 796 | fd, |
| 797 | &data_file_size)); |
| 798 | CheckGraph(graph); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 799 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 800 | TEST_AND_RETURN_FALSE(ReadUnwrittenBlocks(blocks, |
| 801 | fd, |
| 802 | &data_file_size, |
| 803 | new_image, |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 804 | &final_op)); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 805 | |
| 806 | // Read kernel partition |
| 807 | TEST_AND_RETURN_FALSE(DeltaCompressKernelPartition(old_kernel_part, |
| 808 | new_kernel_part, |
| 809 | &kernel_ops, |
| 810 | fd, |
| 811 | &data_file_size)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 812 | } |
| 813 | CheckGraph(graph); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 814 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 815 | LOG(INFO) << "Creating edges..."; |
| 816 | CreateEdges(&graph, blocks); |
| 817 | CheckGraph(graph); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 818 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 819 | CycleBreaker cycle_breaker; |
| 820 | LOG(INFO) << "Finding cycles..."; |
| 821 | set<Edge> cut_edges; |
| 822 | cycle_breaker.BreakCycles(graph, &cut_edges); |
| 823 | CheckGraph(graph); |
| 824 | |
| 825 | // Calculate number of scratch blocks needed |
| 826 | |
| 827 | LOG(INFO) << "Cutting cycles..."; |
| 828 | TEST_AND_RETURN_FALSE(CutEdges(&graph, blocks, cut_edges)); |
| 829 | CheckGraph(graph); |
| 830 | |
| 831 | vector<Vertex::Index> final_order; |
| 832 | LOG(INFO) << "Ordering..."; |
| 833 | TopologicalSort(graph, &final_order); |
| 834 | CheckGraph(graph); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 835 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 836 | // Convert to protobuf Manifest object |
| 837 | DeltaArchiveManifest manifest; |
| 838 | CheckGraph(graph); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 839 | InstallOperationsToManifest(graph, final_order, kernel_ops, &manifest); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 840 | { |
| 841 | // Write final operation |
| 842 | DeltaArchiveManifest_InstallOperation* op = |
| 843 | manifest.add_install_operations(); |
| 844 | *op = final_op; |
| 845 | CHECK(op->has_type()); |
| 846 | LOG(INFO) << "final op length: " << op->data_length(); |
| 847 | } |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 848 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 849 | CheckGraph(graph); |
| 850 | manifest.set_block_size(kBlockSize); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 851 | |
| 852 | // Reorder the data blobs with the newly ordered manifest |
| 853 | string ordered_blobs_path; |
| 854 | TEST_AND_RETURN_FALSE(utils::MakeTempFile( |
| 855 | "/tmp/CrAU_temp_data.ordered.XXXXXX", |
| 856 | &ordered_blobs_path, |
| 857 | false)); |
| 858 | TEST_AND_RETURN_FALSE(ReorderDataBlobs(&manifest, |
| 859 | temp_file_path, |
| 860 | ordered_blobs_path)); |
| 861 | |
| 862 | // Check that install op blobs are in order and that all blocks are written. |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 863 | uint64_t next_blob_offset = 0; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 864 | { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 865 | vector<uint32_t> written_count(blocks.size(), 0); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 866 | for (int i = 0; i < (manifest.install_operations_size() + |
| 867 | manifest.kernel_install_operations_size()); i++) { |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 868 | DeltaArchiveManifest_InstallOperation* op = |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 869 | i < manifest.install_operations_size() ? |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 870 | manifest.mutable_install_operations(i) : |
| 871 | manifest.mutable_kernel_install_operations( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 872 | i - manifest.install_operations_size()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 873 | for (int j = 0; j < op->dst_extents_size(); j++) { |
| 874 | const Extent& extent = op->dst_extents(j); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 875 | for (uint64_t block = extent.start_block(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 876 | block < (extent.start_block() + extent.num_blocks()); block++) { |
| 877 | written_count[block]++; |
| 878 | } |
| 879 | } |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 880 | if (op->has_data_offset()) { |
| 881 | if (op->data_offset() != next_blob_offset) { |
| 882 | LOG(FATAL) << "bad blob offset! " << op->data_offset() << " != " |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 883 | << next_blob_offset; |
| 884 | } |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 885 | next_blob_offset += op->data_length(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | // check all blocks written to |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 889 | for (vector<uint32_t>::size_type i = 0; i < written_count.size(); i++) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 890 | if (written_count[i] == 0) { |
| 891 | LOG(FATAL) << "block " << i << " not written!"; |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 896 | // Signatures appear at the end of the blobs. Note the offset in the |
| 897 | // manifest |
| 898 | if (!private_key_path.empty()) { |
| 899 | LOG(INFO) << "Making room for signature in file"; |
| 900 | manifest.set_signatures_offset(next_blob_offset); |
| 901 | LOG(INFO) << "set? " << manifest.has_signatures_offset(); |
| 902 | // Add a dummy op at the end to appease older clients |
| 903 | DeltaArchiveManifest_InstallOperation* dummy_op = |
| 904 | manifest.add_kernel_install_operations(); |
| 905 | dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE); |
| 906 | dummy_op->set_data_offset(next_blob_offset); |
| 907 | manifest.set_signatures_offset(next_blob_offset); |
| 908 | uint64_t signature_blob_length = 0; |
| 909 | TEST_AND_RETURN_FALSE( |
| 910 | PayloadSigner::SignatureBlobLength(private_key_path, |
| 911 | &signature_blob_length)); |
| 912 | dummy_op->set_data_length(signature_blob_length); |
| 913 | manifest.set_signatures_size(signature_blob_length); |
| 914 | Extent* dummy_extent = dummy_op->add_dst_extents(); |
| 915 | // Tell the dummy op to write this data to a big sparse hole |
| 916 | dummy_extent->set_start_block(kSparseHole); |
| 917 | dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) / |
| 918 | kBlockSize); |
| 919 | } |
| 920 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 921 | // Serialize protobuf |
| 922 | string serialized_manifest; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 923 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 924 | CheckGraph(graph); |
| 925 | TEST_AND_RETURN_FALSE(manifest.AppendToString(&serialized_manifest)); |
| 926 | CheckGraph(graph); |
| 927 | |
| 928 | LOG(INFO) << "Writing final delta file header..."; |
| 929 | DirectFileWriter writer; |
| 930 | TEST_AND_RETURN_FALSE_ERRNO(writer.Open(output_path.c_str(), |
| 931 | O_WRONLY | O_CREAT | O_TRUNC, |
| 932 | 0644) == 0); |
| 933 | ScopedFileWriterCloser writer_closer(&writer); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 934 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 935 | // Write header |
| 936 | TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic)) == |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 937 | static_cast<ssize_t>(strlen(kDeltaMagic))); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 938 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 939 | // Write version number |
| 940 | TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, kVersionNumber)); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 941 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 942 | // Write protobuf length |
| 943 | TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, |
| 944 | serialized_manifest.size())); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 945 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 946 | // Write protobuf |
| 947 | LOG(INFO) << "Writing final delta file protobuf... " |
| 948 | << serialized_manifest.size(); |
| 949 | TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(), |
| 950 | serialized_manifest.size()) == |
| 951 | static_cast<ssize_t>(serialized_manifest.size())); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 952 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 953 | // Append the data blobs |
| 954 | LOG(INFO) << "Writing final delta file data blobs..."; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 955 | 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] | 956 | ScopedFdCloser blobs_fd_closer(&blobs_fd); |
| 957 | TEST_AND_RETURN_FALSE(blobs_fd >= 0); |
| 958 | for (;;) { |
| 959 | char buf[kBlockSize]; |
| 960 | ssize_t rc = read(blobs_fd, buf, sizeof(buf)); |
| 961 | if (0 == rc) { |
| 962 | // EOF |
| 963 | break; |
| 964 | } |
| 965 | TEST_AND_RETURN_FALSE_ERRNO(rc > 0); |
| 966 | TEST_AND_RETURN_FALSE(writer.Write(buf, rc) == rc); |
| 967 | } |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 968 | |
| 969 | // Write signature blob. |
| 970 | if (!private_key_path.empty()) { |
| 971 | LOG(INFO) << "Signing the update..."; |
| 972 | vector<char> signature_blob; |
| 973 | TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(output_path, |
| 974 | private_key_path, |
| 975 | &signature_blob)); |
| 976 | TEST_AND_RETURN_FALSE(writer.Write(&signature_blob[0], |
| 977 | signature_blob.size()) == |
| 978 | static_cast<ssize_t>(signature_blob.size())); |
| 979 | } |
| 980 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 981 | LOG(INFO) << "All done. Successfully created delta file."; |
| 982 | return true; |
| 983 | } |
| 984 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 985 | const char* const kBsdiffPath = "/usr/bin/bsdiff"; |
| 986 | const char* const kBspatchPath = "/usr/bin/bspatch"; |
| 987 | const char* const kDeltaMagic = "CrAU"; |
| 988 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 989 | }; // namespace chromeos_update_engine |