diff --git a/Cargo.lock b/Cargo.lock index cc1dcff..cf92250 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -117,6 +117,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "fs-err" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ebd3504ad6116843b8375ad70df74e7bfe83cac77a1f3fe73200c844d43bfe0" + [[package]] name = "getrandom" version = "0.2.3" @@ -206,6 +212,7 @@ dependencies = [ "atty", "bzip2", "flate2", + "fs-err", "infer", "lazy_static", "libc", diff --git a/Cargo.toml b/Cargo.toml index f1d3a9f..32e22f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ description = "A command-line utility for easily compressing and decompressing f # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +fs-err = "2.6.0" atty = "0.2.14" lazy_static = "1.4.0" walkdir = "2.3.2" diff --git a/src/archive/tar.rs b/src/archive/tar.rs index a5fa6f0..c97c41a 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -1,7 +1,8 @@ //! Contains Tar-specific building and unpacking functions use std::{ - env, fs, + env, + fs::File, io::prelude::*, path::{Path, PathBuf}, }; @@ -57,7 +58,7 @@ where if path.is_dir() { builder.append_dir(path, path)?; } else { - let mut file = fs::File::open(path)?; + let mut file = File::open(path)?; builder.append_file(path, &mut file)?; } } diff --git a/src/archive/zip.rs b/src/archive/zip.rs index be47ea8..793ab8a 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -1,11 +1,14 @@ //! Contains Zip-specific building and unpacking functions use std::{ - env, fs, + env, + fs::Permissions, io::{self, prelude::*}, path::{Path, PathBuf}, }; +use fs_err as fs; + use walkdir::WalkDir; use zip::{self, read::ZipFile, ZipArchive}; @@ -124,7 +127,7 @@ fn __unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()> use std::os::unix::fs::PermissionsExt; if let Some(mode) = file.unix_mode() { - fs::set_permissions(file_path, fs::Permissions::from_mode(mode))?; + fs::set_permissions(file_path, Permissions::from_mode(mode))?; } Ok(()) diff --git a/src/cli.rs b/src/cli.rs index 9ff64c6..8d93c01 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -9,6 +9,8 @@ use std::{ vec::Vec, }; +use fs_err as fs; + use strsim::normalized_damerau_levenshtein; use crate::{arg_flag, flag, oof, Error}; @@ -73,7 +75,7 @@ fn is_typo(path: impl AsRef) -> bool { } fn canonicalize(path: impl AsRef) -> crate::Result { - match std::fs::canonicalize(&path.as_ref()) { + match fs::canonicalize(&path.as_ref()) { Ok(abs_path) => Ok(abs_path), Err(io_err) => { if !path.as_ref().exists() { diff --git a/src/commands.rs b/src/commands.rs index bf278b6..40a6e8d 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -3,11 +3,11 @@ //! Also, where correctly call functions based on the detected `Command`. use std::{ - fs, io::{self, BufReader, BufWriter, Read, Write}, path::{Path, PathBuf}, }; +use fs_err as fs; use utils::colors; use crate::{ @@ -29,7 +29,7 @@ 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); + let is_non_empty = || !dir_is_empty(path); path.is_dir().then(is_non_empty).unwrap_or_default() }; diff --git a/src/utils.rs b/src/utils.rs index a03d227..2298f2b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,10 +1,12 @@ use std::{ cmp, env, ffi::OsStr, - fs::{self, ReadDir}, + fs::ReadDir, path::{Path, PathBuf}, }; +use fs_err as fs; + use crate::{dialogs::Confirmation, info, oof}; /// Checks if the given path represents an empty directory. diff --git a/tests/utils.rs b/tests/utils.rs index 3ab9831..20d0133 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -2,15 +2,15 @@ #![allow(dead_code)] -use std::{ - fs, - path::{Path, PathBuf}, -}; +use std::path::{Path, PathBuf}; +use fs_err as fs; use ouch::{cli::Command, commands::run, oof}; +use rand::{distributions::Alphanumeric, prelude::SmallRng, Rng, SeedableRng}; pub fn create_empty_dir(at: &Path, filename: &str) -> PathBuf { let dirname = Path::new(filename); + let full_path = at.join(dirname); fs::create_dir(&full_path).expect("Failed to create an empty directory"); @@ -19,7 +19,11 @@ pub fn create_empty_dir(at: &Path, filename: &str) -> PathBuf { } pub fn compress_files(at: &Path, paths_to_compress: &[PathBuf], format: &str) -> PathBuf { - let archive_path = String::from("archive.") + format; + let rng = SmallRng::from_entropy(); + + let s: String = rng.sample_iter(&Alphanumeric).take(7).map(char::from).collect(); + + let archive_path = format!("archive{}{}", s, format); let archive_path = at.join(archive_path); let command = Command::Compress { files: paths_to_compress.to_vec(), output_path: archive_path.to_path_buf() };