Index: base/files/important_file_writer.cc |
diff --git a/base/files/important_file_writer.cc b/base/files/important_file_writer.cc |
index 536b0fb161b93cadd3a38a355bf2e071b9c1f59d..351adc242098637514328ee7e6897a4db321fcba 100644 |
--- a/base/files/important_file_writer.cc |
+++ b/base/files/important_file_writer.cc |
@@ -42,7 +42,11 @@ void LogFailure(const FilePath& path, TempFileFailure failure_code, |
<< " : " << message; |
} |
-void WriteToDiskTask(const FilePath& path, const std::string& data) { |
+} // namespace |
+ |
+// static |
+bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, |
+ const std::string& data) { |
// Write the data to a temp file then rename to avoid data loss if we crash |
// while writing the file. Ensure that the temp file is on the same volume |
// as target file, so it can be moved in one step, and that the temp file |
@@ -50,7 +54,7 @@ void WriteToDiskTask(const FilePath& path, const std::string& data) { |
FilePath tmp_file_path; |
if (!file_util::CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) { |
LogFailure(path, FAILED_CREATING, "could not create temporary file"); |
- return; |
+ return false; |
} |
int flags = PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE; |
@@ -58,7 +62,7 @@ void WriteToDiskTask(const FilePath& path, const std::string& data) { |
CreatePlatformFile(tmp_file_path, flags, NULL, NULL); |
if (tmp_file == kInvalidPlatformFileValue) { |
LogFailure(path, FAILED_OPENING, "could not open temporary file"); |
- return; |
+ return false; |
} |
// If this happens in the wild something really bad is going on. |
@@ -70,24 +74,24 @@ void WriteToDiskTask(const FilePath& path, const std::string& data) { |
if (!ClosePlatformFile(tmp_file)) { |
LogFailure(path, FAILED_CLOSING, "failed to close temporary file"); |
file_util::Delete(tmp_file_path, false); |
- return; |
+ return false; |
} |
if (bytes_written < static_cast<int>(data.length())) { |
LogFailure(path, FAILED_WRITING, "error writing, bytes_written=" + |
IntToString(bytes_written)); |
file_util::Delete(tmp_file_path, false); |
- return; |
+ return false; |
} |
if (!file_util::ReplaceFile(tmp_file_path, path)) { |
LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); |
file_util::Delete(tmp_file_path, false); |
- return; |
+ return false; |
} |
-} |
-} // namespace |
+ return true; |
+} |
ImportantFileWriter::ImportantFileWriter( |
const FilePath& path, base::SequencedTaskRunner* task_runner) |
@@ -122,14 +126,17 @@ void ImportantFileWriter::WriteNow(const std::string& data) { |
if (HasPendingWrite()) |
timer_.Stop(); |
- if (!task_runner_->PostTask(FROM_HERE, |
- MakeCriticalClosure(Bind(&WriteToDiskTask, path_, data)))) { |
+ if (!task_runner_->PostTask( |
+ FROM_HERE, |
+ MakeCriticalClosure( |
+ Bind(IgnoreResult(&ImportantFileWriter::WriteFileAtomically), |
+ path_, data)))) { |
// Posting the task to background message loop is not expected |
// to fail, but if it does, avoid losing data and just hit the disk |
// on the current thread. |
NOTREACHED(); |
- WriteToDiskTask(path_, data); |
+ WriteFileAtomically(path_, data); |
} |
} |