more work

This commit is contained in:
timeshifter
2026-05-03 14:57:06 +02:00
parent af6423cb21
commit 5f1cbf5f67
9 changed files with 314 additions and 112 deletions
+34 -37
View File
@@ -1,22 +1,17 @@
use std::fmt::Write;
use std::{
fmt::Display,
path::{Path, PathBuf},
process::Child,
};
use std::{fmt::Display, path::PathBuf, process::Child};
use anyhow::{Result, anyhow, bail};
use crate::cmd::zfs::*;
use crate::io::read_vars_from_file;
/// A ZFS pool. It holds the name of the pool and the list of datasets belonging to it.
pub struct Pool {
name: String,
pub struct ZPool {
pub name: String,
pub datasets: Vec<Dataset>,
}
impl Pool {
impl ZPool {
pub fn new(arg: &str) -> Result<Self> {
let result = Self {
name: arg.to_string(),
@@ -62,11 +57,6 @@ impl Pool {
Ok(())
}
pub fn load_datasets_from_file(&mut self, filename: &str) -> Result<()> {
self.datasets = Dataset::from_file(&PathBuf::from(filename), &self.name)?;
Ok(())
}
pub fn get_datasets_from_system(&mut self) -> Result<()> {
let datasets = zfs_list_main_datasets(&self.name)?;
@@ -99,12 +89,24 @@ impl Pool {
None
}
}
}
pub fn import(name: &str) -> Result<Pool> {
zpool_import(name)?;
let result = Pool::new(name)?;
Ok(result)
pub fn export(self) -> Result<()> {
todo!()
}
pub fn scrub(&self) -> Result<()> {
zpool_scrub(&self.name)
}
pub fn status(&self) -> Result<String> {
todo!()
}
pub fn import(name: &str) -> Result<ZPool> {
zpool_import(name)?;
let result = ZPool::new(name)?;
Ok(result)
}
}
/// Full path of a ZFS dataset.
@@ -119,27 +121,18 @@ impl Dataset {
Self(path)
}
fn from_file(filename: &Path, pool: &str) -> Result<Vec<Self>> {
let datasets = read_vars_from_file(filename.to_str().unwrap())?;
Ok(datasets
.into_iter()
.map(|ds| {
let mut pb = PathBuf::new();
pb.push(pool);
pb.push(ds);
Self::new(pb)
})
.collect())
}
pub fn find_snapshots_like_tag(&self, snapshot_tag: &str) -> Result<Vec<Snapshot>> {
pub fn find_snapshots_like_tag(
&self,
snapshot_tag: &str,
snapshot_interval: &str,
) -> Result<Vec<Snapshot>> {
let snapshots = zfs_list_snapshots(self.0.to_str().unwrap())?;
let mut result = vec![];
for item in snapshots {
let snapshot_name = item.split('@').next_back().unwrap();
if snapshot_name.contains(snapshot_tag) {
if snapshot_name.contains(snapshot_tag) && snapshot_name.contains(snapshot_interval) {
result.push(Snapshot(snapshot_name.to_string()));
}
}
@@ -147,8 +140,12 @@ impl Dataset {
Ok(result)
}
pub fn find_last_snapshot_with_tag(&self, snapshot_tag: &str) -> Result<Snapshot> {
let mut snapshots = self.find_snapshots_like_tag(snapshot_tag)?;
pub fn find_last_snapshot_with_tag(
&self,
snapshot_tag: &str,
snapshot_interval: &str,
) -> Result<Snapshot> {
let mut snapshots = self.find_snapshots_like_tag(snapshot_tag, snapshot_interval)?;
snapshots.sort_unstable();
let result = snapshots
.last()
@@ -167,7 +164,7 @@ impl Dataset {
Ok(snapshots)
}
pub fn change_pool_name(&self, new_pool: &Pool) -> Self {
pub fn change_pool_name(&self, new_pool: &ZPool) -> Self {
let new_pool = new_pool.name.as_str();
let mut pathbuf = PathBuf::new();
pathbuf.push(new_pool);