Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / disk_cache / simple / simple_util.cc
blobd0e1ce5f99543341d73225e650c3141d67fd52b9
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.
5 #include "net/disk_cache/simple/simple_util.h"
7 #include <limits>
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"
20 namespace {
22 // Size of the uint64 hash_key number in Hex format in a string.
23 const size_t kEntryHashKeyAsHexStringSize = 2 * sizeof(uint64);
25 #if defined(OS_POSIX)
26 // TODO(clamy, gavinp): this should go in base
27 bool GetNanoSecsFromStat(const struct stat& st,
28 time_t* out_sec,
29 long* out_nsec) {
30 #if defined(OS_ANDROID)
31 *out_sec = st.st_mtime;
32 *out_nsec = st.st_mtime_nsec;
33 return true;
34 #elif defined(OS_LINUX)
35 *out_sec = st.st_mtim.tv_sec;
36 *out_nsec = st.st_mtim.tv_nsec;
37 return true;
38 #elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD)
39 *out_sec = st.st_mtimespec.tv_sec;
40 *out_nsec = st.st_mtimespec.tv_nsec;
41 return true;
42 #else
43 return false;
44 #endif
46 #endif // defined(OS_POSIX)
48 } // namespace
50 namespace disk_cache {
52 namespace simple_util {
54 std::string ConvertEntryHashKeyToHexString(uint64 hash_key) {
55 const std::string hash_key_str = base::StringPrintf("%016" PRIx64, hash_key);
56 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size());
57 return hash_key_str;
60 std::string GetEntryHashKeyAsHexString(const std::string& key) {
61 std::string hash_key_str =
62 ConvertEntryHashKeyToHexString(GetEntryHashKey(key));
63 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size());
64 return hash_key_str;
67 bool GetEntryHashKeyFromHexString(const base::StringPiece& hash_key,
68 uint64* hash_key_out) {
69 if (hash_key.size() != kEntryHashKeyAsHexStringSize) {
70 return false;
72 return base::HexStringToUInt64(hash_key, hash_key_out);
75 uint64 GetEntryHashKey(const std::string& key) {
76 union {
77 unsigned char sha_hash[base::kSHA1Length];
78 uint64 key_hash;
79 } u;
80 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(key.data()),
81 key.size(), u.sha_hash);
82 return u.key_hash;
85 std::string GetFilenameFromEntryHashAndFileIndex(uint64 entry_hash,
86 int file_index) {
87 return base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, file_index);
90 std::string GetSparseFilenameFromEntryHash(uint64 entry_hash) {
91 return base::StringPrintf("%016" PRIx64 "_s", entry_hash);
94 std::string GetFilenameFromKeyAndFileIndex(const std::string& key,
95 int file_index) {
96 return GetEntryHashKeyAsHexString(key) +
97 base::StringPrintf("_%1d", file_index);
100 int32 GetDataSizeFromKeyAndFileSize(const std::string& key, int64 file_size) {
101 int64 data_size = file_size - key.size() - sizeof(SimpleFileHeader) -
102 sizeof(SimpleFileEOF);
103 return base::checked_cast<int32>(data_size);
106 int64 GetFileSizeFromKeyAndDataSize(const std::string& key, int32 data_size) {
107 return data_size + key.size() + sizeof(SimpleFileHeader) +
108 sizeof(SimpleFileEOF);
111 int GetFileIndexFromStreamIndex(int stream_index) {
112 return (stream_index == 2) ? 1 : 0;
115 // TODO(clamy, gavinp): this should go in base
116 bool GetMTime(const base::FilePath& path, base::Time* out_mtime) {
117 DCHECK(out_mtime);
118 #if defined(OS_POSIX)
119 base::ThreadRestrictions::AssertIOAllowed();
120 struct stat file_stat;
121 if (stat(path.value().c_str(), &file_stat) != 0)
122 return false;
123 time_t sec;
124 long nsec;
125 if (GetNanoSecsFromStat(file_stat, &sec, &nsec)) {
126 int64 usec = (nsec / base::Time::kNanosecondsPerMicrosecond);
127 *out_mtime = base::Time::FromTimeT(sec)
128 + base::TimeDelta::FromMicroseconds(usec);
129 return true;
131 #endif
132 base::File::Info file_info;
133 if (!base::GetFileInfo(path, &file_info))
134 return false;
135 *out_mtime = file_info.last_modified;
136 return true;
139 } // namespace simple_backend
141 } // namespace disk_cache