Skip to content

Commit

Permalink
Fix non-archives overwriting files without asking and error-ing on dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
SpyrosRoum committed Nov 2, 2021
1 parent ebe3918 commit 70c81ed
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,22 @@ fn decompress_file(
Gzip | Bzip | Lzma | Zstd => {
reader = chain_reader_decoder(&formats[0].compression_formats[0], reader)?;

// TODO: improve error treatment
let mut writer = fs::File::create(&output_path)?;
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)),
};

io::copy(&mut reader, &mut writer)?;
files_unpacked = vec![output_path];
Expand Down

0 comments on commit 70c81ed

Please sign in to comment.