online storage & fix directory parent find & and so on

This commit is contained in:
qwjyh 2023-12-06 11:18:46 +09:00
parent 83233740aa
commit b54cbf00f0
5 changed files with 99 additions and 36 deletions

View file

@ -2,13 +2,7 @@
use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt::{self, write},
hash::Hash,
path,
rc::Rc,
};
use std::{collections::HashMap, fmt, path};
use crate::devices;
@ -31,7 +25,7 @@ impl Directory {
/// - `notes`: supplimental notes.
fn new(
name: String,
parent: String, // todo implement serialize
parent: String,
relative_path: path::PathBuf,
notes: String,
local_info: HashMap<String, LocalInfo>,
@ -58,7 +52,11 @@ impl Directory {
.filter_map(|(k, v)| {
let diff = pathdiff::diff_paths(&path, v.mount_path(&device, &storages).unwrap())?;
trace!("Pathdiff: {:?}", diff);
Some((k, diff))
if diff.components().any(|c| c == path::Component::ParentDir) {
None
} else {
Some((k, diff))
}
})
.min_by_key(|(_k, v)| {
let diff_paths: Vec<path::Component<'_>> = v.components().collect();
@ -106,6 +104,16 @@ impl Directory {
let parent_mount_path = parent.mount_path(&device, &storages)?;
Ok(parent_mount_path.join(self.relative_path.clone()))
}
fn bind_device(&mut self, alias: String, device: &devices::Device, storages: &HashMap<String, Storage>) -> Result<()> {
let mount_path = self.mount_path(&device, &storages)?;
let new_local_info = LocalInfo::new(alias, mount_path);
match self.local_info.insert(device.name(), new_local_info) {
Some(v) => println!("Value updated. old val is: {:?}", v),
None => println!("inserted new val"),
};
Ok(())
}
}
impl StorageExt for Directory {

View file

@ -0,0 +1,61 @@
//! Online storage which is not a children of any physical drive.
use anyhow::Context;
use byte_unit::Byte;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::path;
use crate::devices;
use super::local_info::LocalInfo;
use super::StorageExt;
#[derive(Serialize, Deserialize, Debug)]
pub struct OnlineStorage {
name: String,
provider: String,
capacity: u8,
local_info: HashMap<String, LocalInfo>,
}
impl OnlineStorage {
fn new(name: String, provider: String, capacity: u8, path: path::PathBuf, device: &devices::Device) -> OnlineStorage {
todo!()
}
}
impl StorageExt for OnlineStorage {
fn name(&self) -> &String {
&self.name
}
fn has_alias(&self, device: &devices::Device) -> bool {
self.local_info.get(&device.name()).is_some()
}
fn mount_path(
&self,
device: &devices::Device,
_storages: &HashMap<String, super::Storage>,
) -> anyhow::Result<std::path::PathBuf> {
Ok(self
.local_info
.get(&device.name())
.context(format!("LocalInfo for storage: {} not found", &self.name()))?
.mount_path())
}
}
impl fmt::Display for OnlineStorage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"O {name:<10} {size} {provider:<10}",
name = self.name(),
size = Byte::from_bytes(self.capacity.into()).get_appropriate_unit(true),
provider = self.provider,
)
}
}

View file

@ -28,11 +28,6 @@ pub struct PhysicalDrivePartition {
local_info: HashMap<String, LocalInfo>,
}
pub struct PhysicalDrivePartitionLocal {
alias: String,
mount_path: path::PathBuf,
}
impl PhysicalDrivePartition {
/// Try to get Physical drive info from sysinfo.
pub fn try_from_sysinfo_disk(
@ -60,7 +55,7 @@ impl PhysicalDrivePartition {
})
}
pub fn add_alias(
pub fn bind_device(
&mut self,
disk: &sysinfo::Disk,
config_dir: &std::path::PathBuf,