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