Chromium Code Reviews| Index: net/base/file_stream_posix.cc |
| diff --git a/net/base/file_stream_posix.cc b/net/base/file_stream_posix.cc |
| index 68732e3aac39fa6db4e34ffe93b96c79b3d64b7f..b9cc1abf351d1f0b9390ae39a0e0bd4c3d41d584 100644 |
| --- a/net/base/file_stream_posix.cc |
| +++ b/net/base/file_stream_posix.cc |
| @@ -62,50 +62,61 @@ int64 MapErrorCode(int err) { |
| // ReadFile() is a simple wrapper around read() that handles EINTR signals and |
| // calls MapErrorCode() to map errno to net error codes. |
| -int ReadFile(base::PlatformFile file, char* buf, int buf_len) { |
| +int ReadFile(base::PlatformFile file, char* buf, int buf_len, int class_flags) { |
| base::ThreadRestrictions::AssertIOAllowed(); |
| // read(..., 0) returns 0 to indicate end-of-file. |
| // Loop in the case of getting interrupted by a signal. |
| ssize_t res = HANDLE_EINTR(read(file, buf, static_cast<size_t>(buf_len))); |
| - if (res == static_cast<ssize_t>(-1)) |
| - return MapErrorCode(errno); |
| + if (res == static_cast<ssize_t>(-1)) { |
| + int error = errno; |
| + RecordFileError(error, FILE_ERROR_TYPES_READ, class_flags); |
| + return MapErrorCode(error); |
| + } |
| return static_cast<int>(res); |
| } |
| void ReadFileTask(base::PlatformFile file, |
| char* buf, |
| int buf_len, |
| + int class_flags, |
| CompletionCallback* callback) { |
| - callback->Run(ReadFile(file, buf, buf_len)); |
| + callback->Run(ReadFile(file, buf, buf_len, class_flags)); |
| } |
| // WriteFile() is a simple wrapper around write() that handles EINTR signals and |
| // calls MapErrorCode() to map errno to net error codes. It tries to write to |
| // completion. |
| -int WriteFile(base::PlatformFile file, const char* buf, int buf_len) { |
| +int WriteFile(base::PlatformFile file, const char* buf, int buf_len, |
| + int class_flags) { |
| base::ThreadRestrictions::AssertIOAllowed(); |
| ssize_t res = HANDLE_EINTR(write(file, buf, buf_len)); |
| - if (res == -1) |
| - return MapErrorCode(errno); |
| + if (res == -1) { |
| + int error = errno; |
| + RecordFileError(error, FILE_ERROR_TYPES_WRITE, class_flags); |
| + return MapErrorCode(error); |
| + } |
| return res; |
| } |
| void WriteFileTask(base::PlatformFile file, |
| const char* buf, |
| - int buf_len, |
| + int buf_len, int class_flags, |
| CompletionCallback* callback) { |
| - callback->Run(WriteFile(file, buf, buf_len)); |
| + callback->Run(WriteFile(file, buf, buf_len, class_flags)); |
| } |
| // FlushFile() is a simple wrapper around fsync() that handles EINTR signals and |
| // calls MapErrorCode() to map errno to net error codes. It tries to flush to |
| // completion. |
| -int FlushFile(base::PlatformFile file) { |
| +int FlushFile(base::PlatformFile file, int class_flags) { |
| base::ThreadRestrictions::AssertIOAllowed(); |
| ssize_t res = HANDLE_EINTR(fsync(file)); |
| - if (res == -1) |
| - return MapErrorCode(errno); |
| + if (res == -1) { |
| + int error = errno; |
| + RecordFileError(error, FILE_ERROR_TYPES_FLUSH, class_flags); |
| + return MapErrorCode(error); |
| + } |
| return res; |
| } |
| @@ -136,15 +147,15 @@ class CancelableCallbackTask : public CancelableTask { |
| class FileStream::AsyncContext { |
| public: |
| - AsyncContext(); |
| + explicit AsyncContext(); |
|
ahendrickson
2011/08/12 16:06:24
Removed 'explicit'
|
| ~AsyncContext(); |
| // These methods post synchronous read() and write() calls to a WorkerThread. |
| void InitiateAsyncRead( |
| - base::PlatformFile file, char* buf, int buf_len, |
| + base::PlatformFile file, char* buf, int buf_len, int class_flags, |
| CompletionCallback* callback); |
| void InitiateAsyncWrite( |
| - base::PlatformFile file, const char* buf, int buf_len, |
| + base::PlatformFile file, const char* buf, int buf_len, int class_flags, |
| CompletionCallback* callback); |
| CompletionCallback* callback() const { return callback_; } |
| @@ -211,7 +222,7 @@ FileStream::AsyncContext::~AsyncContext() { |
| } |
| void FileStream::AsyncContext::InitiateAsyncRead( |
| - base::PlatformFile file, char* buf, int buf_len, |
| + base::PlatformFile file, char* buf, int buf_len, int class_flags, |
| CompletionCallback* callback) { |
| DCHECK(!callback_); |
| callback_ = callback; |
| @@ -219,13 +230,13 @@ void FileStream::AsyncContext::InitiateAsyncRead( |
| base::WorkerPool::PostTask(FROM_HERE, |
| NewRunnableFunction( |
| &ReadFileTask, |
| - file, buf, buf_len, |
| + file, buf, buf_len, class_flags, |
| &background_io_completed_callback_), |
| true /* task_is_slow */); |
| } |
| void FileStream::AsyncContext::InitiateAsyncWrite( |
| - base::PlatformFile file, const char* buf, int buf_len, |
| + base::PlatformFile file, const char* buf, int buf_len, int class_flags, |
| CompletionCallback* callback) { |
| DCHECK(!callback_); |
| callback_ = callback; |
| @@ -233,7 +244,7 @@ void FileStream::AsyncContext::InitiateAsyncWrite( |
| base::WorkerPool::PostTask(FROM_HERE, |
| NewRunnableFunction( |
| &WriteFileTask, |
| - file, buf, buf_len, |
| + file, buf, buf_len, class_flags, |
| &background_io_completed_callback_), |
| true /* task_is_slow */); |
| } |
| @@ -278,6 +289,14 @@ FileStream::FileStream() |
| DCHECK(!IsOpen()); |
| } |
| +FileStream::FileStream(int class_flags) |
| + : file_(base::kInvalidPlatformFileValue), |
| + open_flags_(0), |
| + auto_closed_(true), |
| + class_flags_(class_flags) { |
| + DCHECK(!IsOpen()); |
| +} |
| + |
| FileStream::FileStream(base::PlatformFile file, int flags) |
| : file_(file), |
| open_flags_(flags), |
| @@ -289,6 +308,18 @@ FileStream::FileStream(base::PlatformFile file, int flags) |
| } |
| } |
| +FileStream::FileStream(base::PlatformFile file, int flags, int class_flags) |
| + : file_(file), |
| + open_flags_(flags), |
| + auto_closed_(false), |
| + class_flags_(class_flags) { |
| + // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to |
| + // make sure we will perform asynchronous File IO to it. |
| + if (flags & base::PLATFORM_FILE_ASYNC) { |
| + async_context_.reset(new AsyncContext()); |
| + } |
| +} |
| + |
| FileStream::~FileStream() { |
| if (auto_closed_) |
| Close(); |
| @@ -315,6 +346,7 @@ int FileStream::Open(const FilePath& path, int open_flags) { |
| open_flags_ = open_flags; |
| file_ = base::CreatePlatformFile(path, open_flags_, NULL, NULL); |
| if (file_ == base::kInvalidPlatformFileValue) { |
| + RecordFileError(errno, FILE_ERROR_TYPES_OPEN, class_flags_); |
|
ahendrickson
2011/08/12 16:06:24
Saved errno, in case it gets changed by the record
|
| return MapErrorCode(errno); |
| } |
| @@ -332,16 +364,20 @@ bool FileStream::IsOpen() const { |
| int64 FileStream::Seek(Whence whence, int64 offset) { |
| base::ThreadRestrictions::AssertIOAllowed(); |
| - if (!IsOpen()) |
| + if (!IsOpen()) { |
| + RecordFileError(EINVAL, FILE_ERROR_TYPES_IS_NOT_OPEN, class_flags_); |
| return ERR_UNEXPECTED; |
| + } |
| // If we're in async, make sure we don't have a request in flight. |
| DCHECK(!async_context_.get() || !async_context_->callback()); |
| off_t res = lseek(file_, static_cast<off_t>(offset), |
| static_cast<int>(whence)); |
| - if (res == static_cast<off_t>(-1)) |
| + if (res == static_cast<off_t>(-1)) { |
| + RecordFileError(errno, FILE_ERROR_TYPES_SEEK, class_flags_); |
| return MapErrorCode(errno); |
| + } |
| return res; |
| } |
| @@ -349,17 +385,20 @@ int64 FileStream::Seek(Whence whence, int64 offset) { |
| int64 FileStream::Available() { |
| base::ThreadRestrictions::AssertIOAllowed(); |
| - if (!IsOpen()) |
| + if (!IsOpen()) { |
| + RecordFileError(EINVAL, FILE_ERROR_TYPES_IS_NOT_OPEN, class_flags_); |
| return ERR_UNEXPECTED; |
| + } |
| int64 cur_pos = Seek(FROM_CURRENT, 0); |
| if (cur_pos < 0) |
| return cur_pos; |
| struct stat info; |
| - if (fstat(file_, &info) != 0) |
| + if (fstat(file_, &info) != 0) { |
| + RecordFileError(errno, FILE_ERROR_TYPES_GET_SIZE, class_flags_); |
| return MapErrorCode(errno); |
| - |
| + } |
| int64 size = static_cast<int64>(info.st_size); |
| DCHECK_GT(size, cur_pos); |
| @@ -368,8 +407,10 @@ int64 FileStream::Available() { |
| int FileStream::Read( |
| char* buf, int buf_len, CompletionCallback* callback) { |
| - if (!IsOpen()) |
| + if (!IsOpen()) { |
| + RecordFileError(EINVAL, FILE_ERROR_TYPES_IS_NOT_OPEN, class_flags_); |
| return ERR_UNEXPECTED; |
| + } |
| // read(..., 0) will return 0, which indicates end-of-file. |
| DCHECK(buf_len > 0); |
| @@ -379,10 +420,11 @@ int FileStream::Read( |
| DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); |
| // If we're in async, make sure we don't have a request in flight. |
| DCHECK(!async_context_->callback()); |
| - async_context_->InitiateAsyncRead(file_, buf, buf_len, callback); |
| + async_context_->InitiateAsyncRead(file_, buf, buf_len, class_flags_, |
| + callback); |
| return ERR_IO_PENDING; |
| } else { |
| - return ReadFile(file_, buf, buf_len); |
| + return ReadFile(file_, buf, buf_len, class_flags_); |
| } |
| } |
| @@ -412,25 +454,30 @@ int FileStream::Write( |
| // write(..., 0) will return 0, which indicates end-of-file. |
| DCHECK_GT(buf_len, 0); |
| - if (!IsOpen()) |
| + if (!IsOpen()) { |
| + RecordFileError(EINVAL, FILE_ERROR_TYPES_IS_NOT_OPEN, class_flags_); |
| return ERR_UNEXPECTED; |
| + } |
| if (async_context_.get()) { |
| DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); |
| // If we're in async, make sure we don't have a request in flight. |
| DCHECK(!async_context_->callback()); |
| - async_context_->InitiateAsyncWrite(file_, buf, buf_len, callback); |
| + async_context_->InitiateAsyncWrite(file_, buf, buf_len, class_flags_, |
| + callback); |
| return ERR_IO_PENDING; |
| } else { |
| - return WriteFile(file_, buf, buf_len); |
| + return WriteFile(file_, buf, buf_len, class_flags_); |
| } |
| } |
| int64 FileStream::Truncate(int64 bytes) { |
| base::ThreadRestrictions::AssertIOAllowed(); |
| - if (!IsOpen()) |
| + if (!IsOpen()) { |
| + RecordFileError(EINVAL, FILE_ERROR_TYPES_IS_NOT_OPEN, class_flags_); |
| return ERR_UNEXPECTED; |
| + } |
| // We better be open for reading. |
| DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| @@ -442,14 +489,27 @@ int64 FileStream::Truncate(int64 bytes) { |
| // And truncate the file. |
| int result = ftruncate(file_, bytes); |
| - return result == 0 ? seek_position : MapErrorCode(errno); |
| + if (result == 0) |
| + return seek_position; |
| + |
| + RecordFileError(errno, FILE_ERROR_TYPES_SET_EOF, class_flags_); |
| + return MapErrorCode(errno); |
| } |
| int FileStream::Flush() { |
| - if (!IsOpen()) |
| + if (!IsOpen()) { |
| + RecordFileError(EINVAL, FILE_ERROR_TYPES_IS_NOT_OPEN, class_flags_); |
| return ERR_UNEXPECTED; |
| + } |
| + |
| + return FlushFile(file_, class_flags_); |
| +} |
| - return FlushFile(file_); |
| +void FileStream::EnableRecording(bool enable, int class_flags) { |
| + if (enable) |
| + class_flags_ |= class_flags; |
| + else |
| + class_flags_ &= ~class_flags; |
| } |
| } // namespace net |