OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/common/important_file_writer.h" | |
6 | |
7 #include <stdio.h> | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/file_path.h" | |
13 #include "base/file_util.h" | |
14 #include "base/logging.h" | |
15 #include "base/message_loop_proxy.h" | |
16 #include "base/metrics/histogram.h" | |
17 #include "base/string_number_conversions.h" | |
18 #include "base/threading/thread.h" | |
19 #include "base/time.h" | |
20 | |
21 using base::TimeDelta; | |
22 | |
23 namespace { | |
24 | |
25 const int kDefaultCommitIntervalMs = 10000; | |
26 | |
27 enum TempFileFailure { | |
28 FAILED_CREATING, | |
29 FAILED_OPENING, | |
30 FAILED_CLOSING, | |
31 FAILED_WRITING, | |
32 FAILED_RENAMING, | |
33 TEMP_FILE_FAILURE_MAX | |
34 }; | |
35 | |
36 void LogFailure(const FilePath& path, TempFileFailure failure_code, | |
37 const std::string& message) { | |
38 UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code, | |
39 TEMP_FILE_FAILURE_MAX); | |
40 DPLOG(WARNING) << "temp file failure: " << path.value() | |
41 << " : " << message; | |
42 } | |
43 | |
44 void WriteToDiskTask(const FilePath& path, const std::string& data) { | |
45 // Write the data to a temp file then rename to avoid data loss if we crash | |
46 // while writing the file. Ensure that the temp file is on the same volume | |
47 // as target file, so it can be moved in one step, and that the temp file | |
48 // is securely created. | |
49 FilePath tmp_file_path; | |
50 if (!file_util::CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) { | |
51 LogFailure(path, FAILED_CREATING, "could not create temporary file"); | |
52 return; | |
53 } | |
54 | |
55 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE; | |
56 base::PlatformFile tmp_file = | |
57 base::CreatePlatformFile(tmp_file_path, flags, NULL, NULL); | |
58 if (tmp_file == base::kInvalidPlatformFileValue) { | |
59 LogFailure(path, FAILED_OPENING, "could not open temporary file"); | |
60 return; | |
61 } | |
62 | |
63 // If this happens in the wild something really bad is going on. | |
64 CHECK_LE(data.length(), static_cast<size_t>(kint32max)); | |
65 int bytes_written = base::WritePlatformFile( | |
66 tmp_file, 0, data.data(), static_cast<int>(data.length())); | |
67 base::FlushPlatformFile(tmp_file); // Ignore return value. | |
68 | |
69 if (!base::ClosePlatformFile(tmp_file)) { | |
70 LogFailure(path, FAILED_CLOSING, "failed to close temporary file"); | |
71 file_util::Delete(tmp_file_path, false); | |
72 return; | |
73 } | |
74 | |
75 if (bytes_written < static_cast<int>(data.length())) { | |
76 LogFailure(path, FAILED_WRITING, "error writing, bytes_written=" + | |
77 base::IntToString(bytes_written)); | |
78 file_util::Delete(tmp_file_path, false); | |
79 return; | |
80 } | |
81 | |
82 if (!file_util::ReplaceFile(tmp_file_path, path)) { | |
83 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); | |
84 file_util::Delete(tmp_file_path, false); | |
85 return; | |
86 } | |
87 } | |
88 | |
89 } // namespace | |
90 | |
91 ImportantFileWriter::ImportantFileWriter( | |
92 const FilePath& path, base::MessageLoopProxy* file_message_loop_proxy) | |
93 : path_(path), | |
94 file_message_loop_proxy_(file_message_loop_proxy), | |
95 serializer_(NULL), | |
96 commit_interval_(TimeDelta::FromMilliseconds( | |
97 kDefaultCommitIntervalMs)) { | |
98 DCHECK(CalledOnValidThread()); | |
99 DCHECK(file_message_loop_proxy_.get()); | |
100 } | |
101 | |
102 ImportantFileWriter::~ImportantFileWriter() { | |
103 // We're usually a member variable of some other object, which also tends | |
104 // to be our serializer. It may not be safe to call back to the parent object | |
105 // being destructed. | |
106 DCHECK(!HasPendingWrite()); | |
107 } | |
108 | |
109 bool ImportantFileWriter::HasPendingWrite() const { | |
110 DCHECK(CalledOnValidThread()); | |
111 return timer_.IsRunning(); | |
112 } | |
113 | |
114 void ImportantFileWriter::WriteNow(const std::string& data) { | |
115 DCHECK(CalledOnValidThread()); | |
116 if (data.length() > static_cast<size_t>(kint32max)) { | |
117 NOTREACHED(); | |
118 return; | |
119 } | |
120 | |
121 if (HasPendingWrite()) | |
122 timer_.Stop(); | |
123 | |
124 if (!file_message_loop_proxy_->PostTask( | |
125 FROM_HERE, base::Bind(&WriteToDiskTask, path_, data))) { | |
126 // Posting the task to background message loop is not expected | |
127 // to fail, but if it does, avoid losing data and just hit the disk | |
128 // on the current thread. | |
129 NOTREACHED(); | |
130 | |
131 WriteToDiskTask(path_, data); | |
132 } | |
133 } | |
134 | |
135 void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { | |
136 DCHECK(CalledOnValidThread()); | |
137 | |
138 DCHECK(serializer); | |
139 serializer_ = serializer; | |
140 | |
141 if (!timer_.IsRunning()) { | |
142 timer_.Start(FROM_HERE, commit_interval_, this, | |
143 &ImportantFileWriter::DoScheduledWrite); | |
144 } | |
145 } | |
146 | |
147 void ImportantFileWriter::DoScheduledWrite() { | |
148 DCHECK(serializer_); | |
149 std::string data; | |
150 if (serializer_->SerializeData(&data)) { | |
151 WriteNow(data); | |
152 } else { | |
153 DLOG(WARNING) << "failed to serialize data to be saved in " | |
154 << path_.value(); | |
155 } | |
156 serializer_ = NULL; | |
157 } | |
OLD | NEW |