Skip to content

Commit

Permalink
Fixed problems with the backup
Browse files Browse the repository at this point in the history
  • Loading branch information
kinire98 committed Jun 17, 2024
1 parent b08e0f3 commit 40dc2e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
19 changes: 7 additions & 12 deletions src/app/initial_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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,
Expand All @@ -78,9 +75,7 @@ fn copy_operations(
Ok(_) => (),
Err(error) => match error.kind {
fs_extra::error::ErrorKind::NotFound => (),
_ => {
panic!("{}", error);
}
_ => panic!("{:?}", error),
},
}
Ok(())
Expand Down
8 changes: 7 additions & 1 deletion src/app/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum ErrorKind {
OperationAbortedByUser,
PatternAlreadyExist,
TargetDirInsideOrigin,
PermissionDenied,
UndefinedError,
}

Expand All @@ -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"),
}
}
Expand All @@ -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"),
}
}
Expand Down
33 changes: 24 additions & 9 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Self, Self::Error> {
match fs::metadata(value) {
Ok(file_metadata) => Ok(UnixFileTime {
Expand All @@ -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(),
Expand All @@ -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(())
}
}

0 comments on commit 40dc2e1

Please sign in to comment.