From 9cfd2c515e6f709dbbd9837c0b4862411e50f601 Mon Sep 17 00:00:00 2001 From: Scott Graham Date: Fri, 19 Dec 2014 15:21:19 -0800 Subject: [PATCH] Make http_body cross-platform 'util' builds on Windows after this. R=mark@chromium.org, rsesek@chromium.org BUG=crashpad:1 Review URL: https://codereview.chromium.org/791493007 --- util/net/http_body.cc | 27 ++++++++++----------------- util/net/http_body.h | 17 ++++++++--------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/util/net/http_body.cc b/util/net/http_body.cc index e731c59..cdf810e 100644 --- a/util/net/http_body.cc +++ b/util/net/http_body.cc @@ -14,17 +14,13 @@ #include "util/net/http_body.h" -#include #include -#include #include #include #include "base/logging.h" -#include "base/posix/eintr_wrapper.h" #include "base/stl_util.h" -#include "util/file/file_io.h" namespace crashpad { @@ -50,37 +46,34 @@ ssize_t StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { } FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path) - : HTTPBodyStream(), path_(path), fd_(kUnopenedFile) { + : HTTPBodyStream(), path_(path), file_(), file_state_(kUnopenedFile) { } FileHTTPBodyStream::~FileHTTPBodyStream() { - if (fd_ >= 0) { - LoggingCloseFile(fd_); - } } ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { - switch (fd_) { + switch (file_state_) { case kUnopenedFile: - fd_ = HANDLE_EINTR(open(path_.value().c_str(), O_RDONLY)); - if (fd_ < 0) { - fd_ = kFileOpenError; - PLOG(ERROR) << "Cannot open " << path_.value(); + file_.reset(LoggingOpenFileForRead(path_)); + if (!file_.is_valid()) { + file_state_ = kFileOpenError; return -1; } + file_state_ = kReading; break; case kFileOpenError: return -1; case kClosedAtEOF: return 0; - default: + case kReading: break; } - ssize_t rv = ReadFile(fd_, buffer, max_len); + ssize_t rv = ReadFile(file_.get(), buffer, max_len); if (rv == 0) { - LoggingCloseFile(fd_); - fd_ = kClosedAtEOF; + file_.reset(); + file_state_ = kClosedAtEOF; } else if (rv < 0) { PLOG(ERROR) << "read"; } diff --git a/util/net/http_body.h b/util/net/http_body.h index 0347e63..3cc2f19 100644 --- a/util/net/http_body.h +++ b/util/net/http_body.h @@ -23,6 +23,7 @@ #include "base/basictypes.h" #include "base/files/file_path.h" +#include "util/file/file_io.h" namespace crashpad { @@ -83,18 +84,16 @@ class FileHTTPBodyStream : public HTTPBodyStream { ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override; private: - enum InvalidFD { - kUnopenedFile = -1, - kFileOpenError = -2, - kClosedAtEOF = -3, + enum FileState { + kUnopenedFile, + kFileOpenError, + kClosedAtEOF, + kReading, }; base::FilePath path_; - - // If |fd_| is greater than or equal to zero, it is an opened descriptor - // from which an instance of this class is reading. If |fd_| is less than - // zero, the value corresponds to an InvalidFD value. - int fd_; + ScopedFileHandle file_; + FileState file_state_; DISALLOW_COPY_AND_ASSIGN(FileHTTPBodyStream); };