Skip to main content

parallel_disk_usage/
device.rs

1use derive_more::{Display, From, Into, LowerHex, Octal, UpperHex};
2
3#[cfg(feature = "json")]
4use serde::{Deserialize, Serialize};
5
6/// Whether to cross device boundary into a different filesystem.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum DeviceBoundary {
9    Cross,
10    Stay,
11}
12
13impl DeviceBoundary {
14    /// Derive device boundary from `--one-file-system`.
15    #[cfg(feature = "cli")]
16    pub(crate) fn from_one_file_system(one_file_system: bool) -> Self {
17        match one_file_system {
18            false => DeviceBoundary::Cross,
19            true => DeviceBoundary::Stay,
20        }
21    }
22}
23
24/// The device number of a filesystem.
25#[derive(
26    Debug, Display, LowerHex, UpperHex, Octal, Clone, Copy, PartialEq, Eq, Hash, From, Into,
27)]
28#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
29pub struct DeviceNumber(u64);
30
31/// POSIX-exclusive functions.
32#[cfg(unix)]
33impl DeviceNumber {
34    /// Get device number of a [`std::fs::Metadata`].
35    #[inline]
36    pub fn get(stats: &std::fs::Metadata) -> Self {
37        use pipe_trait::Pipe;
38        use std::os::unix::fs::MetadataExt;
39        stats.dev().pipe(DeviceNumber)
40    }
41}