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

fix(ext/node): stat.mode on windows #24434

Merged
merged 9 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ext/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ nix.workspace = true
[target.'cfg(windows)'.dependencies]
winapi = { workspace = true, features = ["winbase"] }
junction.workspace = true
ntapi = "0.4.1"
littledivy marked this conversation as resolved.
Show resolved Hide resolved
55 changes: 55 additions & 0 deletions ext/fs/std_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,30 @@ fn stat_extra(
Ok(info.dwVolumeSerialNumber as u64)
}

use ntapi::ntioapi::FILE_ALL_INFORMATION;

unsafe fn query_file_information(
handle: winapi::shared::ntdef::HANDLE,
) -> std::io::Result<FILE_ALL_INFORMATION> {
use ntapi::ntioapi::FileAllInformation;
use ntapi::ntioapi::NtQueryInformationFile;

let mut info = std::mem::MaybeUninit::<FILE_ALL_INFORMATION>::zeroed();
let status = NtQueryInformationFile(
handle,
std::ptr::null_mut(),
info.as_mut_ptr() as *mut _,
std::mem::size_of::<FILE_ALL_INFORMATION>() as _,
FileAllInformation,
);

if status < 0 {
return Err(std::io::Error::last_os_error());
}

Ok(info.assume_init())
}
littledivy marked this conversation as resolved.
Show resolved Hide resolved

// SAFETY: winapi calls
unsafe {
let mut path: Vec<_> = path.as_os_str().encode_wide().collect();
Expand All @@ -790,6 +814,37 @@ fn stat_extra(
CloseHandle(file_handle);
fsstat.dev = result?;

let file_info = query_file_information(file_handle)?;
if file_info.BasicInformation.FileAttributes
& winapi::um::winnt::FILE_ATTRIBUTE_REPARSE_POINT
!= 0
{
fsstat.is_symlink = true;
}

if file_info.BasicInformation.FileAttributes
& winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY
!= 0
{
fsstat.mode |= libc::S_IFDIR as u32;
fsstat.size = 0;
} else {
fsstat.mode |= libc::S_IFREG as u32;
fsstat.size = *file_info.StandardInformation.EndOfFile.QuadPart() as _;
}

if file_info.BasicInformation.FileAttributes
& winapi::um::winnt::FILE_ATTRIBUTE_READONLY
!= 0
{
fsstat.mode |=
(libc::S_IREAD | (libc::S_IREAD >> 3) | (libc::S_IREAD >> 6)) as u32;
} else {
fsstat.mode |= ((libc::S_IREAD | libc::S_IWRITE)
| ((libc::S_IREAD | libc::S_IWRITE) >> 3)
| ((libc::S_IREAD | libc::S_IWRITE) >> 6)) as u32;
}

Ok(())
}
}
Expand Down
Loading