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" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/sha1.h" | 12 #include "base/sha1.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "net/disk_cache/simple/simple_entry_format.h" | 17 #include "net/disk_cache/simple/simple_entry_format.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Size of the uint64 hash_key number in Hex format in a string. | 21 // Size of the uint64 hash_key number in Hex format in a string. |
22 const size_t kEntryHashKeyAsHexStringSize = 2 * sizeof(uint64); | 22 const size_t kEntryHashKeyAsHexStringSize = 2 * sizeof(uint64); |
23 | 23 |
24 // TODO(clamy, gavinp): this should go in base | |
25 bool GetNanoSecsFromStat(const struct stat& st, | |
26 time_t* out_sec, | |
27 long* out_nsec) { | |
28 #if defined(OS_ANDROID) | |
29 *out_sec = st.st_mtime; | |
30 *out_nsec = st.st_mtime_nsec; | |
31 #elif defined(OS_LINUX) | |
32 *out_sec = st.st_mtim.tv_sec; | |
33 *out_nsec = st.st_mtim.tv_nsec; | |
34 #elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD) | |
35 *out_sec = st.st_mtimespec.tv_sec; | |
36 *out_nsec = st.st_mtimespec.tv_nsec; | |
37 #else | |
38 return false; | |
39 #endif | |
40 return true; | |
41 } | |
42 | |
43 } // namespace | 24 } // namespace |
44 | 25 |
45 namespace disk_cache { | 26 namespace disk_cache { |
46 | 27 |
47 namespace simple_util { | 28 namespace simple_util { |
48 | 29 |
49 std::string ConvertEntryHashKeyToHexString(uint64 hash_key) { | 30 std::string ConvertEntryHashKeyToHexString(uint64 hash_key) { |
50 const std::string hash_key_str = base::StringPrintf("%016" PRIx64, hash_key); | 31 const std::string hash_key_str = base::StringPrintf("%016" PRIx64, hash_key); |
51 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size()); | 32 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size()); |
52 return hash_key_str; | 33 return hash_key_str; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 81 |
101 int64 GetFileOffsetFromKeyAndDataOffset(const std::string& key, | 82 int64 GetFileOffsetFromKeyAndDataOffset(const std::string& key, |
102 int data_offset) { | 83 int data_offset) { |
103 const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key.size(); | 84 const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key.size(); |
104 return headers_size + data_offset; | 85 return headers_size + data_offset; |
105 } | 86 } |
106 | 87 |
107 // TODO(clamy, gavinp): this should go in base | 88 // TODO(clamy, gavinp): this should go in base |
108 bool GetMTime(const base::FilePath& path, base::Time* out_mtime) { | 89 bool GetMTime(const base::FilePath& path, base::Time* out_mtime) { |
109 DCHECK(out_mtime); | 90 DCHECK(out_mtime); |
110 #if defined(OS_POSIX) | |
111 base::ThreadRestrictions::AssertIOAllowed(); | |
112 struct stat file_stat; | |
113 if (stat(path.value().c_str(), &file_stat) != 0) | |
114 return false; | |
115 time_t sec; | |
116 long nsec; | |
117 if (GetNanoSecsFromStat(file_stat, &sec, &nsec)) { | |
118 int64 usec = (nsec / base::Time::kNanosecondsPerMicrosecond); | |
119 *out_mtime = base::Time::FromTimeT(sec) | |
120 + base::TimeDelta::FromMicroseconds(usec); | |
121 return true; | |
122 } | |
123 #endif | |
124 base::PlatformFileInfo file_info; | 91 base::PlatformFileInfo file_info; |
125 if (!file_util::GetFileInfo(path, &file_info)) | 92 if (!file_util::GetFileInfo(path, &file_info)) |
126 return false; | 93 return false; |
127 *out_mtime = file_info.last_modified; | 94 *out_mtime = file_info.last_modified; |
128 return true; | 95 return true; |
129 } | 96 } |
130 | 97 |
131 } // namespace simple_backend | 98 } // namespace simple_backend |
132 | 99 |
133 } // namespace disk_cache | 100 } // namespace disk_cache |
OLD | NEW |