aboutsummaryrefslogtreecommitdiffstats
path: root/zone_zfs/src/file_system.rs
diff options
context:
space:
mode:
Diffstat (limited to 'zone_zfs/src/file_system.rs')
-rw-r--r--zone_zfs/src/file_system.rs73
1 files changed, 29 insertions, 44 deletions
diff --git a/zone_zfs/src/file_system.rs b/zone_zfs/src/file_system.rs
index 50e3c56..83164c9 100644
--- a/zone_zfs/src/file_system.rs
+++ b/zone_zfs/src/file_system.rs
@@ -1,12 +1,11 @@
-use anyhow::{anyhow, Context, Result};
use std::{
ffi::{OsStr, OsString},
fmt::Display,
path::PathBuf,
- process::{Command, Output},
+ process::Command,
};
-use super::snapshot::Snapshot;
+use crate::{snapshot::Snapshot, Error, Result};
#[derive(Debug)]
pub struct FileSystem {
@@ -22,9 +21,9 @@ impl Display for FileSystem {
// pool/000.0/nkollack-1
impl TryFrom<OsString> for FileSystem {
- type Error = anyhow::Error;
+ type Error = Error;
- fn try_from(value: OsString) -> Result<Self, Self::Error> {
+ fn try_from(value: OsString) -> Result<Self> {
Ok(FileSystem {
value,
mountpoint: None,
@@ -33,9 +32,9 @@ impl TryFrom<OsString> for FileSystem {
}
impl TryFrom<&str> for FileSystem {
- type Error = anyhow::Error;
+ type Error = Error;
- fn try_from(value: &str) -> Result<Self, Self::Error> {
+ fn try_from(value: &str) -> Result<Self> {
value.try_into()
}
}
@@ -58,34 +57,24 @@ impl From<FileSystem> for String {
}
}
-impl TryFrom<Output> for FileSystem {
- type Error = anyhow::Error;
-
- fn try_from(value: Output) -> Result<Self, Self::Error> {
- std::str::from_utf8(&value.stdout)?.try_into()
- }
-}
-
impl FileSystem {
pub(super) fn get_name(&self) -> Result<String> {
Ok(PathBuf::from(self.value.clone())
.file_name()
- .context("Invalid path for filesystem")?
+ .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<()> {
- match Command::new("zfs")
+ Command::new("zfs")
.arg("set")
.arg(format!("quota={}", quota))
.arg(&self.value)
.status()?
.success()
- {
- true => Ok(()),
- false => Err(anyhow!("Failed to set a quota: {:?}", self)),
- }
+ .then(|| ())
+ .ok_or_else(|| Error::FileSystem(format!("Failed to set quota: {:?}", self)))
}
pub(super) fn get_snapshots(&self) -> Result<Vec<Snapshot>> {
@@ -100,7 +89,8 @@ impl FileSystem {
.output()?
.stdout;
- String::from_utf8(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()
@@ -115,7 +105,7 @@ impl FileSystem {
}
pub(super) fn get_file_systems() -> Result<Vec<FileSystem>> {
- let output = Command::new("zfs")
+ let stdout = Command::new("zfs")
.arg("list")
.arg("-H")
.arg("-o")
@@ -123,34 +113,31 @@ impl FileSystem {
.output()?
.stdout;
- std::str::from_utf8(&output)?
+ String::from_utf8(stdout)
+ .map_err(|err| Error::FileSystem(format!("Failed to parse command output: {:?}", err)))?
.split_whitespace()
- .map(|fs| fs.try_into())
+ .map(|s| s.try_into())
.collect()
}
pub fn mount(&self) -> Result<()> {
- match Command::new("zfs")
+ Command::new("zfs")
.arg("mount")
.arg(&self.value)
.status()?
.success()
- {
- true => Ok(()),
- false => Err(anyhow!("Failed to mount the filesystem: {:?}", self)),
- }
+ .then(|| ())
+ .ok_or_else(|| Error::FileSystem(format!("Failed to mount: {:?}", self)))
}
pub fn unmount(&self) -> Result<()> {
- match Command::new("zfs")
+ Command::new("zfs")
.arg("unmount")
.arg(&self.value)
.status()?
.success()
- {
- true => Ok(()),
- false => Err(anyhow!("Failed to unmount the filesystem: {:?}", self)),
- }
+ .then(|| ())
+ .ok_or_else(|| Error::FileSystem(format!("Failed to unmount: {:?}", self)))
}
pub fn destroy(&self, force: bool) -> Result<()> {
@@ -161,14 +148,12 @@ impl FileSystem {
}
args.push(&self.value);
- match Command::new("zfs")
- .arg("destroy")
- .args(args)
- .status()?
- .success()
- {
- true => Ok(()),
- false => Err(anyhow!("Failed to destroy the filesystem: {:?}", self)),
- }
+ Command::new("zfs")
+ .arg("destroy")
+ .args(args)
+ .status()?
+ .success()
+ .then(|| ())
+ .ok_or_else(|| Error::FileSystem(format!("Failed to destroy: {:?}", self)))
}
}