| 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 <functional> | 9 #include <functional> | 
| 10 #include <limits> | 10 #include <limits> | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 24 #include "net/base/hash_value.h" | 24 #include "net/base/hash_value.h" | 
| 25 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" | 
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" | 
| 27 #include "net/disk_cache/simple/simple_backend_version.h" | 27 #include "net/disk_cache/simple/simple_backend_version.h" | 
| 28 #include "net/disk_cache/simple/simple_experiment.h" | 28 #include "net/disk_cache/simple/simple_experiment.h" | 
| 29 #include "net/disk_cache/simple/simple_histogram_enums.h" | 29 #include "net/disk_cache/simple/simple_histogram_enums.h" | 
| 30 #include "net/disk_cache/simple/simple_histogram_macros.h" | 30 #include "net/disk_cache/simple/simple_histogram_macros.h" | 
| 31 #include "net/disk_cache/simple/simple_util.h" | 31 #include "net/disk_cache/simple/simple_util.h" | 
| 32 #include "third_party/zlib/zlib.h" | 32 #include "third_party/zlib/zlib.h" | 
| 33 | 33 | 
| 34 using base::File; |  | 
| 35 using base::FilePath; | 34 using base::FilePath; | 
| 36 using base::Time; | 35 using base::Time; | 
| 37 | 36 | 
| 38 namespace disk_cache { | 37 namespace disk_cache { | 
| 39 | 38 | 
| 40 namespace { | 39 namespace { | 
| 41 | 40 | 
| 42 void RecordSyncOpenResult(net::CacheType cache_type, | 41 void RecordSyncOpenResult(net::CacheType cache_type, | 
| 43                           OpenEntryResult result, | 42                           OpenEntryResult result, | 
| 44                           bool had_index) { | 43                           bool had_index) { | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 82   SIMPLE_CACHE_UMA(BOOLEAN, "SyncOpenDidPrefetch", cache_type, result); | 81   SIMPLE_CACHE_UMA(BOOLEAN, "SyncOpenDidPrefetch", cache_type, result); | 
| 83 } | 82 } | 
| 84 | 83 | 
| 85 bool CanOmitEmptyFile(int file_index) { | 84 bool CanOmitEmptyFile(int file_index) { | 
| 86   DCHECK_GE(file_index, 0); | 85   DCHECK_GE(file_index, 0); | 
| 87   DCHECK_LT(file_index, kSimpleEntryFileCount); | 86   DCHECK_LT(file_index, kSimpleEntryFileCount); | 
| 88   return file_index == simple_util::GetFileIndexFromStreamIndex(2); | 87   return file_index == simple_util::GetFileIndexFromStreamIndex(2); | 
| 89 } | 88 } | 
| 90 | 89 | 
| 91 bool TruncatePath(const FilePath& filename_to_truncate) { | 90 bool TruncatePath(const FilePath& filename_to_truncate) { | 
| 92   File file_to_truncate; | 91   base::File file_to_truncate; | 
| 93   int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE | | 92   int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_W
      RITE | | 
| 94               File::FLAG_SHARE_DELETE; | 93               base::File::FLAG_SHARE_DELETE; | 
| 95   file_to_truncate.Initialize(filename_to_truncate, flags); | 94   file_to_truncate.Initialize(filename_to_truncate, flags); | 
| 96   if (!file_to_truncate.IsValid()) | 95   if (!file_to_truncate.IsValid()) | 
| 97     return false; | 96     return false; | 
| 98   if (!file_to_truncate.SetLength(0)) | 97   if (!file_to_truncate.SetLength(0)) | 
| 99     return false; | 98     return false; | 
| 100   return true; | 99   return true; | 
| 101 } | 100 } | 
| 102 | 101 | 
| 103 void CalculateSHA256OfKey(const std::string& key, | 102 void CalculateSHA256OfKey(const std::string& key, | 
| 104                           net::SHA256HashValue* out_hash_value) { | 103                           net::SHA256HashValue* out_hash_value) { | 
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 389     // Don't create a new file if the entry has been doomed, to avoid it being | 388     // Don't create a new file if the entry has been doomed, to avoid it being | 
| 390     // mixed up with a newly-created entry with the same key. | 389     // mixed up with a newly-created entry with the same key. | 
| 391     if (doomed) { | 390     if (doomed) { | 
| 392       DLOG(WARNING) << "Rejecting write to lazily omitted stream " | 391       DLOG(WARNING) << "Rejecting write to lazily omitted stream " | 
| 393                     << in_entry_op.index << " of doomed cache entry."; | 392                     << in_entry_op.index << " of doomed cache entry."; | 
| 394       RecordWriteResult(cache_type_, | 393       RecordWriteResult(cache_type_, | 
| 395                         SYNC_WRITE_RESULT_LAZY_STREAM_ENTRY_DOOMED); | 394                         SYNC_WRITE_RESULT_LAZY_STREAM_ENTRY_DOOMED); | 
| 396       *out_result = net::ERR_CACHE_WRITE_FAILURE; | 395       *out_result = net::ERR_CACHE_WRITE_FAILURE; | 
| 397       return; | 396       return; | 
| 398     } | 397     } | 
| 399     File::Error error; | 398     base::File::Error error; | 
| 400     if (!MaybeCreateFile(file_index, FILE_REQUIRED, &error)) { | 399     if (!MaybeCreateFile(file_index, FILE_REQUIRED, &error)) { | 
| 401       RecordWriteResult(cache_type_, SYNC_WRITE_RESULT_LAZY_CREATE_FAILURE); | 400       RecordWriteResult(cache_type_, SYNC_WRITE_RESULT_LAZY_CREATE_FAILURE); | 
| 402       Doom(); | 401       Doom(); | 
| 403       *out_result = net::ERR_CACHE_WRITE_FAILURE; | 402       *out_result = net::ERR_CACHE_WRITE_FAILURE; | 
| 404       return; | 403       return; | 
| 405     } | 404     } | 
| 406     CreateEntryResult result; | 405     CreateEntryResult result; | 
| 407     if (!InitializeCreatedFile(file_index, &result)) { | 406     if (!InitializeCreatedFile(file_index, &result)) { | 
| 408       RecordWriteResult(cache_type_, SYNC_WRITE_RESULT_LAZY_INITIALIZE_FAILURE); | 407       RecordWriteResult(cache_type_, SYNC_WRITE_RESULT_LAZY_INITIALIZE_FAILURE); | 
| 409       Doom(); | 408       Doom(); | 
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 833 } | 832 } | 
| 834 | 833 | 
| 835 SimpleSynchronousEntry::~SimpleSynchronousEntry() { | 834 SimpleSynchronousEntry::~SimpleSynchronousEntry() { | 
| 836   DCHECK(!(have_open_files_ && initialized_)); | 835   DCHECK(!(have_open_files_ && initialized_)); | 
| 837   if (have_open_files_) | 836   if (have_open_files_) | 
| 838     CloseFiles(); | 837     CloseFiles(); | 
| 839 } | 838 } | 
| 840 | 839 | 
| 841 bool SimpleSynchronousEntry::MaybeOpenFile( | 840 bool SimpleSynchronousEntry::MaybeOpenFile( | 
| 842     int file_index, | 841     int file_index, | 
| 843     File::Error* out_error) { | 842     base::File::Error* out_error) { | 
| 844   DCHECK(out_error); | 843   DCHECK(out_error); | 
| 845 | 844 | 
| 846   FilePath filename = GetFilenameFromFileIndex(file_index); | 845   FilePath filename = GetFilenameFromFileIndex(file_index); | 
| 847   int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE | | 846   int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_W
      RITE | | 
| 848               File::FLAG_SHARE_DELETE; | 847               base::File::FLAG_SHARE_DELETE; | 
| 849   files_[file_index].Initialize(filename, flags); | 848   files_[file_index].Initialize(filename, flags); | 
| 850   *out_error = files_[file_index].error_details(); | 849   *out_error = files_[file_index].error_details(); | 
| 851 | 850 | 
| 852   if (CanOmitEmptyFile(file_index) && !files_[file_index].IsValid() && | 851   if (CanOmitEmptyFile(file_index) && !files_[file_index].IsValid() && | 
| 853       *out_error == File::FILE_ERROR_NOT_FOUND) { | 852       *out_error == base::File::FILE_ERROR_NOT_FOUND) { | 
| 854     empty_file_omitted_[file_index] = true; | 853     empty_file_omitted_[file_index] = true; | 
| 855     return true; | 854     return true; | 
| 856   } | 855   } | 
| 857 | 856 | 
| 858   return files_[file_index].IsValid(); | 857   return files_[file_index].IsValid(); | 
| 859 } | 858 } | 
| 860 | 859 | 
| 861 bool SimpleSynchronousEntry::MaybeCreateFile( | 860 bool SimpleSynchronousEntry::MaybeCreateFile( | 
| 862     int file_index, | 861     int file_index, | 
| 863     FileRequired file_required, | 862     FileRequired file_required, | 
| 864     File::Error* out_error) { | 863     base::File::Error* out_error) { | 
| 865   DCHECK(out_error); | 864   DCHECK(out_error); | 
| 866 | 865 | 
| 867   if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) { | 866   if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) { | 
| 868     empty_file_omitted_[file_index] = true; | 867     empty_file_omitted_[file_index] = true; | 
| 869     return true; | 868     return true; | 
| 870   } | 869   } | 
| 871 | 870 | 
| 872   FilePath filename = GetFilenameFromFileIndex(file_index); | 871   FilePath filename = GetFilenameFromFileIndex(file_index); | 
| 873   int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE | | 872   int flags = base::File::FLAG_CREATE | base::File::FLAG_READ | base::File::FLAG
      _WRITE | | 
| 874               File::FLAG_SHARE_DELETE; | 873               base::File::FLAG_SHARE_DELETE; | 
| 875   files_[file_index].Initialize(filename, flags); | 874   files_[file_index].Initialize(filename, flags); | 
| 876 | 875 | 
| 877   // It's possible that the creation failed because someone deleted the | 876   // It's possible that the creation failed because someone deleted the | 
| 878   // directory (e.g. because someone pressed "clear cache" on Android). | 877   // directory (e.g. because someone pressed "clear cache" on Android). | 
| 879   // If so, we would keep failing for a while until periodic index snapshot | 878   // If so, we would keep failing for a while until periodic index snapshot | 
| 880   // re-creates the cache dir, so try to recover from it quickly here. | 879   // re-creates the cache dir, so try to recover from it quickly here. | 
| 881   if (!files_[file_index].IsValid() && | 880   if (!files_[file_index].IsValid() && | 
| 882       files_[file_index].error_details() == File::FILE_ERROR_NOT_FOUND && | 881       files_[file_index].error_details() == base::File::FILE_ERROR_NOT_FOUND && | 
| 883       !base::DirectoryExists(path_)) { | 882       !base::DirectoryExists(path_)) { | 
| 884     if (base::CreateDirectory(path_)) | 883     if (base::CreateDirectory(path_)) | 
| 885       files_[file_index].Initialize(filename, flags); | 884       files_[file_index].Initialize(filename, flags); | 
| 886   } | 885   } | 
| 887 | 886 | 
| 888   *out_error = files_[file_index].error_details(); | 887   *out_error = files_[file_index].error_details(); | 
| 889   empty_file_omitted_[file_index] = false; | 888   empty_file_omitted_[file_index] = false; | 
| 890 | 889 | 
| 891   return files_[file_index].IsValid(); | 890   return files_[file_index].IsValid(); | 
| 892 } | 891 } | 
| 893 | 892 | 
| 894 bool SimpleSynchronousEntry::OpenFiles(SimpleEntryStat* out_entry_stat) { | 893 bool SimpleSynchronousEntry::OpenFiles(SimpleEntryStat* out_entry_stat) { | 
| 895   for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 894   for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 
| 896     File::Error error; | 895     base::File::Error error; | 
| 897     if (!MaybeOpenFile(i, &error)) { | 896     if (!MaybeOpenFile(i, &error)) { | 
| 898       // TODO(juliatuttle,gavinp): Remove one each of these triplets of | 897       // TODO(juliatuttle,gavinp): Remove one each of these triplets of | 
| 899       // histograms. We can calculate the third as the sum or difference of the | 898       // histograms. We can calculate the third as the sum or difference of the | 
| 900       // other two. | 899       // other two. | 
| 901       RecordSyncOpenResult(cache_type_, OPEN_ENTRY_PLATFORM_FILE_ERROR, | 900       RecordSyncOpenResult(cache_type_, OPEN_ENTRY_PLATFORM_FILE_ERROR, | 
| 902                            had_index_); | 901                            had_index_); | 
| 903       SIMPLE_CACHE_UMA(ENUMERATION, | 902       SIMPLE_CACHE_UMA(ENUMERATION, | 
| 904                        "SyncOpenPlatformFileError", cache_type_, | 903                        "SyncOpenPlatformFileError", cache_type_, | 
| 905                        -error, -base::File::FILE_ERROR_MAX); | 904                        -error, -base::File::FILE_ERROR_MAX); | 
| 906       if (had_index_) { | 905       if (had_index_) { | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
| 921 | 920 | 
| 922   have_open_files_ = true; | 921   have_open_files_ = true; | 
| 923 | 922 | 
| 924   base::TimeDelta entry_age = base::Time::Now() - base::Time::UnixEpoch(); | 923   base::TimeDelta entry_age = base::Time::Now() - base::Time::UnixEpoch(); | 
| 925   for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 924   for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 
| 926     if (empty_file_omitted_[i]) { | 925     if (empty_file_omitted_[i]) { | 
| 927       out_entry_stat->set_data_size(i + 1, 0); | 926       out_entry_stat->set_data_size(i + 1, 0); | 
| 928       continue; | 927       continue; | 
| 929     } | 928     } | 
| 930 | 929 | 
| 931     File::Info file_info; | 930     base::File::Info file_info; | 
| 932     bool success = files_[i].GetInfo(&file_info); | 931     bool success = files_[i].GetInfo(&file_info); | 
| 933     base::Time file_last_modified; | 932     base::Time file_last_modified; | 
| 934     if (!success) { | 933     if (!success) { | 
| 935       DLOG(WARNING) << "Could not get platform file info."; | 934       DLOG(WARNING) << "Could not get platform file info."; | 
| 936       continue; | 935       continue; | 
| 937     } | 936     } | 
| 938     out_entry_stat->set_last_used(file_info.last_accessed); | 937     out_entry_stat->set_last_used(file_info.last_accessed); | 
| 939     out_entry_stat->set_last_modified(file_info.last_modified); | 938     out_entry_stat->set_last_modified(file_info.last_modified); | 
| 940 | 939 | 
| 941     base::TimeDelta stream_age = | 940     base::TimeDelta stream_age = | 
| (...skipping 25 matching lines...) Expand all  Loading... | 
| 967                    "SyncOpenEntryAge", cache_type_, | 966                    "SyncOpenEntryAge", cache_type_, | 
| 968                    entry_age.InHours(), 1, 1000, 50); | 967                    entry_age.InHours(), 1, 1000, 50); | 
| 969 | 968 | 
| 970   files_created_ = false; | 969   files_created_ = false; | 
| 971 | 970 | 
| 972   return true; | 971   return true; | 
| 973 } | 972 } | 
| 974 | 973 | 
| 975 bool SimpleSynchronousEntry::CreateFiles(SimpleEntryStat* out_entry_stat) { | 974 bool SimpleSynchronousEntry::CreateFiles(SimpleEntryStat* out_entry_stat) { | 
| 976   for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 975   for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 
| 977     File::Error error; | 976     base::File::Error error; | 
| 978     if (!MaybeCreateFile(i, FILE_NOT_REQUIRED, &error)) { | 977     if (!MaybeCreateFile(i, FILE_NOT_REQUIRED, &error)) { | 
| 979       // TODO(juliatuttle,gavinp): Remove one each of these triplets of | 978       // TODO(juliatuttle,gavinp): Remove one each of these triplets of | 
| 980       // histograms. We can calculate the third as the sum or difference of the | 979       // histograms. We can calculate the third as the sum or difference of the | 
| 981       // other two. | 980       // other two. | 
| 982       RecordSyncCreateResult(CREATE_ENTRY_PLATFORM_FILE_ERROR, had_index_); | 981       RecordSyncCreateResult(CREATE_ENTRY_PLATFORM_FILE_ERROR, had_index_); | 
| 983       SIMPLE_CACHE_UMA(ENUMERATION, | 982       SIMPLE_CACHE_UMA(ENUMERATION, | 
| 984                        "SyncCreatePlatformFileError", cache_type_, | 983                        "SyncCreatePlatformFileError", cache_type_, | 
| 985                        -error, -base::File::FILE_ERROR_MAX); | 984                        -error, -base::File::FILE_ERROR_MAX); | 
| 986       if (had_index_) { | 985       if (had_index_) { | 
| 987         SIMPLE_CACHE_UMA(ENUMERATION, | 986         SIMPLE_CACHE_UMA(ENUMERATION, | 
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1442   return path_.AppendASCII( | 1441   return path_.AppendASCII( | 
| 1443       GetFilenameFromEntryHashAndFileIndex(entry_hash_, file_index)); | 1442       GetFilenameFromEntryHashAndFileIndex(entry_hash_, file_index)); | 
| 1444 } | 1443 } | 
| 1445 | 1444 | 
| 1446 bool SimpleSynchronousEntry::OpenSparseFileIfExists( | 1445 bool SimpleSynchronousEntry::OpenSparseFileIfExists( | 
| 1447     int32_t* out_sparse_data_size) { | 1446     int32_t* out_sparse_data_size) { | 
| 1448   DCHECK(!sparse_file_open()); | 1447   DCHECK(!sparse_file_open()); | 
| 1449 | 1448 | 
| 1450   FilePath filename = path_.AppendASCII( | 1449   FilePath filename = path_.AppendASCII( | 
| 1451       GetSparseFilenameFromEntryHash(entry_hash_)); | 1450       GetSparseFilenameFromEntryHash(entry_hash_)); | 
| 1452   int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE | | 1451   int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_W
      RITE | | 
| 1453               File::FLAG_SHARE_DELETE; | 1452               base::File::FLAG_SHARE_DELETE; | 
| 1454   sparse_file_.Initialize(filename, flags); | 1453   sparse_file_.Initialize(filename, flags); | 
| 1455   if (sparse_file_.IsValid()) | 1454   if (sparse_file_.IsValid()) | 
| 1456     return ScanSparseFile(out_sparse_data_size); | 1455     return ScanSparseFile(out_sparse_data_size); | 
| 1457 | 1456 | 
| 1458   return sparse_file_.error_details() == File::FILE_ERROR_NOT_FOUND; | 1457   return sparse_file_.error_details() == base::File::FILE_ERROR_NOT_FOUND; | 
| 1459 } | 1458 } | 
| 1460 | 1459 | 
| 1461 bool SimpleSynchronousEntry::CreateSparseFile() { | 1460 bool SimpleSynchronousEntry::CreateSparseFile() { | 
| 1462   DCHECK(!sparse_file_open()); | 1461   DCHECK(!sparse_file_open()); | 
| 1463 | 1462 | 
| 1464   FilePath filename = path_.AppendASCII( | 1463   FilePath filename = path_.AppendASCII( | 
| 1465       GetSparseFilenameFromEntryHash(entry_hash_)); | 1464       GetSparseFilenameFromEntryHash(entry_hash_)); | 
| 1466   int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE | | 1465   int flags = base::File::FLAG_CREATE | base::File::FLAG_READ | base::File::FLAG
      _WRITE | | 
| 1467               File::FLAG_SHARE_DELETE; | 1466               base::File::FLAG_SHARE_DELETE; | 
| 1468   sparse_file_.Initialize(filename, flags); | 1467   sparse_file_.Initialize(filename, flags); | 
| 1469   if (!sparse_file_.IsValid()) | 1468   if (!sparse_file_.IsValid()) | 
| 1470     return false; | 1469     return false; | 
| 1471 | 1470 | 
| 1472   return InitializeSparseFile(); | 1471   return InitializeSparseFile(); | 
| 1473 } | 1472 } | 
| 1474 | 1473 | 
| 1475 void SimpleSynchronousEntry::CloseSparseFile() { | 1474 void SimpleSynchronousEntry::CloseSparseFile() { | 
| 1476   DCHECK(sparse_file_open()); | 1475   DCHECK(sparse_file_open()); | 
| 1477   sparse_file_.Close(); | 1476   sparse_file_.Close(); | 
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1687   range.offset = offset; | 1686   range.offset = offset; | 
| 1688   range.length = len; | 1687   range.length = len; | 
| 1689   range.data_crc32 = data_crc32; | 1688   range.data_crc32 = data_crc32; | 
| 1690   range.file_offset = data_file_offset; | 1689   range.file_offset = data_file_offset; | 
| 1691   sparse_ranges_.insert(std::make_pair(offset, range)); | 1690   sparse_ranges_.insert(std::make_pair(offset, range)); | 
| 1692 | 1691 | 
| 1693   return true; | 1692   return true; | 
| 1694 } | 1693 } | 
| 1695 | 1694 | 
| 1696 }  // namespace disk_cache | 1695 }  // namespace disk_cache | 
| OLD | NEW | 
|---|