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