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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
mod file;
mod file_opener;
mod filesystem;
mod stdio;

use file::{File, FileHandle};
pub use file_opener::FileOpener;
pub use filesystem::FileSystem;
pub use stdio::{Stderr, Stdin, Stdout};

use crate::Metadata;
use std::ffi::{OsStr, OsString};

type Inode = usize;
const ROOT_INODE: Inode = 0;

#[derive(Debug)]
enum Node {
    File {
        inode: Inode,
        name: OsString,
        file: File,
        metadata: Metadata,
    },
    Directory {
        inode: Inode,
        name: OsString,
        children: Vec<Inode>,
        metadata: Metadata,
    },
}

impl Node {
    fn inode(&self) -> Inode {
        *match self {
            Self::File { inode, .. } => inode,
            Self::Directory { inode, .. } => inode,
        }
    }

    fn name(&self) -> &OsStr {
        match self {
            Self::File { name, .. } => name.as_os_str(),
            Self::Directory { name, .. } => name.as_os_str(),
        }
    }

    fn metadata(&self) -> &Metadata {
        match self {
            Self::File { metadata, .. } => metadata,
            Self::Directory { metadata, .. } => metadata,
        }
    }

    fn metadata_mut(&mut self) -> &mut Metadata {
        match self {
            Self::File { metadata, .. } => metadata,
            Self::Directory { metadata, .. } => metadata,
        }
    }

    fn set_name(&mut self, new_name: OsString) {
        match self {
            Self::File { name, .. } => *name = new_name,
            Self::Directory { name, .. } => *name = new_name,
        }
    }
}

fn time() -> u64 {
    #[cfg(not(feature = "no-time"))]
    {
        // SAFETY: It's very unlikely that the system returns a time that
        // is before `UNIX_EPOCH` :-).
        std::time::SystemTime::now()
            .duration_since(std::time::SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs()
    }

    #[cfg(feature = "no-time")]
    {
        0
    }
}

// If the `host-fs` feature is not enabled, let's write a
// `TryInto<i32>` implementation for `FileDescriptor`, otherwise on
// Unix, it conflicts with `TryInto<RawFd>` (where `RawFd` is an alias
// to `i32`).
#[cfg(not(all(unix, feature = "host-fs")))]
impl std::convert::TryInto<i32> for crate::FileDescriptor {
    type Error = crate::FsError;

    fn try_into(self) -> std::result::Result<i32, Self::Error> {
        self.0.try_into().map_err(|_| crate::FsError::InvalidFd)
    }
}