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