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