Skip to content

Commit

Permalink
fix(tianmu): feature: The error display is too general, and the cause…
Browse files Browse the repository at this point in the history
… of the error cannot be clearly located #706
  • Loading branch information
CodingSuen authored and mergify[bot] committed Oct 24, 2022
1 parent 97c9fb4 commit 2da2e5d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions storage/tianmu/system/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ int TianmuFile::Open(std::string const &file, int flags, mode_t mode) {
name_ = file;

fd_ = open(file.c_str(), flags, mode);
if (fd_ == -1) ThrowError(errno);
if (fd_ == -1)
throw common::TIANMUError(common::ErrorCode::FAILED,
"ErrorCode: " + std::to_string(errno) + " - " + strerror(errno));
return fd_;
}

size_t TianmuFile::Read(void *buf, size_t count) {
DEBUG_ASSERT(fd_ != -1);
auto read_bytes = read(fd_, buf, count);
if (read_bytes == -1) ThrowError(errno);
if (read_bytes == -1)
ThrowError(errno);
return read_bytes;
}

Expand Down Expand Up @@ -72,7 +75,8 @@ void TianmuFile::ReadExact(void *buf, size_t count) {
size_t read_bytes = 0;
while (read_bytes < count) {
auto rb = Read((char *)buf + read_bytes, count - read_bytes);
if (rb == 0) break;
if (rb == 0)
break;
read_bytes += rb;
}
if (read_bytes != count) {
Expand All @@ -86,7 +90,8 @@ void TianmuFile::WriteExact(const void *buf, size_t count) {
size_t total_writen_bytes = 0;
while (total_writen_bytes < count) {
auto writen_bytes = write(fd_, ((char *)buf) + total_writen_bytes, count - total_writen_bytes);
if (writen_bytes == -1) ThrowError(errno);
if (writen_bytes == -1)
ThrowError(errno);
total_writen_bytes += writen_bytes;
}
}
Expand All @@ -97,15 +102,17 @@ off_t TianmuFile::Seek(off_t pos, int whence) {

new_pos = lseek(fd_, pos, whence);

if (new_pos == (off_t)-1) ThrowError(errno);
if (new_pos == (off_t)-1)
ThrowError(errno);
return new_pos;
}

off_t TianmuFile::Tell() {
off_t pos;
DEBUG_ASSERT(fd_ != -1);
pos = lseek(fd_, 0, SEEK_CUR);
if (pos == (off_t)-1) ThrowError(errno);
if (pos == (off_t)-1)
ThrowError(errno);
return pos;
}

Expand Down

0 comments on commit 2da2e5d

Please sign in to comment.