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
3 // found in the LICENSE file.
5 #include "base/files/important_file_writer.h"
11 #include "base/bind.h"
12 #include "base/critical_closure.h"
13 #include "base/file_util.h"
14 #include "base/files/file_path.h"
15 #include "base/logging.h"
16 #include "base/metrics/histogram.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/task_runner.h"
19 #include "base/threading/thread.h"
20 #include "base/time/time.h"
26 const int kDefaultCommitIntervalMs
= 10000;
28 enum TempFileFailure
{
37 void LogFailure(const FilePath
& path
, TempFileFailure failure_code
,
38 const std::string
& message
) {
39 UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code
,
40 TEMP_FILE_FAILURE_MAX
);
41 DPLOG(WARNING
) << "temp file failure: " << path
.value().c_str()
48 bool ImportantFileWriter::WriteFileAtomically(const FilePath
& path
,
49 const std::string
& data
) {
50 // Write the data to a temp file then rename to avoid data loss if we crash
51 // while writing the file. Ensure that the temp file is on the same volume
52 // as target file, so it can be moved in one step, and that the temp file
53 // is securely created.
54 FilePath tmp_file_path
;
55 if (!file_util::CreateTemporaryFileInDir(path
.DirName(), &tmp_file_path
)) {
56 LogFailure(path
, FAILED_CREATING
, "could not create temporary file");
60 int flags
= PLATFORM_FILE_OPEN
| PLATFORM_FILE_WRITE
;
61 PlatformFile tmp_file
=
62 CreatePlatformFile(tmp_file_path
, flags
, NULL
, NULL
);
63 if (tmp_file
== kInvalidPlatformFileValue
) {
64 LogFailure(path
, FAILED_OPENING
, "could not open temporary file");
68 // If this happens in the wild something really bad is going on.
69 CHECK_LE(data
.length(), static_cast<size_t>(kint32max
));
70 int bytes_written
= WritePlatformFile(
71 tmp_file
, 0, data
.data(), static_cast<int>(data
.length()));
72 FlushPlatformFile(tmp_file
); // Ignore return value.
74 if (!ClosePlatformFile(tmp_file
)) {
75 LogFailure(path
, FAILED_CLOSING
, "failed to close temporary file");
76 base::DeleteFile(tmp_file_path
, false);
80 if (bytes_written
< static_cast<int>(data
.length())) {
81 LogFailure(path
, FAILED_WRITING
, "error writing, bytes_written=" +
82 IntToString(bytes_written
));
83 base::DeleteFile(tmp_file_path
, false);
87 if (!base::ReplaceFile(tmp_file_path
, path
, NULL
)) {
88 LogFailure(path
, FAILED_RENAMING
, "could not rename temporary file");
89 base::DeleteFile(tmp_file_path
, false);
96 ImportantFileWriter::ImportantFileWriter(
97 const FilePath
& path
, base::SequencedTaskRunner
* task_runner
)
99 task_runner_(task_runner
),
101 commit_interval_(TimeDelta::FromMilliseconds(
102 kDefaultCommitIntervalMs
)) {
103 DCHECK(CalledOnValidThread());
104 DCHECK(task_runner_
.get());
107 ImportantFileWriter::~ImportantFileWriter() {
108 // We're usually a member variable of some other object, which also tends
109 // to be our serializer. It may not be safe to call back to the parent object
111 DCHECK(!HasPendingWrite());
114 bool ImportantFileWriter::HasPendingWrite() const {
115 DCHECK(CalledOnValidThread());
116 return timer_
.IsRunning();
119 void ImportantFileWriter::WriteNow(const std::string
& data
) {
120 DCHECK(CalledOnValidThread());
121 if (data
.length() > static_cast<size_t>(kint32max
)) {
126 if (HasPendingWrite())
129 if (!task_runner_
->PostTask(
132 Bind(IgnoreResult(&ImportantFileWriter::WriteFileAtomically
),
134 // Posting the task to background message loop is not expected
135 // to fail, but if it does, avoid losing data and just hit the disk
136 // on the current thread.
139 WriteFileAtomically(path_
, data
);
143 void ImportantFileWriter::ScheduleWrite(DataSerializer
* serializer
) {
144 DCHECK(CalledOnValidThread());
147 serializer_
= serializer
;
149 if (!timer_
.IsRunning()) {
150 timer_
.Start(FROM_HERE
, commit_interval_
, this,
151 &ImportantFileWriter::DoScheduledWrite
);
155 void ImportantFileWriter::DoScheduledWrite() {
158 if (serializer_
->SerializeData(&data
)) {
161 DLOG(WARNING
) << "failed to serialize data to be saved in "
162 << path_
.value().c_str();