updated build system
This commit is contained in:
@ -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;
|
||||
|
@ -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_};
|
||||
|
@ -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> {
|
||||
|
Reference in New Issue
Block a user