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

feat: add tier-1 platform support for change_time #128256

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
29 changes: 20 additions & 9 deletions library/std/src/os/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl OpenOptionsExt for OpenOptions {
/// of the [`BY_HANDLE_FILE_INFORMATION`] structure.
///
/// [`BY_HANDLE_FILE_INFORMATION`]:
/// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/ns-fileapi-by_handle_file_information
/// https://docs.microsoft.com/windows/win32/api/fileapi/ns-fileapi-by_handle_file_information
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
/// Returns the value of the `dwFileAttributes` field of this metadata.
Expand All @@ -322,7 +322,7 @@ pub trait MetadataExt {
/// ```
///
/// [File Attribute Constants]:
/// https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
/// https://docs.microsoft.com/windows/win32/fileio/file-attribute-constants
#[stable(feature = "metadata_ext", since = "1.1.0")]
fn file_attributes(&self) -> u32;

Expand Down Expand Up @@ -351,7 +351,7 @@ pub trait MetadataExt {
/// }
/// ```
///
/// [`FILETIME`]: https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
/// [`FILETIME`]: https://docs.microsoft.com/windows/win32/api/minwinbase/ns-minwinbase-filetime
#[stable(feature = "metadata_ext", since = "1.1.0")]
fn creation_time(&self) -> u64;

Expand Down Expand Up @@ -386,7 +386,7 @@ pub trait MetadataExt {
/// }
/// ```
///
/// [`FILETIME`]: https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
/// [`FILETIME`]: https://docs.microsoft.com/windows/win32/api/minwinbase/ns-minwinbase-filetime
#[stable(feature = "metadata_ext", since = "1.1.0")]
fn last_access_time(&self) -> u64;

Expand Down Expand Up @@ -419,7 +419,7 @@ pub trait MetadataExt {
/// }
/// ```
///
/// [`FILETIME`]: https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
/// [`FILETIME`]: https://docs.microsoft.com/windows/win32/api/minwinbase/ns-minwinbase-filetime
#[stable(feature = "metadata_ext", since = "1.1.0")]
fn last_write_time(&self) -> u64;

Expand Down Expand Up @@ -471,10 +471,21 @@ pub trait MetadataExt {
#[unstable(feature = "windows_by_handle", issue = "63010")]
fn file_index(&self) -> Option<u64>;

/// Returns the change time, which is the last time file metadata was changed, such as
/// renames, attributes, etc
///
/// This will return `None` if the `Metadata` instance was not created using the `FILE_BASIC_INFO` type.
/// Returns the value of the `ChangeTime{Low,High}` field from the
/// [`FILE_BASIC_INFO`] struct associated with the current file handle.
/// [`ChangeTime`], is the last time file metadata was changed, such as
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// [`ChangeTime`], is the last time file metadata was changed, such as
/// [`ChangeTime`] is the last time file metadata was changed, such as

Spurious comma

/// renames, attributes, etc.
///
/// This will return `None` if `Metadata` was populated without access to
/// [`FILE_BASIC_INFO`]. For example, the result of `std::fs::read_dir`
/// will be derived from [`WIN32_FIND_DATA`] which does not have access to
/// `ChangeTime`.
///
/// [`FILE_BASIC_INFO`]: https://learn.microsoft.com/windows/win32/api/winbase/ns-winbase-file_basic_info
/// [`WIN32_FIND_DATA`]: https://learn.microsoft.com/windows/win32/api/minwinbase/ns-minwinbase-win32_find_dataw
/// [`FindFirstFile`]: https://learn.microsoft.com/windows/win32/api/fileapi/nf-fileapi-findfirstfilea
/// [`FindNextFile`]: https://learn.microsoft.com/windows/win32/api/fileapi/nf-fileapi-findnextfilea
/// [`ChangeTime`]: https://devblogs.microsoft.com/oldnewthing/20100709-00/?p=13463#:~:text=I%E2%80%99m%20told%20that%20the%20difference%20is%20metadata.%20The,attributes%20%28hidden%2C%20read-only%2C%20etc.%29%20or%20renaming%20the%20file.
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this has some highlighting, https://devblogs.microsoft.com/oldnewthing/20100709-00/?p=13463 seems to work

#[unstable(feature = "windows_change_time", issue = "121478")]
fn change_time(&self) -> Option<u64>;
}
Expand Down
18 changes: 9 additions & 9 deletions library/std/src/sys/pal/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,19 @@ impl File {
reparse_tag = attr_tag.ReparseTag;
}
}

// FILE_BASIC_INFO contains additional fields not returned by GetFileInformationByHandle
let basic_info = self.basic_info()?;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know what the failure conditions are here, is accessing FILE_BASIC_INFO expected to never fail? Looks like uwp already does this.

If failures are possible, you can just do self.basic_info().ok().map(|info| c::FILETIME { ... }).


Ok(FileAttr {
attributes: info.dwFileAttributes,
creation_time: info.ftCreationTime,
last_access_time: info.ftLastAccessTime,
last_write_time: info.ftLastWriteTime,
change_time: None, // Only available in FILE_BASIC_INFO
change_time: Some(c::FILETIME {
dwLowDateTime: basic_info.ChangeTime as u32,
dwHighDateTime: (basic_info.ChangeTime >> 32) as u32,
}),
file_size: (info.nFileSizeLow as u64) | ((info.nFileSizeHigh as u64) << 32),
reparse_tag,
volume_serial_number: Some(info.dwVolumeSerialNumber),
Expand All @@ -391,14 +398,7 @@ impl File {
#[cfg(target_vendor = "uwp")]
pub fn file_attr(&self) -> io::Result<FileAttr> {
unsafe {
let mut info: c::FILE_BASIC_INFO = mem::zeroed();
let size = mem::size_of_val(&info);
cvt(c::GetFileInformationByHandleEx(
self.handle.as_raw_handle(),
c::FileBasicInfo,
core::ptr::addr_of_mut!(info) as *mut c_void,
size as u32,
))?;
let mut info: c::FILE_BASIC_INFO = self.basic_info()?;
let mut attr = FileAttr {
attributes: info.FileAttributes,
creation_time: c::FILETIME {
Expand Down
Loading