updated build system
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good

This commit is contained in:
2024-08-31 16:47:57 -05:00
parent 345b200b0f
commit f68d7e2ae6
4 changed files with 99 additions and 39 deletions

View File

@ -67,9 +67,11 @@ get_free_drive_space(std::string_view path) -> std::optional<std::uint64_t>;
[[nodiscard]] auto [[nodiscard]] auto
get_free_drive_space(std::wstring_view path) -> std::optional<std::uint64_t>; get_free_drive_space(std::wstring_view path) -> std::optional<std::uint64_t>;
// INFO: has test
[[nodiscard]] auto get_time(std::string_view path, [[nodiscard]] auto get_time(std::string_view path,
time_type type) -> std::optional<std::uint64_t>; time_type type) -> std::optional<std::uint64_t>;
// INFO: has test
[[nodiscard]] auto get_time(std::wstring_view path, [[nodiscard]] auto get_time(std::wstring_view path,
time_type type) -> std::optional<std::uint64_t>; time_type type) -> std::optional<std::uint64_t>;
@ -227,6 +229,7 @@ public:
[[nodiscard]] auto sha256() -> std::optional<std::string>; [[nodiscard]] auto sha256() -> std::optional<std::string>;
#endif // defined(PROJECT_ENABLE_LIBSODIUM) #endif // defined(PROJECT_ENABLE_LIBSODIUM)
[[nodiscard]] auto size() const -> std::optional<std::uint64_t> override; [[nodiscard]] auto size() const -> std::optional<std::uint64_t> override;
[[nodiscard]] auto truncate(std::size_t size) -> bool override; [[nodiscard]] auto truncate(std::size_t size) -> bool override;
@ -770,6 +773,7 @@ public:
#if defined(PROJECT_ENABLE_JSON) #if defined(PROJECT_ENABLE_JSON)
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
// INFO: has test
[[nodiscard]] auto [[nodiscard]] auto
read_json_file(std::string_view path, nlohmann::json &data, read_json_file(std::string_view path, nlohmann::json &data,
std::optional<std::string_view> password = std::nullopt) -> bool; std::optional<std::string_view> password = std::nullopt) -> bool;
@ -779,6 +783,7 @@ read_json_file(std::string_view path, nlohmann::json &data,
std::wstring_view path, nlohmann::json &data, std::wstring_view path, nlohmann::json &data,
std::optional<std::wstring_view> password = std::nullopt) -> bool; std::optional<std::wstring_view> password = std::nullopt) -> bool;
// INFO: has test
[[nodiscard]] auto write_json_file( [[nodiscard]] auto write_json_file(
std::string_view path, const nlohmann::json &data, std::string_view path, const nlohmann::json &data,
std::optional<std::string_view> password = std::nullopt) -> bool; std::optional<std::string_view> password = std::nullopt) -> bool;
@ -792,6 +797,7 @@ read_json_file(std::string_view path, nlohmann::json &data,
[[nodiscard]] auto read_json_file(std::string_view path, [[nodiscard]] auto read_json_file(std::string_view path,
nlohmann::json &data) -> bool; nlohmann::json &data) -> bool;
// INFO: has test
[[nodiscard]] auto read_json_file(std::wstring_view path, [[nodiscard]] auto read_json_file(std::wstring_view path,
nlohmann::json &data) -> bool; nlohmann::json &data) -> bool;
@ -799,11 +805,13 @@ read_json_file(std::string_view path, nlohmann::json &data,
[[nodiscard]] auto write_json_file(std::string_view path, [[nodiscard]] auto write_json_file(std::string_view path,
const nlohmann::json &data) -> bool; const nlohmann::json &data) -> bool;
// INFO: has test
[[nodiscard]] auto write_json_file(std::wstring_view path, [[nodiscard]] auto write_json_file(std::wstring_view path,
const nlohmann::json &data) -> bool; const nlohmann::json &data) -> bool;
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
#endif // defined(PROJECT_ENABLE_JSON) #endif // defined(PROJECT_ENABLE_JSON)
// INFO: has test
template <typename string_t> template <typename string_t>
[[nodiscard]] inline auto directory_exists_in_path_t( [[nodiscard]] inline auto directory_exists_in_path_t(
std::basic_string_view<typename string_t::value_type> path, std::basic_string_view<typename string_t::value_type> path,
@ -812,6 +820,7 @@ template <typename string_t>
return directory(utils::path::combine(path, {sub_directory})).exists(); return directory(utils::path::combine(path, {sub_directory})).exists();
} }
// INFO: has test
template <typename string_t> template <typename string_t>
[[nodiscard]] inline auto file_exists_in_path_t( [[nodiscard]] inline auto file_exists_in_path_t(
std::basic_string_view<typename string_t::value_type> path, std::basic_string_view<typename string_t::value_type> path,
@ -819,21 +828,25 @@ template <typename string_t>
return file(utils::path::combine(path, {file_name})).exists(); return file(utils::path::combine(path, {file_name})).exists();
} }
// INFO: has test
inline auto directory_exists_in_path(std::string_view path, inline auto directory_exists_in_path(std::string_view path,
std::string_view sub_directory) -> bool { std::string_view sub_directory) -> bool {
return directory_exists_in_path_t<std::string>(path, sub_directory); return directory_exists_in_path_t<std::string>(path, sub_directory);
} }
// INFO: has test
inline auto directory_exists_in_path(std::wstring_view path, inline auto directory_exists_in_path(std::wstring_view path,
std::wstring_view sub_directory) -> bool { std::wstring_view sub_directory) -> bool {
return directory_exists_in_path_t<std::wstring>(path, sub_directory); return directory_exists_in_path_t<std::wstring>(path, sub_directory);
} }
// INFO: has test
inline auto file_exists_in_path(std::string_view path, inline auto file_exists_in_path(std::string_view path,
std::string_view file_name) -> bool { std::string_view file_name) -> bool {
return file_exists_in_path_t<std::string>(path, file_name); return file_exists_in_path_t<std::string>(path, file_name);
} }
// INFO: has test
inline auto file_exists_in_path(std::wstring_view path, inline auto file_exists_in_path(std::wstring_view path,
std::wstring_view file_name) -> bool { std::wstring_view file_name) -> bool {
return file_exists_in_path_t<std::wstring>(path, file_name); return file_exists_in_path_t<std::wstring>(path, file_name);

View File

@ -176,8 +176,10 @@ auto get_times(std::string_view path) -> std::optional<file_times> {
file_times ret{}; file_times ret{};
#if defined(_WIN32) #if defined(_WIN32)
auto file_handle = ::CreateFileA(std::string{path}.c_str(), GENERIC_READ, 0, auto file_handle =
nullptr, OPEN_EXISTING, 0, nullptr); ::CreateFileA(std::string{path}.c_str(), GENERIC_READ,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, OPEN_EXISTING, 0U, nullptr);
if (file_handle == INVALID_HANDLE_VALUE) { if (file_handle == INVALID_HANDLE_VALUE) {
throw std::runtime_error("failed to get file times|" + std::string{path} + throw std::runtime_error("failed to get file times|" + std::string{path} +
'|' + '|' +

View File

@ -49,7 +49,8 @@ auto smb_file::copy_to(std::string_view new_path,
// auto to_path = utils::path::absolute(new_path); // auto to_path = utils::path::absolute(new_path);
throw std::runtime_error("failed to copy file|" + path_ + '|' + throw std::runtime_error("failed to copy file|" + path_ + '|' +
std::string{new_path} + "|not implemented"); std::string{new_path} + '|' +
std::to_string(overwrite) + "|not implemented");
} catch (const std::exception &e) { } catch (const std::exception &e) {
utils::error::handle_exception(function_name, e); utils::error::handle_exception(function_name, e);
} catch (...) { } catch (...) {

View File

@ -105,38 +105,76 @@ TEST(utils_file, write_fails_for_read_only_file) {
TEST(utils_file, read_and_write_json_file) { TEST(utils_file, read_and_write_json_file) {
auto path = test::generate_test_file_name("utils_file"); auto path = test::generate_test_file_name("utils_file");
auto data = nlohmann::json({{"moose", "cow"}}); auto json_data = nlohmann::json({{"moose", "cow"}});
EXPECT_TRUE(utils::file::write_json_file(path, data)); EXPECT_TRUE(utils::file::write_json_file(path, json_data));
nlohmann::json data2{}; {
EXPECT_TRUE(utils::file::read_json_file(path, data2)); nlohmann::json result_data{};
EXPECT_STREQ(data.dump().c_str(), data2.dump().c_str()); EXPECT_TRUE(utils::file::read_json_file(path, result_data));
EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str());
}
{
nlohmann::json result_data{};
EXPECT_TRUE(utils::file::read_json_file(utils::string::from_utf8(path),
result_data));
EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str());
}
} }
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
TEST(utils_file, read_and_write_json_file_encrypted) { TEST(utils_file, read_and_write_json_file_encrypted) {
auto path = test::generate_test_file_name("utils_file"); {
auto path = test::generate_test_file_name("utils_file");
auto data = nlohmann::json({{"moose", "cow"}}); auto json_data = nlohmann::json({{"moose", "cow"}});
EXPECT_TRUE(utils::file::write_json_file(path, data, "moose")); EXPECT_TRUE(utils::file::write_json_file(path, json_data, "moose"));
nlohmann::json data2{}; nlohmann::json result_data{};
EXPECT_TRUE(utils::file::read_json_file(path, data2, "moose")); EXPECT_TRUE(utils::file::read_json_file(path, result_data, "moose"));
EXPECT_STREQ(data.dump().c_str(), data2.dump().c_str()); EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str());
{
auto file = utils::file::file::open_file(path);
data_buffer encrypted_data{};
EXPECT_TRUE(file->read_all(encrypted_data, 0U));
data_buffer decrypted_data{};
EXPECT_TRUE(utils::encryption::decrypt_data("moose", encrypted_data,
decrypted_data));
EXPECT_STREQ(json_data.dump().c_str(),
nlohmann::json::parse(std::string(decrypted_data.begin(),
decrypted_data.end()))
.dump()
.c_str());
}
}
{ {
auto file = utils::file::file::open_file(path); auto path =
data_buffer encrypted_data{}; utils::string::from_utf8(test::generate_test_file_name("utils_file"));
EXPECT_TRUE(file->read_all(encrypted_data, 0U));
data_buffer decrypted_data{}; auto json_data = nlohmann::json({{"moose", "cow"}});
EXPECT_TRUE(utils::encryption::decrypt_data("moose", encrypted_data, EXPECT_TRUE(utils::file::write_json_file(path, json_data, L"moose"));
decrypted_data));
EXPECT_STREQ(data.dump().c_str(), nlohmann::json result_data{};
nlohmann::json::parse( EXPECT_TRUE(utils::file::read_json_file(path, result_data, L"moose"));
std::string(decrypted_data.begin(), decrypted_data.end())) EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str());
.dump()
.c_str()); {
auto file = utils::file::file::open_file(path);
data_buffer encrypted_data{};
EXPECT_TRUE(file->read_all(encrypted_data, 0U));
data_buffer decrypted_data{};
EXPECT_TRUE(utils::encryption::decrypt_data("moose", encrypted_data,
decrypted_data));
EXPECT_STREQ(json_data.dump().c_str(),
nlohmann::json::parse(std::string(decrypted_data.begin(),
decrypted_data.end()))
.dump()
.c_str());
}
} }
} }
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
@ -394,7 +432,8 @@ TEST(utils_file, get_total_drive_space_fails_for_bad_path) {
TEST(utils_file, get_times) { TEST(utils_file, get_times) {
{ {
auto times = utils::file::get_times(__FILE__); auto times =
utils::file::get_times(test::create_random_file(1U).get_path());
EXPECT_TRUE(times.has_value()); EXPECT_TRUE(times.has_value());
EXPECT_LT(0U, times->get(utils::file::time_type::accessed)); EXPECT_LT(0U, times->get(utils::file::time_type::accessed));
EXPECT_LT(0U, times->get(utils::file::time_type::created)); EXPECT_LT(0U, times->get(utils::file::time_type::created));
@ -403,7 +442,8 @@ TEST(utils_file, get_times) {
} }
{ {
auto times = utils::file::get_times(utils::string::from_utf8(__FILE__)); auto times = utils::file::get_times(
utils::string::from_utf8(test::create_random_file(1U).get_path()));
EXPECT_TRUE(times.has_value()); EXPECT_TRUE(times.has_value());
EXPECT_LT(0U, times->get(utils::file::time_type::accessed)); EXPECT_LT(0U, times->get(utils::file::time_type::accessed));
EXPECT_LT(0U, times->get(utils::file::time_type::created)); EXPECT_LT(0U, times->get(utils::file::time_type::created));
@ -420,45 +460,49 @@ TEST(utils_file, get_times_fails_if_not_found) {
TEST(utils_file, get_time) { TEST(utils_file, get_time) {
{ {
auto file_path = test::create_random_file(1U).get_path();
auto file_time = auto file_time =
utils::file::get_time(__FILE__, utils::file::time_type::accessed); utils::file::get_time(file_path, utils::file::time_type::accessed);
EXPECT_TRUE(file_time.has_value()); EXPECT_TRUE(file_time.has_value());
EXPECT_LT(0U, file_time.value()); EXPECT_LT(0U, file_time.value());
file_time = file_time =
utils::file::get_time(__FILE__, utils::file::time_type::created); utils::file::get_time(file_path, utils::file::time_type::created);
EXPECT_TRUE(file_time.has_value()); EXPECT_TRUE(file_time.has_value());
EXPECT_LT(0U, file_time.value()); EXPECT_LT(0U, file_time.value());
file_time = file_time =
utils::file::get_time(__FILE__, utils::file::time_type::modified); utils::file::get_time(file_path, utils::file::time_type::modified);
EXPECT_TRUE(file_time.has_value()); EXPECT_TRUE(file_time.has_value());
EXPECT_LT(0U, file_time.value()); EXPECT_LT(0U, file_time.value());
file_time = file_time =
utils::file::get_time(__FILE__, utils::file::time_type::written); utils::file::get_time(file_path, utils::file::time_type::written);
EXPECT_TRUE(file_time.has_value()); EXPECT_TRUE(file_time.has_value());
EXPECT_LT(0U, file_time.value()); EXPECT_LT(0U, file_time.value());
} }
{ {
auto file_time = utils::file::get_time(utils::string::from_utf8(__FILE__), auto file_path =
utils::file::time_type::accessed); utils::string::from_utf8(test::create_random_file(1U).get_path());
auto file_time =
utils::file::get_time(file_path, utils::file::time_type::accessed);
EXPECT_TRUE(file_time.has_value()); EXPECT_TRUE(file_time.has_value());
EXPECT_LT(0U, file_time.value()); EXPECT_LT(0U, file_time.value());
file_time = utils::file::get_time(utils::string::from_utf8(__FILE__), file_time =
utils::file::time_type::created); utils::file::get_time(file_path, utils::file::time_type::created);
EXPECT_TRUE(file_time.has_value()); EXPECT_TRUE(file_time.has_value());
EXPECT_LT(0U, file_time.value()); EXPECT_LT(0U, file_time.value());
file_time = utils::file::get_time(utils::string::from_utf8(__FILE__), file_time =
utils::file::time_type::modified); utils::file::get_time(file_path, utils::file::time_type::modified);
EXPECT_TRUE(file_time.has_value()); EXPECT_TRUE(file_time.has_value());
EXPECT_LT(0U, file_time.value()); EXPECT_LT(0U, file_time.value());
file_time = utils::file::get_time(utils::string::from_utf8(__FILE__), file_time =
utils::file::time_type::written); utils::file::get_time(file_path, utils::file::time_type::written);
EXPECT_TRUE(file_time.has_value()); EXPECT_TRUE(file_time.has_value());
EXPECT_LT(0U, file_time.value()); EXPECT_LT(0U, file_time.value());
} }