]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Use mkdtempat instead of mkdtemp master
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 13 Apr 2026 06:58:22 +0000 (23:58 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 13 Apr 2026 07:02:17 +0000 (00:02 -0700)
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.

gnulib.modules
lib/Makefile.am
lib/mkdtempat.c [new file with mode: 0644]
lib/mkdtempat.h [new file with mode: 0644]
src/incremen.c

index 17ef44fd7e6d07bd6bd57d89c6a80c4f51e1c637..0dc024feac7aacfff4fc66aa819bbc9b92966f57 100644 (file)
@@ -80,7 +80,6 @@ mbrtoc32-regular
 mcel-prefer
 mempcpy
 mkdirat
-mkdtemp
 mkfifoat
 modechange
 nstrftime-limited
@@ -115,6 +114,7 @@ stringeq
 strnlen
 symlinkat
 sys_stat-h
+tempname
 time_rz
 timespec
 timespec-sub
index 2baa7be5e41a029004030f17eefcad50d0d4c8eb..4888ed36f426031d093513158885dcb66e47290d 100644 (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 \
diff --git a/lib/mkdtempat.c b/lib/mkdtempat.c
new file mode 100644 (file)
index 0000000..e35b9bb
--- /dev/null
@@ -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);
+}
diff --git a/lib/mkdtempat.h b/lib/mkdtempat.h
new file mode 100644 (file)
index 0000000..03a18dc
--- /dev/null
@@ -0,0 +1 @@
+char *mkdtempat (int, char *);
index 923c632385cc87e95f1abb33eb853d9cdf370451..53c6b3f32d706a7c937c7b1699873b890d17d9e0 100644 (file)
@@ -21,6 +21,7 @@
 #include <c-ctype.h>
 #include <flexmember.h>
 #include <hash.h>
+#include <mkdtempat.h>
 #include <quotearg.h>
 #include <same-inode.h>
 #include "common.h"
@@ -1665,7 +1666,9 @@ purge_directory (char const *directory_name)
          *copy_end = '/';
          memcpy (copy_end + !ISSLASH (copy_end[-1]), TEMP_DIR_TEMPLATE,
                  sizeof TEMP_DIR_TEMPLATE);
-         if (!mkdtemp (temp_stub))
+         struct fdbase f = fdbase (temp_stub);
+         if (f.fd == BADFD
+             || !mkdtempat (f.fd, temp_stub + (f.base - temp_stub)))
            {
              paxerror (errno, _("Cannot create temporary directory using template %s"),
                        quote (temp_stub));