From 83233740aade6bba92a686a01810700073712a3d Mon Sep 17 00:00:00 2001 From: qwjyh Date: Tue, 5 Dec 2023 03:58:19 +0900 Subject: [PATCH] move device related functions & add doc --- src/devices.rs | 52 ++++++++++++++++++- src/main.rs | 65 ++++-------------------- src/storages/local_info.rs | 2 + src/storages/physical_drive_partition.rs | 6 ++- 4 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/devices.rs b/src/devices.rs index e308929..6c5861a 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -1,6 +1,10 @@ //! Manipulates each client device. +use anyhow::{anyhow, Context, Result}; use serde::{Deserialize, Serialize}; +use std::fs::{File, OpenOptions}; +use std::io::{BufRead, BufReader, BufWriter}; +use std::path::Path; use sysinfo::{System, SystemExt}; /// YAML file to store known devices. @@ -53,4 +57,50 @@ mod tests { let device = Device::new("test".to_string()); assert_eq!("test".to_string(), device.name()); } -} \ No newline at end of file +} + +/// Get devname of the device from file `devname`. +fn get_devname(config_dir: &Path) -> Result { + let f = File::open(config_dir.join("devname")).context("Failed to open devname file")?; + let bufreader = BufReader::new(f); + let devname = bufreader + .lines() + .next() + .context("Couldn't get devname.")??; + trace!("devname: {}", devname); + Ok(devname) +} + +/// Get current device. +pub fn get_device(config_dir: &Path) -> Result { + let devname = get_devname(config_dir)?; + let devices = get_devices(config_dir)?; + trace!("devname: {}", devname); + trace!("devices: {:?}", devices); + devices + .into_iter() + .filter(|dev| dev.name() == devname) + .next() + .context("Couldn't find Device in devices.yml") +} + +/// Get `Vec` from yaml file in `config_dir`. +pub fn get_devices(config_dir: &Path) -> Result> { + trace!("get_devices"); + let f = File::open(config_dir.join(DEVICESFILE))?; + let reader = BufReader::new(f); + let yaml: Vec = + serde_yaml::from_reader(reader).context("Failed to parse devices.yml")?; + return Ok(yaml); +} + +/// Write `devices` to yaml file in `config_dir`. +pub fn write_devices(config_dir: &Path, devices: Vec) -> Result<()> { + trace!("write_devices"); + let f = OpenOptions::new() + .create(true) + .write(true) + .open(config_dir.join(DEVICESFILE))?; + let writer = BufWriter::new(f); + serde_yaml::to_writer(writer, &devices).map_err(|e| anyhow!(e)) +} diff --git a/src/main.rs b/src/main.rs index d5d99c6..6be4517 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ -//! # Main variables +//! # Main types //! * [Device]: represents PC. -//! * [Storage]: all storages -//! * [PhysicalDrivePartition]: partition on a physical disk. +//! * [Storage]: all storages. module [storages] +//! * [PhysicalDrivePartition]: partition on a physical disk. [storages::physical_drive_partition] +//! * [Directory]: sub-directory of other storages. [storages::directory] +//! * [storages::local_info::LocalInfo]: stores [Device] specific common data for [Storage]s. //! #[macro_use] @@ -26,18 +28,15 @@ use std::{ ffi::OsString, io::{self, BufWriter}, }; -use std::{ - fmt::Debug, - fs::{File, OpenOptions}, -}; +use std::{fmt::Debug, fs::File}; use std::{fs, io::prelude::*}; use sysinfo::{Disk, DiskExt, SystemExt}; use crate::storages::{ - get_storages, local_info, physical_drive_partition::*, write_storages, Storage, StorageExt, - StorageType, STORAGESFILE, + directory::Directory, get_storages, local_info, physical_drive_partition::*, write_storages, + Storage, StorageExt, StorageType, STORAGESFILE, }; -use devices::{Device, DEVICESFILE}; +use devices::{Device, DEVICESFILE, *}; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -319,31 +318,6 @@ fn main() -> Result<()> { Ok(()) } -/// Get devname of the device from file `devname`. -fn get_devname(config_dir: &Path) -> Result { - let f = File::open(config_dir.join("devname")).context("Failed to open devname file")?; - let bufreader = BufReader::new(f); - let devname = bufreader - .lines() - .next() - .context("Couldn't get devname.")??; - trace!("devname: {}", devname); - Ok(devname) -} - -/// Get current device. -fn get_device(config_dir: &Path) -> Result { - let devname = get_devname(config_dir)?; - let devices = get_devices(config_dir)?; - trace!("devname: {}", devname); - trace!("devices: {:?}", devices); - devices - .into_iter() - .filter(|dev| dev.name() == devname) - .next() - .context("Couldn't find Device in devices.yml") -} - /// Set device name interactively. fn set_device_name() -> Result { let validator = |input: &str| { @@ -376,27 +350,6 @@ fn set_device_name() -> Result { return Ok(device); } -/// Get `Vec` from yaml file in `config_dir`. -fn get_devices(config_dir: &Path) -> Result> { - trace!("get_devices"); - let f = File::open(config_dir.join(DEVICESFILE))?; - let reader = BufReader::new(f); - let yaml: Vec = - serde_yaml::from_reader(reader).context("Failed to parse devices.yml")?; - return Ok(yaml); -} - -/// Write `devices` to yaml file in `config_dir`. -fn write_devices(config_dir: &Path, devices: Vec) -> Result<()> { - trace!("write_devices"); - let f = OpenOptions::new() - .create(true) - .write(true) - .open(config_dir.join(DEVICESFILE))?; - let writer = BufWriter::new(f); - serde_yaml::to_writer(writer, &devices).map_err(|e| anyhow!(e)) -} - fn ask_unique_name(storages: &HashMap, target: String) -> Result { let mut disk_name = String::new(); loop { diff --git a/src/storages/local_info.rs b/src/storages/local_info.rs index 024647c..1369ba8 100644 --- a/src/storages/local_info.rs +++ b/src/storages/local_info.rs @@ -1,3 +1,5 @@ +//! Device specific common data for storages. + use serde::{Deserialize, Serialize}; use std::path; diff --git a/src/storages/physical_drive_partition.rs b/src/storages/physical_drive_partition.rs index 744f68e..8f25c62 100644 --- a/src/storages/physical_drive_partition.rs +++ b/src/storages/physical_drive_partition.rs @@ -91,7 +91,11 @@ impl StorageExt for PhysicalDrivePartition { self.local_info.get(&device.name()).is_some() } - fn mount_path(&self, device: &devices::Device, _: &HashMap) -> Result { + fn mount_path( + &self, + device: &devices::Device, + _: &HashMap, + ) -> Result { Ok(self .local_info .get(&device.name())