From 158cd55b1ae6d9030a5f607209b0c7f61b93581c Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Mon, 28 Oct 2024 12:50:20 -0500 Subject: [PATCH] fixes --- .../src/drives/winfsp/winfsp_drive.cpp | 78 ++++++++++--------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp b/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp index af7ec2d1..15c42f8e 100644 --- a/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp +++ b/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp @@ -1088,32 +1088,33 @@ auto winfsp_drive::SetFileSize(PVOID /*file_node*/, PVOID file_desc, REPERTORY_USES_FUNCTION_NAME(); std::string api_path; - auto error = api_error::invalid_handle; + std::shared_ptr file; + const auto handle_error = [this, &api_path, &file, + &file_info](auto error) -> NTSTATUS { + auto ret = utils::from_api_error(error); + RAISE_WINFSP_EVENT(function_name, api_path, ret); + + if (file_info != nullptr && file && error == api_error::success) { + // Populate file information + api_meta_map meta; + if (provider_.get_item_meta(api_path, meta) == api_error::success) { + populate_file_info(file->get_file_size(), meta, *file_info); + } + } + + return ret; + }; + auto handle = static_cast(reinterpret_cast(file_desc)); - if (handle != 0U) { - std::shared_ptr file; - if (fm_->get_open_file(handle, true, file)) { - api_path = file->get_api_path(); - if (set_allocation_size != 0U) { - const auto allocator = [&](native_handle cur_handle) -> api_error { - std::string meta_allocation_size; - utils::calculate_allocation_size(false, 0, new_size, - meta_allocation_size); + if (handle == 0U || not fm_->get_open_file(handle, true, file)) { + return handle_error(api_error::invalid_handle); + } - FILE_ALLOCATION_INFO allocation_info{}; - allocation_info.AllocationSize.QuadPart = - static_cast(new_size); - return ::SetFileInformationByHandle(cur_handle, FileAllocationInfo, - &allocation_info, - sizeof(allocation_info)) != 0 - ? api_error::success - : api_error::os_error; - }; - - error = file->native_operation(allocator); - } else { - const auto allocator = [&](native_handle cur_handle) -> api_error { + api_path = file->get_api_path(); + if (set_allocation_size == 0U) { + return handle_error(file->native_operation( + new_size, [&](native_handle cur_handle) -> api_error { FILE_END_OF_FILE_INFO end_of_file_info{}; end_of_file_info.EndOfFile.QuadPart = static_cast(new_size); return ::SetFileInformationByHandle(cur_handle, FileEndOfFileInfo, @@ -1121,23 +1122,24 @@ auto winfsp_drive::SetFileSize(PVOID /*file_node*/, PVOID file_desc, sizeof(end_of_file_info)) != 0 ? api_error::success : api_error::os_error; - }; - error = file->native_operation(new_size, allocator); - } - - if (file_info != nullptr) { - // Populate file information - api_meta_map meta; - if (provider_.get_item_meta(api_path, meta) == api_error::success) { - populate_file_info(file->get_file_size(), meta, *file_info); - } - } - } + })); } - auto ret = utils::from_api_error(error); - RAISE_WINFSP_EVENT(function_name, api_path, ret); - return ret; + return handle_error( + file->native_operation([&](native_handle cur_handle) -> api_error { + std::string meta_allocation_size; + utils::calculate_allocation_size(false, 0, new_size, + meta_allocation_size); + + FILE_ALLOCATION_INFO allocation_info{}; + allocation_info.AllocationSize.QuadPart = + static_cast(new_size); + return ::SetFileInformationByHandle(cur_handle, FileAllocationInfo, + &allocation_info, + sizeof(allocation_info)) == 0 + ? api_error::os_error + : api_error::success; + })); } VOID winfsp_drive::Unmounted(PVOID host) {