Change file op |ssize_t|s to FileOperationResult

R=mark@chromium.org

Review URL: https://codereview.chromium.org/1416493006 .
This commit is contained in:
Scott Graham 2015-10-22 16:14:18 -07:00
parent 63916623cd
commit 3d598cdbcd
15 changed files with 70 additions and 59 deletions

View File

@ -24,12 +24,6 @@
#include <stdint.h>
#ifdef _WIN64
typedef int64_t ssize_t;
#else
typedef __w64 int ssize_t;
#endif
typedef unsigned int pid_t;
#endif // CRASHPAD_COMPAT_WIN_SYS_TYPES_H_

View File

@ -566,7 +566,7 @@ int DatabaseUtilMain(int argc, char* argv[]) {
call_error_writing_crash_report(database.get(), new_report);
char buf[4096];
ssize_t read_result;
FileOperationResult read_result;
do {
read_result = file_reader->Read(buf, sizeof(buf));
if (read_result < 0) {

View File

@ -20,8 +20,8 @@
namespace crashpad {
bool LoggingReadFile(FileHandle file, void* buffer, size_t size) {
ssize_t expect = base::checked_cast<ssize_t>(size);
ssize_t rv = ReadFile(file, buffer, size);
FileOperationResult expect = base::checked_cast<FileOperationResult>(size);
FileOperationResult rv = ReadFile(file, buffer, size);
if (rv < 0) {
PLOG(ERROR) << "read";
return false;
@ -35,8 +35,8 @@ bool LoggingReadFile(FileHandle file, void* buffer, size_t size) {
}
bool LoggingWriteFile(FileHandle file, const void* buffer, size_t size) {
ssize_t expect = base::checked_cast<ssize_t>(size);
ssize_t rv = WriteFile(file, buffer, size);
FileOperationResult expect = base::checked_cast<FileOperationResult>(size);
FileOperationResult rv = WriteFile(file, buffer, size);
if (rv < 0) {
PLOG(ERROR) << "write";
return false;
@ -59,7 +59,7 @@ void CheckedWriteFile(FileHandle file, const void* buffer, size_t size) {
void CheckedReadFileAtEOF(FileHandle file) {
char c;
ssize_t rv = ReadFile(file, &c, 1);
FileOperationResult rv = ReadFile(file, &c, 1);
if (rv < 0) {
PCHECK(rv == 0) << "read";
} else {

View File

@ -21,8 +21,8 @@
namespace crashpad {
bool FileReaderInterface::ReadExactly(void* data, size_t size) {
ssize_t expect = base::checked_cast<ssize_t>(size);
ssize_t rv = Read(data, size);
FileOperationResult expect = base::checked_cast<FileOperationResult>(size);
FileOperationResult rv = Read(data, size);
if (rv < 0) {
// Read() will have logged its own error.
return false;
@ -41,14 +41,14 @@ WeakFileHandleFileReader::WeakFileHandleFileReader(FileHandle file_handle)
WeakFileHandleFileReader::~WeakFileHandleFileReader() {
}
ssize_t WeakFileHandleFileReader::Read(void* data, size_t size) {
FileOperationResult WeakFileHandleFileReader::Read(void* data, size_t size) {
DCHECK_NE(file_handle_, kInvalidFileHandle);
// Dont use LoggingReadFile(), which insists on a full read and only returns
// a bool. This method permits short reads and returns the number of bytes
// read.
base::checked_cast<ssize_t>(size);
ssize_t rv = ReadFile(file_handle_, data, size);
base::checked_cast<FileOperationResult>(size);
FileOperationResult rv = ReadFile(file_handle_, data, size);
if (rv < 0) {
PLOG(ERROR) << "read";
return -1;
@ -88,7 +88,7 @@ void FileReader::Close() {
file_.reset();
}
ssize_t FileReader::Read(void* data, size_t size) {
FileOperationResult FileReader::Read(void* data, size_t size) {
DCHECK(file_.is_valid());
return weak_file_handle_file_reader_.Read(data, size);
}
@ -105,7 +105,7 @@ WeakStdioFileReader::WeakStdioFileReader(FILE* file)
WeakStdioFileReader::~WeakStdioFileReader() {
}
ssize_t WeakStdioFileReader::Read(void* data, size_t size) {
FileOperationResult WeakStdioFileReader::Read(void* data, size_t size) {
DCHECK(file_);
size_t rv = fread(data, 1, size, file_);

View File

@ -37,7 +37,7 @@ class FileReaderInterface : public virtual FileSeekerInterface {
//! \return The number of bytes actually read if the operation succeeded,
//! which may be `0` or any positive value less than or equal to \a size.
//! `-1` if the operation failed, with an error message logged.
virtual ssize_t Read(void* data, size_t size) = 0;
virtual FileOperationResult Read(void* data, size_t size) = 0;
//! \brief Wraps Read(), ensuring that the read succeeded and exactly \a size
//! bytes were read.
@ -69,7 +69,7 @@ class WeakFileHandleFileReader : public FileReaderInterface {
~WeakFileHandleFileReader() override;
// FileReaderInterface:
ssize_t Read(void* data, size_t size) override;
FileOperationResult Read(void* data, size_t size) override;
// FileSeekerInterface:
@ -125,7 +125,7 @@ class FileReader : public FileReaderInterface {
//!
//! \note It is only valid to call this method between a successful Open() and
//! a Close().
ssize_t Read(void* data, size_t size) override;
FileOperationResult Read(void* data, size_t size) override;
// FileSeekerInterface:
@ -160,7 +160,7 @@ class WeakStdioFileReader : public FileReaderInterface {
~WeakStdioFileReader() override;
// FileReaderInterface:
ssize_t Read(void* data, size_t size) override;
FileOperationResult Read(void* data, size_t size) override;
// FileSeekerInterface:

View File

@ -33,8 +33,9 @@ StringFile::~StringFile() {
}
void StringFile::SetString(const std::string& string) {
CHECK_LE(string.size(),
implicit_cast<size_t>(std::numeric_limits<ssize_t>::max()));
CHECK_LE(
string.size(),
implicit_cast<size_t>(std::numeric_limits<FileOperationResult>::max()));
string_ = string;
offset_ = 0;
}
@ -44,7 +45,7 @@ void StringFile::Reset() {
offset_ = 0;
}
ssize_t StringFile::Read(void* data, size_t size) {
FileOperationResult StringFile::Read(void* data, size_t size) {
DCHECK(offset_.IsValid());
const size_t offset = offset_.ValueOrDie();
@ -54,7 +55,7 @@ ssize_t StringFile::Read(void* data, size_t size) {
const size_t nread = std::min(size, string_.size() - offset);
base::CheckedNumeric<ssize_t> new_offset = offset_;
base::CheckedNumeric<FileOperationResult> new_offset = offset_;
new_offset += nread;
if (!new_offset.IsValid()) {
LOG(ERROR) << "Read(): file too large";
@ -75,7 +76,7 @@ bool StringFile::Write(const void* data, size_t size) {
string_.resize(offset);
}
base::CheckedNumeric<ssize_t> new_offset = offset_;
base::CheckedNumeric<FileOperationResult> new_offset = offset_;
new_offset += size;
if (!new_offset.IsValid()) {
LOG(ERROR) << "Write(): file too large";
@ -97,7 +98,7 @@ bool StringFile::WriteIoVec(std::vector<WritableIoVec>* iovecs) {
}
// Avoid writing anything at all if it would cause an overflow.
base::CheckedNumeric<ssize_t> new_offset = offset_;
base::CheckedNumeric<FileOperationResult> new_offset = offset_;
for (const WritableIoVec& iov : *iovecs) {
new_offset += iov.iov_len;
if (!new_offset.IsValid()) {

View File

@ -21,6 +21,7 @@
#include "base/basictypes.h"
#include "base/numerics/safe_math.h"
#include "util/file/file_io.h"
#include "util/file/file_reader.h"
#include "util/file/file_writer.h"
@ -50,7 +51,7 @@ class StringFile : public FileReaderInterface, public FileWriterInterface {
void Reset();
// FileReaderInterface:
ssize_t Read(void* data, size_t size) override;
FileOperationResult Read(void* data, size_t size) override;
// FileWriterInterface:
bool Write(const void* data, size_t size) override;

View File

@ -197,13 +197,16 @@ TEST(StringFile, WriteInvalid) {
EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR));
EXPECT_FALSE(string_file.Write(
"", implicit_cast<size_t>(std::numeric_limits<ssize_t>::max()) + 1));
"",
implicit_cast<size_t>(std::numeric_limits<FileOperationResult>::max()) +
1));
EXPECT_TRUE(string_file.string().empty());
EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR));
EXPECT_TRUE(string_file.Write("a", 1));
EXPECT_FALSE(string_file.Write(
"", implicit_cast<size_t>(std::numeric_limits<ssize_t>::max())));
"",
implicit_cast<size_t>(std::numeric_limits<FileOperationResult>::max())));
EXPECT_EQ(1u, string_file.string().size());
EXPECT_EQ("a", string_file.string());
EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR));
@ -290,7 +293,7 @@ TEST(StringFile, WriteIoVecInvalid) {
WritableIoVec iov;
EXPECT_EQ(1, string_file.Seek(1, SEEK_CUR));
iov.iov_base = "a";
iov.iov_len = std::numeric_limits<ssize_t>::max();
iov.iov_len = std::numeric_limits<FileOperationResult>::max();
iovecs.push_back(iov);
EXPECT_FALSE(string_file.WriteIoVec(&iovecs));
EXPECT_TRUE(string_file.string().empty());
@ -300,7 +303,7 @@ TEST(StringFile, WriteIoVecInvalid) {
iov.iov_base = "a";
iov.iov_len = 1;
iovecs.push_back(iov);
iov.iov_len = std::numeric_limits<ssize_t>::max() - 1;
iov.iov_len = std::numeric_limits<FileOperationResult>::max() - 1;
iovecs.push_back(iov);
EXPECT_FALSE(string_file.WriteIoVec(&iovecs));
EXPECT_TRUE(string_file.string().empty());
@ -449,7 +452,9 @@ TEST(StringFile, SeekInvalid) {
EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR));
EXPECT_LT(string_file.Seek(-1, SEEK_SET), 0);
EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR));
EXPECT_LT(string_file.Seek(std::numeric_limits<ssize_t>::min(), SEEK_SET), 0);
EXPECT_LT(string_file.Seek(std::numeric_limits<FileOperationResult>::min(),
SEEK_SET),
0);
EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR));
EXPECT_LT(string_file.Seek(std::numeric_limits<FileOffset>::min(), SEEK_SET),
0);
@ -487,7 +492,8 @@ TEST(StringFile, SeekSet) {
EXPECT_EQ(10, string_file.Seek(0, SEEK_CUR));
EXPECT_FALSE(string_file.SeekSet(-1));
EXPECT_EQ(10, string_file.Seek(0, SEEK_CUR));
EXPECT_FALSE(string_file.SeekSet(std::numeric_limits<ssize_t>::min()));
EXPECT_FALSE(
string_file.SeekSet(std::numeric_limits<FileOperationResult>::min()));
EXPECT_EQ(10, string_file.Seek(0, SEEK_CUR));
EXPECT_FALSE(string_file.SeekSet(std::numeric_limits<FileOffset>::min()));
EXPECT_EQ(10, string_file.Seek(0, SEEK_CUR));

View File

@ -32,15 +32,16 @@ StringHTTPBodyStream::StringHTTPBodyStream(const std::string& string)
StringHTTPBodyStream::~StringHTTPBodyStream() {
}
ssize_t StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) {
FileOperationResult StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer,
size_t max_len) {
size_t num_bytes_remaining = string_.length() - bytes_read_;
if (num_bytes_remaining == 0) {
return num_bytes_remaining;
}
size_t num_bytes_returned =
std::min(std::min(num_bytes_remaining, max_len),
implicit_cast<size_t>(std::numeric_limits<ssize_t>::max()));
size_t num_bytes_returned = std::min(
std::min(num_bytes_remaining, max_len),
implicit_cast<size_t>(std::numeric_limits<FileOperationResult>::max()));
memcpy(buffer, &string_[bytes_read_], num_bytes_returned);
bytes_read_ += num_bytes_returned;
return num_bytes_returned;
@ -53,7 +54,8 @@ FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path)
FileHTTPBodyStream::~FileHTTPBodyStream() {
}
ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) {
FileOperationResult FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer,
size_t max_len) {
switch (file_state_) {
case kUnopenedFile:
file_.reset(LoggingOpenFileForRead(path_));
@ -71,7 +73,7 @@ ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) {
break;
}
ssize_t rv = ReadFile(file_.get(), buffer, max_len);
FileOperationResult rv = ReadFile(file_.get(), buffer, max_len);
if (rv == 0) {
file_.reset();
file_state_ = kClosedAtEOF;
@ -90,14 +92,16 @@ CompositeHTTPBodyStream::~CompositeHTTPBodyStream() {
STLDeleteContainerPointers(parts_.begin(), parts_.end());
}
ssize_t CompositeHTTPBodyStream::GetBytesBuffer(uint8_t* buffer,
size_t buffer_len) {
ssize_t max_len = std::min(
buffer_len, implicit_cast<size_t>(std::numeric_limits<ssize_t>::max()));
ssize_t bytes_copied = 0;
FileOperationResult CompositeHTTPBodyStream::GetBytesBuffer(uint8_t* buffer,
size_t buffer_len) {
FileOperationResult max_len = std::min(
buffer_len,
implicit_cast<size_t>(std::numeric_limits<FileOperationResult>::max()));
FileOperationResult bytes_copied = 0;
while (bytes_copied < max_len && current_part_ != parts_.end()) {
ssize_t this_read = (*current_part_)->GetBytesBuffer(
buffer + bytes_copied, max_len - bytes_copied);
FileOperationResult this_read =
(*current_part_)
->GetBytesBuffer(buffer + bytes_copied, max_len - bytes_copied);
if (this_read == 0) {
// If the current part has returned 0 indicating EOF, advance the current

View File

@ -42,7 +42,8 @@ class HTTPBodyStream {
//! \return On success, a positive number indicating the number of bytes
//! actually copied to \a buffer. On failure, a negative number. When
//! the stream has no more data, returns `0`.
virtual ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) = 0;
virtual FileOperationResult GetBytesBuffer(uint8_t* buffer,
size_t max_len) = 0;
protected:
HTTPBodyStream() {}
@ -60,7 +61,7 @@ class StringHTTPBodyStream : public HTTPBodyStream {
~StringHTTPBodyStream() override;
// HTTPBodyStream:
ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
FileOperationResult GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
private:
std::string string_;
@ -81,7 +82,7 @@ class FileHTTPBodyStream : public HTTPBodyStream {
~FileHTTPBodyStream() override;
// HTTPBodyStream:
ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
FileOperationResult GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
private:
enum FileState {
@ -115,7 +116,7 @@ class CompositeHTTPBodyStream : public HTTPBodyStream {
~CompositeHTTPBodyStream() override;
// HTTPBodyStream:
ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
FileOperationResult GetBytesBuffer(uint8_t* buffer, size_t max_len) override;
private:
PartsList parts_;

View File

@ -47,7 +47,7 @@ TEST(StringHTTPBodyStream, SmallString) {
std::string string("Hello, world");
StringHTTPBodyStream stream(string);
EXPECT_EQ(implicit_cast<ssize_t>(string.length()),
EXPECT_EQ(implicit_cast<FileOperationResult>(string.length()),
stream.GetBytesBuffer(buf, sizeof(buf)));
std::string actual(reinterpret_cast<const char*>(buf), string.length());

View File

@ -18,6 +18,7 @@
#include "base/memory/scoped_ptr.h"
#include "gtest/gtest.h"
#include "util/file/file_io.h"
#include "util/net/http_body.h"
namespace crashpad {
@ -31,7 +32,7 @@ std::string ReadStreamToString(HTTPBodyStream* stream, size_t buffer_size) {
scoped_ptr<uint8_t[]> buf(new uint8_t[buffer_size]);
std::string result;
ssize_t bytes_read;
FileOperationResult bytes_read;
while ((bytes_read = stream->GetBytesBuffer(buf.get(), buffer_size)) != 0) {
if (bytes_read < 0) {
ADD_FAILURE() << "Failed to read from stream: " << bytes_read;

View File

@ -22,6 +22,7 @@
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "third_party/apple_cf/CFStreamAbstract.h"
#include "util/file/file_io.h"
#include "util/misc/implicit_cast.h"
#include "util/net/http_body.h"
@ -93,7 +94,8 @@ class HTTPBodyStreamCFReadStream {
return 0;
}
ssize_t bytes_read = GetStream(info)->GetBytesBuffer(buffer, buffer_length);
FileOperationResult bytes_read =
GetStream(info)->GetBytesBuffer(buffer, buffer_length);
if (bytes_read < 0) {
error->error = -1;
error->domain = kCFStreamErrorDomainCustom;

View File

@ -121,7 +121,7 @@ class HTTPTransportTestFixture : public MultiprocessExec {
// Read until the child's stdout closes.
std::string request;
char buf[32];
ssize_t bytes_read;
FileOperationResult bytes_read;
while ((bytes_read = ReadFile(ReadPipeHandle(), buf, sizeof(buf))) != 0) {
ASSERT_GE(bytes_read, 0);
request.append(buf, bytes_read);

View File

@ -22,8 +22,9 @@
#include "base/scoped_generic.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "util/net/http_body.h"
#include "package.h"
#include "util/file/file_io.h"
#include "util/net/http_body.h"
namespace crashpad {
@ -174,7 +175,7 @@ bool HTTPTransportWin::ExecuteSynchronously(std::string* response_body) {
const size_t kBufferSize = 4096;
for (;;) {
uint8_t buffer[kBufferSize];
ssize_t bytes_to_write =
FileOperationResult bytes_to_write =
body_stream()->GetBytesBuffer(buffer, sizeof(buffer));
if (bytes_to_write == 0)
break;