diff --git a/src/app/initial_copy.rs b/src/app/initial_copy.rs index 676fe7c..da7f9e8 100644 --- a/src/app/initial_copy.rs +++ b/src/app/initial_copy.rs @@ -4,7 +4,6 @@ use crate::output::cli; use core::panic; use std::fs::DirEntry; use std::path::PathBuf; -use std::process::Command; use std::sync::mpsc::{self, Receiver, Sender}; pub fn initial_copy(origin_dir: PathBuf, mut target_dir: PathBuf) -> Result<(), Error> { @@ -46,27 +45,25 @@ fn copy_operations( if origin_dir.path().is_symlink() { return Ok(()); } - tx.send(target_dir.display().to_string()) + tx.send(origin_dir.path().display().to_string()) .map_err(|_| Error { kind: ErrorKind::IOError, })?; if origin_dir.path().is_dir() { + dbg!(origin_dir.path().display()); match std::fs::create_dir_all(&target_dir) { - Ok(_) => (), + Ok(()) => (), Err(err) => match err.kind() { std::io::ErrorKind::PermissionDenied => { - #[cfg(windows)] - panic!("It seems some files in your directory need administrator privileges. Start the console with adming privileges and rerun this"); - #[cfg(unix)] - Command::new("sudo su"); - std::fs::create_dir_all(&target_dir).unwrap(); + return Err(Error { + kind: ErrorKind::PermissionDenied, + }) } _ => (), }, } return start_initial_copy(origin_dir.path(), target_dir, tx); } - std::fs::create_dir_all(&path_target_dir).unwrap(); match fs_extra::file::copy( origin_dir.path(), target_dir, @@ -78,9 +75,7 @@ fn copy_operations( Ok(_) => (), Err(error) => match error.kind { fs_extra::error::ErrorKind::NotFound => (), - _ => { - panic!("{}", error); - } + _ => panic!("{:?}", error), }, } Ok(()) diff --git a/src/app/operations.rs b/src/app/operations.rs index 4168ccb..94813e5 100644 --- a/src/app/operations.rs +++ b/src/app/operations.rs @@ -67,9 +67,15 @@ fn backup_operations( dir.path().is_dir() || target_file.is_dir(), ) { // If shouldn't be backed we finish and return - (false, _) => Ok(()), + (false, _) => { + println!("doesnt"); + Ok(())} + , // Should be backed and is a file. We copy the file and return (true, false) => { + println!("Should be here"); + println!("{:?}, \n{}", dir.path().display(), dir.path().is_dir()); + println!("{:?}\n {}", &target_file, target_file.is_dir()); // Copy contents tx.send(dir.path().display().to_string()) .map_err(|_| error::Error { diff --git a/src/error.rs b/src/error.rs index ec661bc..0275af8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -24,6 +24,7 @@ pub enum ErrorKind { OperationAbortedByUser, PatternAlreadyExist, TargetDirInsideOrigin, + PermissionDenied, UndefinedError, } @@ -47,6 +48,7 @@ impl Display for Error { }, ErrorKind::PatternAlreadyExist => write!(f, "The pattern you introduced already exists"), ErrorKind::TargetDirInsideOrigin => write!(f, "The directory where you want to store the backup can't be inside or be a child of the directory to back"), + ErrorKind::PermissionDenied => write!(f, "Make sure you have the correct permissions to read and write in both folders"), ErrorKind::UndefinedError => write!(f, "An undefined error has ocurred"), } } @@ -71,6 +73,7 @@ impl Debug for Error { }, ErrorKind::PatternAlreadyExist => write!(f, "The pattern you introduced already exists"), ErrorKind::TargetDirInsideOrigin => write!(f, "The directory where you want to store the backup can't be inside or be a child of the directory to back"), + ErrorKind::PermissionDenied => write!(f, "Make sure you have the correct permissions to read and write in both folders"), ErrorKind::UndefinedError => write!(f, "An undefined error has ocurred"), } } diff --git a/src/sys/unix.rs b/src/sys/unix.rs index c8ed6e9..0f8d7a6 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -1,7 +1,6 @@ use crate::error::{Error, ErrorKind}; use fs_extra::dir::create_all; use std::fs; -use std::fs::OpenOptions; use std::io; use std::os::unix::fs::MetadataExt; use std::path::PathBuf; @@ -20,6 +19,9 @@ impl super::File for UnixFileTime { } impl TryFrom<&PathBuf> for UnixFileTime { type Error = crate::error::Error; + // If a folder has an extension will be created as file (No problem with this) + // If a file doesn't have it it will be created as folder (Half problem with this. Files with + // no extension are the ones users don't usually work with) fn try_from(value: &PathBuf) -> Result { match fs::metadata(value) { Ok(file_metadata) => Ok(UnixFileTime { @@ -28,14 +30,13 @@ impl TryFrom<&PathBuf> for UnixFileTime { }), Err(err) => match err.kind() { io::ErrorKind::NotFound => { - if !value.is_file() && !value.is_symlink() { - create_all(value, false).unwrap(); - } else if value.is_file() { - let _ = OpenOptions::new() - .write(true) - .create_new(true) - .open(value.display().to_string()) - .expect("Temporary"); + match value.extension() { + None => { + create_all(value, false).unwrap(); + } + Some(_) => { + create_file(value)?; + } } Ok(UnixFileTime { creation_time: fs::metadata(value).expect("Checked").mtime(), @@ -49,3 +50,17 @@ impl TryFrom<&PathBuf> for UnixFileTime { } } } +fn create_file(path: &PathBuf) -> Result<(), Error> { + if let Err(err) = fs::write(path, "") { + println!("File"); + match err.kind() { + io::ErrorKind::PermissionDenied => Err(Error { + kind: ErrorKind::PermissionDenied, + }), + io::ErrorKind::AlreadyExists => Ok(()), + _ => panic!("{:?}", err), + } + } else { + Ok(()) + } +}