diff --git a/repertory/librepertory/include/utils/file_utils.hpp b/repertory/librepertory/include/utils/file_utils.hpp index 748a43ba..bf3c5ec5 100644 --- a/repertory/librepertory/include/utils/file_utils.hpp +++ b/repertory/librepertory/include/utils/file_utils.hpp @@ -26,10 +26,6 @@ #include "utils/file.hpp" namespace repertory::utils::file { -// Prototypes -[[nodiscard]] auto calculate_used_space(std::string path, - bool recursive) -> std::uint64_t; - void change_to_process_directory(); [[nodiscard]] auto copy_directory_recursively(std::string from_path, @@ -66,10 +62,6 @@ is_modified_date_older_than(const std::string &path, read_file_lines(const std::string &path) -> std::vector; [[nodiscard]] auto reset_modified_time(const std::string &path) -> bool; - -[[nodiscard]] auto retry_delete_directory(const std::string &dir) -> bool; - -[[nodiscard]] auto retry_delete_file(const std::string &file) -> bool; } // namespace repertory::utils::file #endif // INCLUDE_UTILS_FILE_UTILS_HPP_ diff --git a/repertory/librepertory/src/drives/eviction.cpp b/repertory/librepertory/src/drives/eviction.cpp index e152d270..05f79240 100644 --- a/repertory/librepertory/src/drives/eviction.cpp +++ b/repertory/librepertory/src/drives/eviction.cpp @@ -92,7 +92,7 @@ void eviction::service_function() { // Handle maximum cache size eviction auto used_bytes = - utils::file::calculate_used_space(config_.get_cache_directory(), false); + utils::file::directory{config_.get_cache_directory()}.size(); if (config_.get_enable_max_cache_size()) { should_evict = (used_bytes > config_.get_max_cache_size_bytes()); } diff --git a/repertory/librepertory/src/file_manager/file_manager.cpp b/repertory/librepertory/src/file_manager/file_manager.cpp index 67d21421..45c0ee5c 100644 --- a/repertory/librepertory/src/file_manager/file_manager.cpp +++ b/repertory/librepertory/src/file_manager/file_manager.cpp @@ -244,7 +244,7 @@ auto file_manager::evict_file(const std::string &api_path) -> bool { open_file_lookup_.erase(api_path); - auto removed = utils::file::retry_delete_file(source_path); + auto removed = utils::file::file(source_path).remove(); if (removed) { event_system::instance().raise(api_path, source_path); @@ -513,8 +513,8 @@ void file_manager::queue_upload(const std::string &api_path, db::db_insert{*db_.get(), upload_table} .or_replace() .column_value("api_path", api_path) - .column_value("date_time", static_cast( - utils::time::get_time_now())) + .column_value("date_time", + static_cast(utils::time::get_time_now())) .column_value("source_path", source_path) .go(); if (result.ok()) { @@ -557,7 +557,7 @@ auto file_manager::remove_file(const std::string &api_path) -> api_error { return res; } - if (not utils::file::retry_delete_file(fsi.source_path)) { + 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"); @@ -780,7 +780,7 @@ auto file_manager::rename_file(const std::string &from_api_path, res = remove_file(to_api_path); if ((res == api_error::success) || (res == api_error::item_not_found)) { - if (not utils::file::retry_delete_file(fsi.source_path)) { + 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"); diff --git a/repertory/librepertory/src/file_manager/file_manager_open_file.cpp b/repertory/librepertory/src/file_manager/file_manager_open_file.cpp index 9690265c..da2dfe45 100644 --- a/repertory/librepertory/src/file_manager/file_manager_open_file.cpp +++ b/repertory/librepertory/src/file_manager/file_manager_open_file.cpp @@ -458,7 +458,7 @@ auto file_manager::open_file::close() -> bool { mgr_.store_resume(*this); } else if (get_api_error() != api_error::success) { mgr_.remove_resume(get_api_path(), get_source_path()); - if (not utils::file::retry_delete_file(fsi_.source_path)) { + if (not utils::file::file(fsi_.source_path).remove()) { utils::error::raise_api_path_error( function_name, get_api_path(), fsi_.source_path, utils::get_last_error_code(), "failed to delete file"); diff --git a/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp b/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp index 8cb91fa4..5f925a6d 100644 --- a/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp +++ b/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp @@ -93,7 +93,7 @@ file_manager::ring_buffer_open_file::~ring_buffer_open_file() { close(); nf_->close(); - if (not utils::file::retry_delete_file(fsi_.source_path)) { + 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 file"); diff --git a/repertory/librepertory/src/rpc/server/full_server.cpp b/repertory/librepertory/src/rpc/server/full_server.cpp index 7e794ae9..e09d27b6 100644 --- a/repertory/librepertory/src/rpc/server/full_server.cpp +++ b/repertory/librepertory/src/rpc/server/full_server.cpp @@ -22,12 +22,11 @@ #include "rpc/server/full_server.hpp" #include "app_config.hpp" -#include "drives/directory_iterator.hpp" #include "file_manager/i_file_manager.hpp" #include "providers/i_provider.hpp" #include "types/repertory.hpp" #include "types/rpc.hpp" -#include "utils/file_utils.hpp" +#include "utils/file.hpp" #include "utils/path.hpp" namespace repertory { @@ -51,11 +50,11 @@ void full_server::handle_get_directory_items(const httplib::Request &req, void full_server::handle_get_drive_information(const httplib::Request & /*req*/, httplib::Response &res) { + auto dir_size = + utils::file::directory(get_config().get_cache_directory()).size(); res.set_content( json({ - {"cache_space_used", - utils::file::calculate_used_space( - get_config().get_cache_directory(), false)}, + {"cache_space_used", dir_size}, {"drive_space_total", provider_.get_total_drive_space()}, {"drive_space_used", provider_.get_used_drive_space()}, {"item_count", provider_.get_total_item_count()}, diff --git a/repertory/librepertory/src/utils/file_utils.cpp b/repertory/librepertory/src/utils/file_utils.cpp index 1240792c..ee293ebb 100644 --- a/repertory/librepertory/src/utils/file_utils.cpp +++ b/repertory/librepertory/src/utils/file_utils.cpp @@ -30,56 +30,6 @@ #include "utils/utils.hpp" namespace repertory::utils::file { -auto calculate_used_space(std::string path, bool recursive) -> std::uint64_t { - path = utils::path::absolute(path); - std::uint64_t ret{}; -#if defined(_WIN32) - WIN32_FIND_DATA fd{}; - const auto search = utils::path::combine(path, {"*.*"}); - auto find = ::FindFirstFile(search.c_str(), &fd); - if (find != INVALID_HANDLE_VALUE) { - do { - const auto file_name = std::string(fd.cFileName); - if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - if (recursive && (file_name != ".") && (file_name != "..")) { - ret += calculate_used_space(utils::path::combine(path, {file_name}), - recursive); - } - } else { - std::uint64_t file_size{}; - if (get_file_size(utils::path::combine(path, {file_name}), file_size)) { - ret += file_size; - } - } - } while (::FindNextFile(find, &fd) != 0); - ::FindClose(find); - } -#else - auto *root = opendir(path.c_str()); - if (root) { - struct dirent *de{}; - while ((de = readdir(root)) != nullptr) { - if (de->d_type == DT_DIR) { - if (recursive && (strcmp(de->d_name, ".") != 0) && - (strcmp(de->d_name, "..") != 0)) { - ret += calculate_used_space(utils::path::combine(path, {de->d_name}), - recursive); - } - } else { - std::uint64_t file_size{}; - if (get_file_size(utils::path::combine(path, {de->d_name}), - file_size)) { - ret += file_size; - } - } - } - closedir(root); - } -#endif - - return ret; -} - void change_to_process_directory() { #if defined(_WIN32) std::string file_name; @@ -459,24 +409,4 @@ auto reset_modified_time(const std::string &path) -> bool { #endif return ret; } - -auto retry_delete_directory(const std::string &dir) -> bool { - auto deleted = false; - for (std::uint8_t i = 0U; - not(deleted = directory(dir).remove()) && (i < 200U); i++) { - std::this_thread::sleep_for(10ms); - } - - return deleted; -} - -auto retry_delete_file(const std::string &file) -> bool { - auto deleted = false; - for (std::uint8_t i = 0U; - not(deleted = utils::file::file{file}.remove()) && (i < 200U); i++) { - std::this_thread::sleep_for(10ms); - } - - return deleted; -} } // namespace repertory::utils::file diff --git a/repertory/repertory/main.cpp b/repertory/repertory/main.cpp index 074ae8e7..791b9eea 100644 --- a/repertory/repertory/main.cpp +++ b/repertory/repertory/main.cpp @@ -21,7 +21,7 @@ */ #if defined(PROJECT_ENABLE_BACKWARD_CPP) #include "backward.hpp" -#endif +#endif // defined(PROJECT_ENABLE_BACKWARD_CPP) #include "cli/actions.hpp" #include "initialize.hpp" @@ -34,7 +34,7 @@ using namespace repertory; auto main(int argc, char **argv) -> int { #if defined(PROJECT_ENABLE_BACKWARD_CPP) static backward::SignalHandling sh; -#endif +#endif // defined(PROJECT_ENABLE_BACKWARD_CPP) if (not repertory::project_initialize()) { std::cerr << "fatal: failed to initialize repertory" << std::endl; diff --git a/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp b/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp index 71cda2f4..63a1ee8d 100644 --- a/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp +++ b/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp @@ -132,7 +132,7 @@ public: utils::path::combine(mount_location_, {to_api_path}); if (overwrite) { - if (not utils::file::retry_delete_file(to_file_path)) { + if (not utils::file::file(to_file_path).remove()) { return -1; } } else if (utils::file::directory(to_file_path).exists()) || diff --git a/repertory/repertory_test/src/fuse_drive_test.cpp b/repertory/repertory_test/src/fuse_drive_test.cpp index 1767b0d6..ed79c146 100644 --- a/repertory/repertory_test/src/fuse_drive_test.cpp +++ b/repertory/repertory_test/src/fuse_drive_test.cpp @@ -431,8 +431,8 @@ namespace repertory { // // utils::path::combine(mount_location, {"to_rename_file_test"}); // // test_rename_file(file_path, to_file_path, // // provider_ptr->is_rename_supported()); -// // EXPECT_TRUE(utils::file::retry_delete_file(file_path)); -// // EXPECT_TRUE(utils::file::retry_delete_file(to_file_path)); +// // EXPECT_TRUE(utils::file::file(file_path).remove()); +// // EXPECT_TRUE(utils::file::file(to_file_path).remove()); // // // // file_path = // // utils::path::combine(mount_location, diff --git a/repertory/repertory_test/src/remote_fuse_test.cpp b/repertory/repertory_test/src/remote_fuse_test.cpp index 529517c1..84c3513c 100644 --- a/repertory/repertory_test/src/remote_fuse_test.cpp +++ b/repertory/repertory_test/src/remote_fuse_test.cpp @@ -52,7 +52,7 @@ static std::string fuse_remote_dir = static void access_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"access.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -64,13 +64,13 @@ static void access_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_access(api_path.c_str(), 0)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void chflags_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"chflags.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -86,13 +86,13 @@ static void chflags_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void chmod_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"chmod.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -108,13 +108,13 @@ static void chmod_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void chown_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"chown.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -134,7 +134,7 @@ static void chown_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void @@ -143,7 +143,7 @@ create_and_release_test(repertory::remote_fuse::remote_client &client, const auto test_file = utils::path::combine(fuse_remote_dir, {"create_and_release.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -156,7 +156,7 @@ create_and_release_test(repertory::remote_fuse::remote_client &client, EXPECT_EQ(0u, server.get_open_file_count(test_file)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void destroy_test(repertory::remote_fuse::remote_client &client) { @@ -167,7 +167,7 @@ static void destroy_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"fallocate.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -181,14 +181,14 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { EXPECT_EQ(100, file_size); } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); }*/ static void fgetattr_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"fgetattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -228,14 +228,14 @@ static void fgetattr_test(repertory::remote_fuse::remote_client &client) { st.st_birthtimespec / NANOS_PER_SECOND); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void fsetattr_x_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"fsetattr_x.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -253,13 +253,13 @@ static void fsetattr_x_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void fsync_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"fsync.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -271,14 +271,14 @@ static void fsync_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void ftruncate_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"ftruncate.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -294,13 +294,13 @@ static void ftruncate_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(100U, opt_size.value()); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void getattr_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"getattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -339,14 +339,14 @@ static void getattr_test(repertory::remote_fuse::remote_client &client) { st.st_birthtimespec / NANOS_PER_SECOND); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } /*static void getxattr_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"getxattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -359,14 +359,14 @@ nullptr, 0)); #else EXPECT_EQ(-EACCES, client.fuse_getxattr(api_path.c_str(), "test", nullptr, 0)); #endif } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); } static void getxattr_osx_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"getxattr_osx.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -377,14 +377,14 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { client.fuse_getxattrOSX(api_path.c_str(), "test", nullptr, 0, 0)); } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); }*/ static void getxtimes_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"getxtimes.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -403,7 +403,7 @@ static void getxtimes_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void init_test(repertory::remote_fuse::remote_client &client) { @@ -414,7 +414,7 @@ static void init_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"listxattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -427,7 +427,7 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { #endif } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); }*/ static void mkdir_test(repertory::remote_fuse::remote_client &client) { @@ -449,7 +449,7 @@ static void mkdir_test(repertory::remote_fuse::remote_client &client) { static void open_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"open.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; #if defined(_WIN32) @@ -471,7 +471,7 @@ static void open_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle2)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void @@ -499,7 +499,7 @@ static void read_and_write_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"read_and_write.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -517,7 +517,7 @@ static void read_and_write_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void @@ -525,7 +525,7 @@ read_and_write_base64_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"read_and_write_base64.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -544,7 +544,7 @@ read_and_write_base64_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void readdir_test(repertory::remote_fuse::remote_client &client) { @@ -579,7 +579,7 @@ static void readdir_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"removexattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -592,7 +592,7 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { "test")); #endif } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); }*/ static void rename_test(repertory::remote_fuse::remote_client &client) { @@ -602,8 +602,8 @@ static void rename_test(repertory::remote_fuse::remote_client &client) { const auto api_path = test_file.substr(mount_location_.size()); const auto renamed_api_path = renamed_test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(renamed_test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); + EXPECT_TRUE(utils::file::file(renamed_test_file).remove()); remote::file_handle handle; #if defined(_WIN32) @@ -625,8 +625,8 @@ static void rename_test(repertory::remote_fuse::remote_client &client) { EXPECT_TRUE(utils::file::file(renamed_test_file).exists()); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(renamed_test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); + EXPECT_TRUE(utils::file::file(renamed_test_file).remove()); } static void rmdir_test(repertory::remote_fuse::remote_client &client) { @@ -652,7 +652,7 @@ static void setattr_x_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setattr_x.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -670,14 +670,14 @@ static void setattr_x_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void setbkuptime_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setbkuptime.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -695,14 +695,14 @@ static void setbkuptime_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void setchgtime_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setchgtime.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -720,14 +720,14 @@ static void setchgtime_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void setcrtime_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setcrtime.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -745,7 +745,7 @@ static void setcrtime_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void setvolname_test(repertory::remote_fuse::remote_client &client) { @@ -756,7 +756,7 @@ static void setvolname_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setxattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -771,14 +771,14 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { 5, 0)); #endif } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); } static void setxattr_osx_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setxattr_osx.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -791,7 +791,7 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { 0)); } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); }*/ #if defined(_WIN32) @@ -853,7 +853,7 @@ static void truncate_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"truncate.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; #if defined(_WIN32) @@ -876,13 +876,13 @@ static void truncate_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(100U, opt_size.value()); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void unlink_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"unlink.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -895,13 +895,13 @@ static void unlink_test(repertory::remote_fuse::remote_client &client) { EXPECT_FALSE(utils::file::file(test_file).exists()); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void utimens_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"utimens.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -915,7 +915,7 @@ static void utimens_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_utimens(api_path.c_str(), tv, 0, 0)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } TEST(remote_fuse, all_tests) { diff --git a/repertory/repertory_test/src/remote_winfsp_test.cpp b/repertory/repertory_test/src/remote_winfsp_test.cpp index 5e0ec63b..0ce6ff52 100644 --- a/repertory/repertory_test/src/remote_winfsp_test.cpp +++ b/repertory/repertory_test/src/remote_winfsp_test.cpp @@ -45,7 +45,7 @@ static std::string win_remote_dir = static void can_delete_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"candelete.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -58,14 +58,14 @@ static void can_delete_test(remote_client &client) { api_path.data())); nf.close(); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } } template static void create_and_close_test(remote_client &client, t &server) { const auto test_file = utils::path::combine(win_remote_dir, {"create.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -83,12 +83,12 @@ static void create_and_close_test(remote_client &client, t &server) { EXPECT_EQ(0u, client.get_open_file_count(utils::string::to_utf8(api_path))); EXPECT_EQ(0u, server.get_open_file_count(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void cleanup_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"cleanup.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -107,12 +107,12 @@ static void cleanup_test(remote_client &client) { EXPECT_FALSE(was_closed); EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void flush_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"flush.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -129,13 +129,13 @@ static void flush_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void get_file_info_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"get_file_info.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -152,13 +152,13 @@ static void get_file_info_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void get_security_by_name_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"get_security_by_name.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -181,7 +181,7 @@ static void get_security_by_name_test(remote_client &client) { EXPECT_EQ(static_cast(FILE_ATTRIBUTE_NORMAL), attributes); EXPECT_FALSE(str_descriptor.empty()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void get_volume_info_test(remote_client &client) { @@ -202,7 +202,7 @@ static void mounted_test(remote_client &client) { static void open_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"open.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -229,13 +229,13 @@ static void open_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc2)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void overwrite_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"overwrite.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -259,7 +259,7 @@ static void overwrite_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void create_and_read_directory_test(remote_client &client) { @@ -329,7 +329,7 @@ static void open_and_read_directory_test(remote_client &client) { static void read_and_write_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"read_and_write.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -365,14 +365,14 @@ static void read_and_write_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void rename_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"rename.txt"}); const auto test_file2 = utils::path::combine(win_remote_dir, {"rename2.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file2)); + EXPECT_TRUE(utils::file::file(test_file).remove()); + EXPECT_TRUE(utils::file::file(test_file2).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); auto api_path2 = @@ -394,15 +394,15 @@ static void rename_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file2)); + EXPECT_TRUE(utils::file::file(test_file).remove()); + EXPECT_TRUE(utils::file::file(test_file2).remove()); } static void set_basic_info_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"set_basic_info.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -447,13 +447,13 @@ static void set_basic_info_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void set_file_size_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"set_file_size.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -478,7 +478,7 @@ static void set_file_size_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void unmounted_test(remote_client &client) { diff --git a/repertory/repertory_test/src/winfsp_test.cpp b/repertory/repertory_test/src/winfsp_test.cpp index 8a52abd2..5d5a91c1 100644 --- a/repertory/repertory_test/src/winfsp_test.cpp +++ b/repertory/repertory_test/src/winfsp_test.cpp @@ -113,7 +113,7 @@ static auto create_test(winfsp_test *test, const std::string &mount_point) { static void delete_file_test(const std::string &file) { TEST_HEADER(__FUNCTION__); event_capture ec({"file_removed"}); - EXPECT_TRUE(utils::file::retry_delete_file(file)); + EXPECT_TRUE(utils::file::file(file).remove()); EXPECT_FALSE(utils::file::file(file).exists()); } diff --git a/support/include/utils/common.hpp b/support/include/utils/common.hpp index 952e426e..1b81fd43 100644 --- a/support/include/utils/common.hpp +++ b/support/include/utils/common.hpp @@ -33,6 +33,8 @@ struct result final { [[nodiscard]] operator bool() const { return ok; } }; +using retryable_action_t = std::function; + [[nodiscard]] inline constexpr auto calculate_read_size(std::uint64_t total_size, std::size_t read_size, std::uint64_t offset) -> std::size_t { @@ -90,6 +92,11 @@ get_next_available_port(std::uint16_t first_port, std::uint16_t &available_port) -> bool; #endif // defined(PROJECT_ENABLE_BOOST) +[[nodiscard]] auto retry_action(retryable_action_t action, + std::size_t retry_count = 200U, + std::chrono::milliseconds retry_wait = + std::chrono::milliseconds(10)) -> bool; + template inline constexpr auto divide_with_ceiling(result_t numerator, data_t denominator) -> result_t { diff --git a/support/src/utils/common.cpp b/support/src/utils/common.cpp index c548d446..dc3648a9 100644 --- a/support/src/utils/common.cpp +++ b/support/src/utils/common.cpp @@ -21,6 +21,7 @@ */ #include "utils/common.hpp" +#include "utils/error.hpp" #include "utils/string.hpp" namespace repertory::utils { @@ -153,4 +154,27 @@ auto get_next_available_port(std::uint16_t first_port, return not error_code; } #endif // defined(PROJECT_ENABLE_BOOST) + +auto retry_action(retryable_action_t action, std::size_t retry_count, + std::chrono::milliseconds retry_wait) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + for (std::size_t idx = 0U; idx < retry_count; ++idx) { + if (action()) { + return true; + } + + std::this_thread::sleep_for(retry_wait); + } + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} } // namespace repertory::utils diff --git a/support/src/utils/file_directory.cpp b/support/src/utils/file_directory.cpp index 2f0873a7..61ac0508 100644 --- a/support/src/utils/file_directory.cpp +++ b/support/src/utils/file_directory.cpp @@ -21,6 +21,7 @@ */ #include "utils/file.hpp" +#include "utils/common.hpp" #include "utils/error.hpp" #include "utils/unix.hpp" #include "utils/windows.hpp" @@ -329,19 +330,21 @@ auto directory::remove() -> bool { static_cast(__FUNCTION__), }; - try { + return utils::retry_action([this]() -> bool { + try { #if defined(_WIN32) - return (not exists() || ::RemoveDirectoryA(path_.c_str())); + return (not exists() || ::RemoveDirectoryA(path_.c_str())); #else // !defined(_WIN32) - return not exists() || (rmdir(path_.c_str()) == 0); + return not exists() || (rmdir(path_.c_str()) == 0); #endif // defined(_WIN32) - } catch (const std::exception &e) { - utils::error::handle_exception(function_name, e); - } catch (...) { - utils::error::handle_exception(function_name); - } + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } - return false; + return false; + }); } auto directory::remove_recursively() -> bool { diff --git a/support/src/utils/file_file.cpp b/support/src/utils/file_file.cpp index c1cd7f92..b6662abf 100644 --- a/support/src/utils/file_file.cpp +++ b/support/src/utils/file_file.cpp @@ -21,6 +21,7 @@ */ #include "utils/file.hpp" +#include "utils/common.hpp" #include "utils/encryption.hpp" #include "utils/error.hpp" #include "utils/path.hpp" @@ -339,12 +340,14 @@ auto file::remove() -> bool { return true; } + return utils::retry_action([this]() -> bool { #if defined(_WIN32) - return !!::DeleteFileA(path_.c_str()); + return !!::DeleteFileA(path_.c_str()); #else // !defined(_WIN32) - std::error_code ec{}; - return std::filesystem::remove(path_, ec); + std::error_code ec{}; + return std::filesystem::remove(path_, ec); #endif // defined(_WIN32) + }); } auto file::truncate(std::size_t size) -> bool { diff --git a/support/src/utils/file_smb_directory.cpp b/support/src/utils/file_smb_directory.cpp index 53e0ee73..d5b74966 100644 --- a/support/src/utils/file_smb_directory.cpp +++ b/support/src/utils/file_smb_directory.cpp @@ -21,6 +21,7 @@ */ #include "utils/file.hpp" +#include "utils/common.hpp" #include "utils/error.hpp" #if defined(PROJECT_ENABLE_LIBDSM) @@ -490,14 +491,24 @@ auto smb_directory::remove() -> bool { throw std::runtime_error("session not found|" + path_); } - 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)); - } + return utils::retry_action([this]() -> bool { + try { + 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)); + } - return true; + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; + }); } catch (const std::exception &e) { utils::error::handle_exception(function_name, e); } catch (...) { diff --git a/support/src/utils/file_smb_file.cpp b/support/src/utils/file_smb_file.cpp index 671e6685..1c49e563 100644 --- a/support/src/utils/file_smb_file.cpp +++ b/support/src/utils/file_smb_file.cpp @@ -21,6 +21,7 @@ */ #include "utils/file.hpp" +#include "utils/common.hpp" #include "utils/error.hpp" #include "utils/string.hpp" @@ -270,32 +271,34 @@ auto smb_file::remove() -> bool { static_cast(__FUNCTION__), }; - try { - close(); + return utils::retry_action([this]() -> bool { + try { + close(); - 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)); + 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)); + } + + 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()))); + } + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_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()))); - } - - return true; - } catch (const std::exception &e) { - utils::error::handle_exception(function_name, e); - } catch (...) { - utils::error::handle_exception(function_name); - } - - return false; + return false; + }); } auto smb_file::size() const -> std::optional {