Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: courgette/ensemble_apply.cc

Issue 2534873005: Sandbox the component updater's patcher utility process. (Closed)
Patch Set: Through #29 Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file contains the code to apply a Courgette patch. 5 // This file contains the code to apply a Courgette patch.
6 6
7 #include "courgette/ensemble.h" 7 #include "courgette/ensemble.h"
8 8
9 #include <stddef.h> 9 #include <stddef.h>
10 #include <stdint.h> 10 #include <stdint.h>
11 11
12 #include <memory> 12 #include <memory>
13 #include <utility> 13 #include <utility>
14 14
15 #include "base/files/file.h"
15 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
16 #include "base/files/memory_mapped_file.h" 17 #include "base/files/memory_mapped_file.h"
17 #include "base/logging.h" 18 #include "base/logging.h"
18 #include "base/macros.h" 19 #include "base/macros.h"
19 #include "courgette/crc.h" 20 #include "courgette/crc.h"
20 #include "courgette/patcher_x86_32.h" 21 #include "courgette/patcher_x86_32.h"
21 #include "courgette/region.h" 22 #include "courgette/region.h"
22 #include "courgette/simple_delta.h" 23 #include "courgette/simple_delta.h"
23 #include "courgette/streams.h" 24 #include "courgette/streams.h"
24 25
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 SourceStream final_patch_prediction; 365 SourceStream final_patch_prediction;
365 final_patch_prediction.Init(original_ensemble_and_corrected_base_elements); 366 final_patch_prediction.Init(original_ensemble_and_corrected_base_elements);
366 status = patch_process.SubpatchFinalOutput(&final_patch_prediction, 367 status = patch_process.SubpatchFinalOutput(&final_patch_prediction,
367 ensemble_correction, output); 368 ensemble_correction, output);
368 if (status != C_OK) 369 if (status != C_OK)
369 return status; 370 return status;
370 371
371 return C_OK; 372 return C_OK;
372 } 373 }
373 374
374 Status ApplyEnsemblePatch(const base::FilePath::CharType* old_file_name, 375 Status ApplyEnsemblePatch(base::File old_file,
375 const base::FilePath::CharType* patch_file_name, 376 base::File patch_file,
376 const base::FilePath::CharType* new_file_name) { 377 base::File new_file) {
377 base::FilePath patch_file_path(patch_file_name); 378 base::MemoryMappedFile patch_file_mem;
378 base::MemoryMappedFile patch_file; 379 if (!patch_file_mem.Initialize(std::move(patch_file)))
379 if (!patch_file.Initialize(patch_file_path))
380 return C_READ_OPEN_ERROR; 380 return C_READ_OPEN_ERROR;
381 381
382 // 'Dry-run' the first step of the patch process to validate format of header. 382 // 'Dry-run' the first step of the patch process to validate format of header.
383 SourceStream patch_header_stream; 383 SourceStream patch_header_stream;
384 patch_header_stream.Init(patch_file.data(), patch_file.length()); 384 patch_header_stream.Init(patch_file_mem.data(), patch_file_mem.length());
385 EnsemblePatchApplication patch_process; 385 EnsemblePatchApplication patch_process;
386 Status status = patch_process.ReadHeader(&patch_header_stream); 386 Status status = patch_process.ReadHeader(&patch_header_stream);
387 if (status != C_OK) 387 if (status != C_OK)
388 return status; 388 return status;
389 389
390 // Read the old_file. 390 // Read the old_file.
391 base::FilePath old_file_path(old_file_name); 391 base::MemoryMappedFile old_file_mem;
392 base::MemoryMappedFile old_file; 392 if (!old_file_mem.Initialize(std::move(old_file)))
393 if (!old_file.Initialize(old_file_path))
394 return C_READ_ERROR; 393 return C_READ_ERROR;
395 394
396 // Apply patch on streams. 395 // Apply patch on streams.
397 SourceStream old_source_stream; 396 SourceStream old_source_stream;
398 SourceStream patch_source_stream; 397 SourceStream patch_source_stream;
399 old_source_stream.Init(old_file.data(), old_file.length()); 398 old_source_stream.Init(old_file_mem.data(), old_file_mem.length());
400 patch_source_stream.Init(patch_file.data(), patch_file.length()); 399 patch_source_stream.Init(patch_file_mem.data(), patch_file_mem.length());
401 SinkStream new_sink_stream; 400 SinkStream new_sink_stream;
402 status = ApplyEnsemblePatch(&old_source_stream, &patch_source_stream, 401 status = ApplyEnsemblePatch(&old_source_stream, &patch_source_stream,
403 &new_sink_stream); 402 &new_sink_stream);
404 if (status != C_OK) 403 if (status != C_OK)
405 return status; 404 return status;
406 405
407 // Write the patched data to |new_file_name|. 406 // Write the patched data to |new_file_name|.
408 base::FilePath new_file_path(new_file_name); 407 int written = new_file.Write(
409 int written = 408 0,
410 base::WriteFile( 409 reinterpret_cast<const char*>(new_sink_stream.Buffer()),
411 new_file_path, 410 static_cast<int>(new_sink_stream.Length()));
412 reinterpret_cast<const char*>(new_sink_stream.Buffer()),
413 static_cast<int>(new_sink_stream.Length()));
414 if (written == -1) 411 if (written == -1)
415 return C_WRITE_OPEN_ERROR; 412 return C_WRITE_OPEN_ERROR;
416 if (static_cast<size_t>(written) != new_sink_stream.Length()) 413 if (static_cast<size_t>(written) != new_sink_stream.Length())
417 return C_WRITE_ERROR; 414 return C_WRITE_ERROR;
418 415
419 return C_OK; 416 return C_OK;
420 } 417 }
421 418
422 } // namespace 419 Status ApplyEnsemblePatch(const base::FilePath::CharType* old_file_name,
420 const base::FilePath::CharType* patch_file_name,
421 const base::FilePath::CharType* new_file_name) {
422 Status result = ApplyEnsemblePatch(
423 base::File(
424 base::FilePath(old_file_name),
425 base::File::FLAG_OPEN | base::File::FLAG_READ),
426 base::File(
427 base::FilePath(patch_file_name),
428 base::File::FLAG_OPEN | base::File::FLAG_READ),
429 base::File(
430 base::FilePath(new_file_name),
431 base::File::FLAG_CREATE_ALWAYS |
432 base::File::FLAG_WRITE |
433 base::File::FLAG_EXCLUSIVE_WRITE));
434 if (result != C_OK)
435 base::DeleteFile(base::FilePath(new_file_name), false);
436 return result;
437 }
438
439 } // namespace courgette
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698