Skip to content

Commit

Permalink
add missing error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ban-nobuhiro committed Oct 6, 2023
1 parent 11ec85c commit 581d67b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
23 changes: 18 additions & 5 deletions src/limestone/datastore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ datastore::datastore(configuration const& conf) : location_(conf.data_locations_
LOG_LP(ERROR) << "does not have write permission for the log_location directory, path: " << location_;
throw std::runtime_error("does not have write permission for the log_location directory"); //NOLINT
}
fclose(strm); // NOLINT TODO: error check
if (fclose(strm) != 0) { // NOLINT
LOG_LP(ERROR) << "fclose failed, errno = " << errno;
throw std::runtime_error("I/O error"); //NOLINT
}
add_file(epoch_file_path_);
}

Expand Down Expand Up @@ -115,7 +118,7 @@ void datastore::switch_epoch(epoch_id_type new_epoch_id) noexcept {
update_min_epoch_id(true);
}

void datastore::update_min_epoch_id(bool from_switch_epoch) noexcept {
void datastore::update_min_epoch_id(bool from_switch_epoch) noexcept { // NOLINT(readability-function-cognitive-complexity)
auto upper_limit = epoch_id_switched_.load() - 1;
epoch_id_type max_finished_epoch = 0;

Expand Down Expand Up @@ -143,14 +146,24 @@ void datastore::update_min_epoch_id(bool from_switch_epoch) noexcept {
if (epoch_id_recorded_.compare_exchange_strong(old_epoch_id, to_be_epoch)) {
std::lock_guard<std::mutex> lock(mtx_epoch_file_);

FILE* strm = fopen(epoch_file_path_.c_str(), "a"); // NOLINT TODO: error check
FILE* strm = fopen(epoch_file_path_.c_str(), "a"); // NOLINT
if (!strm) {
LOG_LP(ERROR) << "fopen failed, errno = " << errno;
std::abort();
}
log_entry::durable_epoch(strm, static_cast<epoch_id_type>(epoch_id_informed_.load()));
fflush(strm); // NOLINT TODO: error check
if (fflush(strm) != 0) {
LOG_LP(ERROR) << "fflush failed, errno = " << errno;
std::abort();
}
if (int rc = fsync(fileno(strm)); rc != 0) {
LOG_LP(ERROR) << "fsync failed, errno = " << errno;
std::abort();
}
fclose(strm); // NOLINT TODO: error check
if (fclose(strm) != 0) { // NOLINT
LOG_LP(ERROR) << "fclose failed, errno = " << errno;
std::abort();
}
break;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/limestone/datastore_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ void datastore::create_snapshot() noexcept { // NOLINT(readability-function-cog
}
});
#endif
fclose(ostrm); // NOLINT TODO: error check
if (fclose(ostrm) != 0) { // NOLINT
LOG_LP(ERROR) << "cannot close snapshot file (" << snapshot_file << "), errno = " << errno;
std::abort();
}
}

} // namespace limestone::api
16 changes: 13 additions & 3 deletions src/limestone/log_channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ void log_channel::begin_session() noexcept {
} while (current_epoch_id_.load() != envelope_.epoch_id_switched_.load());

auto log_file = file_path();
strm_ = fopen(log_file.c_str(), "a"); // NOLINT TODO: error check
strm_ = fopen(log_file.c_str(), "a"); // NOLINT
if (!strm_) {
LOG_LP(ERROR) << "I/O error, cannot make file on " << location_ << ", errno = " << errno;
std::abort();
}
setvbuf(strm_, nullptr, _IOFBF, 1024L * 1024L); // NOLINT TODO: error check
if (!registered_) {
envelope_.add_file(log_file);
Expand All @@ -54,15 +58,21 @@ void log_channel::begin_session() noexcept {
}

void log_channel::end_session() noexcept {
fflush(strm_); // NOLINT TODO: error check
if (fflush(strm_) != 0) {
LOG_LP(ERROR) << "fflush failed, errno = " << errno;
std::abort();
}
if (int rc = fsync(fileno(strm_)); rc != 0) {
LOG_LP(ERROR) << "fsync failed, errno = " << errno;
std::abort();
}
finished_epoch_id_.store(current_epoch_id_.load());
current_epoch_id_.store(UINT64_MAX);
envelope_.update_min_epoch_id();
fclose(strm_); // NOLINT TODO: error check
if (fclose(strm_) != 0) { // NOLINT
LOG_LP(ERROR) << "fclose failed, errno = " << errno;
std::abort();
}
}

void log_channel::abort_session([[maybe_unused]] status status_code, [[maybe_unused]] const std::string& message) noexcept {
Expand Down

0 comments on commit 581d67b

Please sign in to comment.