diff -urN tar-1.26/src/common.h tar-1.26new/src/common.h --- tar-1.26/src/common.h 2017-01-24 09:43:08.638550577 +0100 +++ tar-1.26new/src/common.h 2017-01-23 10:58:19.675411746 +0100 @@ -598,6 +598,7 @@ char *normalize_filename (const char *name); void replace_prefix (char **pname, const char *samp, size_t slen, const char *repl, size_t rlen); +char *tar_savedir (const char *name, int must_exist); typedef struct namebuf *namebuf_t; namebuf_t namebuf_create (const char *dir); diff -urN tar-1.26/src/incremen.c tar-1.26new/src/incremen.c --- tar-1.26/src/incremen.c 2011-02-16 23:12:16.000000000 +0100 +++ tar-1.26new/src/incremen.c 2017-01-23 10:58:19.676411754 +0100 @@ -1580,7 +1580,7 @@ if (!is_dumpdir (¤t_stat_info)) return false; - current_dir = savedir (directory_name); + current_dir = tar_savedir (directory_name, 0); if (!current_dir) /* The directory doesn't exist now. It'll be created. In any diff -urN tar-1.26/src/misc.c tar-1.26new/src/misc.c --- tar-1.26/src/misc.c 2011-02-16 23:12:16.000000000 +0100 +++ tar-1.26new/src/misc.c 2017-01-23 10:58:19.677411762 +0100 @@ -483,7 +483,7 @@ case RECURSIVE_REMOVE_OPTION: { - char *directory = savedir (file_name); + char *directory = tar_savedir (file_name, 0); char const *entry; size_t entrylen; @@ -945,3 +945,31 @@ strcpy (buf->buffer + buf->dir_length, name); return buf->buffer; } + +/* Return the filenames in directory NAME, relative to the chdir_fd. + If the directory does not exist, report error if MUST_EXIST is + true. + + Return NULL on errors. +*/ +char * +tar_savedir (const char *name, int must_exist) +{ + char *ret = NULL; + DIR *dir = NULL; + int fd = openat (chdir_fd, name, open_read_flags | O_DIRECTORY); + if (fd < 0) + { + if (!must_exist && errno == ENOENT) + return NULL; + open_error (name); + } + else if (! ((dir = fdopendir (fd)) + && (ret = streamsavedir (dir)))) + savedir_error (name); + + if (dir ? closedir (dir) != 0 : 0 <= fd && close (fd) != 0) + savedir_error (name); + + return ret; +} diff -urN tar-1.26/src/update.c tar-1.26new/src/update.c --- tar-1.26/src/update.c 2017-01-24 09:43:08.620550423 +0100 +++ tar-1.26new/src/update.c 2017-01-23 12:29:20.410943374 +0100 @@ -144,16 +144,8 @@ { if (S_ISDIR (s.st_mode)) { - char *p, *dirp; - DIR *stream; - int fd = openat (chdir_fd, name->name, - open_read_flags | O_DIRECTORY); - if (fd < 0) - open_error (name->name); - else if (! ((stream = fdopendir (fd)) - && (dirp = streamsavedir (stream)))) - savedir_error (name->name); - else + char *p, *dirp = tar_savedir (name->name, 1); + if (dirp) { namebuf_t nbuf = namebuf_create (name->name); @@ -166,11 +158,6 @@ remname (name); } - - if (stream - ? closedir (stream) != 0 - : 0 <= fd && close (fd) != 0) - savedir_error (name->name); } else if (tar_timespec_cmp (get_stat_mtime (&s), current_stat_info.mtime) diff -urN tar-1.26/tests/incr07.at tar-1.26new/tests/incr07.at --- tar-1.26/tests/incr07.at 1970-01-01 01:00:00.000000000 +0100 +++ tar-1.26new/tests/incr07.at 2017-01-24 09:39:34.305615960 +0100 @@ -0,0 +1,112 @@ +# Process this file with autom4te to create testsuite. -*- Autotest -*- +# Test suite for GNU tar. +# Copyright 2009, 2013 Free Software Foundation, Inc. +# +# GNU tar is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# GNU tar is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +AT_SETUP([incremental restores with -C]) +AT_KEYWORDS([incremental extract incr07]) + +# Tar 1.26 had problems extracting from incremental restores when given +# the -C option. The code in incremen.c:try_purge_directory and +# misc.c:remove_any_file was using savedir(), which ignored eventual changes +# in the current working directory and caused the malfunctioning. +# +# The problem was reported by Piotr Rotter on 2013-03-22. +# +# This testcase is based on scripts provided by Piotr Rotter and Nathan +# Stratton Treadway. +# +# References: <514C8F56.90900@active24.pl>, +# http://lists.gnu.org/archive/html/bug-tar/2013-03/msg00036.html, +# <20130326181922.GZ3732@shire.ontko.com>, +# http://lists.gnu.org/archive/html/bug-tar/2013-03/msg00042.html, +# <20130327051828.GA3732@shire.ontko.com>, +# http://lists.gnu.org/archive/html/bug-tar/2013-03/msg00043.html, +# <20130327054957.GB3732@shire.ontko.com>, +# http://lists.gnu.org/archive/html/bug-tar/2013-03/msg00044.html + +AT_TAR_CHECK([ +mkdir dirA +echo 'a' > dirA/a +echo 'a' > dirA/b + +decho C0 +tar -g test.snar -vcf test.0.tar dirA + +echo 'a' > dirA/c +decho C1 +tar -g test.snar -vcf test.1.tar dirA + +rm -f dirA/a +decho C2 +tar -g test.snar -vcf test.2.tar dirA + +mkdir ext +rm -rf dirA + +decho E0 +tar -g test.snar -vxf test.0.tar -C ext/ +decho E1 +tar -g test.snar -vxf test.1.tar -C ext/ + +decho E2 +tar -g test.snar -vxf test.2.tar -C ext/ + +mkdir ext/dirA/dirB +touch ext/dirA/dirB/file + +decho E3 +tar -g test.snar -vxf test.2.tar -C ext/ + +echo FIN +test -d dirA && echo >&2 "toplevel dirA exists" +exit 0 +], +[0], +[C0 +dirA/ +dirA/a +dirA/b +C1 +dirA/ +dirA/c +C2 +dirA/ +E0 +dirA/ +dirA/a +dirA/b +E1 +dirA/ +dirA/c +E2 +dirA/ +tar: Deleting `dirA/a' +E3 +dirA/ +tar: Deleting `dirA/dirB' +FIN +], +[C0 +tar: dirA: Directory is new +C1 +C2 +E0 +E1 +E2 +E3 +],[],[],[gnu, oldgnu, posix]) + +AT_CLEANUP diff -urN tar-1.26/tests/Makefile.am tar-1.26new/tests/Makefile.am --- tar-1.26/tests/Makefile.am 2017-01-24 09:43:08.656550731 +0100 +++ tar-1.26new/tests/Makefile.am 2017-01-23 10:58:19.678411770 +0100 @@ -98,6 +98,7 @@ incr04.at\ incr05.at\ incr06.at\ + incr07.at\ indexfile.at\ ignfail.at\ label01.at\ diff -urN tar-1.26/tests/testsuite.at tar-1.26new/tests/testsuite.at --- tar-1.26/tests/testsuite.at 2017-01-24 09:43:08.656550731 +0100 +++ tar-1.26new/tests/testsuite.at 2017-01-23 10:58:19.678411770 +0100 @@ -259,6 +259,7 @@ m4_include([incr04.at]) m4_include([incr05.at]) m4_include([incr06.at]) +m4_include([incr07.at]) m4_include([filerem01.at]) m4_include([filerem02.at]) diff -urN tar-1.26/THANKS tar-1.26new/THANKS --- tar-1.26/THANKS 2017-01-24 09:43:08.636550560 +0100 +++ tar-1.26new/THANKS 2017-01-23 10:58:19.674411738 +0100 @@ -397,6 +397,7 @@ Philippe Defert defert@cern.ch Piercarlo Grandi piercarl@sabi.demon.co.uk Pierce Cantrell cantrell@ee.tamu.edu +Piotr Rotter piotr.rotter@active24.pl R. Kent Dybvig dyb@cadence.bloomington.in.us R. Scott Butler butler@prism.es.dupont.com Rainer Orth ro@TechFak.Uni-Bielefeld.DE