Skip to content

Commit

Permalink
Check if a folder is being compressed to a non-archive format (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrmiguel authored Oct 7, 2021
1 parent a739b5a commit 14961be
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
8 changes: 2 additions & 6 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use zip::{self, read::ZipFile, ZipArchive};

use crate::{
info, oof,
utils::{self, Bytes},
utils::{self, dir_is_empty, Bytes},
};

use self::utf8::get_invalid_utf8_paths;
Expand Down Expand Up @@ -93,7 +93,7 @@ where
info!("Compressing '{}'.", utils::to_utf(path));

if path.is_dir() {
if dir_is_empty(path)? {
if dir_is_empty(path) {
writer.add_directory(path.to_str().unwrap().to_owned(), options)?;
}
// If a dir has files, the files are responsible for creating them.
Expand All @@ -119,10 +119,6 @@ fn check_for_comments(file: &ZipFile) {
}
}

fn dir_is_empty(dir_path: &Path) -> crate::Result<bool> {
Ok(dir_path.read_dir()?.next().is_none())
}

#[cfg(unix)]
fn __unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()> {
use std::os::unix::fs::PermissionsExt;
Expand Down
17 changes: 14 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,25 @@ use crate::{
self,
CompressionFormat::{self, *},
},
info, oof, utils,
info, oof,
utils::to_utf,
utils::{self, dir_is_empty},
Error,
};

// Used in BufReader and BufWriter to perform less syscalls
const BUFFER_CAPACITY: usize = 1024 * 64;

fn represents_several_files(files: &[PathBuf]) -> bool {
let is_non_empty_dir = |path: &PathBuf| {
let is_non_empty = || !dir_is_empty(&path);

path.is_dir().then(is_non_empty).unwrap_or_default()
};

files.iter().any(is_non_empty_dir) || files.len() > 1
}

pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
match command {
Command::Compress { files, output_path } => {
Expand All @@ -45,7 +56,7 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
return Err(Error::with_reason(reason));
}

if matches!(&formats[0], Bzip | Gzip | Lzma) && files.len() > 1 {
if matches!(&formats[0], Bzip | Gzip | Lzma) && represents_several_files(&files) {
// This piece of code creates a sugestion for compressing multiple files
// It says:
// Change from file.bz.xz
Expand Down Expand Up @@ -205,7 +216,7 @@ fn compress_files(
Zip => {
eprintln!("{yellow}Warning:{reset}", yellow = colors::yellow(), reset = colors::reset());
eprintln!("\tCompressing .zip entirely in memory.");
eprintln!("\tIf the file is too big, your pc might freeze!");
eprintln!("\tIf the file is too big, your PC might freeze!");
eprintln!(
"\tThis is a limitation for formats like '{}'.",
formats.iter().map(|format| format.to_string()).collect::<String>()
Expand Down
11 changes: 7 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum Error {

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Default, PartialEq)]
#[derive(Clone, Debug, Default, PartialEq)]
pub struct FinalError {
title: String,
details: Vec<String>,
Expand Down Expand Up @@ -153,9 +153,12 @@ impl fmt::Display for Error {
Error::CompressionTypo => FinalError::with_title("Possible typo detected")
.hint(format!("Did you mean '{}ouch compress{}'?", magenta(), reset()))
.into_owned(),
_err => {
todo!();
}
Error::UnknownExtensionError(_) => todo!(),
Error::AlreadyExists => todo!(),
Error::InvalidZipArchive(_) => todo!(),
Error::PermissionDenied => todo!(),
Error::UnsupportedZipArchive(_) => todo!(),
Error::Custom { reason } => reason.clone(),
};

write!(f, "{}", err)
Expand Down
9 changes: 8 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use std::{
cmp, env,
ffi::OsStr,
fs,
fs::{self, ReadDir},
path::{Path, PathBuf},
};

use crate::{dialogs::Confirmation, info, oof};

/// Checks if the given path represents an empty directory.
pub fn dir_is_empty(dir_path: &Path) -> bool {
let is_empty = |mut rd: ReadDir| rd.next().is_none();

dir_path.read_dir().ok().map(is_empty).unwrap_or_default()
}

pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
if !path.exists() {
fs::create_dir_all(path)?;
Expand Down

0 comments on commit 14961be

Please sign in to comment.