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"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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