refactor encryption provider db

This commit is contained in:
Scott E. Graves 2024-12-17 11:16:00 -06:00
parent db0b209ca6
commit 5f1d65f1f2
4 changed files with 56 additions and 98 deletions

View File

@ -29,6 +29,12 @@ class i_file_db {
INTERFACE_SETUP(i_file_db);
public:
struct file_info final {
std::string api_path;
bool directory;
std::string source_path;
};
struct file_data final {
std::string api_path;
std::uint64_t file_size{};
@ -74,6 +80,9 @@ public:
get_file_source_path(const std::string &api_path,
std::string &source_path) const -> api_error = 0;
[[nodiscard]] virtual auto get_item_list() const
-> std::vector<file_info> = 0;
[[nodiscard]] virtual auto get_source_path(const std::string &api_path,
std::string &source_path) const
-> api_error = 0;

View File

@ -77,6 +77,9 @@ public:
std::string &source_path) const
-> api_error override;
[[nodiscard]] auto get_item_list() const
-> std::vector<i_file_db::file_info> override;
[[nodiscard]] auto get_source_path(const std::string &api_path,
std::string &source_path) const
-> api_error override;

View File

@ -52,7 +52,7 @@ const std::map<std::string, std::string> sql_create_tables = {
namespace repertory {
sqlite_file_db::sqlite_file_db(const app_config &cfg) {
db_ = utils::db::sqlite::create_db(
utils::path::combine(cfg.get_data_directory(), {"file.db"}),
utils::path::combine(cfg.get_data_directory(), {"provider_file.db"}),
sql_create_tables);
}
@ -275,6 +275,27 @@ auto sqlite_file_db::get_file_source_path(const std::string &api_path,
return api_error::item_not_found;
}
auto sqlite_file_db::get_item_list() const
-> std::vector<i_file_db::file_info> {
std::vector<i_file_db::file_info> ret;
auto result = utils::db::sqlite::db_select{*db_, file_table}.go();
while (result.has_value()) {
std::optional<utils::db::sqlite::db_result::row> row;
if (result.get_row(row) && row.has_value()) {
ret.emplace_back(i_file_db::file_info{
row->get_column("api_path").get_value<std::string>(),
row->get_column("directory").get_value<std::int64_t>() == 1,
row->get_column("source_path").get_value<std::string>(),
});
}
result.next_row();
}
return ret;
}
auto sqlite_file_db::get_source_path(const std::string &api_path,
std::string &source_path) const
-> api_error {

View File

@ -27,10 +27,6 @@
#include "types/repertory.hpp"
#include "types/startup_exception.hpp"
#include "utils/collection.hpp"
#include "utils/db/sqlite/db_common.hpp"
#include "utils/db/sqlite/db_delete.hpp"
#include "utils/db/sqlite/db_insert.hpp"
#include "utils/db/sqlite/db_select.hpp"
#include "utils/encrypting_reader.hpp"
#include "utils/encryption.hpp"
#include "utils/file_utils.hpp"
@ -794,47 +790,15 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path,
}
void encrypt_provider::remove_deleted_files(const stop_type &stop_requested) {
struct removed_item {
std::string api_path;
bool directory{};
std::string source_path;
};
std::vector<i_file_db::file_info> removed_list{};
std::vector<removed_item> removed_list{};
std::vector<utils::db::sqlite::db_result::row> row_list{};
auto result = utils::db::sqlite::db_select{*db_, file_table}.go();
while (result.has_row()) {
for (const auto &item : db_->get_item_list()) {
if (stop_requested) {
return;
}
std::optional<utils::db::sqlite::db_result::row> row;
if (result.get_row(row) && row.has_value()) {
row_list.push_back(row.value());
}
}
for (const auto &row : row_list) {
if (stop_requested) {
return;
}
auto source_path = row.get_column("source_path").get_value<std::string>();
if (not utils::path::exists(source_path)) {
auto api_path = row.get_column("api_path").get_value<std::string>();
result = utils::db::sqlite::db_select{*db_, file_table}
.column("source_path")
.where("source_path")
.equals(source_path)
.op()
.limit(1)
.go();
removed_list.emplace_back(removed_item{
api_path,
row.get_column("directory").get_value<std::int64_t>() == 1,
source_path,
});
if (not utils::path::exists(item.source_path)) {
removed_list.emplace_back(item);
}
}
@ -843,45 +807,16 @@ void encrypt_provider::remove_deleted_files(const stop_type &stop_requested) {
return;
}
// TODO handle error
auto del_res = db_->remove_item(item);
if (item.directory) {
continue;
}
// TODO handle error
auto del_res = utils::db::sqlite::db_delete{*db_, file_table}
.where("source_path")
.equals(item.source_path)
.and_()
.where("directory")
.equals(0)
.op()
.limit(1)
.go();
event_system::instance().raise<file_removed_externally>(item.api_path,
item.source_path);
}
for (const auto &item : removed_list) {
if (stop_requested) {
return;
}
if (not item.directory) {
continue;
}
// TODO handle error
auto del_res = utils::db::sqlite::db_delete{*db_, file_table}
.where("source_path")
.equals(item.source_path)
.and_()
.where("directory")
.equals(1)
.op()
.limit(1)
.go();
event_system::instance().raise<directory_removed_externally>(
item.api_path, item.source_path);
continue;
}
event_system::instance().raise<file_removed_externally>(item.api_path,
item.source_path);
}
}
@ -898,20 +833,16 @@ auto encrypt_provider::start(api_item_added_callback /*api_item_added*/,
auto cfg = config_.get_encrypt_config();
auto cfg_path = utils::path::absolute(cfg.path);
auto result = utils::db::sqlite::db_select{*db_, file_table}
.column("source_path")
.where("source_path")
.equals(cfg_path)
.and_()
.where("directory")
.equals(1)
.op()
.limit(1)
.go();
std::optional<utils::db::sqlite::db_result::row> row;
if (result.get_row(row) && row.has_value()) {
auto cur_path = utils::path::absolute(
row->get_column("source_path").get_value<std::string>());
std::string source_path;
auto result = db_->get_directory_source_path("/", source_path);
if (result != api_error::success &&
result != api_error::directory_not_found) {
throw startup_exception(
fmt::format("failed to get root|{}", api_error_to_string(result)));
}
if (result == api_error::success) {
auto cur_path = utils::path::absolute(source_path);
#if defined(_WIN32)
if (utils::string::to_lower(cur_path) !=
utils::string::to_lower(cfg_path)) {
@ -922,16 +853,10 @@ auto encrypt_provider::start(api_item_added_callback /*api_item_added*/,
"source path has changed|cur|{}|cfg|{}", cur_path, cfg.path));
}
} else {
auto ins_res = utils::db::sqlite::db_insert{*db_, file_table}
.column_value("api_path", "/")
.column_value("data", "")
.column_value("directory", 1)
.column_value("source_path", cfg_path)
.go();
if (not ins_res.ok()) {
throw startup_exception(fmt::format("failed to create root|{}|{}",
ins_res.get_error(),
ins_res.get_error_str()));
result = db_->add_directory("/", utils::path::absolute(source_path));
if (result != api_error::success) {
throw startup_exception(
fmt::format("failed to create root|{}", api_error_to_string(result)));
}
}