| 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_util.h" | 5 #include "net/disk_cache/simple/simple_util.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
| 13 #include "base/sha1.h" | 13 #include "base/sha1.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "net/disk_cache/simple/simple_entry_format.h" | 18 #include "net/disk_cache/simple/simple_entry_format.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // Size of the uint64 hash_key number in Hex format in a string. | 22 // Size of the uint64_t hash_key number in Hex format in a string. |
| 23 const size_t kEntryHashKeyAsHexStringSize = 2 * sizeof(uint64); | 23 const size_t kEntryHashKeyAsHexStringSize = 2 * sizeof(uint64_t); |
| 24 | 24 |
| 25 #if defined(OS_POSIX) | 25 #if defined(OS_POSIX) |
| 26 // TODO(clamy, gavinp): this should go in base | 26 // TODO(clamy, gavinp): this should go in base |
| 27 bool GetNanoSecsFromStat(const struct stat& st, | 27 bool GetNanoSecsFromStat(const struct stat& st, |
| 28 time_t* out_sec, | 28 time_t* out_sec, |
| 29 long* out_nsec) { | 29 long* out_nsec) { |
| 30 #if defined(OS_ANDROID) | 30 #if defined(OS_ANDROID) |
| 31 *out_sec = st.st_mtime; | 31 *out_sec = st.st_mtime; |
| 32 *out_nsec = st.st_mtime_nsec; | 32 *out_nsec = st.st_mtime_nsec; |
| 33 return true; | 33 return true; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 44 #endif | 44 #endif |
| 45 } | 45 } |
| 46 #endif // defined(OS_POSIX) | 46 #endif // defined(OS_POSIX) |
| 47 | 47 |
| 48 } // namespace | 48 } // namespace |
| 49 | 49 |
| 50 namespace disk_cache { | 50 namespace disk_cache { |
| 51 | 51 |
| 52 namespace simple_util { | 52 namespace simple_util { |
| 53 | 53 |
| 54 std::string ConvertEntryHashKeyToHexString(uint64 hash_key) { | 54 std::string ConvertEntryHashKeyToHexString(uint64_t hash_key) { |
| 55 const std::string hash_key_str = base::StringPrintf("%016" PRIx64, hash_key); | 55 const std::string hash_key_str = base::StringPrintf("%016" PRIx64, hash_key); |
| 56 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size()); | 56 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size()); |
| 57 return hash_key_str; | 57 return hash_key_str; |
| 58 } | 58 } |
| 59 | 59 |
| 60 std::string GetEntryHashKeyAsHexString(const std::string& key) { | 60 std::string GetEntryHashKeyAsHexString(const std::string& key) { |
| 61 std::string hash_key_str = | 61 std::string hash_key_str = |
| 62 ConvertEntryHashKeyToHexString(GetEntryHashKey(key)); | 62 ConvertEntryHashKeyToHexString(GetEntryHashKey(key)); |
| 63 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size()); | 63 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size()); |
| 64 return hash_key_str; | 64 return hash_key_str; |
| 65 } | 65 } |
| 66 | 66 |
| 67 bool GetEntryHashKeyFromHexString(const base::StringPiece& hash_key, | 67 bool GetEntryHashKeyFromHexString(const base::StringPiece& hash_key, |
| 68 uint64* hash_key_out) { | 68 uint64_t* hash_key_out) { |
| 69 if (hash_key.size() != kEntryHashKeyAsHexStringSize) { | 69 if (hash_key.size() != kEntryHashKeyAsHexStringSize) { |
| 70 return false; | 70 return false; |
| 71 } | 71 } |
| 72 return base::HexStringToUInt64(hash_key, hash_key_out); | 72 return base::HexStringToUInt64(hash_key, hash_key_out); |
| 73 } | 73 } |
| 74 | 74 |
| 75 uint64 GetEntryHashKey(const std::string& key) { | 75 uint64_t GetEntryHashKey(const std::string& key) { |
| 76 union { | 76 union { |
| 77 unsigned char sha_hash[base::kSHA1Length]; | 77 unsigned char sha_hash[base::kSHA1Length]; |
| 78 uint64 key_hash; | 78 uint64_t key_hash; |
| 79 } u; | 79 } u; |
| 80 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(key.data()), | 80 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(key.data()), |
| 81 key.size(), u.sha_hash); | 81 key.size(), u.sha_hash); |
| 82 return u.key_hash; | 82 return u.key_hash; |
| 83 } | 83 } |
| 84 | 84 |
| 85 std::string GetFilenameFromEntryHashAndFileIndex(uint64 entry_hash, | 85 std::string GetFilenameFromEntryHashAndFileIndex(uint64_t entry_hash, |
| 86 int file_index) { | 86 int file_index) { |
| 87 return base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, file_index); | 87 return base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, file_index); |
| 88 } | 88 } |
| 89 | 89 |
| 90 std::string GetSparseFilenameFromEntryHash(uint64 entry_hash) { | 90 std::string GetSparseFilenameFromEntryHash(uint64_t entry_hash) { |
| 91 return base::StringPrintf("%016" PRIx64 "_s", entry_hash); | 91 return base::StringPrintf("%016" PRIx64 "_s", entry_hash); |
| 92 } | 92 } |
| 93 | 93 |
| 94 std::string GetFilenameFromKeyAndFileIndex(const std::string& key, | 94 std::string GetFilenameFromKeyAndFileIndex(const std::string& key, |
| 95 int file_index) { | 95 int file_index) { |
| 96 return GetEntryHashKeyAsHexString(key) + | 96 return GetEntryHashKeyAsHexString(key) + |
| 97 base::StringPrintf("_%1d", file_index); | 97 base::StringPrintf("_%1d", file_index); |
| 98 } | 98 } |
| 99 | 99 |
| 100 int32 GetDataSizeFromKeyAndFileSize(const std::string& key, int64 file_size) { | 100 int32_t GetDataSizeFromKeyAndFileSize(const std::string& key, |
| 101 int64 data_size = file_size - key.size() - sizeof(SimpleFileHeader) - | 101 int64_t file_size) { |
| 102 sizeof(SimpleFileEOF); | 102 int64_t data_size = |
| 103 return base::checked_cast<int32>(data_size); | 103 file_size - key.size() - sizeof(SimpleFileHeader) - sizeof(SimpleFileEOF); |
| 104 return base::checked_cast<int32_t>(data_size); |
| 104 } | 105 } |
| 105 | 106 |
| 106 int64 GetFileSizeFromKeyAndDataSize(const std::string& key, int32 data_size) { | 107 int64_t GetFileSizeFromKeyAndDataSize(const std::string& key, |
| 108 int32_t data_size) { |
| 107 return data_size + key.size() + sizeof(SimpleFileHeader) + | 109 return data_size + key.size() + sizeof(SimpleFileHeader) + |
| 108 sizeof(SimpleFileEOF); | 110 sizeof(SimpleFileEOF); |
| 109 } | 111 } |
| 110 | 112 |
| 111 int GetFileIndexFromStreamIndex(int stream_index) { | 113 int GetFileIndexFromStreamIndex(int stream_index) { |
| 112 return (stream_index == 2) ? 1 : 0; | 114 return (stream_index == 2) ? 1 : 0; |
| 113 } | 115 } |
| 114 | 116 |
| 115 // TODO(clamy, gavinp): this should go in base | 117 // TODO(clamy, gavinp): this should go in base |
| 116 bool GetMTime(const base::FilePath& path, base::Time* out_mtime) { | 118 bool GetMTime(const base::FilePath& path, base::Time* out_mtime) { |
| 117 DCHECK(out_mtime); | 119 DCHECK(out_mtime); |
| 118 #if defined(OS_POSIX) | 120 #if defined(OS_POSIX) |
| 119 base::ThreadRestrictions::AssertIOAllowed(); | 121 base::ThreadRestrictions::AssertIOAllowed(); |
| 120 struct stat file_stat; | 122 struct stat file_stat; |
| 121 if (stat(path.value().c_str(), &file_stat) != 0) | 123 if (stat(path.value().c_str(), &file_stat) != 0) |
| 122 return false; | 124 return false; |
| 123 time_t sec; | 125 time_t sec; |
| 124 long nsec; | 126 long nsec; |
| 125 if (GetNanoSecsFromStat(file_stat, &sec, &nsec)) { | 127 if (GetNanoSecsFromStat(file_stat, &sec, &nsec)) { |
| 126 int64 usec = (nsec / base::Time::kNanosecondsPerMicrosecond); | 128 int64_t usec = (nsec / base::Time::kNanosecondsPerMicrosecond); |
| 127 *out_mtime = base::Time::FromTimeT(sec) | 129 *out_mtime = base::Time::FromTimeT(sec) |
| 128 + base::TimeDelta::FromMicroseconds(usec); | 130 + base::TimeDelta::FromMicroseconds(usec); |
| 129 return true; | 131 return true; |
| 130 } | 132 } |
| 131 #endif | 133 #endif |
| 132 base::File::Info file_info; | 134 base::File::Info file_info; |
| 133 if (!base::GetFileInfo(path, &file_info)) | 135 if (!base::GetFileInfo(path, &file_info)) |
| 134 return false; | 136 return false; |
| 135 *out_mtime = file_info.last_modified; | 137 *out_mtime = file_info.last_modified; |
| 136 return true; | 138 return true; |
| 137 } | 139 } |
| 138 | 140 |
| 139 } // namespace simple_backend | 141 } // namespace simple_backend |
| 140 | 142 |
| 141 } // namespace disk_cache | 143 } // namespace disk_cache |
| OLD | NEW |