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