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