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
This commit is contained in:
Scott Graham 2014-12-19 15:21:19 -08:00
parent 4e8a78c182
commit 9cfd2c515e
2 changed files with 18 additions and 26 deletions

View File

@ -14,17 +14,13 @@
#include "util/net/http_body.h" #include "util/net/http_body.h"
#include <fcntl.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <algorithm> #include <algorithm>
#include <limits> #include <limits>
#include "base/logging.h" #include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "util/file/file_io.h"
namespace crashpad { namespace crashpad {
@ -50,37 +46,34 @@ ssize_t StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) {
} }
FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path) FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path)
: HTTPBodyStream(), path_(path), fd_(kUnopenedFile) { : HTTPBodyStream(), path_(path), file_(), file_state_(kUnopenedFile) {
} }
FileHTTPBodyStream::~FileHTTPBodyStream() { FileHTTPBodyStream::~FileHTTPBodyStream() {
if (fd_ >= 0) {
LoggingCloseFile(fd_);
}
} }
ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) {
switch (fd_) { switch (file_state_) {
case kUnopenedFile: case kUnopenedFile:
fd_ = HANDLE_EINTR(open(path_.value().c_str(), O_RDONLY)); file_.reset(LoggingOpenFileForRead(path_));
if (fd_ < 0) { if (!file_.is_valid()) {
fd_ = kFileOpenError; file_state_ = kFileOpenError;
PLOG(ERROR) << "Cannot open " << path_.value();
return -1; return -1;
} }
file_state_ = kReading;
break; break;
case kFileOpenError: case kFileOpenError:
return -1; return -1;
case kClosedAtEOF: case kClosedAtEOF:
return 0; return 0;
default: case kReading:
break; break;
} }
ssize_t rv = ReadFile(fd_, buffer, max_len); ssize_t rv = ReadFile(file_.get(), buffer, max_len);
if (rv == 0) { if (rv == 0) {
LoggingCloseFile(fd_); file_.reset();
fd_ = kClosedAtEOF; file_state_ = kClosedAtEOF;
} else if (rv < 0) { } else if (rv < 0) {
PLOG(ERROR) << "read"; PLOG(ERROR) << "read";
} }

View File

@ -23,6 +23,7 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "util/file/file_io.h"
namespace crashpad { namespace crashpad {
@ -83,18 +84,16 @@ class FileHTTPBodyStream : public HTTPBodyStream {
ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override; ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
private: private:
enum InvalidFD { enum FileState {
kUnopenedFile = -1, kUnopenedFile,
kFileOpenError = -2, kFileOpenError,
kClosedAtEOF = -3, kClosedAtEOF,
kReading,
}; };
base::FilePath path_; base::FilePath path_;
ScopedFileHandle file_;
// If |fd_| is greater than or equal to zero, it is an opened descriptor FileState file_state_;
// from which an instance of this class is reading. If |fd_| is less than
// zero, the value corresponds to an InvalidFD value.
int fd_;
DISALLOW_COPY_AND_ASSIGN(FileHTTPBodyStream); DISALLOW_COPY_AND_ASSIGN(FileHTTPBodyStream);
}; };