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