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