| OLD | NEW |
| 1 // Copyright 2003, 2004 Colin Percival | 1 // Copyright 2003, 2004 Colin Percival |
| 2 // All rights reserved | 2 // All rights reserved |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted providing that the following conditions | 5 // modification, are permitted providing that the following conditions |
| 6 // are met: | 6 // are met: |
| 7 // 1. Redistributions of source code must retain the above copyright | 7 // 1. Redistributions of source code must retain the above copyright |
| 8 // notice, this list of conditions and the following disclaimer. | 8 // notice, this list of conditions and the following disclaimer. |
| 9 // 2. Redistributions in binary form must reproduce the above copyright | 9 // 2. Redistributions in binary form must reproduce the above copyright |
| 10 // notice, this list of conditions and the following disclaimer in the | 10 // notice, this list of conditions and the following disclaimer in the |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 34 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 35 // Use of this source code is governed by a BSD-style license that can be | 35 // Use of this source code is governed by a BSD-style license that can be |
| 36 // found in the LICENSE file. | 36 // found in the LICENSE file. |
| 37 | 37 |
| 38 #include "courgette/third_party/bsdiff/bsdiff.h" | 38 #include "courgette/third_party/bsdiff/bsdiff.h" |
| 39 | 39 |
| 40 #include <stddef.h> | 40 #include <stddef.h> |
| 41 #include <stdint.h> | 41 #include <stdint.h> |
| 42 | 42 |
| 43 #include "base/files/file_util.h" |
| 43 #include "base/files/memory_mapped_file.h" | 44 #include "base/files/memory_mapped_file.h" |
| 44 #include "courgette/crc.h" | 45 #include "courgette/crc.h" |
| 45 #include "courgette/streams.h" | 46 #include "courgette/streams.h" |
| 46 | 47 |
| 47 namespace { | 48 namespace { |
| 48 | 49 |
| 49 using courgette::CalculateCrc; | 50 using courgette::CalculateCrc; |
| 50 using courgette::SinkStream; | 51 using courgette::SinkStream; |
| 51 using courgette::SinkStreamSet; | 52 using courgette::SinkStreamSet; |
| 52 using courgette::SourceStream; | 53 using courgette::SourceStream; |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 return UNEXPECTED_ERROR; | 183 return UNEXPECTED_ERROR; |
| 183 | 184 |
| 184 if (CalculateCrc(old_start, old_size) != header.scrc32) | 185 if (CalculateCrc(old_start, old_size) != header.scrc32) |
| 185 return CRC_ERROR; | 186 return CRC_ERROR; |
| 186 | 187 |
| 187 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); | 188 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); |
| 188 | 189 |
| 189 return OK; | 190 return OK; |
| 190 } | 191 } |
| 191 | 192 |
| 192 BSDiffStatus ApplyBinaryPatch(const base::FilePath& old_file_path, | 193 BSDiffStatus ApplyBinaryPatch(base::File old_file, |
| 193 const base::FilePath& patch_file_path, | 194 base::File patch_file, |
| 194 const base::FilePath& new_file_path) { | 195 base::File new_file) { |
| 195 // Set up the old stream. | 196 // Set up the old stream. |
| 196 base::MemoryMappedFile old_file; | 197 base::MemoryMappedFile old_file_mem; |
| 197 if (!old_file.Initialize(old_file_path)) { | 198 if (!old_file_mem.Initialize(std::move(old_file))) { |
| 198 return READ_ERROR; | 199 return READ_ERROR; |
| 199 } | 200 } |
| 200 SourceStream old_file_stream; | 201 SourceStream old_file_stream; |
| 201 old_file_stream.Init(old_file.data(), old_file.length()); | 202 old_file_stream.Init(old_file_mem.data(), old_file_mem.length()); |
| 202 | 203 |
| 203 // Set up the patch stream. | 204 // Set up the patch stream. |
| 204 base::MemoryMappedFile patch_file; | 205 base::MemoryMappedFile patch_file_mem; |
| 205 if (!patch_file.Initialize(patch_file_path)) { | 206 if (!patch_file_mem.Initialize(std::move(patch_file))) { |
| 206 return READ_ERROR; | 207 return READ_ERROR; |
| 207 } | 208 } |
| 208 SourceStream patch_file_stream; | 209 SourceStream patch_file_stream; |
| 209 patch_file_stream.Init(patch_file.data(), patch_file.length()); | 210 patch_file_stream.Init(patch_file_mem.data(), patch_file_mem.length()); |
| 210 | 211 |
| 211 // Set up the new stream and apply the patch. | 212 // Set up the new stream and apply the patch. |
| 212 SinkStream new_sink_stream; | 213 SinkStream new_sink_stream; |
| 213 BSDiffStatus status = | 214 BSDiffStatus status = |
| 214 ApplyBinaryPatch(&old_file_stream, &patch_file_stream, &new_sink_stream); | 215 ApplyBinaryPatch(&old_file_stream, &patch_file_stream, &new_sink_stream); |
| 215 if (status != OK) { | 216 if (status != OK) { |
| 216 return status; | 217 return status; |
| 217 } | 218 } |
| 218 | 219 |
| 219 // Write the stream to disk. | 220 // Write the stream to disk. |
| 220 int written = base::WriteFile( | 221 int written = new_file.Write( |
| 221 new_file_path, reinterpret_cast<const char*>(new_sink_stream.Buffer()), | 222 0, |
| 223 reinterpret_cast<const char*>(new_sink_stream.Buffer()), |
| 222 static_cast<int>(new_sink_stream.Length())); | 224 static_cast<int>(new_sink_stream.Length())); |
| 223 if (written != static_cast<int>(new_sink_stream.Length())) | 225 if (written != static_cast<int>(new_sink_stream.Length())) |
| 224 return WRITE_ERROR; | 226 return WRITE_ERROR; |
| 225 return OK; | 227 return OK; |
| 226 } | 228 } |
| 227 | 229 |
| 230 BSDiffStatus ApplyBinaryPatch(const base::FilePath& old_file_path, |
| 231 const base::FilePath& patch_file_path, |
| 232 const base::FilePath& new_file_path) { |
| 233 BSDiffStatus result = ApplyBinaryPatch( |
| 234 base::File( |
| 235 old_file_path, |
| 236 base::File::FLAG_OPEN | base::File::FLAG_READ), |
| 237 base::File( |
| 238 patch_file_path, |
| 239 base::File::FLAG_OPEN | base::File::FLAG_READ), |
| 240 base::File( |
| 241 new_file_path, |
| 242 base::File::FLAG_CREATE_ALWAYS | |
| 243 base::File::FLAG_WRITE | |
| 244 base::File::FLAG_EXCLUSIVE_WRITE)); |
| 245 if (result != OK) |
| 246 base::DeleteFile(new_file_path, false); |
| 247 return result; |
| 248 } |
| 249 |
| 228 } // namespace bsdiff | 250 } // namespace bsdiff |
| OLD | NEW |