Use mkdtempat instead of mkdtemp

This fixes an interaction of -C with incremental 'X'.
Problem reported by Pavel Cahyna.
* gnulib.modules: Remove mkdtemp.
Add tempname, since our mkdtempat uses it.
* lib/mkdtempat.c, lib/mkdtempat.h: New files.
* lib/Makefile.am (noinst_HEADERS): Add mkdtempat.h.
(libtar_a_SOURCES): Add mkdtempat.c.
* src/incremen.c: Include mkdtempat.h.
(purge_directory): Use mkdtempat, not mkdtemp.
This commit is contained in:
Paul Eggert
2026-04-12 23:58:22 -07:00
parent 0470c109c0
commit d479b2cc91
5 changed files with 53 additions and 2 deletions

View File

@@ -30,6 +30,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/gnu -I../ -I../gnu
AM_CFLAGS = $(GNULIB_WARN_CFLAGS) $(WERROR_CFLAGS)
noinst_HEADERS = \
mkdtempat.h\
paxlib.h\
rmt.h\
system.h\
@@ -37,6 +38,7 @@ noinst_HEADERS = \
xattr-at.h
libtar_a_SOURCES = \
mkdtempat.c\
paxerror.c paxexit-status.c paxlib.h paxnames.c \
rtapelib.c \
rmt.h \

45
lib/mkdtempat.c Normal file
View File

@@ -0,0 +1,45 @@
/* Copyright 2026 Free Software Foundation, Inc.
This program 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.
This program 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 <http://www.gnu.org/licenses/>.
Written by Paul Eggert. */
#include <config.h>
#include "mkdtempat.h"
#include <tempname.h>
#include <stddef.h>
#include <sys/stat.h>
static int
try_dir (char *tmpl, void *flags)
{
int *pdirfd = flags;
return mkdirat (*pdirfd, tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
}
/* Relative to the directory DIRFD if XTEMPLATE is relative,
generate a unique temporary directory from XTEMPLATE.
The last six characters of XTEMPLATE must be "XXXXXX";
replace them with a string that makes the generated directory unique.
Create the directory mode 700, and return its name.
On failure, return NULL and set errno. */
char *
mkdtempat (int dirfd, char *xtemplate)
{
return (try_tempname_len (xtemplate, 0, &dirfd, try_dir, 6) < 0
? NULL : xtemplate);
}

1
lib/mkdtempat.h Normal file
View File

@@ -0,0 +1 @@
char *mkdtempat (int, char *);