diff options
Diffstat (limited to 'net/base/file_stream_context_win.cc')
-rw-r--r-- | net/base/file_stream_context_win.cc | 122 |
1 files changed, 60 insertions, 62 deletions
diff --git a/net/base/file_stream_context_win.cc b/net/base/file_stream_context_win.cc index 7072f1d..f5656a3 100644 --- a/net/base/file_stream_context_win.cc +++ b/net/base/file_stream_context_win.cc @@ -47,7 +47,6 @@ FileStream::Context::Context(const BoundNetLog& bound_net_log) bound_net_log_(bound_net_log), error_source_(FILE_ERROR_SOURCE_COUNT) { io_context_.handler = this; - memset(&io_context_.overlapped, 0, sizeof(io_context_.overlapped)); } FileStream::Context::Context(base::PlatformFile file, @@ -61,7 +60,6 @@ FileStream::Context::Context(base::PlatformFile file, bound_net_log_(bound_net_log), error_source_(FILE_ERROR_SOURCE_COUNT) { io_context_.handler = this; - memset(&io_context_.overlapped, 0, sizeof(io_context_.overlapped)); if (file_ != base::kInvalidPlatformFileValue && (open_flags & base::PLATFORM_FILE_ASYNC)) { OnAsyncFileOpened(); @@ -74,10 +72,9 @@ FileStream::Context::~Context() { int64 FileStream::Context::GetFileSize() const { LARGE_INTEGER file_size; if (!GetFileSizeEx(file_, &file_size)) { - IOResult error = IOResult::FromOSError(GetLastError()); - LOG(WARNING) << "GetFileSizeEx failed: " << error.os_error; - RecordError(error, FILE_ERROR_SOURCE_GET_SIZE); - return error.result; + DWORD error = GetLastError(); + LOG(WARNING) << "GetFileSizeEx failed: " << error; + return RecordAndMapError(error, FILE_ERROR_SOURCE_GET_SIZE); } return file_size.QuadPart; @@ -89,39 +86,44 @@ int FileStream::Context::ReadAsync(IOBuffer* buf, DCHECK(!async_in_progress_); error_source_ = FILE_ERROR_SOURCE_READ; + int rv = 0; + DWORD bytes_read; if (!ReadFile(file_, buf->data(), buf_len, &bytes_read, &io_context_.overlapped)) { - IOResult error = IOResult::FromOSError(GetLastError()); - if (error.os_error == ERROR_IO_PENDING) { + DWORD error = GetLastError(); + if (error == ERROR_IO_PENDING) { IOCompletionIsPending(callback, buf); - } else if (error.os_error == ERROR_HANDLE_EOF) { - return 0; // Report EOF by returning 0 bytes read. + rv = ERR_IO_PENDING; + } else if (error == ERROR_HANDLE_EOF) { + rv = 0; // Report EOF by returning 0 bytes read. } else { - LOG(WARNING) << "ReadFile failed: " << error.os_error; - RecordError(error, FILE_ERROR_SOURCE_READ); + LOG(WARNING) << "ReadFile failed: " << error; + rv = RecordAndMapError(error, FILE_ERROR_SOURCE_READ); } - return error.result; + } else { + IOCompletionIsPending(callback, buf); + rv = ERR_IO_PENDING; } - - IOCompletionIsPending(callback, buf); - return ERR_IO_PENDING; + return rv; } int FileStream::Context::ReadSync(char* buf, int buf_len) { + int rv = 0; + DWORD bytes_read; if (!ReadFile(file_, buf, buf_len, &bytes_read, NULL)) { - IOResult error = IOResult::FromOSError(GetLastError()); - if (error.os_error == ERROR_HANDLE_EOF) { - return 0; // Report EOF by returning 0 bytes read. + DWORD error = GetLastError(); + if (error == ERROR_HANDLE_EOF) { + rv = 0; // Report EOF by returning 0 bytes read. } else { - LOG(WARNING) << "ReadFile failed: " << error.os_error; - RecordError(error, FILE_ERROR_SOURCE_READ); + LOG(WARNING) << "ReadFile failed: " << error; + rv = RecordAndMapError(error, FILE_ERROR_SOURCE_READ); } - return error.result; + } else { + rv = static_cast<int>(bytes_read); } - - return bytes_read; + return rv; } int FileStream::Context::WriteAsync(IOBuffer* buf, @@ -129,68 +131,69 @@ int FileStream::Context::WriteAsync(IOBuffer* buf, const CompletionCallback& callback) { error_source_ = FILE_ERROR_SOURCE_WRITE; + int rv = 0; DWORD bytes_written = 0; if (!WriteFile(file_, buf->data(), buf_len, &bytes_written, &io_context_.overlapped)) { - IOResult error = IOResult::FromOSError(GetLastError()); - if (error.os_error == ERROR_IO_PENDING) { + DWORD error = GetLastError(); + if (error == ERROR_IO_PENDING) { IOCompletionIsPending(callback, buf); + rv = ERR_IO_PENDING; } else { - LOG(WARNING) << "WriteFile failed: " << error.os_error; - RecordError(error, FILE_ERROR_SOURCE_WRITE); + LOG(WARNING) << "WriteFile failed: " << error; + rv = RecordAndMapError(error, FILE_ERROR_SOURCE_WRITE); } - return error.result; + } else { + IOCompletionIsPending(callback, buf); + rv = ERR_IO_PENDING; } - - IOCompletionIsPending(callback, buf); - return ERR_IO_PENDING; + return rv; } int FileStream::Context::WriteSync(const char* buf, int buf_len) { + int rv = 0; DWORD bytes_written = 0; if (!WriteFile(file_, buf, buf_len, &bytes_written, NULL)) { - IOResult error = IOResult::FromOSError(GetLastError()); - LOG(WARNING) << "WriteFile failed: " << error.os_error; - RecordError(error, FILE_ERROR_SOURCE_WRITE); - return error.result; + DWORD error = GetLastError(); + LOG(WARNING) << "WriteFile failed: " << error; + rv = RecordAndMapError(error, FILE_ERROR_SOURCE_WRITE); + } else { + rv = static_cast<int>(bytes_written); } - - return bytes_written; + return rv; } int FileStream::Context::Truncate(int64 bytes) { - if (!SetEndOfFile(file_)) { - IOResult error = IOResult::FromOSError(GetLastError()); - LOG(WARNING) << "SetEndOfFile failed: " << error.os_error; - RecordError(error, FILE_ERROR_SOURCE_SET_EOF); - return error.result; - } + BOOL result = SetEndOfFile(file_); + if (result) + return bytes; - return bytes; + DWORD error = GetLastError(); + LOG(WARNING) << "SetEndOfFile failed: " << error; + return RecordAndMapError(error, FILE_ERROR_SOURCE_SET_EOF); } void FileStream::Context::OnAsyncFileOpened() { MessageLoopForIO::current()->RegisterIOHandler(file_, this); } -FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence, - int64 offset) { +int64 FileStream::Context::SeekFileImpl(Whence whence, int64 offset) { LARGE_INTEGER distance, res; distance.QuadPart = offset; DWORD move_method = static_cast<DWORD>(whence); if (SetFilePointerEx(file_, distance, &res, move_method)) { SetOffset(&io_context_.overlapped, res); - return IOResult(res.QuadPart, 0); + return res.QuadPart; } - return IOResult::FromOSError(GetLastError()); + return -static_cast<int>(GetLastError()); } -FileStream::Context::IOResult FileStream::Context::FlushFileImpl() { +int64 FileStream::Context::FlushFileImpl() { if (FlushFileBuffers(file_)) - return IOResult(OK, 0); + return OK; - return IOResult::FromOSError(GetLastError()); + return -static_cast<int>(GetLastError()); } void FileStream::Context::IOCompletionIsPending( @@ -217,17 +220,12 @@ void FileStream::Context::OnIOCompleted(MessageLoopForIO::IOContext* context, return; } - int result; - if (error == ERROR_HANDLE_EOF) { - result = 0; - } else if (error) { - IOResult error_result = IOResult::FromOSError(error); - RecordError(error_result, error_source_); - result = error_result.result; - } else { - result = bytes_read; + int result = static_cast<int>(bytes_read); + if (error && error != ERROR_HANDLE_EOF) + result = RecordAndMapError(error, error_source_); + + if (bytes_read) IncrementOffset(&io_context_.overlapped, bytes_read); - } CompletionCallback temp_callback = callback_; callback_.Reset(); |