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