This commit is contained in:
Scott E. Graves 2024-10-28 12:50:20 -05:00
parent 5fcc59434b
commit 158cd55b1a

View File

@ -1088,15 +1088,45 @@ 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<i_open_file> 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<std::uint64_t>(reinterpret_cast<std::uintptr_t>(file_desc));
if (handle != 0U) {
std::shared_ptr<i_open_file> file;
if (fm_->get_open_file(handle, true, file)) {
if (handle == 0U || not fm_->get_open_file(handle, true, file)) {
return handle_error(api_error::invalid_handle);
}
api_path = file->get_api_path();
if (set_allocation_size != 0U) {
const auto allocator = [&](native_handle cur_handle) -> api_error {
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<LONGLONG>(new_size);
return ::SetFileInformationByHandle(cur_handle, FileEndOfFileInfo,
&end_of_file_info,
sizeof(end_of_file_info)) != 0
? api_error::success
: api_error::os_error;
}));
}
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);
@ -1106,38 +1136,10 @@ auto winfsp_drive::SetFileSize(PVOID /*file_node*/, PVOID file_desc,
static_cast<LONGLONG>(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 {
FILE_END_OF_FILE_INFO end_of_file_info{};
end_of_file_info.EndOfFile.QuadPart = static_cast<LONGLONG>(new_size);
return ::SetFileInformationByHandle(cur_handle, FileEndOfFileInfo,
&end_of_file_info,
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;
sizeof(allocation_info)) == 0
? api_error::os_error
: api_error::success;
}));
}
VOID winfsp_drive::Unmounted(PVOID host) {