mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
Merge pull request #94 from ouch-org/issue-56
Introduce fs_err as a replacement for fs
This commit is contained in:
commit
54cd148e0b
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -148,6 +148,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"
|
||||||
@ -284,6 +290,7 @@ dependencies = [
|
|||||||
"bzip2",
|
"bzip2",
|
||||||
"clap",
|
"clap",
|
||||||
"flate2",
|
"flate2",
|
||||||
|
"fs-err",
|
||||||
"infer",
|
"infer",
|
||||||
"libc",
|
"libc",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
@ -15,6 +15,7 @@ description = "A command-line utility for easily compressing and decompressing f
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
clap = "=3.0.0-beta.5" # Keep it pinned while in beta!
|
clap = "=3.0.0-beta.5" # Keep it pinned while in beta!
|
||||||
atty = "0.2.14"
|
atty = "0.2.14"
|
||||||
|
fs-err = "2.6.0"
|
||||||
once_cell = "1.8.0"
|
once_cell = "1.8.0"
|
||||||
walkdir = "2.3.2"
|
walkdir = "2.3.2"
|
||||||
bzip2 = "0.4.3"
|
bzip2 = "0.4.3"
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
//! 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,
|
info,
|
||||||
utils::{self, Bytes, QuestionPolicy},
|
utils::{self, Bytes, QuestionPolicy},
|
||||||
};
|
};
|
||||||
@ -62,7 +64,12 @@ 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)?;
|
builder.append_file(path, file.file_mut()).map_err(|err| {
|
||||||
|
FinalError::with_title("Could not create archive")
|
||||||
|
.detail("Unexpected error while trying to read file")
|
||||||
|
.detail(format!("Error: {}.", err))
|
||||||
|
.into_owned()
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
env::set_current_dir(previous_location)?;
|
env::set_current_dir(previous_location)?;
|
||||||
|
@ -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};
|
||||||
|
|
||||||
@ -126,10 +128,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(())
|
||||||
|
@ -6,6 +6,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use clap::{Parser, ValueHint};
|
use clap::{Parser, ValueHint};
|
||||||
|
use fs_err as fs;
|
||||||
|
|
||||||
pub use crate::utils::QuestionPolicy;
|
pub use crate::utils::QuestionPolicy;
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
@ -73,7 +74,7 @@ impl Opts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
||||||
|
@ -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::{
|
||||||
|
20
src/error.rs
20
src/error.rs
@ -21,7 +21,7 @@ pub enum Error {
|
|||||||
FileNotFound(PathBuf),
|
FileNotFound(PathBuf),
|
||||||
AlreadyExists,
|
AlreadyExists,
|
||||||
InvalidZipArchive(&'static str),
|
InvalidZipArchive(&'static str),
|
||||||
PermissionDenied,
|
PermissionDenied { error_title: String },
|
||||||
UnsupportedZipArchive(&'static str),
|
UnsupportedZipArchive(&'static str),
|
||||||
InternalError,
|
InternalError,
|
||||||
CompressingRootFolder,
|
CompressingRootFolder,
|
||||||
@ -78,6 +78,10 @@ impl FinalError {
|
|||||||
self.hints.push(hint.to_string());
|
self.hints.push(hint.to_string());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn into_owned(&mut self) -> Self {
|
||||||
|
std::mem::take(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
@ -134,7 +138,9 @@ impl fmt::Display for Error {
|
|||||||
Error::UnknownExtensionError(_) => todo!(),
|
Error::UnknownExtensionError(_) => todo!(),
|
||||||
Error::AlreadyExists => todo!(),
|
Error::AlreadyExists => todo!(),
|
||||||
Error::InvalidZipArchive(_) => 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::UnsupportedZipArchive(_) => todo!(),
|
||||||
Error::Custom { reason } => reason.clone(),
|
Error::Custom { reason } => reason.clone(),
|
||||||
};
|
};
|
||||||
@ -152,8 +158,8 @@ impl Error {
|
|||||||
impl From<std::io::Error> for Error {
|
impl From<std::io::Error> for Error {
|
||||||
fn from(err: std::io::Error) -> Self {
|
fn from(err: std::io::Error) -> Self {
|
||||||
match err.kind() {
|
match err.kind() {
|
||||||
std::io::ErrorKind::NotFound => panic!("{}", err),
|
std::io::ErrorKind::NotFound => todo!(),
|
||||||
std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
|
std::io::ErrorKind::PermissionDenied => Self::PermissionDenied { error_title: err.to_string() },
|
||||||
std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
|
std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
|
||||||
_other => Self::IoError { reason: err.to_string() },
|
_other => Self::IoError { reason: err.to_string() },
|
||||||
}
|
}
|
||||||
@ -177,3 +183,9 @@ impl From<walkdir::Error> for Error {
|
|||||||
Self::WalkdirError { reason: err.to_string() }
|
Self::WalkdirError { reason: err.to_string() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<FinalError> for Error {
|
||||||
|
fn from(err: FinalError) -> Self {
|
||||||
|
Self::Custom { reason: err }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cmp, env,
|
cmp, env,
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
fs::{self, ReadDir},
|
|
||||||
path::Component,
|
path::Component,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use fs_err as fs;
|
||||||
|
|
||||||
use crate::{dialogs::Confirmation, info};
|
use crate::{dialogs::Confirmation, info};
|
||||||
|
|
||||||
/// 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().map(is_empty).unwrap_or_default()
|
dir_path.read_dir().map(is_empty).unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
@ -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::{
|
use ouch::{
|
||||||
cli::{Opts, QuestionPolicy, Subcommand},
|
cli::{Opts, QuestionPolicy, Subcommand},
|
||||||
commands::run,
|
commands::run,
|
||||||
|
@ -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::{
|
use ouch::{
|
||||||
cli::{Opts, QuestionPolicy, Subcommand},
|
cli::{Opts, QuestionPolicy, Subcommand},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user