| 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/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 uint64 GetEntryHashKey(const std::string& key) { | 70 uint64 GetEntryHashKey(const std::string& key) { |
| 71 union { | 71 union { |
| 72 unsigned char sha_hash[base::kSHA1Length]; | 72 unsigned char sha_hash[base::kSHA1Length]; |
| 73 uint64 key_hash; | 73 uint64 key_hash; |
| 74 } u; | 74 } u; |
| 75 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(key.data()), | 75 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(key.data()), |
| 76 key.size(), u.sha_hash); | 76 key.size(), u.sha_hash); |
| 77 return u.key_hash; | 77 return u.key_hash; |
| 78 } | 78 } |
| 79 | 79 |
| 80 std::string GetFilenameFromEntryHashAndIndex(uint64 entry_hash, | 80 std::string GetFilenameFromEntryHashAndFileIndex(uint64 entry_hash, |
| 81 int index) { | 81 int file_index) { |
| 82 return base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, index); | 82 return base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, file_index); |
| 83 } | 83 } |
| 84 | 84 |
| 85 std::string GetFilenameFromKeyAndIndex(const std::string& key, int index) { | 85 std::string GetFilenameFromKeyAndFileIndex(const std::string& key, |
| 86 return GetEntryHashKeyAsHexString(key) + base::StringPrintf("_%1d", index); | 86 int file_index) { |
| 87 return GetEntryHashKeyAsHexString(key) + |
| 88 base::StringPrintf("_%1d", file_index); |
| 87 } | 89 } |
| 88 | 90 |
| 89 int32 GetDataSizeFromKeyAndFileSize(const std::string& key, int64 file_size) { | 91 int32 GetDataSizeFromKeyAndFileSize(const std::string& key, int64 file_size) { |
| 90 int64 data_size = file_size - key.size() - sizeof(SimpleFileHeader) - | 92 int64 data_size = file_size - key.size() - sizeof(SimpleFileHeader) - |
| 91 sizeof(SimpleFileEOF); | 93 sizeof(SimpleFileEOF); |
| 92 DCHECK_GE(implicit_cast<int64>(std::numeric_limits<int32>::max()), data_size); | 94 DCHECK_GE(implicit_cast<int64>(std::numeric_limits<int32>::max()), data_size); |
| 93 return data_size; | 95 return data_size; |
| 94 } | 96 } |
| 95 | 97 |
| 96 int64 GetFileSizeFromKeyAndDataSize(const std::string& key, int32 data_size) { | 98 int64 GetFileSizeFromKeyAndDataSize(const std::string& key, int32 data_size) { |
| 97 return data_size + key.size() + sizeof(SimpleFileHeader) + | 99 return data_size + key.size() + sizeof(SimpleFileHeader) + |
| 98 sizeof(SimpleFileEOF); | 100 sizeof(SimpleFileEOF); |
| 99 } | 101 } |
| 100 | 102 |
| 101 int64 GetFileOffsetFromKeyAndDataOffset(const std::string& key, | 103 int GetFileIndexFromStreamIndex(int stream_index) { |
| 102 int data_offset) { | 104 return (stream_index == 2) ? 1 : 0; |
| 103 const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key.size(); | |
| 104 return headers_size + data_offset; | |
| 105 } | 105 } |
| 106 | 106 |
| 107 // TODO(clamy, gavinp): this should go in base | 107 // TODO(clamy, gavinp): this should go in base |
| 108 bool GetMTime(const base::FilePath& path, base::Time* out_mtime) { | 108 bool GetMTime(const base::FilePath& path, base::Time* out_mtime) { |
| 109 DCHECK(out_mtime); | 109 DCHECK(out_mtime); |
| 110 #if defined(OS_POSIX) | 110 #if defined(OS_POSIX) |
| 111 base::ThreadRestrictions::AssertIOAllowed(); | 111 base::ThreadRestrictions::AssertIOAllowed(); |
| 112 struct stat file_stat; | 112 struct stat file_stat; |
| 113 if (stat(path.value().c_str(), &file_stat) != 0) | 113 if (stat(path.value().c_str(), &file_stat) != 0) |
| 114 return false; | 114 return false; |
| 115 time_t sec; | 115 time_t sec; |
| 116 long nsec; | 116 long nsec; |
| 117 if (GetNanoSecsFromStat(file_stat, &sec, &nsec)) { | 117 if (GetNanoSecsFromStat(file_stat, &sec, &nsec)) { |
| 118 int64 usec = (nsec / base::Time::kNanosecondsPerMicrosecond); | 118 int64 usec = (nsec / base::Time::kNanosecondsPerMicrosecond); |
| 119 *out_mtime = base::Time::FromTimeT(sec) | 119 *out_mtime = base::Time::FromTimeT(sec) |
| 120 + base::TimeDelta::FromMicroseconds(usec); | 120 + base::TimeDelta::FromMicroseconds(usec); |
| 121 return true; | 121 return true; |
| 122 } | 122 } |
| 123 #endif | 123 #endif |
| 124 base::PlatformFileInfo file_info; | 124 base::PlatformFileInfo file_info; |
| 125 if (!file_util::GetFileInfo(path, &file_info)) | 125 if (!file_util::GetFileInfo(path, &file_info)) |
| 126 return false; | 126 return false; |
| 127 *out_mtime = file_info.last_modified; | 127 *out_mtime = file_info.last_modified; |
| 128 return true; | 128 return true; |
| 129 } | 129 } |
| 130 | 130 |
| 131 } // namespace simple_backend | 131 } // namespace simple_backend |
| 132 | 132 |
| 133 } // namespace disk_cache | 133 } // namespace disk_cache |
| OLD | NEW |