| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // 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" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 temp->Run(result); | 113 temp->Run(result); |
| 114 } | 114 } |
| 115 | 115 |
| 116 // FileStream ------------------------------------------------------------ | 116 // FileStream ------------------------------------------------------------ |
| 117 | 117 |
| 118 FileStream::FileStream() | 118 FileStream::FileStream() |
| 119 : file_(INVALID_HANDLE_VALUE), | 119 : file_(INVALID_HANDLE_VALUE), |
| 120 open_flags_(0) { | 120 open_flags_(0) { |
| 121 } | 121 } |
| 122 | 122 |
| 123 FileStream::FileStream(base::PlatformFile file, int flags) | 123 FileStream::FileStream(base::PlatformFile file, int open_flags) |
| 124 : file_(file), | 124 : file_(INVALID_HANDLE_VALUE), |
| 125 open_flags_(flags) { | 125 open_flags_(0) { |
| 126 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to | 126 Open(file, open_flags); |
| 127 // make sure we will perform asynchronous File IO to it. | |
| 128 if (flags & base::PLATFORM_FILE_ASYNC) { | |
| 129 async_context_.reset(new AsyncContext(this)); | |
| 130 MessageLoopForIO::current()->RegisterIOHandler(file_, | |
| 131 async_context_.get()); | |
| 132 } | |
| 133 } | 127 } |
| 134 | 128 |
| 135 FileStream::~FileStream() { | 129 FileStream::~FileStream() { |
| 136 Close(); | 130 Close(); |
| 137 } | 131 } |
| 138 | 132 |
| 139 void FileStream::Close() { | 133 void FileStream::Close() { |
| 140 if (file_ != INVALID_HANDLE_VALUE) | 134 if (file_ != INVALID_HANDLE_VALUE) |
| 141 CancelIo(file_); | 135 CancelIo(file_); |
| 142 | 136 |
| 143 async_context_.reset(); | 137 async_context_.reset(); |
| 144 if (file_ != INVALID_HANDLE_VALUE) { | 138 if (file_ != INVALID_HANDLE_VALUE) { |
| 145 CloseHandle(file_); | 139 CloseHandle(file_); |
| 146 file_ = INVALID_HANDLE_VALUE; | 140 file_ = INVALID_HANDLE_VALUE; |
| 147 } | 141 } |
| 148 } | 142 } |
| 149 | 143 |
| 144 void FileStream::Release() { |
| 145 if (file_ != INVALID_HANDLE_VALUE) |
| 146 CancelIo(file_); |
| 147 async_context_.reset(); |
| 148 file_ = INVALID_HANDLE_VALUE; |
| 149 } |
| 150 |
| 150 int FileStream::Open(const FilePath& path, int open_flags) { | 151 int FileStream::Open(const FilePath& path, int open_flags) { |
| 151 if (IsOpen()) { | 152 if (IsOpen()) { |
| 152 DLOG(FATAL) << "File is already open!"; | 153 DLOG(FATAL) << "File is already open!"; |
| 153 return ERR_UNEXPECTED; | 154 return ERR_UNEXPECTED; |
| 154 } | 155 } |
| 155 | 156 |
| 156 open_flags_ = open_flags; | 157 open_flags_ = open_flags; |
| 157 file_ = base::CreatePlatformFile(path.value(), open_flags_, NULL); | 158 file_ = base::CreatePlatformFile(path.value(), open_flags_, NULL); |
| 158 if (file_ == INVALID_HANDLE_VALUE) { | 159 if (file_ == INVALID_HANDLE_VALUE) { |
| 159 DWORD error = GetLastError(); | 160 DWORD error = GetLastError(); |
| 160 LOG(WARNING) << "Failed to open file: " << error; | 161 LOG(WARNING) << "Failed to open file: " << error; |
| 161 return MapErrorCode(error); | 162 return MapErrorCode(error); |
| 162 } | 163 } |
| 163 | 164 |
| 164 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | 165 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { |
| 165 async_context_.reset(new AsyncContext(this)); | 166 async_context_.reset(new AsyncContext(this)); |
| 166 MessageLoopForIO::current()->RegisterIOHandler(file_, | 167 MessageLoopForIO::current()->RegisterIOHandler(file_, |
| 167 async_context_.get()); | 168 async_context_.get()); |
| 168 } | 169 } |
| 169 | 170 |
| 170 return OK; | 171 return OK; |
| 171 } | 172 } |
| 172 | 173 |
| 174 int FileStream::Open(base::PlatformFile file, int open_flags) { |
| 175 if (IsOpen()) { |
| 176 DLOG(FATAL) << "File is already open!"; |
| 177 return ERR_UNEXPECTED; |
| 178 } |
| 179 |
| 180 open_flags_ = open_flags; |
| 181 file_ = file; |
| 182 |
| 183 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to |
| 184 // make sure we will perform asynchronous File IO to it. |
| 185 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { |
| 186 async_context_.reset(new AsyncContext(this)); |
| 187 MessageLoopForIO::current()->RegisterIOHandler(file_, |
| 188 async_context_.get()); |
| 189 } |
| 190 |
| 191 return OK; |
| 192 } |
| 193 |
| 173 bool FileStream::IsOpen() const { | 194 bool FileStream::IsOpen() const { |
| 174 return file_ != INVALID_HANDLE_VALUE; | 195 return file_ != INVALID_HANDLE_VALUE; |
| 175 } | 196 } |
| 176 | 197 |
| 177 int64 FileStream::Seek(Whence whence, int64 offset) { | 198 int64 FileStream::Seek(Whence whence, int64 offset) { |
| 178 if (!IsOpen()) | 199 if (!IsOpen()) |
| 179 return ERR_UNEXPECTED; | 200 return ERR_UNEXPECTED; |
| 180 DCHECK(!async_context_.get() || !async_context_->callback()); | 201 DCHECK(!async_context_.get() || !async_context_->callback()); |
| 181 | 202 |
| 182 LARGE_INTEGER distance, result; | 203 LARGE_INTEGER distance, result; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 DWORD error = GetLastError(); | 337 DWORD error = GetLastError(); |
| 317 LOG(WARNING) << "SetEndOfFile failed: " << error; | 338 LOG(WARNING) << "SetEndOfFile failed: " << error; |
| 318 return MapErrorCode(error); | 339 return MapErrorCode(error); |
| 319 } | 340 } |
| 320 | 341 |
| 321 // Success. | 342 // Success. |
| 322 return seek_position; | 343 return seek_position; |
| 323 } | 344 } |
| 324 | 345 |
| 325 } // namespace net | 346 } // namespace net |
| OLD | NEW |