updated build system and fixes
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit
This commit is contained in:
@ -27,6 +27,9 @@
|
||||
namespace repertory::utils::file {
|
||||
class file final {
|
||||
public:
|
||||
[[nodiscard]] static auto attach_file(native_handle handle,
|
||||
bool read_only = false) -> file;
|
||||
|
||||
[[nodiscard]] static auto open_file(std::filesystem::path path,
|
||||
bool read_only = false) -> file;
|
||||
|
||||
@ -103,31 +106,14 @@ public:
|
||||
[[nodiscard]] auto truncate(std::size_t size) -> bool;
|
||||
|
||||
[[nodiscard]] auto write(const data_buffer &data, std::uint64_t offset,
|
||||
std::size_t *total_written = nullptr) -> bool {
|
||||
return write_(reinterpret_cast<const unsigned char *>(data.data()),
|
||||
data.size() * sizeof(data_buffer::value_type), offset,
|
||||
total_written);
|
||||
}
|
||||
std::size_t *total_written = nullptr) -> bool;
|
||||
|
||||
[[nodiscard]] auto write(std::string_view data, std::uint64_t offset,
|
||||
std::size_t *total_written = nullptr) -> bool {
|
||||
return write_(reinterpret_cast<const unsigned char *>(data.data()),
|
||||
data.size(), offset, total_written);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto write(std::wstring_view data, std::uint64_t offset,
|
||||
std::size_t *total_written = nullptr) -> bool {
|
||||
return write_(reinterpret_cast<const unsigned char *>(data.data()),
|
||||
data.size() * sizeof(wchar_t), offset, total_written);
|
||||
}
|
||||
[[nodiscard]] auto write(const unsigned char *data, std::size_t to_write,
|
||||
std::size_t offset,
|
||||
std::size_t *total_written = nullptr) -> bool;
|
||||
|
||||
public:
|
||||
[[nodiscard]] operator bool() const { return file_ != nullptr; }
|
||||
|
||||
private:
|
||||
[[nodiscard]] auto write_(const unsigned char *data, std::size_t to_write,
|
||||
std::size_t offset,
|
||||
std::size_t *total_written) -> bool;
|
||||
};
|
||||
|
||||
[[nodiscard]] auto get_file_size(std::string_view path,
|
||||
|
@ -157,7 +157,9 @@ auto write_json_file(std::string_view path,
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||
|
||||
return file.write(data.dump(), 0U);
|
||||
auto json_str = data.dump();
|
||||
return file.write(reinterpret_cast<const unsigned char *>(json_str.c_str()),
|
||||
json_str.size(), 0U);
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
|
@ -27,6 +27,55 @@
|
||||
#include "utils/string.hpp"
|
||||
|
||||
namespace repertory::utils::file {
|
||||
auto file::attach_file(native_handle handle, bool read_only) -> file {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
std::string path;
|
||||
|
||||
#if defined(_WIN32)
|
||||
path.resize(MAX_PATH + 1);
|
||||
|
||||
::GetFinalPathNameByHandleA(handle, path.data(),
|
||||
static_cast<DWORD>(path.size()),
|
||||
FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
|
||||
#else // !defined(_WIN32)
|
||||
source_path.resize(PATH_MAX + 1);
|
||||
|
||||
#if defined(__APPLE__)
|
||||
fcntl(handle, F_GETPATH, source_path.data());
|
||||
#else // !defined(__APPLE__)
|
||||
readlink(("/proc/self/fd/" + std::to_string(handle)).c_str(),
|
||||
source_path.data(), source_path.size());
|
||||
#endif // defined(__APPLE__)
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
path = path.c_str();
|
||||
|
||||
#if defined(_WIN32)
|
||||
auto *ptr = _fdopen(
|
||||
static_cast<int>(_open_osfhandle(reinterpret_cast<intptr_t>(handle),
|
||||
read_only ? _O_RDONLY : _O_RDWR)),
|
||||
read_only ? "rb" : "rb+");
|
||||
#else // !defined(_WIN32)
|
||||
auto *ptr = fdopen(handle, read_only ? "rb" : "rb+");
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
return file{
|
||||
file_t{ptr},
|
||||
utils::path::absolute(path),
|
||||
};
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
auto file::open_file(std::filesystem::path path, bool read_only) -> file {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
@ -285,8 +334,8 @@ auto file::truncate(std::size_t size) -> bool {
|
||||
return ec.value() == 0;
|
||||
}
|
||||
|
||||
auto file::write_(const unsigned char *data, std::size_t to_write,
|
||||
std::size_t offset, std::size_t *total_written) -> bool {
|
||||
auto file::write(const unsigned char *data, std::size_t to_write,
|
||||
std::size_t offset, std::size_t *total_written) -> bool {
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
@ -361,4 +410,11 @@ auto file::size() const -> std::uint64_t {
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
auto file::write(const data_buffer &data, std::uint64_t offset,
|
||||
std::size_t *total_written) -> bool {
|
||||
return write(reinterpret_cast<const unsigned char *>(data.data()),
|
||||
data.size() * sizeof(data_buffer::value_type), offset,
|
||||
total_written);
|
||||
}
|
||||
} // namespace repertory::utils::file
|
||||
|
Reference in New Issue
Block a user