From 4d6305cdc8f6fbdecaf745898b4445a97b1cb88a Mon Sep 17 00:00:00 2001 From: Jenna Elwing Date: Tue, 26 Mar 2024 09:27:50 -0400 Subject: [PATCH] copy.rs in progress --- src/internal/git/repo.rs | 17 +++++++++++++---- src/internal/storage/copy.rs | 7 ++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/internal/git/repo.rs b/src/internal/git/repo.rs index 5506a03..8450438 100644 --- a/src/internal/git/repo.rs +++ b/src/internal/git/repo.rs @@ -1,12 +1,21 @@ use std::path::{PathBuf, Path}; use std::fs; -fn get_relative_path(root_dir: &PathBuf, file_path: &PathBuf) -> PathBuf { - let abs_file_path = file_path.canonicalize().expect("no such file or directory"); +fn get_relative_path(root_dir: &PathBuf, file_path: &PathBuf) -> Result { + let abs_file_path = match file_path.canonicalize() { + Ok(path) => path, + Err(e) => return Err(e), + }; - let abs_root_dir = root_dir.canonicalize().expect("no such file or directory"); + let abs_root_dir = match root_dir.canonicalize() { + Ok(path) => path, + Err(e) => return Err(e), + }; - abs_file_path.strip_prefix(abs_root_dir).expect("paths not relative").to_path_buf() + match abs_file_path.strip_prefix(abs_root_dir) { + Ok(path) => return Ok(path.to_path_buf()), + Err(_) => return Err(std::io::Error::other("paths not relative")), + } } fn is_git_repo(dir: &PathBuf) -> bool { diff --git a/src/internal/storage/copy.rs b/src/internal/storage/copy.rs index e55d031..6e62847 100644 --- a/src/internal/storage/copy.rs +++ b/src/internal/storage/copy.rs @@ -6,10 +6,12 @@ use std::fs; use std::fs::Permissions; pub fn copy(src_path: &PathBuf, dest_path: &PathBuf) -> Result<(), std::io::Error> { + // Ignore .. and . paths if *src_path == PathBuf::from(r"..") || *src_path == PathBuf::from(r".") { return Err(std::io::Error::other("copy failed")); } + // Open source file let src_file = match File::open(src_path) { Ok(file) => { // json @@ -21,11 +23,11 @@ pub fn copy(src_path: &PathBuf, dest_path: &PathBuf) -> Result<(), std::io::Erro } }; + // Get file size let src_file_data = match src_file.metadata() { Ok(data) => data, Err(e) => return Err(e), }; - let src_file_size = src_file_data.len(); // ensure destination exists @@ -40,8 +42,11 @@ pub fn copy(src_path: &PathBuf, dest_path: &PathBuf) -> Result<(), std::io::Erro Err(e) => return Err(e), }; + let path = dest_path.join(dest_file); + let mode: u32 = 0o664; let permissions: Permissions = fs::Permissions::from_mode(mode); + fs::set_permissions(dest_file, fs::Permissions::from_mode(mode)).unwrap(); match fs::set_permissions(dest_path.join(dest_file), permissions) { Ok(_) => { // json: success