Skip to content

Commit

Permalink
RROW-1500: [C++] Do not ignore return value from truncate in MemoryM…
Browse files Browse the repository at this point in the history
…appedFile::Create

Author: Amir Malekpour <a.malekpour@gmail.com>
  • Loading branch information
amirma committed Sep 20, 2017
1 parent 903d03b commit 689aaa9
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions cpp/src/arrow/io/file.cc
Expand Up @@ -134,12 +134,13 @@ struct PlatformFilename {
};
#endif

static inline Status CheckOpenResult(int ret, int errno_actual,
const PlatformFilename& file_name) {
static inline Status CheckFileOpResult(int ret, int errno_actual,
const PlatformFilename& file_name,
const std::string& opname) {
if (ret == -1) {
// TODO: errno codes to strings
std::stringstream ss;
ss << "Failed to open local file: " << file_name.string();
ss << "Failed to " << opname << " file: " << file_name.string();
ss << " , error: " << std::strerror(errno_actual);
return Status::IOError(ss.str());
}
return Status::OK();
Expand Down Expand Up @@ -168,7 +169,7 @@ static inline Status FileOpenReadable(const PlatformFilename& file_name, int* fd
errno_actual = errno;
#endif

return CheckOpenResult(ret, errno_actual, file_name);
return CheckFileOpResult(ret, errno_actual, file_name, "open local");
}

static inline Status FileOpenWriteable(const PlatformFilename& file_name, bool write_only,
Expand Down Expand Up @@ -211,7 +212,7 @@ static inline Status FileOpenWriteable(const PlatformFilename& file_name, bool w

ret = *fd = open(file_name.c_str(), oflag, ARROW_WRITE_SHMODE);
#endif
return CheckOpenResult(ret, errno_actual, file_name);
return CheckFileOpResult(ret, errno_actual, file_name, "open local");
}

static inline Status FileTell(int fd, int64_t* pos) {
Expand Down Expand Up @@ -599,13 +600,21 @@ MemoryMappedFile::~MemoryMappedFile() {}

Status MemoryMappedFile::Create(const std::string& path, int64_t size,
std::shared_ptr<MemoryMappedFile>* out) {
int ret;
errno_t errno_actual;
std::shared_ptr<FileOutputStream> file;
RETURN_NOT_OK(FileOutputStream::Open(path, &file));

#ifdef _MSC_VER
_chsize_s(file->file_descriptor(), static_cast<size_t>(size));
errno_actual = _chsize_s(file->file_descriptor(), static_cast<size_t>(size));
ret = errno_actual == 0 ? 0 : -1;
#else
ftruncate(file->file_descriptor(), static_cast<size_t>(size));
ret = ftruncate(file->file_descriptor(), static_cast<size_t>(size));
errno_actual = errno;
#endif

RETURN_NOT_OK(CheckFileOpResult(ret, errno_actual, PlatformFilename(path), "truncate"));

RETURN_NOT_OK(file->Close());
return MemoryMappedFile::Open(path, FileMode::READWRITE, out);
}
Expand Down

0 comments on commit 689aaa9

Please sign in to comment.