Compare commits

..

No commits in common. "8b0dbb2314d2ab3cd9081163899ea4e52a193e4d" and "bc3939c9bc09bee4e5b0c943e6ecde3f652aa947" have entirely different histories.

4 changed files with 31 additions and 43 deletions

View file

@ -33,27 +33,22 @@ pub struct BackupTarget {
/// Use `String` for serialization/deserialization.
pub storage: String,
/// Relative path to the `storage`.
pub path: Vec<String>,
pub path: PathBuf,
}
impl BackupTarget {
pub fn new(storage_name: String, relative_path: PathBuf) -> Result<Self> {
let relative_path = relative_path
.components()
.map(|c| c.as_os_str().to_str().map(|s| s.to_owned()))
.collect::<Option<_>>()
.context("Path contains non-utf8 character")?;
Ok(BackupTarget {
pub fn new(storage_name: String, relative_path: PathBuf) -> Self {
BackupTarget {
storage: storage_name,
path: relative_path,
})
}
}
/// Get full path of the [`BackupTarget`].
pub fn path(&self, storages: &Storages, device: &Device) -> Option<PathBuf> {
let parent = storages.get(&self.storage).unwrap();
let parent_path = parent.mount_path(device)?;
Some(parent_path.join(self.path.clone().iter().collect::<PathBuf>()))
Some(parent_path.join(self.path.clone()))
}
}
@ -180,7 +175,7 @@ impl Backup {
&self.name
}
pub fn device<'a>(&'a self, devices: &'a [Device]) -> Option<&'a Device> {
pub fn device<'a>(&'a self, devices: &'a [Device]) -> Option<&Device> {
devices.iter().find(|dev| dev.name() == self.device)
}

View file

@ -89,8 +89,8 @@ fn new_backup(
Ok(Backup::new(
name,
device.name(),
src_target?,
dest_target?,
src_target,
dest_target,
command,
))
}
@ -361,9 +361,9 @@ mod test {
&storages,
)?;
assert!(backup.source().storage == "online");
assert_eq!(backup.source().path, vec!["docs"]);
assert_eq!(backup.source().path, PathBuf::from("docs"));
assert!(backup.destination().storage == "online");
assert!(backup.destination().path == vec!["tmp"]);
assert!(backup.destination().path == PathBuf::from("tmp"));
Ok(())
}
}

View file

@ -146,7 +146,7 @@ fn parent_backups<'a>(
#[cfg(test)]
mod test {
use std::{path::PathBuf, vec};
use std::path::PathBuf;
use crate::{
backups::{self, ExternallyInvoked},
@ -201,11 +201,11 @@ mod test {
device1.name().to_string(),
backups::BackupTarget {
storage: "storage_1".to_string(),
path: vec!["bar".to_string()],
path: PathBuf::from("bar"),
},
backups::BackupTarget {
storage: "storage_1".to_string(),
path: vec!["hoge".to_string()],
path: PathBuf::from("hoge"),
},
backups::BackupCommand::ExternallyInvoked(ExternallyInvoked::new(
"cmd".to_string(),
@ -217,11 +217,11 @@ mod test {
device2.name().to_string(),
backups::BackupTarget {
storage: "storage_1".to_string(),
path: vec!["".to_string()],
path: PathBuf::from(""),
},
backups::BackupTarget {
storage: "storage_3".to_string(),
path: vec!["foo".to_string()],
path: PathBuf::from("foo"),
},
backups::BackupCommand::ExternallyInvoked(ExternallyInvoked::new(
"cmd".to_string(),

View file

@ -2,7 +2,6 @@
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::{collections::BTreeMap, fmt, path};
use crate::devices;
@ -18,7 +17,7 @@ pub struct Directory {
/// ID of parent storage.
parent: String,
/// Relative path to the parent storage.
relative_path: Vec<String>,
relative_path: path::PathBuf,
pub notes: String,
/// [`devices::Device`] name and localinfo pairs.
local_infos: BTreeMap<String, LocalInfo>,
@ -35,19 +34,14 @@ impl Directory {
relative_path: path::PathBuf,
notes: String,
local_infos: BTreeMap<String, LocalInfo>,
) -> Result<Directory> {
let relative_path = relative_path
.components()
.map(|c| c.as_os_str().to_str().map(|s| s.to_owned()))
.collect::<Option<Vec<_>>>()
.context("Path contains non-utf8 character")?;
Ok(Directory {
) -> Directory {
Directory {
name,
parent,
relative_path,
notes,
local_infos,
})
}
}
pub fn try_from_device_path(
@ -62,23 +56,23 @@ impl Directory {
.context("Failed to compare diff of paths")?;
trace!("Selected parent: {}", parent.name());
let local_info = LocalInfo::new(alias, path);
Directory::new(
Ok(Directory::new(
name,
parent.name().to_string(),
diff_path,
notes,
BTreeMap::from([(device.name(), local_info)]),
)
))
}
pub fn update_note(self, notes: String) -> Directory {
Directory {
name: self.name,
parent: self.parent,
relative_path: self.relative_path,
Directory::new(
self.name,
self.parent,
self.relative_path,
notes,
local_infos: self.local_infos,
}
self.local_infos,
)
}
/// Resolve mount path of directory with current device.
@ -88,7 +82,7 @@ impl Directory {
.context("Can't find parent storage")?
.mount_path(device)
.context("Can't find mount path")?;
Ok(parent_mount_path.join(self.relative_path.clone().iter().collect::<PathBuf>()))
Ok(parent_mount_path.join(self.relative_path.clone()))
}
}
@ -127,7 +121,7 @@ impl StorageExt for Directory {
}
// Get parent `&Storage` of directory.
fn parent<'a>(&'a self, storages: &'a Storages) -> Option<&'a Storage> {
fn parent<'a>(&'a self, storages: &'a Storages) -> Option<&Storage> {
storages.get(&self.parent)
}
}
@ -139,7 +133,7 @@ impl fmt::Display for Directory {
"S {name:<10} < {parent:<10}{relative_path:<10} : {notes}",
name = self.name(),
parent = self.parent,
relative_path = self.relative_path.iter().collect::<PathBuf>().display(),
relative_path = self.relative_path.display(),
notes = self.notes,
)
}
@ -183,8 +177,7 @@ mod test {
"subdir".into(),
"some note".to_string(),
local_infos,
)
.unwrap();
);
let mut storages = Storages::new();
storages.add(storages::Storage::Physical(physical)).unwrap();
storages.add(Storage::SubDirectory(directory)).unwrap();