Skip to content
Snippets Groups Projects
Verified Commit 0922bae8 authored by nfontrod's avatar nfontrod
Browse files

src/pull.rs: add function used to bypass borg security

parent 2a0f72d4
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@ use crate::mount::file_diff;
use crate::push;
use colored::Colorize;
use std::path::PathBuf;
use std::process::exit;
use std::process::{exit, Command, Stdio};
pub(crate) fn get_savefolder(borg_folder: &PathBuf) -> PathBuf {
let mut save_dir = file_diff::get_tmp_folder(borg_folder);
......@@ -99,6 +99,125 @@ fn save_and_clean_borg_folder(borg_folder: &PathBuf) {
}
}
fn check_ok(path_str: &str) -> PathBuf {
let path = PathBuf::from(path_str);
if !path.is_dir() {
eprintln!(
"{}: {} is not a directory !",
"error:".red(),
path.display()
);
exit(1)
} else {
return path;
}
}
/// Get the base dir
fn get_base_dir() -> PathBuf {
let base_dir = std::env::var("BORG_BASE_DIR");
match base_dir {
Ok(dir) => check_ok(&dir),
Err(_) => {
let base_dir = std::env::var("HOME");
match base_dir {
Ok(dir) => check_ok(&dir),
Err(_) => {
eprintln!("{}: HOME environment variable not set !", "error".red());
exit(1);
}
}
}
}
}
/// This function get the config borg directory
fn get_config_dir() -> PathBuf {
let mut config = get_base_dir();
config.push(".config");
let mut new_config = if !std::env::var("BORG_BASE_DIR").is_ok() {
let new_config = match std::env::var("XDG_CONFIG_HOME") {
Ok(p) => check_ok(&p),
Err(_) => config,
};
new_config
} else {
config
};
new_config.push("borg");
let config_path = match std::env::var("BORG_CONFIG_DIR") {
Ok(p) => check_ok(&p),
Err(_) => new_config,
};
if !config_path.is_dir() {
eprintln!("{}: Failed to get borg config dir", &config_path.display());
exit(1);
}
config_path
}
/// This function aims to get the borg security dir
fn get_security_dir() -> PathBuf {
let security_dir = std::env::var("BORG_SECURITY_DIR");
match security_dir {
Ok(dir) => check_ok(&dir),
Err(_) => {
let mut path = get_config_dir();
path.push("security");
return path;
}
}
}
/// Get the timestamp manifest for the current borg project
/// # Return
/// A file corresponding to the timestamp manifest of the project folder
fn get_manifest_timestamp(security_dir: PathBuf) -> PathBuf {
let current_id = push::get_project_id();
let mut security_dir = security_dir;
security_dir.push(current_id);
security_dir.push("manifest-timestamp");
if !security_dir.is_file() {
eprintln!(
"{}: {} is not a file !",
"error".red(),
&security_dir.display()
);
exit(1);
};
security_dir
}
/// Function that remove the manifest-timestamp
fn remove_manifest_timestamp() {
let security_dir = get_security_dir();
let manifest = get_manifest_timestamp(security_dir);
std::fs::remove_file(&manifest).expect(&format!(
"{}: Unable to delete {}",
"error".red(),
&manifest.display()
));
}
/// Function that removes .borg cache
/// # Arguments
/// - `borg_folder`: The .borg folder of the current project
fn borg_delete_cache(borg_folder: &PathBuf) {
let args = vec!["delete", "--cache-only", borg_folder.to_str().unwrap()];
let mut output = Command::new("borg")
.args(&args)
.stdout(Stdio::inherit())
.stdin(Stdio::inherit())
.spawn()
.unwrap();
match output.wait() {
Err(e) => {
eprintln!("{}: borg terminaned with an error: {}", "error".red(), e);
exit(1);
}
Ok(_) => (),
}
}
/// Create a push the tar achive on the selected remote path
/// # Arguments
......@@ -112,4 +231,6 @@ pub fn pull(remote: &str) -> () {
push::handle_existing_remote_dir(&remote_dir, &adress, "pull");
save_and_clean_borg_folder(&borg_folder);
push::copy_file(&borg_folder, &remote_dir, &adress, "pull");
remove_manifest_timestamp();
borg_delete_cache(&borg_folder);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment