OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/file.h" | 5 #include "net/disk_cache/file.h" |
6 | 6 |
7 #include "net/disk_cache/disk_cache.h" | 7 #include "net/disk_cache/disk_cache.h" |
8 | 8 |
9 namespace { | 9 namespace { |
10 | 10 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 delete data; | 81 delete data; |
82 } else { | 82 } else { |
83 // Somebody is waiting for this so don't delete data and instead notify | 83 // Somebody is waiting for this so don't delete data and instead notify |
84 // that we were called. | 84 // that we were called. |
85 data->actual_bytes = actual_bytes; | 85 data->actual_bytes = actual_bytes; |
86 data->file->Release(); | 86 data->file->Release(); |
87 data->called = true; | 87 data->called = true; |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 File::File(OSFile file) | 91 File::File(base::PlatformFile file) |
92 : init_(true), mixed_(true), os_file_(INVALID_HANDLE_VALUE), | 92 : init_(true), mixed_(true), platform_file_(INVALID_HANDLE_VALUE), |
93 sync_os_file_(file) { | 93 sync_platform_file_(file) { |
94 } | 94 } |
95 | 95 |
96 bool File::Init(const std::wstring& name) { | 96 bool File::Init(const std::wstring& name) { |
97 DCHECK(!init_); | 97 DCHECK(!init_); |
98 if (init_) | 98 if (init_) |
99 return false; | 99 return false; |
100 | 100 |
101 os_file_ = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, | 101 platform_file_ = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, |
102 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, | 102 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, |
103 FILE_FLAG_OVERLAPPED, NULL); | 103 FILE_FLAG_OVERLAPPED, NULL); |
104 | 104 |
105 if (INVALID_HANDLE_VALUE == os_file_) | 105 if (INVALID_HANDLE_VALUE == platform_file_) |
106 return false; | 106 return false; |
107 | 107 |
108 init_ = true; | 108 init_ = true; |
109 if (mixed_) { | 109 if (mixed_) { |
110 sync_os_file_ = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, | 110 sync_platform_file_ = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, |
111 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | 111 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
112 OPEN_EXISTING, 0, NULL); | 112 OPEN_EXISTING, 0, NULL); |
113 | 113 |
114 if (INVALID_HANDLE_VALUE == sync_os_file_) | 114 if (INVALID_HANDLE_VALUE == sync_platform_file_) |
115 return false; | 115 return false; |
116 } else { | 116 } else { |
117 sync_os_file_ = INVALID_HANDLE_VALUE; | 117 sync_platform_file_ = INVALID_HANDLE_VALUE; |
118 } | 118 } |
119 | 119 |
120 return true; | 120 return true; |
121 } | 121 } |
122 | 122 |
123 File::~File() { | 123 File::~File() { |
124 if (!init_) | 124 if (!init_) |
125 return; | 125 return; |
126 | 126 |
127 if (INVALID_HANDLE_VALUE != os_file_) | 127 if (INVALID_HANDLE_VALUE != platform_file_) |
128 CloseHandle(os_file_); | 128 CloseHandle(platform_file_); |
129 if (mixed_ && INVALID_HANDLE_VALUE != sync_os_file_) | 129 if (mixed_ && INVALID_HANDLE_VALUE != sync_platform_file_) |
130 CloseHandle(sync_os_file_); | 130 CloseHandle(sync_platform_file_); |
131 } | 131 } |
132 | 132 |
133 OSFile File::os_file() const { | 133 base::PlatformFile File::platform_file() const { |
134 DCHECK(init_); | 134 DCHECK(init_); |
135 return (INVALID_HANDLE_VALUE == os_file_) ? sync_os_file_ : os_file_; | 135 return (INVALID_HANDLE_VALUE == platform_file_) ? sync_platform_file_ : |
| 136 platform_file_; |
136 } | 137 } |
137 | 138 |
138 bool File::IsValid() const { | 139 bool File::IsValid() const { |
139 if (!init_) | 140 if (!init_) |
140 return false; | 141 return false; |
141 return (INVALID_HANDLE_VALUE != os_file_ || | 142 return (INVALID_HANDLE_VALUE != platform_file_ || |
142 INVALID_HANDLE_VALUE != sync_os_file_); | 143 INVALID_HANDLE_VALUE != sync_platform_file_); |
143 } | 144 } |
144 | 145 |
145 bool File::Read(void* buffer, size_t buffer_len, size_t offset) { | 146 bool File::Read(void* buffer, size_t buffer_len, size_t offset) { |
146 DCHECK(init_); | 147 DCHECK(init_); |
147 if (!mixed_ || buffer_len > ULONG_MAX || offset > LONG_MAX) | 148 if (!mixed_ || buffer_len > ULONG_MAX || offset > LONG_MAX) |
148 return false; | 149 return false; |
149 | 150 |
150 DWORD ret = SetFilePointer(sync_os_file_, static_cast<LONG>(offset), NULL, | 151 DWORD ret = SetFilePointer(sync_platform_file_, |
| 152 static_cast<LONG>(offset), |
| 153 NULL, |
151 FILE_BEGIN); | 154 FILE_BEGIN); |
152 if (INVALID_SET_FILE_POINTER == ret) | 155 if (INVALID_SET_FILE_POINTER == ret) |
153 return false; | 156 return false; |
154 | 157 |
155 DWORD actual; | 158 DWORD actual; |
156 DWORD size = static_cast<DWORD>(buffer_len); | 159 DWORD size = static_cast<DWORD>(buffer_len); |
157 if (!ReadFile(sync_os_file_, buffer, size, &actual, NULL)) | 160 if (!ReadFile(sync_platform_file_, buffer, size, &actual, NULL)) |
158 return false; | 161 return false; |
159 return actual == size; | 162 return actual == size; |
160 } | 163 } |
161 | 164 |
162 bool File::Write(const void* buffer, size_t buffer_len, size_t offset) { | 165 bool File::Write(const void* buffer, size_t buffer_len, size_t offset) { |
163 DCHECK(init_); | 166 DCHECK(init_); |
164 if (!mixed_ || buffer_len > ULONG_MAX || offset > ULONG_MAX) | 167 if (!mixed_ || buffer_len > ULONG_MAX || offset > ULONG_MAX) |
165 return false; | 168 return false; |
166 | 169 |
167 DWORD ret = SetFilePointer(sync_os_file_, static_cast<LONG>(offset), NULL, | 170 DWORD ret = SetFilePointer(sync_platform_file_, |
| 171 static_cast<LONG>(offset), |
| 172 NULL, |
168 FILE_BEGIN); | 173 FILE_BEGIN); |
169 if (INVALID_SET_FILE_POINTER == ret) | 174 if (INVALID_SET_FILE_POINTER == ret) |
170 return false; | 175 return false; |
171 | 176 |
172 DWORD actual; | 177 DWORD actual; |
173 DWORD size = static_cast<DWORD>(buffer_len); | 178 DWORD size = static_cast<DWORD>(buffer_len); |
174 if (!WriteFile(sync_os_file_, buffer, size, &actual, NULL)) | 179 if (!WriteFile(sync_platform_file_, buffer, size, &actual, NULL)) |
175 return false; | 180 return false; |
176 return actual == size; | 181 return actual == size; |
177 } | 182 } |
178 | 183 |
179 // We have to increase the ref counter of the file before performing the IO to | 184 // We have to increase the ref counter of the file before performing the IO to |
180 // prevent the completion to happen with an invalid handle (if the file is | 185 // prevent the completion to happen with an invalid handle (if the file is |
181 // closed while the IO is in flight). | 186 // closed while the IO is in flight). |
182 bool File::Read(void* buffer, size_t buffer_len, size_t offset, | 187 bool File::Read(void* buffer, size_t buffer_len, size_t offset, |
183 FileIOCallback* callback, bool* completed) { | 188 FileIOCallback* callback, bool* completed) { |
184 DCHECK(init_); | 189 DCHECK(init_); |
185 if (buffer_len > ULONG_MAX || offset > ULONG_MAX) | 190 if (buffer_len > ULONG_MAX || offset > ULONG_MAX) |
186 return false; | 191 return false; |
187 | 192 |
188 MyOverlapped* data = new MyOverlapped; | 193 MyOverlapped* data = new MyOverlapped; |
189 memset(data, 0, sizeof(*data)); | 194 memset(data, 0, sizeof(*data)); |
190 | 195 |
191 SyncCallback local_callback; | 196 SyncCallback local_callback; |
192 data->overlapped.Offset = static_cast<DWORD>(offset); | 197 data->overlapped.Offset = static_cast<DWORD>(offset); |
193 data->callback = callback ? callback : &local_callback; | 198 data->callback = callback ? callback : &local_callback; |
194 data->file = this; | 199 data->file = this; |
195 | 200 |
196 DWORD size = static_cast<DWORD>(buffer_len); | 201 DWORD size = static_cast<DWORD>(buffer_len); |
197 AddRef(); | 202 AddRef(); |
198 | 203 |
199 if (!ReadFileEx(os_file_, buffer, size, &data->overlapped, &IoCompletion)) { | 204 if (!ReadFileEx(platform_file_, buffer, size, &data->overlapped, |
| 205 &IoCompletion)) { |
200 Release(); | 206 Release(); |
201 delete data; | 207 delete data; |
202 return false; | 208 return false; |
203 } | 209 } |
204 | 210 |
205 if (callback) { | 211 if (callback) { |
206 *completed = false; | 212 *completed = false; |
207 // Let's check if the operation is already finished. | 213 // Let's check if the operation is already finished. |
208 SleepEx(0, TRUE); | 214 SleepEx(0, TRUE); |
209 if (data->called) { | 215 if (data->called) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 data->file = this; | 259 data->file = this; |
254 if (!callback && !notify) { | 260 if (!callback && !notify) { |
255 data->delete_buffer = true; | 261 data->delete_buffer = true; |
256 data->callback = NULL; | 262 data->callback = NULL; |
257 data->buffer = buffer; | 263 data->buffer = buffer; |
258 } | 264 } |
259 | 265 |
260 DWORD size = static_cast<DWORD>(buffer_len); | 266 DWORD size = static_cast<DWORD>(buffer_len); |
261 AddRef(); | 267 AddRef(); |
262 | 268 |
263 if (!WriteFileEx(os_file_, buffer, size, &data->overlapped, &IoCompletion)) { | 269 if (!WriteFileEx(platform_file_, buffer, size, &data->overlapped, |
| 270 &IoCompletion)) { |
264 Release(); | 271 Release(); |
265 delete data; | 272 delete data; |
266 return false; | 273 return false; |
267 } | 274 } |
268 | 275 |
269 if (callback) { | 276 if (callback) { |
270 *completed = false; | 277 *completed = false; |
271 SleepEx(0, TRUE); | 278 SleepEx(0, TRUE); |
272 if (data->called) { | 279 if (data->called) { |
273 *completed = (data->actual_bytes == size); | 280 *completed = (data->actual_bytes == size); |
(...skipping 14 matching lines...) Expand all Loading... |
288 | 295 |
289 return true; | 296 return true; |
290 } | 297 } |
291 | 298 |
292 bool File::SetLength(size_t length) { | 299 bool File::SetLength(size_t length) { |
293 DCHECK(init_); | 300 DCHECK(init_); |
294 if (length > ULONG_MAX) | 301 if (length > ULONG_MAX) |
295 return false; | 302 return false; |
296 | 303 |
297 DWORD size = static_cast<DWORD>(length); | 304 DWORD size = static_cast<DWORD>(length); |
298 HANDLE file = os_file(); | 305 HANDLE file = platform_file(); |
299 if (INVALID_SET_FILE_POINTER == SetFilePointer(file, size, NULL, FILE_BEGIN)) | 306 if (INVALID_SET_FILE_POINTER == SetFilePointer(file, size, NULL, FILE_BEGIN)) |
300 return false; | 307 return false; |
301 | 308 |
302 return TRUE == SetEndOfFile(file); | 309 return TRUE == SetEndOfFile(file); |
303 } | 310 } |
304 | 311 |
305 size_t File::GetLength() { | 312 size_t File::GetLength() { |
306 DCHECK(init_); | 313 DCHECK(init_); |
307 LARGE_INTEGER size; | 314 LARGE_INTEGER size; |
308 HANDLE file = os_file(); | 315 HANDLE file = platform_file(); |
309 if (!GetFileSizeEx(file, &size)) | 316 if (!GetFileSizeEx(file, &size)) |
310 return 0; | 317 return 0; |
311 if (size.HighPart) | 318 if (size.HighPart) |
312 return ULONG_MAX; | 319 return ULONG_MAX; |
313 | 320 |
314 return static_cast<size_t>(size.LowPart); | 321 return static_cast<size_t>(size.LowPart); |
315 } | 322 } |
316 | 323 |
317 } // namespace disk_cache | 324 } // namespace disk_cache |
318 | 325 |
OLD | NEW |