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"
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"
22 // Size of the uint64 hash_key number in Hex format in a string.
23 const size_t kEntryHashKeyAsHexStringSize
= 2 * sizeof(uint64
);
26 // TODO(clamy, gavinp): this should go in base
27 bool GetNanoSecsFromStat(const struct stat
& st
,
30 #if defined(OS_ANDROID)
31 *out_sec
= st
.st_mtime
;
32 *out_nsec
= st
.st_mtime_nsec
;
34 #elif defined(OS_LINUX)
35 *out_sec
= st
.st_mtim
.tv_sec
;
36 *out_nsec
= st
.st_mtim
.tv_nsec
;
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
;
46 #endif // defined(OS_POSIX)
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());
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());
67 bool GetEntryHashKeyFromHexString(const base::StringPiece
& hash_key
,
68 uint64
* hash_key_out
) {
69 if (hash_key
.size() != kEntryHashKeyAsHexStringSize
) {
72 return base::HexStringToUInt64(hash_key
, hash_key_out
);
75 uint64
GetEntryHashKey(const std::string
& key
) {
77 unsigned char sha_hash
[base::kSHA1Length
];
80 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(key
.data()),
81 key
.size(), u
.sha_hash
);
85 std::string
GetFilenameFromEntryHashAndFileIndex(uint64 entry_hash
,
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
,
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
) {
118 #if defined(OS_POSIX)
119 base::ThreadRestrictions::AssertIOAllowed();
120 struct stat file_stat
;
121 if (stat(path
.value().c_str(), &file_stat
) != 0)
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
);
132 base::File::Info file_info
;
133 if (!base::GetFileInfo(path
, &file_info
))
135 *out_mtime
= file_info
.last_modified
;
139 } // namespace simple_backend
141 } // namespace disk_cache