diff --git a/repertory/librepertory/include/file_manager/events.hpp b/repertory/librepertory/include/file_manager/events.hpp index 18059695..13bd3389 100644 --- a/repertory/librepertory/include/file_manager/events.hpp +++ b/repertory/librepertory/include/file_manager/events.hpp @@ -72,27 +72,22 @@ E_SIMPLE3(download_restore_failed, error, true, std::string, error, err, E_STRING ); -E_SIMPLE2(download_resumed, info, true, - std::string, api_path, ap, E_STRING, - std::string, dest_path, dest, E_STRING -); - -E_SIMPLE2(download_stored, info, true, - std::string, api_path, ap, E_STRING, - std::string, dest_path, dest, E_STRING -); - -E_SIMPLE2(download_stored_removed, info, true, - std::string, api_path, ap, E_STRING, - std::string, dest_path, dest, E_STRING -); - -E_SIMPLE3(download_stored_failed, error, true, +E_SIMPLE3(download_resume_add_failed, error, true, std::string, api_path, ap, E_STRING, std::string, dest_path, dest, E_STRING, std::string, error, err, E_STRING ); +E_SIMPLE2(download_resume_added, info, true, + std::string, api_path, ap, E_STRING, + std::string, dest_path, dest, E_STRING +); + +E_SIMPLE2(download_resume_removed, info, true, + std::string, api_path, ap, E_STRING, + std::string, dest_path, dest, E_STRING +); + E_SIMPLE1(item_timeout, debug, true, std::string, api_path, ap, E_STRING ); diff --git a/repertory/librepertory/src/file_manager/file_manager.cpp b/repertory/librepertory/src/file_manager/file_manager.cpp index 3adbee12..5985f0a0 100644 --- a/repertory/librepertory/src/file_manager/file_manager.cpp +++ b/repertory/librepertory/src/file_manager/file_manager.cpp @@ -380,33 +380,43 @@ auto file_manager::get_stored_downloads() const -> std::vector { auto file_manager::handle_file_rename(const std::string &from_api_path, const std::string &to_api_path) -> api_error { + auto should_upload{false}; std::string source_path{}; - bool should_upload{upload_lookup_.contains(from_api_path)}; - if (should_upload) { - source_path = upload_lookup_.at(from_api_path)->get_source_path(); - remove_upload(from_api_path); - } else { - auto result = db::db_select{*db_.get(), upload_table} - .column("source_path") - .where("api_path") - .equals(from_api_path) - .go(); - std::optional row; - should_upload = result.get_row(row) && row.has_value(); + auto file_iter = open_file_lookup_.find(from_api_path); + if (file_iter != open_file_lookup_.end()) { + should_upload = file_iter->second->is_modified(); + source_path = file_iter->second->get_source_path(); + } + + if (not should_upload) { + should_upload = upload_lookup_.contains(from_api_path); if (should_upload) { - source_path = row->get_column("source_path").get_value(); - remove_upload(from_api_path); + source_path = upload_lookup_.at(from_api_path)->get_source_path(); + } else { + auto result = db::db_select{*db_.get(), upload_table} + .column("source_path") + .where("api_path") + .equals(from_api_path) + .go(); + std::optional row; + should_upload = result.get_row(row) && row.has_value(); + if (should_upload) { + source_path = row->get_column("source_path").get_value(); + } } } + if (should_upload) { + remove_upload(from_api_path); + } + auto ret = provider_.rename_file(from_api_path, to_api_path); if (ret == api_error::success) { swap_renamed_items(from_api_path, to_api_path); - if (should_upload) { - queue_upload(to_api_path, source_path, false); - } - } else if (should_upload) { - queue_upload(from_api_path, source_path, false); + } + + if (should_upload) { + queue_upload(to_api_path, source_path, false); } return ret; @@ -537,11 +547,6 @@ auto file_manager::remove_file(const std::string &api_path) -> api_error { }; recur_mutex_lock open_lock(open_file_mtx_); - auto file_iter = open_file_lookup_.find(api_path); - if (file_iter != open_file_lookup_.end() && - file_iter->second->is_modified()) { - return api_error::file_in_use; - } filesystem_item fsi{}; auto res = provider_.get_filesystem_item(api_path, false, fsi); @@ -561,7 +566,6 @@ auto file_manager::remove_file(const std::string &api_path) -> api_error { utils::error::raise_api_path_error( function_name, fsi.api_path, fsi.source_path, utils::get_last_error_code(), "failed to delete source"); - return api_error::success; } return api_error::success; @@ -575,7 +579,7 @@ void file_manager::remove_resume(const std::string &api_path, .equals(api_path) .go(); if (result.ok()) { - event_system::instance().raise(api_path, + event_system::instance().raise(api_path, source_path); } } @@ -760,32 +764,6 @@ auto file_manager::rename_file(const std::string &from_api_path, return api_error::item_exists; } - // Don't rename if destination file has open handles - if (get_open_file_count(to_api_path) != 0U) { - return api_error::file_in_use; - } - - // Handle destination file exists (should overwrite) - if (dest_exists) { - filesystem_item fsi{}; - res = provider_.get_filesystem_item(to_api_path, false, fsi); - if (res != api_error::success) { - return res; - } - - res = remove_file(to_api_path); - if ((res == api_error::success) || (res == api_error::item_not_found)) { - if (not utils::file::file(fsi.source_path).remove()) { - utils::error::raise_api_path_error( - function_name, fsi.api_path, fsi.source_path, - utils::get_last_error_code(), "failed to delete source path"); - } - return handle_file_rename(from_api_path, to_api_path); - } - - return res; - } - // Check destination parent directory exists res = provider_.is_directory(utils::path::get_parent_api_path(to_api_path), exists); @@ -796,6 +774,14 @@ auto file_manager::rename_file(const std::string &from_api_path, return api_error::directory_not_found; } + // Handle destination file exists (should overwrite) + if (dest_exists) { + res = remove_file(to_api_path); + if ((res != api_error::success) && (res != api_error::item_not_found)) { + return res; + } + } + return handle_file_rename(from_api_path, to_api_path); } @@ -966,12 +952,12 @@ void file_manager::store_resume(const i_open_file &file) { .column_value("data", create_resume_entry(file).dump()) .go(); if (result.ok()) { - event_system::instance().raise(file.get_api_path(), - file.get_source_path()); + event_system::instance().raise( + file.get_api_path(), file.get_source_path()); return; } - event_system::instance().raise( + event_system::instance().raise( file.get_api_path(), file.get_source_path(), "failed to insert|" + std::to_string(result.get_error()) + '|' + result.get_error_str());