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