Skip to content

Commit

Permalink
fixed epect error handling, set permissions in add
Browse files Browse the repository at this point in the history
  • Loading branch information
jenna-a2ai committed Mar 27, 2024
1 parent c32b1c2 commit 45718ae
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 34 deletions.
7 changes: 5 additions & 2 deletions src/cmd/add_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ pub fn run_add_cmd(files: &Vec<PathBuf>, message: &String) -> Result<(), std::io
};

// load the config
let conf = config::read(&git_dir).expect("could not open yaml");
let conf = match config::read(&git_dir) {
Ok(config) => config,
Err(_) => return Err(std::io::Error::other("config not readable")),
};

let mut queued_paths: Vec<PathBuf> = Vec::new();

Expand Down Expand Up @@ -54,7 +57,7 @@ pub fn run_add_cmd(files: &Vec<PathBuf>, message: &String) -> Result<(), std::io

// add each file in queued_paths to storage
for file in &queued_paths {
match add::add(file, &conf.storage_dir, &git_dir, &message) {
match add::add(file, &conf, &git_dir, &message) {
Ok(_) => {}
Err(e) => return Err(e),
};
Expand Down
4 changes: 3 additions & 1 deletion src/internal/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::path::PathBuf;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct Config {
pub storage_dir: PathBuf
pub storage_dir: PathBuf,
pub permissions: u32,
pub group: String
}

pub fn read(root_dir: &PathBuf) -> Result<Config, serde_yaml::Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/git/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn add_gitignore_entry(git_dir: &PathBuf, path: &PathBuf) -> Result<(), std:
// open the gitignore file, creating one if it doesn't exist
let ignore_file = git_dir.join(".gitignore");
if !ignore_file.exists() {
File::create(&ignore_file).expect("gitignore cannot be created");
File::create(&ignore_file)?;
}
let contents = std::fs::read_to_string(&ignore_file).unwrap();
if !contents.contains(&ignore_entry) {
Expand Down
1 change: 0 additions & 1 deletion src/internal/meta/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub struct Metadata {
pub file_size: u64,
pub time_stamp: SystemTime,
pub message: String,
pub group: String,
pub saved_by: String
}

Expand Down
63 changes: 43 additions & 20 deletions src/internal/storage/add.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use std::path::Path;
use std::time::SystemTime;
use file_owner::PathExt;
use file_owner::{Group, PathExt};
use std::fs::{self, Permissions};

use crate::internal::config::config::Config;
use crate::internal::file::hash;
use crate::internal::storage::copy;
use crate::internal::meta::file;
use crate::internal::git::ignore;


pub fn add(local_path: &PathBuf, storage_dir: &PathBuf, git_dir: &PathBuf, message: &String) -> Result<String, std::io::Error> {
pub fn add(local_path: &PathBuf, conf: &Config, git_dir: &PathBuf, message: &String) -> Result<String, std::io::Error> {
// get file hash
let file_hash = match hash::hash_file_with_blake3(local_path) {
Ok(file_hash) => {
Expand All @@ -23,14 +25,19 @@ pub fn add(local_path: &PathBuf, storage_dir: &PathBuf, git_dir: &PathBuf, messa
};

// get storage path
let dest_path = hash::get_storage_path(&storage_dir, &file_hash);
let storage_dir_abs = match conf.storage_dir.canonicalize() {
Ok(path) => path,
Err(e) => return Err(e),
};
let dest_path = hash::get_storage_path(&storage_dir_abs, &file_hash);

// Copy the file to the storage directory
// if the destination already exists, skip copying
if !dest_path.exists() {
// copy
match copy::copy(&local_path, &dest_path) {
Ok(_) => {

// json
}
Err(e) => {
Expand All @@ -44,52 +51,68 @@ pub fn add(local_path: &PathBuf, storage_dir: &PathBuf, git_dir: &PathBuf, messa
// json
}

// set permissions
let mode = conf.permissions;
let permissions: Permissions = fs::Permissions::from_mode(mode);
match fs::set_permissions(&dest_path, permissions) {
Ok(_) => {
// json: success
},
Err(e) => {
// json: fail
return Err(e)
}
};

// set group ownership
let group_name = conf.group.as_str();
let group = match Group::from_name(group_name) {
Ok(group) => {
// json
group
}
Err(_) => return Err(std::io::Error::other("group name is invalid"))
};
match dest_path.set_group(group) {
Ok(_) => {},
Err(_) => return Err(std::io::Error::other("group name is invalid"))
};

// get file size
let local_path_data = match local_path.metadata() {
Ok(data) => data,
Err(e) => return Err(e),
};
let file_size = local_path_data.len();

// get user
// get user name
let owner = match local_path.owner() {
Ok(owner) => owner,
Err(_) => return Err(std::io::Error::other("file owner not found")),
};
// get user name
let owner_name = match owner.name() {
Ok(name) => name.unwrap(),
Err(_) => {return Err(std::io::Error::other("file owner not found"))},
};

// get group
let group = match local_path.group() {
Ok(group) => group,
Err(_) => return Err(std::io::Error::other("file group not found")),
};
// get group name
let group_name = match group.name() {
Ok(name) => name.unwrap(),
Err(_) => {return Err(std::io::Error::other("file group not found"))},
};

// create + write metadata file
let metadata = file::Metadata{
file_hash: file_hash.clone(),
file_size,
time_stamp: SystemTime::now(),
message: message.clone(),
group: group_name,
saved_by: owner_name
};

match file::save(&metadata, &local_path) {
Ok(_) => {},
Err(_) => return Err(std::io::Error::other("metadata file not created"))
};

// Add file to gitignore
ignore::add_gitignore_entry(git_dir, local_path).expect("gitignore entry unable to be added");
match ignore::add_gitignore_entry(git_dir, local_path) {
Ok(_) => {},
Err(_) => return Err(std::io::Error::other("gitignore entry could not be created"))
};

return Ok(file_hash);
}
9 changes: 1 addition & 8 deletions src/internal/storage/copy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::path::PathBuf;
use std::fs::{create_dir_all, File};
use std::os::unix::fs::PermissionsExt;
use std::fs;
use std::fs::Permissions;

pub fn copy(src_path: &PathBuf, dest_path: &PathBuf) -> Result<(), std::io::Error> {
// Ignore .. and . paths
Expand Down Expand Up @@ -41,13 +39,8 @@ pub fn copy(src_path: &PathBuf, dest_path: &PathBuf) -> Result<(), std::io::Erro
Err(e) => return Err(e),
};

// Set destination file permissions
let mode: u32 = 0o664;
let permissions: Permissions = fs::Permissions::from_mode(mode);
fs::set_permissions(dest_path, permissions).expect("file unable to be copied, permissions denied");

// Copy the file
fs::copy(src_path, dest_path).expect("file unable to be copied");
fs::copy(src_path, dest_path)?;

Ok(())
}
2 changes: 1 addition & 1 deletion src/internal/storage/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn init(root_dir: &PathBuf, storage_dir: &PathBuf, mode: &u32, group_name: &
};

// write config
match config::write(&Config{storage_dir: storage_dir_abs.clone()}, &root_dir) {
match config::write(&Config{storage_dir: storage_dir_abs.clone(), permissions: mode.clone(), group: group_name.clone()}, &root_dir) {
Ok(_) => {
// json
}
Expand Down

0 comments on commit 45718ae

Please sign in to comment.