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