Chromium Code Reviews| 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_index.h" | 5 #include "net/disk_cache/simple/simple_index.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 return true; | 30 return true; |
| 31 } | 31 } |
| 32 | 32 |
| 33 bool CheckHeader(disk_cache::SimpleIndexFile::Header header) { | 33 bool CheckHeader(disk_cache::SimpleIndexFile::Header header) { |
| 34 return header.number_of_entries <= kMaxEntiresInIndex && | 34 return header.number_of_entries <= kMaxEntiresInIndex && |
| 35 header.initial_magic_number == | 35 header.initial_magic_number == |
| 36 disk_cache::kSimpleIndexInitialMagicNumber && | 36 disk_cache::kSimpleIndexInitialMagicNumber && |
| 37 header.version == disk_cache::kSimpleVersion; | 37 header.version == disk_cache::kSimpleVersion; |
| 38 } | 38 } |
| 39 | 39 |
| 40 class FileAutoCloser { | |
| 41 public: | |
| 42 explicit FileAutoCloser(const base::PlatformFile& file) : file_(file) { } | |
| 43 ~FileAutoCloser() { | |
| 44 base::ClosePlatformFile(file_); | |
| 45 } | |
| 46 private: | |
| 47 base::PlatformFile file_; | |
| 48 DISALLOW_COPY_AND_ASSIGN(FileAutoCloser); | |
| 49 }; | |
| 50 | |
| 40 } // namespace | 51 } // namespace |
| 41 | 52 |
| 42 namespace disk_cache { | 53 namespace disk_cache { |
| 43 | 54 |
| 44 SimpleIndex::SimpleIndex( | 55 SimpleIndex::SimpleIndex( |
| 45 const scoped_refptr<base::TaskRunner>& cache_thread, | 56 const scoped_refptr<base::TaskRunner>& cache_thread, |
| 57 const scoped_refptr<base::TaskRunner>& io_thread, | |
| 46 const base::FilePath& path) | 58 const base::FilePath& path) |
| 47 : path_(path), | 59 : cache_size_(0), |
| 48 cache_thread_(cache_thread) { | 60 initialized_(false), |
| 49 index_filename_ = path_.AppendASCII("index"); | 61 index_filename_(path.AppendASCII("simple-index")), |
| 62 cache_thread_(cache_thread), | |
| 63 io_thread_(io_thread) {} | |
| 64 | |
| 65 void SimpleIndex::Initialize() { | |
| 66 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 67 MergeCallback merge_callback = base::Bind(&SimpleIndex::MergeInitializingSet, | |
| 68 this->AsWeakPtr()); | |
| 69 base::WorkerPool::PostTask(FROM_HERE, | |
| 70 base::Bind(&SimpleIndex::LoadFromDisk, | |
| 71 index_filename_, | |
| 72 io_thread_, | |
| 73 merge_callback), | |
| 74 true); | |
| 50 } | 75 } |
| 51 | 76 |
| 52 bool SimpleIndex::Initialize() { | 77 // static |
| 53 if (!OpenIndexFile()) | 78 void SimpleIndex::LoadFromDisk( |
| 54 return RestoreFromDisk(); | 79 const base::FilePath& index_filename, |
| 80 const scoped_refptr<base::TaskRunner>& io_thread, | |
| 81 const MergeCallback& merge_callback) { | |
| 82 // Open the index file. | |
| 83 base::PlatformFileError error; | |
| 84 base::PlatformFile index_file = base::CreatePlatformFile( | |
| 85 index_filename, | |
| 86 base::PLATFORM_FILE_OPEN_ALWAYS | | |
| 87 base::PLATFORM_FILE_READ | | |
| 88 base::PLATFORM_FILE_WRITE, | |
| 89 NULL, | |
| 90 &error); | |
| 91 FileAutoCloser auto_close_index_file(index_file); | |
| 92 if (error != base::PLATFORM_FILE_OK) { | |
| 93 LOG(ERROR) << "Error opening file " << index_filename.value(); | |
| 94 return RestoreFromDisk(index_filename, io_thread, merge_callback); | |
| 95 } | |
| 96 | |
| 55 uLong incremental_crc = crc32(0L, Z_NULL, 0); | 97 uLong incremental_crc = crc32(0L, Z_NULL, 0); |
| 56 int64 index_file_offset = 0; | 98 int64 index_file_offset = 0; |
| 57 SimpleIndexFile::Header header; | 99 SimpleIndexFile::Header header; |
| 58 if (base::ReadPlatformFile(index_file_, | 100 if (base::ReadPlatformFile(index_file, |
| 59 index_file_offset, | 101 index_file_offset, |
| 60 reinterpret_cast<char*>(&header), | 102 reinterpret_cast<char*>(&header), |
| 61 sizeof(header)) != sizeof(header)) { | 103 sizeof(header)) != sizeof(header)) { |
| 62 return RestoreFromDisk(); | 104 return RestoreFromDisk(index_filename, io_thread, merge_callback); |
| 63 } | 105 } |
| 64 index_file_offset += sizeof(header); | 106 index_file_offset += sizeof(header); |
| 65 incremental_crc = crc32(incremental_crc, | 107 incremental_crc = crc32(incremental_crc, |
| 66 reinterpret_cast<const Bytef*>(&header), | 108 reinterpret_cast<const Bytef*>(&header), |
| 67 implicit_cast<uInt>(sizeof(header))); | 109 implicit_cast<uInt>(sizeof(header))); |
| 68 | 110 |
| 69 if (!CheckHeader(header)) { | 111 if (!CheckHeader(header)) { |
| 70 LOG(ERROR) << "Invalid header on Simple Cache Index."; | 112 LOG(ERROR) << "Invalid header on Simple Cache Index."; |
| 71 return RestoreFromDisk(); | 113 return RestoreFromDisk(index_filename, io_thread, merge_callback); |
| 72 } | 114 } |
| 73 | 115 |
| 74 const int entries_buffer_size = | 116 const int entries_buffer_size = |
| 75 header.number_of_entries * SimpleIndexFile::kEntryMetadataSize; | 117 header.number_of_entries * SimpleIndexFile::kEntryMetadataSize; |
| 76 | 118 |
| 77 scoped_ptr<char[]> entries_buffer(new char[entries_buffer_size]); | 119 scoped_ptr<char[]> entries_buffer(new char[entries_buffer_size]); |
| 78 if (base::ReadPlatformFile(index_file_, | 120 if (base::ReadPlatformFile(index_file, |
| 79 index_file_offset, | 121 index_file_offset, |
| 80 entries_buffer.get(), | 122 entries_buffer.get(), |
| 81 entries_buffer_size) != entries_buffer_size) { | 123 entries_buffer_size) != entries_buffer_size) { |
| 82 return RestoreFromDisk(); | 124 return RestoreFromDisk(index_filename, io_thread, merge_callback); |
| 83 } | 125 } |
| 84 index_file_offset += entries_buffer_size; | 126 index_file_offset += entries_buffer_size; |
| 85 incremental_crc = crc32(incremental_crc, | 127 incremental_crc = crc32(incremental_crc, |
| 86 reinterpret_cast<const Bytef*>(entries_buffer.get()), | 128 reinterpret_cast<const Bytef*>(entries_buffer.get()), |
| 87 implicit_cast<uInt>(entries_buffer_size)); | 129 implicit_cast<uInt>(entries_buffer_size)); |
| 88 | 130 |
| 89 SimpleIndexFile::Footer footer; | 131 SimpleIndexFile::Footer footer; |
| 90 if (base::ReadPlatformFile(index_file_, | 132 if (base::ReadPlatformFile(index_file, |
| 91 index_file_offset, | 133 index_file_offset, |
| 92 reinterpret_cast<char*>(&footer), | 134 reinterpret_cast<char*>(&footer), |
| 93 sizeof(footer)) != sizeof(footer)) { | 135 sizeof(footer)) != sizeof(footer)) { |
| 94 return RestoreFromDisk(); | 136 return RestoreFromDisk(index_filename, io_thread, merge_callback); |
| 95 } | 137 } |
| 96 const uint32 crc_read = footer.crc; | 138 const uint32 crc_read = footer.crc; |
| 97 const uint32 crc_calculated = incremental_crc; | 139 const uint32 crc_calculated = incremental_crc; |
| 98 if (crc_read != crc_calculated) | 140 if (crc_read != crc_calculated) |
| 99 return RestoreFromDisk(); | 141 return RestoreFromDisk(index_filename, io_thread, merge_callback); |
| 100 | 142 |
| 143 scoped_ptr<EntrySet> index_file_entries(new EntrySet()); | |
| 101 int entries_buffer_offset = 0; | 144 int entries_buffer_offset = 0; |
| 102 while(entries_buffer_offset < entries_buffer_size) { | 145 while(entries_buffer_offset < entries_buffer_size) { |
| 103 SimpleIndexFile::EntryMetadata entry_metadata; | 146 SimpleIndexFile::EntryMetadata entry_metadata; |
| 104 SimpleIndexFile::EntryMetadata::DeSerialize( | 147 SimpleIndexFile::EntryMetadata::DeSerialize( |
| 105 &entries_buffer.get()[entries_buffer_offset], &entry_metadata); | 148 &entries_buffer.get()[entries_buffer_offset], &entry_metadata); |
| 106 InsertInternal(entry_metadata); | 149 InsertInternal(index_file_entries.get(), entry_metadata); |
| 107 entries_buffer_offset += SimpleIndexFile::kEntryMetadataSize; | 150 entries_buffer_offset += SimpleIndexFile::kEntryMetadataSize; |
| 108 } | 151 } |
| 109 DCHECK_EQ(header.number_of_entries, entries_set_.size()); | 152 DCHECK_EQ(header.number_of_entries, index_file_entries->size()); |
| 110 CloseIndexFile(); | 153 |
| 111 return true; | 154 io_thread->PostTask(FROM_HERE, |
| 155 base::Bind(merge_callback, | |
| 156 base::Passed(&index_file_entries))); | |
| 112 } | 157 } |
| 113 | 158 |
| 114 void SimpleIndex::Insert(const std::string& key) { | 159 void SimpleIndex::Insert(const std::string& key) { |
| 160 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 115 // Upon insert we don't know yet the size of the entry. | 161 // Upon insert we don't know yet the size of the entry. |
| 116 // It will be updated later when the SimpleEntryImpl finishes opening or | 162 // It will be updated later when the SimpleEntryImpl finishes opening or |
| 117 // creating the new entry, and then UpdateEntrySize will be called. | 163 // creating the new entry, and then UpdateEntrySize will be called. |
| 118 InsertInternal(SimpleIndexFile::EntryMetadata(GetEntryHashForKey(key), | 164 const std::string hash_key = GetEntryHashForKey(key); |
| 119 base::Time::Now(), 0)); | 165 InsertInternal(&entries_set_, SimpleIndexFile::EntryMetadata( |
| 166 hash_key, | |
| 167 base::Time::Now(), 0)); | |
| 168 if (!initialized_) | |
| 169 removed_entries_.erase(hash_key); | |
| 120 } | 170 } |
| 121 | 171 |
| 122 void SimpleIndex::Remove(const std::string& key) { | 172 void SimpleIndex::Remove(const std::string& key) { |
| 173 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 123 UpdateEntrySize(key, 0); | 174 UpdateEntrySize(key, 0); |
| 124 entries_set_.erase(GetEntryHashForKey(key)); | 175 const std::string hash_key = GetEntryHashForKey(key); |
| 176 entries_set_.erase(hash_key); | |
| 177 | |
| 178 if (!initialized_) | |
| 179 removed_entries_.insert(hash_key); | |
| 125 } | 180 } |
| 126 | 181 |
| 127 bool SimpleIndex::Has(const std::string& key) const { | 182 bool SimpleIndex::Has(const std::string& key) const { |
| 128 return entries_set_.count(GetEntryHashForKey(key)) != 0; | 183 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 184 // If not initialized, always return true, forcing it to go to the disk. | |
| 185 return !initialized_ || entries_set_.count(GetEntryHashForKey(key)) != 0; | |
| 129 } | 186 } |
| 130 | 187 |
| 131 bool SimpleIndex::UseIfExists(const std::string& key) { | 188 bool SimpleIndex::UseIfExists(const std::string& key) { |
| 189 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 190 // Always update the last used time, even if it is during initialization. | |
| 191 // It will be merged later. | |
| 132 EntrySet::iterator it = entries_set_.find(GetEntryHashForKey(key)); | 192 EntrySet::iterator it = entries_set_.find(GetEntryHashForKey(key)); |
| 133 if (it == entries_set_.end()) | 193 if (it == entries_set_.end()) |
| 134 return false; | 194 // If not initialized, always return true, forcing it to go to the disk. |
| 195 return !initialized_; | |
| 135 it->second.SetLastUsedTime(base::Time::Now()); | 196 it->second.SetLastUsedTime(base::Time::Now()); |
| 136 return true; | 197 return true; |
| 137 } | 198 } |
| 138 | 199 |
| 139 bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) { | 200 bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) { |
| 201 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 140 EntrySet::iterator it = entries_set_.find(GetEntryHashForKey(key)); | 202 EntrySet::iterator it = entries_set_.find(GetEntryHashForKey(key)); |
| 141 if (it == entries_set_.end()) | 203 if (it == entries_set_.end()) |
| 142 return false; | 204 return false; |
| 143 | 205 |
| 144 // Update the total cache size with the new entry size. | 206 // Update the total cache size with the new entry size. |
| 145 cache_size_ -= it->second.entry_size; | 207 cache_size_ -= it->second.entry_size; |
| 146 cache_size_ += entry_size; | 208 cache_size_ += entry_size; |
| 147 it->second.entry_size = entry_size; | 209 it->second.entry_size = entry_size; |
| 148 | 210 |
| 149 return true; | 211 return true; |
| 150 } | 212 } |
| 151 | 213 |
| 214 // static | |
| 152 void SimpleIndex::InsertInternal( | 215 void SimpleIndex::InsertInternal( |
| 216 EntrySet* entry_set, | |
| 153 const SimpleIndexFile::EntryMetadata& entry_metadata) { | 217 const SimpleIndexFile::EntryMetadata& entry_metadata) { |
| 154 entries_set_.insert(make_pair(entry_metadata.GetHashKey(), entry_metadata)); | 218 // TODO(felipeg): Use a hash_set instead of a hash_map. |
| 219 DCHECK(entry_set); | |
| 220 entry_set->insert(make_pair(entry_metadata.GetHashKey(), entry_metadata)); | |
| 155 } | 221 } |
| 156 | 222 |
| 157 bool SimpleIndex::RestoreFromDisk() { | 223 // static |
| 224 void SimpleIndex::RestoreFromDisk( | |
| 225 const base::FilePath& index_filename, | |
| 226 const scoped_refptr<base::TaskRunner>& io_thread, | |
| 227 const MergeCallback& merge_callback) { | |
| 158 using file_util::FileEnumerator; | 228 using file_util::FileEnumerator; |
| 159 LOG(INFO) << "Simple Cache Index is being restored from disk."; | 229 LOG(INFO) << "Simple Cache Index is being restored from disk."; |
| 160 CloseIndexFile(); | 230 |
| 161 file_util::Delete(index_filename_, /* recursive = */ false); | 231 file_util::Delete(index_filename, /* recursive = */ false); |
| 162 entries_set_.clear(); | 232 scoped_ptr<EntrySet> index_file_entries(new EntrySet()); |
| 163 | 233 |
| 164 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. | 234 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. |
| 165 COMPILE_ASSERT(kSimpleEntryFileCount == 3, | 235 COMPILE_ASSERT(kSimpleEntryFileCount == 3, |
| 166 file_pattern_must_match_file_count); | 236 file_pattern_must_match_file_count); |
| 167 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]"); | 237 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]"); |
| 168 FileEnumerator enumerator(path_, | 238 FileEnumerator enumerator(index_filename.DirName(), |
| 169 false /* recursive */, | 239 false /* recursive */, |
| 170 FileEnumerator::FILES, | 240 FileEnumerator::FILES, |
| 171 file_pattern); | 241 file_pattern); |
| 172 | |
| 173 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); | 242 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); |
| 174 file_path = enumerator.Next()) { | 243 file_path = enumerator.Next()) { |
| 175 const base::FilePath::StringType base_name = file_path.BaseName().value(); | 244 const base::FilePath::StringType base_name = file_path.BaseName().value(); |
| 176 // Converting to std::string is OK since we never use UTF8 wide chars in our | 245 // Converting to std::string is OK since we never use UTF8 wide chars in our |
| 177 // file names. | 246 // file names. |
| 178 const std::string hash_name(base_name.begin(), base_name.end()); | 247 const std::string hash_name(base_name.begin(), base_name.end()); |
| 179 const std::string hash_key = hash_name.substr(0, kEntryHashKeySize); | 248 const std::string hash_key = hash_name.substr(0, kEntryHashKeySize); |
| 180 | 249 |
| 181 FileEnumerator::FindInfo find_info = {}; | 250 FileEnumerator::FindInfo find_info = {}; |
| 182 enumerator.GetFindInfo(&find_info); | 251 enumerator.GetFindInfo(&find_info); |
| 183 base::Time last_used_time; | 252 base::Time last_used_time; |
| 184 #if defined(OS_POSIX) | 253 #if defined(OS_POSIX) |
| 185 // For POSIX systems, a last access time is available. However, it's not | 254 // For POSIX systems, a last access time is available. However, it's not |
| 186 // guaranteed to be more accurate than mtime. It is no worse though. | 255 // guaranteed to be more accurate than mtime. It is no worse though. |
| 187 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime); | 256 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime); |
| 188 #endif | 257 #endif |
| 189 if (last_used_time.is_null()) | 258 if (last_used_time.is_null()) |
| 190 last_used_time = FileEnumerator::GetLastModifiedTime(find_info); | 259 last_used_time = FileEnumerator::GetLastModifiedTime(find_info); |
| 191 | 260 |
| 192 int64 file_size = FileEnumerator::GetFilesize(find_info); | 261 int64 file_size = FileEnumerator::GetFilesize(find_info); |
| 193 EntrySet::iterator it = entries_set_.find(hash_key); | 262 EntrySet::iterator it = index_file_entries->find(hash_key); |
| 194 if (it == entries_set_.end()) { | 263 if (it == index_file_entries->end()) { |
| 195 InsertInternal(SimpleIndexFile::EntryMetadata( | 264 InsertInternal(index_file_entries.get(), SimpleIndexFile::EntryMetadata( |
| 196 hash_key, last_used_time, file_size)); | 265 hash_key, last_used_time, file_size)); |
| 197 } else { | 266 } else { |
| 198 // Summing up the total size of the entry through all the *_[0-2] files | 267 // Summing up the total size of the entry through all the *_[0-2] files |
| 199 it->second.entry_size += file_size; | 268 it->second.entry_size += file_size; |
| 200 } | 269 } |
| 201 } | 270 } |
| 202 | 271 |
| 203 // TODO(felipeg): Detect unrecoverable problems and return false here. | 272 io_thread->PostTask(FROM_HERE, |
| 204 return true; | 273 base::Bind(merge_callback, |
| 274 base::Passed(&index_file_entries))); | |
| 275 } | |
| 276 | |
| 277 void SimpleIndex::MergeInitializingSet( | |
| 278 scoped_ptr<EntrySet> index_file_entries) { | |
| 279 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 280 // First, remove the entries that are in the |removed_entries_| from both | |
| 281 // sets. | |
| 282 for (base::hash_set<std::string>::const_iterator it = | |
| 283 removed_entries_.begin(); it != removed_entries_.end(); ++it) { | |
| 284 entries_set_.erase(*it); | |
| 285 index_file_entries->erase(*it); | |
| 286 } | |
| 287 | |
| 288 // Recalculate the cache size while merging the two sets. | |
| 289 cache_size_ = 0; | |
| 290 for (EntrySet::const_iterator it = index_file_entries->begin(); | |
| 291 it != index_file_entries->end(); ++it) { | |
| 292 // If there is already an entry in the current entries_set_, we need to | |
| 293 // merge the new data there with the data loaded in the initialization. | |
| 294 EntrySet::iterator current_entry = entries_set_.find(it->first); | |
| 295 if (current_entry != entries_set_.end()) { | |
| 296 // When Merging, existing valid data in the |current_entry| will prevail. | |
| 297 SimpleIndexFile::EntryMetadata::Merge( | |
| 298 it->second, &(current_entry->second)); | |
| 299 cache_size_ += current_entry->second.entry_size; | |
| 300 } else { | |
| 301 InsertInternal(&entries_set_, it->second); | |
| 302 cache_size_ += it->second.entry_size; | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 initialized_ = true; | |
| 205 } | 307 } |
| 206 | 308 |
| 207 void SimpleIndex::Serialize(std::string* out_buffer) { | 309 void SimpleIndex::Serialize(std::string* out_buffer) { |
| 310 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 208 DCHECK(out_buffer); | 311 DCHECK(out_buffer); |
| 209 SimpleIndexFile::Header header; | 312 SimpleIndexFile::Header header; |
| 210 SimpleIndexFile::Footer footer; | 313 SimpleIndexFile::Footer footer; |
| 211 | 314 |
| 212 header.initial_magic_number = kSimpleIndexInitialMagicNumber; | 315 header.initial_magic_number = kSimpleIndexInitialMagicNumber; |
| 213 header.version = kSimpleVersion; | 316 header.version = kSimpleVersion; |
| 214 header.number_of_entries = entries_set_.size(); | 317 header.number_of_entries = entries_set_.size(); |
| 215 | 318 |
| 216 out_buffer->reserve(sizeof(header) + | 319 out_buffer->reserve(sizeof(header) + |
| 217 kEntryHashKeySize * entries_set_.size() + | 320 kEntryHashKeySize * entries_set_.size() + |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 228 } | 331 } |
| 229 | 332 |
| 230 // Then, CRC. | 333 // Then, CRC. |
| 231 footer.crc = crc32(crc32(0, Z_NULL, 0), | 334 footer.crc = crc32(crc32(0, Z_NULL, 0), |
| 232 reinterpret_cast<const Bytef*>(out_buffer->data()), | 335 reinterpret_cast<const Bytef*>(out_buffer->data()), |
| 233 implicit_cast<uInt>(out_buffer->size())); | 336 implicit_cast<uInt>(out_buffer->size())); |
| 234 | 337 |
| 235 out_buffer->append(reinterpret_cast<const char*>(&footer), sizeof(footer)); | 338 out_buffer->append(reinterpret_cast<const char*>(&footer), sizeof(footer)); |
| 236 } | 339 } |
| 237 | 340 |
| 238 void SimpleIndex::Cleanup() { | 341 void SimpleIndex::WriteToDisk() { |
| 342 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 239 scoped_ptr<std::string> buffer(new std::string()); | 343 scoped_ptr<std::string> buffer(new std::string()); |
| 240 Serialize(buffer.get()); | 344 Serialize(buffer.get()); |
| 241 cache_thread_->PostTask(FROM_HERE, | 345 cache_thread_->PostTask(FROM_HERE, base::Bind( |
| 242 base::Bind(&SimpleIndex::UpdateFile, | 346 &SimpleIndex::UpdateFile, |
| 243 index_filename_, | 347 index_filename_, |
| 244 path_.AppendASCII("index_temp"), | 348 index_filename_.DirName().AppendASCII("index_temp"), |
| 245 base::Passed(&buffer))); | 349 base::Passed(&buffer))); |
| 246 } | 350 } |
| 247 | 351 |
| 248 SimpleIndex::~SimpleIndex() { | 352 SimpleIndex::~SimpleIndex() { |
|
gavinp
2013/04/11 11:59:26
Missed one for the DCHECK. Also see the ordering.
felipeg
2013/04/11 12:12:54
Done.
| |
| 249 CloseIndexFile(); | |
| 250 } | |
| 251 | |
| 252 bool SimpleIndex::OpenIndexFile() { | |
| 253 base::PlatformFileError error; | |
| 254 index_file_ = base::CreatePlatformFile(index_filename_, | |
| 255 base::PLATFORM_FILE_OPEN_ALWAYS | | |
| 256 base::PLATFORM_FILE_READ | | |
| 257 base::PLATFORM_FILE_WRITE, | |
| 258 NULL, | |
| 259 &error); | |
| 260 if (error != base::PLATFORM_FILE_OK) { | |
| 261 LOG(ERROR) << "Error opening file " << index_filename_.value(); | |
| 262 return false; | |
| 263 } | |
| 264 return true; | |
| 265 } | |
| 266 | |
| 267 bool SimpleIndex::CloseIndexFile() { | |
| 268 return base::ClosePlatformFile(index_file_); | |
| 269 } | 353 } |
| 270 | 354 |
| 271 // static | 355 // static |
| 272 void SimpleIndex::UpdateFile(const base::FilePath& index_filename, | 356 void SimpleIndex::UpdateFile(const base::FilePath& index_filename, |
| 273 const base::FilePath& temp_filename, | 357 const base::FilePath& temp_filename, |
| 274 scoped_ptr<std::string> buffer) { | 358 scoped_ptr<std::string> buffer) { |
| 275 int bytes_written = file_util::WriteFile( | 359 int bytes_written = file_util::WriteFile( |
| 276 temp_filename, buffer->data(), buffer->size()); | 360 temp_filename, buffer->data(), buffer->size()); |
| 277 DCHECK_EQ(bytes_written, implicit_cast<int>(buffer->size())); | 361 DCHECK_EQ(bytes_written, implicit_cast<int>(buffer->size())); |
| 278 if (bytes_written != static_cast<int>(buffer->size())) { | 362 if (bytes_written != static_cast<int>(buffer->size())) { |
| 279 // TODO(felipeg): Add better error handling. | 363 // TODO(felipeg): Add better error handling. |
| 280 LOG(ERROR) << "Could not write Simple Cache index to temporary file: " | 364 LOG(ERROR) << "Could not write Simple Cache index to temporary file: " |
| 281 << temp_filename.value(); | 365 << temp_filename.value(); |
| 282 file_util::Delete(temp_filename, /* recursive = */ false); | 366 file_util::Delete(temp_filename, /* recursive = */ false); |
| 283 return; | 367 return; |
| 284 } | 368 } |
| 285 // Swap temp and index_file. | 369 // Swap temp and index_file. |
| 286 bool result = file_util::ReplaceFile(temp_filename, index_filename); | 370 bool result = file_util::ReplaceFile(temp_filename, index_filename); |
| 287 DCHECK(result); | 371 DCHECK(result); |
| 288 } | 372 } |
| 289 | 373 |
| 290 } // namespace disk_cache | 374 } // namespace disk_cache |
| OLD | NEW |