mirror of
https://github.com/qwjyh/xdbm
synced 2024-11-22 06:40:12 +09:00
(WIP) implementing storage::directory
This commit is contained in:
parent
33e1a9aba7
commit
1f0e43cd52
4 changed files with 68 additions and 6 deletions
|
@ -213,6 +213,9 @@ fn main() -> Result<()> {
|
||||||
println!("Added new storage.");
|
println!("Added new storage.");
|
||||||
trace!("Finished adding storage");
|
trace!("Finished adding storage");
|
||||||
}
|
}
|
||||||
|
StorageType::SubDirectory => {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StorageCommands::List {} => {
|
StorageCommands::List {} => {
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
//! Manipulates storages.
|
//! Manipulates storages.
|
||||||
|
|
||||||
|
use crate::storages::directory::Directory;
|
||||||
|
use crate::storages::physical_drive_partition::PhysicalDrivePartition;
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use clap::ValueEnum;
|
use clap::ValueEnum;
|
||||||
use physical_drive_partition::PhysicalDrivePartition;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{ffi, fmt, fs, path::Path, io};
|
use std::{ffi, fmt, fs, io, path::Path};
|
||||||
|
|
||||||
/// YAML file to store known storages..
|
/// YAML file to store known storages..
|
||||||
pub const STORAGESFILE: &str = "storages.yml";
|
pub const STORAGESFILE: &str = "storages.yml";
|
||||||
|
@ -12,6 +13,7 @@ pub const STORAGESFILE: &str = "storages.yml";
|
||||||
#[derive(ValueEnum, Clone, Copy, Debug)]
|
#[derive(ValueEnum, Clone, Copy, Debug)]
|
||||||
pub enum StorageType {
|
pub enum StorageType {
|
||||||
Physical,
|
Physical,
|
||||||
|
SubDirectory,
|
||||||
// Online,
|
// Online,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +21,7 @@ pub enum StorageType {
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub enum Storage {
|
pub enum Storage {
|
||||||
PhysicalStorage(PhysicalDrivePartition),
|
PhysicalStorage(PhysicalDrivePartition),
|
||||||
|
SubDirectory(Directory),
|
||||||
// /// Online storage provided by others.
|
// /// Online storage provided by others.
|
||||||
// OnlineStorage {
|
// OnlineStorage {
|
||||||
// name: String,
|
// name: String,
|
||||||
|
@ -28,9 +31,14 @@ pub enum Storage {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Storage {
|
impl Storage {
|
||||||
pub fn add_alias(&mut self, disk: &sysinfo::Disk, config_dir: &std::path::PathBuf) -> anyhow::Result<()> {
|
pub fn add_alias(
|
||||||
|
&mut self,
|
||||||
|
disk: &sysinfo::Disk,
|
||||||
|
config_dir: &std::path::PathBuf,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
match self {
|
match self {
|
||||||
Self::PhysicalStorage(s) => s.add_alias(disk, config_dir),
|
Self::PhysicalStorage(s) => s.add_alias(disk, config_dir),
|
||||||
|
Self::SubDirectory(_) => Err(anyhow!("SubDirectory doesn't have system alias.")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,15 +47,16 @@ impl StorageExt for Storage {
|
||||||
fn name(&self) -> &String {
|
fn name(&self) -> &String {
|
||||||
match self {
|
match self {
|
||||||
Self::PhysicalStorage(s) => s.name(),
|
Self::PhysicalStorage(s) => s.name(),
|
||||||
|
Self::SubDirectory(s) => s.name(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Storage {
|
impl fmt::Display for Storage {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::PhysicalStorage(s) => s.fmt(f),
|
Self::PhysicalStorage(s) => s.fmt(f),
|
||||||
|
Self::SubDirectory(s) => s.fmt(f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,6 +66,7 @@ pub trait StorageExt {
|
||||||
fn name(&self) -> &String;
|
fn name(&self) -> &String;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod directory;
|
||||||
pub mod physical_drive_partition;
|
pub mod physical_drive_partition;
|
||||||
|
|
||||||
/// Get `Vec<Storage>` from devices.yml([DEVICESFILE]).
|
/// Get `Vec<Storage>` from devices.yml([DEVICESFILE]).
|
||||||
|
@ -91,4 +101,4 @@ pub fn write_storages(config_dir: &Path, storages: Vec<Storage>) -> Result<()> {
|
||||||
let f = fs::File::create(config_dir.join(STORAGESFILE))?;
|
let f = fs::File::create(config_dir.join(STORAGESFILE))?;
|
||||||
let writer = io::BufWriter::new(f);
|
let writer = io::BufWriter::new(f);
|
||||||
serde_yaml::to_writer(writer, &storages).map_err(|e| anyhow!(e))
|
serde_yaml::to_writer(writer, &storages).map_err(|e| anyhow!(e))
|
||||||
}
|
}
|
||||||
|
|
49
src/storages/directory.rs
Normal file
49
src/storages/directory.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
//! Manipulate subdirectories of other storages, including directories.
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{path, fmt::{self, write}, rc::Rc};
|
||||||
|
|
||||||
|
use super::{Storage, StorageExt};
|
||||||
|
|
||||||
|
/// Subdirectory of other [Storage]s.
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Directory {
|
||||||
|
name: String,
|
||||||
|
parent: Box<Storage>,
|
||||||
|
relative_path: path::PathBuf,
|
||||||
|
notes: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Directory {
|
||||||
|
/// - `name`: id
|
||||||
|
/// - `parent`: where the directory locates.
|
||||||
|
/// - `relative_path`: path from root of the parent storage.
|
||||||
|
/// - `notes`: supplimental notes.
|
||||||
|
pub fn new(
|
||||||
|
name: String,
|
||||||
|
parent: Rc<Storage>, // todo implement serialize
|
||||||
|
relative_path: path::PathBuf,
|
||||||
|
notes: String,
|
||||||
|
) -> Directory {
|
||||||
|
Directory {name, parent, relative_path, notes}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StorageExt for Directory {
|
||||||
|
fn name(&self) -> &String {
|
||||||
|
&self.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Directory {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"S {name:<10} < {parent:<10}{relative_path:<10} : {notes}",
|
||||||
|
name = self.name(),
|
||||||
|
parent = self.parent,
|
||||||
|
relative_path = self.relative_path.display(),
|
||||||
|
notes = self.notes,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
|
@ -80,7 +80,7 @@ impl fmt::Display for PhysicalDrivePartition {
|
||||||
let removable_indicator = if self.is_removable { "+" } else { "-" };
|
let removable_indicator = if self.is_removable { "+" } else { "-" };
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{name:<10} {size} {removable:<1} {kind:<6} {fs:<5}",
|
"P {name:<10} {size} {removable:<1} {kind:<6} {fs:<5}",
|
||||||
name = self.name(),
|
name = self.name(),
|
||||||
size = Byte::from_bytes(self.capacity.into()).get_appropriate_unit(true),
|
size = Byte::from_bytes(self.capacity.into()).get_appropriate_unit(true),
|
||||||
removable = removable_indicator,
|
removable = removable_indicator,
|
||||||
|
|
Loading…
Reference in a new issue