Skip to content

Commit

Permalink
Close file handle after getting its index (windows) (#2728)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus-consoli authored and valeriewong052 committed Sep 27, 2024
1 parent aff81c4 commit 8979634
Showing 1 changed file with 6 additions and 2 deletions.
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

0 comments on commit 8979634

Please sign in to comment.