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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 sizeof(SimpleFileEOF); | 91 sizeof(SimpleFileEOF); |
92 DCHECK_GE(implicit_cast<int64>(std::numeric_limits<int32>::max()), data_size); | 92 DCHECK_GE(implicit_cast<int64>(std::numeric_limits<int32>::max()), data_size); |
93 return data_size; | 93 return data_size; |
94 } | 94 } |
95 | 95 |
96 int64 GetFileSizeFromKeyAndDataSize(const std::string& key, int32 data_size) { | 96 int64 GetFileSizeFromKeyAndDataSize(const std::string& key, int32 data_size) { |
97 return data_size + key.size() + sizeof(SimpleFileHeader) + | 97 return data_size + key.size() + sizeof(SimpleFileHeader) + |
98 sizeof(SimpleFileEOF); | 98 sizeof(SimpleFileEOF); |
99 } | 99 } |
100 | 100 |
101 int64 GetFileOffsetFromKeyAndDataOffset(const std::string& key, | 101 int GetFileIndexFromStreamIndex(int stream_index) { |
102 int data_offset) { | 102 return (stream_index == 2) ? 1 : 0; |
103 } | |
104 | |
105 int GetLastEOFRecordOffset(int file_index, const int data_size[]) { | |
106 if (file_index == 0) | |
107 return data_size[0] + data_size[1] + sizeof(SimpleFileEOF); | |
Deprecated (see juliatuttle)
2013/09/16 20:01:52
This looks like it's vulnerable to an integer over
pasko
2013/09/17 10:06:34
This should be fine.
Currently Write operations w
| |
108 return data_size[2]; | |
109 } | |
110 | |
111 int64 GetFileOffsetFromDataOffset(const std::string& key, | |
112 int data_offset, | |
113 int index, | |
114 int stream_1_size) { | |
103 const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key.size(); | 115 const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key.size(); |
104 return headers_size + data_offset; | 116 const int64 additional_offset = |
117 index == 0 ? stream_1_size + sizeof(SimpleFileEOF) : 0; | |
118 return headers_size + data_offset + additional_offset; | |
105 } | 119 } |
106 | 120 |
107 // TODO(clamy, gavinp): this should go in base | 121 // TODO(clamy, gavinp): this should go in base |
108 bool GetMTime(const base::FilePath& path, base::Time* out_mtime) { | 122 bool GetMTime(const base::FilePath& path, base::Time* out_mtime) { |
109 DCHECK(out_mtime); | 123 DCHECK(out_mtime); |
110 #if defined(OS_POSIX) | 124 #if defined(OS_POSIX) |
111 base::ThreadRestrictions::AssertIOAllowed(); | 125 base::ThreadRestrictions::AssertIOAllowed(); |
112 struct stat file_stat; | 126 struct stat file_stat; |
113 if (stat(path.value().c_str(), &file_stat) != 0) | 127 if (stat(path.value().c_str(), &file_stat) != 0) |
114 return false; | 128 return false; |
115 time_t sec; | 129 time_t sec; |
116 long nsec; | 130 long nsec; |
117 if (GetNanoSecsFromStat(file_stat, &sec, &nsec)) { | 131 if (GetNanoSecsFromStat(file_stat, &sec, &nsec)) { |
118 int64 usec = (nsec / base::Time::kNanosecondsPerMicrosecond); | 132 int64 usec = (nsec / base::Time::kNanosecondsPerMicrosecond); |
119 *out_mtime = base::Time::FromTimeT(sec) | 133 *out_mtime = base::Time::FromTimeT(sec) |
120 + base::TimeDelta::FromMicroseconds(usec); | 134 + base::TimeDelta::FromMicroseconds(usec); |
121 return true; | 135 return true; |
122 } | 136 } |
123 #endif | 137 #endif |
124 base::PlatformFileInfo file_info; | 138 base::PlatformFileInfo file_info; |
125 if (!file_util::GetFileInfo(path, &file_info)) | 139 if (!file_util::GetFileInfo(path, &file_info)) |
126 return false; | 140 return false; |
127 *out_mtime = file_info.last_modified; | 141 *out_mtime = file_info.last_modified; |
128 return true; | 142 return true; |
129 } | 143 } |
130 | 144 |
131 } // namespace simple_backend | 145 } // namespace simple_backend |
132 | 146 |
133 } // namespace disk_cache | 147 } // namespace disk_cache |
OLD | NEW |