Skip to content

Commit

Permalink
errno2str -> safe_strerror
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis committed Jan 16, 2024
1 parent d850710 commit 5f7c09a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
9 changes: 5 additions & 4 deletions silkworm/infra/common/memory_mapped_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <gsl/util>

#include "ensure.hpp"
#include "safe_strerror.hpp"

namespace silkworm {

Expand Down Expand Up @@ -137,7 +138,7 @@ void MemoryMappedFile::cleanup() {
void MemoryMappedFile::map_existing(bool read_only) {
FileDescriptor fd = ::open(path_.c_str(), read_only ? O_RDONLY : O_RDWR);
if (fd == -1) {
throw std::runtime_error{"open failed for: " + path_.string() + " error: " + strerror(errno)};
throw std::runtime_error{"open failed for: " + path_.string() + " error: " + safe_strerror(errno)};
}
[[maybe_unused]] auto _ = gsl::finally([fd]() { ::close(fd); });

Expand All @@ -163,7 +164,7 @@ void* MemoryMappedFile::mmap(FileDescriptor fd, bool read_only) {

const auto address = ::mmap(nullptr, length_, read_only ? PROT_READ : (PROT_READ | PROT_WRITE), flags, fd, 0);
if (address == MAP_FAILED) {
throw std::runtime_error{"mmap failed for: " + path_.string() + " error: " + strerror(errno)};
throw std::runtime_error{"mmap failed for: " + path_.string() + " error: " + safe_strerror(errno)};
}

return address;
Expand All @@ -173,7 +174,7 @@ void MemoryMappedFile::unmap() {
if (address_ != nullptr) {
const int result = ::munmap(address_, length_);
if (result == -1) {
throw std::runtime_error{"munmap failed for: " + path_.string() + " error: " + strerror(errno)};
throw std::runtime_error{"munmap failed for: " + path_.string() + " error: " + safe_strerror(errno)};
}
}
}
Expand All @@ -183,7 +184,7 @@ void MemoryMappedFile::advise(int advice) {
if (result == -1) {
// Ignore not implemented in kernel error because it still works (from Erigon)
if (errno != ENOSYS) {
throw std::runtime_error{"madvise failed for: " + path_.string() + " error: " + strerror(errno)};
throw std::runtime_error{"madvise failed for: " + path_.string() + " error: " + safe_strerror(errno)};
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
limitations under the License.
*/

#include "util.hpp"
#include "safe_strerror.hpp"

namespace silkworm::etl {
namespace silkworm {

std::string errno2str(int err_code) {
char msg[64];
#if defined(_WIN32) || defined(_WIN64)
std::string safe_strerror(int err_code) {
char msg[1024];
#if defined(_WIN32)
if (strerror_s(msg, sizeof(msg), err_code) != 0) {
(void)strncpy_s(msg, "Unknown error", _TRUNCATE);
}
Expand All @@ -33,4 +33,4 @@ std::string errno2str(int err_code) {
return {msg};
}

} // namespace silkworm::etl
} // namespace silkworm
27 changes: 27 additions & 0 deletions silkworm/infra/common/safe_strerror.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2024 The Silkworm Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <string>

namespace silkworm {

//! \brief Converts a system error code into its message.
//! \remarks Thread-safe version of strerror.
std::string safe_strerror(int err_code);

} // namespace silkworm
9 changes: 5 additions & 4 deletions silkworm/node/etl/file_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <filesystem>

#include <silkworm/core/common/bytes_to_string.hpp>
#include <silkworm/infra/common/safe_strerror.hpp>

namespace silkworm::etl {

Expand All @@ -45,7 +46,7 @@ void FileProvider::flush(Buffer& buffer) {
file_.open(file_name_, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
if (!file_.is_open()) {
reset();
throw etl_error(errno2str(errno));
throw etl_error(safe_strerror(errno));
}

for (const auto& entry : entries) {
Expand All @@ -56,7 +57,7 @@ void FileProvider::flush(Buffer& buffer) {
!file_.write(byte_ptr_cast(entry.value.data()), static_cast<std::streamsize>(entry.value.size()))) {
auto err{errno};
reset();
throw etl_error(errno2str(err));
throw etl_error(safe_strerror(err));
}
}

Expand All @@ -69,7 +70,7 @@ void FileProvider::flush(Buffer& buffer) {
if (!file_.is_open()) {
auto err{errno};
reset();
throw etl_error(errno2str(err));
throw etl_error(safe_strerror(err));
}
}

Expand All @@ -90,7 +91,7 @@ std::optional<std::pair<Entry, size_t>> FileProvider::read_entry() {
!file_.read(byte_ptr_cast(entry.value.data()), head.lengths[1])) {
auto err{errno};
reset();
throw etl_error(errno2str(err));
throw etl_error(safe_strerror(err));
}

return std::make_pair(entry, id_);
Expand Down
4 changes: 0 additions & 4 deletions silkworm/node/etl/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@

namespace silkworm::etl {

//! \brief Converts a system error code into its message
//! \remarks Removes the deprecation strerror
std::string errno2str(int err_code);

class etl_error : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
Expand Down

0 comments on commit 5f7c09a

Please sign in to comment.