Skip to content

Commit

Permalink
feat: set correct file permissions and add chmod helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandervantrijffel committed Aug 5, 2023
1 parent c5fe6e5 commit 6873dc0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/file_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<()> {
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/fsops.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Context, Result};
Expand All @@ -17,7 +18,7 @@ pub fn have_equal_contents(source: &str, destination: &str) -> Result<bool> {
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"))?;
Expand All @@ -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))
Expand Down Expand Up @@ -62,3 +65,9 @@ pub fn get_unique_filepath(file_path: &str) -> Result<String> {
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())
}

0 comments on commit 6873dc0

Please sign in to comment.