| 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_file.h" | 5 #include "net/disk_cache/simple/simple_index_file.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 index_filename.DirName().AppendASCII("index_temp"); | 44 index_filename.DirName().AppendASCII("index_temp"); |
| 45 int bytes_written = file_util::WriteFile( | 45 int bytes_written = file_util::WriteFile( |
| 46 temp_filename, | 46 temp_filename, |
| 47 reinterpret_cast<const char*>(pickle->data()), | 47 reinterpret_cast<const char*>(pickle->data()), |
| 48 pickle->size()); | 48 pickle->size()); |
| 49 DCHECK_EQ(bytes_written, implicit_cast<int>(pickle->size())); | 49 DCHECK_EQ(bytes_written, implicit_cast<int>(pickle->size())); |
| 50 if (bytes_written != static_cast<int>(pickle->size())) { | 50 if (bytes_written != static_cast<int>(pickle->size())) { |
| 51 // TODO(felipeg): Add better error handling. | 51 // TODO(felipeg): Add better error handling. |
| 52 LOG(ERROR) << "Could not write Simple Cache index to temporary file: " | 52 LOG(ERROR) << "Could not write Simple Cache index to temporary file: " |
| 53 << temp_filename.value(); | 53 << temp_filename.value(); |
| 54 file_util::Delete(temp_filename, /* recursive = */ false); | 54 base::Delete(temp_filename, /* recursive = */ false); |
| 55 } else { | 55 } else { |
| 56 // Swap temp and index_file. | 56 // Swap temp and index_file. |
| 57 bool result = file_util::ReplaceFile(temp_filename, index_filename); | 57 bool result = file_util::ReplaceFile(temp_filename, index_filename); |
| 58 DCHECK(result); | 58 DCHECK(result); |
| 59 } | 59 } |
| 60 if (app_on_background) { | 60 if (app_on_background) { |
| 61 UMA_HISTOGRAM_TIMES("SimpleCache.IndexWriteToDiskTime.Background", | 61 UMA_HISTOGRAM_TIMES("SimpleCache.IndexWriteToDiskTime.Background", |
| 62 (base::TimeTicks::Now() - start_time)); | 62 (base::TimeTicks::Now() - start_time)); |
| 63 } else { | 63 } else { |
| 64 UMA_HISTOGRAM_TIMES("SimpleCache.IndexWriteToDiskTime.Foreground", | 64 UMA_HISTOGRAM_TIMES("SimpleCache.IndexWriteToDiskTime.Foreground", |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 // If not true, we need to restore the index. | 171 // If not true, we need to restore the index. |
| 172 return index_mtime < dir_mtime; | 172 return index_mtime < dir_mtime; |
| 173 } | 173 } |
| 174 | 174 |
| 175 // static | 175 // static |
| 176 scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::LoadFromDisk( | 176 scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::LoadFromDisk( |
| 177 const base::FilePath& index_filename) { | 177 const base::FilePath& index_filename) { |
| 178 std::string contents; | 178 std::string contents; |
| 179 if (!file_util::ReadFileToString(index_filename, &contents)) { | 179 if (!file_util::ReadFileToString(index_filename, &contents)) { |
| 180 LOG(WARNING) << "Could not read Simple Index file."; | 180 LOG(WARNING) << "Could not read Simple Index file."; |
| 181 file_util::Delete(index_filename, false); | 181 base::Delete(index_filename, false); |
| 182 return scoped_ptr<SimpleIndex::EntrySet>(); | 182 return scoped_ptr<SimpleIndex::EntrySet>(); |
| 183 } | 183 } |
| 184 | 184 |
| 185 scoped_ptr<SimpleIndex::EntrySet> entries = | 185 scoped_ptr<SimpleIndex::EntrySet> entries = |
| 186 SimpleIndexFile::Deserialize(contents.data(), contents.size()); | 186 SimpleIndexFile::Deserialize(contents.data(), contents.size()); |
| 187 if (!entries) { | 187 if (!entries) { |
| 188 file_util::Delete(index_filename, false); | 188 base::Delete(index_filename, false); |
| 189 return scoped_ptr<SimpleIndex::EntrySet>(); | 189 return scoped_ptr<SimpleIndex::EntrySet>(); |
| 190 } | 190 } |
| 191 | 191 |
| 192 return entries.Pass(); | 192 return entries.Pass(); |
| 193 } | 193 } |
| 194 | 194 |
| 195 // static | 195 // static |
| 196 scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::Deserialize(const char* data, | 196 scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::Deserialize(const char* data, |
| 197 int data_len) { | 197 int data_len) { |
| 198 DCHECK(data); | 198 DCHECK(data); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 base::Bind(completion_callback, | 324 base::Bind(completion_callback, |
| 325 base::Passed(&index_file_entries), | 325 base::Passed(&index_file_entries), |
| 326 force_index_flush)); | 326 force_index_flush)); |
| 327 } | 327 } |
| 328 | 328 |
| 329 // static | 329 // static |
| 330 scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::RestoreFromDisk( | 330 scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::RestoreFromDisk( |
| 331 const base::FilePath& index_file_path) { | 331 const base::FilePath& index_file_path) { |
| 332 LOG(INFO) << "Simple Cache Index is being restored from disk."; | 332 LOG(INFO) << "Simple Cache Index is being restored from disk."; |
| 333 | 333 |
| 334 file_util::Delete(index_file_path, /* recursive = */ false); | 334 base::Delete(index_file_path, /* recursive = */ false); |
| 335 scoped_ptr<SimpleIndex::EntrySet> index_file_entries( | 335 scoped_ptr<SimpleIndex::EntrySet> index_file_entries( |
| 336 new SimpleIndex::EntrySet()); | 336 new SimpleIndex::EntrySet()); |
| 337 | 337 |
| 338 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. | 338 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. |
| 339 COMPILE_ASSERT(kSimpleEntryFileCount == 3, | 339 COMPILE_ASSERT(kSimpleEntryFileCount == 3, |
| 340 file_pattern_must_match_file_count); | 340 file_pattern_must_match_file_count); |
| 341 | 341 |
| 342 const int kFileSuffixLength = sizeof("_0") - 1; | 342 const int kFileSuffixLength = sizeof("_0") - 1; |
| 343 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]"); | 343 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]"); |
| 344 base::FileEnumerator enumerator(index_file_path.DirName(), | 344 base::FileEnumerator enumerator(index_file_path.DirName(), |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 index_file_entries.get()); | 380 index_file_entries.get()); |
| 381 } else { | 381 } else { |
| 382 // Summing up the total size of the entry through all the *_[0-2] files | 382 // Summing up the total size of the entry through all the *_[0-2] files |
| 383 it->second.SetEntrySize(it->second.GetEntrySize() + file_size); | 383 it->second.SetEntrySize(it->second.GetEntrySize() + file_size); |
| 384 } | 384 } |
| 385 } | 385 } |
| 386 return index_file_entries.Pass(); | 386 return index_file_entries.Pass(); |
| 387 } | 387 } |
| 388 | 388 |
| 389 } // namespace disk_cache | 389 } // namespace disk_cache |
| OLD | NEW |