mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 20:15:27 +00:00
Merge pull request #190 from SpyrosRoum/fix-decompressing-failing-to-create-dir
Fix not overwriting files/dirs when trying to create a directory
This commit is contained in:
commit
180f63209a
@ -31,17 +31,11 @@ pub fn unpack_archive(
|
|||||||
let mut file = file?;
|
let mut file = file?;
|
||||||
|
|
||||||
let file_path = output_folder.join(file.path()?);
|
let file_path = output_folder.join(file.path()?);
|
||||||
if file_path.exists() && !utils::user_wants_to_overwrite(&file_path, question_policy)? {
|
if !utils::clear_path(&file_path, question_policy)? {
|
||||||
|
// User doesn't want to overwrite
|
||||||
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()));
|
||||||
|
@ -15,8 +15,8 @@ use crate::{
|
|||||||
info,
|
info,
|
||||||
list::FileInArchive,
|
list::FileInArchive,
|
||||||
utils::{
|
utils::{
|
||||||
cd_into_same_dir_as, concatenate_os_str_list, dir_is_empty, get_invalid_utf8_paths, strip_cur_dir, to_utf,
|
cd_into_same_dir_as, clear_path, concatenate_os_str_list, dir_is_empty, get_invalid_utf8_paths, strip_cur_dir,
|
||||||
user_wants_to_overwrite, Bytes,
|
to_utf, Bytes,
|
||||||
},
|
},
|
||||||
QuestionPolicy,
|
QuestionPolicy,
|
||||||
};
|
};
|
||||||
@ -39,17 +39,11 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
let file_path = into.join(file_path);
|
let file_path = into.join(file_path);
|
||||||
if file_path.exists() && !user_wants_to_overwrite(&file_path, question_policy)? {
|
if !clear_path(&file_path, question_policy)? {
|
||||||
|
// User doesn't want to overwrite
|
||||||
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('/') {
|
||||||
|
@ -342,6 +342,10 @@ fn decompress_file(
|
|||||||
//
|
//
|
||||||
// Any other Zip decompression done can take up the whole RAM and freeze ouch.
|
// Any other Zip decompression done can take up the whole RAM and freeze ouch.
|
||||||
if formats.len() == 1 && *formats[0].compression_formats == [Zip] {
|
if formats.len() == 1 && *formats[0].compression_formats == [Zip] {
|
||||||
|
if !utils::clear_path(output_dir, question_policy)? {
|
||||||
|
// User doesn't want to overwrite
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
utils::create_dir_if_non_existent(output_dir)?;
|
utils::create_dir_if_non_existent(output_dir)?;
|
||||||
let zip_archive = zip::ZipArchive::new(reader)?;
|
let zip_archive = zip::ZipArchive::new(reader)?;
|
||||||
let _files = crate::archive::zip::unpack_archive(zip_archive, output_dir, question_policy)?;
|
let _files = crate::archive::zip::unpack_archive(zip_archive, output_dir, question_policy)?;
|
||||||
@ -370,6 +374,10 @@ fn decompress_file(
|
|||||||
reader = chain_reader_decoder(format, reader)?;
|
reader = chain_reader_decoder(format, reader)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !utils::clear_path(&output_path, question_policy)? {
|
||||||
|
// User doesn't want to overwrite
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
utils::create_dir_if_non_existent(output_dir)?;
|
utils::create_dir_if_non_existent(output_dir)?;
|
||||||
|
|
||||||
let files_unpacked;
|
let files_unpacked;
|
||||||
|
@ -9,8 +9,8 @@ use std::{
|
|||||||
|
|
||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
|
|
||||||
use super::to_utf;
|
use super::{to_utf, user_wants_to_overwrite};
|
||||||
use crate::{extension::Extension, info};
|
use crate::{extension::Extension, info, QuestionPolicy};
|
||||||
|
|
||||||
/// Checks if given path points to an empty directory.
|
/// Checks if 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 {
|
||||||
@ -19,6 +19,25 @@ pub fn dir_is_empty(dir_path: &Path) -> bool {
|
|||||||
dir_path.read_dir().map(is_empty).unwrap_or_default()
|
dir_path.read_dir().map(is_empty).unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remove `path` asking the user to overwrite if necessary.
|
||||||
|
///
|
||||||
|
/// * `Ok(true)` means the path is clear,
|
||||||
|
/// * `Ok(false)` means the user doesn't want to overwrite
|
||||||
|
/// * `Err(_)` is an error
|
||||||
|
pub fn clear_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result<bool> {
|
||||||
|
if path.exists() && !user_wants_to_overwrite(path, question_policy)? {
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if path.is_dir() {
|
||||||
|
fs::remove_dir_all(path)?;
|
||||||
|
} else if path.is_file() {
|
||||||
|
fs::remove_file(path)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a directory at the path, if there is nothing there.
|
/// Creates a directory at the path, if there is nothing there.
|
||||||
pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
|
pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
|
@ -9,7 +9,7 @@ mod fs;
|
|||||||
mod question;
|
mod question;
|
||||||
|
|
||||||
pub use formatting::{concatenate_os_str_list, nice_directory_display, strip_cur_dir, to_utf, Bytes};
|
pub use formatting::{concatenate_os_str_list, nice_directory_display, strip_cur_dir, to_utf, Bytes};
|
||||||
pub use fs::{cd_into_same_dir_as, create_dir_if_non_existent, dir_is_empty, try_infer_extension};
|
pub use fs::{cd_into_same_dir_as, clear_path, create_dir_if_non_existent, dir_is_empty, try_infer_extension};
|
||||||
pub use question::{
|
pub use question::{
|
||||||
create_or_ask_overwrite, user_wants_to_continue_decompressing, user_wants_to_overwrite, QuestionPolicy,
|
create_or_ask_overwrite, user_wants_to_continue_decompressing, user_wants_to_overwrite, QuestionPolicy,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user