io: Use struct statx and xstatx in tests

This avoids the need to define struct_statx to an appropriate
struct stat type variant because struct statx does not change
based on time/file offset flags.

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

View File

@ -1,2 +1 @@
#define struct_stat struct stat
#include "tst-futimens.c" #include "tst-futimens.c"

View File

@ -18,26 +18,23 @@
#include <support/check.h> #include <support/check.h>
#include <support/xunistd.h> #include <support/xunistd.h>
#include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifndef struct_stat
# define struct_stat struct stat64
#endif
static int static int
test_futimens_helper (const char *file, int fd, const struct timespec *ts) test_futimens_helper (const char *file, int fd, const struct timespec *ts)
{ {
int result = futimens (fd, ts); int result = futimens (fd, ts);
TEST_VERIFY_EXIT (result == 0); TEST_VERIFY_EXIT (result == 0);
struct_stat st; struct statx st;
xfstat (fd, &st); xstatx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &st);
/* Check if seconds for atime match */ /* Check if seconds for atime match */
TEST_COMPARE (st.st_atime, ts[0].tv_sec); TEST_COMPARE (st.stx_atime.tv_sec, ts[0].tv_sec);
/* Check if seconds for mtime match */ /* Check if seconds for mtime match */
TEST_COMPARE (st.st_mtime, ts[1].tv_sec); TEST_COMPARE (st.stx_mtime.tv_sec, ts[1].tv_sec);
return 0; return 0;
} }

View File

@ -1,2 +1 @@
#define struct_stat struct stat
#include "tst-futimes.c" #include "tst-futimes.c"

View File

@ -18,27 +18,24 @@
#include <support/check.h> #include <support/check.h>
#include <support/xunistd.h> #include <support/xunistd.h>
#include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#ifndef struct_stat
# define struct_stat struct stat64
#endif
static int static int
test_futimens_helper (const char *file, int fd, const struct timeval *tv) test_futimens_helper (const char *file, int fd, const struct timeval *tv)
{ {
int r = futimes (fd, tv); int r = futimes (fd, tv);
TEST_VERIFY_EXIT (r == 0); TEST_VERIFY_EXIT (r == 0);
struct_stat st; struct statx st;
xfstat (fd, &st); xstatx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &st);
/* Check if seconds for atime match */ /* Check if seconds for atime match */
TEST_COMPARE (st.st_atime, tv[0].tv_sec); TEST_COMPARE (st.stx_atime.tv_sec, tv[0].tv_sec);
/* Check if seconds for mtime match */ /* Check if seconds for mtime match */
TEST_COMPARE (st.st_mtime, tv[1].tv_sec); TEST_COMPARE (st.stx_mtime.tv_sec, tv[1].tv_sec);
return 0; return 0;
} }

View File

@ -1,4 +1 @@
#define struct_stat struct stat
#define fstat fstat
#define fstatat fstatat
#include "io/tst-futimesat.c" #include "io/tst-futimesat.c"

View File

