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). | |
| 6 #define _FILE_OFFSET_BITS 64 | |
| 7 | |
| 8 #include "net/base/file_stream_context.h" | 5 #include "net/base/file_stream_context.h" |
| 9 | 6 |
| 10 #include <errno.h> | 7 #include <errno.h> |
| 11 #include <fcntl.h> | |
| 12 #include <sys/stat.h> | |
| 13 #include <sys/types.h> | |
| 14 #include <unistd.h> | |
| 15 | 8 |
| 16 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 17 #include "base/bind.h" | 10 #include "base/bind.h" |
| 18 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 19 #include "base/callback.h" | 12 #include "base/callback.h" |
| 20 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 21 #include "base/location.h" | 14 #include "base/location.h" |
| 22 #include "base/logging.h" | 15 #include "base/logging.h" |
| 23 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 24 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
| 25 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
| 26 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
| 27 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
| 28 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 29 | 22 |
| 30 #if defined(OS_ANDROID) | |
| 31 // Android's bionic libc only supports the LFS transitional API. | |
| 32 #define off_t off64_t | |
| 33 #define lseek lseek64 | |
| 34 #define stat stat64 | |
| 35 #define fstat fstat64 | |
| 36 #endif | |
| 37 | |
| 38 namespace net { | 23 namespace net { |
| 39 | 24 |
| 40 // We cast back and forth, so make sure it's the size we're expecting. | |
| 41 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); | |
| 42 | |
| 43 // Make sure our Whence mappings match the system headers. | |
| 44 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && | |
| 45 FROM_CURRENT == SEEK_CUR && | |
| 46 FROM_END == SEEK_END, whence_matches_system); | |
| 47 | |
| 48 FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner) | 25 FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner) |
| 49 : async_in_progress_(false), | 26 : async_in_progress_(false), |
| 50 orphaned_(false), | 27 orphaned_(false), |
|
hashimoto
2014/06/12 13:03:37
nit: Please don't leave buf_len_ uninitialized.
rvargas (doing something else)
2014/06/13 02:49:10
Done.
| |
| 51 task_runner_(task_runner) { | 28 task_runner_(task_runner) { |
| 52 } | 29 } |
| 53 | 30 |
| 54 FileStream::Context::Context(base::File file, | 31 FileStream::Context::Context(base::File file, |
| 55 const scoped_refptr<base::TaskRunner>& task_runner) | 32 const scoped_refptr<base::TaskRunner>& task_runner) |
| 56 : file_(file.Pass()), | 33 : file_(file.Pass()), |
| 57 async_in_progress_(false), | 34 async_in_progress_(false), |
| 58 orphaned_(false), | 35 orphaned_(false), |
| 59 task_runner_(task_runner) { | 36 task_runner_(task_runner) { |
| 60 } | 37 } |
| 61 | 38 |
| 62 FileStream::Context::~Context() { | 39 FileStream::Context::~Context() { |
| 63 } | 40 } |
| 64 | 41 |
| 65 int FileStream::Context::ReadAsync(IOBuffer* in_buf, | 42 int FileStream::Context::Read(IOBuffer* in_buf, |
| 66 int buf_len, | 43 int buf_len, |
| 67 const CompletionCallback& callback) { | 44 const CompletionCallback& callback) { |
| 68 DCHECK(!async_in_progress_); | 45 DCHECK(!async_in_progress_); |
| 69 | 46 |
| 70 scoped_refptr<IOBuffer> buf = in_buf; | 47 scoped_refptr<IOBuffer> buf = in_buf; |
| 48 buf_ = NULL; | |
| 71 const bool posted = base::PostTaskAndReplyWithResult( | 49 const bool posted = base::PostTaskAndReplyWithResult( |
| 72 task_runner_.get(), | 50 task_runner_.get(), |
| 73 FROM_HERE, | 51 FROM_HERE, |
| 74 base::Bind(&Context::ReadFileImpl, base::Unretained(this), buf, buf_len), | 52 base::Bind(&Context::ReadFileImpl, base::Unretained(this), buf, buf_len), |
| 75 base::Bind(&Context::OnAsyncCompleted, | 53 base::Bind(&Context::OnAsyncCompleted, |
| 76 base::Unretained(this), | 54 base::Unretained(this), |
| 77 IntToInt64(callback))); | 55 IntToInt64(callback))); |
| 78 DCHECK(posted); | 56 DCHECK(posted); |
| 79 | 57 |
| 80 async_in_progress_ = true; | 58 async_in_progress_ = true; |
| 81 return ERR_IO_PENDING; | 59 return ERR_IO_PENDING; |
| 82 } | 60 } |
| 83 | 61 |
| 84 int FileStream::Context::WriteAsync(IOBuffer* in_buf, | 62 int FileStream::Context::ReadNoBlocking(IOBuffer* buf, |
| 85 int buf_len, | 63 int buf_len, |
| 86 const CompletionCallback& callback) { | 64 const CompletionCallback& callback) { |
| 65 DCHECK(!async_in_progress_); | |
| 66 | |
| 67 buf_ = buf; | |
| 68 buf_len_ = buf_len; | |
| 69 callback_ = callback; | |
| 70 | |
| 71 base::MessageLoopForIO::current()->WatchFileDescriptor( | |
| 72 file_.GetPlatformFile(), false /* persistent */, | |
| 73 base::MessageLoopForIO::WATCH_READ, &file_watcher_, this); | |
|
hashimoto
2014/06/12 13:03:37
I'm quite unfamiliar with libevent nor details of
rvargas (doing something else)
2014/06/13 02:49:10
Good point.
I don't know if there is something to
hashimoto
2014/06/13 06:10:42
Makes sense.
Hope the comment in file_stream.h pre
| |
| 74 | |
| 75 async_in_progress_ = true; | |
| 76 return ERR_IO_PENDING; | |
| 77 } | |
| 78 | |
| 79 int FileStream::Context::Write(IOBuffer* in_buf, | |
| 80 int buf_len, | |
| 81 const CompletionCallback& callback) { | |
| 87 DCHECK(!async_in_progress_); | 82 DCHECK(!async_in_progress_); |
| 88 | 83 |
| 89 scoped_refptr<IOBuffer> buf = in_buf; | 84 scoped_refptr<IOBuffer> buf = in_buf; |
| 85 buf_ = NULL; | |
| 90 const bool posted = base::PostTaskAndReplyWithResult( | 86 const bool posted = base::PostTaskAndReplyWithResult( |
| 91 task_runner_.get(), | 87 task_runner_.get(), |
| 92 FROM_HERE, | 88 FROM_HERE, |
| 93 base::Bind(&Context::WriteFileImpl, base::Unretained(this), buf, buf_len), | 89 base::Bind(&Context::WriteFileImpl, base::Unretained(this), buf, buf_len), |
| 94 base::Bind(&Context::OnAsyncCompleted, | 90 base::Bind(&Context::OnAsyncCompleted, |
| 95 base::Unretained(this), | 91 base::Unretained(this), |
| 96 IntToInt64(callback))); | 92 IntToInt64(callback))); |
| 97 DCHECK(posted); | 93 DCHECK(posted); |
| 98 | 94 |
| 99 async_in_progress_ = true; | 95 async_in_progress_ = true; |
| 100 return ERR_IO_PENDING; | 96 return ERR_IO_PENDING; |
| 101 } | 97 } |
| 102 | 98 |
| 103 FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence, | 99 int FileStream::Context::WriteNoBlocking(IOBuffer* buf, |
| 104 int64 offset) { | 100 int buf_len, |
| 105 off_t res = lseek(file_.GetPlatformFile(), static_cast<off_t>(offset), | 101 const CompletionCallback& callback) { |
| 106 static_cast<int>(whence)); | 102 DCHECK(!async_in_progress_); |
| 107 if (res == static_cast<off_t>(-1)) | |
| 108 return IOResult::FromOSError(errno); | |
| 109 | 103 |
| 110 return IOResult(res, 0); | 104 buf_ = buf; |
| 105 buf_len_ = buf_len; | |
| 106 callback_ = callback; | |
| 107 | |
| 108 base::MessageLoopForIO::current()->WatchFileDescriptor( | |
| 109 file_.GetPlatformFile(), false /* persistent */, | |
| 110 base::MessageLoopForIO::WATCH_WRITE, &file_watcher_, this); | |
| 111 | |
| 112 async_in_progress_ = true; | |
| 113 return ERR_IO_PENDING; | |
| 111 } | 114 } |
| 112 | 115 |
| 113 FileStream::Context::IOResult FileStream::Context::FlushFileImpl() { | 116 FileStream::Context::IOResult FileStream::Context::SeekFileImpl( |
| 114 ssize_t res = HANDLE_EINTR(fsync(file_.GetPlatformFile())); | 117 base::File::Whence whence, |
| 118 int64 offset) { | |
| 119 int64 res = file_.Seek(whence, offset); | |
| 115 if (res == -1) | 120 if (res == -1) |
| 116 return IOResult::FromOSError(errno); | 121 return IOResult::FromOSError(errno); |
| 117 | 122 |
| 118 return IOResult(res, 0); | 123 return IOResult(res, 0); |
| 119 } | 124 } |
| 120 | 125 |
| 121 FileStream::Context::IOResult FileStream::Context::ReadFileImpl( | 126 FileStream::Context::IOResult FileStream::Context::ReadFileImpl( |
| 122 scoped_refptr<IOBuffer> buf, | 127 scoped_refptr<IOBuffer> buf, |
| 123 int buf_len) { | 128 int buf_len) { |
| 124 // Loop in the case of getting interrupted by a signal. | 129 int res = file_.ReadAtCurrentPosNoBestEffort(buf->data(), buf_len); |
| 125 ssize_t res = HANDLE_EINTR(read(file_.GetPlatformFile(), buf->data(), | |
| 126 static_cast<size_t>(buf_len))); | |
| 127 if (res == -1) | 130 if (res == -1) |
| 128 return IOResult::FromOSError(errno); | 131 return IOResult::FromOSError(errno); |
| 129 | 132 |
| 130 return IOResult(res, 0); | 133 return IOResult(res, 0); |
| 131 } | 134 } |
| 132 | 135 |
| 133 FileStream::Context::IOResult FileStream::Context::WriteFileImpl( | 136 FileStream::Context::IOResult FileStream::Context::WriteFileImpl( |
| 134 scoped_refptr<IOBuffer> buf, | 137 scoped_refptr<IOBuffer> buf, |
| 135 int buf_len) { | 138 int buf_len) { |
| 136 ssize_t res = HANDLE_EINTR(write(file_.GetPlatformFile(), buf->data(), | 139 int res = file_.WriteAtCurrentPosNoBestEffort(buf->data(), buf_len); |
| 137 buf_len)); | |
| 138 if (res == -1) | 140 if (res == -1) |
| 139 return IOResult::FromOSError(errno); | 141 return IOResult::FromOSError(errno); |
| 140 | 142 |
| 141 return IOResult(res, 0); | 143 return IOResult(res, 0); |
| 142 } | 144 } |
| 143 | 145 |
| 146 void FileStream::Context::OnFileCanReadWithoutBlocking(int fd) { | |
| 147 DCHECK_EQ(file_.GetPlatformFile(), fd); | |
| 148 DCHECK(async_in_progress_); | |
| 149 async_in_progress_ = false; | |
| 150 Read(buf_.get(), buf_len_, callback_); | |
| 151 } | |
| 152 | |
| 153 void FileStream::Context::OnFileCanWriteWithoutBlocking(int fd) { | |
| 154 DCHECK_EQ(file_.GetPlatformFile(), fd); | |
| 155 DCHECK(async_in_progress_); | |
| 156 async_in_progress_ = false; | |
| 157 Write(buf_.get(), buf_len_, callback_); | |
| 158 } | |
| 159 | |
| 144 } // namespace net | 160 } // namespace net |
| OLD | NEW |