Index: net/base/file_stream_posix.cc |
=================================================================== |
--- net/base/file_stream_posix.cc (revision 4000) |
+++ net/base/file_stream_posix.cc (working copy) |
@@ -5,7 +5,7 @@ |
// For 64-bit file access (off_t = off64_t, lseek64, etc). |
#define _FILE_OFFSET_BITS 64 |
-#include "net/base/file_input_stream.h" |
+#include "net/base/file_stream.h" |
#include <sys/types.h> |
#include <sys/stat.h> |
@@ -29,10 +29,10 @@ |
namespace net { |
-// FileInputStream::AsyncContext ---------------------------------------------- |
+// FileStream::AsyncContext ---------------------------------------------- |
// TODO(deanm): Figure out how to best do async IO. |
-class FileInputStream::AsyncContext { |
+class FileStream::AsyncContext { |
public: |
CompletionCallback* callback() const { return NULL; } |
@@ -41,22 +41,22 @@ |
DISALLOW_COPY_AND_ASSIGN(AsyncContext); |
}; |
-// FileInputStream ------------------------------------------------------------ |
+// FileStream ------------------------------------------------------------ |
-FileInputStream::FileInputStream() : fd_(-1) { |
+FileStream::FileStream() : file_(base::kInvalidPlatformFileValue) { |
DCHECK(!IsOpen()); |
} |
-FileInputStream::~FileInputStream() { |
+FileStream::~FileStream() { |
Close(); |
} |
-void FileInputStream::Close() { |
- if (fd_ != -1) { |
- if (close(fd_) != 0) { |
+void FileStream::Close() { |
+ if (file_ != base::kInvalidPlatformFileValue) { |
+ if (close(file_) != 0) { |
NOTREACHED(); |
} |
- fd_ = -1; |
+ file_ = base::kInvalidPlatformFileValue; |
} |
async_context_.reset(); |
} |
@@ -74,34 +74,42 @@ |
} |
} |
-int FileInputStream::Open(const std::wstring& path, bool asynchronous_mode) { |
- // We don't need O_LARGEFILE here since we set the 64-bit off_t feature. |
- fd_ = open(WideToUTF8(path).c_str(), 0, O_RDONLY); |
- if (fd_ == -1) |
+int FileStream::Open(const std::wstring& path, int open_flags) { |
+ if (IsOpen()) { |
+ DLOG(FATAL) << "File is already open!"; |
+ return ERR_UNEXPECTED; |
+ } |
+ |
+ open_flags_ = open_flags; |
+ file_ = base::CreatePlatformFile(path, open_flags_, NULL); |
+ if (file_ == base::kInvalidPlatformFileValue) { |
+ LOG(WARNING) << "Failed to open file: " << errno; |
return MapErrorCode(errno); |
+ } |
return OK; |
} |
-bool FileInputStream::IsOpen() const { |
- return fd_ != -1; |
+bool FileStream::IsOpen() const { |
+ return file_ != base::kInvalidPlatformFileValue; |
} |
-int64 FileInputStream::Seek(Whence whence, int64 offset) { |
+int64 FileStream::Seek(Whence whence, int64 offset) { |
if (!IsOpen()) |
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(fd_, static_cast<off_t>(offset), static_cast<int>(whence)); |
+ off_t res = lseek(file_, static_cast<off_t>(offset), |
+ static_cast<int>(whence)); |
if (res == static_cast<off_t>(-1)) |
return MapErrorCode(errno); |
return res; |
} |
-int64 FileInputStream::Available() { |
+int64 FileStream::Available() { |
if (!IsOpen()) |
return ERR_UNEXPECTED; |
@@ -110,7 +118,7 @@ |
return cur_pos; |
struct stat info; |
- if (fstat(fd_, &info) != 0) |
+ if (fstat(file_, &info) != 0) |
return MapErrorCode(errno); |
int64 size = static_cast<int64>(info.st_size); |
@@ -120,7 +128,7 @@ |
} |
// TODO(deanm): async. |
-int FileInputStream::Read( |
+int FileStream::Read( |
char* buf, int buf_len, CompletionCallback* callback) { |
// read(..., 0) will return 0, which indicates end-of-file. |
DCHECK(buf_len > 0 && buf_len <= SSIZE_MAX); |
@@ -130,7 +138,7 @@ |
// Loop in the case of getting interrupted by a signal. |
for (;;) { |
- ssize_t res = read(fd_, buf, static_cast<size_t>(buf_len)); |
+ ssize_t res = read(file_, buf, static_cast<size_t>(buf_len)); |
if (res == static_cast<ssize_t>(-1)) { |
if (errno == EINTR) |
continue; |
@@ -140,4 +148,30 @@ |
} |
} |
+// TODO(deanm): async. |
+int FileStream::Write( |
+ const char* buf, int buf_len, CompletionCallback* callback) { |
+ |
+ // read(..., 0) will return 0, which indicates end-of-file. |
+ DCHECK(buf_len > 0 && buf_len <= SSIZE_MAX); |
+ |
+ if (!IsOpen()) |
+ return ERR_UNEXPECTED; |
+ |
+ int total_bytes_written = 0; |
+ size_t len = static_cast<size_t>(buf_len); |
+ while (total_bytes_written < buf_len) { |
+ ssize_t res = write(file_, buf, len); |
+ if (res == static_cast<ssize_t>(-1)) { |
+ if (errno == EINTR) |
+ continue; |
+ return MapErrorCode(errno); |
+ } |
+ total_bytes_written += res; |
+ buf += res; |
+ len -= res; |
+ } |
+ return total_bytes_written; |
+} |
+ |
} // namespace net |
Property changes on: net\base\file_stream_posix.cc |
___________________________________________________________________ |
Added: svn:mergeinfo |
Merged /branches/chrome_webkit_merge_branch/net/base/file_input_stream_posix.cc:r69-2775 |