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