Struct Metadata
pub struct Metadata(pub(in ::fs) FileAttr);
Metadata information about a file.
This structure is returned from the metadata or
symlink_metadata function or method and represents known
metadata about a file such as its permissions, size, modification
times, etc.
Fields
0: FileAttr
Implementations
impl Metadata
fn file_type(&self) -> FileTypeReturns the file type for this metadata.
Examples
fn main() -> std::io::Result<()> { use std::fs; let metadata = fs::metadata("foo.txt")?; println!("{:?}", metadata.file_type()); Ok(()) }fn is_dir(&self) -> boolReturns
trueif this metadata is for a directory. The result is mutually exclusive to the result ofMetadata::is_file, and will be false for symlink metadata obtained fromsymlink_metadata.Examples
fn main() -> std::io::Result<()> { use std::fs; let metadata = fs::metadata("foo.txt")?; assert!(!metadata.is_dir()); Ok(()) }fn is_file(&self) -> boolReturns
trueif this metadata is for a regular file. The result is mutually exclusive to the result ofMetadata::is_dir, and will be false for symlink metadata obtained fromsymlink_metadata.When the goal is simply to read from (or write to) the source, the most reliable way to test the source can be read (or written to) is to open it. Only using
is_filecan break workflows likediff <( prog_a )on a Unix-like system for example. SeeFile::openorOpenOptions::openfor more information.Examples
use std::fs; fn main() -> std::io::Result<()> { let metadata = fs::metadata("foo.txt")?; assert!(metadata.is_file()); Ok(()) }fn is_symlink(&self) -> boolReturns
trueif this metadata is for a symbolic link.Examples
use std::fs; use std::path::Path; use std::os::unix::fs::symlink; fn main() -> std::io::Result<()> { let link_path = Path::new("link"); symlink("/origin_does_not_exist/", link_path)?; let metadata = fs::symlink_metadata(link_path)?; assert!(metadata.is_symlink()); Ok(()) }fn len(&self) -> u64Returns the size of the file, in bytes, this metadata is for.
Examples
use std::fs; fn main() -> std::io::Result<()> { let metadata = fs::metadata("foo.txt")?; assert_eq!(0, metadata.len()); Ok(()) }fn permissions(&self) -> PermissionsReturns the permissions of the file this metadata is for.
Examples
use std::fs; fn main() -> std::io::Result<()> { let metadata = fs::metadata("foo.txt")?; assert!(!metadata.permissions().readonly()); Ok(()) }fn modified(&self) -> Result<SystemTime>Returns the last modification time listed in this metadata.
The returned value corresponds to the
mtimefield ofstaton Unix platforms and theftLastWriteTimefield on Windows platforms.Errors
This field might not be available on all platforms, and will return an
Erron platforms where it is not available.Examples
use std::fs; fn main() -> std::io::Result<()> { let metadata = fs::metadata("foo.txt")?; if let Ok(time) = metadata.modified() { println!("{time:?}"); } else { println!("Not supported on this platform"); } Ok(()) }fn accessed(&self) -> Result<SystemTime>Returns the last access time of this metadata.
The returned value corresponds to the
atimefield ofstaton Unix platforms and theftLastAccessTimefield on Windows platforms.Note that not all platforms will keep this field update in a file's metadata, for example Windows has an option to disable updating this time when files are accessed and Linux similarly has
noatime.Errors
This field might not be available on all platforms, and will return an
Erron platforms where it is not available.Examples
use std::fs; fn main() -> std::io::Result<()> { let metadata = fs::metadata("foo.txt")?; if let Ok(time) = metadata.accessed() { println!("{time:?}"); } else { println!("Not supported on this platform"); } Ok(()) }fn created(&self) -> Result<SystemTime>Returns the creation time listed in this metadata.
The returned value corresponds to the
btimefield ofstatxon Linux kernel starting from to 4.11, thebirthtimefield ofstaton other Unix platforms, and theftCreationTimefield on Windows platforms.Errors
This field might not be available on all platforms, and will return an
Erron platforms or filesystems where it is not available.Examples
use std::fs; fn main() -> std::io::Result<()> { let metadata = fs::metadata("foo.txt")?; if let Ok(time) = metadata.created() { println!("{time:?}"); } else { println!("Not supported on this platform or filesystem"); } Ok(()) }
Trait Implementations
impl AsInner<FileAttr> for Metadata
fn as_inner(&self) -> &FileAttr
impl Clone for Metadata
fn clone(&self) -> Metadata
impl Debug for Metadata
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl FromInner<FileAttr> for Metadata
fn from_inner(attr: FileAttr) -> Metadata
impl IntoInner<FileAttr> for Metadata
fn into_inner(self) -> FileAttr
impl MetadataExt for Metadata
fn dev(&self) -> u64fn ino(&self) -> u64fn nlink(&self) -> u64
impl MetadataExt for Metadata
fn as_raw_stat(&self) -> &statfn st_dev(&self) -> u64fn st_ino(&self) -> u64fn st_mode(&self) -> u32fn st_nlink(&self) -> u64fn st_uid(&self) -> u32fn st_gid(&self) -> u32fn st_rdev(&self) -> u64fn st_size(&self) -> u64fn st_atime(&self) -> i64fn st_atime_nsec(&self) -> i64fn st_mtime(&self) -> i64fn st_mtime_nsec(&self) -> i64fn st_ctime(&self) -> i64fn st_ctime_nsec(&self) -> i64fn st_birthtime(&self) -> i64fn st_birthtime_nsec(&self) -> i64fn st_blksize(&self) -> u64fn st_blocks(&self) -> u64fn st_gen(&self) -> u32fn st_flags(&self) -> u32fn st_lspare(&self) -> u32
impl MetadataExt for Metadata
fn file_attributes(&self) -> u32fn creation_time(&self) -> u64fn last_access_time(&self) -> u64fn last_write_time(&self) -> u64fn file_size(&self) -> u64fn volume_serial_number(&self) -> Option<u32>fn number_of_links(&self) -> Option<u32>fn file_index(&self) -> Option<u64>fn change_time(&self) -> Option<u64>
impl MetadataExt for Metadata
fn as_raw_stat(&self) -> &statfn st_dev(&self) -> u64fn st_ino(&self) -> u64fn st_mode(&self) -> u32fn st_nlink(&self) -> u64fn st_uid(&self) -> u32fn st_gid(&self) -> u32fn st_rdev(&self) -> u64fn st_size(&self) -> u64fn st_atime(&self) -> i64fn st_atime_nsec(&self) -> i64fn st_mtime(&self) -> i64fn st_mtime_nsec(&self) -> i64fn st_ctime(&self) -> i64fn st_ctime_nsec(&self) -> i64fn st_blksize(&self) -> u64fn st_blocks(&self) -> u64
impl MetadataExt for Metadata
fn dev(&self) -> u64fn ino(&self) -> u64fn mode(&self) -> u32fn nlink(&self) -> u64fn uid(&self) -> u32fn gid(&self) -> u32fn rdev(&self) -> u64fn size(&self) -> u64fn atime(&self) -> i64fn atime_nsec(&self) -> i64fn mtime(&self) -> i64fn mtime_nsec(&self) -> i64fn ctime(&self) -> i64fn ctime_nsec(&self) -> i64fn blksize(&self) -> u64fn blocks(&self) -> u64
Auto Trait Implementations
impl Freeze for Metadata
impl RefUnwindSafe for Metadata
impl Send for Metadata
impl Sync for Metadata
impl Unpin for Metadata
impl UnsafeUnpin for Metadata
impl UnwindSafe for Metadata
Blanket Implementations
impl<T> Any for Metadata
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Metadata
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Metadata
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Metadata
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Metadata
fn from(t: T) -> TReturns the argument unchanged.
impl<T> SizeHint for Metadata
where
T: ?Sized,
fn lower_bound(&self) -> usizefn upper_bound(&self) -> Option<usize>
impl<T> SizedTypeProperties for Metadata
impl<T> ToOwned for Metadata
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for Metadata
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Metadata
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for Metadata
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>