1 /* Basic test of stat with 64-bit time_t interfaces.
2    Copyright (C) 2021-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdbool.h>
22 #include <support/check.h>
23 #include <support/support.h>
24 #include <support/temp_file.h>
25 #include <support/xunistd.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <sys/sysmacros.h>
29 
30 /* The idea of the test is check whether the 64-bit time_t stat implementation
31    returns the expected value for comparable fields, so it does not really
32    matter whether statx uses a fallback implementation or not.  */
33 
34 static void
stat_check(int fd,const char * path,struct stat * st)35 stat_check (int fd, const char *path, struct stat *st)
36 {
37   TEST_COMPARE (stat (path, st), 0);
38 }
39 
40 static void
lstat_check(int fd,const char * path,struct stat * st)41 lstat_check (int fd, const char *path, struct stat *st)
42 {
43   TEST_COMPARE (lstat (path, st), 0);
44 }
45 
46 static void
fstat_check(int fd,const char * path,struct stat * st)47 fstat_check (int fd, const char *path, struct stat *st)
48 {
49   TEST_COMPARE (fstat (fd, st), 0);
50 }
51 
52 static void
fstatat_check(int fd,const char * path,struct stat * st)53 fstatat_check (int fd, const char *path, struct stat *st)
54 {
55   TEST_COMPARE (fstatat (fd, path, st, 0), 0);
56 }
57 
58 typedef void (*test_t)(int, const char *path, struct stat *);
59 
60 static int
do_test(void)61 do_test (void)
62 {
63   char *path;
64   int fd = create_temp_file ("tst-statx-", &path);
65   TEST_VERIFY_EXIT (fd >= 0);
66   support_write_file_string (path, "abc");
67 
68   struct statx stx;
69   TEST_COMPARE (statx (fd, path, 0, STATX_BASIC_STATS, &stx), 0);
70 
71   for (test_t *test = (test_t[]) { stat_check, lstat_check, fstat_check,
72 				   fstatat_check, NULL };
73        *test != NULL; test++)
74   {
75     struct stat st;
76     (*test) (fd, path, &st);
77 
78     TEST_COMPARE (stx.stx_dev_major, major (st.st_dev));
79     TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev));
80     TEST_COMPARE (stx.stx_ino, st.st_ino);
81     TEST_COMPARE (stx.stx_mode, st.st_mode);
82     TEST_COMPARE (stx.stx_nlink, st.st_nlink);
83     TEST_COMPARE (stx.stx_uid, st.st_uid);
84     TEST_COMPARE (stx.stx_gid, st.st_gid);
85     TEST_COMPARE (stx.stx_rdev_major, major (st.st_rdev));
86     TEST_COMPARE (stx.stx_rdev_minor, minor (st.st_rdev));
87     TEST_COMPARE (stx.stx_blksize, st.st_blksize);
88     TEST_COMPARE (stx.stx_blocks, st.st_blocks);
89 
90     TEST_COMPARE (stx.stx_ctime.tv_sec, st.st_ctim.tv_sec);
91     TEST_COMPARE (stx.stx_ctime.tv_nsec, st.st_ctim.tv_nsec);
92     TEST_COMPARE (stx.stx_mtime.tv_sec, st.st_mtim.tv_sec);
93     TEST_COMPARE (stx.stx_mtime.tv_nsec, st.st_mtim.tv_nsec);
94   }
95 
96   xclose (fd);
97   free (path);
98 
99   return 0;
100 }
101 
102 #include <support/test-driver.c>
103