Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Criddle committed Aug 7, 2021
1 parent e41a28c commit 459a216
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod cfg;
use cfg::Cfg;
use log::{debug, error, info};
use notify::{raw_watcher, RawEvent, RecursiveMode, Watcher};
use std::error::Error;
use std::sync::mpsc::channel;
use std::{
ffi::OsString,
Expand All @@ -25,7 +26,7 @@ fn hdd_has_space(hdd: &str, sys: &System, plot_sz: u64) -> bool {
panic!("hdd {} not found in system disks!", hdd);
}

fn get_free_space(hdds: &mut Vec<String>, plot_sz: u64) -> &str {
fn get_free_space(hdds: &mut Vec<String>, plot_sz: u64) -> Result<&str, Box<dyn Error>> {
let mut remove_dirs = vec![];

let mut hdd_idx: i8 = -1;
Expand All @@ -35,20 +36,20 @@ fn get_free_space(hdds: &mut Vec<String>, plot_sz: u64) -> &str {
for (i, path) in hdds.iter().enumerate() {
if hdd_has_space(&path, &sys, plot_sz) {
info!("hdd {:?} has space, using that", path);
return &hdds[i];
return Ok(&hdds[i]);
}
}

// otherwise, remove a plot
for (i, path) in hdds.iter().enumerate() {
let legacy_path = format!("{}{}", path, "/legacy_plots");

let legacy_plots = fs::read_dir(&legacy_path).unwrap();
let legacy_plots = fs::read_dir(&legacy_path)?;
let remove_plot = legacy_plots.into_iter().last();
if remove_plot.is_none() {
remove_dirs.push(hdds[i].clone());
} else {
let path_buf = remove_plot.unwrap().unwrap().path();
let path_buf = remove_plot.unwrap()?.path();
let res = fs::remove_file(&path_buf);
if res.is_err() {
error!("unable to remove {:?} err= {}", &path_buf, res.unwrap_err());
Expand All @@ -70,40 +71,41 @@ fn get_free_space(hdds: &mut Vec<String>, plot_sz: u64) -> &str {
if hdd_idx == -1 {
panic!("no plots were available to remove! are we done re-plotting ?!?!?!")
}
&hdds[hdd_idx as usize]
Ok(&hdds[hdd_idx as usize])
}

fn move_file(source: &DirEntry, hdds: &mut Vec<String>) {
let source_file = String::from(source.file_name().to_str().unwrap());
let source_path = String::from(source.path().to_str().unwrap());
let source_sz = fs::metadata(&source_path).unwrap().len();
let dest_path = format!(
"{}{}{}",
get_free_space(hdds, source_sz),
"/pool_plots/",
&source_file
);

info!("copy plot {:?} to {:?} ... ", &source_path, dest_path);

let res_cpy = fs::copy(&source_path, &dest_path);
let free_path = match get_free_space(hdds, source_sz) {
Ok(path) => path,
Err(e) => {
error!("No free space found, aborting the move! err: {}", e);
return;
}
};
let dest_path = format!("{}{}{}", free_path, "/pool_plots/", &source_file);

if res_cpy.is_err() {
println!(
"unable to copy {} to {} ! err= {}",
&source_path,
&dest_path,
res_cpy.unwrap_err()
);
} else {
info!(" ... completed");
info!("copy plot {:?} to {:?} ... ", &source_path, dest_path);

let res_rm = fs::remove_file(&source_path);
if res_rm.is_err() {
match fs::copy(&source_path, &dest_path) {
Ok(_) => {
info!(" ... completed");
let res_rm = fs::remove_file(&source_path);
if res_rm.is_err() {
error!(
"unable to remove {}! err: {}",
source_path,
res_rm.unwrap_err()
);
}
}
Err(e) => {
error!(
"unable to remove {} ! err= {}",
source_path,
res_rm.unwrap_err()
"unable to copy {} to {}! err: {}",
&source_path, &dest_path, e
);
}
}
Expand Down

0 comments on commit 459a216

Please sign in to comment.