support: Use macros for *stat wrappers

Macros will automatically use the correct types, without
having to fiddle with internal glibc macros.  It's also
impossible to get the types wrong due to aliasing because
support_check_stat_fd and support_check_stat_path do not
depend on the struct stat* types.

The changes reveal some inconsistencies in tests.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
This commit is contained in:
Florian Weimer 2024-08-16 16:05:20 +02:00
parent bf29274841
commit e7c14e542d
16 changed files with 34 additions and 168 deletions

View File

@ -85,7 +85,7 @@ do_test (void)
support_capture_subprocess_check (&result, "execv", 0, sc_allow_none); support_capture_subprocess_check (&result, "execv", 0, sc_allow_none);
support_capture_subprocess_free (&result); support_capture_subprocess_free (&result);
xstat (path, &fs); xstat64 (path, &fs);
size = fs.st_size; size = fs.st_size;
/* Run 3 tests, each truncating aux-cache shorter and shorter. */ /* Run 3 tests, each truncating aux-cache shorter and shorter. */

View File

@ -117,7 +117,7 @@ simple_file_copy (void)
TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 6 + length); TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 6 + length);
struct stat64 st; struct stat64 st;
xfstat (outfd, &st); xfstat64 (outfd, &st);
if (length > 0) if (length > 0)
TEST_COMPARE (st.st_size, out_skipped + length); TEST_COMPARE (st.st_size, out_skipped + length);
else else

View File

@ -78,7 +78,7 @@ both_implementations_tests (statx_function impl, const char *path, int fd)
struct statx stx = { 0, }; struct statx stx = { 0, };
TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &stx), 0); TEST_COMPARE (statx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &stx), 0);
struct stat64 st; struct stat64 st;
xfstat (fd, &st); xfstat64 (fd, &st);
TEST_COMPARE (stx.stx_mode, st.st_mode); TEST_COMPARE (stx.stx_mode, st.st_mode);
TEST_COMPARE (stx.stx_dev_major, major (st.st_dev)); TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev)); TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
@ -88,7 +88,7 @@ both_implementations_tests (statx_function impl, const char *path, int fd)
TEST_COMPARE (statx (AT_FDCWD, "/dev/null", 0, STATX_BASIC_STATS, &stx), TEST_COMPARE (statx (AT_FDCWD, "/dev/null", 0, STATX_BASIC_STATS, &stx),
0); 0);
struct stat64 st; struct stat64 st;
xstat ("/dev/null", &st); xstat64 ("/dev/null", &st);
TEST_COMPARE (stx.stx_mode, st.st_mode); TEST_COMPARE (stx.stx_mode, st.st_mode);
TEST_COMPARE (stx.stx_dev_major, major (st.st_dev)); TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev)); TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));

View File

@ -84,7 +84,7 @@ run_test (void *closure)
support_capture_subprocess_free (&result); support_capture_subprocess_free (&result);
/* Verify path is present and is a directory. */ /* Verify path is present and is a directory. */
xstat (path, &fs); xstat64 (path, &fs);
if (!S_ISDIR (fs.st_mode)) if (!S_ISDIR (fs.st_mode))
{ {
support_record_failure (); support_record_failure ();

View File

@ -62,7 +62,7 @@ check_link (struct test_data step)
char *output; char *output;
output = xasprintf ("%s/%s", support_complocaledir_prefix, step.output); output = xasprintf ("%s/%s", support_complocaledir_prefix, step.output);
xstat (output, &locale); xstat64 (output, &locale);
free (output); free (output);
TEST_COMPARE (locale.st_nlink, step.st_nlink); TEST_COMPARE (locale.st_nlink, step.st_nlink);
} }

View File

@ -155,7 +155,7 @@ do_test (void)
tmp_sh = xasprintf ("%s/tmp_sh", tmp_dir); tmp_sh = xasprintf ("%s/tmp_sh", tmp_dir);
add_temp_file (tmp_sh); add_temp_file (tmp_sh);
fd_out = xopen (symlink_name, O_CREAT | O_WRONLY, 0); fd_out = xopen (symlink_name, O_CREAT | O_WRONLY, 0);
xstat ("/bin/sh", &st); xstat64 ("/bin/sh", &st);
fd = xopen ("/bin/sh", O_RDONLY, 0); fd = xopen ("/bin/sh", O_RDONLY, 0);
xcopy_file_range (fd, 0, fd_out, 0, st.st_size, 0); xcopy_file_range (fd, 0, fd_out, 0, st.st_size, 0);
xfchmod (fd_out, 0700); xfchmod (fd_out, 0700);

