Disk

Struct Disk 

Source
pub struct Disk {
    pub dev_path: PathBuf,
    pub model: Option<String>,
    pub size: Memory,
    pub partitions: Vec<Partition>,
    pub technology: DiskTechnology,
    pub removable: bool,
    pub partition_table_type: Option<PartitionTableType>,
}
Expand description

Contains basic information about disk

Fields§

§dev_path: PathBuf

Device path to disk
e.g. “/dev/nvme0n1” or “/dev/sda”

§model: Option<String>

Disk model

§size: Memory

Disk size

§partitions: Vec<Partition>

Partition on disk

§technology: DiskTechnology

SSD or HDD

§removable: bool

Disk is removable

§partition_table_type: Option<PartitionTableType>

Partition table type

Implementations§

Source§

impl Disk

Source

pub fn is_ssd(&self) -> bool

I don’t know why this function is here, it’s useless i think

Source

pub fn fetch_table_type(&mut self) -> Result<()>

Fetch partition table type
this function updates Disk’s field Disk::partition_table_type root access is required, otherwise you will get an error “Permission denied”

Examples found in repository?
examples/disk.rs (line 15)
5fn main() {
6    if let Ok(all_drives) = disk::fetch_all() {
7        for mut disk in all_drives {
8            println!("Device: {:?}", disk.dev_path);
9            if let Some(model) = disk.model.clone() {
10                println!("- Model: {model}");
11            }
12            println!("- Size: {:.1}GiB", disk.size.gb);
13            println!("- Technology: {:?}", disk.technology);
14            println!("- Is removable: {}", disk.removable);
15            if disk.fetch_table_type().is_ok() {
16                println!("- Partition table: {:?}", disk.partition_table_type);
17            }
18            match disk.fetch_partitions() {
19                Ok(_) => {
20                    if !disk.partitions.is_empty() {
21                        disk.sort_partitions();
22                        println!("- Partitions:");
23                        for partition in disk.partitions.clone().into_iter() {
24                            println!("  Device: {}", partition.dev_path.to_string_lossy());
25                            println!("  - Partition number: {}", partition.number);
26                            println!("  - Size: {:.1}GiB", partition.size.gb);
27                            println!("  - Is read-only: {}", partition.readonly);
28                        }
29                    }
30                }
31                Err(err) => eprintln!("encountered error during partitions fetching: {err:#?}"),
32            }
33        }
34    } else {
35        println!("Platform not supported.");
36    }
37}
Source

pub fn fetch_partitions(&mut self) -> Result<()>

Fetch all partitions on disk

Examples found in repository?
examples/disk.rs (line 18)
5fn main() {
6    if let Ok(all_drives) = disk::fetch_all() {
7        for mut disk in all_drives {
8            println!("Device: {:?}", disk.dev_path);
9            if let Some(model) = disk.model.clone() {
10                println!("- Model: {model}");
11            }
12            println!("- Size: {:.1}GiB", disk.size.gb);
13            println!("- Technology: {:?}", disk.technology);
14            println!("- Is removable: {}", disk.removable);
15            if disk.fetch_table_type().is_ok() {
16                println!("- Partition table: {:?}", disk.partition_table_type);
17            }
18            match disk.fetch_partitions() {
19                Ok(_) => {
20                    if !disk.partitions.is_empty() {
21                        disk.sort_partitions();
22                        println!("- Partitions:");
23                        for partition in disk.partitions.clone().into_iter() {
24                            println!("  Device: {}", partition.dev_path.to_string_lossy());
25                            println!("  - Partition number: {}", partition.number);
26                            println!("  - Size: {:.1}GiB", partition.size.gb);
27                            println!("  - Is read-only: {}", partition.readonly);
28                        }
29                    }
30                }
31                Err(err) => eprintln!("encountered error during partitions fetching: {err:#?}"),
32            }
33        }
34    } else {
35        println!("Platform not supported.");
36    }
37}
Source

pub fn fetch_filesystems(&mut self) -> Result<Option<Filesystem>>

Function under construction !
Fetch filesystems on partitions
Make sure that disk contains partitions (run Disk::fetch_partitions ) and table type (run Disk::fetch_table_type)
Currently only MBR disks supported

Source

pub fn sort_partitions(&mut self)

Sort Disk::partitions by number

Examples found in repository?
examples/disk.rs (line 21)
5fn main() {
6    if let Ok(all_drives) = disk::fetch_all() {
7        for mut disk in all_drives {
8            println!("Device: {:?}", disk.dev_path);
9            if let Some(model) = disk.model.clone() {
10                println!("- Model: {model}");
11            }
12            println!("- Size: {:.1}GiB", disk.size.gb);
13            println!("- Technology: {:?}", disk.technology);
14            println!("- Is removable: {}", disk.removable);
15            if disk.fetch_table_type().is_ok() {
16                println!("- Partition table: {:?}", disk.partition_table_type);
17            }
18            match disk.fetch_partitions() {
19                Ok(_) => {
20                    if !disk.partitions.is_empty() {
21                        disk.sort_partitions();
22                        println!("- Partitions:");
23                        for partition in disk.partitions.clone().into_iter() {
24                            println!("  Device: {}", partition.dev_path.to_string_lossy());
25                            println!("  - Partition number: {}", partition.number);
26                            println!("  - Size: {:.1}GiB", partition.size.gb);
27                            println!("  - Is read-only: {}", partition.readonly);
28                        }
29                    }
30                }
31                Err(err) => eprintln!("encountered error during partitions fetching: {err:#?}"),
32            }
33        }
34    } else {
35        println!("Platform not supported.");
36    }
37}

Trait Implementations§

Source§

impl Clone for Disk

Source§

fn clone(&self) -> Disk

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 Disk

Source§

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

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

impl Default for Disk

Source§

fn default() -> Disk

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Disk

§

impl RefUnwindSafe for Disk

§

impl Send for Disk

§

impl Sync for Disk

§

impl Unpin for Disk

§

impl UnwindSafe for Disk

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.