From 09b050d836596fb6f0ae3e0b2f796812d75b359b Mon Sep 17 00:00:00 2001 From: Gabriel Simonetto Date: Thu, 14 Oct 2021 20:10:07 -0300 Subject: [PATCH] Introduce fs_err as a replacement for fs --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/archive/tar.rs | 19 ++++++++++++++++--- src/archive/zip.rs | 7 +++++-- src/cli.rs | 4 +++- src/commands.rs | 2 +- src/utils.rs | 5 +++-- tests/compress_and_decompress.rs | 4 +++- tests/utils.rs | 7 +++---- 9 files changed, 42 insertions(+), 14 deletions(-) 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..4d8320e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ description = "A command-line utility for easily compressing and decompressing f [dependencies] atty = "0.2.14" +fs-err = "2.6.0" lazy_static = "1.4.0" walkdir = "2.3.2" strsim = "0.10.0" diff --git a/src/archive/tar.rs b/src/archive/tar.rs index a5fa6f0..0536841 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -1,17 +1,19 @@ //! Contains Tar-specific building and unpacking functions use std::{ - env, fs, + env, io::prelude::*, path::{Path, PathBuf}, }; +use fs_err as fs; use tar; use walkdir::WalkDir; use crate::{ + error::FinalError, info, oof, - utils::{self, Bytes}, + utils::{self, to_utf, Bytes}, }; pub fn unpack_archive(reader: Box, output_folder: &Path, flags: &oof::Flags) -> crate::Result> { @@ -58,7 +60,18 @@ where builder.append_dir(path, path)?; } else { let mut file = fs::File::open(path)?; - builder.append_file(path, &mut file)?; + dbg!(&path); + dbg!(&file); + dbg!(&entry); + dbg!(&previous_location); + dbg!(&filename); + + // builder.append_file(path, file.file_mut())?; + builder.append_file(path, file.file_mut()).map_err(|err| { + FinalError::with_title(format!("Could not create archive '{}'", to_utf(path.clone()))) // output_path == writer? da + .detail(format!("Unexpected error while trying to read file '{}'", to_utf(output_path))) + .detail(format!("Error: {}.", err)) + })?; } } env::set_current_dir(previous_location)?; diff --git a/src/archive/zip.rs b/src/archive/zip.rs index be47ea8..1b07c20 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -1,11 +1,13 @@ //! Contains Zip-specific building and unpacking functions use std::{ - env, fs, + env, io::{self, prelude::*}, path::{Path, PathBuf}, }; +use fs_err as fs; + use walkdir::WalkDir; use zip::{self, read::ZipFile, ZipArchive}; @@ -121,10 +123,11 @@ fn check_for_comments(file: &ZipFile) { #[cfg(unix)] fn __unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()> { + use std::fs::Permissions; 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..b97338c 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::{ diff --git a/src/utils.rs b/src/utils.rs index a03d227..6c2b214 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,15 +1,16 @@ use std::{ cmp, env, ffi::OsStr, - fs::{self, ReadDir}, path::{Path, PathBuf}, }; +use fs_err as fs; + 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(); + let is_empty = |mut rd: std::fs::ReadDir| rd.next().is_none(); dir_path.read_dir().ok().map(is_empty).unwrap_or_default() } diff --git a/tests/compress_and_decompress.rs b/tests/compress_and_decompress.rs index 22b99fc..3879e2b 100644 --- a/tests/compress_and_decompress.rs +++ b/tests/compress_and_decompress.rs @@ -1,12 +1,14 @@ mod utils; use std::{ - env, fs, + env, io::prelude::*, path::{Path, PathBuf}, time::Duration, }; +use fs_err as fs; + use ouch::{cli::Command, commands::run, oof}; use rand::{rngs::SmallRng, RngCore, SeedableRng}; use tempfile::NamedTempFile; diff --git a/tests/utils.rs b/tests/utils.rs index 3ab9831..60d1ea3 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -2,10 +2,9 @@ #![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};