2023-08-29 02:11:47 +09:00
|
|
|
//! Manipulates storages.
|
|
|
|
|
2023-12-05 03:24:49 +09:00
|
|
|
use crate::devices;
|
2023-12-06 11:18:46 +09:00
|
|
|
use crate::storages::directory::Directory;
|
|
|
|
use crate::storages::online_storage::OnlineStorage;
|
|
|
|
use crate::storages::physical_drive_partition::PhysicalDrivePartition;
|
2023-08-30 03:05:26 +09:00
|
|
|
use anyhow::{anyhow, Context, Result};
|
2023-08-29 02:11:47 +09:00
|
|
|
use clap::ValueEnum;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2023-12-05 03:24:49 +09:00
|
|
|
use std::{collections::HashMap, ffi, fmt, fs, io, path};
|
2023-08-29 02:11:47 +09:00
|
|
|
|
|
|
|
/// YAML file to store known storages..
|
|
|
|
pub const STORAGESFILE: &str = "storages.yml";
|
|
|
|
|
|
|
|
#[derive(ValueEnum, Clone, Copy, Debug)]
|
|
|
|
pub enum StorageType {
|
|
|
|
Physical,
|
2023-09-01 10:37:30 +09:00
|
|
|
SubDirectory,
|
2023-12-06 11:18:46 +09:00
|
|
|
Online,
|
2023-08-29 02:11:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/// All storage types.
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub enum Storage {
|
|
|
|
PhysicalStorage(PhysicalDrivePartition),
|
2023-09-01 10:37:30 +09:00
|
|
|
SubDirectory(Directory),
|
2023-12-06 11:18:46 +09:00
|
|
|
Online(OnlineStorage),
|
2023-08-29 02:11:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Storage {
|
2023-12-04 21:34:24 +09:00
|
|
|
/// Add or update alias of `disk` for current device.
|
2023-12-06 11:18:46 +09:00
|
|
|
pub fn bind_device(
|
2023-09-01 10:37:30 +09:00
|
|
|
&mut self,
|
|
|
|
disk: &sysinfo::Disk,
|
|
|
|
config_dir: &std::path::PathBuf,
|
|
|
|
) -> anyhow::Result<()> {
|
2023-08-29 21:33:46 +09:00
|
|
|
match self {
|
2023-12-06 11:18:46 +09:00
|
|
|
Self::PhysicalStorage(s) => s.bind_device(disk, config_dir),
|
2023-09-01 10:37:30 +09:00
|
|
|
Self::SubDirectory(_) => Err(anyhow!("SubDirectory doesn't have system alias.")),
|
2023-12-06 11:18:46 +09:00
|
|
|
Self::Online(_) => todo!(),
|
2023-08-29 21:33:46 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StorageExt for Storage {
|
|
|
|
fn name(&self) -> &String {
|
2023-08-29 02:11:47 +09:00
|
|
|
match self {
|
|
|
|
Self::PhysicalStorage(s) => s.name(),
|
2023-09-01 10:37:30 +09:00
|
|
|
Self::SubDirectory(s) => s.name(),
|
2023-12-06 11:18:46 +09:00
|
|
|
Self::Online(s) => s.name(),
|
2023-08-29 02:11:47 +09:00
|
|
|
}
|
|
|
|
}
|
2023-12-04 21:34:24 +09:00
|
|
|
|
2023-12-06 11:18:46 +09:00
|
|
|
fn has_alias(&self, device: &devices::Device) -> bool {
|
2023-12-05 03:24:49 +09:00
|
|
|
match self {
|
|
|
|
Self::PhysicalStorage(s) => s.has_alias(&device),
|
|
|
|
Self::SubDirectory(s) => s.has_alias(&device),
|
2023-12-06 11:18:46 +09:00
|
|
|
Self::Online(s) => s.has_alias(&device),
|
2023-12-05 03:24:49 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mount_path(
|
|
|
|
&self,
|
|
|
|
device: &devices::Device,
|
|
|
|
storages: &HashMap<String, Storage>,
|
|
|
|
) -> Result<path::PathBuf> {
|
|
|
|
match self {
|
|
|
|
Self::PhysicalStorage(s) => s.mount_path(&device, &storages),
|
|
|
|
Self::SubDirectory(s) => s.mount_path(&device, &storages),
|
2023-12-06 11:18:46 +09:00
|
|
|
Self::Online(s) => s.mount_path(&device, &storages),
|
2023-12-05 03:24:49 +09:00
|
|
|
}
|
|
|
|
}
|
2023-08-29 02:11:47 +09:00
|
|
|
}
|
|
|
|
|
2023-08-29 04:22:04 +09:00
|
|
|
impl fmt::Display for Storage {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::PhysicalStorage(s) => s.fmt(f),
|
2023-09-01 10:37:30 +09:00
|
|
|
Self::SubDirectory(s) => s.fmt(f),
|
2023-12-06 11:18:46 +09:00
|
|
|
Self::Online(s) => s.fmt(f),
|
2023-08-29 04:22:04 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 02:11:47 +09:00
|
|
|
/// Trait to manipulate all `Storage`s (Enums).
|
|
|
|
pub trait StorageExt {
|
|
|
|
fn name(&self) -> &String;
|
2023-12-05 03:24:49 +09:00
|
|
|
fn has_alias(&self, device: &devices::Device) -> bool;
|
2023-12-06 11:18:46 +09:00
|
|
|
/// Get mount path of `self` on `device`.
|
2023-12-05 03:24:49 +09:00
|
|
|
fn mount_path(
|
|
|
|
&self,
|
|
|
|
device: &devices::Device,
|
|
|
|
storages: &HashMap<String, Storage>,
|
|
|
|
) -> Result<path::PathBuf>;
|
2023-08-29 02:11:47 +09:00
|
|
|
}
|
|
|
|
|
2023-09-01 10:37:30 +09:00
|
|
|
pub mod directory;
|
2023-12-04 21:34:24 +09:00
|
|
|
pub mod local_info;
|
2023-12-06 11:18:46 +09:00
|
|
|
pub mod online_storage;
|
2023-08-29 02:11:47 +09:00
|
|
|
pub mod physical_drive_partition;
|
2023-08-30 03:05:26 +09:00
|
|
|
|
|
|
|
/// Get `Vec<Storage>` from devices.yml([DEVICESFILE]).
|
|
|
|
/// If [DEVICESFILE] isn't found, return empty vec.
|
2023-12-05 03:24:49 +09:00
|
|
|
pub fn get_storages(config_dir: &path::Path) -> Result<HashMap<String, Storage>> {
|
2023-08-30 03:05:26 +09:00
|
|
|
if let Some(storages_file) = fs::read_dir(&config_dir)?
|
|
|
|
.filter(|f| {
|
|
|
|
f.as_ref().map_or_else(
|
|
|
|
|_e| false,
|
|
|
|
|f| {
|
|
|
|
let storagesfile: ffi::OsString = STORAGESFILE.into();
|
|
|
|
f.path().file_name() == Some(&storagesfile)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.next()
|
|
|
|
{
|
|
|
|
trace!("{} found: {:?}", STORAGESFILE, storages_file);
|
|
|
|
let f = fs::File::open(config_dir.join(STORAGESFILE))?;
|
|
|
|
let reader = io::BufReader::new(f);
|
2023-12-04 21:34:24 +09:00
|
|
|
let yaml: HashMap<String, Storage> =
|
2023-08-30 03:05:26 +09:00
|
|
|
serde_yaml::from_reader(reader).context("Failed to read devices.yml")?;
|
|
|
|
Ok(yaml)
|
|
|
|
} else {
|
|
|
|
trace!("No {} found", STORAGESFILE);
|
2023-12-04 21:34:24 +09:00
|
|
|
Ok(HashMap::new())
|
2023-08-30 03:05:26 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write `storages` to yaml file in `config_dir`.
|
2023-12-05 03:24:49 +09:00
|
|
|
pub fn write_storages(config_dir: &path::Path, storages: HashMap<String, Storage>) -> Result<()> {
|
2023-08-30 03:05:26 +09:00
|
|
|
let f = fs::File::create(config_dir.join(STORAGESFILE))?;
|
|
|
|
let writer = io::BufWriter::new(f);
|
|
|
|
serde_yaml::to_writer(writer, &storages).map_err(|e| anyhow!(e))
|
2023-09-01 10:37:30 +09:00
|
|
|
}
|