Commit 28470cbf authored by Etienne Bergeron's avatar Etienne Bergeron Committed by Commit Bot

Use a temporary trace file to avoid corrupt traces

R=gab@chromium.org,eseckler@chromium.org

Bug: 1082916
Change-Id: Ie0f50d29df84b360ecc7a556bcbaea5801b73f32
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2225245
Commit-Queue: Etienne Bergeron <etienneb@chromium.org>
Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774680}
parent 0fce1e8a
...@@ -85,11 +85,14 @@ class FileTraceDataEndpoint : public TracingController::TraceDataEndpoint { ...@@ -85,11 +85,14 @@ class FileTraceDataEndpoint : public TracingController::TraceDataEndpoint {
} }
bool OpenFileIfNeededOnBlockingThread() { bool OpenFileIfNeededOnBlockingThread() {
base::ScopedBlockingCall scoped_blocking_call(
FROM_HERE, base::BlockingType::MAY_BLOCK);
if (file_ != nullptr) if (file_ != nullptr)
return true; return true;
file_ = base::OpenFile(file_path_, "w");
// The temporary trace file is produced in the same folder since paths must
// be on the same volume.
file_ = FileToFILE(CreateAndOpenTemporaryFileInDir(file_path_.DirName(),
&pending_file_path_),
"w");
if (file_ == nullptr) { if (file_ == nullptr) {
LOG(ERROR) << "Failed to open " << file_path_.value(); LOG(ERROR) << "Failed to open " << file_path_.value();
return false; return false;
...@@ -103,6 +106,14 @@ class FileTraceDataEndpoint : public TracingController::TraceDataEndpoint { ...@@ -103,6 +106,14 @@ class FileTraceDataEndpoint : public TracingController::TraceDataEndpoint {
file_ = nullptr; file_ = nullptr;
} }
base::File::Error error;
if (!base::ReplaceFile(pending_file_path_, file_path_, &error)) {
LOG(ERROR) << "Cannot replace file '" << file_path_
<< "' : " << base::File::ErrorToString(error);
base::DeleteFile(pending_file_path_, false);
return;
}
GetUIThreadTaskRunner({})->PostTask( GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, FROM_HERE,
base::BindOnce(&FileTraceDataEndpoint::FinalizeOnUIThread, this)); base::BindOnce(&FileTraceDataEndpoint::FinalizeOnUIThread, this));
...@@ -111,6 +122,7 @@ class FileTraceDataEndpoint : public TracingController::TraceDataEndpoint { ...@@ -111,6 +122,7 @@ class FileTraceDataEndpoint : public TracingController::TraceDataEndpoint {
void FinalizeOnUIThread() { std::move(completion_callback_).Run(); } void FinalizeOnUIThread() { std::move(completion_callback_).Run(); }
base::FilePath file_path_; base::FilePath file_path_;
base::FilePath pending_file_path_;
base::OnceClosure completion_callback_; base::OnceClosure completion_callback_;
FILE* file_ = nullptr; FILE* file_ = nullptr;
const scoped_refptr<base::SequencedTaskRunner> may_block_task_runner_; const scoped_refptr<base::SequencedTaskRunner> may_block_task_runner_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment