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