updated build system
Some checks reported errors
BlockStorage/repertory/pipeline/head Something is wrong with the build of this commit

This commit is contained in:
2024-10-19 11:10:36 -05:00
parent c72dec6369
commit 2fb53e34af
24 changed files with 1330 additions and 831 deletions

View File

@ -34,25 +34,30 @@ void sqlite3_deleter::operator()(sqlite3 *db3) const {
return;
}
utils::error::handle_error(function_name, "closing database handle");
std::string err_msg;
if (not execute_sql(*db3, "VACUUM;", err_msg)) {
utils::error::handle_error(function_name,
utils::error::create_error_message({
"failed to vacuum database",
err_msg,
}));
}
if (not utils::retry_action(
[&db3]() -> bool {
auto res = sqlite3_close_v2(db3);
if (res == SQLITE_OK) {
return true;
}
if (not utils::retry_action([&db3]() -> bool {
auto res = sqlite3_close_v2(db3);
if (res == SQLITE_OK) {
return true;
}
auto &&err_str = sqlite3_errstr(res);
utils::error::handle_error(
function_name,
utils::error::create_error_message({
"failed to close database",
(err_str == nullptr ? std::to_string(res) : err_str),
}));
return false;
},
60U)) {
auto &&err_str = sqlite3_errstr(res);
utils::error::handle_error(
function_name,
utils::error::create_error_message({
"failed to close database",
(err_str == nullptr ? std::to_string(res) : err_str),
}));
return false;
})) {
repertory::utils::error::handle_error(function_name,
"failed to close database");
}
@ -71,8 +76,50 @@ auto db_column::get_value_as_json() const -> nlohmann::json {
}
#endif // defined(PROJECT_ENABLE_JSON)
auto execute_sql(sqlite3 &db3, const std::string &sql, std::string &err)
-> bool {
auto create_db(std::string db_path,
const std::map<std::string, std::string> &sql_create_tables)
-> db3_t {
REPERTORY_USES_FUNCTION_NAME();
sqlite3 *db_ptr{nullptr};
auto db_res =
sqlite3_open_v2(db_path.c_str(), &db_ptr,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);
if (db_res != SQLITE_OK) {
const auto *msg = sqlite3_errstr(db_res);
throw utils::error::create_exception({
function_name,
"failed to open db",
db_path,
(msg == nullptr ? std::to_string(db_res) : msg),
});
}
auto db3 = db3_t{
db_ptr,
sqlite3_deleter(),
};
for (auto &&create_item : sql_create_tables) {
std::string err_msg;
if (not sqlite::execute_sql(*db3, create_item.second, err_msg)) {
db3.reset();
throw utils::error::create_exception({
function_name,
err_msg,
});
}
}
set_journal_mode(*db3);
return db3;
}
auto execute_sql(sqlite3 &db3, const std::string &sql,
std::string &err) -> bool {
REPERTORY_USES_FUNCTION_NAME();
char *err_msg{nullptr};
auto res = sqlite3_exec(&db3, sql.c_str(), nullptr, nullptr, &err_msg);
if (err_msg != nullptr) {
@ -85,15 +132,20 @@ auto execute_sql(sqlite3 &db3, const std::string &sql, std::string &err)
return true;
}
err = "failed to execute sql|" + sql + "|" + std::to_string(res) + '|' +
(err.empty() ? sqlite3_errstr(res) : err);
err = utils::error::create_error_message({
function_name,
"failed to execute sql",
err,
sql,
});
return false;
}
void set_journal_mode(sqlite3 &db3) {
sqlite3_exec(&db3,
"PRAGMA journal_mode = WAL;PRAGMA synchronous = NORMAL;PRAGMA "
"auto_vacuum = FULL;",
"auto_vacuum = NONE;",
nullptr, nullptr, nullptr);
}
} // namespace repertory::utils::db::sqlite

View File

@ -60,8 +60,13 @@ protected:
std::ios_base::openmode which = std::ios_base::out |
std::ios_base::in)
-> pos_type override {
REPERTORY_USES_FUNCTION_NAME();
if ((which & std::ios_base::in) != std::ios_base::in) {
throw std::runtime_error("output is not supported");
throw utils::error::create_exception({
function_name,
"output is not supported",
});
}
const auto set_position = [this](char *next) -> pos_type {
@ -179,9 +184,14 @@ encrypting_reader::encrypting_reader(
stop_requested_(stop_requested),
error_return_(error_return),
source_file_(utils::file::file::open_or_create_file(source_path, true)) {
REPERTORY_USES_FUNCTION_NAME();
if (not *source_file_) {
throw std::runtime_error("file open failed|src|" +
std::string{source_path});
throw utils::error::create_exception({
function_name,
"file open failed",
source_path,
});
}
data_buffer result;
@ -204,8 +214,11 @@ encrypting_reader::encrypting_reader(
auto opt_size = source_file_->size();
if (not opt_size.has_value()) {
throw std::runtime_error("failed to get file size|" +
source_file_->get_path());
throw utils::error::create_exception({
function_name,
"failed to get file size",
source_file_->get_path(),
});
}
auto file_size = opt_size.value();
@ -233,9 +246,14 @@ encrypting_reader::encrypting_reader(std::string_view encrypted_file_path,
stop_requested_(stop_requested),
error_return_(error_return),
source_file_(utils::file::file::open_or_create_file(source_path, true)) {
REPERTORY_USES_FUNCTION_NAME();
if (not *source_file_) {
throw std::runtime_error("file open failed|src|" +
std::string{source_path});
throw utils::error::create_exception({
function_name,
"file open failed",
source_path,
});
}
encrypted_file_path_ = encrypted_file_path;
@ -243,8 +261,11 @@ encrypting_reader::encrypting_reader(std::string_view encrypted_file_path,
auto opt_size = source_file_->size();
if (not opt_size.has_value()) {
throw std::runtime_error("failed to get file size|" +
source_file_->get_path());
throw utils::error::create_exception({
function_name,
"failed to get file size",
source_file_->get_path(),
});
}
auto file_size = opt_size.value();
@ -274,9 +295,14 @@ encrypting_reader::encrypting_reader(
stop_requested_(stop_requested),
error_return_(error_return),
source_file_(utils::file::file::open_or_create_file(source_path, true)) {
REPERTORY_USES_FUNCTION_NAME();
if (not *source_file_) {
throw std::runtime_error("file open failed|src|" +
std::string{source_path});
throw utils::error::create_exception({
function_name,
"file open failed",
source_path,
});
}
encrypted_file_path_ = encrypted_file_path;
@ -284,9 +310,12 @@ encrypting_reader::encrypting_reader(
auto opt_size = source_file_->size();
if (not opt_size.has_value()) {
throw std::runtime_error("get file size failed|src|" +
source_file_->get_path() + '|' +
std::to_string(utils::get_last_error_code()));
throw utils::error::create_exception({
function_name,
"get file size failed",
std::to_string(utils::get_last_error_code()),
source_file_->get_path(),
});
}
auto file_size{opt_size.value()};
@ -315,9 +344,15 @@ encrypting_reader::encrypting_reader(const encrypting_reader &reader)
last_data_chunk_size_(reader.last_data_chunk_size_),
read_offset_(reader.read_offset_),
total_size_(reader.total_size_) {
REPERTORY_USES_FUNCTION_NAME();
if (not *source_file_) {
throw std::runtime_error("file open failed|src|" +
source_file_->get_path());
throw utils::error::create_exception({
function_name,
"file open failed",
std::to_string(utils::get_last_error_code()),
source_file_->get_path(),
});
}
}
@ -331,11 +366,16 @@ auto encrypting_reader::calculate_decrypted_size(std::uint64_t total_size)
auto encrypting_reader::calculate_encrypted_size(std::string_view source_path)
-> std::uint64_t {
REPERTORY_USES_FUNCTION_NAME();
auto opt_size = utils::file::file{source_path}.size();
if (not opt_size.has_value()) {
throw std::runtime_error("get file size failed|src|" +
std::string{source_path} + '|' +
std::to_string(utils::get_last_error_code()));
throw utils::error::create_exception({
function_name,
"get file size failed",
std::to_string(utils::get_last_error_code()),
source_path,
});
}
auto file_size{opt_size.value()};

View File

@ -38,6 +38,11 @@ auto create_error_message(std::vector<std::string_view> items) -> std::string {
return stream.str();
}
auto create_exception(std::vector<std::string_view> items)
-> std::runtime_error {
return std::runtime_error(create_error_message(items));
}
void handle_error(std::string_view function_name, std::string_view msg) {
const i_exception_handler *handler{exception_handler};
if (handler != nullptr) {

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,7 @@
#include "utils/common.hpp"
#include "utils/error.hpp"
#include "utils/string.hpp"
#include "utils/unix.hpp"
#include "utils/windows.hpp"
@ -32,6 +33,8 @@ auto traverse_directory(
std::function<bool(repertory::utils::file::directory)> directory_action,
std::function<bool(repertory::utils::file::file)> file_action,
repertory::stop_type *stop_requested) -> bool {
REPERTORY_USES_FUNCTION_NAME();
auto res{true};
const auto is_stop_requested = [&stop_requested]() -> bool {
@ -43,9 +46,12 @@ auto traverse_directory(
auto search = repertory::utils::path::combine(path, {"*.*"});
auto find = ::FindFirstFileA(search.c_str(), &fd);
if (find == INVALID_HANDLE_VALUE) {
throw std::runtime_error(
"failed to open directory|" + std::string{path} + '|' +
std::to_string(repertory::utils::get_last_error_code()));
throw repertory::utils::error::create_exception({
function_name,
"failed to open directory",
std::to_string(repertory::utils::get_last_error_code()),
path,
});
}
do {
@ -67,9 +73,12 @@ auto traverse_directory(
#else // !defined(_WIN32)
auto *root = opendir(std::string{path}.c_str());
if (root == nullptr) {
throw std::runtime_error(
"failed to open directory|" + std::string{path} + '|' +
std::to_string(repertory::utils::get_last_error_code()));
throw repertory::utils::error::create_exception({
function_name,
"failed to open directory",
std::to_string(repertory::utils::get_last_error_code()),
path,
});
}
struct dirent *de{nullptr};
@ -94,14 +103,19 @@ auto traverse_directory(
} // namespace
namespace repertory::utils::file {
auto directory::copy_to(std::string_view new_path, bool overwrite) const
-> bool {
auto directory::copy_to(std::string_view new_path,
bool overwrite) const -> bool {
REPERTORY_USES_FUNCTION_NAME();
try {
throw std::runtime_error("failed to copy directory|" + path_ + '|' +
std::string{new_path} + '+' +
std::to_string(overwrite) + "|not implemented");
throw utils::error::create_exception({
function_name,
"failed to copy directory",
"not implemented",
path_,
new_path,
utils::string::from_bool(overwrite),
});
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
@ -157,11 +171,14 @@ auto directory::create_directory(std::string_view path) const
auto res = ::SHCreateDirectory(nullptr,
utils::string::from_utf8(abs_path).c_str());
if (res != ERROR_SUCCESS) {
throw std::runtime_error("failed to create directory|" +
std::string{abs_path} + '|' +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to create directory",
std::to_string(res),
abs_path,
});
}
#else // !defined(_WIN32)
#else // !defined(_WIN32)
auto ret{true};
auto paths =
utils::string::split(abs_path, utils::path::directory_seperator, false);
@ -194,7 +211,7 @@ auto directory::exists() const -> bool {
#if defined(_WIN32)
return ::PathIsDirectoryA(path_.c_str()) != 0;
#else // !defined(_WIN32)
struct stat64 st{};
struct stat64 st {};
return (stat64(path_.c_str(), &st) == 0 && S_ISDIR(st.st_mode));
#endif // defined(_WIN32)
@ -247,8 +264,8 @@ auto directory::get_directories() const -> std::vector<fs_directory_t> {
return {};
}
auto directory::create_file(std::string_view file_name, bool read_only) const
-> fs_file_t {
auto directory::create_file(std::string_view file_name,
bool read_only) const -> fs_file_t {
REPERTORY_USES_FUNCTION_NAME();
try {
@ -360,8 +377,13 @@ auto directory::move_to(std::string_view new_path) -> bool {
REPERTORY_USES_FUNCTION_NAME();
try {
throw std::runtime_error("failed to move directory|" + path_ + '|' +
std::string{new_path} + "|not implemented");
throw utils::error::create_exception({
function_name,
"failed to move directory",
"not implemented",
path_,
new_path,
});
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
@ -383,7 +405,10 @@ auto directory::remove() -> bool {
#endif // defined(_WIN32)
if (not ret) {
utils::error::handle_error(function_name,
"failed to remove directory|" + path_);
utils::error::create_error_message({
"failed to remove directory",
path_,
}));
}
return ret;

View File

@ -33,7 +33,7 @@ namespace {
file_size = 0U;
#if defined(_WIN32)
struct _stat64 st{};
struct _stat64 st {};
auto res = _stat64(std::string{path}.c_str(), &st);
if (res != 0) {
return false;
@ -55,7 +55,7 @@ namespace {
return ((::PathFileExistsA(abs_path.c_str()) != 0) &&
(::PathIsDirectoryA(abs_path.c_str()) == 0));
#else // !defined(_WIN32)
struct stat64 st{};
struct stat64 st {};
return (stat64(abs_path.c_str(), &st) == 0 && not S_ISDIR(st.st_mode));
#endif // defined(_WIN32)
}
@ -112,8 +112,14 @@ namespace repertory::utils::file {
// }
void file::open() {
REPERTORY_USES_FUNCTION_NAME();
if (not is_file(path_)) {
throw std::runtime_error("file not found: " + path_);
throw utils::error::create_exception({
function_name,
"file not found",
path_,
});
}
#if defined(_WIN32)
@ -150,8 +156,8 @@ auto file::open_file(std::string_view path, bool read_only) -> fs_file_t {
return new_file;
}
auto file::open_or_create_file(std::string_view path, bool read_only)
-> fs_file_t {
auto file::open_or_create_file(std::string_view path,
bool read_only) -> fs_file_t {
auto abs_path = utils::path::absolute(path);
if (not is_file(abs_path)) {
#if defined(_WIN32)
@ -294,12 +300,20 @@ auto file::read(unsigned char *data, std::size_t to_read, std::uint64_t offset,
try {
if (not file_) {
throw std::runtime_error("file is not open for reading");
throw utils::error::create_exception({
function_name,
"file is not open for reading",
path_,
});
}
if (fseeko(file_.get(), static_cast<std::int64_t>(offset), SEEK_SET) ==
-1) {
throw std::runtime_error("failed to seek before read");
throw utils::error::create_exception({
function_name,
"failed to seek before read",
path_,
});
}
std::size_t bytes_read{0U};
@ -307,7 +321,11 @@ auto file::read(unsigned char *data, std::size_t to_read, std::uint64_t offset,
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");
throw utils::error::create_exception({
function_name,
"failed to read file bytes",
path_,
});
}
if (res == 0) {
@ -349,8 +367,12 @@ auto file::sha256() -> std::optional<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));
throw utils::error::create_exception({
function_name,
"failed to initialize sha256",
std::to_string(res),
path_,
});
}
{
@ -367,8 +389,12 @@ auto file::sha256() -> std::optional<std::string> {
&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));
throw utils::error::create_exception({
function_name,
"failed to update sha256",
std::to_string(res),
path_,
});
}
}
}
@ -376,8 +402,12 @@ auto file::sha256() -> std::optional<std::string> {
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));
throw utils::error::create_exception({
function_name,
"failed to finalize sha256",
std::to_string(res),
path_,
});
}
ret = utils::collection::to_hex_string(out);
@ -411,7 +441,10 @@ auto file::remove() -> bool {
#endif // defined(_WIN32)
if (not ret) {
utils::error::handle_error(function_name,
"failed to remove file|" + path_);
utils::error::create_error_message({
"failed to remove file",
path_,
}));
}
return ret;
@ -464,12 +497,20 @@ auto file::write(const unsigned char *data, std::size_t to_write,
try {
if (not file_) {
throw std::runtime_error("file is not open for writing");
throw utils::error::create_exception({
function_name,
"file is not open for writing",
path_,
});
}
auto res = fseeko(file_.get(), static_cast<std::int64_t>(offset), SEEK_SET);
if (res == -1) {
throw std::runtime_error("failed to seek before write");
throw utils::error::create_exception({
function_name,
"failed to seek before write",
path_,
});
}
std::size_t bytes_written{0U};
@ -478,7 +519,11 @@ auto file::write(const unsigned char *data, std::size_t to_write,
fwrite(reinterpret_cast<const char *>(&data[bytes_written]), 1U,
to_write - bytes_written, file_.get());
if (not feof(file_.get()) && ferror(file_.get())) {
throw std::runtime_error("failed to write file bytes");
throw utils::error::create_exception({
function_name,
"failed to write file bytes",
path_,
});
}
if (written == 0U) {
@ -510,12 +555,20 @@ auto file::size() const -> std::optional<std::uint64_t> {
try {
if (file_) {
if (fseeko(file_.get(), 0, SEEK_END) == -1) {
throw std::runtime_error("failed to seek");
throw utils::error::create_exception({
function_name,
"failed to seek",
path_,
});
}
auto size = ftello(file_.get());
if (size == -1) {
throw std::runtime_error("failed to get position");
throw utils::error::create_exception({
function_name,
"failed to get position",
path_,
});
}
return static_cast<std::uint64_t>(size);
@ -523,7 +576,11 @@ auto file::size() const -> std::optional<std::uint64_t> {
std::uint64_t size{};
if (not get_file_size(path_, size)) {
throw std::runtime_error("failed to get file size");
throw utils::error::create_exception({
function_name,
"failed to get file size",
path_,
});
}
return size;

View File

@ -50,8 +50,12 @@ auto smb_directory::open(std::string_view host, std::string_view user,
if (res != DSM_SUCCESS) {
res = inet_pton(AF_INET, std::string{host}.c_str(), &addr.sin_addr);
if (res != 1) {
throw std::runtime_error("failed to resolve host|" + std::string{host} +
'|' + std::to_string(errno));
throw utils::error::create_exception({
function_name,
"failed to resolve host",
std::to_string(utils::get_last_error_code()),
host,
});
}
}
@ -59,8 +63,12 @@ auto smb_directory::open(std::string_view host, std::string_view user,
static_cast<std::uint32_t>(addr.sin_addr.s_addr),
SMB_TRANSPORT_TCP);
if (res != DSM_SUCCESS) {
throw std::runtime_error("failed to connect to host|" +
std::string{host} + '|' + std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to connect to host",
std::to_string(res),
host,
});
}
smb_session_set_creds(session.get(), std::string{host}.c_str(),
@ -68,9 +76,13 @@ auto smb_directory::open(std::string_view host, std::string_view user,
std::string{password}.c_str());
res = smb_session_login(session.get());
if (res != DSM_SUCCESS) {
throw std::runtime_error("failed to logon to host|" + std::string{host} +
'|' + std::string{user} + '|' +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to logon to host",
std::to_string(res),
host,
user,
});
}
auto share_name = utils::string::split(path, '/', false).at(0U);
@ -78,9 +90,12 @@ auto smb_directory::open(std::string_view host, std::string_view user,
smb_tid tid{};
res = smb_tree_connect(session.get(), share_name.c_str(), &tid);
if (res != DSM_SUCCESS) {
throw std::runtime_error("failed to connect to share|" +
std::string{share_name} + '|' +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to connect to share",
std::to_string(res),
share_name,
});
}
return smb_directory_t{
@ -115,13 +130,22 @@ auto smb_directory::copy_to(std::string_view new_path,
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
// auto to_path = utils::path::absolute(new_path);
throw std::runtime_error("failed to copy directory|" + path_ + '|' +
std::string{new_path} + "|not implemented");
throw utils::error::create_exception({
function_name,
"failed to copy directory",
"not implemented",
path_,
new_path,
});
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
@ -136,7 +160,11 @@ auto smb_directory::count(bool recursive) const -> std::uint64_t {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
smb_stat_list_t list{
@ -149,8 +177,13 @@ auto smb_directory::count(bool recursive) const -> std::uint64_t {
return count;
}
throw std::runtime_error("failed to get directory count recursively|" +
path_ + "|not implemented");
throw utils::error::create_exception({
function_name,
"failed to get directory count recursively",
"not implemented",
utils::string::from_bool(recursive),
path_,
});
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
@ -166,7 +199,11 @@ auto smb_directory::create_directory(std::string_view path) const
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
auto dir = get_directory(path);
@ -178,8 +215,12 @@ auto smb_directory::create_directory(std::string_view path) const
session_.get(), tid_,
smb_create_and_validate_relative_path(path_, path).c_str());
if (res != DSM_SUCCESS) {
throw std::runtime_error("failed to create directory|" + path_ + '/' +
std::string{path} + '|' + std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to create directory",
std::to_string(res),
path_,
});
}
return get_directory(path);
@ -198,14 +239,21 @@ auto smb_directory::create_file(std::string_view file_name,
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
auto fs_file = get_file(file_name);
if (fs_file) {
if (not dynamic_cast<smb_file *>(fs_file.get())->open(read_only)) {
throw std::runtime_error("failed to open existing file|" +
std::string{file_name});
throw utils::error::create_exception({
function_name,
"failed to open existing file",
file_name,
});
}
return fs_file;
@ -243,7 +291,11 @@ auto smb_directory::exists() const -> bool {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
smb_stat_t st{
@ -271,7 +323,11 @@ auto smb_directory::get_directory(std::string_view path) const
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
auto rel_path = smb_create_and_validate_relative_path(path_, path);
@ -280,12 +336,20 @@ auto smb_directory::get_directory(std::string_view path) const
smb_stat_deleter(),
};
if (not st) {
throw std::runtime_error("failed to stat directory|" + rel_path);
throw utils::error::create_exception({
function_name,
"failed to stat directory",
rel_path,
});
}
bool is_dir{smb_stat_get(st.get(), SMB_STAT_ISDIR) != 0U};
if (not is_dir) {
throw std::runtime_error("path is not a directory|" + rel_path);
throw utils::error::create_exception({
function_name,
"path is not a directory",
rel_path,
});
}
return smb_directory_t{
@ -311,7 +375,11 @@ auto smb_directory::get_directories() const -> std::vector<fs_directory_t> {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
smb_stat_list_t list{
smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str()),
@ -319,7 +387,11 @@ auto smb_directory::get_directories() const -> std::vector<fs_directory_t> {
smb_stat_list_deleter(),
};
if (not list) {
throw std::runtime_error("failed to get directory list|" + path_);
throw utils::error::create_exception({
function_name,
"failed to get directory list",
path_,
});
}
std::vector<fs_directory_t> ret{};
@ -370,7 +442,11 @@ auto smb_directory::get_file(std::string_view path) const -> fs_file_t {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
auto rel_path = smb_create_and_validate_relative_path(path_, path);
@ -379,12 +455,20 @@ auto smb_directory::get_file(std::string_view path) const -> fs_file_t {
smb_stat_deleter(),
};
if (not st) {
throw std::runtime_error("failed to stat file|" + rel_path);
throw utils::error::create_exception({
function_name,
"failed to stat file",
rel_path,
});
}
bool is_dir{smb_stat_get(st.get(), SMB_STAT_ISDIR) != 0U};
if (is_dir) {
throw std::runtime_error("path is not a file|" + rel_path);
throw utils::error::create_exception({
function_name,
"path is not a file",
rel_path,
});
}
return std::make_unique<smb_file>(
@ -404,7 +488,11 @@ auto smb_directory::get_files() const -> std::vector<fs_file_t> {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
smb_stat_list_t list{
@ -412,7 +500,11 @@ auto smb_directory::get_files() const -> std::vector<fs_file_t> {
smb_stat_list_deleter(),
};
if (not list) {
throw std::runtime_error("failed to get file list|" + path_);
throw utils::error::create_exception({
function_name,
"failed to get file list",
path_,
});
}
std::vector<fs_file_t> ret{};
@ -453,7 +545,11 @@ auto smb_directory::get_items() const -> std::vector<fs_item_t> {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
smb_stat_list_t list{
@ -461,7 +557,11 @@ auto smb_directory::get_items() const -> std::vector<fs_item_t> {
smb_stat_list_deleter(),
};
if (not list) {
throw std::runtime_error("failed to get item list|" + path_);
throw utils::error::create_exception({
function_name,
"failed to get item list",
path_,
});
}
std::vector<fs_item_t> ret{};
@ -530,7 +630,11 @@ auto smb_directory::is_symlink() const -> bool {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
} catch (const std::exception &e) {
@ -547,11 +651,20 @@ auto smb_directory::move_to(std::string_view new_path) -> bool {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
throw std::runtime_error("failed to move directory|" + path_ + '|' +
std::string{new_path} + "|not implemented");
throw utils::error::create_exception({
function_name,
"failed to move directory",
"not implemented",
path_,
new_path,
});
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
@ -566,7 +679,11 @@ auto smb_directory::remove() -> bool {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
return utils::retry_action([this]() -> bool {
@ -578,8 +695,12 @@ auto smb_directory::remove() -> bool {
auto res = smb_directory_rm(session_.get(), tid_,
smb_create_relative_path(path_).c_str());
if (res != DSM_SUCCESS) {
throw std::runtime_error("failed to remove directory|" + path_ + '|' +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to remove directory",
std::to_string(res),
path_,
});
}
return true;
@ -605,15 +726,23 @@ auto smb_directory::remove_recursively() -> bool {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
if (not exists()) {
return true;
}
throw std::runtime_error("failed to remove directory recursively|" + path_ +
"|not implemented");
throw utils::error::create_exception({
function_name,
"failed to remove directory recursively",
"not implemented",
path_,
});
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
@ -628,11 +757,19 @@ auto smb_directory::size(bool /* recursive */) const -> std::uint64_t {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
throw std::runtime_error("failed to get directory size|" + path_ +
"|not implemented");
throw utils::error::create_exception({
function_name,
"failed to get directory size",
"not implemented",
path_,
});
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {

View File

@ -41,14 +41,23 @@ auto smb_file::copy_to(std::string_view new_path,
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
// auto to_path = utils::path::absolute(new_path);
throw std::runtime_error("failed to copy file|" + path_ + '|' +
std::string{new_path} + '|' +
std::to_string(overwrite) + "|not implemented");
throw utils::error::create_exception({
function_name,
"failed to copy file",
"not implemented",
std::to_string(overwrite),
path_,
new_path,
});
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
@ -63,7 +72,11 @@ auto smb_file::exists() const -> bool {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
smb_stat_t st{
@ -89,8 +102,12 @@ void smb_file::flush() const {
REPERTORY_USES_FUNCTION_NAME();
try {
throw std::runtime_error("failed to flush file|" + path_ +
"|not implemented");
throw utils::error::create_exception({
function_name,
"failed to flush file",
"not implemented",
path_,
});
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
@ -104,7 +121,11 @@ auto smb_file::get_time(smb_session *session, smb_tid tid, std::string path,
try {
if (session == nullptr) {
throw std::runtime_error("session not found|" + path);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
auto rel_path = smb_create_relative_path(path);
@ -113,7 +134,12 @@ auto smb_file::get_time(smb_session *session, smb_tid tid, std::string path,
smb_stat_deleter(),
};
if (not st) {
throw std::runtime_error("failed to stat directory|" + rel_path);
throw utils::error::create_exception({
function_name,
"failed to stat file",
"not implemented",
rel_path,
});
}
switch (type) {
@ -143,7 +169,11 @@ auto smb_file::is_symlink() const -> bool {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
} catch (const std::exception &e) {
@ -160,9 +190,13 @@ auto smb_file::move_to(std::string_view new_path) -> bool {
try {
if (utils::string::begins_with(new_path, "//")) {
throw std::runtime_error("failed to move file|" + path_ + '|' +
std::string{new_path} +
"|new path must be in same share");
throw utils::error::create_exception({
function_name,
"failed to move file",
"new path must be in same share",
path_,
new_path,
});
}
auto from_path = smb_create_relative_path(path_);
@ -179,15 +213,23 @@ auto smb_file::move_to(std::string_view new_path) -> bool {
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));
throw utils::error::create_exception({
function_name,
"failed to connect to share",
path_std::to_string(res),
share_name_,
});
}
res = smb_file_mv(session_.get(), tid_, from_path.c_str(), to_path.c_str());
if (res != DSM_SUCCESS) {
throw std::runtime_error("failed to move file|" + path_ + '|' +
from_path + '|' + to_path + '|' +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to move file",
path_std::to_string(res),
from_path,
to_path,
});
}
path_ = smb_create_smb_path(path_, to_path);
@ -221,17 +263,26 @@ auto smb_file::open(bool read_only) -> bool {
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));
throw utils::error::create_exception({
function_name,
"failed to connect to share",
path_std::to_string(res),
share_name_,
});
}
smb_fd fd{};
res = smb_fopen(session_.get(), tid_, rel_path.c_str(),
read_only ? SMB_MOD_RO : SMB_MOD_RW2, &fd);
if (res != DSM_SUCCESS) {
throw std::runtime_error("failed to open file|" + path_ + '|' + rel_path +
'|' + utils::string::from_bool(read_only) + '|' +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to open file",
utils::string::from_bool(read_only),
path_std::to_string(res),
path_,
rel_path,
});
}
fd_ = fd;
@ -257,16 +308,24 @@ auto smb_file::read(unsigned char *data, std::size_t to_read,
}
if (not fd_.has_value()) {
throw std::runtime_error("failed to read file|" + path_ +
"|file not open");
throw utils::error::create_exception({
function_name,
"failed to read file",
"file not open",
path_,
});
}
auto res = smb_fseek(session_.get(), *fd_, static_cast<off_t>(offset),
SMB_SEEK_SET);
if (res == -1) {
throw std::runtime_error("failed to seek file|" + path_ + '|' +
std::to_string(offset) + '|' +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to seek file",
std::to_string(res),
std::to_string(offset),
path_,
});
}
std::size_t bytes_read{0U};
@ -274,9 +333,14 @@ auto smb_file::read(unsigned char *data, std::size_t to_read,
res = smb_fread(session_.get(), *fd_, &data[bytes_read],
to_read - bytes_read);
if (res == -1) {
throw std::runtime_error("failed to read file|" + path_ + '|' +
std::to_string(to_read) + '|' +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to read file",
std::to_string(res),
std::to_string(offset),
std::to_string(to_read),
path_,
});
}
if (res == 0) {
@ -314,17 +378,25 @@ auto smb_file::remove() -> 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));
throw utils::error::create_exception({
function_name,
"failed to connect to share",
path_std::to_string(res),
share_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())));
throw utils::error::create_exception({
function_name,
"failed to remove file",
std::to_string(res),
std::to_string(smb_session_get_nt_status(session_.get())),
path_,
rel_path,
});
}
return true;
@ -350,7 +422,11 @@ auto smb_file::size() const -> std::optional<std::uint64_t> {
try {
if (not session_) {
throw std::runtime_error("session not found|" + path_);
throw utils::error::create_exception({
function_name,
"session not found",
path_,
});
}
auto rel_path = smb_create_relative_path(path_);
@ -359,7 +435,11 @@ auto smb_file::size() const -> std::optional<std::uint64_t> {
smb_stat_deleter(),
};
if (not st) {
throw std::runtime_error("failed to stat directory|" + rel_path);
throw utils::error::create_exception({
function_name,
"failed to stat directory",
rel_path,
});
}
return smb_stat_get(st.get(), SMB_STAT_SIZE);
@ -376,8 +456,13 @@ auto smb_file::truncate(std::size_t size) -> bool {
REPERTORY_USES_FUNCTION_NAME();
try {
throw std::runtime_error("failed to truncate file|" + path_ + '|' +
std::to_string(size) + "|not implemented");
throw utils::error::create_exception({
function_name,
"failed to truncate file",
"not implemented",
std::to_string(size),
path_,
});
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
@ -397,16 +482,24 @@ auto smb_file::write(const unsigned char *data, std::size_t to_write,
}
if (not fd_.has_value()) {
throw std::runtime_error("failed to write file|" + path_ +
"|file not open");
throw utils::error::create_exception({
function_name,
"failed to write file",
"file not open",
path_,
});
}
auto res = smb_fseek(session_.get(), *fd_, static_cast<off_t>(offset),
SMB_SEEK_SET);
if (res == -1) {
throw std::runtime_error("failed to seek file|" + path_ + '|' +
std::to_string(offset) + '|' +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to seek file",
std::to_string(res),
std::to_string(offset),
path_,
});
}
std::size_t bytes_written{0U};
@ -415,9 +508,14 @@ auto smb_file::write(const unsigned char *data, std::size_t to_write,
const_cast<unsigned char *>(&data[bytes_written]),
to_write - bytes_written);
if (res == -1) {
throw std::runtime_error("failed to write file|" + path_ + '|' +
std::to_string(to_write) + '|' +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to write file",
std::to_string(res),
std::to_string(offset),
std::to_string(to_write),
path_,
});
}
if (res == 0) {

View File

@ -23,6 +23,8 @@
#include "utils/hash.hpp"
#include "utils/error.hpp"
namespace repertory::utils::encryption {
auto create_hash_blake2b_256(std::string_view data) -> hash_256_t {
return create_hash_blake2b_t<hash_256_t>(
@ -111,24 +113,36 @@ auto create_hash_sha512(const data_buffer &data) -> hash_512_t {
auto create_hash_sha512(const unsigned char *data,
std::size_t data_size) -> hash_512_t {
REPERTORY_USES_FUNCTION_NAME();
hash_512_t hash{};
crypto_hash_sha512_state state{};
auto res = crypto_hash_sha512_init(&state);
if (res != 0) {
throw std::runtime_error("failed to initialize sha-512|" +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to initialize sha-512",
std::to_string(res),
});
}
res = crypto_hash_sha512_update(&state, data, data_size);
if (res != 0) {
throw std::runtime_error("failed to update sha-512|" + std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to update sha-512",
std::to_string(res),
});
}
res = crypto_hash_sha512_final(&state, hash.data());
if (res != 0) {
throw std::runtime_error("failed to finalize sha-512|" +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to finalize sha-512",
std::to_string(res),
});
}
return hash;
@ -136,24 +150,36 @@ auto create_hash_sha512(const unsigned char *data,
auto create_hash_sha256(const unsigned char *data,
std::size_t data_size) -> hash_256_t {
REPERTORY_USES_FUNCTION_NAME();
hash_256_t hash{};
crypto_hash_sha256_state state{};
auto res = crypto_hash_sha256_init(&state);
if (res != 0) {
throw std::runtime_error("failed to initialize sha-256|" +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to initialize sha-256",
std::to_string(res),
});
}
res = crypto_hash_sha256_update(&state, data, data_size);
if (res != 0) {
throw std::runtime_error("failed to update sha-256|" + std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to update sha-256",
std::to_string(res),
});
}
res = crypto_hash_sha256_final(&state, hash.data());
if (res != 0) {
throw std::runtime_error("failed to finalize sha-256|" +
std::to_string(res));
throw utils::error::create_exception({
function_name,
"failed to finalize sha-256",
std::to_string(res),
});
}
return hash;

View File

@ -29,6 +29,8 @@
namespace {
[[nodiscard]] auto resolve(std::string path) -> std::string {
REPERTORY_USES_FUNCTION_NAME();
#if defined(_WIN32)
if (repertory::utils::string::contains(path, "~\\")) {
repertory::utils::string::replace(path, "~\\", "%USERPROFILE%\\");
@ -63,7 +65,11 @@ namespace {
}
});
if (res) {
throw std::runtime_error("failed to getpwuid: " + res.reason);
throw repertory::utils::error::create_exception({
function_name,
"failed to getpwuid",
res.reason,
});
}
path = repertory::utils::string::replace(path, "~/", home + "/");

View File

@ -24,6 +24,7 @@
#include "utils/windows.hpp"
#include "utils/com_init_wrapper.hpp"
#include "utils/error.hpp"
#include "utils/string.hpp"
namespace repertory::utils {
@ -56,13 +57,13 @@ void create_console() {
std::wcin.clear();
}
void free_console() {
::FreeConsole();
}
void free_console() { ::FreeConsole(); }
auto get_last_error_code() -> DWORD { return ::GetLastError(); }
auto get_local_app_data_directory() -> const std::string & {
REPERTORY_USES_FUNCTION_NAME();
static std::string app_data = ([]() -> std::string {
com_init_wrapper cw;
PWSTR local_app_data{};
@ -73,7 +74,10 @@ auto get_local_app_data_directory() -> const std::string & {
return ret;
}
throw std::runtime_error("unable to detect local application data folder");
throw utils::error::create_exception({
function_name,
"unable to detect local application data folder",
});
})();
return app_data;