2016-12-09 07:18:27 +00:00
|
|
|
/* Temporary file handling for tests.
|
2024-01-01 18:12:26 +00:00
|
|
|
Copyright (C) 1998-2024 Free Software Foundation, Inc.
|
2022-01-18 07:59:36 +00:00
|
|
|
Copyright The GNU Tools Authors.
|
2016-12-09 07:18:27 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library 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
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2016-12-09 07:18:27 +00:00
|
|
|
|
|
|
|
/* This is required to get an mkstemp which can create large files on
|
|
|
|
some 32-bit platforms. */
|
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
|
2022-01-18 07:59:36 +00:00
|
|
|
#include <support/check.h>
|
2016-12-09 07:18:27 +00:00
|
|
|
#include <support/temp_file.h>
|
|
|
|
#include <support/temp_file-internal.h>
|
|
|
|
#include <support/support.h>
|
|
|
|
|
2022-01-18 07:59:36 +00:00
|
|
|
#include <errno.h>
|
2016-12-09 07:18:27 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-01-18 07:59:36 +00:00
|
|
|
#include <xunistd.h>
|
2016-12-09 07:18:27 +00:00
|
|
|
|
|
|
|
/* List of temporary files. */
|
|
|
|
static struct temp_name_list
|
|
|
|
{
|
2017-05-08 12:32:58 +00:00
|
|
|
struct temp_name_list *next;
|
2016-12-09 07:18:27 +00:00
|
|
|
char *name;
|
2017-05-08 12:57:59 +00:00
|
|
|
pid_t owner;
|
2022-01-18 07:59:36 +00:00
|
|
|
bool toolong;
|
2016-12-09 07:18:27 +00:00
|
|
|
} *temp_name_list;
|
|
|
|
|
|
|
|
/* Location of the temporary files. Set by the test skeleton via
|
|
|
|
support_set_test_dir. The string is not be freed. */
|
|
|
|
static const char *test_dir = _PATH_TMP;
|
|
|
|
|
2022-01-18 07:59:36 +00:00
|
|
|
/* Name of subdirectories in a too long temporary directory tree. */
|
|
|
|
static char toolong_subdir[NAME_MAX + 1];
|
|
|
|
static bool toolong_initialized;
|
|
|
|
static size_t toolong_path_max;
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_temp_file_internal (const char *name, bool toolong)
|
2016-12-09 07:18:27 +00:00
|
|
|
{
|
|
|
|
struct temp_name_list *newp
|
|
|
|
= (struct temp_name_list *) xcalloc (sizeof (*newp), 1);
|
|
|
|
char *newname = strdup (name);
|
|
|
|
if (newname != NULL)
|
|
|
|
{
|
|
|
|
newp->name = newname;
|
2017-05-08 12:32:58 +00:00
|
|
|
newp->next = temp_name_list;
|
2017-05-08 12:57:59 +00:00
|
|
|
newp->owner = getpid ();
|
2022-01-18 07:59:36 +00:00
|
|
|
newp->toolong = toolong;
|
2017-05-08 12:32:58 +00:00
|
|
|
temp_name_list = newp;
|
2016-12-09 07:18:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
free (newp);
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:59:36 +00:00
|
|
|
void
|
|
|
|
add_temp_file (const char *name)
|
|
|
|
{
|
|
|
|
add_temp_file_internal (name, false);
|
|
|
|
}
|
|
|
|
|
2016-12-09 07:18:27 +00:00
|
|
|
int
|
2020-10-15 18:14:22 +00:00
|
|
|
create_temp_file_in_dir (const char *base, const char *dir, char **filename)
|
2016-12-09 07:18:27 +00:00
|
|
|
{
|
|
|
|
char *fname;
|
|
|
|
int fd;
|
|
|
|
|
2020-10-15 18:14:22 +00:00
|
|
|
fname = xasprintf ("%s/%sXXXXXX", dir, base);
|
2016-12-09 07:18:27 +00:00
|
|
|
|
|
|
|
fd = mkstemp (fname);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
printf ("cannot open temporary file '%s': %m\n", fname);
|
|
|
|
free (fname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_temp_file (fname);
|
|
|
|
if (filename != NULL)
|
|
|
|
*filename = fname;
|
|
|
|
else
|
|
|
|
free (fname);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2020-10-15 18:14:22 +00:00
|
|
|
int
|
|
|
|
create_temp_file (const char *base, char **filename)
|
|
|
|
{
|
|
|
|
return create_temp_file_in_dir (base, test_dir, filename);
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:59:36 +00:00
|
|
|
static char *
|
|
|
|
create_temp_directory_internal (const char *base, bool toolong)
|
2017-11-12 08:53:06 +00:00
|
|
|
{
|
2017-11-18 13:11:09 +00:00
|
|
|
char *path = xasprintf ("%s/%sXXXXXX", test_dir, base);
|
|
|
|
if (mkdtemp (path) == NULL)
|
2017-11-12 08:53:06 +00:00
|
|
|
{
|
2017-11-18 13:11:09 +00:00
|
|
|
printf ("error: mkdtemp (\"%s\"): %m", path);
|
2017-11-12 08:53:06 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
2022-01-18 07:59:36 +00:00
|
|
|
add_temp_file_internal (path, toolong);
|
2017-11-18 13:11:09 +00:00
|
|
|
return path;
|
2017-11-12 08:53:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 07:59:36 +00:00
|
|
|
char *
|
|
|
|
support_create_temp_directory (const char *base)
|
|
|
|
{
|
|
|
|
return create_temp_directory_internal (base, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ensure_toolong_initialized (void)
|
|
|
|
{
|
|
|
|
if (!toolong_initialized)
|
|
|
|
FAIL_EXIT1 ("uninitialized toolong directory tree\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
initialize_toolong (const char *base)
|
|
|
|
{
|
|
|
|
long name_max = pathconf (base, _PC_NAME_MAX);
|
|
|
|
name_max = (name_max < 0 ? 64
|
|
|
|
: (name_max < sizeof (toolong_subdir) ? name_max
|
|
|
|
: sizeof (toolong_subdir) - 1));
|
|
|
|
|
|
|
|
long path_max = pathconf (base, _PC_PATH_MAX);
|
|
|
|
path_max = (path_max < 0 ? 1024
|
|
|
|
: path_max <= PTRDIFF_MAX ? path_max : PTRDIFF_MAX);
|
|
|
|
|
|
|
|
/* Sanity check to ensure that the test does not create temporary directories
|
|
|
|
in different filesystems because this API doesn't support it. */
|
|
|
|
if (toolong_initialized)
|
|
|
|
{
|
|
|
|
if (name_max != strlen (toolong_subdir))
|
|
|
|
FAIL_UNSUPPORTED ("name_max: Temporary directories in different"
|
|
|
|
" filesystems not supported yet\n");
|
|
|
|
if (path_max != toolong_path_max)
|
|
|
|
FAIL_UNSUPPORTED ("path_max: Temporary directories in different"
|
|
|
|
" filesystems not supported yet\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
toolong_path_max = path_max;
|
|
|
|
|
|
|
|
size_t len = name_max;
|
|
|
|
memset (toolong_subdir, 'X', len);
|
|
|
|
toolong_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
support_create_and_chdir_toolong_temp_directory (const char *basename)
|
|
|
|
{
|
|
|
|
char *base = create_temp_directory_internal (basename, true);
|
|
|
|
xchdir (base);
|
|
|
|
|
|
|
|
initialize_toolong (base);
|
|
|
|
|
|
|
|
size_t sz = strlen (toolong_subdir);
|
|
|
|
|
|
|
|
/* Create directories and descend into them so that the final path is larger
|
|
|
|
than PATH_MAX. */
|
|
|
|
for (size_t i = 0; i <= toolong_path_max / sz; i++)
|
|
|
|
{
|
|
|
|
int ret = mkdir (toolong_subdir, S_IRWXU);
|
|
|
|
if (ret != 0 && errno == ENAMETOOLONG)
|
|
|
|
FAIL_UNSUPPORTED ("Filesystem does not support creating too long "
|
|
|
|
"directory trees\n");
|
|
|
|
else if (ret != 0)
|
|
|
|
FAIL_EXIT1 ("Failed to create directory tree: %m\n");
|
|
|
|
xchdir (toolong_subdir);
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
}
|
2016-12-09 07:18:27 +00:00
|
|
|
|
|
|
|
void
|
2022-01-18 07:59:36 +00:00
|
|
|
support_chdir_toolong_temp_directory (const char *base)
|
2016-12-09 07:18:27 +00:00
|
|
|
{
|
2022-01-18 07:59:36 +00:00
|
|
|
ensure_toolong_initialized ();
|
|
|
|
|
|
|
|
xchdir (base);
|
|
|
|
|
|
|
|
size_t sz = strlen (toolong_subdir);
|
|
|
|
for (size_t i = 0; i <= toolong_path_max / sz; i++)
|
|
|
|
xchdir (toolong_subdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper functions called by the test skeleton follow. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_toolong_subdirs (const char *base)
|
|
|
|
{
|
|
|
|
ensure_toolong_initialized ();
|
|
|
|
|
|
|
|
if (chdir (base) != 0)
|
|
|
|
{
|
|
|
|
printf ("warning: toolong cleanup base failed: chdir (\"%s\"): %m\n",
|
|
|
|
base);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Descend. */
|
|
|
|
int levels = 0;
|
|
|
|
size_t sz = strlen (toolong_subdir);
|
|
|
|
for (levels = 0; levels <= toolong_path_max / sz; levels++)
|
|
|
|
if (chdir (toolong_subdir) != 0)
|
|
|
|
{
|
|
|
|
printf ("warning: toolong cleanup failed: chdir (\"%s\"): %m\n",
|
|
|
|
toolong_subdir);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ascend and remove. */
|
|
|
|
while (--levels >= 0)
|
|
|
|
{
|
|
|
|
if (chdir ("..") != 0)
|
|
|
|
{
|
|
|
|
printf ("warning: toolong cleanup failed: chdir (\"..\"): %m\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (remove (toolong_subdir) != 0)
|
|
|
|
{
|
|
|
|
printf ("warning: could not remove subdirectory: %s: %m\n",
|
|
|
|
toolong_subdir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-12-09 07:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
support_delete_temp_files (void)
|
|
|
|
{
|
2017-05-08 12:57:59 +00:00
|
|
|
pid_t pid = getpid ();
|
2016-12-09 07:18:27 +00:00
|
|
|
while (temp_name_list != NULL)
|
|
|
|
{
|
2023-05-20 13:37:47 +00:00
|
|
|
/* Only perform the removal if the path was registered in the same
|
2017-05-08 12:57:59 +00:00
|
|
|
process, as identified by the PID. (This assumes that the
|
|
|
|
parent process which registered the temporary file sticks
|
|
|
|
around, to prevent PID reuse.) */
|
|
|
|
if (temp_name_list->owner == pid)
|
|
|
|
{
|
2022-01-18 07:59:36 +00:00
|
|
|
if (temp_name_list->toolong)
|
|
|
|
remove_toolong_subdirs (temp_name_list->name);
|
|
|
|
|
2017-05-08 12:57:59 +00:00
|
|
|
if (remove (temp_name_list->name) != 0)
|
|
|
|
printf ("warning: could not remove temporary file: %s: %m\n",
|
|
|
|
temp_name_list->name);
|
|
|
|
}
|
2016-12-09 07:18:27 +00:00
|
|
|
free (temp_name_list->name);
|
|
|
|
|
2017-05-08 12:32:58 +00:00
|
|
|
struct temp_name_list *next = temp_name_list->next;
|
2016-12-09 07:18:27 +00:00
|
|
|
free (temp_name_list);
|
|
|
|
temp_name_list = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
support_print_temp_files (FILE *f)
|
|
|
|
{
|
|
|
|
if (temp_name_list != NULL)
|
|
|
|
{
|
|
|
|
struct temp_name_list *n;
|
|
|
|
fprintf (f, "temp_files=(\n");
|
2017-05-08 12:32:58 +00:00
|
|
|
for (n = temp_name_list; n != NULL; n = n->next)
|
2016-12-09 07:18:27 +00:00
|
|
|
fprintf (f, " '%s'\n", n->name);
|
|
|
|
fprintf (f, ")\n");
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 07:59:36 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
support_set_test_dir (const char *path)
|
|
|
|
{
|
|
|
|
test_dir = path;
|
|
|
|
}
|