refactor
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit

This commit is contained in:
2024-08-30 20:56:59 -05:00
parent cf16a1532a
commit 9f45a6ab81
8 changed files with 247 additions and 148 deletions

View File

@ -56,6 +56,8 @@ struct file_times final {
}
};
[[nodiscard]] auto change_to_process_directory() -> bool;
[[nodiscard]] inline auto
directory_exists_in_path(std::string_view path,
std::string_view sub_directory) -> bool;
@ -71,6 +73,9 @@ file_exists_in_path(std::string_view path, std::string_view file_name) -> bool;
file_exists_in_path(std::wstring_view path,
std::wstring_view file_name) -> bool;
[[nodiscard]] auto
get_free_drive_space(std::string_view path) -> std::optional<std::uint64_t>;
[[nodiscard]] auto get_time(std::string_view path,
time_type type) -> std::optional<std::uint64_t>;
@ -83,6 +88,9 @@ get_times(std::string_view path) -> std::optional<file_times>;
[[nodiscard]] auto
get_times(std::wstring_view path) -> std::optional<file_times>;
[[nodiscard]] auto
get_total_drive_space(std::string_view path) -> std::optional<std::uint64_t>;
#if defined(PROJECT_ENABLE_LIBDSM)
[[nodiscard]] auto
smb_create_and_validate_relative_path(std::string_view smb_path,

View File

@ -26,9 +26,102 @@
#include "utils/path.hpp"
#include "utils/string.hpp"
#include "utils/time.hpp"
#include "utils/unix.hpp"
#include "utils/windows.hpp"
namespace repertory::utils::file {
auto change_to_process_directory() -> bool {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
try {
#if defined(_WIN32)
std::string file_name;
file_name.resize(MAX_PATH + 1U);
::GetModuleFileNameA(nullptr, file_name.data(),
static_cast<DWORD>(file_name.size() - 1U));
auto path = utils::path::strip_to_file_name(file_name.c_str());
::SetCurrentDirectoryA(path.c_str());
#else // !defined(_WIN32)
std::string path;
path.resize(PATH_MAX + 1);
#if defined(__APPLE__)
proc_pidpath(getpid(), path.c_str(), path.size());
#else // !defined(__APPLE__)
auto res = readlink("/proc/self/exe", path.data(), path.size());
if (res == -1) {
throw std::runtime_error("failed to readlink|" + path + '|' +
std::to_string(errno));
}
#endif // defined(__APPLE__)
path = utils::path::get_parent_path(path);
res = chdir(path.c_str());
if (res != 0) {
throw std::runtime_error("failed to chdir|" + path + '|' +
std::to_string(errno));
}
#endif // defined(_WIN32)
return true;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return false;
}
auto get_free_drive_space(std::string_view path)
-> std::optional<std::uint64_t> {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
try {
#if defined(_WIN32)
ULARGE_INTEGER li{};
if (not ::GetDiskFreeSpaceEx(path.c_str(), &li, nullptr, nullptr)) {
throw std::runtime_error("failed to get free disk space|" +
std::string{path} + '|' +
std::to_string(utils::get_last_error_code()));
}
return li.QuadPart;
#endif // defined(_WIN32)
#if defined(__linux__)
struct statfs64 st {};
if (statfs64(std::string{path}.c_str(), &st) != 0) {
throw std::runtime_error("failed to get free disk space|" +
std::string{path} + '|' +
std::to_string(utils::get_last_error_code()));
}
return st.f_bfree * static_cast<std::uint64_t>(st.f_bsize);
#endif // defined(__linux__)
#if defined(__APPLE__)
struct statvfs st {};
if (statvfs(path.c_str(), &st) != 0) {
throw std::runtime_error("failed to get free disk space|" +
std::string{path} + '|' +
std::to_string(utils::get_last_error_code()));
}
return st.f_bfree * static_cast<std::uint64_t>(st.f_frsize);
#endif // defined(__APPLE__)
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return std::nullopt;
}
auto get_time(std::string_view path,
time_type type) -> std::optional<std::uint64_t> {
auto times = get_times(path);
@ -111,6 +204,53 @@ auto get_times(std::wstring_view path) -> std::optional<file_times> {
return get_times(utils::string::to_utf8(path));
}
auto get_total_drive_space(std::string_view path)
-> std::optional<std::uint64_t> {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
try {
#if defined(_WIN32)
ULARGE_INTEGER li{};
if (not ::GetDiskFreeSpaceEx(path.c_str(), nullptr, &li, nullptr)) {
throw std::runtime_error("failed to get total disk space|" +
std::string{path} + '|' +
std::to_string(utils::get_last_error_code()));
}
return li.QuadPart;
#endif // defined(_WIN32)
#if defined(__linux__)
struct statfs64 st {};
if (statfs64(std::string{path}.c_str(), &st) != 0) {
throw std::runtime_error("failed to get total disk space|" +
std::string{path} + '|' +
std::to_string(utils::get_last_error_code()));
}
return st.f_blocks * static_cast<std::uint64_t>(st.f_bsize);
#endif // defined(__linux__)
#if defined(__APPLE__)
struct statvfs st {};
if (statvfs(path.c_str(), &st) != 0) {
throw std::runtime_error("failed to get total disk space|" +
std::string{path} + '|' +
std::to_string(utils::get_last_error_code()));
}
return st.f_blocks * static_cast<std::uint64_t>(st.f_frsize);
#endif // defined(__APPLE__)
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return std::nullopt;
}
auto i_fs_item::get_time(time_type type) const -> std::optional<std::uint64_t> {
return utils::file::get_time(get_path(), type);
}