View File

@ -82,7 +82,7 @@ static void
check_size (const char *path, off64_t expected_size) check_size (const char *path, off64_t expected_size)
{ {
struct stat64 st; struct stat64 st;
xstat (path, &st); xstat64 (path, &st);
if (st.st_size != expected_size) if (st.st_size != expected_size)
FAIL_EXIT1 ("file \"%s\": expected size %lld, actual size %lld", FAIL_EXIT1 ("file \"%s\": expected size %lld, actual size %lld",
path, (unsigned long long int) expected_size, path, (unsigned long long int) expected_size,

View File

@ -169,7 +169,7 @@ do_test (void)
{ {
struct stat64 st; struct stat64 st;
xstat (_PATH_BSHELL, &st); xstat64 (_PATH_BSHELL, &st);
mode_t mode = st.st_mode; mode_t mode = st.st_mode;
xchmod (_PATH_BSHELL, mode & ~(S_IXUSR | S_IXGRP | S_IXOTH)); xchmod (_PATH_BSHELL, mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));

View File

@ -42,14 +42,12 @@ libsupport-routines = \
resolv_test \ resolv_test \
set_fortify_handler \ set_fortify_handler \
support-open-dev-null-range \ support-open-dev-null-range \
support-xfstat \
support-xfstat-time64 \
support-xstat \
support-xstat-time64 \
support_become_root \ support_become_root \
support_can_chroot \ support_can_chroot \
support_capture_subprocess \ support_capture_subprocess \
support_capture_subprocess_check \ support_capture_subprocess_check \
support_check_stat_fd \
support_check_stat_path \
support_chroot \ support_chroot \
support_copy_file \ support_copy_file \
support_copy_file_range \ support_copy_file_range \
@ -135,8 +133,6 @@ libsupport-routines = \
xgetsockname \ xgetsockname \
xlisten \ xlisten \
xlseek \ xlseek \
xlstat \
xlstat-time64 \
xmalloc \ xmalloc \
xmemstream \ xmemstream \
xmkdir \ xmkdir \

View File

@ -1,32 +0,0 @@
/* 64-bit time_t stat with error checking.
Copyright (C) 2021-2024 Free Software Foundation, Inc.
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
<https://www.gnu.org/licenses/>. */
/* NB: Non-standard file name to avoid sysdeps override for xstat. */
#include <support/check.h>
#include <support/xunistd.h>
#include <sys/stat.h>
#if __TIMESIZE != 64
void
xfstat_time64 (int fd, struct __stat64_t64 *result)
{
if (__fstat64_time64 (fd, result) != 0)
FAIL_EXIT1 ("__fstat64_time64 (%d): %m", fd);
}
#endif

View File

@ -1,32 +0,0 @@
/* 64-bit time_t stat with error checking.
Copyright (C) 2021-2024 Free Software Foundation, Inc.
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
<https://www.gnu.org/licenses/>. */
/* NB: Non-standard file name to avoid sysdeps override for xstat. */
#include <support/check.h>
#include <support/xunistd.h>
#include <sys/stat.h>
#if __TIMESIZE != 64
void
xstat_time64 (const char *path, struct __stat64_t64 *result)
{
if (__stat64_time64 (path, result) != 0)
FAIL_EXIT1 ("__stat64_time64 (\"%s\"): %m", path);
}
#endif

View File

@ -1,30 +0,0 @@
/* stat64 with error checking.
Copyright (C) 2017-2024 Free Software Foundation, Inc.
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
<https://www.gnu.org/licenses/>. */
/* NB: Non-standard file name to avoid sysdeps override for xstat. */
#include <support/check.h>
#include <support/xunistd.h>
#include <sys/stat.h>
void
xstat (const char *path, struct stat64 *result)
{
if (stat64 (path, result) != 0)
FAIL_EXIT1 ("stat64 (\"%s\"): %m", path);
}

View File

@ -1,5 +1,5 @@
/* lstat64 with error checking. /* Error checking for descriptor-based stat functions.
Copyright (C) 2017-2024 Free Software Foundation, Inc. Copyright (C) 2024 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -18,11 +18,10 @@
#include <support/check.h> #include <support/check.h>
#include <support/xunistd.h> #include <support/xunistd.h>
#include <sys/stat.h>
void void
xlstat (const char *path, struct stat64 *result) support_check_stat_fd (const char *name, int fd, int result)
{ {
if (lstat64 (path, result) != 0) if (result != 0)
FAIL_EXIT1 ("lstat64 (\"%s\"): %m", path); FAIL_EXIT1 ("%s (%d): %m", name, fd);
} }

View File

@ -1,4 +1,4 @@
/* fstat64 with error checking. /* Error checking for path-based stat functions.
Copyright (C) 2017-2024 Free Software Foundation, Inc. Copyright (C) 2017-2024 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
@ -18,11 +18,10 @@
#include <support/check.h> #include <support/check.h>
#include <support/xunistd.h> #include <support/xunistd.h>
#include <sys/stat.h>
void void
xfstat (int fd, struct stat64 *result) support_check_stat_path (const char *name, const char *path, int result)
{ {
if (fstat64 (fd, result) != 0) if (result != 0)
FAIL_EXIT1 ("fstat64 (%d): %m", fd); FAIL_EXIT1 ("%s (\"%s\"): %m", name, path);
} }

View File

@ -1,32 +0,0 @@
/* 64-bit time_t stat with error checking.
Copyright (C) 2021-2024 Free Software Foundation, Inc.
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
<https://www.gnu.org/licenses/>. */
/* NB: Non-standard file name to avoid sysdeps override for xstat. */
#include <support/check.h>
#include <support/xunistd.h>
#include <sys/stat.h>
#if __TIMESIZE != 64
void
xlstat_time64 (const char *path, struct __stat64_t64 *result)
{
if (__lstat64_time64 (path, result) != 0)
FAIL_EXIT1 ("__lstat64_time64 (\"%s\"): %m", path);
}
#endif

View File

@ -29,7 +29,6 @@
__BEGIN_DECLS __BEGIN_DECLS
struct stat64;
struct statx; struct statx;
pid_t xfork (void); pid_t xfork (void);
@ -37,21 +36,20 @@ pid_t xwaitpid (pid_t, int *status, int flags);
void xpipe (int[2]); void xpipe (int[2]);
void xdup2 (int, int); void xdup2 (int, int);
int xopen (const char *path, int flags, mode_t); int xopen (const char *path, int flags, mode_t);
#ifndef __USE_TIME64_REDIRECTS void support_check_stat_fd (const char *name, int fd, int result);
# ifdef __USE_FILE_OFFSET64 void support_check_stat_path (const char *name, const char *path, int result);
void xstat (const char *path, struct stat *); #define xstat(path, st) \
void xlstat (const char *path, struct stat *); (support_check_stat_path ("stat", (path), stat ((path), (st))))
void xfstat (int fd, struct stat *); #define xfstat(fd, st) \
# else (support_check_stat_fd ("fstat", (fd), fstat ((fd), (st))))
void xstat (const char *path, struct stat64 *); #define xlstat(path, st) \
void xlstat (const char *path, struct stat64 *); (support_check_stat_path ("lstat", (path), lstat ((path), (st))))
void xfstat (int fd, struct stat64 *); #define xstat64(path, st) \
# endif (support_check_stat_path ("stat64", (path), stat64 ((path), (st))))
#else #define xfstat64(fd, st) \
void __REDIRECT (xstat, (const char *path, struct stat *), xstat_time64); (support_check_stat_fd ("fstat64", (fd), fstat64 ((fd), (st))))
void __REDIRECT (xlstat, (const char *path, struct stat *), xlstat_time64); #define xlstat64(path, st) \
void __REDIRECT (xfstat, (int fd, struct stat *), xfstat_time64); (support_check_stat_path ("lstat64", (path), lstat64 ((path), (st))))
#endif
void xstatx (int, const char *, int, unsigned int, struct statx *); void xstatx (int, const char *, int, unsigned int, struct statx *);
void xmkdir (const char *path, mode_t); void xmkdir (const char *path, mode_t);
void xchroot (const char *path); void xchroot (const char *path);