1 /* Get file status. Linux version.
2 Copyright (C) 2020-2021 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 #define __fstatat __redirect___fstatat
20 #define fstatat __redirect_fstatat
21 #include <inttypes.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <kernel_stat.h>
26 #include <sysdep.h>
27 #include <time.h>
28 #include <kstat_cp.h>
29 #include <stat_t64_cp.h>
30 #include <sys/sysmacros.h>
31
32 #if __TIMESIZE == 64 \
33 && (__WORDSIZE == 32 \
34 && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32))
35 /* Sanity check to avoid newer 32-bit ABI to support non-LFS calls. */
36 _Static_assert (sizeof (__off_t) == sizeof (__off64_t),
37 "__blkcnt_t and __blkcnt64_t must match");
38 _Static_assert (sizeof (__ino_t) == sizeof (__ino64_t),
39 "__blkcnt_t and __blkcnt64_t must match");
40 _Static_assert (sizeof (__blkcnt_t) == sizeof (__blkcnt64_t),
41 "__blkcnt_t and __blkcnt64_t must match");
42 #endif
43
44 static inline int
fstatat64_time64_statx(int fd,const char * file,struct __stat64_t64 * buf,int flag)45 fstatat64_time64_statx (int fd, const char *file, struct __stat64_t64 *buf,
46 int flag)
47 {
48 /* 32-bit kABI with default 64-bit time_t, e.g. arc, riscv32. Also
49 64-bit time_t support is done through statx syscall. */
50 struct statx tmp;
51 int r = INTERNAL_SYSCALL_CALL (statx, fd, file, AT_NO_AUTOMOUNT | flag,
52 STATX_BASIC_STATS, &tmp);
53 if (r != 0)
54 return r;
55
56 *buf = (struct __stat64_t64) {
57 .st_dev = gnu_dev_makedev (tmp.stx_dev_major, tmp.stx_dev_minor),
58 .st_rdev = gnu_dev_makedev (tmp.stx_rdev_major, tmp.stx_rdev_minor),
59 .st_ino = tmp.stx_ino,
60 .st_mode = tmp.stx_mode,
61 .st_nlink = tmp.stx_nlink,
62 .st_uid = tmp.stx_uid,
63 .st_gid = tmp.stx_gid,
64 .st_atime = tmp.stx_atime.tv_sec,
65 .st_atim.tv_nsec = tmp.stx_atime.tv_nsec,
66 .st_mtime = tmp.stx_mtime.tv_sec,
67 .st_mtim.tv_nsec = tmp.stx_mtime.tv_nsec,
68 .st_ctime = tmp.stx_ctime.tv_sec,
69 .st_ctim.tv_nsec = tmp.stx_ctime.tv_nsec,
70 .st_size = tmp.stx_size,
71 .st_blocks = tmp.stx_blocks,
72 .st_blksize = tmp.stx_blksize,
73 };
74
75 return r;
76 }
77
78 static inline struct __timespec64
valid_timespec_to_timespec64(const struct timespec ts)79 valid_timespec_to_timespec64 (const struct timespec ts)
80 {
81 struct __timespec64 ts64;
82
83 ts64.tv_sec = ts.tv_sec;
84 ts64.tv_nsec = ts.tv_nsec;
85
86 return ts64;
87 }
88
89 static inline int
fstatat64_time64_stat(int fd,const char * file,struct __stat64_t64 * buf,int flag)90 fstatat64_time64_stat (int fd, const char *file, struct __stat64_t64 *buf,
91 int flag)
92 {
93 int r;
94
95 #if XSTAT_IS_XSTAT64
96 # ifdef __NR_newfstatat
97 /* 64-bit kABI, e.g. aarch64, ia64, powerpc64*, s390x, riscv64, and
98 x86_64. */
99 r = INTERNAL_SYSCALL_CALL (newfstatat, fd, file, buf, flag);
100 # elif defined __NR_fstatat64
101 # if STAT64_IS_KERNEL_STAT64
102 /* 64-bit kABI outlier, e.g. alpha */
103 r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, buf, flag);
104 # else
105 /* 64-bit kABI outlier, e.g. sparc64. */
106 struct kernel_stat64 kst64;
107 r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, &kst64, flag);
108 if (r == 0)
109 __cp_stat64_kstat64 (buf, &kst64);
110 # endif
111 # endif
112 #else
113 # ifdef __NR_fstatat64
114 /* All kABIs with non-LFS support and with old 32-bit time_t support
115 e.g. arm, csky, i386, hppa, m68k, microblaze, nios2, sh, powerpc32,
116 and sparc32. */
117 struct stat64 st64;
118 r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, &st64, flag);
119 if (r == 0)
120 {
121 /* Clear both pad and reserved fields. */
122 memset (buf, 0, sizeof (*buf));
123
124 buf->st_dev = st64.st_dev,
125 buf->st_ino = st64.st_ino;
126 buf->st_mode = st64.st_mode;
127 buf->st_nlink = st64.st_nlink;
128 buf->st_uid = st64.st_uid;
129 buf->st_gid = st64.st_gid;
130 buf->st_rdev = st64.st_rdev;
131 buf->st_size = st64.st_size;
132 buf->st_blksize = st64.st_blksize;
133 buf->st_blocks = st64.st_blocks;
134 buf->st_atim = valid_timespec_to_timespec64 (st64.st_atim);
135 buf->st_mtim = valid_timespec_to_timespec64 (st64.st_mtim);
136 buf->st_ctim = valid_timespec_to_timespec64 (st64.st_ctim);
137 }
138 # else
139 /* 64-bit kabi outlier, e.g. mips64 and mips64-n32. */
140 struct kernel_stat kst;
141 r = INTERNAL_SYSCALL_CALL (newfstatat, fd, file, &kst, flag);
142 if (r == 0)
143 __cp_kstat_stat64_t64 (&kst, buf);
144 # endif
145 #endif
146
147 return r;
148 }
149
150 #if (__WORDSIZE == 32 \
151 && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32)) \
152 || defined STAT_HAS_TIME32
153 # define FSTATAT_USE_STATX 1
154 #else
155 # define FSTATAT_USE_STATX 0
156 #endif
157
158 int
__fstatat64_time64(int fd,const char * file,struct __stat64_t64 * buf,int flag)159 __fstatat64_time64 (int fd, const char *file, struct __stat64_t64 *buf,
160 int flag)
161 {
162 int r;
163
164 #if FSTATAT_USE_STATX
165 r = fstatat64_time64_statx (fd, file, buf, flag);
166 # ifndef __ASSUME_STATX
167 if (r == -ENOSYS)
168 r = fstatat64_time64_stat (fd, file, buf, flag);
169 # endif
170 #else
171 r = fstatat64_time64_stat (fd, file, buf, flag);
172 #endif
173
174 return INTERNAL_SYSCALL_ERROR_P (r)
175 ? INLINE_SYSCALL_ERROR_RETURN_VALUE (-r)
176 : 0;
177 }
178 #if __TIMESIZE != 64
hidden_def(__fstatat64_time64)179 hidden_def (__fstatat64_time64)
180
181 int
182 __fstatat64 (int fd, const char *file, struct stat64 *buf, int flags)
183 {
184 struct __stat64_t64 st_t64;
185 return __fstatat64_time64 (fd, file, &st_t64, flags)
186 ?: __cp_stat64_t64_stat64 (&st_t64, buf);
187 }
188 #endif
189
190 #undef __fstatat
191 #undef fstatat
192
193 hidden_def (__fstatat64)
194 weak_alias (__fstatat64, fstatat64)
195
196 #if XSTAT_IS_XSTAT64
197 strong_alias (__fstatat64, __fstatat)
198 weak_alias (__fstatat64, fstatat)
199 strong_alias (__fstatat64, __GI___fstatat);
200 #endif
201