updated build system
This commit is contained in:
@ -27,7 +27,8 @@
|
||||
#include "utils/string.hpp"
|
||||
|
||||
namespace repertory::utils::file {
|
||||
auto file::attach_file(native_handle handle, bool read_only) -> file {
|
||||
auto file::attach_file(native_handle handle,
|
||||
bool read_only) -> std::unique_ptr<i_file> {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
@ -36,13 +37,12 @@ auto file::attach_file(native_handle handle, bool read_only) -> file {
|
||||
std::string path;
|
||||
|
||||
#if defined(_WIN32)
|
||||
path.resize(MAX_PATH + 1);
|
||||
|
||||
path.resize(repertory::max_path_length + 1);
|
||||
::GetFinalPathNameByHandleA(handle, path.data(),
|
||||
static_cast<DWORD>(path.size()),
|
||||
FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
|
||||
#else // !defined(_WIN32)
|
||||
path.resize(PATH_MAX + 1);
|
||||
path.resize(repertory::max_path_length + 1);
|
||||
|
||||
#if defined(__APPLE__)
|
||||
fcntl(handle, F_GETPATH, source_path.data());
|
||||
@ -63,54 +63,60 @@ auto file::attach_file(native_handle handle, bool read_only) -> file {
|
||||
auto *ptr = fdopen(handle, read_only ? "rb" : "rb+");
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
return file{
|
||||
return std::unique_ptr<i_file>(new file{
|
||||
file_t{ptr},
|
||||
utils::path::absolute(path),
|
||||
};
|
||||
read_only,
|
||||
});
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return {};
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto file::open_file(std::filesystem::path path, bool read_only) -> file {
|
||||
void file::open() {
|
||||
if (not is_file(path_)) {
|
||||
throw std::runtime_error("file not found: " + path_);
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
file_.reset(_fsopen(path_.c_str(), read_only_ ? "rb" : "rb+", _SH_DENYNO));
|
||||
#else // !defined(_WIN32)
|
||||
file_.reset(fopen(path_.c_str(), read_only_ ? "rb" : "rb+"));
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
auto file::open_file(std::string_view path,
|
||||
bool read_only) -> std::unique_ptr<i_file> {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
auto *ptr = new file{
|
||||
nullptr,
|
||||
utils::path::absolute(path),
|
||||
read_only,
|
||||
};
|
||||
auto new_file = std::unique_ptr<i_file>(ptr);
|
||||
|
||||
try {
|
||||
path = utils::path::absolute(path.string());
|
||||
if (not is_file(path.string())) {
|
||||
throw std::runtime_error("file not found: " + path.string());
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
auto *ptr =
|
||||
_fsopen(path.string().c_str(), read_only ? "rb" : "rb+", _SH_DENYNO);
|
||||
#else // !defined(_WIN32)
|
||||
auto *ptr = fopen(path.string().c_str(), read_only ? "rb" : "rb+");
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
return file{
|
||||
file_t{ptr},
|
||||
path,
|
||||
};
|
||||
ptr->open();
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return {};
|
||||
return new_file;
|
||||
}
|
||||
|
||||
auto file::open_or_create_file(std::filesystem::path path,
|
||||
bool read_only) -> file {
|
||||
path = utils::path::absolute(path.string());
|
||||
if (not utils::file::is_file(path.string())) {
|
||||
auto file::open_or_create_file(std::string_view path,
|
||||
bool read_only) -> std::unique_ptr<i_file> {
|
||||
auto abs_path = utils::path::absolute(path);
|
||||
if (not is_file(abs_path)) {
|
||||
#if defined(_WIN32)
|
||||
int old_mode{};
|
||||
_umask_s(077, &old_mode);
|
||||
@ -119,36 +125,44 @@ auto file::open_or_create_file(std::filesystem::path path,
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
#if defined(_WIN32)
|
||||
auto *ptr = _fsopen(path.string().c_str(), "ab+", _SH_DENYNO);
|
||||
auto *ptr = _fsopen(abs_path.c_str(), "ab+", _SH_DENYNO);
|
||||
#else // !defined(_WIN32)
|
||||
auto *ptr = fopen(path.string().c_str(), "ab+");
|
||||
auto *ptr = fopen(abs_path.c_str(), "ab+");
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
if (ptr != nullptr) {
|
||||
fclose(ptr);
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
_umask_s(old_mode, nullptr);
|
||||
#else // !defined(_WIN32)
|
||||
umask(old_mode);
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
if (ptr != nullptr) {
|
||||
fclose(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
return open_file(path, read_only);
|
||||
return open_file(abs_path, read_only);
|
||||
}
|
||||
|
||||
void file::close() {
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{mtx_};
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
file_.reset();
|
||||
}
|
||||
|
||||
void file::flush() {
|
||||
auto file::exists() const -> bool {
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{mtx_};
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
return is_file(path_);
|
||||
}
|
||||
|
||||
void file::flush() const {
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
if (file_) {
|
||||
@ -157,6 +171,10 @@ void file::flush() {
|
||||
}
|
||||
|
||||
auto file::get_handle() const -> native_handle {
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
if (file_) {
|
||||
#if defined(_WIN32)
|
||||
return reinterpret_cast<native_handle>(
|
||||
@ -169,12 +187,16 @@ auto file::get_handle() const -> native_handle {
|
||||
return INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
auto file::move_to(std::filesystem::path new_path) -> bool {
|
||||
auto file::move_to(std::string_view path) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{mtx_};
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
new_path = utils::path::absolute(new_path.string());
|
||||
auto abs_path = utils::path::absolute(path);
|
||||
|
||||
auto reopen{false};
|
||||
if (file_) {
|
||||
@ -182,19 +204,29 @@ auto file::move_to(std::filesystem::path new_path) -> bool {
|
||||
close();
|
||||
}
|
||||
|
||||
auto success{false};
|
||||
#if defined(_WIN32)
|
||||
success = !!::MoveFileExA(path_.c_str(), abs_path.c_str(),
|
||||
MOVEFILE_REPLACE_EXISTING);
|
||||
#else // !// defined(_WIN32)
|
||||
std::error_code ec{};
|
||||
std::filesystem::rename(path_, new_path, ec);
|
||||
if (not ec) {
|
||||
path_ = new_path;
|
||||
if (reopen) {
|
||||
*this = open_file(path_);
|
||||
}
|
||||
std::filesystem::rename(path_, abs_path, ec);
|
||||
success = ec.value() == 0;
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
return true;
|
||||
if (success) {
|
||||
path_ = abs_path;
|
||||
}
|
||||
|
||||
if (reopen) {
|
||||
*this = open_file(path_);
|
||||
try {
|
||||
open();
|
||||
return success;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -203,11 +235,11 @@ auto file::move_to(std::filesystem::path new_path) -> bool {
|
||||
auto file::read_all(data_buffer &data, std::uint64_t offset,
|
||||
std::size_t *total_read) -> bool {
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{mtx_};
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
data_buffer buffer;
|
||||
buffer.resize(65536U);
|
||||
buffer.resize(read_buffer_size);
|
||||
|
||||
std::size_t current_read{};
|
||||
while (read(reinterpret_cast<unsigned char *>(buffer.data()),
|
||||
@ -237,7 +269,7 @@ auto file::read_all(data_buffer &data, std::uint64_t offset,
|
||||
auto file::read(data_buffer &data, std::uint64_t offset,
|
||||
std::size_t *total_read) -> bool {
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{mtx_};
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
std::size_t bytes_read{};
|
||||
@ -255,14 +287,14 @@ auto file::read(data_buffer &data, std::uint64_t offset,
|
||||
|
||||
auto file::read(unsigned char *data, std::size_t to_read, std::uint64_t offset,
|
||||
std::size_t *total_read) -> bool {
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
if (total_read != nullptr) {
|
||||
(*total_read) = 0U;
|
||||
}
|
||||
@ -298,18 +330,30 @@ auto file::read(unsigned char *data, std::size_t to_read, std::uint64_t offset,
|
||||
|
||||
auto file::remove() -> bool {
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{mtx_};
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
close();
|
||||
|
||||
if (not exists()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
return !!::DeleteFileA(path_.c_str());
|
||||
#else // !defined(_WIN32)
|
||||
std::error_code ec{};
|
||||
return std::filesystem::remove(path_, ec);
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
auto file::truncate(std::size_t size) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{mtx_};
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
auto reopen{false};
|
||||
@ -320,23 +364,34 @@ auto file::truncate(std::size_t size) -> bool {
|
||||
|
||||
std::error_code ec{};
|
||||
std::filesystem::resize_file(path_, size, ec);
|
||||
|
||||
auto success{ec.value() == 0};
|
||||
|
||||
if (reopen) {
|
||||
*this = open_file(path_);
|
||||
try {
|
||||
open();
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
success = false;
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
|
||||
return ec.value() == 0;
|
||||
return success;
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
if (total_written != nullptr) {
|
||||
(*total_written) = 0U;
|
||||
}
|
||||
@ -374,14 +429,14 @@ auto file::write(const unsigned char *data, std::size_t to_write,
|
||||
}
|
||||
|
||||
auto file::size() const -> std::uint64_t {
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
try {
|
||||
if (file_) {
|
||||
if (fseeko(file_.get(), 0, SEEK_END) == -1) {
|
||||
@ -395,6 +450,13 @@ auto file::size() const -> std::uint64_t {
|
||||
|
||||
return static_cast<std::uint64_t>(size);
|
||||
}
|
||||
|
||||
std::uint64_t size{};
|
||||
if (not get_file_size(path_, size)) {
|
||||
throw std::runtime_error("failed to get file size");
|
||||
}
|
||||
|
||||
return size;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
|
Reference in New Issue
Block a user