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