@ -30,12 +30,6 @@
#include <support/temp_file.h> #include <support/temp_file.h>
#include <support/xunistd.h> #include <support/xunistd.h>
#ifndef struct_stat
# define struct_stat struct stat64
# define fstat fstat64
# define fstatat fstatat64
#endif
static int dir_fd; static int dir_fd;
static void static void
@ -118,19 +112,15 @@ do_test (void)
xwrite (fd, "hello", 5); xwrite (fd, "hello", 5);
puts ("file created"); puts ("file created");
struct_stat st1; struct statx st1;
if (fstat (fd, &st1) != 0) xstatx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &st1);
{
puts ("fstat64 failed");
return 1;
}
close (fd); close (fd);
struct timeval tv[2]; struct timeval tv[2];
tv[0].tv_sec = st1.st_atime + 1; tv[0].tv_sec = st1.stx_atime.tv_sec + 1;
tv[0].tv_usec = 0; tv[0].tv_usec = 0;
tv[1].tv_sec = st1.st_mtime + 1; tv[1].tv_sec = st1.stx_mtime.tv_sec + 1;
tv[1].tv_usec = 0; tv[1].tv_usec = 0;
if (futimesat (dir_fd, "some-file", tv) != 0) if (futimesat (dir_fd, "some-file", tv) != 0)
{ {
@ -138,16 +128,12 @@ do_test (void)
return 1; return 1;
} }
struct_stat st2; struct statx st2;
if (fstatat (dir_fd, "some-file", &st2, 0) != 0) xstatx (dir_fd, "some-file", 0, STATX_BASIC_STATS, &st2);
{
puts ("fstatat64 failed");
return 1;
}
if (st2.st_mtime != tv[1].tv_sec if (st2.stx_mtime.tv_sec != tv[1].tv_sec
#ifdef _STATBUF_ST_NSEC #ifdef _STATBUF_ST_NSEC
|| st2.st_mtim.tv_nsec != 0 || st2.stx_mtime.tv_nsec != 0
#endif #endif
) )
{ {

View File

@ -1,2 +1 @@
#define struct_stat struct stat
#include "tst-lutimes.c" #include "tst-lutimes.c"

View File

@ -18,34 +18,32 @@
#include <support/check.h> #include <support/check.h>
#include <support/xunistd.h> #include <support/xunistd.h>
#include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#ifndef struct_stat
# define struct_stat struct stat64
#endif
static int static int
test_lutimes_helper (const char *testfile, int fd, const char *testlink, test_lutimes_helper (const char *testfile, int fd, const char *testlink,
const struct timeval *tv) const struct timeval *tv)
{ {
struct_stat stfile_orig; struct statx stfile_orig;
xlstat (testfile, &stfile_orig); xstatx (AT_FDCWD, testfile, AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS,
&stfile_orig);
TEST_VERIFY_EXIT (lutimes (testlink, tv) == 0); TEST_VERIFY_EXIT (lutimes (testlink, tv) == 0);
struct_stat stlink; struct statx stlink;
xlstat (testlink, &stlink); xstatx (AT_FDCWD, testlink, AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS, &stlink);
TEST_COMPARE (stlink.st_atime, tv[0].tv_sec); TEST_COMPARE (stlink.stx_atime.tv_sec, tv[0].tv_sec);
TEST_COMPARE (stlink.st_mtime, tv[1].tv_sec); TEST_COMPARE (stlink.stx_mtime.tv_sec, tv[1].tv_sec);
/* Check if the timestamp from original file is not changed. */ /* Check if the timestamp from original file is not changed. */
struct_stat stfile; struct statx stfile;
xlstat (testfile, &stfile); xstatx (AT_FDCWD, testfile, AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS, &stfile);
TEST_COMPARE (stfile_orig.st_atime, stfile.st_atime); TEST_COMPARE (stfile_orig.stx_atime.tv_sec, stfile.stx_atime.tv_sec);
TEST_COMPARE (stfile_orig.st_mtime, stfile.st_mtime); TEST_COMPARE (stfile_orig.stx_mtime.tv_sec, stfile.stx_mtime.tv_sec);
return 0; return 0;
} }

View File

@ -1,2 +1 @@
#define struct_stat struct stat
#include "tst-utime.c" #include "tst-utime.c"

View File

@ -19,26 +19,23 @@
#include <utime.h> #include <utime.h>
#include <support/check.h> #include <support/check.h>
#include <support/xunistd.h> #include <support/xunistd.h>
#include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifndef struct_stat
# define struct_stat struct stat64
#endif
static int static int
test_utime_helper (const char *file, int fd, const struct utimbuf *ut) test_utime_helper (const char *file, int fd, const struct utimbuf *ut)
{ {
int result = utime (file, ut); int result = utime (file, ut);
TEST_VERIFY_EXIT (result == 0); TEST_VERIFY_EXIT (result == 0);
struct_stat st; struct statx st;
xfstat (fd, &st); xstatx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &st);
/* Check if seconds for actime match */ /* Check if seconds for actime match */
TEST_COMPARE (st.st_atime, ut->actime); TEST_COMPARE (st.stx_atime.tv_sec, ut->actime);
/* Check if seconds for modtime match */ /* Check if seconds for modtime match */
TEST_COMPARE (st.st_mtime, ut->modtime); TEST_COMPARE (st.stx_mtime.tv_sec, ut->modtime);
return 0; return 0;
} }

View File

@ -1,2 +1 @@
#define struct_stat struct stat
#include "tst-utimensat.c" #include "tst-utimensat.c"

View File

@ -22,10 +22,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#ifndef struct_stat
# define struct_stat struct stat64
#endif
static int static int
test_utimesat_helper (const char *testfile, int fd, const char *testlink, test_utimesat_helper (const char *testfile, int fd, const char *testlink,
const struct timespec *ts) const struct timespec *ts)
@ -33,35 +29,38 @@ test_utimesat_helper (const char *testfile, int fd, const char *testlink,
{ {
TEST_VERIFY_EXIT (utimensat (fd, testfile, ts, 0) == 0); TEST_VERIFY_EXIT (utimensat (fd, testfile, ts, 0) == 0);
struct_stat st; struct statx st;
xfstat (fd, &st); xstatx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &st);
/* Check if seconds for atime match */ /* Check if seconds for atime match */
TEST_COMPARE (st.st_atime, ts[0].tv_sec); TEST_COMPARE (st.stx_atime.tv_sec, ts[0].tv_sec);
/* Check if seconds for mtime match */ /* Check if seconds for mtime match */
TEST_COMPARE (st.st_mtime, ts[1].tv_sec); TEST_COMPARE (st.stx_mtime.tv_sec, ts[1].tv_sec);
} }
{ {
struct_stat stfile_orig; struct statx stfile_orig;
xlstat (testfile, &stfile_orig); xstatx (AT_FDCWD, testfile, AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS,
&stfile_orig);
TEST_VERIFY_EXIT (utimensat (0 /* ignored */, testlink, ts, TEST_VERIFY_EXIT (utimensat (0 /* ignored */, testlink, ts,
AT_SYMLINK_NOFOLLOW) AT_SYMLINK_NOFOLLOW)
== 0); == 0);
struct_stat stlink; struct statx stlink;
xlstat (testlink, &stlink); xstatx (AT_FDCWD, testlink, AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS,
&stlink);
TEST_COMPARE (stlink.st_atime, ts[0].tv_sec); TEST_COMPARE (stlink.stx_atime.tv_sec, ts[0].tv_sec);
TEST_COMPARE (stlink.st_mtime, ts[1].tv_sec); TEST_COMPARE (stlink.stx_mtime.tv_sec, ts[1].tv_sec);
/* Check if the timestamp from original file is not changed. */ /* Check if the timestamp from original file is not changed. */
struct_stat stfile; struct statx stfile;
xlstat (testfile, &stfile); xstatx (AT_FDCWD, testfile, AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS,
&stfile);
TEST_COMPARE (stfile_orig.st_atime, stfile.st_atime); TEST_COMPARE (stfile_orig.stx_atime.tv_sec, stfile.stx_atime.tv_sec);
TEST_COMPARE (stfile_orig.st_mtime, stfile.st_mtime); TEST_COMPARE (stfile_orig.stx_mtime.tv_sec, stfile.stx_mtime.tv_sec);
} }
return 0; return 0;

View File

@ -1,2 +1 @@
#define struct_stat struct stat
#include "tst-utimes.c" #include "tst-utimes.c"

View File

@ -18,28 +18,25 @@
#include <support/check.h> #include <support/check.h>
#include <support/xunistd.h> #include <support/xunistd.h>
#include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#ifndef struct_stat
# define struct_stat struct stat64
#endif
static int static int
test_utimes_helper (const char *file, int fd, const struct timeval *tv) test_utimes_helper (const char *file, int fd, const struct timeval *tv)
{ {
int result = utimes (file, tv); int result = utimes (file, tv);
TEST_VERIFY_EXIT (result == 0); TEST_VERIFY_EXIT (result == 0);
struct_stat st; struct statx st;
xfstat (fd, &st); xstatx (fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &st);
/* Check if seconds for atime match */ /* Check if seconds for atime match */
TEST_COMPARE (st.st_atime, tv[0].tv_sec); TEST_COMPARE (st.stx_atime.tv_sec, tv[0].tv_sec);
/* Check if seconds for mtime match */ /* Check if seconds for mtime match */
TEST_COMPARE (st.st_mtime, tv[1].tv_sec); TEST_COMPARE (st.stx_mtime.tv_sec, tv[1].tv_sec);
return 0; return 0;
} }