From 01b6dc89deddbf638e587c870c453bca6b6bed1f Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 27 Oct 2021 15:58:31 +0200 Subject: [PATCH 1/4] Check the format with Github Action (#126) * Add cargo fmt to the CI Use a job to check the format of the sourcecode with `cargo fmt` command. The new step does not change any code, but returns an error when `check` does not pass with 0 issues. --- .github/workflows/build.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4f6592f..aa0f697 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -274,3 +274,26 @@ jobs: # with: # name: 'ouch-x86_64-pc-windows-gnu' # path: target\x86_64-pc-windows-gnu\release\ouch.exe + + fmt: + name: Check sourcecode format + runs-on: ubuntu-latest + + steps: + - name: Checkout sources + uses: actions/checkout@v2 + + - name: Install toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: nightly + target: x86_64-unknown-linux-musl + components: rustfmt + override: true + + - name: Check format with cargo fmt + uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check From 09b050d836596fb6f0ae3e0b2f796812d75b359b Mon Sep 17 00:00:00 2001 From: Gabriel Simonetto Date: Thu, 14 Oct 2021 20:10:07 -0300 Subject: [PATCH 2/4] 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}; From 6c6f721f352e708f199f6aad6f8d40aaada2db9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 29 Oct 2021 17:27:48 -0300 Subject: [PATCH 3/4] Solved FinalError compile errors --- src/archive/tar.rs | 14 ++++---------- src/error.rs | 6 ++++++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/archive/tar.rs b/src/archive/tar.rs index 0536841..e92c31d 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -13,7 +13,7 @@ use walkdir::WalkDir; use crate::{ error::FinalError, info, oof, - utils::{self, to_utf, Bytes}, + utils::{self, Bytes}, }; pub fn unpack_archive(reader: Box, output_folder: &Path, flags: &oof::Flags) -> crate::Result> { @@ -60,17 +60,11 @@ where builder.append_dir(path, path)?; } else { let mut file = fs::File::open(path)?; - 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))) + FinalError::with_title("Could not create archive") + .detail("Unexpected error while trying to read file") .detail(format!("Error: {}.", err)) + .into_owned() })?; } } diff --git a/src/error.rs b/src/error.rs index 3792bfb..b84220a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -205,3 +205,9 @@ impl From for Error { Self::OofError(err) } } + +impl From for Error { + fn from(err: FinalError) -> Self { + Self::Custom { reason: err } + } +} From d1c905cda489275afc8ba13da9a1da5fab6af705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 29 Oct 2021 17:35:53 -0300 Subject: [PATCH 4/4] Improving permission denied error message --- src/error.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index b84220a..090c2ec 100644 --- a/src/error.rs +++ b/src/error.rs @@ -21,7 +21,7 @@ pub enum Error { FileNotFound(PathBuf), AlreadyExists, InvalidZipArchive(&'static str), - PermissionDenied, + PermissionDenied { error_title: String }, UnsupportedZipArchive(&'static str), InternalError, OofError(oof::OofError), @@ -156,7 +156,9 @@ impl fmt::Display for Error { Error::UnknownExtensionError(_) => todo!(), Error::AlreadyExists => todo!(), Error::InvalidZipArchive(_) => todo!(), - Error::PermissionDenied => todo!(), + Error::PermissionDenied { error_title } => { + FinalError::with_title(error_title).detail("Permission denied").to_owned() + } Error::UnsupportedZipArchive(_) => todo!(), Error::Custom { reason } => reason.clone(), }; @@ -174,8 +176,8 @@ impl Error { impl From for Error { fn from(err: std::io::Error) -> Self { match err.kind() { - std::io::ErrorKind::NotFound => panic!("{}", err), - std::io::ErrorKind::PermissionDenied => Self::PermissionDenied, + std::io::ErrorKind::NotFound => todo!(), + std::io::ErrorKind::PermissionDenied => Self::PermissionDenied { error_title: err.to_string() }, std::io::ErrorKind::AlreadyExists => Self::AlreadyExists, _other => Self::IoError { reason: err.to_string() }, }