Struct FileID

Source
pub struct FileID(/* private fields */);
Expand description

A file’s identifier, can be compared with other FileIDs to check if 2 variables point to the same file.

This struct is the combination of 2 identifiers:

  • The id of the storage that contains the file.
  • The internal file id, unique only across files in the same storage.

Combining both allows to uniquely identify the file within the entire system.

FileID can be used to identify everything that implements AsRawFd (or AsRawHandle on Windows), but it may function differently when identifying non-files.
For example, when used on stdout/stderr/stdin, the 3 will share the same identifier if they belong to the same process. (Only works on Unix, on Windows it will just error…)

Implementations§

Source§

impl FileID

Source

pub fn new<T: GetID + ?Sized>(file: &T) -> Result<Self>

Obtains the identifier of a file, directory, etc.

§Platform-specific behavior

While on Unix obtaining the identifier of a directory is possible, on Windows an error will be returned instead.

This function uses:

  • fstat64 on Unix
  • GetFileInformationByHandleEx on Windows.
  • fd_filestat_get on WASI.

This may change in the future.

§Errors

This function will error if it fails to open the file or fails to obtain the metadata containing the identifier.

§Examples

Basic usage:

use fs_id::FileID;
 
fn main() -> std::io::Result<()> {
    let file_id1 = FileID::new("/some/file/path.txt")?;
    let file_id2 = FileID::new("/some/file/path.txt")?;
    let file_id3 = FileID::new("/some/other/file.txt")?;
    assert_eq!(file_id1, file_id2);
    assert_ne!(file_id1, file_id3);
    Ok(())
}

Many different types can be used:

use fs_id::FileID;
 
fn main() -> std::io::Result<()> {
    let file_id1 = FileID::new("using_str.txt")?;
    let file_id2 = FileID::new(std::ffi::OsStr::new("using_os_str.txt"))?;
    let file_id3 = FileID::new(&std::fs::File::open("using_a_file.txt")?)?;
    let file_id4 = FileID::new(&std::io::stdout())?;
    // etc...
    Ok(())
}
Source

pub const fn storage_id(&self) -> u64

Returns the storage identifier from the file identifier.

§Platform-specific behavior

This returns:

  • st_dev on Unix.
  • VolumeSerialNumber on Windows.
  • dev on WASI.

This may change in the future.

§Examples
use fs_id::FileID;
 
fn main() -> std::io::Result<()> {
    let file_id = FileID::new("/some/file/path.txt")?;
    println!("{}", file_id.storage_id());
    Ok(())
}
Source

pub const fn internal_file_id(&self) -> u128

Returns the internal file identifier from the file identifier.
Note that this value alone cannot uniquely identify the file within the system.

§Platform-specific behavior

This returns:

  • st_ino on Unix.
  • FileId on Windows.
  • ino on WASI.

This may change in the future.

On Unix and WASI only 64 of the returned 128 bits are effectively used.

§Examples
use fs_id::FileID;
 
fn main() -> std::io::Result<()> {
    let file_id = FileID::new("/some/file/path.txt")?;
    println!("{}", file_id.internal_file_id());
    Ok(())
}

Trait Implementations§

Source§

impl Clone for FileID

Source§

fn clone(&self) -> FileID

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FileID

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl GetID for FileID

Source§

fn get_id(&self) -> Result<FileID>

Returns a copy of itself wrapped inside Ok.

Source§

impl Hash for FileID

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for FileID

Source§

fn eq(&self, other: &FileID) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for FileID

Source§

impl Eq for FileID

Source§

impl StructuralPartialEq for FileID

Auto Trait Implementations§

§

impl Freeze for FileID

§

impl RefUnwindSafe for FileID

§

impl Send for FileID

§

impl Sync for FileID

§

impl Unpin for FileID

§

impl UnwindSafe for FileID

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.