OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "net/disk_cache/simple/simple_synchronous_entry.h" | 5 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/hash.h" | 12 #include "base/hash.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/message_loop_proxy.h" | 14 #include "base/message_loop_proxy.h" |
15 #include "base/sha1.h" | 15 #include "base/sha1.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
17 #include "base/task_runner.h" | 17 #include "base/task_runner.h" |
18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
20 #include "net/disk_cache/simple/simple_disk_format.h" | |
21 | 20 |
22 using base::ClosePlatformFile; | 21 using base::ClosePlatformFile; |
23 using base::GetPlatformFileInfo; | 22 using base::GetPlatformFileInfo; |
24 using base::PlatformFileError; | 23 using base::PlatformFileError; |
25 using base::PlatformFileInfo; | 24 using base::PlatformFileInfo; |
26 using base::PLATFORM_FILE_CREATE_ALWAYS; | 25 using base::PLATFORM_FILE_CREATE_ALWAYS; |
27 using base::PLATFORM_FILE_OK; | 26 using base::PLATFORM_FILE_OK; |
28 using base::PLATFORM_FILE_OPEN; | 27 using base::PLATFORM_FILE_OPEN; |
29 using base::PLATFORM_FILE_READ; | 28 using base::PLATFORM_FILE_READ; |
30 using base::PLATFORM_FILE_WRITE; | 29 using base::PLATFORM_FILE_WRITE; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 base::Bind(callback, sync_entry, | 94 base::Bind(callback, sync_entry, |
96 sync_entry ? net::OK : net::ERR_FAILED)); | 95 sync_entry ? net::OK : net::ERR_FAILED)); |
97 } | 96 } |
98 | 97 |
99 // static | 98 // static |
100 void SimpleSynchronousEntry::DoomEntry( | 99 void SimpleSynchronousEntry::DoomEntry( |
101 const FilePath& path, | 100 const FilePath& path, |
102 const std::string& key, | 101 const std::string& key, |
103 scoped_refptr<TaskRunner> callback_runner, | 102 scoped_refptr<TaskRunner> callback_runner, |
104 const net::CompletionCallback& callback) { | 103 const net::CompletionCallback& callback) { |
105 for (int i = 0; i < kIndexCount; ++i) { | 104 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
106 bool delete_result = | 105 bool delete_result = |
107 file_util::Delete(path.AppendASCII(GetFilenameForKeyAndIndex(key, i)), | 106 file_util::Delete(path.AppendASCII(GetFilenameForKeyAndIndex(key, i)), |
108 false); | 107 false); |
109 DCHECK(delete_result); | 108 DCHECK(delete_result); |
110 } | 109 } |
111 if (!callback.is_null()) | 110 if (!callback.is_null()) |
112 callback_runner->PostTask(FROM_HERE, base::Bind(callback, net::OK)); | 111 callback_runner->PostTask(FROM_HERE, base::Bind(callback, net::OK)); |
113 } | 112 } |
114 | 113 |
115 void SimpleSynchronousEntry::DoomAndClose() { | 114 void SimpleSynchronousEntry::DoomAndClose() { |
116 scoped_refptr<TaskRunner> callback_runner = callback_runner_; | 115 scoped_refptr<TaskRunner> callback_runner = callback_runner_; |
117 FilePath path = path_; | 116 FilePath path = path_; |
118 std::string key = key_; | 117 std::string key = key_; |
119 | 118 |
120 Close(); | 119 Close(); |
121 // |this| is now deleted. | 120 // |this| is now deleted. |
122 | 121 |
123 DoomEntry(path, key, callback_runner, net::CompletionCallback()); | 122 DoomEntry(path, key, callback_runner, net::CompletionCallback()); |
124 } | 123 } |
125 | 124 |
126 void SimpleSynchronousEntry::Close() { | 125 void SimpleSynchronousEntry::Close() { |
127 for (int i = 0; i < kIndexCount; ++i) { | 126 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
128 bool result = ClosePlatformFile(files_[i]); | 127 bool result = ClosePlatformFile(files_[i]); |
129 DCHECK(result); | 128 DCHECK(result); |
130 } | 129 } |
131 delete this; | 130 delete this; |
132 } | 131 } |
133 | 132 |
134 void SimpleSynchronousEntry::ReadData( | 133 void SimpleSynchronousEntry::ReadData( |
135 int index, | 134 int index, |
136 int offset, | 135 int offset, |
137 net::IOBuffer* buf, | 136 net::IOBuffer* buf, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 const std::string& key) : callback_runner_(callback_runner), | 201 const std::string& key) : callback_runner_(callback_runner), |
203 path_(path), | 202 path_(path), |
204 key_(key), | 203 key_(key), |
205 initialized_(false) { | 204 initialized_(false) { |
206 } | 205 } |
207 | 206 |
208 SimpleSynchronousEntry::~SimpleSynchronousEntry() { | 207 SimpleSynchronousEntry::~SimpleSynchronousEntry() { |
209 } | 208 } |
210 | 209 |
211 bool SimpleSynchronousEntry::OpenOrCreateFiles(bool create) { | 210 bool SimpleSynchronousEntry::OpenOrCreateFiles(bool create) { |
212 for (int i = 0; i < kIndexCount; ++i) { | 211 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
213 FilePath filename = path_.AppendASCII(GetFilenameForKeyAndIndex(key_, i)); | 212 FilePath filename = path_.AppendASCII(GetFilenameForKeyAndIndex(key_, i)); |
214 int flags = PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; | 213 int flags = PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; |
215 if (create) | 214 if (create) |
216 flags |= PLATFORM_FILE_CREATE_ALWAYS; | 215 flags |= PLATFORM_FILE_CREATE_ALWAYS; |
217 else | 216 else |
218 flags |= PLATFORM_FILE_OPEN; | 217 flags |= PLATFORM_FILE_OPEN; |
219 PlatformFileError error; | 218 PlatformFileError error; |
220 files_[i] = CreatePlatformFile(filename, flags, NULL, &error); | 219 files_[i] = CreatePlatformFile(filename, flags, NULL, &error); |
221 if (error != PLATFORM_FILE_OK) { | 220 if (error != PLATFORM_FILE_OK) { |
222 while (--i >= 0) { | 221 while (--i >= 0) { |
223 bool did_close = ClosePlatformFile(files_[i]); | 222 bool did_close = ClosePlatformFile(files_[i]); |
224 DCHECK(did_close); | 223 DCHECK(did_close); |
225 } | 224 } |
226 return false; | 225 return false; |
227 } | 226 } |
228 } | 227 } |
229 | 228 |
230 for (int i = 0; i < kIndexCount; ++i) { | 229 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
231 PlatformFileInfo file_info; | 230 PlatformFileInfo file_info; |
232 CHECK(GetPlatformFileInfo(files_[i], &file_info)); | 231 CHECK(GetPlatformFileInfo(files_[i], &file_info)); |
233 last_used_ = std::max(last_used_, file_info.last_accessed); | 232 last_used_ = std::max(last_used_, file_info.last_accessed); |
234 last_modified_ = std::max(last_modified_, file_info.last_modified); | 233 last_modified_ = std::max(last_modified_, file_info.last_modified); |
235 data_size_[i] = DataSizeFromKeyAndFileSize(key_.size(), file_info.size); | 234 data_size_[i] = DataSizeFromKeyAndFileSize(key_.size(), file_info.size); |
236 } | 235 } |
237 | 236 |
238 return true; | 237 return true; |
239 } | 238 } |
240 | 239 |
241 bool SimpleSynchronousEntry::InitializeForOpen() { | 240 bool SimpleSynchronousEntry::InitializeForOpen() { |
242 DCHECK(!initialized_); | 241 DCHECK(!initialized_); |
243 if (!OpenOrCreateFiles(false)) | 242 if (!OpenOrCreateFiles(false)) |
244 return false; | 243 return false; |
245 | 244 |
246 for (int i = 0; i < kIndexCount; ++i) { | 245 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
247 SimpleFileHeader header; | 246 SimpleFileHeader header; |
248 if (ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), | 247 if (ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), |
249 sizeof(header)) != sizeof(header)) { | 248 sizeof(header)) != sizeof(header)) { |
250 return false; | 249 return false; |
251 } | 250 } |
252 | 251 |
253 if (header.initial_magic_number != kSimpleInitialMagicNumber) { | 252 if (header.initial_magic_number != kSimpleInitialMagicNumber) { |
254 // TODO(gavinp): This seems very bad; for now we log at WARNING, but we | 253 // TODO(gavinp): This seems very bad; for now we log at WARNING, but we |
255 // should give consideration to not saturating the log with these if that | 254 // should give consideration to not saturating the log with these if that |
256 // becomes a problem. | 255 // becomes a problem. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 return true; | 293 return true; |
295 } | 294 } |
296 | 295 |
297 bool SimpleSynchronousEntry::InitializeForCreate() { | 296 bool SimpleSynchronousEntry::InitializeForCreate() { |
298 DCHECK(!initialized_); | 297 DCHECK(!initialized_); |
299 if (!OpenOrCreateFiles(true)) { | 298 if (!OpenOrCreateFiles(true)) { |
300 LOG(ERROR) << "Could not create platform files for key " << key_; | 299 LOG(ERROR) << "Could not create platform files for key " << key_; |
301 return false; | 300 return false; |
302 } | 301 } |
303 | 302 |
304 for (int i = 0; i < kIndexCount; ++i) { | 303 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
305 SimpleFileHeader header; | 304 SimpleFileHeader header; |
306 header.initial_magic_number = kSimpleInitialMagicNumber; | 305 header.initial_magic_number = kSimpleInitialMagicNumber; |
307 header.version = kSimpleVersion; | 306 header.version = kSimpleVersion; |
308 | 307 |
309 header.key_length = key_.size(); | 308 header.key_length = key_.size(); |
310 header.key_hash = base::Hash(key_); | 309 header.key_hash = base::Hash(key_); |
311 | 310 |
312 if (WritePlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), | 311 if (WritePlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), |
313 sizeof(header)) != sizeof(header)) { | 312 sizeof(header)) != sizeof(header)) { |
314 // TODO(gavinp): Clean up created files. | 313 // TODO(gavinp): Clean up created files. |
315 LOG(WARNING) << "Could not write headers to new cache entry."; | 314 LOG(WARNING) << "Could not write headers to new cache entry."; |
316 return false; | 315 return false; |
317 } | 316 } |
318 | 317 |
319 if (WritePlatformFile(files_[i], sizeof(header), key_.data(), | 318 if (WritePlatformFile(files_[i], sizeof(header), key_.data(), |
320 key_.size()) != implicit_cast<int>(key_.size())) { | 319 key_.size()) != implicit_cast<int>(key_.size())) { |
321 // TODO(gavinp): Clean up created files. | 320 // TODO(gavinp): Clean up created files. |
322 LOG(WARNING) << "Could not write keys to new cache entry."; | 321 LOG(WARNING) << "Could not write keys to new cache entry."; |
323 return false; | 322 return false; |
324 } | 323 } |
325 } | 324 } |
326 | 325 |
327 initialized_ = true; | 326 initialized_ = true; |
328 return true; | 327 return true; |
329 } | 328 } |
330 | 329 |
331 } // namespace disk_cache | 330 } // namespace disk_cache |
OLD | NEW |