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