| 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 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 using base::PLATFORM_FILE_READ; | 33 using base::PLATFORM_FILE_READ; |
| 34 using base::PLATFORM_FILE_WRITE; | 34 using base::PLATFORM_FILE_WRITE; |
| 35 using base::ReadPlatformFile; | 35 using base::ReadPlatformFile; |
| 36 using base::TaskRunner; | 36 using base::TaskRunner; |
| 37 using base::Time; | 37 using base::Time; |
| 38 using base::TruncatePlatformFile; | 38 using base::TruncatePlatformFile; |
| 39 using base::WritePlatformFile; | 39 using base::WritePlatformFile; |
| 40 | 40 |
| 41 namespace disk_cache { | 41 namespace disk_cache { |
| 42 | 42 |
| 43 using simple_util::ConvertEntryHashKeyToHexString; |
| 44 using simple_util::GetEntryHashKeyAsHexString; |
| 45 using simple_util::GetFilenameFromHexStringAndIndex; |
| 43 using simple_util::GetFilenameFromKeyAndIndex; | 46 using simple_util::GetFilenameFromKeyAndIndex; |
| 44 using simple_util::GetDataSizeFromKeyAndFileSize; | 47 using simple_util::GetDataSizeFromKeyAndFileSize; |
| 45 using simple_util::GetFileSizeFromKeyAndDataSize; | 48 using simple_util::GetFileSizeFromKeyAndDataSize; |
| 46 using simple_util::GetFileOffsetFromKeyAndDataOffset; | 49 using simple_util::GetFileOffsetFromKeyAndDataOffset; |
| 47 | 50 |
| 48 // static | 51 // static |
| 49 void SimpleSynchronousEntry::OpenEntry( | 52 void SimpleSynchronousEntry::OpenEntry( |
| 50 const FilePath& path, | 53 const FilePath& path, |
| 51 const std::string& key, | 54 const std::string& key, |
| 52 const scoped_refptr<TaskRunner>& callback_runner, | 55 const scoped_refptr<TaskRunner>& callback_runner, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 75 if (rv != net::ERR_FILE_EXISTS) { | 78 if (rv != net::ERR_FILE_EXISTS) { |
| 76 sync_entry->Doom(); | 79 sync_entry->Doom(); |
| 77 } | 80 } |
| 78 delete sync_entry; | 81 delete sync_entry; |
| 79 sync_entry = NULL; | 82 sync_entry = NULL; |
| 80 } | 83 } |
| 81 callback_runner->PostTask(FROM_HERE, base::Bind(callback, sync_entry)); | 84 callback_runner->PostTask(FROM_HERE, base::Bind(callback, sync_entry)); |
| 82 } | 85 } |
| 83 | 86 |
| 84 // static | 87 // static |
| 88 bool SimpleSynchronousEntry::DeleteFilesForEntry(const FilePath& path, |
| 89 const std::string& hash_key) { |
| 90 bool result = true; |
| 91 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 92 FilePath to_delete = path.AppendASCII( |
| 93 GetFilenameFromHexStringAndIndex(hash_key, i)); |
| 94 if (!file_util::Delete(to_delete, false)) { |
| 95 result = false; |
| 96 DLOG(ERROR) << "Could not delete " << to_delete.MaybeAsASCII(); |
| 97 } |
| 98 } |
| 99 return result; |
| 100 } |
| 101 |
| 102 // static |
| 85 void SimpleSynchronousEntry::DoomEntry( | 103 void SimpleSynchronousEntry::DoomEntry( |
| 86 const FilePath& path, | 104 const FilePath& path, |
| 87 const std::string& key, | 105 const std::string& key, |
| 88 const scoped_refptr<TaskRunner>& callback_runner, | 106 const scoped_refptr<TaskRunner>& callback_runner, |
| 89 const net::CompletionCallback& callback) { | 107 const net::CompletionCallback& callback) { |
| 90 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 108 bool deleted_well = DeleteFilesForEntry(path, |
| 91 FilePath to_delete = path.AppendASCII(GetFilenameFromKeyAndIndex(key, i)); | 109 GetEntryHashKeyAsHexString(key)); |
| 92 bool ALLOW_UNUSED result = file_util::Delete(to_delete, false); | 110 int result = deleted_well ? net::OK : net::ERR_FAILED; |
| 93 DLOG_IF(ERROR, !result) << "Could not delete " << to_delete.MaybeAsASCII(); | 111 callback_runner->PostTask(FROM_HERE, base::Bind(callback, result)); |
| 94 } | 112 } |
| 113 |
| 114 // static |
| 115 void SimpleSynchronousEntry::DoomEntrySet( |
| 116 scoped_ptr<std::vector<uint64> > key_hashes, |
| 117 const FilePath& path, |
| 118 scoped_refptr<TaskRunner> callback_runner, |
| 119 const net::CompletionCallback& callback) { |
| 120 bool deleted_well = true; |
| 121 for (std::vector<uint64>::const_iterator it = key_hashes->begin(), |
| 122 end = key_hashes->end(); it != end; ++it) |
| 123 deleted_well &= DeleteFilesForEntry(path, |
| 124 ConvertEntryHashKeyToHexString((*it))); |
| 125 int result = deleted_well ? net::OK : net::ERR_FAILED; |
| 95 if (!callback.is_null()) | 126 if (!callback.is_null()) |
| 96 callback_runner->PostTask(FROM_HERE, base::Bind(callback, net::OK)); | 127 callback_runner->PostTask(FROM_HERE, base::Bind(callback, result)); |
| 97 } | 128 } |
| 98 | 129 |
| 99 void SimpleSynchronousEntry::Close() { | 130 void SimpleSynchronousEntry::Close() { |
| 100 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 131 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 101 bool ALLOW_UNUSED result = ClosePlatformFile(files_[i]); | 132 bool ALLOW_UNUSED result = ClosePlatformFile(files_[i]); |
| 102 DLOG_IF(INFO, !result) << "Could not Close() file."; | 133 DLOG_IF(INFO, !result) << "Could not Close() file."; |
| 103 } | 134 } |
| 104 delete this; | 135 delete this; |
| 105 } | 136 } |
| 106 | 137 |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 DLOG(WARNING) << "Could not write keys to new cache entry."; | 325 DLOG(WARNING) << "Could not write keys to new cache entry."; |
| 295 return net::ERR_FAILED; | 326 return net::ERR_FAILED; |
| 296 } | 327 } |
| 297 } | 328 } |
| 298 initialized_ = true; | 329 initialized_ = true; |
| 299 return net::OK; | 330 return net::OK; |
| 300 } | 331 } |
| 301 | 332 |
| 302 void SimpleSynchronousEntry::Doom() { | 333 void SimpleSynchronousEntry::Doom() { |
| 303 // TODO(gavinp): Consider if we should guard against redundant Doom() calls. | 334 // TODO(gavinp): Consider if we should guard against redundant Doom() calls. |
| 304 DoomEntry(path_, key_, | 335 DeleteFilesForEntry(path_, GetEntryHashKeyAsHexString(key_)); |
| 305 scoped_refptr<base::TaskRunner>(), net::CompletionCallback()); | |
| 306 } | 336 } |
| 307 | 337 |
| 308 } // namespace disk_cache | 338 } // namespace disk_cache |
| OLD | NEW |