Skip to content

Commit c69f367

Browse files
committed
std: Add more accessors for Metadata on Windows
This commit adds accessors for more fields in `fs::Metadata` on Windows which weren't previously exposed. There's two sources of `fs::Metadata` on Windows currently, one from `DirEntry` and one from a file itself. These two sources of information don't actually have the same set of fields exposed in their stat information, however. To handle this the platform-specific accessors of Windows-specific information all return `Option` to return `None` in the case a metadata comes from a `DirEntry`, but they're guaranteed to return `Some` if it comes from a file itself. This is motivated by some changes in CraneStation/wasi-common#42, and I'm curious how others feel about this platform-specific functionality!
1 parent 1a56336 commit c69f367

File tree

2 files changed

+69
-16
lines changed

2 files changed

+69
-16
lines changed

Diff for: src/libstd/sys/windows/ext/fs.rs

+30
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,33 @@ pub trait MetadataExt {
437437
/// ```
438438
#[stable(feature = "metadata_ext", since = "1.1.0")]
439439
fn file_size(&self) -> u64;
440+
441+
/// Returns the value of the `dwVolumeSerialNumber` field of this
442+
/// metadata.
443+
///
444+
/// This will return `None` if the `Metadata` instance was created from a
445+
/// call to `DirEntry::metadata`. If this `Metadata` was created by using
446+
/// `fs::metadata` or `File::metadata`, then this will return `Some`.
447+
#[unstable(feature = "windows_by_handle", issue = "63010")]
448+
fn volume_serial_number(&self) -> Option<u32>;
449+
450+
/// Returns the value of the `nNumberOfLinks` field of this
451+
/// metadata.
452+
///
453+
/// This will return `None` if the `Metadata` instance was created from a
454+
/// call to `DirEntry::metadata`. If this `Metadata` was created by using
455+
/// `fs::metadata` or `File::metadata`, then this will return `Some`.
456+
#[unstable(feature = "windows_by_handle", issue = "63010")]
457+
fn number_of_links(&self) -> Option<u32>;
458+
459+
/// Returns the value of the `nFileIndex{Low,High}` fields of this
460+
/// metadata.
461+
///
462+
/// This will return `None` if the `Metadata` instance was created from a
463+
/// call to `DirEntry::metadata`. If this `Metadata` was created by using
464+
/// `fs::metadata` or `File::metadata`, then this will return `Some`.
465+
#[unstable(feature = "windows_by_handle", issue = "63010")]
466+
fn file_index(&self) -> Option<u64>;
440467
}
441468

442469
#[stable(feature = "metadata_ext", since = "1.1.0")]
@@ -446,6 +473,9 @@ impl MetadataExt for Metadata {
446473
fn last_access_time(&self) -> u64 { self.as_inner().accessed_u64() }
447474
fn last_write_time(&self) -> u64 { self.as_inner().modified_u64() }
448475
fn file_size(&self) -> u64 { self.as_inner().size() }
476+
fn volume_serial_number(&self) -> Option<u32> { self.as_inner().volume_serial_number() }
477+
fn number_of_links(&self) -> Option<u32> { self.as_inner().number_of_links() }
478+
fn file_index(&self) -> Option<u64> { self.as_inner().file_index() }
449479
}
450480

451481
/// Windows-specific extensions to [`FileType`].

Diff for: src/libstd/sys/windows/fs.rs

+39-16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub struct FileAttr {
2525
last_write_time: c::FILETIME,
2626
file_size: u64,
2727
reparse_tag: c::DWORD,
28+
volume_serial_number: Option<u32>,
29+
number_of_links: Option<u32>,
30+
file_index: Option<u64>,
2831
}
2932

3033
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
@@ -156,6 +159,9 @@ impl DirEntry {
156159
} else {
157160
0
158161
},
162+
volume_serial_number: None,
163+
number_of_links: None,
164+
file_index: None,
159165
})
160166
}
161167
}
@@ -291,23 +297,26 @@ impl File {
291297
pub fn file_attr(&self) -> io::Result<FileAttr> {
292298
unsafe {
293299
let mut info: c::BY_HANDLE_FILE_INFORMATION = mem::zeroed();
294-
cvt(c::GetFileInformationByHandle(self.handle.raw(),
295-
&mut info))?;
296-
let mut attr = FileAttr {
297-
attributes: info.dwFileAttributes,
298-
creation_time: info.ftCreationTime,
299-
last_access_time: info.ftLastAccessTime,
300-
last_write_time: info.ftLastWriteTime,
301-
file_size: ((info.nFileSizeHigh as u64) << 32) | (info.nFileSizeLow as u64),
302-
reparse_tag: 0,
303-
};
304-
if attr.is_reparse_point() {
300+
cvt(c::GetFileInformationByHandle(self.handle.raw(), &mut info))?;
301+
let mut reparse_tag = 0;
302+
if info.dwFileAttributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
305303
let mut b = [0; c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
306304
if let Ok((_, buf)) = self.reparse_point(&mut b) {
307-
attr.reparse_tag = buf.ReparseTag;
305+
reparse_tag = buf.ReparseTag;
308306
}
309307
}
310-
Ok(attr)
308+
Ok(FileAttr {
309+
attributes: info.dwFileAttributes,
310+
creation_time: info.ftCreationTime,
311+
last_access_time: info.ftLastAccessTime,
312+
last_write_time: info.ftLastWriteTime,
313+
file_size: (info.nFileSizeLow as u64) | ((info.nFileSizeHigh as u64) << 32),
314+
reparse_tag,
315+
volume_serial_number: Some(info.dwVolumeSerialNumber),
316+
number_of_links: Some(info.nNumberOfLinks),
317+
file_index: Some((info.nFileIndexLow as u64) |
318+
((info.nFileIndexHigh as u64) << 32)),
319+
})
311320
}
312321
}
313322

@@ -336,6 +345,9 @@ impl File {
336345
},
337346
file_size: 0,
338347
reparse_tag: 0,
348+
volume_serial_number: None,
349+
number_of_links: None,
350+
file_index: None,
339351
};
340352
let mut info: c::FILE_STANDARD_INFO = mem::zeroed();
341353
let size = mem::size_of_val(&info);
@@ -344,6 +356,7 @@ impl File {
344356
&mut info as *mut _ as *mut libc::c_void,
345357
size as c::DWORD))?;
346358
attr.file_size = info.AllocationSize as u64;
359+
attr.number_of_links = Some(info.NumberOfLinks);
347360
if attr.is_reparse_point() {
348361
let mut b = [0; c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
349362
if let Ok((_, buf)) = self.reparse_point(&mut b) {
@@ -507,7 +520,9 @@ impl FileAttr {
507520
FilePermissions { attrs: self.attributes }
508521
}
509522

510-
pub fn attrs(&self) -> u32 { self.attributes as u32 }
523+
pub fn attrs(&self) -> u32 {
524+
self.attributes
525+
}
511526

512527
pub fn file_type(&self) -> FileType {
513528
FileType::new(self.attributes, self.reparse_tag)
@@ -537,8 +552,16 @@ impl FileAttr {
537552
to_u64(&self.creation_time)
538553
}
539554

540-
fn is_reparse_point(&self) -> bool {
541-
self.attributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0
555+
pub fn volume_serial_number(&self) -> Option<u32> {
556+
self.volume_serial_number
557+
}
558+
559+
pub fn number_of_links(&self) -> Option<u32> {
560+
self.number_of_links
561+
}
562+
563+
pub fn file_index(&self) -> Option<u64> {
564+
self.file_index
542565
}
543566
}
544567

0 commit comments

Comments
 (0)