Skip to content

Commit

Permalink
Extract function
Browse files Browse the repository at this point in the history
  • Loading branch information
SpyrosRoum committed Nov 2, 2021
1 parent 8ef1b25 commit 547b8c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
22 changes: 6 additions & 16 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,22 +326,12 @@ fn decompress_file(
Gzip | Bzip | Lzma | Zstd => {
reader = chain_reader_decoder(&formats[0].compression_formats[0], reader)?;

let mut writer = match fs::OpenOptions::new().write(true).create_new(true).open(&output_path) {
Ok(w) => w,
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
if utils::user_wants_to_overwrite(&output_path, question_policy)? {
if output_path.is_dir() {
// We can't just use `fs::File::create(&output_path)` because it would return io::ErrorKind::IsADirectory
// ToDo: Maybe we should emphasise that `output_path` is a directory and everything inside it will be gone?
fs::remove_dir_all(&output_path)?;
}
fs::File::create(&output_path)?
} else {
return Ok(());
}
}
Err(e) => return Err(Error::from(e)),
};
let writer = utils::create_or_ask_overwrite(&output_path, question_policy)?;
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)?;
files_unpacked = vec![output_path];
Expand Down
24 changes: 23 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,35 @@
use std::{
cmp, env,
ffi::OsStr,
io,
path::Component,
path::{Path, PathBuf},
};

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 if the given path represents an empty directory.
pub fn dir_is_empty(dir_path: &Path) -> bool {
Expand Down

0 comments on commit 547b8c9

Please sign in to comment.