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 <sys/stat.h>
22#include <fcntl.h>
23#include <string.h>
24#include <kernel_stat.h>
25#include <sysdep.h>
26#include <time.h>
27#include <statx_cp.h>
28#include <kstat_cp.h>
29#include <stat_t64_cp.h>
30
31int
32__fstatat64_time64 (int fd, const char *file, struct __stat64_t64 *buf,
33 int flag)
34{
35 int r;
36
37#if (__WORDSIZE == 32 \
38 && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32))
39 /* 32-bit kABI with default 64-bit time_t, e.g. arc, riscv32. Also
40 64-bit time_t support is done through statx syscall. */
41 struct statx tmp;
42 r = INTERNAL_SYSCALL_CALL (statx, fd, file, AT_NO_AUTOMOUNT | flag,
43 STATX_BASIC_STATS, &tmp);
44 if (r == 0)
45 {
46 __cp_stat64_t64_statx (buf, &tmp);
47 return 0;
48 }
49 if (-r != ENOSYS)
50 return INLINE_SYSCALL_ERROR_RETURN_VALUE (-r);
51#endif
52
53#if XSTAT_IS_XSTAT64
54# ifdef __NR_newfstatat
55 /* 64-bit kABI, e.g. aarch64, ia64, powerpc64*, s390x, riscv64, and
56 x86_64. */
57 r = INTERNAL_SYSCALL_CALL (newfstatat, fd, file, buf, flag);
58# elif defined __NR_fstatat64
59# if STAT64_IS_KERNEL_STAT64
60 /* 64-bit kABI outlier, e.g. alpha */
61 r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, buf, flag);
62# else
63 /* 64-bit kABI outlier, e.g. sparc64. */
64 struct kernel_stat64 kst64;
65 r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, &kst64, flag);
66 if (r == 0)
67 __cp_stat64_kstat64 (buf, &kst64);
68# endif
69# endif
70#else
71# ifdef __NR_fstatat64
72 /* All kABIs with non-LFS support and with old 32-bit time_t support
73 e.g. arm, csky, i386, hppa, m68k, microblaze, nios2, sh, powerpc32,
74 and sparc32. */
75 struct stat64 st64;
76 r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, &st64, flag);
77 if (r == 0)
78 {
79 /* Clear both pad and reserved fields. */
80 memset (buf, 0, sizeof (*buf));
81
82 buf->st_dev = st64.st_dev,
83 buf->st_ino = st64.st_ino;
84 buf->st_mode = st64.st_mode;
85 buf->st_nlink = st64.st_nlink;
86 buf->st_uid = st64.st_uid;
87 buf->st_gid = st64.st_gid;
88 buf->st_rdev = st64.st_rdev;
89 buf->st_size = st64.st_size;
90 buf->st_blksize = st64.st_blksize;
91 buf->st_blocks = st64.st_blocks;
92 buf->st_atim = valid_timespec_to_timespec64 (st64.st_atim);
93 buf->st_mtim = valid_timespec_to_timespec64 (st64.st_mtim);
94 buf->st_ctim = valid_timespec_to_timespec64 (st64.st_ctim);
95 }
96# else
97 /* 64-bit kabi outlier, e.g. mips64 and mips64-n32. */
98 struct kernel_stat kst;
99 r = INTERNAL_SYSCALL_CALL (newfstatat, fd, file, &kst, flag);
100 if (r == 0)
101 __cp_kstat_stat64_t64 (&kst, buf);
102# endif
103#endif
104
105 return INTERNAL_SYSCALL_ERROR_P (r)
106 ? INLINE_SYSCALL_ERROR_RETURN_VALUE (-r)
107 : 0;
108}
109#if __TIMESIZE != 64
110hidden_def (__fstatat64_time64)
111
112int
113__fstatat64 (int fd, const char *file, struct stat64 *buf, int flags)
114{
115 struct __stat64_t64 st_t64;
116 return __fstatat64_time64 (fd, file, &st_t64, flags)
117 ?: __cp_stat64_t64_stat64 (&st_t64, buf);
118}
119#endif
120
121#undef __fstatat
122#undef fstatat
123
124hidden_def (__fstatat64)
125weak_alias (__fstatat64, fstatat64)
126
127#if XSTAT_IS_XSTAT64
128strong_alias (__fstatat64, __fstatat)
129weak_alias (__fstatat64, fstatat)
130strong_alias (__fstatat64, __GI___fstatat);
131#endif
132