diff --git a/repertory/librepertory/src/file_manager/file_manager.cpp b/repertory/librepertory/src/file_manager/file_manager.cpp index e2ad0f8f..c1260328 100644 --- a/repertory/librepertory/src/file_manager/file_manager.cpp +++ b/repertory/librepertory/src/file_manager/file_manager.cpp @@ -115,7 +115,10 @@ file_manager::file_manager(app_config &config, i_provider &provider) throw startup_exception("failed to open db|" + db_path + '|' + std::to_string(res) + '|' + sqlite3_errstr(res)); } - db_.reset(db3); + db_ = utils::db::sqlite::db3_t{ + db3, + utils::db::sqlite::sqlite3_deleter(), + }; for (auto &&create_item : sql_create_tables) { std::string err; diff --git a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp index 6d57a624..24716485 100644 --- a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp +++ b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp @@ -1058,7 +1058,10 @@ auto encrypt_provider::start(api_item_added_callback /*api_item_added*/, '|' + sqlite3_errstr(res)); return false; } - db_.reset(db3); + db_ = utils::db::sqlite::db3_t{ + db3, + utils::db::sqlite::sqlite3_deleter(), + }; for (auto &&create : sql_create_tables) { std::string err; diff --git a/repertory/librepertory/src/providers/meta_db.cpp b/repertory/librepertory/src/providers/meta_db.cpp index 36424c18..c68d70a2 100644 --- a/repertory/librepertory/src/providers/meta_db.cpp +++ b/repertory/librepertory/src/providers/meta_db.cpp @@ -48,7 +48,10 @@ meta_db::meta_db(const app_config &cfg) { '|' + sqlite3_errstr(res)); return; } - db_.reset(db3); + db_ = utils::db::sqlite::db3_t{ + db3, + utils::db::sqlite::sqlite3_deleter(), + }; const auto *create = "CREATE TABLE IF NOT EXISTS " "meta " diff --git a/support/include/utils/config.hpp b/support/include/utils/config.hpp index 8794abba..963ce6a6 100644 --- a/support/include/utils/config.hpp +++ b/support/include/utils/config.hpp @@ -174,7 +174,7 @@ extern "C" { } struct netbios_ns_deleter final { - void operator()(netbios_ns *ns) { + void operator()(netbios_ns *ns) const { if (ns != nullptr) { netbios_ns_destroy(ns); } @@ -190,7 +190,7 @@ inline const auto smb_session_deleter = [](smb_session *session) { using smb_session_t = std::shared_ptr; struct smb_stat_deleter final { - void operator()(smb_stat st) { + void operator()(smb_stat st) const { if (st != nullptr) { smb_stat_destroy(st); } @@ -199,7 +199,7 @@ struct smb_stat_deleter final { using smb_stat_t = std::unique_ptr; struct smb_stat_list_deleter final { - void operator()(smb_file *list) { + void operator()(smb_file *list) const { if (list != nullptr) { smb_stat_list_destroy(list); } @@ -241,7 +241,7 @@ using smb_stat_list_t = std::unique_ptr; } struct vlc_deleter final { - void operator()(libvlc_instance_t *inst) { + void operator()(libvlc_instance_t *inst) const { if (inst != nullptr) { libvlc_release(inst); } @@ -250,7 +250,7 @@ struct vlc_deleter final { using vlc_t = std::unique_ptr; struct vlc_media_deleter final { - void operator()(libvlc_media_t *media) { + void operator()(libvlc_media_t *media) const { if (media != nullptr) { libvlc_media_release(media); } @@ -259,7 +259,7 @@ struct vlc_media_deleter final { using vlc_media_t = std::unique_ptr; struct vlc_media_list_deleter final { - void operator()(libvlc_media_list_t *media_list) { + void operator()(libvlc_media_list_t *media_list) const { if (media_list != nullptr) { libvlc_media_list_release(media_list); } @@ -269,7 +269,7 @@ using vlc_media_list_t = std::unique_ptr; struct vlc_string_deleter final { - void operator()(char *str) { + void operator()(char *str) const { if (str != nullptr) { libvlc_free(str); } diff --git a/support/include/utils/db/sqlite/db_common.hpp b/support/include/utils/db/sqlite/db_common.hpp index a12d2220..02d100ff 100644 --- a/support/include/utils/db/sqlite/db_common.hpp +++ b/support/include/utils/db/sqlite/db_common.hpp @@ -27,7 +27,7 @@ namespace repertory::utils::db::sqlite { using db_types_t = std::variant; struct sqlite3_deleter { - void operator()(sqlite3 *db3) { + void operator()(sqlite3 *db3) const { if (db3 != nullptr) { sqlite3_close_v2(db3); } @@ -37,8 +37,9 @@ struct sqlite3_deleter { using db3_t = std::unique_ptr; struct sqlite3_statement_deleter { - void operator()(sqlite3_stmt *stmt) { + void operator()(sqlite3_stmt *stmt) const { if (stmt != nullptr) { + sqlite3_reset(stmt); sqlite3_finalize(stmt); } } @@ -59,6 +60,9 @@ struct db_comp_data_t final { struct db_context_t { db_context_t(sqlite3 &db3_, std::string table_name_) : db3(db3_), table_name(std::move(table_name_)) {} + + virtual ~db_context_t() = default; + sqlite3 &db3; std::string table_name; diff --git a/support/src/utils/db/sqlite/db_delete.cpp b/support/src/utils/db/sqlite/db_delete.cpp index fad23bcc..09a45c6e 100644 --- a/support/src/utils/db/sqlite/db_delete.cpp +++ b/support/src/utils/db/sqlite/db_delete.cpp @@ -39,20 +39,19 @@ auto db_delete::dump() const -> std::string { } auto db_delete::go() const -> db_result { - static constexpr const std::string_view function_name{ - static_cast(__FUNCTION__), - }; - sqlite3_stmt *stmt_ptr{nullptr}; auto query_str = dump(); auto res = sqlite3_prepare_v2(&context_->db3, query_str.c_str(), -1, &stmt_ptr, nullptr); + context_->stmt = db3_stmt_t{ + stmt_ptr, + sqlite3_statement_deleter(), + }; + if (res != SQLITE_OK) { return {context_, res}; } - context_->stmt.reset(stmt_ptr); - for (std::int32_t idx = 0; idx < static_cast(context_->where_values.size()); idx++) { res = std::visit( diff --git a/support/src/utils/db/sqlite/db_insert.cpp b/support/src/utils/db/sqlite/db_insert.cpp index b7d2b854..4170eca8 100644 --- a/support/src/utils/db/sqlite/db_insert.cpp +++ b/support/src/utils/db/sqlite/db_insert.cpp @@ -68,12 +68,15 @@ auto db_insert::go() const -> db_result { auto query_str = dump(); auto res = sqlite3_prepare_v2(&context_->db3, query_str.c_str(), -1, &stmt_ptr, nullptr); + context_->stmt = db3_stmt_t{ + stmt_ptr, + sqlite3_statement_deleter(), + }; + if (res != SQLITE_OK) { return {context_, res}; } - context_->stmt.reset(stmt_ptr); - for (std::int32_t idx = 0; idx < static_cast(context_->values.size()); idx++) { res = std::visit( diff --git a/support/src/utils/db/sqlite/db_select.cpp b/support/src/utils/db/sqlite/db_select.cpp index 3a40dcd1..69148aec 100644 --- a/support/src/utils/db/sqlite/db_select.cpp +++ b/support/src/utils/db/sqlite/db_select.cpp @@ -85,20 +85,19 @@ auto db_select::dump() const -> std::string { } auto db_select::go() const -> db_result { - static constexpr const std::string_view function_name{ - static_cast(__FUNCTION__), - }; - sqlite3_stmt *stmt_ptr{nullptr}; auto query_str = dump(); auto res = sqlite3_prepare_v2(&context_->db3, query_str.c_str(), -1, &stmt_ptr, nullptr); + context_->stmt = db3_stmt_t{ + stmt_ptr, + sqlite3_statement_deleter(), + }; + if (res != SQLITE_OK) { return {context_, res}; } - context_->stmt.reset(stmt_ptr); - for (std::int32_t idx = 0; idx < static_cast(context_->where_values.size()); idx++) { res = std::visit( diff --git a/support/src/utils/db/sqlite/db_update.cpp b/support/src/utils/db/sqlite/db_update.cpp index 1cc316a9..64776262 100644 --- a/support/src/utils/db/sqlite/db_update.cpp +++ b/support/src/utils/db/sqlite/db_update.cpp @@ -64,18 +64,18 @@ auto db_update::dump() const -> std::string { } auto db_update::go() const -> db_result { - static constexpr const std::string_view function_name{ - static_cast(__FUNCTION__), - }; - sqlite3_stmt *stmt_ptr{nullptr}; auto query_str = dump(); auto res = sqlite3_prepare_v2(&context_->db3, query_str.c_str(), -1, &stmt_ptr, nullptr); + context_->stmt = db3_stmt_t{ + stmt_ptr, + sqlite3_statement_deleter(), + }; + if (res != SQLITE_OK) { return {context_, res}; } - context_->stmt.reset(stmt_ptr); for (std::int32_t idx = 0; idx < static_cast(context_->column_values.size()); idx++) { diff --git a/support/src/utils/file_file.cpp b/support/src/utils/file_file.cpp index 983c9032..b1049411 100644 --- a/support/src/utils/file_file.cpp +++ b/support/src/utils/file_file.cpp @@ -119,9 +119,11 @@ void file::open() { } #if defined(_WIN32) - file_.reset(_fsopen(path_.c_str(), read_only_ ? "rb" : "rb+", _SH_DENYNO)); + file_ = file_t(_fsopen(path_.c_str(), read_only_ ? "rb" : "rb+", _SH_DENYNO), + file_deleter()); #else // !defined(_WIN32) - file_.reset(fopen(path_.c_str(), read_only_ ? "rb" : "rb+")); + file_ = + file_t(fopen(path_.c_str(), read_only_ ? "rb" : "rb+"), file_deleter()); #endif // defined(_WIN32) } diff --git a/support/src/utils/file_smb_directory.cpp b/support/src/utils/file_smb_directory.cpp index 54e9162e..fac7f371 100644 --- a/support/src/utils/file_smb_directory.cpp +++ b/support/src/utils/file_smb_directory.cpp @@ -34,8 +34,14 @@ auto smb_directory::open(std::string_view host, std::string_view user, }; try { - smb_session_t session{smb_session_new(), smb_session_deleter}; - netbios_ns_t ns{netbios_ns_new()}; + smb_session_t session{ + smb_session_new(), + smb_session_deleter(), + }; + netbios_ns_t ns{ + netbios_ns_new(), + netbios_ns_deleter(), + }; sockaddr_in addr{}; @@ -137,7 +143,9 @@ auto smb_directory::count(bool recursive) const -> std::uint64_t { } smb_stat_list_t list{ - smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str())}; + smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str()), + smb_stat_list_deleter(), + }; auto count = smb_stat_list_count(list.get()); if (not recursive) { @@ -247,8 +255,11 @@ auto smb_directory::exists() const -> bool { throw std::runtime_error("session not found|" + path_); } - smb_stat_t st{smb_fstat(session_.get(), tid_, - smb_create_relative_path(path_).c_str())}; + smb_stat_t st{ + smb_fstat(session_.get(), tid_, + smb_create_relative_path(path_).c_str()), + smb_stat_deleter(), + }; if (not st) { return false; } @@ -275,7 +286,10 @@ auto smb_directory::get_directory(std::string_view path) const } auto rel_path = smb_create_and_validate_relative_path(path_, path); - smb_stat_t st{smb_fstat(session_.get(), tid_, rel_path.c_str())}; + smb_stat_t st{ + smb_fstat(session_.get(), tid_, rel_path.c_str()), + smb_stat_deleter(), + }; if (not st) { throw std::runtime_error("failed to stat directory|" + rel_path); } @@ -313,7 +327,9 @@ auto smb_directory::get_directories() const -> std::vector { } smb_stat_list_t list{ - smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str())}; + smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str()), + smb_stat_list_deleter(), + }; if (not list) { throw std::runtime_error("failed to get directory list|" + path_); } @@ -365,7 +381,10 @@ auto smb_directory::get_file(std::string_view path) const -> fs_file_t { } auto rel_path = smb_create_and_validate_relative_path(path_, path); - smb_stat_t st{smb_fstat(session_.get(), tid_, rel_path.c_str())}; + smb_stat_t st{ + smb_fstat(session_.get(), tid_, rel_path.c_str()), + smb_stat_deleter(), + }; if (not st) { throw std::runtime_error("failed to stat file|" + rel_path); } @@ -398,7 +417,9 @@ auto smb_directory::get_files() const -> std::vector { } smb_stat_list_t list{ - smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str())}; + smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str()), + smb_stat_list_deleter(), + }; if (not list) { throw std::runtime_error("failed to get file list|" + path_); } @@ -440,7 +461,9 @@ auto smb_directory::get_items() const -> std::vector { } smb_stat_list_t list{ - smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str())}; + smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str()), + smb_stat_list_deleter(), + }; if (not list) { throw std::runtime_error("failed to get item list|" + path_); } diff --git a/support/src/utils/file_smb_file.cpp b/support/src/utils/file_smb_file.cpp index dde4eb71..a1cd9d7a 100644 --- a/support/src/utils/file_smb_file.cpp +++ b/support/src/utils/file_smb_file.cpp @@ -70,8 +70,11 @@ auto smb_file::exists() const -> bool { throw std::runtime_error("session not found|" + path_); } - smb_stat_t st{smb_fstat(session_.get(), tid_, - smb_create_relative_path(path_).c_str())}; + smb_stat_t st{ + smb_fstat(session_.get(), tid_, + smb_create_relative_path(path_).c_str()), + smb_stat_deleter(), + }; if (not st) { return false; } @@ -113,7 +116,10 @@ auto smb_file::get_time(smb_session *session, smb_tid tid, std::string path, } auto rel_path = smb_create_relative_path(path); - smb_stat_t st{smb_fstat(session, tid, rel_path.c_str())}; + smb_stat_t st{ + smb_fstat(session, tid, rel_path.c_str()), + smb_stat_deleter(), + }; if (not st) { throw std::runtime_error("failed to stat directory|" + rel_path); } @@ -368,7 +374,10 @@ auto smb_file::size() const -> std::optional { } auto rel_path = smb_create_relative_path(path_); - smb_stat_t st{smb_fstat(session_.get(), tid_, rel_path.c_str())}; + smb_stat_t st{ + smb_fstat(session_.get(), tid_, rel_path.c_str()), + smb_stat_deleter(), + }; if (not st) { throw std::runtime_error("failed to stat directory|" + rel_path); } diff --git a/support/test/src/utils/db_sqlite_test.cpp b/support/test/src/utils/db_sqlite_test.cpp index 0e233af2..c4351a37 100644 --- a/support/test/src/utils/db_sqlite_test.cpp +++ b/support/test/src/utils/db_sqlite_test.cpp @@ -34,7 +34,10 @@ public: auto res = sqlite3_open(":memory:", &db3_ptr); ASSERT_EQ(SQLITE_OK, res); ASSERT_TRUE(db3_ptr != nullptr); - db3.reset(db3_ptr); + db3 = utils::db::sqlite::db3_t{ + db3_ptr, + utils::db::sqlite::sqlite3_deleter(), + }; } utils::db::sqlite::db3_stmt_t db3_stmt; @@ -47,8 +50,11 @@ public: sqlite3_stmt *stmt_ptr{nullptr}; auto res = sqlite3_prepare_v2(db3.get(), sql.c_str(), -1, &stmt_ptr, nullptr); + db3_stmt = utils::db::sqlite::db3_stmt_t{ + stmt_ptr, + utils::db::sqlite::sqlite3_statement_deleter(), + }; ASSERT_EQ(SQLITE_OK, res); - db3_stmt.reset(stmt_ptr); } auto res = sqlite3_step(db3_stmt.get());