Introduce fs_err as a replacement for fs

This commit is contained in:
Gabriel Simonetto 2021-10-14 20:10:07 -03:00
parent f46ff3c26e
commit 09b050d836
9 changed files with 42 additions and 14 deletions

7
Cargo.lock generated
View File

@ -117,6 +117,12 @@ dependencies = [
"miniz_oxide", "miniz_oxide",
] ]
[[package]]
name = "fs-err"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ebd3504ad6116843b8375ad70df74e7bfe83cac77a1f3fe73200c844d43bfe0"
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.3" version = "0.2.3"
@ -206,6 +212,7 @@ dependencies = [
"atty", "atty",
"bzip2", "bzip2",
"flate2", "flate2",
"fs-err",
"infer", "infer",
"lazy_static", "lazy_static",
"libc", "libc",

View File

@ -14,6 +14,7 @@ description = "A command-line utility for easily compressing and decompressing f
[dependencies] [dependencies]
atty = "0.2.14" atty = "0.2.14"
fs-err = "2.6.0"
lazy_static = "1.4.0" lazy_static = "1.4.0"
walkdir = "2.3.2" walkdir = "2.3.2"
strsim = "0.10.0" strsim = "0.10.0"

View File

@ -1,17 +1,19 @@
//! Contains Tar-specific building and unpacking functions //! Contains Tar-specific building and unpacking functions
use std::{ use std::{
env, fs, env,
io::prelude::*, io::prelude::*,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use fs_err as fs;
use tar; use tar;
use walkdir::WalkDir; use walkdir::WalkDir;
use crate::{ use crate::{
error::FinalError,
info, oof, info, oof,
utils::{self, Bytes}, utils::{self, to_utf, Bytes},
}; };
pub fn unpack_archive(reader: Box<dyn Read>, output_folder: &Path, flags: &oof::Flags) -> crate::Result<Vec<PathBuf>> { pub fn unpack_archive(reader: Box<dyn Read>, output_folder: &Path, flags: &oof::Flags) -> crate::Result<Vec<PathBuf>> {
@ -58,7 +60,18 @@ where
builder.append_dir(path, path)?; builder.append_dir(path, path)?;
} else { } else {
let mut file = fs::File::open(path)?; 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)?; env::set_current_dir(previous_location)?;

View File

@ -1,11 +1,13 @@
//! Contains Zip-specific building and unpacking functions //! Contains Zip-specific building and unpacking functions
use std::{ use std::{
env, fs, env,
io::{self, prelude::*}, io::{self, prelude::*},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use fs_err as fs;
use walkdir::WalkDir; use walkdir::WalkDir;
use zip::{self, read::ZipFile, ZipArchive}; use zip::{self, read::ZipFile, ZipArchive};
@ -121,10 +123,11 @@ fn check_for_comments(file: &ZipFile) {
#[cfg(unix)] #[cfg(unix)]
fn __unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()> { fn __unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()> {
use std::fs::Permissions;
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode() { 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(()) Ok(())

View File

@ -9,6 +9,8 @@ use std::{
vec::Vec, vec::Vec,
}; };
use fs_err as fs;
use strsim::normalized_damerau_levenshtein; use strsim::normalized_damerau_levenshtein;
use crate::{arg_flag, flag, oof, Error}; use crate::{arg_flag, flag, oof, Error};
@ -73,7 +75,7 @@ fn is_typo(path: impl AsRef<Path>) -> bool {
} }
fn canonicalize(path: impl AsRef<Path>) -> crate::Result<PathBuf> { fn canonicalize(path: impl AsRef<Path>) -> crate::Result<PathBuf> {
match std::fs::canonicalize(&path.as_ref()) { match fs::canonicalize(&path.as_ref()) {
Ok(abs_path) => Ok(abs_path), Ok(abs_path) => Ok(abs_path),
Err(io_err) => { Err(io_err) => {
if !path.as_ref().exists() { if !path.as_ref().exists() {

View File

@ -3,11 +3,11 @@
//! Also, where correctly call functions based on the detected `Command`. //! Also, where correctly call functions based on the detected `Command`.
use std::{ use std::{
fs,
io::{self, BufReader, BufWriter, Read, Write}, io::{self, BufReader, BufWriter, Read, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use fs_err as fs;
use utils::colors; use utils::colors;
use crate::{ use crate::{

View File

@ -1,15 +1,16 @@
use std::{ use std::{
cmp, env, cmp, env,
ffi::OsStr, ffi::OsStr,
fs::{self, ReadDir},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use fs_err as fs;
use crate::{dialogs::Confirmation, info, oof}; use crate::{dialogs::Confirmation, info, oof};
/// Checks if the given path represents an empty directory. /// Checks if the given path represents an empty directory.
pub fn dir_is_empty(dir_path: &Path) -> bool { 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() dir_path.read_dir().ok().map(is_empty).unwrap_or_default()
} }

View File

@ -1,12 +1,14 @@
mod utils; mod utils;
use std::{ use std::{
env, fs, env,
io::prelude::*, io::prelude::*,
path::{Path, PathBuf}, path::{Path, PathBuf},
time::Duration, time::Duration,
}; };
use fs_err as fs;
use ouch::{cli::Command, commands::run, oof}; use ouch::{cli::Command, commands::run, oof};
use rand::{rngs::SmallRng, RngCore, SeedableRng}; use rand::{rngs::SmallRng, RngCore, SeedableRng};
use tempfile::NamedTempFile; use tempfile::NamedTempFile;

View File

@ -2,10 +2,9 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::{ use std::path::{Path, PathBuf};
fs,
path::{Path, PathBuf}, use fs_err as fs;
};
use ouch::{cli::Command, commands::run, oof}; use ouch::{cli::Command, commands::run, oof};