1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::Stdio;
use nix::libc;
use nix::sys::stat::{fstat, stat, FileStat};
use std::convert::TryFrom;
use std::path::Path;

/// Re-export of nix
pub use nix;

/// Implementation of `Clircle` for Unix.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct UnixIdentifier {
    /// The `st_dev` of a `FileStat` (returned by the `stat` family of functions).
    pub device: libc::dev_t,
    /// The `st_ino` of a `FileStat` (returned by the `stat` family of functions).
    pub inode: libc::ino_t,
}

impl TryFrom<Stdio> for UnixIdentifier {
    type Error = nix::Error;

    fn try_from(stdio: Stdio) -> Result<Self, Self::Error> {
        let fd = match stdio {
            Stdio::Stdin => libc::STDIN_FILENO,
            Stdio::Stdout => libc::STDOUT_FILENO,
            Stdio::Stderr => libc::STDERR_FILENO,
        };
        fstat(fd).map(UnixIdentifier::from)
    }
}

impl<'a> TryFrom<&'a Path> for UnixIdentifier {
    type Error = nix::Error;

    fn try_from(path: &'a Path) -> Result<Self, Self::Error> {
        stat(path).map(UnixIdentifier::from)
    }
}

impl From<FileStat> for UnixIdentifier {
    fn from(stats: FileStat) -> Self {
        UnixIdentifier {
            device: stats.st_dev,
            inode: stats.st_ino,
        }
    }
}