From dc48b84191650ed687782fa948cce3cdea3a2a6e Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 18 Nov 2023 19:07:53 -0600 Subject: [PATCH] fix truncate --- src/drives/fuse/fuse_drive.cpp | 52 ++++++++++--------- src/file_manager/file_manager.cpp | 13 +++-- .../file_manager_open_file_base.cpp | 2 + tests/fuse_drive_test.cpp | 9 ++-- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/drives/fuse/fuse_drive.cpp b/src/drives/fuse/fuse_drive.cpp index 64190c83..cd827c38 100644 --- a/src/drives/fuse/fuse_drive.cpp +++ b/src/drives/fuse/fuse_drive.cpp @@ -159,29 +159,32 @@ auto fuse_drive::create_impl(std::string api_path, mode_t mode, } std::uint64_t handle{}; - std::shared_ptr open_file; - if (is_create_op) { - const auto now = utils::get_file_time_now(); + { + std::shared_ptr open_file; + if (is_create_op) { + const auto now = utils::get_file_time_now(); #ifdef __APPLE__ - const auto osx_flags = static_cast(file_info->flags); + const auto osx_flags = static_cast(file_info->flags); #else - const auto osx_flags = 0U; + const auto osx_flags = 0U; #endif - auto meta = create_meta_attributes( - now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now, now, - is_directory_op, get_effective_gid(), "", mode, now, 0U, osx_flags, 0U, - utils::path::combine(config_.get_cache_directory(), - {utils::create_uuid_string()}), - get_effective_uid(), now); + auto meta = create_meta_attributes( + now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now, now, + is_directory_op, get_effective_gid(), "", mode, now, 0U, osx_flags, + 0U, + utils::path::combine(config_.get_cache_directory(), + {utils::create_uuid_string()}), + get_effective_uid(), now); - res = fm_->create(api_path, meta, file_info->flags, handle, open_file); - if ((res != api_error::item_exists) && (res != api_error::success)) { + res = fm_->create(api_path, meta, file_info->flags, handle, open_file); + if ((res != api_error::item_exists) && (res != api_error::success)) { + return res; + } + } else if (((res = fm_->open(api_path, is_directory_op, file_info->flags, + handle, open_file)) != api_error::success)) { return res; } - } else if (((res = fm_->open(api_path, is_directory_op, file_info->flags, - handle, open_file)) != api_error::success)) { - return res; } file_info->fh = handle; @@ -1242,17 +1245,18 @@ auto fuse_drive::truncate_impl(std::string api_path, off_t size) -> api_error { return res; } - open_file_data ofd{O_RDWR}; std::uint64_t handle{}; - std::shared_ptr open_file; - if ((res = fm_->open(api_path, false, ofd, handle, open_file)) != - api_error::success) { - return res; + { + open_file_data ofd{O_RDWR}; + std::shared_ptr open_file; + if ((res = fm_->open(api_path, false, ofd, handle, open_file)) != + api_error::success) { + return res; + } + + res = open_file->resize(static_cast(size)); } - - res = open_file->resize(static_cast(size)); fm_->close(handle); - return res; } diff --git a/src/file_manager/file_manager.cpp b/src/file_manager/file_manager.cpp index 60e42e71..0513e0b1 100644 --- a/src/file_manager/file_manager.cpp +++ b/src/file_manager/file_manager.cpp @@ -137,7 +137,7 @@ void file_manager::close_timed_out_files() { for (const auto &api_path : closeable_list) { auto closeable_file = open_file_lookup_.at(api_path); open_file_lookup_.erase(api_path); - open_files.push_back(std::move(closeable_file)); + open_files.push_back(closeable_file); } closeable_list.clear(); file_lock.unlock(); @@ -434,8 +434,8 @@ void file_manager::queue_upload(const std::string &api_path, auto file_manager::remove_file(const std::string &api_path) -> api_error { recur_mutex_lock open_lock(open_file_mtx_); - auto it = open_file_lookup_.find(api_path); - if (it != open_file_lookup_.end() && it->second->is_modified()) { + auto iter = open_file_lookup_.find(api_path); + if (iter != open_file_lookup_.end() && iter->second->is_modified()) { return api_error::file_in_use; } @@ -445,14 +445,13 @@ auto file_manager::remove_file(const std::string &api_path) -> api_error { return res; } + remove_upload(api_path); + close_all(api_path); + if ((res = provider_.remove_file(api_path)) != api_error::success) { return res; } - remove_upload(api_path); - - close_all(api_path); - if (not utils::file::retry_delete_file(fsi.source_path)) { utils::error::raise_api_path_error( __FUNCTION__, fsi.api_path, fsi.source_path, diff --git a/src/file_manager/file_manager_open_file_base.cpp b/src/file_manager/file_manager_open_file_base.cpp index 4d094fb6..9faca8ff 100644 --- a/src/file_manager/file_manager_open_file_base.cpp +++ b/src/file_manager/file_manager_open_file_base.cpp @@ -233,6 +233,8 @@ auto file_manager::open_file_base::close() -> bool { io_thread_.reset(); return true; } + + return false; } io_thread_notify_.notify_all(); diff --git a/tests/fuse_drive_test.cpp b/tests/fuse_drive_test.cpp index 08dad4f0..70f09aad 100644 --- a/tests/fuse_drive_test.cpp +++ b/tests/fuse_drive_test.cpp @@ -519,15 +519,16 @@ static void test_xattr_removexattr(const std::string &file_path) { TEST(fuse_drive, all_tests) { auto current_directory = std::filesystem::current_path(); - for (std::size_t idx = 0U; idx < 2U; idx++) { + for (std::size_t idx = 1U; idx < 2U; idx++) { std::filesystem::current_path(current_directory); const auto test_directory = utils::path::absolute("./fuse_drive" + std::to_string(idx)); + EXPECT_TRUE(utils::file::delete_directory_recursively(test_directory)); const auto mount_location = utils::path::absolute( utils::path::combine(test_directory, {"mount", std::to_string(idx)})); - EXPECT_TRUE(utils::file::delete_directory_recursively(mount_location)); + EXPECT_TRUE(utils::file::create_full_directory_path(mount_location)); { std::unique_ptr config_ptr{}; @@ -536,7 +537,6 @@ TEST(fuse_drive, all_tests) { const auto cfg_directory = utils::path::absolute( utils::path::combine(test_directory, {"cfg", std::to_string(idx)})); - EXPECT_TRUE(utils::file::delete_directory_recursively(cfg_directory)); EXPECT_TRUE(utils::file::create_full_directory_path(cfg_directory)); std::vector drive_args{}; @@ -704,9 +704,6 @@ TEST(fuse_drive, all_tests) { drive_args.push_back(mount_location); execute_mount(config_ptr->get_data_directory(), drive_args, th); } - - EXPECT_TRUE(utils::file::delete_directory_recursively(mount_location)); - EXPECT_TRUE(utils::file::delete_directory_recursively(test_directory)); } std::filesystem::current_path(current_directory);