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

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