Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decompression overwritting files without asking and failing on directories #141

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/archive/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ pub fn unpack_archive(
continue;
}

if file_path.is_dir() {
// We can't just use `fs::File::create(&file_path)` because it would return io::ErrorKind::IsADirectory
// ToDo: Maybe we should emphasise that `file_path` is a directory and everything inside it will be gone?
fs::remove_dir_all(&file_path)?;
fs::File::create(&file_path)?;
}

file.unpack_in(output_folder)?;

info!("{:?} extracted. ({})", output_folder.join(file.path()?), Bytes::new(file.size()));
Expand Down
7 changes: 7 additions & 0 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ where
continue;
}

if file_path.is_dir() {
// We can't just use `fs::File::create(&file_path)` because it would return io::ErrorKind::IsADirectory
// ToDo: Maybe we should emphasise that `file_path` is a directory and everything inside it will be gone?
fs::remove_dir_all(&file_path)?;
fs::File::create(&file_path)?;
}

check_for_comments(&file);

match (&*file.name()).ends_with('/') {
Expand Down
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)),
};
marcospb19 marked this conversation as resolved.
Show resolved Hide resolved

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