Skip to content

Commit 54728b1

Browse files
committed
feat: adding ext that returns change_time for Windows
1 parent a2cf636 commit 54728b1

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

std/src/os/windows/fs.rs

+10
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,13 @@ pub trait MetadataExt {
471471
/// `fs::metadata` or `File::metadata`, then this will return `Some`.
472472
#[unstable(feature = "windows_by_handle", issue = "63010")]
473473
fn file_index(&self) -> Option<u64>;
474+
475+
/// Returns the change time, which is the last time file metadata was changed, such as
476+
/// renames, attributes, etc
477+
///
478+
/// This will return `None` if the `Metadata` instance was not created using the `FILE_BASIC_INFO` type.
479+
#[unstable(feature = "windows_change_time", issue = "121478")]
480+
fn change_time(&self) -> Option<u64>;
474481
}
475482

476483
#[stable(feature = "metadata_ext", since = "1.1.0")]
@@ -499,6 +506,9 @@ impl MetadataExt for Metadata {
499506
fn file_index(&self) -> Option<u64> {
500507
self.as_inner().file_index()
501508
}
509+
fn change_time(&self) -> Option<u64> {
510+
self.as_inner().changed_u64()
511+
}
502512
}
503513

504514
/// Windows-specific extensions to [`fs::FileType`].

std/src/sys/pal/windows/fs.rs

+11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct FileAttr {
3232
creation_time: c::FILETIME,
3333
last_access_time: c::FILETIME,
3434
last_write_time: c::FILETIME,
35+
change_time: Option<c::FILETIME>,
3536
file_size: u64,
3637
reparse_tag: u32,
3738
volume_serial_number: Option<u32>,
@@ -377,6 +378,7 @@ impl File {
377378
creation_time: info.ftCreationTime,
378379
last_access_time: info.ftLastAccessTime,
379380
last_write_time: info.ftLastWriteTime,
381+
change_time: None, // Only available in FILE_BASIC_INFO
380382
file_size: (info.nFileSizeLow as u64) | ((info.nFileSizeHigh as u64) << 32),
381383
reparse_tag,
382384
volume_serial_number: Some(info.dwVolumeSerialNumber),
@@ -413,6 +415,10 @@ impl File {
413415
dwLowDateTime: info.LastWriteTime as u32,
414416
dwHighDateTime: (info.LastWriteTime >> 32) as u32,
415417
},
418+
change_time: Some(c::FILETIME {
419+
dhLowDateTime: info.ChangeTime as c::DWORD,
420+
dhHighDateTime: (info.ChangeTime >> 32) as c::DWORD,
421+
}),
416422
file_size: 0,
417423
reparse_tag: 0,
418424
volume_serial_number: None,
@@ -957,6 +963,10 @@ impl FileAttr {
957963
to_u64(&self.creation_time)
958964
}
959965

966+
pub fn changed_u64(&self) -> Option<u64> {
967+
self.change_time.as_ref().map(|c| to_u64(c))
968+
}
969+
960970
pub fn volume_serial_number(&self) -> Option<u32> {
961971
self.volume_serial_number
962972
}
@@ -976,6 +986,7 @@ impl From<c::WIN32_FIND_DATAW> for FileAttr {
976986
creation_time: wfd.ftCreationTime,
977987
last_access_time: wfd.ftLastAccessTime,
978988
last_write_time: wfd.ftLastWriteTime,
989+
change_time: None,
979990
file_size: ((wfd.nFileSizeHigh as u64) << 32) | (wfd.nFileSizeLow as u64),
980991
reparse_tag: if wfd.dwFileAttributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
981992
// reserved unless this is a reparse point

0 commit comments

Comments
 (0)