updated build system

This commit is contained in:
2024-08-23 10:01:31 -05:00
parent 4eff7aae64
commit 6dc10d2a11
8 changed files with 147 additions and 85 deletions

View File

@ -86,6 +86,7 @@ ecdh
endforeach
endfunction
eventlib
expect_streq
fext
fgetattr
flac_version

View File

@ -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;

View File

@ -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<const unsigned char *>(buffer.data()),
bytes_read);
if (res != 0) {
throw std::runtime_error("failed to update sha256|" +
std::to_string(res));
}
}
}
std::array<unsigned char, crypto_hash_sha256_BYTES> 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{};

View File

@ -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());

View File

@ -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

View File

@ -283,6 +283,10 @@ public:
return read_buffer_size;
}
#if defined(PROJECT_ENABLE_LIBSODIUM)
[[nodiscard]] auto sha256() -> std::optional<std::string>;
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
[[nodiscard]] auto size() const -> std::optional<std::uint64_t> override;
[[nodiscard]] auto truncate(std::size_t size) -> bool override;

View File

@ -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 <optional>
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<std::int64_t>(offset), SEEK_SET);
if (res == -1) {
if (fseeko(file_.get(), static_cast<std::int64_t>(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<std::string> {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
auto should_close{false};
auto read_only{read_only_};
std::optional<std::string> 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<const unsigned char *>(buffer.data()),
bytes_read);
if (res != 0) {
throw std::runtime_error("failed to update sha256|" +
std::to_string(res));
}
}
}
std::array<unsigned char, crypto_hash_sha256_BYTES> 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_};

View File

@ -271,34 +271,42 @@ auto smb_file::remove() -> bool {
static_cast<const char *>(__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<std::uint64_t> {