diff --git a/src/file_management.rs b/src/file_management.rs index faeb78a..fa5bc12 100644 --- a/src/file_management.rs +++ b/src/file_management.rs @@ -4,7 +4,10 @@ use std::{env, fs}; use globwalk::GlobWalkerBuilder; -use crate::fsops::{can_safely_overwrite, get_unique_filepath, have_equal_contents, safe_rename}; +use crate::fsops::{ + can_safely_overwrite, chmod, get_unique_filepath, have_equal_contents, + rename_with_create_dir_all, +}; use crate::image::{ Image, ImageToReview, PhotoReview, PhotoReview as ReviewedPhoto, PhotosToReview, }; @@ -58,7 +61,8 @@ impl FileManager { final_destination_file ); } - safe_rename(source_file, &final_destination_file) + rename_with_create_dir_all(source_file, &final_destination_file, 0o775)?; + chmod(&final_destination_file, 0o775) } pub fn undo(&self, review: &PhotoReview) -> Result<()> { @@ -67,7 +71,7 @@ impl FileManager { if !PathBuf::from(&destination_file).exists() { bail!("Cannot undo, photo at [{}] not found", &destination_file) } - safe_rename(&destination_file, &review.image.full_path) + rename_with_create_dir_all(&destination_file, &review.image.full_path, 0o775) } } @@ -139,10 +143,11 @@ impl FileManager { .unwrap_or(false) { // move images that are already reviewed to the already_reviewed bucket - return safe_rename( + return rename_with_create_dir_all( &img.full_path, &img.get_destination_path(&ReviewScore::AlreadyReviewed) .expect("failed to get destination path"), + 0o775, ) // if the image was moved successfully, it shouldn't be reviewed // anymore diff --git a/src/fsops.rs b/src/fsops.rs index 5c34436..95fd9ca 100644 --- a/src/fsops.rs +++ b/src/fsops.rs @@ -1,4 +1,5 @@ use std::fs; +use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use anyhow::{anyhow, Context, Result}; @@ -17,7 +18,7 @@ pub fn have_equal_contents(source: &str, destination: &str) -> Result { Ok(fs::read(source)? == fs::read(destination)?) } -pub fn safe_rename(source: &str, destination: &str) -> Result<()> { +pub fn rename_with_create_dir_all(source: &str, destination: &str, mode: u32) -> Result<()> { let destination_folder = Path::new(destination) .parent() .ok_or_else(|| anyhow!("Failed to get parent dir"))?; @@ -27,6 +28,8 @@ pub fn safe_rename(source: &str, destination: &str) -> Result<()> { &destination_folder.display() ) })?; + chmod(destination_folder.to_str().unwrap(), mode)?; + println!("Moving photo from {} to {}", source, destination); fs::rename(source, destination) .with_context(|| format!("Failed to move photo from {} to {}", source, destination)) @@ -62,3 +65,9 @@ pub fn get_unique_filepath(file_path: &str) -> Result { file_path )) } + +pub fn chmod(file_path: &str, mode: u32) -> Result<()> { + let mut perms = fs::metadata(&file_path)?.permissions(); + perms.set_mode(mode); + fs::set_permissions(file_path, perms).map_err(|e| e.into()) +}