Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close file handle after getting its index (windows) #2728

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions core/crates/file-path-helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ pub fn path_is_hidden(path: impl AsRef<Path>, metadata: &Metadata) -> bool {
}

#[cfg(target_family = "windows")]
// TODO(matheus-consoli): rewrite this function using a safe API once `MetadataExt::file_index` got stabilized
// see: `https://doc.rust-lang.org/std/os/windows/fs/trait.MetadataExt.html#tymethod.file_index`
fn get_inode_windows<P: AsRef<Path>>(path: P) -> Result<u64, std::io::Error> {
use std::ptr::null_mut;
use windows::{
core::HSTRING,
Win32::Foundation::HANDLE,
Win32::Foundation::{CloseHandle, HANDLE},
Win32::Storage::FileSystem::{
CreateFileW, GetFileInformationByHandle, BY_HANDLE_FILE_INFORMATION,
FILE_ATTRIBUTE_NORMAL, FILE_FLAG_BACKUP_SEMANTICS, FILE_SHARE_READ, OPEN_EXISTING,
Expand All @@ -127,7 +129,9 @@ fn get_inode_windows<P: AsRef<Path>>(path: P) -> Result<u64, std::io::Error> {
}?;

let mut file_info = BY_HANDLE_FILE_INFORMATION::default();
unsafe { GetFileInformationByHandle(handle, &mut file_info) }?;
let res = unsafe { GetFileInformationByHandle(handle, &mut file_info) };
_ = unsafe { CloseHandle(handle) };
res?;

Ok(file_info.nFileIndexLow as u64 | ((file_info.nFileIndexHigh as u64) << 32))
}
Expand Down
Loading