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/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
17 #include "base/metrics/histogram.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/task_runner.h"
20 #include "base/task_runner_util.h"
21 #include "base/threading/thread.h"
22 #include "base/time/time.h"
28 const int kDefaultCommitIntervalMs
= 10000;
30 enum TempFileFailure
{
40 void LogFailure(const FilePath
& path
, TempFileFailure failure_code
,
41 const std::string
& message
) {
42 UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code
,
43 TEMP_FILE_FAILURE_MAX
);
44 DPLOG(WARNING
) << "temp file failure: " << path
.value().c_str()
51 bool ImportantFileWriter::WriteFileAtomically(const FilePath
& path
,
52 const std::string
& data
) {
53 // Write the data to a temp file then rename to avoid data loss if we crash
54 // while writing the file. Ensure that the temp file is on the same volume
55 // as target file, so it can be moved in one step, and that the temp file
56 // is securely created.
57 FilePath tmp_file_path
;
58 if (!base::CreateTemporaryFileInDir(path
.DirName(), &tmp_file_path
)) {
59 LogFailure(path
, FAILED_CREATING
, "could not create temporary file");
63 File
tmp_file(tmp_file_path
, File::FLAG_OPEN
| File::FLAG_WRITE
);
64 if (!tmp_file
.IsValid()) {
65 LogFailure(path
, FAILED_OPENING
, "could not open temporary file");
69 // If this happens in the wild something really bad is going on.
70 CHECK_LE(data
.length(), static_cast<size_t>(kint32max
));
71 int bytes_written
= tmp_file
.Write(0, data
.data(),
72 static_cast<int>(data
.length()));
73 bool flush_success
= tmp_file
.Flush();
76 if (bytes_written
< static_cast<int>(data
.length())) {
77 LogFailure(path
, FAILED_WRITING
, "error writing, bytes_written=" +
78 IntToString(bytes_written
));
79 base::DeleteFile(tmp_file_path
, false);
84 LogFailure(path
, FAILED_FLUSHING
, "error flushing");
85 base::DeleteFile(tmp_file_path
, false);
89 if (!base::ReplaceFile(tmp_file_path
, path
, NULL
)) {
90 LogFailure(path
, FAILED_RENAMING
, "could not rename temporary file");
91 base::DeleteFile(tmp_file_path
, false);
98 ImportantFileWriter::ImportantFileWriter(
100 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
)
102 task_runner_(task_runner
),
104 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs
)),
105 weak_factory_(this) {
106 DCHECK(CalledOnValidThread());
107 DCHECK(task_runner_
.get());
110 ImportantFileWriter::~ImportantFileWriter() {
111 // We're usually a member variable of some other object, which also tends
112 // to be our serializer. It may not be safe to call back to the parent object
114 DCHECK(!HasPendingWrite());
117 bool ImportantFileWriter::HasPendingWrite() const {
118 DCHECK(CalledOnValidThread());
119 return timer_
.IsRunning();
122 void ImportantFileWriter::WriteNow(const std::string
& data
) {
123 DCHECK(CalledOnValidThread());
124 if (data
.length() > static_cast<size_t>(kint32max
)) {
129 if (HasPendingWrite())
132 if (!PostWriteTask(data
)) {
133 // Posting the task to background message loop is not expected
134 // to fail, but if it does, avoid losing data and just hit the disk
135 // on the current thread.
138 WriteFileAtomically(path_
, data
);
142 void ImportantFileWriter::ScheduleWrite(DataSerializer
* serializer
) {
143 DCHECK(CalledOnValidThread());
146 serializer_
= serializer
;
148 if (!timer_
.IsRunning()) {
149 timer_
.Start(FROM_HERE
, commit_interval_
, this,
150 &ImportantFileWriter::DoScheduledWrite
);
154 void ImportantFileWriter::DoScheduledWrite() {
157 if (serializer_
->SerializeData(&data
)) {
160 DLOG(WARNING
) << "failed to serialize data to be saved in "
161 << path_
.value().c_str();
166 void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback(
167 const base::Closure
& on_next_successful_write
) {
168 DCHECK(on_next_successful_write_
.is_null());
169 on_next_successful_write_
= on_next_successful_write
;
172 bool ImportantFileWriter::PostWriteTask(const std::string
& data
) {
173 // TODO(gab): This code could always use PostTaskAndReplyWithResult and let
174 // ForwardSuccessfulWrite() no-op if |on_next_successful_write_| is null, but
175 // PostTaskAndReply causes memory leaks in tests (crbug.com/371974) and
176 // suppressing all of those is unrealistic hence we avoid most of them by
177 // using PostTask() in the typical scenario below.
178 if (!on_next_successful_write_
.is_null()) {
179 return base::PostTaskAndReplyWithResult(
183 Bind(&ImportantFileWriter::WriteFileAtomically
, path_
, data
)),
184 Bind(&ImportantFileWriter::ForwardSuccessfulWrite
,
185 weak_factory_
.GetWeakPtr()));
187 return task_runner_
->PostTask(
190 Bind(IgnoreResult(&ImportantFileWriter::WriteFileAtomically
),
194 void ImportantFileWriter::ForwardSuccessfulWrite(bool result
) {
195 DCHECK(CalledOnValidThread());
196 if (result
&& !on_next_successful_write_
.is_null()) {
197 on_next_successful_write_
.Run();
198 on_next_successful_write_
.Reset();