aboutsummaryrefslogtreecommitdiffstats
path: root/zone_zfs/src/file_system.rs
blob: 83164c96a9ac6f55edefdae3611752dbdae3b59f (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use std::{
    ffi::{OsStr, OsString},
    fmt::Display,
    path::PathBuf,
    process::Command,
};

use crate::{snapshot::Snapshot, Error, Result};

#[derive(Debug)]
pub struct FileSystem {
    pub(crate) value: OsString,
    pub(crate) mountpoint: Option<PathBuf>,
}

impl Display for FileSystem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.value.to_string_lossy())
    }
}

// pool/000.0/nkollack-1
impl TryFrom<OsString> for FileSystem {
    type Error = Error;

    fn try_from(value: OsString) -> Result<Self> {
        Ok(FileSystem {
            value,
            mountpoint: None,
        })
    }
}

impl TryFrom<&str> for FileSystem {
    type Error = Error;

    fn try_from(value: &str) -> Result<Self> {
        value.try_into()
    }
}

impl AsRef<OsStr> for FileSystem {
    fn as_ref(&self) -> &OsStr {
        self.value.as_ref()
    }
}

impl From<FileSystem> for PathBuf {
    fn from(val: FileSystem) -> Self {
        PathBuf::from(val.value)
    }
}

impl From<FileSystem> for String {
    fn from(val: FileSystem) -> Self {
        val.value.to_string_lossy().to_string()
    }
}

impl FileSystem {
    pub(super) fn get_name(&self) -> Result<String> {
        Ok(PathBuf::from(self.value.clone())
            .file_name()
            .ok_or_else(|| Error::FileSystem(format!("Invalid path for filesystem: {:?}", self)))?
            .to_string_lossy()
            .into_owned())
    }

    pub(super) fn set_quota(&self, quota: &str) -> Result<()> {
        Command::new("zfs")
            .arg("set")
            .arg(format!("quota={}", quota))
            .arg(&self.value)
            .status()?
            .success()
            .then(|| ())
            .ok_or_else(|| Error::FileSystem(format!("Failed to set quota: {:?}", self)))
    }

    pub(super) fn get_snapshots(&self) -> Result<Vec<Snapshot>> {
        let stdout = Command::new("zfs")
            .arg("list")
            .arg("-H")
            .arg("-o")
            .arg("name")
            .arg("-t")
            .arg("snapshot")
            .arg(self)
            .output()?
            .stdout;

        String::from_utf8(stdout)
            .map_err(|err| Error::FileSystem(format!("Failed to parse command output: {:?}", err)))?
            .split_whitespace()
            .map(|s| s.try_into())
            .collect()
    }

    pub(super) fn get_latest_snapshot(&self) -> Result<Option<Snapshot>> {
        // pool/447.0@210119221709
        Ok(self
            .get_snapshots()?
            .into_iter()
            .max_by_key(|s| s.timestamp))
    }

    pub(super) fn get_file_systems() -> Result<Vec<FileSystem>> {
        let stdout = Command::new("zfs")
            .arg("list")
            .arg("-H")
            .arg("-o")
            .arg("name")
            .output()?
            .stdout;

        String::from_utf8(stdout)
            .map_err(|err| Error::FileSystem(format!("Failed to parse command output: {:?}", err)))?
            .split_whitespace()
            .map(|s| s.try_into())
            .collect()
    }

    pub fn mount(&self) -> Result<()> {
        Command::new("zfs")
            .arg("mount")
            .arg(&self.value)
            .status()?
            .success()
            .then(|| ())
            .ok_or_else(|| Error::FileSystem(format!("Failed to mount: {:?}", self)))
    }

    pub fn unmount(&self) -> Result<()> {
        Command::new("zfs")
            .arg("unmount")
            .arg(&self.value)
            .status()?
            .success()
            .then(|| ())
            .ok_or_else(|| Error::FileSystem(format!("Failed to unmount: {:?}", self)))
    }

    pub fn destroy(&self, force: bool) -> Result<()> {
        let mut args: Vec<&OsStr> = Vec::new();
        let f_arg = &OsString::from("-f");
        if force {
            args.push(f_arg);
        }
        args.push(&self.value);

        Command::new("zfs")
            .arg("destroy")
            .args(args)
            .status()?
            .success()
            .then(|| ())
            .ok_or_else(|| Error::FileSystem(format!("Failed to destroy: {:?}", self)))
    }
}