Fix non-archives overwriting files without asking and error-ing on dirs

This commit is contained in:
Spyros Roum 2021-11-02 10:58:58 +02:00
parent ebe3918478
commit 70c81ed8a4

View File

@ -326,8 +326,22 @@ 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 mut writer = match fs::OpenOptions::new().write(true).create_new(true).open(&output_path) {
let mut writer = fs::File::create(&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)?; io::copy(&mut reader, &mut writer)?;
files_unpacked = vec![output_path]; files_unpacked = vec![output_path];