Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // For 64-bit file access (off_t = off64_t, lseek64, etc). | 5 // For 64-bit file access (off_t = off64_t, lseek64, etc). |
| 6 #define _FILE_OFFSET_BITS 64 | 6 #define _FILE_OFFSET_BITS 64 |
| 7 | 7 |
| 8 #include "net/base/file_stream.h" | 8 #include "net/base/file_stream.h" |
| 9 | 9 |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
| 12 #include <fcntl.h> | 12 #include <fcntl.h> |
| 13 #include <unistd.h> | 13 #include <unistd.h> |
| 14 #include <errno.h> | 14 #include <errno.h> |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/bind_helpers.h" | 18 #include "base/bind_helpers.h" |
| 19 #include "base/callback.h" | 19 #include "base/callback.h" |
| 20 #include "base/eintr_wrapper.h" | 20 #include "base/eintr_wrapper.h" |
| 21 #include "base/file_path.h" | 21 #include "base/file_path.h" |
| 22 #include "base/logging.h" | 22 #include "base/logging.h" |
| 23 #include "base/memory/ref_counted.h" | |
| 23 #include "base/message_loop.h" | 24 #include "base/message_loop.h" |
| 24 #include "base/metrics/histogram.h" | 25 #include "base/metrics/histogram.h" |
| 25 #include "base/string_util.h" | 26 #include "base/string_util.h" |
| 26 #include "base/threading/thread_restrictions.h" | 27 #include "base/threading/thread_restrictions.h" |
| 27 #include "base/threading/worker_pool.h" | 28 #include "base/threading/worker_pool.h" |
| 28 #include "base/synchronization/waitable_event.h" | |
| 29 #include "net/base/file_stream_metrics.h" | 29 #include "net/base/file_stream_metrics.h" |
| 30 #include "net/base/file_stream_net_log_parameters.h" | 30 #include "net/base/file_stream_net_log_parameters.h" |
| 31 #include "net/base/io_buffer.h" | 31 #include "net/base/io_buffer.h" |
| 32 #include "net/base/net_errors.h" | 32 #include "net/base/net_errors.h" |
| 33 | 33 |
| 34 #if defined(OS_ANDROID) | 34 #if defined(OS_ANDROID) |
| 35 // Android's bionic libc only supports the LFS transitional API. | 35 // Android's bionic libc only supports the LFS transitional API. |
| 36 #define off_t off64_t | 36 #define off_t off64_t |
| 37 #define lseek lseek64 | 37 #define lseek lseek64 |
| 38 #define stat stat64 | 38 #define stat stat64 |
| 39 #define fstat fstat64 | 39 #define fstat fstat64 |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 namespace net { | 42 namespace net { |
| 43 | 43 |
| 44 // We cast back and forth, so make sure it's the size we're expecting. | 44 // We cast back and forth, so make sure it's the size we're expecting. |
| 45 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); | 45 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); |
| 46 | 46 |
| 47 // Make sure our Whence mappings match the system headers. | 47 // Make sure our Whence mappings match the system headers. |
| 48 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && | 48 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && |
| 49 FROM_CURRENT == SEEK_CUR && | 49 FROM_CURRENT == SEEK_CUR && |
| 50 FROM_END == SEEK_END, whence_matches_system); | 50 FROM_END == SEEK_END, whence_matches_system); |
| 51 | 51 |
| 52 namespace { | 52 class FileStreamPosix::AsyncContext { |
| 53 | 53 public: |
| 54 int RecordAndMapError(int error, | 54 explicit AsyncContext(const BoundNetLog& bound_net_log); |
| 55 FileErrorSource source, | 55 AsyncContext(base::PlatformFile file, const BoundNetLog& bound_net_log); |
| 56 bool record_uma, | 56 |
| 57 const net::BoundNetLog& bound_net_log) { | 57 // Destroys the context. It can be deleted in the method or deletion can be |
| 58 net::Error net_error = MapSystemError(error); | 58 // deferred to WorkerPool if some asynchronous operation is now in progress |
| 59 | 59 // or if auto-closing is needed. |
| 60 bound_net_log.AddEvent( | 60 void Destroy(); |
| 61 net::NetLog::TYPE_FILE_STREAM_ERROR, | 61 |
| 62 base::Bind(&NetLogFileStreamErrorCallback, | 62 bool record_uma() { return record_uma_; } |
|
willchan no longer on Chromium
2012/08/14 19:23:26
All accessors should be const member functions.
| |
| 63 source, error, net_error)); | 63 void set_record_uma(bool value) { record_uma_ = value; } |
| 64 | 64 base::PlatformFile file() { return file_; } |
| 65 RecordFileError(error, source, record_uma); | 65 bool async_in_progress() { return async_in_progress_; } |
| 66 | 66 |
| 67 int RecordAndMapError(int error, FileErrorSource source); | |
| 68 | |
| 69 // Sync and async versions of all operations | |
| 70 void OpenAsync(const FilePath& path, | |
| 71 int open_flags, | |
| 72 const CompletionCallback& callback); | |
| 73 int OpenSync(const FilePath& path, int open_flags); | |
| 74 | |
| 75 void CloseAsync(const CompletionCallback& callback); | |
| 76 void CloseSync(); | |
| 77 | |
| 78 void SeekAsync(Whence whence, | |
| 79 int64 offset, | |
| 80 const Int64CompletionCallback& callback); | |
| 81 int64 SeekSync(Whence whence, int64 offset); | |
| 82 | |
| 83 void ReadAsync(IOBuffer* buf, | |
| 84 int buf_len, | |
| 85 const CompletionCallback& callback); | |
| 86 int ReadSync(char* buf, int buf_len); | |
| 87 | |
| 88 void WriteAsync(IOBuffer* in_buf, | |
| 89 int buf_len, | |
| 90 const CompletionCallback& callback); | |
| 91 int WriteSync(const char* in_buf, int buf_len); | |
| 92 | |
| 93 private: | |
| 94 // Map system error into network error code and log it with |bound_net_log_|. | |
| 95 // Method should be called with |net_log_lock_| locked. | |
| 96 int MapAndLogError(int error, FileErrorSource source); | |
| 97 | |
| 98 // Opens a file with some network logging. | |
| 99 // The result code is written to |result|. | |
| 100 void OpenFileImpl(const FilePath& path, int open_flags, int* result); | |
| 101 | |
| 102 // Closes a file with some network logging. | |
| 103 void CloseFileImpl(); | |
| 104 | |
| 105 // Adjusts the position from where the data is read. | |
| 106 void SeekFileImpl(Whence whence, int64 offset, int64* result); | |
| 107 | |
| 108 // ReadFile() is a simple wrapper around read() that handles EINTR signals | |
| 109 // and calls RecordAndMapError() to map errno to net error codes. | |
| 110 void ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len, int* result); | |
| 111 | |
| 112 // WriteFile() is a simple wrapper around write() that handles EINTR signals | |
| 113 // and calls MapSystemError() to map errno to net error codes. It tries | |
| 114 // to write to completion. | |
| 115 void WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len, int* result); | |
| 116 | |
| 117 // Called when asynchronous Close(), Open(), Read(), Write() or Seek() | |
| 118 // is completed. |result| contains the result or a network error code. | |
| 119 template <typename R> | |
| 120 void OnAsyncCompleted(const base::Callback<void(R)>& callback, R* result); | |
|
willchan no longer on Chromium
2012/08/14 19:23:26
net/ convention is to name this function OnIOCompl
pivanof
2012/08/25 07:35:53
There is already a function with this name in Win
willchan no longer on Chromium
2012/08/26 22:33:59
http://code.google.com/searchframe#OAMlx_jo-ck/src
| |
| 121 | |
| 122 // Delete the context with asynchronous closing if necessary. | |
| 123 void DeleteAbandoned(); | |
| 124 | |
| 125 base::PlatformFile file_; | |
| 126 bool record_uma_; | |
| 127 bool async_in_progress_; | |
| 128 bool destroyed_; | |
|
willchan no longer on Chromium
2012/08/14 19:23:26
I think you should change it so that destroyed_ is
| |
| 129 base::Lock net_log_lock_; | |
|
willchan no longer on Chromium
2012/08/14 19:23:26
This looks wrong. Note that the BoundNetLog is an
mmenke
2012/08/14 20:31:01
It's safe to use a BoundNetLog on any thread, at l
willchan no longer on Chromium
2012/08/14 20:34:12
The problem is that the helper threads being used
| |
| 130 BoundNetLog bound_net_log_; | |
| 131 }; | |
| 132 | |
| 133 | |
| 134 FileStreamPosix::AsyncContext::AsyncContext(const BoundNetLog& bound_net_log) | |
| 135 : file_(base::kInvalidPlatformFileValue), | |
|
mmenke
2012/08/14 20:31:01
These should be using a 4-space indent.
willchan no longer on Chromium
2012/08/14 20:34:12
google style guide says to indent 4 spaces for ini
| |
| 136 record_uma_(false), | |
| 137 async_in_progress_(false), | |
| 138 destroyed_(false), | |
| 139 bound_net_log_(bound_net_log) { | |
| 140 } | |
| 141 | |
| 142 FileStreamPosix::AsyncContext::AsyncContext(base::PlatformFile file, | |
| 143 const BoundNetLog& bound_net_log) | |
| 144 : file_(file), | |
| 145 record_uma_(false), | |
| 146 async_in_progress_(false), | |
| 147 destroyed_(false), | |
| 148 bound_net_log_(bound_net_log) { | |
| 149 } | |
| 150 | |
| 151 void FileStreamPosix::AsyncContext::Destroy() { | |
| 152 { | |
| 153 // By locking we don't allow any operation with |bound_net_log_| to be | |
| 154 // in progress while this method is executed. Attempt to do something | |
| 155 // with |bound_net_log_| will be done either before this method or after | |
| 156 // we switch |context_->destroyed_| which will prohibit any operation on | |
| 157 // |bound_net_log_|. | |
| 158 base::AutoLock locked(net_log_lock_); | |
| 159 destroyed_ = true; | |
| 160 } | |
| 161 if (!async_in_progress_) | |
| 162 DeleteAbandoned(); | |
| 163 } | |
| 164 | |
| 165 int FileStreamPosix::AsyncContext::RecordAndMapError(int error, | |
| 166 FileErrorSource source) { | |
| 167 int net_error; | |
| 168 { | |
| 169 base::AutoLock locked(net_log_lock_); | |
| 170 net_error = MapAndLogError(error, source); | |
| 171 } | |
| 172 RecordFileError(error, source, record_uma_); | |
| 67 return net_error; | 173 return net_error; |
| 68 } | 174 } |
| 69 | 175 |
| 70 // Opens a file with some network logging. | 176 void FileStreamPosix::AsyncContext::OpenAsync( |
| 71 // The opened file and the result code are written to |file| and |result|. | 177 const FilePath& path, |
| 72 void OpenFile(const FilePath& path, | 178 int open_flags, |
| 73 int open_flags, | 179 const CompletionCallback& callback) { |
| 74 bool record_uma, | 180 DCHECK(!async_in_progress_); |
| 75 base::PlatformFile* file, | 181 |
| 76 int* result, | 182 int* result = new int(OK); |
| 77 const net::BoundNetLog& bound_net_log) { | |
| 78 std::string file_name = path.AsUTF8Unsafe(); | |
| 79 bound_net_log.BeginEvent( | |
| 80 net::NetLog::TYPE_FILE_STREAM_OPEN, | |
| 81 NetLog::StringCallback("file_name", &file_name)); | |
| 82 | |
| 83 *result = OK; | |
| 84 *file = base::CreatePlatformFile(path, open_flags, NULL, NULL); | |
| 85 if (*file == base::kInvalidPlatformFileValue) { | |
| 86 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); | |
| 87 *result = RecordAndMapError(errno, FILE_ERROR_SOURCE_OPEN, record_uma, | |
| 88 bound_net_log); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 // Opens a file using OpenFile() and then signals the completion. | |
| 93 void OpenFileAndSignal(const FilePath& path, | |
| 94 int open_flags, | |
| 95 bool record_uma, | |
| 96 base::PlatformFile* file, | |
| 97 int* result, | |
| 98 base::WaitableEvent* on_io_complete, | |
| 99 const net::BoundNetLog& bound_net_log) { | |
| 100 OpenFile(path, open_flags, record_uma, file, result, bound_net_log); | |
| 101 on_io_complete->Signal(); | |
| 102 } | |
| 103 | |
| 104 // Closes a file with some network logging. | |
| 105 void CloseFile(base::PlatformFile file, | |
| 106 const net::BoundNetLog& bound_net_log) { | |
| 107 bound_net_log.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE); | |
| 108 if (file == base::kInvalidPlatformFileValue) | |
| 109 return; | |
| 110 | |
| 111 if (!base::ClosePlatformFile(file)) | |
| 112 NOTREACHED(); | |
| 113 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); | |
| 114 } | |
| 115 | |
| 116 // Closes a file with CloseFile() and signals the completion. | |
| 117 void CloseFileAndSignal(base::PlatformFile* file, | |
| 118 base::WaitableEvent* on_io_complete, | |
| 119 const net::BoundNetLog& bound_net_log) { | |
| 120 CloseFile(*file, bound_net_log); | |
| 121 *file = base::kInvalidPlatformFileValue; | |
| 122 on_io_complete->Signal(); | |
| 123 } | |
| 124 | |
| 125 // Adjusts the position from where the data is read. | |
| 126 void SeekFile(base::PlatformFile file, | |
| 127 Whence whence, | |
| 128 int64 offset, | |
| 129 int64* result, | |
| 130 bool record_uma, | |
| 131 const net::BoundNetLog& bound_net_log) { | |
| 132 off_t res = lseek(file, static_cast<off_t>(offset), | |
| 133 static_cast<int>(whence)); | |
| 134 if (res == static_cast<off_t>(-1)) { | |
| 135 *result = RecordAndMapError(errno, | |
| 136 FILE_ERROR_SOURCE_SEEK, | |
| 137 record_uma, | |
| 138 bound_net_log); | |
| 139 return; | |
| 140 } | |
| 141 *result = res; | |
| 142 } | |
| 143 | |
| 144 // Seeks a file by calling SeekSync() and signals the completion. | |
| 145 void SeekFileAndSignal(base::PlatformFile file, | |
| 146 Whence whence, | |
| 147 int64 offset, | |
| 148 int64* result, | |
| 149 bool record_uma, | |
| 150 base::WaitableEvent* on_io_complete, | |
| 151 const net::BoundNetLog& bound_net_log) { | |
| 152 SeekFile(file, whence, offset, result, record_uma, bound_net_log); | |
| 153 on_io_complete->Signal(); | |
| 154 } | |
| 155 | |
| 156 // ReadFile() is a simple wrapper around read() that handles EINTR signals and | |
| 157 // calls MapSystemError() to map errno to net error codes. | |
| 158 void ReadFile(base::PlatformFile file, | |
| 159 char* buf, | |
| 160 int buf_len, | |
| 161 bool record_uma, | |
| 162 int* result, | |
| 163 const net::BoundNetLog& bound_net_log) { | |
| 164 base::ThreadRestrictions::AssertIOAllowed(); | |
| 165 // read(..., 0) returns 0 to indicate end-of-file. | |
| 166 | |
| 167 // Loop in the case of getting interrupted by a signal. | |
| 168 ssize_t res = HANDLE_EINTR(read(file, buf, static_cast<size_t>(buf_len))); | |
| 169 if (res == -1) { | |
| 170 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_READ, | |
| 171 record_uma, bound_net_log); | |
| 172 } | |
| 173 *result = res; | |
| 174 } | |
| 175 | |
| 176 // Reads a file using ReadFile() and signals the completion. | |
| 177 void ReadFileAndSignal(base::PlatformFile file, | |
| 178 scoped_refptr<IOBuffer> buf, | |
| 179 int buf_len, | |
| 180 bool record_uma, | |
| 181 int* result, | |
| 182 base::WaitableEvent* on_io_complete, | |
| 183 const net::BoundNetLog& bound_net_log) { | |
| 184 ReadFile(file, buf->data(), buf_len, record_uma, result, bound_net_log); | |
| 185 on_io_complete->Signal(); | |
| 186 } | |
| 187 | |
| 188 // WriteFile() is a simple wrapper around write() that handles EINTR signals and | |
| 189 // calls MapSystemError() to map errno to net error codes. It tries to write to | |
| 190 // completion. | |
| 191 void WriteFile(base::PlatformFile file, | |
| 192 const char* buf, | |
| 193 int buf_len, | |
| 194 bool record_uma, | |
| 195 int* result, | |
| 196 const net::BoundNetLog& bound_net_log) { | |
| 197 base::ThreadRestrictions::AssertIOAllowed(); | |
| 198 | |
| 199 ssize_t res = HANDLE_EINTR(write(file, buf, buf_len)); | |
| 200 if (res == -1) { | |
| 201 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_WRITE, record_uma, | |
| 202 bound_net_log); | |
| 203 } | |
| 204 *result = res; | |
| 205 } | |
| 206 | |
| 207 // Writes a file using WriteFile() and signals the completion. | |
| 208 void WriteFileAndSignal(base::PlatformFile file, | |
| 209 scoped_refptr<IOBuffer> buf, | |
| 210 int buf_len, | |
| 211 bool record_uma, | |
| 212 int* result, | |
| 213 base::WaitableEvent* on_io_complete, | |
| 214 const net::BoundNetLog& bound_net_log) { | |
| 215 WriteFile(file, buf->data(), buf_len, record_uma, result, bound_net_log); | |
| 216 on_io_complete->Signal(); | |
| 217 } | |
| 218 | |
| 219 // FlushFile() is a simple wrapper around fsync() that handles EINTR signals and | |
| 220 // calls MapSystemError() to map errno to net error codes. It tries to flush to | |
| 221 // completion. | |
| 222 int FlushFile(base::PlatformFile file, | |
| 223 bool record_uma, | |
| 224 const net::BoundNetLog& bound_net_log) { | |
| 225 base::ThreadRestrictions::AssertIOAllowed(); | |
| 226 ssize_t res = HANDLE_EINTR(fsync(file)); | |
| 227 if (res == -1) { | |
| 228 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_FLUSH, record_uma, | |
| 229 bound_net_log); | |
| 230 } | |
| 231 return res; | |
| 232 } | |
| 233 | |
| 234 // Called when Read(), Write() or Seek() is completed. | |
| 235 // |result| contains the result or a network error code. | |
| 236 template <typename R> | |
| 237 void OnIOComplete(const base::WeakPtr<FileStreamPosix>& stream, | |
| 238 const base::Callback<void(R)>& callback, | |
| 239 R* result) { | |
| 240 if (!stream.get()) | |
| 241 return; | |
| 242 | |
| 243 // Reset this before Run() as Run() may issue a new async operation. | |
| 244 stream->ResetOnIOComplete(); | |
| 245 callback.Run(*result); | |
| 246 } | |
| 247 | |
| 248 } // namespace | |
| 249 | |
| 250 // FileStreamPosix ------------------------------------------------------------ | |
| 251 | |
| 252 FileStreamPosix::FileStreamPosix(net::NetLog* net_log) | |
| 253 : file_(base::kInvalidPlatformFileValue), | |
| 254 open_flags_(0), | |
| 255 auto_closed_(true), | |
| 256 record_uma_(false), | |
| 257 bound_net_log_(net::BoundNetLog::Make(net_log, | |
| 258 net::NetLog::SOURCE_FILESTREAM)), | |
| 259 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 260 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
| 261 } | |
| 262 | |
| 263 FileStreamPosix::FileStreamPosix( | |
| 264 base::PlatformFile file, int flags, net::NetLog* net_log) | |
| 265 : file_(file), | |
| 266 open_flags_(flags), | |
| 267 auto_closed_(false), | |
| 268 record_uma_(false), | |
| 269 bound_net_log_(net::BoundNetLog::Make(net_log, | |
| 270 net::NetLog::SOURCE_FILESTREAM)), | |
| 271 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 272 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
| 273 } | |
| 274 | |
| 275 FileStreamPosix::~FileStreamPosix() { | |
| 276 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | |
| 277 // Block until the last open/close/read/write operation is complete. | |
| 278 // TODO(satorux): Ideally we should not block. crbug.com/115067 | |
| 279 WaitForIOCompletion(); | |
| 280 } | |
| 281 | |
| 282 if (auto_closed_) { | |
| 283 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | |
| 284 // Close the file in the background. | |
| 285 if (IsOpen()) { | |
| 286 const bool posted = base::WorkerPool::PostTask( | |
| 287 FROM_HERE, | |
| 288 base::Bind(&CloseFile, file_, bound_net_log_), | |
| 289 true /* task_is_slow */); | |
| 290 DCHECK(posted); | |
| 291 } | |
| 292 } else { | |
| 293 CloseSync(); | |
| 294 } | |
| 295 } | |
| 296 | |
| 297 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
| 298 } | |
| 299 | |
| 300 void FileStreamPosix::Close(const CompletionCallback& callback) { | |
| 301 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
| 302 | |
| 303 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
| 304 DCHECK(!on_io_complete_.get()); | |
| 305 on_io_complete_.reset(new base::WaitableEvent( | |
| 306 false /* manual_reset */, false /* initially_signaled */)); | |
| 307 | |
| 308 // Passing &file_ to a thread pool looks unsafe but it's safe here as the | |
| 309 // destructor ensures that the close operation is complete with | |
| 310 // WaitForIOCompletion(). See also the destructor. | |
| 311 const bool posted = base::WorkerPool::PostTaskAndReply( | 183 const bool posted = base::WorkerPool::PostTaskAndReply( |
| 312 FROM_HERE, | 184 FROM_HERE, |
| 313 base::Bind(&CloseFileAndSignal, &file_, on_io_complete_.get(), | 185 base::Bind(&AsyncContext::OpenFileImpl, base::Unretained(this), |
| 314 bound_net_log_), | 186 path, open_flags, result), |
| 315 base::Bind(&FileStreamPosix::OnClosed, | 187 base::Bind(&AsyncContext::OnAsyncCompleted<int>, |
| 316 weak_ptr_factory_.GetWeakPtr(), | 188 base::Unretained(this), |
| 317 callback), | 189 callback, base::Owned(result)), |
| 318 true /* task_is_slow */); | 190 true /* task_is_slow */); |
| 319 | |
| 320 DCHECK(posted); | 191 DCHECK(posted); |
| 192 | |
| 193 async_in_progress_ = true; | |
| 194 } | |
| 195 | |
| 196 int FileStreamPosix::AsyncContext::OpenSync(const FilePath& path, | |
| 197 int open_flags) { | |
| 198 int result = OK; | |
| 199 OpenFileImpl(path, open_flags, &result); | |
| 200 return result; | |
| 201 } | |
| 202 | |
| 203 void FileStreamPosix::AsyncContext::CloseAsync( | |
| 204 const CompletionCallback& callback) { | |
| 205 DCHECK(!async_in_progress_); | |
| 206 | |
| 207 // Value OK will never be changed in AsyncContext::CloseFile() and is needed | |
| 208 // here just to use the same AsyncContext::OnAsyncCompleted(). | |
| 209 int* result = new int(OK); | |
| 210 const bool posted = base::WorkerPool::PostTaskAndReply( | |
| 211 FROM_HERE, | |
| 212 base::Bind(&AsyncContext::CloseFileImpl, base::Unretained(this)), | |
| 213 base::Bind(&AsyncContext::OnAsyncCompleted<int>, | |
| 214 base::Unretained(this), | |
| 215 callback, base::Owned(result)), | |
| 216 true /* task_is_slow */); | |
| 217 DCHECK(posted); | |
| 218 | |
| 219 async_in_progress_ = true; | |
| 220 } | |
| 221 | |
| 222 void FileStreamPosix::AsyncContext::CloseSync() { | |
| 223 DCHECK(!async_in_progress_); | |
| 224 CloseFileImpl(); | |
| 225 } | |
| 226 | |
| 227 void FileStreamPosix::AsyncContext::SeekAsync( | |
| 228 Whence whence, | |
| 229 int64 offset, | |
| 230 const Int64CompletionCallback& callback) { | |
| 231 DCHECK(!async_in_progress_); | |
| 232 | |
| 233 int64* result = new int64(-1); | |
| 234 const bool posted = base::WorkerPool::PostTaskAndReply( | |
| 235 FROM_HERE, | |
| 236 base::Bind(&AsyncContext::SeekFileImpl, base::Unretained(this), | |
| 237 whence, offset, result), | |
| 238 base::Bind(&AsyncContext::OnAsyncCompleted<int64>, | |
| 239 base::Unretained(this), | |
| 240 callback, base::Owned(result)), | |
| 241 true /* task is slow */); | |
| 242 DCHECK(posted); | |
| 243 | |
| 244 async_in_progress_ = true; | |
| 245 } | |
| 246 | |
| 247 int64 FileStreamPosix::AsyncContext::SeekSync(Whence whence, int64 offset) { | |
| 248 off_t result = -1; | |
| 249 SeekFileImpl(whence, offset, &result); | |
| 250 return result; | |
| 251 } | |
| 252 | |
| 253 void FileStreamPosix::AsyncContext::ReadAsync( | |
| 254 IOBuffer* in_buf, | |
| 255 int buf_len, | |
| 256 const CompletionCallback& callback) { | |
| 257 DCHECK(!async_in_progress_); | |
| 258 | |
| 259 int* result = new int(OK); | |
| 260 scoped_refptr<IOBuffer> buf = in_buf; | |
| 261 const bool posted = base::WorkerPool::PostTaskAndReply( | |
| 262 FROM_HERE, | |
| 263 base::Bind(&AsyncContext::ReadFileImpl, base::Unretained(this), | |
| 264 buf, buf_len, result), | |
| 265 base::Bind(&AsyncContext::OnAsyncCompleted<int>, | |
| 266 base::Unretained(this), | |
| 267 callback, base::Owned(result)), | |
| 268 true /* task is slow */); | |
| 269 DCHECK(posted); | |
| 270 | |
| 271 async_in_progress_ = true; | |
| 272 } | |
| 273 | |
| 274 int FileStreamPosix::AsyncContext::ReadSync(char* in_buf, int buf_len) { | |
| 275 int result = OK; | |
| 276 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf); | |
| 277 ReadFileImpl(buf, buf_len, &result); | |
| 278 return result; | |
| 279 } | |
| 280 | |
| 281 void FileStreamPosix::AsyncContext::WriteAsync( | |
| 282 IOBuffer* in_buf, | |
| 283 int buf_len, | |
| 284 const CompletionCallback& callback) { | |
| 285 DCHECK(!async_in_progress_); | |
| 286 | |
| 287 int* result = new int(OK); | |
| 288 scoped_refptr<IOBuffer> buf = in_buf; | |
| 289 const bool posted = base::WorkerPool::PostTaskAndReply( | |
| 290 FROM_HERE, | |
| 291 base::Bind(&AsyncContext::WriteFileImpl, base::Unretained(this), | |
| 292 buf, buf_len, result), | |
| 293 base::Bind(&AsyncContext::OnAsyncCompleted<int>, | |
| 294 base::Unretained(this), | |
| 295 callback, base::Owned(result)), | |
| 296 true /* task is slow */); | |
| 297 DCHECK(posted); | |
| 298 | |
| 299 async_in_progress_ = true; | |
| 300 } | |
| 301 | |
| 302 int FileStreamPosix::AsyncContext::WriteSync(const char* in_buf, int buf_len) { | |
| 303 int result = OK; | |
| 304 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf); | |
| 305 WriteFileImpl(buf, buf_len, &result); | |
| 306 return result; | |
| 307 } | |
| 308 | |
| 309 int FileStreamPosix::AsyncContext::MapAndLogError(int error, | |
| 310 FileErrorSource source) { | |
| 311 net_log_lock_.AssertAcquired(); | |
| 312 // The following check is against incorrect use or bug. File descriptor | |
| 313 // shouldn't ever be closed outside of FileStream while it still tries to do | |
| 314 // something with it. | |
| 315 DCHECK(error != EBADF); | |
| 316 net::Error net_error = MapSystemError(error); | |
| 317 | |
| 318 if (!destroyed_) { | |
| 319 bound_net_log_.AddEvent( | |
| 320 net::NetLog::TYPE_FILE_STREAM_ERROR, | |
| 321 base::Bind(&NetLogFileStreamErrorCallback, | |
| 322 source, error, net_error)); | |
| 323 } | |
| 324 | |
| 325 return net_error; | |
| 326 } | |
| 327 | |
| 328 void FileStreamPosix::AsyncContext::OpenFileImpl(const FilePath& path, | |
| 329 int open_flags, | |
| 330 int* result) { | |
| 331 std::string file_name = path.AsUTF8Unsafe(); | |
| 332 { | |
| 333 base::AutoLock locked(net_log_lock_); | |
| 334 // Bail out quickly if operation was already canceled | |
| 335 if (destroyed_) | |
| 336 return; | |
| 337 | |
| 338 bound_net_log_.BeginEvent( | |
| 339 net::NetLog::TYPE_FILE_STREAM_OPEN, | |
| 340 NetLog::StringCallback("file_name", &file_name)); | |
| 341 } | |
| 342 | |
| 343 *result = OK; | |
| 344 file_ = base::CreatePlatformFile(path, open_flags, NULL, NULL); | |
| 345 if (file_ == base::kInvalidPlatformFileValue) { | |
| 346 int error = errno; | |
| 347 { | |
| 348 base::AutoLock locked(net_log_lock_); | |
| 349 if (!destroyed_) | |
| 350 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); | |
| 351 *result = MapAndLogError(error, FILE_ERROR_SOURCE_OPEN); | |
| 352 } | |
| 353 RecordFileError(error, FILE_ERROR_SOURCE_OPEN, record_uma_); | |
| 354 } | |
| 355 } | |
| 356 | |
| 357 void FileStreamPosix::AsyncContext::CloseFileImpl() { | |
| 358 { | |
| 359 base::AutoLock locked(net_log_lock_); | |
| 360 if (!destroyed_) | |
| 361 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE); | |
| 362 } | |
| 363 | |
| 364 if (file_ == base::kInvalidPlatformFileValue) | |
| 365 return; | |
| 366 if (!base::ClosePlatformFile(file_)) | |
| 367 NOTREACHED(); | |
| 368 file_ = base::kInvalidPlatformFileValue; | |
| 369 | |
| 370 { | |
| 371 base::AutoLock locked(net_log_lock_); | |
| 372 if (!destroyed_) | |
| 373 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); | |
| 374 } | |
| 375 } | |
| 376 | |
| 377 void FileStreamPosix::AsyncContext::SeekFileImpl(Whence whence, | |
| 378 int64 offset, | |
| 379 int64* result) { | |
| 380 base::ThreadRestrictions::AssertIOAllowed(); | |
| 381 | |
| 382 // If context has been already destroyed nobody waits for operation results. | |
| 383 if (destroyed_) | |
| 384 return; | |
| 385 | |
| 386 off_t res = lseek(file_, static_cast<off_t>(offset), | |
| 387 static_cast<int>(whence)); | |
| 388 if (res == static_cast<off_t>(-1)) | |
| 389 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_SEEK); | |
| 390 *result = res; | |
| 391 } | |
| 392 | |
| 393 void FileStreamPosix::AsyncContext::ReadFileImpl(scoped_refptr<IOBuffer> buf, | |
| 394 int buf_len, | |
| 395 int* result) { | |
| 396 base::ThreadRestrictions::AssertIOAllowed(); | |
| 397 | |
| 398 // If context has been already destroyed nobody waits for operation results. | |
| 399 if (destroyed_) | |
| 400 return; | |
| 401 | |
| 402 // Loop in the case of getting interrupted by a signal. | |
| 403 ssize_t res = HANDLE_EINTR(read(file_, buf->data(), | |
| 404 static_cast<size_t>(buf_len))); | |
| 405 if (res == -1) | |
| 406 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_READ); | |
| 407 *result = res; | |
| 408 } | |
| 409 | |
| 410 void FileStreamPosix::AsyncContext::WriteFileImpl(scoped_refptr<IOBuffer> buf, | |
| 411 int buf_len, | |
| 412 int* result) { | |
| 413 base::ThreadRestrictions::AssertIOAllowed(); | |
| 414 | |
| 415 // If context has been already destroyed nobody waits for operation results. | |
| 416 if (destroyed_) | |
| 417 return; | |
| 418 | |
| 419 ssize_t res = HANDLE_EINTR(write(file_, buf->data(), buf_len)); | |
| 420 if (res == -1) | |
| 421 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_WRITE); | |
| 422 *result = res; | |
| 423 } | |
| 424 | |
| 425 template <typename R> | |
| 426 void FileStreamPosix::AsyncContext::OnAsyncCompleted( | |
| 427 const base::Callback<void(R)>& callback, | |
| 428 R* result) { | |
| 429 if (destroyed_) { | |
| 430 DeleteAbandoned(); | |
| 431 } else { | |
| 432 // Reset this before Run() as Run() may issue a new async operation. | |
| 433 async_in_progress_ = false; | |
| 434 callback.Run(*result); | |
| 435 } | |
| 436 } | |
| 437 | |
| 438 void FileStreamPosix::AsyncContext::DeleteAbandoned() { | |
| 439 if (file_ != base::kInvalidPlatformFileValue) { | |
| 440 const bool posted = base::WorkerPool::PostTask( | |
| 441 FROM_HERE, | |
| 442 // Context should be deleted after closing, thus Owned(). | |
| 443 base::Bind(&AsyncContext::CloseFileImpl, base::Owned(this)), | |
| 444 true /* task_is_slow */); | |
| 445 DCHECK(posted); | |
| 446 } else { | |
| 447 delete this; | |
| 448 } | |
| 449 } | |
| 450 | |
| 451 | |
| 452 // FileStreamPosix ------------------------------------------------------------ | |
| 453 | |
| 454 FileStreamPosix::FileStreamPosix(net::NetLog* net_log) | |
| 455 : context_(NULL), | |
| 456 open_flags_(0), | |
| 457 bound_net_log_(net::BoundNetLog::Make(net_log, | |
| 458 net::NetLog::SOURCE_FILESTREAM)) { | |
| 459 context_ = new AsyncContext(bound_net_log_); | |
| 460 | |
| 461 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
| 462 } | |
| 463 | |
| 464 FileStreamPosix::FileStreamPosix(base::PlatformFile file, | |
| 465 int flags, | |
| 466 net::NetLog* net_log) | |
| 467 : context_(NULL), | |
| 468 open_flags_(flags), | |
| 469 bound_net_log_(net::BoundNetLog::Make(net_log, | |
| 470 net::NetLog::SOURCE_FILESTREAM)) { | |
| 471 context_ = new AsyncContext(file, bound_net_log_); | |
| 472 | |
| 473 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
| 474 } | |
| 475 | |
| 476 FileStreamPosix::~FileStreamPosix() { | |
| 477 if (IsOpen() && !is_async()) | |
| 478 CloseSync(); | |
| 479 context_->Destroy(); | |
| 480 | |
| 481 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
| 482 } | |
| 483 | |
| 484 void FileStreamPosix::Close(const CompletionCallback& callback) { | |
| 485 DCHECK(is_async()); | |
| 486 context_->CloseAsync(callback); | |
| 321 } | 487 } |
| 322 | 488 |
| 323 void FileStreamPosix::CloseSync() { | 489 void FileStreamPosix::CloseSync() { |
| 490 // CloseSync() should be called on the correct thread even if it eventually | |
| 491 // ends up inside CloseAndCancelAsync(). | |
| 492 base::ThreadRestrictions::AssertIOAllowed(); | |
| 493 | |
| 324 // TODO(satorux): Replace the following async stuff with a | 494 // TODO(satorux): Replace the following async stuff with a |
| 325 // DCHECK(open_flags & ASYNC) once once all async clients are migrated to | 495 // DCHECK(open_flags & ASYNC) once all async clients are migrated to |
| 326 // use Close(). crbug.com/114783 | 496 // use Close(). crbug.com/114783 |
| 327 | 497 if (!context_->async_in_progress()) { |
| 328 // Abort any existing asynchronous operations. | 498 context_->CloseSync(); |
| 329 weak_ptr_factory_.InvalidateWeakPtrs(); | 499 } else { |
| 330 // Block until the last open/read/write operation is complete. | 500 AsyncContext* old_ctx = context_; |
| 331 // TODO(satorux): Ideally we should not block. crbug.com/115067 | 501 context_ = new AsyncContext(bound_net_log_); |
| 332 WaitForIOCompletion(); | 502 context_->set_record_uma(old_ctx->record_uma()); |
| 333 | 503 old_ctx->Destroy(); |
| 334 CloseFile(file_, bound_net_log_); | 504 } |
| 335 file_ = base::kInvalidPlatformFileValue; | 505 } |
| 336 } | 506 |
| 337 | 507 int FileStreamPosix::Open(const FilePath& path, |
| 338 int FileStreamPosix::Open(const FilePath& path, int open_flags, | 508 int open_flags, |
| 339 const CompletionCallback& callback) { | 509 const CompletionCallback& callback) { |
| 340 if (IsOpen()) { | 510 if (IsOpen()) { |
| 341 DLOG(FATAL) << "File is already open!"; | 511 DLOG(FATAL) << "File is already open!"; |
| 342 return ERR_UNEXPECTED; | 512 return ERR_UNEXPECTED; |
| 343 } | 513 } |
| 344 | 514 |
| 345 open_flags_ = open_flags; | 515 open_flags_ = open_flags; |
| 346 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | 516 DCHECK(is_async()); |
| 347 | 517 context_->OpenAsync(path, open_flags, callback); |
| 348 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
| 349 DCHECK(!on_io_complete_.get()); | |
| 350 on_io_complete_.reset(new base::WaitableEvent( | |
| 351 false /* manual_reset */, false /* initially_signaled */)); | |
| 352 | |
| 353 // Passing &file_ to a thread pool looks unsafe but it's safe here as the | |
| 354 // destructor ensures that the open operation is complete with | |
| 355 // WaitForIOCompletion(). See also the destructor. | |
| 356 int* result = new int(OK); | |
| 357 const bool posted = base::WorkerPool::PostTaskAndReply( | |
| 358 FROM_HERE, | |
| 359 base::Bind(&OpenFileAndSignal, | |
| 360 path, open_flags, record_uma_, &file_, result, | |
| 361 on_io_complete_.get(), bound_net_log_), | |
| 362 base::Bind(&OnIOComplete<int>, weak_ptr_factory_.GetWeakPtr(), | |
| 363 callback, base::Owned(result)), | |
| 364 true /* task_is_slow */); | |
| 365 DCHECK(posted); | |
| 366 return ERR_IO_PENDING; | 518 return ERR_IO_PENDING; |
| 367 } | 519 } |
| 368 | 520 |
| 369 int FileStreamPosix::OpenSync(const FilePath& path, int open_flags) { | 521 int FileStreamPosix::OpenSync(const FilePath& path, int open_flags) { |
| 370 if (IsOpen()) { | 522 if (IsOpen()) { |
| 371 DLOG(FATAL) << "File is already open!"; | 523 DLOG(FATAL) << "File is already open!"; |
| 372 return ERR_UNEXPECTED; | 524 return ERR_UNEXPECTED; |
| 373 } | 525 } |
| 374 | 526 |
| 375 open_flags_ = open_flags; | 527 open_flags_ = open_flags; |
| 376 // TODO(satorux): Put a DCHECK once once all async clients are migrated | 528 // TODO(satorux): Put a DCHECK once all async clients are migrated |
| 377 // to use Open(). crbug.com/114783 | 529 // to use Open(). crbug.com/114783 |
| 378 // | 530 // |
| 379 // DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); | 531 // DCHECK(!is_async()); |
| 380 | 532 return context_->OpenSync(path, open_flags_); |
| 381 int result = OK; | |
| 382 OpenFile(path, open_flags_, record_uma_, &file_, &result, bound_net_log_); | |
| 383 return result; | |
| 384 } | 533 } |
| 385 | 534 |
| 386 bool FileStreamPosix::IsOpen() const { | 535 bool FileStreamPosix::IsOpen() const { |
| 387 return file_ != base::kInvalidPlatformFileValue; | 536 return context_->file() != base::kInvalidPlatformFileValue; |
| 388 } | 537 } |
| 389 | 538 |
| 390 int FileStreamPosix::Seek(Whence whence, int64 offset, | 539 int FileStreamPosix::Seek(Whence whence, |
| 540 int64 offset, | |
| 391 const Int64CompletionCallback& callback) { | 541 const Int64CompletionCallback& callback) { |
| 392 if (!IsOpen()) | 542 if (!IsOpen()) |
| 393 return ERR_UNEXPECTED; | 543 return ERR_UNEXPECTED; |
| 394 | 544 |
| 395 // Make sure we're async and we have no other in-flight async operations. | 545 // Make sure we're async. |
| 396 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | 546 DCHECK(is_async()); |
| 397 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | 547 context_->SeekAsync(whence, offset, callback); |
| 398 DCHECK(!on_io_complete_.get()); | |
| 399 | |
| 400 on_io_complete_.reset(new base::WaitableEvent( | |
| 401 false /* manual_reset */, false /* initially_signaled */)); | |
| 402 | |
| 403 int64* result = new int64(-1); | |
| 404 const bool posted = base::WorkerPool::PostTaskAndReply( | |
| 405 FROM_HERE, | |
| 406 base::Bind(&SeekFileAndSignal, file_, whence, offset, result, | |
| 407 record_uma_, on_io_complete_.get(), bound_net_log_), | |
| 408 base::Bind(&OnIOComplete<int64>, | |
| 409 weak_ptr_factory_.GetWeakPtr(), | |
| 410 callback, base::Owned(result)), | |
| 411 true /* task is slow */); | |
| 412 DCHECK(posted); | |
| 413 return ERR_IO_PENDING; | 548 return ERR_IO_PENDING; |
| 414 } | 549 } |
| 415 | 550 |
| 416 int64 FileStreamPosix::SeekSync(Whence whence, int64 offset) { | 551 int64 FileStreamPosix::SeekSync(Whence whence, int64 offset) { |
| 417 base::ThreadRestrictions::AssertIOAllowed(); | |
| 418 | |
| 419 if (!IsOpen()) | 552 if (!IsOpen()) |
| 420 return ERR_UNEXPECTED; | 553 return ERR_UNEXPECTED; |
| 421 | 554 |
| 422 // If we're in async, make sure we don't have a request in flight. | 555 // If we're in async, make sure we don't have a request in flight. |
| 423 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC) || | 556 DCHECK(!is_async() || !context_->async_in_progress()); |
| 424 !on_io_complete_.get()); | 557 return context_->SeekSync(whence, offset); |
| 425 | |
| 426 off_t result = -1; | |
| 427 SeekFile(file_, whence, offset, &result, record_uma_, bound_net_log_); | |
| 428 return result; | |
| 429 } | 558 } |
| 430 | 559 |
| 431 int64 FileStreamPosix::Available() { | 560 int64 FileStreamPosix::Available() { |
| 432 base::ThreadRestrictions::AssertIOAllowed(); | 561 base::ThreadRestrictions::AssertIOAllowed(); |
| 433 | 562 |
| 434 if (!IsOpen()) | 563 if (!IsOpen()) |
| 435 return ERR_UNEXPECTED; | 564 return ERR_UNEXPECTED; |
| 436 | 565 |
| 437 int64 cur_pos = SeekSync(FROM_CURRENT, 0); | 566 int64 cur_pos = SeekSync(FROM_CURRENT, 0); |
| 438 if (cur_pos < 0) | 567 if (cur_pos < 0) |
| 439 return cur_pos; | 568 return cur_pos; |
| 440 | 569 |
| 441 struct stat info; | 570 struct stat info; |
| 442 if (fstat(file_, &info) != 0) { | 571 if (fstat(context_->file(), &info) != 0) |
| 443 return RecordAndMapError(errno, | 572 return context_->RecordAndMapError(errno, FILE_ERROR_SOURCE_GET_SIZE); |
| 444 FILE_ERROR_SOURCE_GET_SIZE, | |
| 445 record_uma_, | |
| 446 bound_net_log_); | |
| 447 } | |
| 448 | 573 |
| 449 int64 size = static_cast<int64>(info.st_size); | 574 int64 size = static_cast<int64>(info.st_size); |
| 450 DCHECK_GT(size, cur_pos); | 575 DCHECK_GT(size, cur_pos); |
| 451 | 576 |
| 452 return size - cur_pos; | 577 return size - cur_pos; |
| 453 } | 578 } |
| 454 | 579 |
| 455 int FileStreamPosix::Read( | 580 int FileStreamPosix::Read(IOBuffer* buf, |
| 456 IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { | 581 int buf_len, |
| 582 const CompletionCallback& callback) { | |
| 457 if (!IsOpen()) | 583 if (!IsOpen()) |
| 458 return ERR_UNEXPECTED; | 584 return ERR_UNEXPECTED; |
| 459 | 585 |
| 460 // read(..., 0) will return 0, which indicates end-of-file. | 586 // read(..., 0) will return 0, which indicates end-of-file. |
| 461 DCHECK_GT(buf_len, 0); | 587 DCHECK_GT(buf_len, 0); |
| 462 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | 588 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); |
| 463 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | 589 DCHECK(is_async()); |
| 464 | 590 |
| 465 // Make sure we don't have a request in flight. | 591 context_->ReadAsync(buf, buf_len, callback); |
| 466 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
| 467 DCHECK(!on_io_complete_.get()); | |
| 468 | |
| 469 on_io_complete_.reset(new base::WaitableEvent( | |
| 470 false /* manual_reset */, false /* initially_signaled */)); | |
| 471 | |
| 472 int* result = new int(OK); | |
| 473 scoped_refptr<IOBuffer> buf = in_buf; | |
| 474 const bool posted = base::WorkerPool::PostTaskAndReply( | |
| 475 FROM_HERE, | |
| 476 base::Bind(&ReadFileAndSignal, file_, buf, buf_len, | |
| 477 record_uma_, result, on_io_complete_.get(), bound_net_log_), | |
| 478 base::Bind(&OnIOComplete<int>, | |
| 479 weak_ptr_factory_.GetWeakPtr(), | |
| 480 callback, base::Owned(result)), | |
| 481 true /* task is slow */); | |
| 482 DCHECK(posted); | |
| 483 return ERR_IO_PENDING; | 592 return ERR_IO_PENDING; |
| 484 } | 593 } |
| 485 | 594 |
| 486 int FileStreamPosix::ReadSync(char* buf, int buf_len) { | 595 int FileStreamPosix::ReadSync(char* buf, int buf_len) { |
| 487 if (!IsOpen()) | 596 if (!IsOpen()) |
| 488 return ERR_UNEXPECTED; | 597 return ERR_UNEXPECTED; |
| 489 | 598 |
| 490 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); | 599 DCHECK(!is_async()); |
| 491 // read(..., 0) will return 0, which indicates end-of-file. | 600 // read(..., 0) will return 0, which indicates end-of-file. |
| 492 DCHECK_GT(buf_len, 0); | 601 DCHECK_GT(buf_len, 0); |
| 493 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | 602 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); |
| 494 | 603 |
| 495 int result = OK; | 604 return context_->ReadSync(buf, buf_len); |
| 496 ReadFile(file_, buf, buf_len, record_uma_, &result, bound_net_log_); | |
| 497 return result; | |
| 498 } | 605 } |
| 499 | 606 |
| 500 int FileStreamPosix::ReadUntilComplete(char *buf, int buf_len) { | 607 int FileStreamPosix::ReadUntilComplete(char *buf, int buf_len) { |
| 501 int to_read = buf_len; | 608 int to_read = buf_len; |
| 502 int bytes_total = 0; | 609 int bytes_total = 0; |
| 503 | 610 |
| 504 do { | 611 do { |
| 505 int bytes_read = ReadSync(buf, to_read); | 612 int bytes_read = ReadSync(buf, to_read); |
| 506 if (bytes_read <= 0) { | 613 if (bytes_read <= 0) { |
| 507 if (bytes_total == 0) | 614 if (bytes_total == 0) |
| 508 return bytes_read; | 615 return bytes_read; |
| 509 | 616 |
| 510 return bytes_total; | 617 return bytes_total; |
| 511 } | 618 } |
| 512 | 619 |
| 513 bytes_total += bytes_read; | 620 bytes_total += bytes_read; |
| 514 buf += bytes_read; | 621 buf += bytes_read; |
| 515 to_read -= bytes_read; | 622 to_read -= bytes_read; |
| 516 } while (bytes_total < buf_len); | 623 } while (bytes_total < buf_len); |
| 517 | 624 |
| 518 return bytes_total; | 625 return bytes_total; |
| 519 } | 626 } |
| 520 | 627 |
| 521 int FileStreamPosix::Write( | 628 int FileStreamPosix::Write(IOBuffer* buf, |
| 522 IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { | 629 int buf_len, |
| 630 const CompletionCallback& callback) { | |
| 523 if (!IsOpen()) | 631 if (!IsOpen()) |
| 524 return ERR_UNEXPECTED; | 632 return ERR_UNEXPECTED; |
| 525 | 633 |
| 526 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | 634 DCHECK(is_async()); |
| 527 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 635 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 528 // write(..., 0) will return 0, which indicates end-of-file. | 636 // write(..., 0) will return 0, which indicates end-of-file. |
| 529 DCHECK_GT(buf_len, 0); | 637 DCHECK_GT(buf_len, 0); |
| 530 | 638 |
| 531 // Make sure we don't have a request in flight. | 639 context_->WriteAsync(buf, buf_len, callback); |
| 532 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
| 533 DCHECK(!on_io_complete_.get()); | |
| 534 on_io_complete_.reset(new base::WaitableEvent( | |
| 535 false /* manual_reset */, false /* initially_signaled */)); | |
| 536 | |
| 537 int* result = new int(OK); | |
| 538 scoped_refptr<IOBuffer> buf = in_buf; | |
| 539 const bool posted = base::WorkerPool::PostTaskAndReply( | |
| 540 FROM_HERE, | |
| 541 base::Bind(&WriteFileAndSignal, file_, buf, buf_len, | |
| 542 record_uma_, result, on_io_complete_.get(), bound_net_log_), | |
| 543 base::Bind(&OnIOComplete<int>, | |
| 544 weak_ptr_factory_.GetWeakPtr(), | |
| 545 callback, base::Owned(result)), | |
| 546 true /* task is slow */); | |
| 547 DCHECK(posted); | |
| 548 return ERR_IO_PENDING; | 640 return ERR_IO_PENDING; |
| 549 } | 641 } |
| 550 | 642 |
| 551 int FileStreamPosix::WriteSync( | 643 int FileStreamPosix::WriteSync(const char* buf, int buf_len) { |
| 552 const char* buf, int buf_len) { | |
| 553 if (!IsOpen()) | 644 if (!IsOpen()) |
| 554 return ERR_UNEXPECTED; | 645 return ERR_UNEXPECTED; |
| 555 | 646 |
| 556 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); | 647 DCHECK(!is_async()); |
| 557 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 648 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 558 // write(..., 0) will return 0, which indicates end-of-file. | 649 // write(..., 0) will return 0, which indicates end-of-file. |
| 559 DCHECK_GT(buf_len, 0); | 650 DCHECK_GT(buf_len, 0); |
| 560 | 651 |
| 561 int result = OK; | 652 return context_->WriteSync(buf, buf_len); |
| 562 WriteFile(file_, buf, buf_len, record_uma_, &result, bound_net_log_); | |
| 563 return result; | |
| 564 } | 653 } |
| 565 | 654 |
| 566 int64 FileStreamPosix::Truncate(int64 bytes) { | 655 int64 FileStreamPosix::Truncate(int64 bytes) { |
| 567 base::ThreadRestrictions::AssertIOAllowed(); | 656 base::ThreadRestrictions::AssertIOAllowed(); |
| 568 | 657 |
| 569 if (!IsOpen()) | 658 if (!IsOpen()) |
| 570 return ERR_UNEXPECTED; | 659 return ERR_UNEXPECTED; |
| 571 | 660 |
| 572 // We'd better be open for writing. | 661 // We'd better be open for writing. |
| 573 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 662 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 574 | 663 |
| 575 // Seek to the position to truncate from. | 664 // Seek to the position to truncate from. |
| 576 int64 seek_position = SeekSync(FROM_BEGIN, bytes); | 665 int64 seek_position = SeekSync(FROM_BEGIN, bytes); |
| 577 if (seek_position != bytes) | 666 if (seek_position != bytes) |
| 578 return ERR_UNEXPECTED; | 667 return ERR_UNEXPECTED; |
| 579 | 668 |
| 580 // And truncate the file. | 669 // And truncate the file. |
| 581 int result = ftruncate(file_, bytes); | 670 int result = ftruncate(context_->file(), bytes); |
| 582 if (result == 0) | 671 if (result == 0) |
| 583 return seek_position; | 672 return seek_position; |
| 584 | 673 |
| 585 return RecordAndMapError(errno, | 674 return context_->RecordAndMapError(errno, FILE_ERROR_SOURCE_SET_EOF); |
| 586 FILE_ERROR_SOURCE_SET_EOF, | |
| 587 record_uma_, | |
| 588 bound_net_log_); | |
| 589 } | 675 } |
| 590 | 676 |
| 591 int FileStreamPosix::Flush() { | 677 int FileStreamPosix::Flush() { |
| 678 base::ThreadRestrictions::AssertIOAllowed(); | |
| 679 | |
| 592 if (!IsOpen()) | 680 if (!IsOpen()) |
| 593 return ERR_UNEXPECTED; | 681 return ERR_UNEXPECTED; |
| 594 | 682 |
| 595 return FlushFile(file_, record_uma_, bound_net_log_); | 683 ssize_t res = HANDLE_EINTR(fsync(context_->file())); |
| 684 if (res == -1) | |
| 685 res = context_->RecordAndMapError(errno, FILE_ERROR_SOURCE_FLUSH); | |
| 686 return res; | |
| 596 } | 687 } |
| 597 | 688 |
| 598 void FileStreamPosix::EnableErrorStatistics() { | 689 void FileStreamPosix::EnableErrorStatistics() { |
| 599 record_uma_ = true; | 690 context_->set_record_uma(true); |
| 600 } | 691 } |
| 601 | 692 |
| 602 void FileStreamPosix::SetBoundNetLogSource( | 693 void FileStreamPosix::SetBoundNetLogSource( |
| 603 const net::BoundNetLog& owner_bound_net_log) { | 694 const net::BoundNetLog& owner_bound_net_log) { |
| 604 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && | 695 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && |
| 605 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { | 696 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { |
| 606 // Both |BoundNetLog|s are invalid. | 697 // Both |BoundNetLog|s are invalid. |
| 607 return; | 698 return; |
| 608 } | 699 } |
| 609 | 700 |
| 610 // Should never connect to itself. | 701 // Should never connect to itself. |
| 611 DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id); | 702 DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id); |
| 612 | 703 |
| 613 bound_net_log_.AddEvent( | 704 bound_net_log_.AddEvent( |
| 614 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, | 705 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, |
| 615 owner_bound_net_log.source().ToEventParametersCallback()); | 706 owner_bound_net_log.source().ToEventParametersCallback()); |
| 616 | 707 |
| 617 owner_bound_net_log.AddEvent( | 708 owner_bound_net_log.AddEvent( |
| 618 net::NetLog::TYPE_FILE_STREAM_SOURCE, | 709 net::NetLog::TYPE_FILE_STREAM_SOURCE, |
| 619 bound_net_log_.source().ToEventParametersCallback()); | 710 bound_net_log_.source().ToEventParametersCallback()); |
| 620 } | 711 } |
| 621 | 712 |
| 622 base::PlatformFile FileStreamPosix::GetPlatformFileForTesting() { | 713 base::PlatformFile FileStreamPosix::GetPlatformFileForTesting() { |
| 623 return file_; | 714 return context_->file(); |
| 624 } | |
| 625 | |
| 626 void FileStreamPosix::ResetOnIOComplete() { | |
| 627 on_io_complete_.reset(); | |
| 628 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 629 } | |
| 630 | |
| 631 void FileStreamPosix::OnClosed(const CompletionCallback& callback) { | |
| 632 file_ = base::kInvalidPlatformFileValue; | |
| 633 | |
| 634 // Reset this before Run() as Run() may issue a new async operation. | |
| 635 ResetOnIOComplete(); | |
| 636 callback.Run(OK); | |
| 637 } | |
| 638 | |
| 639 void FileStreamPosix::WaitForIOCompletion() { | |
| 640 // http://crbug.com/115067 | |
| 641 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 642 if (on_io_complete_.get()) { | |
| 643 on_io_complete_->Wait(); | |
| 644 on_io_complete_.reset(); | |
| 645 } | |
| 646 } | 715 } |
| 647 | 716 |
| 648 } // namespace net | 717 } // namespace net |
| OLD | NEW |