The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright 2003-2005 Colin Percival |
| 3 | * All rights reserved |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted providing that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 22 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 23 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | * POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #if 0 |
| 28 | __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:06 cperciva Exp $"); |
| 29 | #endif |
| 30 | |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 31 | #include "bsdiff/bspatch.h" |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 32 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 33 | #include <errno.h> |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 34 | #include <fcntl.h> |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 35 | #include <inttypes.h> |
Alex Deymo | ea72d9f | 2017-10-26 13:15:13 +0200 | [diff] [blame] | 36 | #include <stdio.h> |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 39 | #include <sys/stat.h> |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 40 | #include <sys/types.h> |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 41 | #include <unistd.h> |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 42 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 43 | #include <algorithm> |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 44 | #include <memory> |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 45 | #include <vector> |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 46 | |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 47 | #include "bsdiff/buffer_file.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 48 | #include "bsdiff/control_entry.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 49 | #include "bsdiff/extents.h" |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 50 | #include "bsdiff/extents_file.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 51 | #include "bsdiff/file.h" |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 52 | #include "bsdiff/file_interface.h" |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 53 | #include "bsdiff/logging.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 54 | #include "bsdiff/memory_file.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 55 | #include "bsdiff/patch_reader.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 56 | #include "bsdiff/sink_file.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 57 | #include "bsdiff/utils.h" |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 58 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 59 | namespace { |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 60 | // Read the data in |stream| and write |size| decompressed data to |file|; |
| 61 | // using the buffer in |buf| with size |buf_size|. |
| 62 | // Returns 0 on success, 1 on I/O error and 2 on data error. |
| 63 | int ReadStreamAndWriteAll( |
| 64 | const std::unique_ptr<bsdiff::FileInterface>& file, |
| 65 | size_t size, |
| 66 | uint8_t* buf, |
| 67 | size_t buf_size, |
| 68 | const std::function<bool(uint8_t*, size_t)>& read_func) { |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 69 | while (size > 0) { |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 70 | size_t bytes_to_output = std::min(size, buf_size); |
| 71 | if (!read_func(buf, bytes_to_output)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 72 | LOG(ERROR) << "Failed to read stream."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 73 | return 2; |
| 74 | } |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 75 | |
| 76 | if (!WriteAll(file, buf, bytes_to_output)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 77 | PLOG(ERROR) << "WriteAll() failed."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 78 | return 1; |
| 79 | } |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 80 | size -= bytes_to_output; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 81 | } |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 82 | return 0; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 85 | } // namespace |
| 86 | |
| 87 | namespace bsdiff { |
| 88 | |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 89 | bool ReadAll(const std::unique_ptr<FileInterface>& file, |
| 90 | uint8_t* data, |
| 91 | size_t size) { |
| 92 | size_t offset = 0, read; |
| 93 | while (offset < size) { |
| 94 | if (!file->Read(data + offset, size - offset, &read) || read == 0) |
| 95 | return false; |
| 96 | offset += read; |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 101 | bool WriteAll(const std::unique_ptr<FileInterface>& file, |
| 102 | const uint8_t* data, |
| 103 | size_t size) { |
| 104 | size_t offset = 0, written; |
| 105 | while (offset < size) { |
Sen Jiang | b14bb55 | 2016-04-11 16:08:03 -0700 | [diff] [blame] | 106 | if (!file->Write(data + offset, size - offset, &written) || written == 0) |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 107 | return false; |
| 108 | offset += written; |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | bool IsOverlapping(const char* old_filename, |
| 114 | const char* new_filename, |
| 115 | const std::vector<ex_t>& old_extents, |
| 116 | const std::vector<ex_t>& new_extents) { |
| 117 | struct stat old_stat, new_stat; |
| 118 | if (stat(new_filename, &new_stat) == -1) { |
| 119 | if (errno == ENOENT) |
| 120 | return false; |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 121 | PLOG(ERROR) << "Error stat the new file: " << new_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 122 | return true; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 123 | } |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 124 | if (stat(old_filename, &old_stat) == -1) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 125 | PLOG(ERROR) << "Error stat the old file: " << old_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 126 | return true; |
| 127 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 128 | |
| 129 | if (old_stat.st_dev != new_stat.st_dev || old_stat.st_ino != new_stat.st_ino) |
| 130 | return false; |
| 131 | |
| 132 | if (old_extents.empty() && new_extents.empty()) |
| 133 | return true; |
| 134 | |
| 135 | for (ex_t old_ex : old_extents) |
| 136 | for (ex_t new_ex : new_extents) |
| 137 | if (static_cast<uint64_t>(old_ex.off) < new_ex.off + new_ex.len && |
| 138 | static_cast<uint64_t>(new_ex.off) < old_ex.off + old_ex.len) |
| 139 | return true; |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 144 | // Patch |old_filename| with |patch_filename| and save it to |new_filename|. |
| 145 | // |old_extents| and |new_extents| are comma-separated lists of "offset:length" |
| 146 | // extents of |old_filename| and |new_filename|. |
| 147 | // Returns 0 on success, 1 on I/O error and 2 on data error. |
| 148 | int bspatch(const char* old_filename, |
| 149 | const char* new_filename, |
| 150 | const char* patch_filename, |
| 151 | const char* old_extents, |
| 152 | const char* new_extents) { |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 153 | std::unique_ptr<FileInterface> patch_file = |
| 154 | File::FOpen(patch_filename, O_RDONLY); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 155 | if (!patch_file) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 156 | PLOG(ERROR) << "Error opening the patch file: " << patch_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 157 | return 1; |
| 158 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 159 | uint64_t patch_size; |
| 160 | patch_file->GetSize(&patch_size); |
| 161 | std::vector<uint8_t> patch(patch_size); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 162 | if (!ReadAll(patch_file, patch.data(), patch_size)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 163 | PLOG(ERROR) << "Error reading the patch file: " << patch_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 164 | return 1; |
| 165 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 166 | patch_file.reset(); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 167 | |
Sen Jiang | 62d5e48 | 2017-01-24 16:25:25 -0800 | [diff] [blame] | 168 | return bspatch(old_filename, new_filename, patch.data(), patch_size, |
| 169 | old_extents, new_extents); |
| 170 | } |
| 171 | |
| 172 | // Patch |old_filename| with |patch_data| and save it to |new_filename|. |
| 173 | // |old_extents| and |new_extents| are comma-separated lists of "offset:length" |
| 174 | // extents of |old_filename| and |new_filename|. |
| 175 | // Returns 0 on success, 1 on I/O error and 2 on data error. |
| 176 | int bspatch(const char* old_filename, |
| 177 | const char* new_filename, |
| 178 | const uint8_t* patch_data, |
| 179 | size_t patch_size, |
| 180 | const char* old_extents, |
| 181 | const char* new_extents) { |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 182 | int using_extents = (old_extents != NULL || new_extents != NULL); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 183 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 184 | // Open input file for reading. |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 185 | std::unique_ptr<FileInterface> old_file = File::FOpen(old_filename, O_RDONLY); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 186 | if (!old_file) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 187 | PLOG(ERROR) << "Error opening the old file: " << old_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 188 | return 1; |
| 189 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 190 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 191 | std::vector<ex_t> parsed_old_extents; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 192 | if (using_extents) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 193 | if (!ParseExtentStr(old_extents, &parsed_old_extents)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 194 | LOG(ERROR) << "Error parsing the old extents."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 195 | return 2; |
| 196 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 197 | old_file.reset(new ExtentsFile(std::move(old_file), parsed_old_extents)); |
| 198 | } |
| 199 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 200 | // Open output file for writing. |
| 201 | std::unique_ptr<FileInterface> new_file = |
| 202 | File::FOpen(new_filename, O_CREAT | O_WRONLY); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 203 | if (!new_file) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 204 | PLOG(ERROR) << "Error opening the new file: " << new_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 205 | return 1; |
| 206 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 207 | |
| 208 | std::vector<ex_t> parsed_new_extents; |
| 209 | if (using_extents) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 210 | if (!ParseExtentStr(new_extents, &parsed_new_extents)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 211 | LOG(ERROR) << "Error parsing the new extents."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 212 | return 2; |
| 213 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 214 | new_file.reset(new ExtentsFile(std::move(new_file), parsed_new_extents)); |
| 215 | } |
| 216 | |
| 217 | if (IsOverlapping(old_filename, new_filename, parsed_old_extents, |
| 218 | parsed_new_extents)) { |
| 219 | // New and old file is overlapping, we can not stream output to new file, |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 220 | // cache it in a buffer and write to the file at the end. |
Sen Jiang | 62d5e48 | 2017-01-24 16:25:25 -0800 | [diff] [blame] | 221 | uint64_t newsize = ParseInt64(patch_data + 24); |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 222 | new_file.reset(new BufferFile(std::move(new_file), newsize)); |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 223 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 224 | |
Sen Jiang | 62d5e48 | 2017-01-24 16:25:25 -0800 | [diff] [blame] | 225 | return bspatch(old_file, new_file, patch_data, patch_size); |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 228 | // Patch |old_data| with |patch_data| and save it by calling sink function. |
| 229 | // Returns 0 on success, 1 on I/O error and 2 on data error. |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 230 | int bspatch(const uint8_t* old_data, |
| 231 | size_t old_size, |
| 232 | const uint8_t* patch_data, |
| 233 | size_t patch_size, |
| 234 | const sink_func& sink) { |
| 235 | std::unique_ptr<FileInterface> old_file(new MemoryFile(old_data, old_size)); |
| 236 | std::unique_ptr<FileInterface> new_file(new SinkFile(sink)); |
| 237 | |
| 238 | return bspatch(old_file, new_file, patch_data, patch_size); |
| 239 | } |
| 240 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 241 | // Patch |old_file| with |patch_data| and save it to |new_file|. |
| 242 | // Returns 0 on success, 1 on I/O error and 2 on data error. |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 243 | int bspatch(const std::unique_ptr<FileInterface>& old_file, |
| 244 | const std::unique_ptr<FileInterface>& new_file, |
| 245 | const uint8_t* patch_data, |
| 246 | size_t patch_size) { |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 247 | BsdiffPatchReader patch_reader; |
| 248 | if (!patch_reader.Init(patch_data, patch_size)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 249 | LOG(ERROR) << "Failed to initialize patch reader."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 250 | return 2; |
| 251 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 252 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 253 | uint64_t old_file_size; |
| 254 | if (!old_file->GetSize(&old_file_size)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 255 | LOG(ERROR) << "Cannot obtain the size of old file."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 256 | return 1; |
| 257 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 258 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 259 | // The oldpos can be negative, but the new pos is only incremented linearly. |
| 260 | int64_t oldpos = 0; |
| 261 | uint64_t newpos = 0; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 262 | std::vector<uint8_t> old_buf(1024 * 1024); |
| 263 | std::vector<uint8_t> new_buf(1024 * 1024); |
| 264 | uint64_t old_file_pos = 0; |
| 265 | while (newpos < patch_reader.new_file_size()) { |
| 266 | ControlEntry control_entry(0, 0, 0); |
| 267 | if (!patch_reader.ParseControlEntry(&control_entry)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 268 | LOG(ERROR) << "Failed to read control stream."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 269 | return 2; |
| 270 | } |
Doug Zongker | 4d05479 | 2014-05-13 08:37:06 -0700 | [diff] [blame] | 271 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 272 | // Sanity-check. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 273 | if (newpos + control_entry.diff_size > patch_reader.new_file_size()) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 274 | LOG(ERROR) << "Corrupt patch."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 275 | return 2; |
| 276 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 277 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 278 | int ret = 0; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 279 | // Add old data to diff string. It is enough to fseek once, at |
| 280 | // the beginning of the sequence, to avoid unnecessary overhead. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 281 | int64_t seek_offset = oldpos; |
| 282 | if (seek_offset < 0) { |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 283 | // Write diff block directly to new file without adding old data, |
| 284 | // because we will skip part where |oldpos| < 0. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 285 | ret = ReadStreamAndWriteAll( |
| 286 | new_file, oldpos - old_file_size, new_buf.data(), new_buf.size(), |
| 287 | std::bind(&BsdiffPatchReader::ReadDiffStream, &patch_reader, |
| 288 | std::placeholders::_1, std::placeholders::_2)); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 289 | if (ret) |
| 290 | return ret; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 291 | seek_offset = 0; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 292 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 293 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 294 | // We just checked that |seek_offset| is not negative. |
| 295 | if (static_cast<uint64_t>(seek_offset) != old_file_pos && |
| 296 | !old_file->Seek(seek_offset)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 297 | PLOG(ERROR) << "Error seeking input file to offset: " << seek_offset; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 298 | return 1; |
| 299 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 300 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 301 | old_file_pos = |
| 302 | std::min<uint64_t>(oldpos + control_entry.diff_size, old_file_size); |
| 303 | size_t chunk_size = old_file_pos - seek_offset; |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 304 | while (chunk_size > 0) { |
| 305 | size_t read_bytes; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 306 | size_t bytes_to_read = std::min(chunk_size, old_buf.size()); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 307 | if (!old_file->Read(old_buf.data(), bytes_to_read, &read_bytes)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 308 | PLOG(ERROR) << "Error reading from input file."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 309 | return 1; |
| 310 | } |
| 311 | if (!read_bytes) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 312 | LOG(ERROR) << "EOF reached while reading from input file."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 313 | return 2; |
| 314 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 315 | // Read same amount of bytes from diff block |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 316 | if (!patch_reader.ReadDiffStream(new_buf.data(), read_bytes)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 317 | LOG(ERROR) << "Failed to read diff stream."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 318 | return 2; |
| 319 | } |
Sen Jiang | d87c835 | 2015-11-20 16:14:36 -0800 | [diff] [blame] | 320 | // new_buf already has data from diff block, adds old data to it. |
| 321 | for (size_t k = 0; k < read_bytes; k++) |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 322 | new_buf[k] += old_buf[k]; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 323 | if (!WriteAll(new_file, new_buf.data(), read_bytes)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 324 | PLOG(ERROR) << "Error writing to new file."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 325 | return 1; |
| 326 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 327 | chunk_size -= read_bytes; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 328 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 329 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 330 | // Adjust pointers. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 331 | newpos += control_entry.diff_size; |
| 332 | oldpos += control_entry.diff_size; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 333 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 334 | if (oldpos > static_cast<int64_t>(old_file_size)) { |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 335 | // Write diff block directly to new file without adding old data, |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 336 | // because we skipped part where |oldpos| > old_file_size. |
| 337 | ret = ReadStreamAndWriteAll( |
| 338 | new_file, oldpos - old_file_size, new_buf.data(), new_buf.size(), |
| 339 | std::bind(&BsdiffPatchReader::ReadDiffStream, &patch_reader, |
| 340 | std::placeholders::_1, std::placeholders::_2)); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 341 | if (ret) |
| 342 | return ret; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 345 | // Sanity-check. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 346 | if (newpos + control_entry.extra_size > patch_reader.new_file_size()) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 347 | LOG(ERROR) << "Corrupt patch."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 348 | return 2; |
| 349 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 350 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 351 | // Read extra block. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 352 | ret = ReadStreamAndWriteAll( |
| 353 | new_file, control_entry.extra_size, new_buf.data(), new_buf.size(), |
| 354 | std::bind(&BsdiffPatchReader::ReadExtraStream, &patch_reader, |
| 355 | std::placeholders::_1, std::placeholders::_2)); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 356 | if (ret) |
| 357 | return ret; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 358 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 359 | // Adjust pointers. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 360 | newpos += control_entry.extra_size; |
| 361 | oldpos += control_entry.offset_increment; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 362 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 363 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 364 | // Close input file. |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 365 | old_file->Close(); |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 366 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 367 | if (!patch_reader.Finish()) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 368 | LOG(ERROR) << "Failed to finish the patch reader."; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 369 | return 2; |
| 370 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 371 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 372 | if (!new_file->Close()) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 373 | PLOG(ERROR) << "Error closing new file."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 374 | return 1; |
| 375 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 376 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 377 | return 0; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 378 | } |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 379 | |
| 380 | } // namespace bsdiff |