aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/tar.test6
-rw-r--r--toys/posix/tar.c6
2 files changed, 10 insertions, 2 deletions
diff --git a/tests/tar.test b/tests/tar.test
index dbe28d78..4b2f2120 100644
--- a/tests/tar.test
+++ b/tests/tar.test
@@ -274,6 +274,12 @@ testcmd 'exclude' '--exclude skip -cvf tar.tar folder && echo yes' \
'folder/\nyes\n' '' ''
rm -rf folder tar.tar
+mkdir -p one/two; echo hello > one/two/three; tar czf test.tar one/two/three
+rm one/two/three; mkdir one/two/three
+testcmd 'replace dir with file' '-xf test.tar && cat one/two/three' \
+ 'hello\n' '' ''
+rm -rf one test.tar
+
if false
then
diff --git a/toys/posix/tar.c b/toys/posix/tar.c
index 66547613..f31640c5 100644
--- a/toys/posix/tar.c
+++ b/toys/posix/tar.c
@@ -485,8 +485,10 @@ static void extract_to_disk(void)
return perror_msg(":%s: can't mkdir", name);
// remove old file, if exists
- if (!FLAG(k) && !S_ISDIR(ala) && unlink(name) && errno!=ENOENT)
- return perror_msg("can't remove: %s", name);
+ if (!FLAG(k) && !S_ISDIR(ala) && unlink(name)) {
+ if (errno==EISDIR && !rmdir(name));
+ else if (errno!=ENOENT) return perror_msg("can't remove: %s", name);
+ }
if (S_ISREG(ala)) {
// hardlink?