From 6dc10d2a11d80c897e7edc676e1a02fd6686bc94 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Fri, 23 Aug 2024 10:01:31 -0500 Subject: [PATCH] updated build system --- .cspell/words.txt | 1 + .../librepertory/include/utils/file_utils.hpp | 2 - .../librepertory/src/utils/file_utils.cpp | 45 +---------- ...ile_manager_ring_buffer_open_file_test.cpp | 40 ++++++++-- repertory/repertory_test/src/utils_test.cpp | 8 +- support/include/utils/file.hpp | 4 + support/src/utils/file_file.cpp | 76 ++++++++++++++++++- support/src/utils/file_smb_file.cpp | 56 ++++++++------ 8 files changed, 147 insertions(+), 85 deletions(-) diff --git a/.cspell/words.txt b/.cspell/words.txt index 0d896b01..5026d6f9 100644 --- a/.cspell/words.txt +++ b/.cspell/words.txt @@ -86,6 +86,7 @@ ecdh endforeach endfunction eventlib +expect_streq fext fgetattr flac_version diff --git a/repertory/librepertory/include/utils/file_utils.hpp b/repertory/librepertory/include/utils/file_utils.hpp index bf3c5ec5..a642ac9a 100644 --- a/repertory/librepertory/include/utils/file_utils.hpp +++ b/repertory/librepertory/include/utils/file_utils.hpp @@ -34,8 +34,6 @@ void change_to_process_directory(); [[nodiscard]] auto copy_file(std::string from_path, std::string to_path) -> bool; -[[nodiscard]] auto generate_sha256(const std::string &file_path) -> std::string; - [[nodiscard]] auto get_accessed_time(const std::string &path, std::uint64_t &accessed) -> bool; diff --git a/repertory/librepertory/src/utils/file_utils.cpp b/repertory/librepertory/src/utils/file_utils.cpp index ee293ebb..8f3ba987 100644 --- a/repertory/librepertory/src/utils/file_utils.cpp +++ b/repertory/librepertory/src/utils/file_utils.cpp @@ -68,7 +68,7 @@ auto copy_directory_recursively(std::string from_path, std::string to_path) -> bool { from_path = utils::path::absolute(from_path); to_path = utils::path::absolute(to_path); - auto ret = utils::file::directory(to_path).create_directory(); + auto ret = utils::file::directory(to_path).create_directory() != nullptr; if (ret) { #if defined(_WIN32) WIN32_FIND_DATA fd{}; @@ -118,49 +118,6 @@ auto copy_directory_recursively(std::string from_path, return ret; } -auto generate_sha256(const std::string &file_path) -> std::string { - crypto_hash_sha256_state state{}; - auto res = crypto_hash_sha256_init(&state); - if (res != 0) { - throw std::runtime_error("failed to initialize sha256|" + - std::to_string(res)); - } - - { - auto input_file = utils::file::file::open_file(file_path); - if (not *input_file) { - throw std::runtime_error("failed to open file|" + file_path); - } - - data_buffer buffer(input_file->get_read_buffer_size()); - std::uint64_t read_offset{0U}; - std::size_t bytes_read{0U}; - while (input_file->read(buffer, read_offset, &bytes_read)) { - if (not bytes_read) { - break; - } - - read_offset += bytes_read; - res = crypto_hash_sha256_update( - &state, reinterpret_cast(buffer.data()), - bytes_read); - if (res != 0) { - throw std::runtime_error("failed to update sha256|" + - std::to_string(res)); - } - } - } - - std::array out{}; - res = crypto_hash_sha256_final(&state, out.data()); - if (res != 0) { - throw std::runtime_error("failed to finalize sha256|" + - std::to_string(res)); - } - - return utils::collection::to_hex_string(out); -} - auto get_free_drive_space(const std::string &path) -> std::uint64_t { #if defined(_WIN32) ULARGE_INTEGER li{}; diff --git a/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp b/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp index f7bf2d89..a1aa273b 100644 --- a/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp +++ b/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp @@ -371,8 +371,14 @@ TEST(ring_buffer_open_file, read_full_file) { nf2.close(); nf.close(); - EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(), - utils::file::generate_sha256(dest_path).c_str()); + auto hash1 = utils::file::file(download_source_path).sha256(); + auto hash2 = utils::file::file(dest_path).sha256(); + + EXPERT_TRUE(hash1.has_value()); + EXPERT_TRUE(hash2.has_value()); + if (hash1.has_value() && hash2.has_value()) { + EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str()); + } } EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); @@ -430,8 +436,14 @@ TEST(ring_buffer_open_file, read_full_file_in_reverse) { nf2.close(); nf.close(); - EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(), - utils::file::generate_sha256(dest_path).c_str()); + auto hash1 = utils::file::file(download_source_path).sha256(); + auto hash2 = utils::file::file(dest_path).sha256(); + + EXPERT_TRUE(hash1.has_value()); + EXPERT_TRUE(hash2.has_value()); + if (hash1.has_value() && hash2.has_value()) { + EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str()); + } } EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); @@ -490,8 +502,14 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) { nf2.close(); nf.close(); - EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(), - utils::file::generate_sha256(dest_path).c_str()); + auto hash1 = utils::file::file(download_source_path).sha256(); + auto hash2 = utils::file::file(dest_path).sha256(); + + EXPERT_TRUE(hash1.has_value()); + EXPERT_TRUE(hash2.has_value()); + if (hash1.has_value() && hash2.has_value()) { + EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str()); + } } EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); @@ -555,8 +573,14 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks_in_reverse) { nf2.close(); nf.close(); - EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(), - utils::file::generate_sha256(dest_path).c_str()); + auto hash1 = utils::file::file(download_source_path).sha256(); + auto hash2 = utils::file::file(dest_path).sha256(); + + EXPERT_TRUE(hash1.has_value()); + EXPERT_TRUE(hash2.has_value()); + if (hash1.has_value() && hash2.has_value()) { + EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str()); + } } EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); diff --git a/repertory/repertory_test/src/utils_test.cpp b/repertory/repertory_test/src/utils_test.cpp index 7a10a938..5d7cf9a3 100644 --- a/repertory/repertory_test/src/utils_test.cpp +++ b/repertory/repertory_test/src/utils_test.cpp @@ -50,8 +50,8 @@ TEST(utils, convert_api_date) { } #endif -TEST(utils, generate_sha256) { - const auto res = utils::file::generate_sha256(__FILE__); - std::cout << res << std::endl; -} +// TEST(utils, generate_sha256) { +// const auto res = utils::file::generate_sha256(__FILE__); +// std::cout << res << std::endl; +// } } // namespace repertory diff --git a/support/include/utils/file.hpp b/support/include/utils/file.hpp index 11bcc995..43080aca 100644 --- a/support/include/utils/file.hpp +++ b/support/include/utils/file.hpp @@ -283,6 +283,10 @@ public: return read_buffer_size; } +#if defined(PROJECT_ENABLE_LIBSODIUM) + [[nodiscard]] auto sha256() -> std::optional; + +#endif // defined(PROJECT_ENABLE_LIBSODIUM) [[nodiscard]] auto size() const -> std::optional override; [[nodiscard]] auto truncate(std::size_t size) -> bool override; diff --git a/support/src/utils/file_file.cpp b/support/src/utils/file_file.cpp index b6662abf..e4fc58c6 100644 --- a/support/src/utils/file_file.cpp +++ b/support/src/utils/file_file.cpp @@ -21,12 +21,14 @@ */ #include "utils/file.hpp" +#include "utils/collection.hpp" #include "utils/common.hpp" #include "utils/encryption.hpp" #include "utils/error.hpp" #include "utils/path.hpp" #include "utils/string.hpp" #include "utils/time.hpp" +#include namespace { [[nodiscard]] auto get_file_size(std::string_view path, @@ -296,14 +298,15 @@ auto file::read(unsigned char *data, std::size_t to_read, std::uint64_t offset, throw std::runtime_error("file is not open for reading"); } - auto res = fseeko(file_.get(), static_cast(offset), SEEK_SET); - if (res == -1) { + if (fseeko(file_.get(), static_cast(offset), SEEK_SET) == + -1) { throw std::runtime_error("failed to seek before read"); } std::size_t bytes_read{0U}; while (bytes_read != to_read) { - res = fread(&data[bytes_read], 1U, to_read - bytes_read, file_.get()); + auto res = + fread(&data[bytes_read], 1U, to_read - bytes_read, file_.get()); if (not feof(file_.get()) && ferror(file_.get())) { throw std::runtime_error("failed to read file bytes"); } @@ -329,6 +332,73 @@ auto file::read(unsigned char *data, std::size_t to_read, std::uint64_t offset, return false; } +#if defined(PROJECT_ENABLE_LIBSODIUM) +auto file::sha256() -> std::optional { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + auto should_close{false}; + auto read_only{read_only_}; + std::optional ret; + + try { + if (file_ == nullptr) { + should_close = true; + read_only_ = true; + this->open(); + } + + crypto_hash_sha256_state state{}; + auto res = crypto_hash_sha256_init(&state); + if (res != 0) { + throw std::runtime_error("failed to initialize sha256|" + + std::to_string(res)); + } + + { + data_buffer buffer(get_read_buffer_size()); + std::uint64_t read_offset{0U}; + std::size_t bytes_read{0U}; + while (i_file::read(buffer, read_offset, &bytes_read)) { + if (not bytes_read) { + break; + } + + read_offset += bytes_read; + res = crypto_hash_sha256_update( + &state, reinterpret_cast(buffer.data()), + bytes_read); + if (res != 0) { + throw std::runtime_error("failed to update sha256|" + + std::to_string(res)); + } + } + } + + std::array out{}; + res = crypto_hash_sha256_final(&state, out.data()); + if (res != 0) { + throw std::runtime_error("failed to finalize sha256|" + + std::to_string(res)); + } + + ret = utils::collection::to_hex_string(out); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + if (should_close) { + read_only_ = read_only; + close(); + } + + return ret; +} +#endif // defined(PROJECT_ENABLE_LIBSODIUM) + auto file::remove() -> bool { #if defined(_WIN32) recur_mutex_lock lock{*mtx_}; diff --git a/support/src/utils/file_smb_file.cpp b/support/src/utils/file_smb_file.cpp index 1c49e563..659c8a92 100644 --- a/support/src/utils/file_smb_file.cpp +++ b/support/src/utils/file_smb_file.cpp @@ -271,34 +271,42 @@ auto smb_file::remove() -> bool { static_cast(__FUNCTION__), }; - return utils::retry_action([this]() -> bool { - try { - close(); + try { + close(); - auto res = smb_tree_connect(session_.get(), share_name_.c_str(), &tid_); - if (res != DSM_SUCCESS) { - throw std::runtime_error("failed to connect to share|" + share_name_ + - '|' + std::to_string(res)); + return utils::retry_action([this]() -> bool { + try { + auto res = smb_tree_connect(session_.get(), share_name_.c_str(), &tid_); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to connect to share|" + share_name_ + + '|' + std::to_string(res)); + } + + auto rel_path = smb_create_relative_path(path_); + res = smb_file_rm(session_.get(), tid_, rel_path.c_str()); + if (res != DSM_SUCCESS) { + throw std::runtime_error( + "failed to remove file|" + path_ + '|' + rel_path + '|' + + std::to_string(res) + '|' + + std::to_string(smb_session_get_nt_status(session_.get()))); + } + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); } - auto rel_path = smb_create_relative_path(path_); - res = smb_file_rm(session_.get(), tid_, rel_path.c_str()); - if (res != DSM_SUCCESS) { - throw std::runtime_error( - "failed to remove file|" + path_ + '|' + rel_path + '|' + - std::to_string(res) + '|' + - std::to_string(smb_session_get_nt_status(session_.get()))); - } + return false; + }); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } - return true; - } catch (const std::exception &e) { - utils::error::handle_exception(function_name, e); - } catch (...) { - utils::error::handle_exception(function_name); - } - - return false; - }); + return false; } auto smb_file::size() const -> std::optional {