mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 03:55:28 +00:00
Merge pull request #141 from SpyrosRoum/fix-decompression-overwritting-files-without-asking
Fix decompression overwritting files without asking and failing on directories
This commit is contained in:
commit
73c1d9a41c
@ -34,6 +34,13 @@ pub fn unpack_archive(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if file_path.is_dir() {
|
||||||
|
// ToDo: Maybe we should emphasise that `file_path` is a directory and everything inside it will be gone?
|
||||||
|
fs::remove_dir_all(&file_path)?;
|
||||||
|
} else if file_path.is_file() {
|
||||||
|
fs::remove_file(&file_path)?;
|
||||||
|
}
|
||||||
|
|
||||||
file.unpack_in(output_folder)?;
|
file.unpack_in(output_folder)?;
|
||||||
|
|
||||||
info!("{:?} extracted. ({})", output_folder.join(file.path()?), Bytes::new(file.size()));
|
info!("{:?} extracted. ({})", output_folder.join(file.path()?), Bytes::new(file.size()));
|
||||||
|
@ -41,6 +41,13 @@ where
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if file_path.is_dir() {
|
||||||
|
// ToDo: Maybe we should emphasise that `file_path` is a directory and everything inside it will be gone?
|
||||||
|
fs::remove_dir_all(&file_path)?;
|
||||||
|
} else if file_path.is_file() {
|
||||||
|
fs::remove_file(&file_path)?;
|
||||||
|
}
|
||||||
|
|
||||||
check_for_comments(&file);
|
check_for_comments(&file);
|
||||||
|
|
||||||
match (&*file.name()).ends_with('/') {
|
match (&*file.name()).ends_with('/') {
|
||||||
|
@ -331,8 +331,12 @@ fn decompress_file(
|
|||||||
Gzip | Bzip | Lzma | Zstd => {
|
Gzip | Bzip | Lzma | Zstd => {
|
||||||
reader = chain_reader_decoder(&formats[0].compression_formats[0], reader)?;
|
reader = chain_reader_decoder(&formats[0].compression_formats[0], reader)?;
|
||||||
|
|
||||||
// TODO: improve error treatment
|
let writer = utils::create_or_ask_overwrite(&output_path, question_policy)?;
|
||||||
let mut writer = fs::File::create(&output_path)?;
|
if writer.is_none() {
|
||||||
|
// Means that the user doesn't want to overwrite
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let mut writer = writer.unwrap();
|
||||||
|
|
||||||
io::copy(&mut reader, &mut writer)?;
|
io::copy(&mut reader, &mut writer)?;
|
||||||
files_unpacked = vec![output_path];
|
files_unpacked = vec![output_path];
|
||||||
|
24
src/utils.rs
24
src/utils.rs
@ -3,13 +3,35 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cmp, env,
|
cmp, env,
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
|
io,
|
||||||
path::Component,
|
path::Component,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
|
|
||||||
use crate::{dialogs::Confirmation, info};
|
use crate::{dialogs::Confirmation, info, Error};
|
||||||
|
|
||||||
|
/// Create the file if it doesn't exist and if it does then ask to overwrite it.
|
||||||
|
/// If the user doesn't want to overwrite then we return [`Ok(None)`]
|
||||||
|
pub fn create_or_ask_overwrite(path: &Path, question_policy: QuestionPolicy) -> Result<Option<fs::File>, Error> {
|
||||||
|
match fs::OpenOptions::new().write(true).create_new(true).open(path) {
|
||||||
|
Ok(w) => Ok(Some(w)),
|
||||||
|
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
|
||||||
|
if user_wants_to_overwrite(path, question_policy)? {
|
||||||
|
if path.is_dir() {
|
||||||
|
// We can't just use `fs::File::create(&path)` because it would return io::ErrorKind::IsADirectory
|
||||||
|
// ToDo: Maybe we should emphasise that `path` is a directory and everything inside it will be gone?
|
||||||
|
fs::remove_dir_all(path)?;
|
||||||
|
}
|
||||||
|
Ok(Some(fs::File::create(path)?))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => Err(Error::from(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Checks given path points to an empty directory.
|
/// Checks given path points to an empty directory.
|
||||||
pub fn dir_is_empty(dir_path: &Path) -> bool {
|
pub fn dir_is_empty(dir_path: &Path) -> bool {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user