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

Update overwrite to also overwrite hard links #319

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/entry.rs
Expand Up @@ -527,17 +527,26 @@ impl<'a> EntryFields<'a> {
}
None => src.into_owned(),
};
fs::hard_link(&link_src, dst).map_err(|err| {
Error::new(
err.kind(),
format!(
"{} when hard linking {} to {}",
err,
link_src.display(),
dst.display()
),
)
})?;
fs::hard_link(&link_src, dst)
.or_else(|err_io| {
if err_io.kind() == io::ErrorKind::AlreadyExists && self.overwrite {
// remove dest and try once more
std::fs::remove_file(dst).and_then(|()| fs::hard_link(&link_src, dst))
} else {
Err(err_io)
}
})
.map_err(|err| {
Error::new(
err.kind(),
format!(
"{} when hard linking {} to {}",
err,
link_src.display(),
dst.display()
),
)
})?;
} else {
symlink(&src, dst)
.or_else(|err_io| {
Expand Down
36 changes: 36 additions & 0 deletions tests/all.rs
Expand Up @@ -370,6 +370,42 @@ fn extracting_duplicate_link_succeed() {
t!(ar.unpack(td.path()));
}

#[test]
#[cfg(unix)]
fn extracting_duplicate_hard_link_fail() {
let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
let path_present = td.path().join("link");
t!(std::fs::write(path_present, "hello"));

let rdr = Cursor::new(tar!("hard_link.tar"));
let mut ar = Archive::new(rdr);
ar.set_overwrite(false);
if let Err(err) = ar.unpack(td.path()) {
if err.kind() == std::io::ErrorKind::AlreadyExists {
// as expected with overwrite false
return;
}
panic!("unexpected error: {:?}", err);
}
panic!(
"unpack() should have returned an error of kind {:?}, returned Ok",
std::io::ErrorKind::AlreadyExists
)
}

#[test]
#[cfg(unix)]
fn extracting_duplicate_hard_link_succeed() {
let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
let path_present = td.path().join("link");
t!(std::fs::write(path_present, "hello"));

let rdr = Cursor::new(tar!("hard_link.tar"));
let mut ar = Archive::new(rdr);
ar.set_overwrite(true);
t!(ar.unpack(td.path()));
}

#[test]
#[cfg(all(unix, feature = "xattr"))]
fn xattrs() {
Expand Down
Binary file added tests/archives/hard_link.tar
Binary file not shown.