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

perf(fs/windows): stat - only open file once #27487

Merged
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
64 changes: 22 additions & 42 deletions ext/fs/std_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl FileSystem for RealFs {
umask(Mode::from_bits_truncate(mask as mode_t))
} else {
// If no mask provided, we query the current. Requires two syscalls.
let prev = umask(Mode::from_bits_truncate(0o777));
let prev = umask(Mode::from_bits_truncate(0));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let _ = umask(prev);
prev
};
Expand Down Expand Up @@ -757,11 +757,16 @@ fn stat(path: &Path) -> FsResult<FsStat> {

#[cfg(windows)]
fn stat(path: &Path) -> FsResult<FsStat> {
let metadata = fs::metadata(path)?;
let mut fsstat = FsStat::from_std(metadata);
use std::os::windows::fs::OpenOptionsExt;
use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS;
let path = path.canonicalize()?;
stat_extra(&mut fsstat, &path, FILE_FLAG_BACKUP_SEMANTICS)?;

let mut opts = fs::OpenOptions::new();
opts.access_mode(0); // no read or write
opts.custom_flags(FILE_FLAG_BACKUP_SEMANTICS);
let file = opts.open(path)?;
let metadata = file.metadata()?;
let mut fsstat = FsStat::from_std(metadata);
stat_extra(&file, &mut fsstat)?;
Ok(fsstat)
}

Expand All @@ -773,34 +778,24 @@ fn lstat(path: &Path) -> FsResult<FsStat> {

#[cfg(windows)]
fn lstat(path: &Path) -> FsResult<FsStat> {
use std::os::windows::fs::OpenOptionsExt;

use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS;
use winapi::um::winbase::FILE_FLAG_OPEN_REPARSE_POINT;

let metadata = fs::symlink_metadata(path)?;
let mut opts = fs::OpenOptions::new();
opts.access_mode(0); // no read or write
opts.custom_flags(FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT);
let file = opts.open(path)?;
let metadata = file.metadata()?;
let mut fsstat = FsStat::from_std(metadata);
stat_extra(
&mut fsstat,
path,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
)?;
stat_extra(&file, &mut fsstat)?;
Ok(fsstat)
}

#[cfg(windows)]
fn stat_extra(
fsstat: &mut FsStat,
path: &Path,
file_flags: winapi::shared::minwindef::DWORD,
) -> FsResult<()> {
use std::os::windows::prelude::OsStrExt;

use winapi::um::fileapi::CreateFileW;
use winapi::um::fileapi::OPEN_EXISTING;
use winapi::um::handleapi::CloseHandle;
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::winnt::FILE_SHARE_DELETE;
use winapi::um::winnt::FILE_SHARE_READ;
use winapi::um::winnt::FILE_SHARE_WRITE;
fn stat_extra(file: &std::fs::File, fsstat: &mut FsStat) -> FsResult<()> {
use std::os::windows::io::AsRawHandle;

unsafe fn get_dev(
handle: winapi::shared::ntdef::HANDLE,
Expand Down Expand Up @@ -869,23 +864,9 @@ fn stat_extra(

// SAFETY: winapi calls
unsafe {
let mut path: Vec<_> = path.as_os_str().encode_wide().collect();
path.push(0);
let file_handle = CreateFileW(
path.as_ptr(),
0,
FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
std::ptr::null_mut(),
OPEN_EXISTING,
file_flags,
std::ptr::null_mut(),
);
if file_handle == INVALID_HANDLE_VALUE {
return Err(std::io::Error::last_os_error().into());
}
let file_handle = file.as_raw_handle();

let result = get_dev(file_handle);
fsstat.dev = result?;
fsstat.dev = get_dev(file_handle)?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also previously weren't closing the file_handle on error here.


if let Ok(file_info) = query_file_information(file_handle) {
fsstat.ctime = Some(windows_time_to_unix_time_msec(
Expand Down Expand Up @@ -924,7 +905,6 @@ fn stat_extra(
}
}

CloseHandle(file_handle);
Ok(())
}
}
Expand Down
Loading