This commit is contained in:
2025-01-02 11:56:12 -06:00
parent c27528fe73
commit b5048a422f
2 changed files with 87 additions and 75 deletions

View File

@ -269,14 +269,13 @@ auto file_manager::get_open_file_count() const -> std::size_t {
auto file_manager::get_open_files() const auto file_manager::get_open_files() const
-> std::unordered_map<std::string, std::size_t> { -> std::unordered_map<std::string, std::size_t> {
std::unordered_map<std::string, std::size_t> ret;
recur_mutex_lock open_lock(open_file_mtx_); recur_mutex_lock open_lock(open_file_mtx_);
for (const auto &item : open_file_lookup_) { return std::accumulate(open_file_lookup_.begin(), open_file_lookup_.end(),
ret[item.first] = item.second->get_open_file_count(); std::unordered_map<std::string, std::size_t>{},
} [](auto map, const auto &item) -> std::size_t {
map[item.first] = item.second->get_open_file_count();
return ret; return map;
});
} }
auto file_manager::get_open_handle_count() const -> std::size_t { auto file_manager::get_open_handle_count() const -> std::size_t {

View File

@ -42,7 +42,7 @@ auto encrypt_provider::create_api_file(const std::string &api_path,
bool directory, bool directory,
const std::string &source_path) const std::string &source_path)
-> api_file { -> api_file {
auto times = utils::file::get_times(source_path); auto times{utils::file::get_times(source_path)};
if (not times.has_value()) { if (not times.has_value()) {
throw std::runtime_error("failed to get file times"); throw std::runtime_error("failed to get file times");
} }
@ -113,7 +113,7 @@ auto encrypt_provider::do_fs_operation(
std::function<api_error(const encrypt_config &cfg, std::function<api_error(const encrypt_config &cfg,
const std::string &source_path)> const std::string &source_path)>
callback) const -> api_error { callback) const -> api_error {
const auto &cfg = get_encrypt_config(); const auto &cfg{get_encrypt_config()};
std::string source_path{api_path}; std::string source_path{api_path};
if (api_path != "/" && not utils::encryption::decrypt_file_path( if (api_path != "/" && not utils::encryption::decrypt_file_path(
@ -130,7 +130,7 @@ auto encrypt_provider::do_fs_operation(
: api_error::item_not_found; : api_error::item_not_found;
} }
auto exists = utils::file::file{source_path}.exists(); auto exists{utils::file::file{source_path}.exists()};
if (exists && directory) { if (exists && directory) {
return api_error::item_exists; return api_error::item_exists;
} }
@ -170,19 +170,21 @@ auto encrypt_provider::get_directory_item_count(
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
std::uint64_t count{}; std::uint64_t count{};
auto res = do_fs_operation( auto res{
api_path, true, do_fs_operation(
[&api_path, &count](const encrypt_config & /* cfg */, api_path, true,
const std::string &source_path) -> api_error { [&api_path, &count](const encrypt_config & /* cfg */,
try { const std::string &source_path) -> api_error {
count = utils::file::directory{source_path}.count(); try {
} catch (const std::exception &ex) { count = utils::file::directory{source_path}.count();
utils::error::raise_api_path_error( } catch (const std::exception &ex) {
function_name, api_path, source_path, ex, utils::error::raise_api_path_error(
"failed to get directory item count"); function_name, api_path, source_path, ex,
} "failed to get directory item count");
return api_error::success; }
}); return api_error::success;
}),
};
if (res != api_error::success) { if (res != api_error::success) {
utils::error::raise_api_path_error(function_name, api_path, res, utils::error::raise_api_path_error(function_name, api_path, res,
"failed to get directory item count"); "failed to get directory item count");
@ -206,8 +208,10 @@ auto encrypt_provider::get_directory_items(const std::string &api_path,
try { try {
std::string current_api_path; std::string current_api_path;
if (dir_entry->is_directory_item()) { if (dir_entry->is_directory_item()) {
auto result = db_->get_directory_api_path(dir_entry->get_path(), auto result{
current_api_path); db_->get_directory_api_path(dir_entry->get_path(),
current_api_path),
};
if (result != api_error::success && if (result != api_error::success &&
result != api_error::directory_not_found) { result != api_error::directory_not_found) {
// TODO raise error // TODO raise error
@ -227,8 +231,10 @@ auto encrypt_provider::get_directory_items(const std::string &api_path,
} }
} }
} else { } else {
auto result = db_->get_file_api_path(dir_entry->get_path(), auto result{
current_api_path); db_->get_file_api_path(dir_entry->get_path(),
current_api_path),
};
if (result != api_error::success && if (result != api_error::success &&
result != api_error::item_not_found) { result != api_error::item_not_found) {
// TODO raise error // TODO raise error
@ -241,9 +247,11 @@ auto encrypt_provider::get_directory_items(const std::string &api_path,
} }
} }
auto file = create_api_file(current_api_path, auto file{
dir_entry->is_directory_item(), create_api_file(current_api_path,
dir_entry->get_path()); dir_entry->is_directory_item(),
dir_entry->get_path()),
};
directory_item dir_item{}; directory_item dir_item{};
dir_item.api_parent = file.api_parent; dir_item.api_parent = file.api_parent;
@ -297,7 +305,7 @@ auto encrypt_provider::get_file(const std::string &api_path,
try { try {
bool exists{}; bool exists{};
auto res = is_directory(api_path, exists); auto res{is_directory(api_path, exists)};
if (res != api_error::success) { if (res != api_error::success) {
return res; return res;
} }
@ -306,7 +314,7 @@ auto encrypt_provider::get_file(const std::string &api_path,
} }
std::string source_path; std::string source_path;
auto result = db_->get_file_source_path(api_path, source_path); auto result{db_->get_file_source_path(api_path, source_path)};
if (result != api_error::success) { if (result != api_error::success) {
return result; return result;
} }
@ -326,9 +334,9 @@ auto encrypt_provider::get_file_list(api_file_list &list,
-> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
const auto &cfg = get_encrypt_config();
try { try {
const auto &cfg{get_encrypt_config()};
for (const auto &dir_entry : utils::file::directory{cfg.path}.get_items()) { for (const auto &dir_entry : utils::file::directory{cfg.path}.get_items()) {
std::string api_path{}; std::string api_path{};
if (process_directory_entry(*dir_entry.get(), cfg, api_path)) { if (process_directory_entry(*dir_entry.get(), cfg, api_path)) {
@ -353,7 +361,7 @@ auto encrypt_provider::get_file_size(const std::string &api_path,
try { try {
std::string source_path; std::string source_path;
auto result = db_->get_file_source_path(api_path, source_path); auto result{db_->get_file_source_path(api_path, source_path)};
if (result != api_error::success) { if (result != api_error::success) {
return result; return result;
} }
@ -375,7 +383,7 @@ auto encrypt_provider::get_filesystem_item(const std::string &api_path,
-> api_error { -> api_error {
std::string source_path; std::string source_path;
if (directory) { if (directory) {
auto result = db_->get_directory_source_path(api_path, source_path); auto result{db_->get_directory_source_path(api_path, source_path)};
if (result != api_error::success) { if (result != api_error::success) {
return result; return result;
} }
@ -388,7 +396,7 @@ auto encrypt_provider::get_filesystem_item(const std::string &api_path,
return api_error::success; return api_error::success;
} }
auto result = db_->get_file_source_path(api_path, source_path); auto result{db_->get_file_source_path(api_path, source_path)};
if (result != api_error::success) { if (result != api_error::success) {
return result; return result;
} }
@ -406,7 +414,7 @@ auto encrypt_provider::get_filesystem_item(const std::string &api_path,
auto encrypt_provider::get_filesystem_item_from_source_path( auto encrypt_provider::get_filesystem_item_from_source_path(
const std::string &source_path, filesystem_item &fsi) const -> api_error { const std::string &source_path, filesystem_item &fsi) const -> api_error {
std::string api_path{}; std::string api_path{};
auto res = get_api_path_from_source(source_path, api_path); auto res{get_api_path_from_source(source_path, api_path)};
if (res != api_error::success) { if (res != api_error::success) {
return res; return res;
} }
@ -431,7 +439,7 @@ auto encrypt_provider::get_filesystem_item_and_file(const std::string &api_path,
try { try {
bool exists{}; bool exists{};
auto res = is_directory(api_path, exists); auto res{is_directory(api_path, exists)};
if (res != api_error::success) { if (res != api_error::success) {
return res; return res;
} }
@ -439,7 +447,7 @@ auto encrypt_provider::get_filesystem_item_and_file(const std::string &api_path,
return api_error::directory_exists; return api_error::directory_exists;
} }
auto ret = get_filesystem_item(api_path, exists, fsi); auto ret{get_filesystem_item(api_path, exists, fsi)};
if (ret != api_error::success) { if (ret != api_error::success) {
return ret; return ret;
} }
@ -464,7 +472,7 @@ auto encrypt_provider::get_item_meta(const std::string &api_path,
try { try {
std::string source_path; std::string source_path;
auto result = db_->get_source_path(api_path, source_path); auto result{db_->get_source_path(api_path, source_path)};
if (result != api_error::success) { if (result != api_error::success) {
return result; return result;
} }
@ -475,7 +483,7 @@ auto encrypt_provider::get_item_meta(const std::string &api_path,
return result; return result;
} }
auto file = create_api_file(api_path, is_dir, source_path); auto file{create_api_file(api_path, is_dir, source_path)};
create_item_meta(meta, is_dir, file); create_item_meta(meta, is_dir, file);
return api_error::success; return api_error::success;
} catch (const std::exception &ex) { } catch (const std::exception &ex) {
@ -490,7 +498,7 @@ auto encrypt_provider::get_item_meta(const std::string &api_path,
const std::string &key, const std::string &key,
std::string &value) const -> api_error { std::string &value) const -> api_error {
api_meta_map meta{}; api_meta_map meta{};
auto ret = get_item_meta(api_path, meta); auto ret{get_item_meta(api_path, meta)};
if (ret != api_error::success) { if (ret != api_error::success) {
return ret; return ret;
} }
@ -518,8 +526,9 @@ auto encrypt_provider::get_total_item_count() const -> std::uint64_t {
} }
auto encrypt_provider::get_used_drive_space() const -> std::uint64_t { auto encrypt_provider::get_used_drive_space() const -> std::uint64_t {
auto free_space = auto free_space{
utils::file::get_free_drive_space(get_encrypt_config().path); utils::file::get_free_drive_space(get_encrypt_config().path),
};
return free_space.has_value() ? get_total_drive_space() - free_space.value() return free_space.has_value() ? get_total_drive_space() - free_space.value()
: 0U; : 0U;
} }
@ -530,7 +539,7 @@ auto encrypt_provider::is_directory(const std::string &api_path,
try { try {
std::string source_path; std::string source_path;
auto result = db_->get_directory_source_path(api_path, source_path); auto result{db_->get_directory_source_path(api_path, source_path)};
if (result != api_error::success) { if (result != api_error::success) {
if (result != api_error::directory_not_found) { if (result != api_error::directory_not_found) {
@ -557,7 +566,7 @@ auto encrypt_provider::is_file(const std::string &api_path, bool &exists) const
try { try {
std::string source_path; std::string source_path;
auto result = db_->get_file_source_path(api_path, source_path); auto result{db_->get_file_source_path(api_path, source_path)};
if (result != api_error::success) { if (result != api_error::success) {
if (result != api_error::item_not_found) { if (result != api_error::item_not_found) {
return result; return result;
@ -594,8 +603,10 @@ auto encrypt_provider::process_directory_entry(
try { try {
const auto do_add_directory = const auto do_add_directory =
[this, &cfg](std::string_view dir_path) -> std::string { [this, &cfg](std::string_view dir_path) -> std::string {
auto encrypted_parts = utils::string::split( auto encrypted_parts{
utils::path::create_api_path(dir_path), '/', false); utils::string::split(utils::path::create_api_path(dir_path), '/',
false),
};
for (std::size_t part_idx = 1U; part_idx < encrypted_parts.size(); for (std::size_t part_idx = 1U; part_idx < encrypted_parts.size();
++part_idx) { ++part_idx) {
@ -618,8 +629,9 @@ auto encrypt_provider::process_directory_entry(
current_source_path = utils::path::combine(current_source_path, {part}); current_source_path = utils::path::combine(current_source_path, {part});
std::string current_api_path{}; std::string current_api_path{};
auto result = auto result{
db_->get_directory_api_path(current_source_path, current_api_path); db_->get_directory_api_path(current_source_path, current_api_path),
};
if (result == api_error::directory_not_found) { if (result == api_error::directory_not_found) {
current_api_path = utils::path::create_api_path( current_api_path = utils::path::create_api_path(
current_encrypted_path + '/' + encrypted_parts.at(current_idx)); current_encrypted_path + '/' + encrypted_parts.at(current_idx));
@ -659,11 +671,12 @@ auto encrypt_provider::process_directory_entry(
} }
if (dir_entry.is_file_item() && not dir_entry.is_symlink()) { if (dir_entry.is_file_item() && not dir_entry.is_symlink()) {
auto relative_path = auto relative_path{
utils::path::get_relative_path(dir_entry.get_path(), cfg.path); utils::path::get_relative_path(dir_entry.get_path(), cfg.path),
};
i_file_db::file_data data; i_file_db::file_data data;
auto file_res = db_->get_file_data(dir_entry.get_path(), data); auto file_res{db_->get_file_data(dir_entry.get_path(), data)};
if (file_res != api_error::success && if (file_res != api_error::success &&
file_res != api_error::item_not_found) { file_res != api_error::item_not_found) {
// TODO raise error // TODO raise error
@ -671,8 +684,10 @@ auto encrypt_provider::process_directory_entry(
} }
std::string api_parent{}; std::string api_parent{};
auto parent_res = db_->get_directory_api_path( auto parent_res{
utils::path::get_parent_path(dir_entry.get_path()), api_parent); db_->get_directory_api_path(
utils::path::get_parent_path(dir_entry.get_path()), api_parent),
};
if (parent_res != api_error::success && if (parent_res != api_error::success &&
parent_res != api_error::directory_not_found) { parent_res != api_error::directory_not_found) {
// TODO raise error // TODO raise error
@ -727,27 +742,28 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path,
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
i_file_db::file_data file_data{}; i_file_db::file_data file_data{};
auto result = db_->get_file_data(api_path, file_data); auto result{db_->get_file_data(api_path, file_data)};
if (result != api_error::success) { if (result != api_error::success) {
return result; return result;
} }
auto opt_size = utils::file::file{file_data.source_path}.size(); auto opt_size{utils::file::file{file_data.source_path}.size()};
if (not opt_size.has_value()) { if (not opt_size.has_value()) {
return api_error::os_error; return api_error::os_error;
} }
auto file_size{opt_size.value()}; auto file_size{opt_size.value()};
const auto &cfg = get_encrypt_config(); const auto &cfg{get_encrypt_config()};
unique_recur_mutex_lock reader_lookup_lock(reader_lookup_mtx_); unique_recur_mutex_lock reader_lookup_lock(reader_lookup_mtx_);
if (file_data.file_size != file_size) { if (file_data.file_size != file_size) {
auto relative_path = auto relative_path{
utils::path::get_relative_path(file_data.source_path, cfg.path); utils::path::get_relative_path(file_data.source_path, cfg.path),
};
auto info = std::make_shared<reader_info>(); auto info{std::make_shared<reader_info>()};
info->reader = std::make_unique<utils::encryption::encrypting_reader>( info->reader = std::make_unique<utils::encryption::encrypting_reader>(
relative_path, file_data.source_path, stop_requested, relative_path, file_data.source_path, stop_requested,
cfg.encryption_token, utils::path::get_parent_path(relative_path)); cfg.encryption_token, utils::path::get_parent_path(relative_path));
@ -765,7 +781,7 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path,
} }
} else if (reader_lookup_.find(file_data.source_path) == } else if (reader_lookup_.find(file_data.source_path) ==
reader_lookup_.end()) { reader_lookup_.end()) {
auto info = std::make_shared<reader_info>(); auto info{std::make_shared<reader_info>()};
info->reader = std::make_unique<utils::encryption::encrypting_reader>( info->reader = std::make_unique<utils::encryption::encrypting_reader>(
api_path, file_data.source_path, stop_requested, cfg.encryption_token, api_path, file_data.source_path, stop_requested, cfg.encryption_token,
std::move(file_data.iv_list)); std::move(file_data.iv_list));
@ -776,7 +792,7 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path,
return api_error::success; return api_error::success;
} }
auto info = reader_lookup_.at(file_data.source_path); auto info{reader_lookup_.at(file_data.source_path)};
info->last_access_time = std::chrono::system_clock::now(); info->last_access_time = std::chrono::system_clock::now();
reader_lookup_lock.unlock(); reader_lookup_lock.unlock();
@ -784,14 +800,11 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path,
info->reader->set_read_position(offset); info->reader->set_read_position(offset);
data.resize(size); data.resize(size);
auto res = auto res{
info->reader->reader_function(reinterpret_cast<char *>(data.data()), 1U, info->reader->reader_function(reinterpret_cast<char *>(data.data()), 1U,
data.size(), info->reader.get()); data.size(), info->reader.get()),
if (res == 0) { };
return api_error::os_error; return res == 0 ? api_error::os_error : api_error::success;
}
return api_error::success;
} }
void encrypt_provider::remove_deleted_files(const stop_type &stop_requested) { void encrypt_provider::remove_deleted_files(const stop_type &stop_requested) {
@ -813,7 +826,7 @@ void encrypt_provider::remove_deleted_files(const stop_type &stop_requested) {
} }
// TODO handle error // TODO handle error
auto del_res = db_->remove_item(item.api_path); auto del_res{db_->remove_item(item.api_path)};
if (item.directory) { if (item.directory) {
event_system::instance().raise<directory_removed_externally>( event_system::instance().raise<directory_removed_externally>(
item.api_path, item.source_path); item.api_path, item.source_path);
@ -836,16 +849,16 @@ auto encrypt_provider::start(api_item_added_callback /*api_item_added*/,
db_ = create_file_db(config_); db_ = create_file_db(config_);
std::string source_path; std::string source_path;
auto result = db_->get_directory_source_path("/", source_path); auto result{db_->get_directory_source_path("/", source_path)};
if (result != api_error::success && if (result != api_error::success &&
result != api_error::directory_not_found) { result != api_error::directory_not_found) {
throw startup_exception( throw startup_exception(
fmt::format("failed to get root|{}", api_error_to_string(result))); fmt::format("failed to get root|{}", api_error_to_string(result)));
} }
auto cfg_path = utils::path::absolute(get_encrypt_config().path); auto cfg_path{utils::path::absolute(get_encrypt_config().path)};
if (result == api_error::success) { if (result == api_error::success) {
auto cur_path = utils::path::absolute(source_path); auto cur_path{utils::path::absolute(source_path)};
#if defined(_WIN32) #if defined(_WIN32)
if (utils::string::to_lower(cur_path) != if (utils::string::to_lower(cur_path) !=
utils::string::to_lower(cfg_path)) { utils::string::to_lower(cfg_path)) {