| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/base/file_stream.h" | 5 #include "net/base/file_stream.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/threading/thread_restrictions.h" |
| 13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 // Ensure that we can just use our Whence values directly. | 18 // Ensure that we can just use our Whence values directly. |
| 18 COMPILE_ASSERT(FROM_BEGIN == FILE_BEGIN, bad_whence_begin); | 19 COMPILE_ASSERT(FROM_BEGIN == FILE_BEGIN, bad_whence_begin); |
| 19 COMPILE_ASSERT(FROM_CURRENT == FILE_CURRENT, bad_whence_current); | 20 COMPILE_ASSERT(FROM_CURRENT == FILE_CURRENT, bad_whence_current); |
| 20 COMPILE_ASSERT(FROM_END == FILE_END, bad_whence_end); | 21 COMPILE_ASSERT(FROM_END == FILE_END, bad_whence_end); |
| 21 | 22 |
| 22 static void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) { | 23 static void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 DWORD error = GetLastError(); | 192 DWORD error = GetLastError(); |
| 192 LOG(WARNING) << "SetFilePointerEx failed: " << error; | 193 LOG(WARNING) << "SetFilePointerEx failed: " << error; |
| 193 return MapErrorCode(error); | 194 return MapErrorCode(error); |
| 194 } | 195 } |
| 195 if (async_context_.get()) | 196 if (async_context_.get()) |
| 196 SetOffset(async_context_->overlapped(), result); | 197 SetOffset(async_context_->overlapped(), result); |
| 197 return result.QuadPart; | 198 return result.QuadPart; |
| 198 } | 199 } |
| 199 | 200 |
| 200 int64 FileStream::Available() { | 201 int64 FileStream::Available() { |
| 202 base::ThreadRestrictions::AssertIOAllowed(); |
| 203 |
| 201 if (!IsOpen()) | 204 if (!IsOpen()) |
| 202 return ERR_UNEXPECTED; | 205 return ERR_UNEXPECTED; |
| 203 | 206 |
| 204 int64 cur_pos = Seek(FROM_CURRENT, 0); | 207 int64 cur_pos = Seek(FROM_CURRENT, 0); |
| 205 if (cur_pos < 0) | 208 if (cur_pos < 0) |
| 206 return cur_pos; | 209 return cur_pos; |
| 207 | 210 |
| 208 LARGE_INTEGER file_size; | 211 LARGE_INTEGER file_size; |
| 209 if (!GetFileSizeEx(file_, &file_size)) { | 212 if (!GetFileSizeEx(file_, &file_size)) { |
| 210 DWORD error = GetLastError(); | 213 DWORD error = GetLastError(); |
| 211 LOG(WARNING) << "GetFileSizeEx failed: " << error; | 214 LOG(WARNING) << "GetFileSizeEx failed: " << error; |
| 212 return MapErrorCode(error); | 215 return MapErrorCode(error); |
| 213 } | 216 } |
| 214 | 217 |
| 215 return file_size.QuadPart - cur_pos; | 218 return file_size.QuadPart - cur_pos; |
| 216 } | 219 } |
| 217 | 220 |
| 218 int FileStream::Read( | 221 int FileStream::Read( |
| 219 char* buf, int buf_len, CompletionCallback* callback) { | 222 char* buf, int buf_len, CompletionCallback* callback) { |
| 220 if (!IsOpen()) | 223 if (!IsOpen()) |
| 221 return ERR_UNEXPECTED; | 224 return ERR_UNEXPECTED; |
| 222 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | 225 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); |
| 223 | 226 |
| 224 OVERLAPPED* overlapped = NULL; | 227 OVERLAPPED* overlapped = NULL; |
| 225 if (async_context_.get()) { | 228 if (async_context_.get()) { |
| 229 DCHECK(callback); |
| 226 DCHECK(!async_context_->callback()); | 230 DCHECK(!async_context_->callback()); |
| 227 overlapped = async_context_->overlapped(); | 231 overlapped = async_context_->overlapped(); |
| 232 } else { |
| 233 DCHECK(!callback); |
| 234 base::ThreadRestrictions::AssertIOAllowed(); |
| 228 } | 235 } |
| 229 | 236 |
| 230 int rv; | 237 int rv; |
| 231 | 238 |
| 232 DWORD bytes_read; | 239 DWORD bytes_read; |
| 233 if (!ReadFile(file_, buf, buf_len, &bytes_read, overlapped)) { | 240 if (!ReadFile(file_, buf, buf_len, &bytes_read, overlapped)) { |
| 234 DWORD error = GetLastError(); | 241 DWORD error = GetLastError(); |
| 235 if (async_context_.get() && error == ERROR_IO_PENDING) { | 242 if (async_context_.get() && error == ERROR_IO_PENDING) { |
| 236 async_context_->IOCompletionIsPending(callback); | 243 async_context_->IOCompletionIsPending(callback); |
| 237 rv = ERR_IO_PENDING; | 244 rv = ERR_IO_PENDING; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 } | 279 } |
| 273 | 280 |
| 274 int FileStream::Write( | 281 int FileStream::Write( |
| 275 const char* buf, int buf_len, CompletionCallback* callback) { | 282 const char* buf, int buf_len, CompletionCallback* callback) { |
| 276 if (!IsOpen()) | 283 if (!IsOpen()) |
| 277 return ERR_UNEXPECTED; | 284 return ERR_UNEXPECTED; |
| 278 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 285 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 279 | 286 |
| 280 OVERLAPPED* overlapped = NULL; | 287 OVERLAPPED* overlapped = NULL; |
| 281 if (async_context_.get()) { | 288 if (async_context_.get()) { |
| 289 DCHECK(callback); |
| 282 DCHECK(!async_context_->callback()); | 290 DCHECK(!async_context_->callback()); |
| 283 overlapped = async_context_->overlapped(); | 291 overlapped = async_context_->overlapped(); |
| 292 } else { |
| 293 DCHECK(!callback); |
| 294 base::ThreadRestrictions::AssertIOAllowed(); |
| 284 } | 295 } |
| 285 | 296 |
| 286 int rv; | 297 int rv; |
| 287 DWORD bytes_written; | 298 DWORD bytes_written; |
| 288 if (!WriteFile(file_, buf, buf_len, &bytes_written, overlapped)) { | 299 if (!WriteFile(file_, buf, buf_len, &bytes_written, overlapped)) { |
| 289 DWORD error = GetLastError(); | 300 DWORD error = GetLastError(); |
| 290 if (async_context_.get() && error == ERROR_IO_PENDING) { | 301 if (async_context_.get() && error == ERROR_IO_PENDING) { |
| 291 async_context_->IOCompletionIsPending(callback); | 302 async_context_->IOCompletionIsPending(callback); |
| 292 rv = ERR_IO_PENDING; | 303 rv = ERR_IO_PENDING; |
| 293 } else { | 304 } else { |
| 294 LOG(WARNING) << "WriteFile failed: " << error; | 305 LOG(WARNING) << "WriteFile failed: " << error; |
| 295 rv = MapErrorCode(error); | 306 rv = MapErrorCode(error); |
| 296 } | 307 } |
| 297 } else if (overlapped) { | 308 } else if (overlapped) { |
| 298 async_context_->IOCompletionIsPending(callback); | 309 async_context_->IOCompletionIsPending(callback); |
| 299 rv = ERR_IO_PENDING; | 310 rv = ERR_IO_PENDING; |
| 300 } else { | 311 } else { |
| 301 rv = static_cast<int>(bytes_written); | 312 rv = static_cast<int>(bytes_written); |
| 302 } | 313 } |
| 303 return rv; | 314 return rv; |
| 304 } | 315 } |
| 305 | 316 |
| 306 int FileStream::Flush() { | 317 int FileStream::Flush() { |
| 318 base::ThreadRestrictions::AssertIOAllowed(); |
| 319 |
| 307 if (!IsOpen()) | 320 if (!IsOpen()) |
| 308 return ERR_UNEXPECTED; | 321 return ERR_UNEXPECTED; |
| 309 | 322 |
| 310 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 323 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 311 if (FlushFileBuffers(file_)) { | 324 if (FlushFileBuffers(file_)) { |
| 312 return OK; | 325 return OK; |
| 313 } | 326 } |
| 314 | 327 |
| 315 int rv; | 328 int rv; |
| 316 DWORD error = GetLastError(); | 329 DWORD error = GetLastError(); |
| 317 rv = MapErrorCode(error); | 330 rv = MapErrorCode(error); |
| 318 return rv; | 331 return rv; |
| 319 } | 332 } |
| 320 | 333 |
| 321 int64 FileStream::Truncate(int64 bytes) { | 334 int64 FileStream::Truncate(int64 bytes) { |
| 335 base::ThreadRestrictions::AssertIOAllowed(); |
| 336 |
| 322 if (!IsOpen()) | 337 if (!IsOpen()) |
| 323 return ERR_UNEXPECTED; | 338 return ERR_UNEXPECTED; |
| 324 | 339 |
| 325 // We better be open for reading. | 340 // We better be open for reading. |
| 326 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 341 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 327 | 342 |
| 328 // Seek to the position to truncate from. | 343 // Seek to the position to truncate from. |
| 329 int64 seek_position = Seek(FROM_BEGIN, bytes); | 344 int64 seek_position = Seek(FROM_BEGIN, bytes); |
| 330 if (seek_position != bytes) | 345 if (seek_position != bytes) |
| 331 return ERR_UNEXPECTED; | 346 return ERR_UNEXPECTED; |
| 332 | 347 |
| 333 // And truncate the file. | 348 // And truncate the file. |
| 334 BOOL result = SetEndOfFile(file_); | 349 BOOL result = SetEndOfFile(file_); |
| 335 if (!result) { | 350 if (!result) { |
| 336 DWORD error = GetLastError(); | 351 DWORD error = GetLastError(); |
| 337 LOG(WARNING) << "SetEndOfFile failed: " << error; | 352 LOG(WARNING) << "SetEndOfFile failed: " << error; |
| 338 return MapErrorCode(error); | 353 return MapErrorCode(error); |
| 339 } | 354 } |
| 340 | 355 |
| 341 // Success. | 356 // Success. |
| 342 return seek_position; | 357 return seek_position; |
| 343 } | 358 } |
| 344 | 359 |
| 345 } // namespace net | 360 } // namespace net |
| OLD | NEW |