only use fstatat and others legacy stat syscalls if they exist
[musl] / src / stat / fstatat.c
1 #define _BSD_SOURCE
2 #include <sys/stat.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <stdint.h>
7 #include <sys/sysmacros.h>
8 #include "syscall.h"
9
10 struct statx {
11         uint32_t stx_mask;
12         uint32_t stx_blksize;
13         uint64_t stx_attributes;
14         uint32_t stx_nlink;
15         uint32_t stx_uid;
16         uint32_t stx_gid;
17         uint16_t stx_mode;
18         uint16_t pad1;
19         uint64_t stx_ino;
20         uint64_t stx_size;
21         uint64_t stx_blocks;
22         uint64_t stx_attributes_mask;
23         struct {
24                 int64_t tv_sec;
25                 uint32_t tv_nsec;
26                 int32_t pad;
27         } stx_atime, stx_btime, stx_ctime, stx_mtime;
28         uint32_t stx_rdev_major;
29         uint32_t stx_rdev_minor;
30         uint32_t stx_dev_major;
31         uint32_t stx_dev_minor;
32         uint64_t spare[14];
33 };
34
35 static int fstatat_statx(int fd, const char *restrict path, struct stat *restrict st, int flag)
36 {
37         struct statx stx;
38
39         int ret = __syscall(SYS_statx, fd, path, flag, 0x7ff, &stx);
40         if (ret) return ret;
41
42         *st = (struct stat){
43                 .st_dev = makedev(stx.stx_dev_major, stx.stx_dev_minor),
44                 .st_ino = stx.stx_ino,
45                 .st_mode = stx.stx_mode,
46                 .st_nlink = stx.stx_nlink,
47                 .st_uid = stx.stx_uid,
48                 .st_gid = stx.stx_gid,
49                 .st_rdev = makedev(stx.stx_rdev_major, stx.stx_rdev_minor),
50                 .st_size = stx.stx_size,
51                 .st_blksize = stx.stx_blksize,
52                 .st_blocks = stx.stx_blocks,
53                 .st_atim.tv_sec = stx.stx_atime.tv_sec,
54                 .st_atim.tv_nsec = stx.stx_atime.tv_nsec,
55                 .st_mtim.tv_sec = stx.stx_mtime.tv_sec,
56                 .st_mtim.tv_nsec = stx.stx_mtime.tv_nsec,
57                 .st_ctim.tv_sec = stx.stx_ctime.tv_sec,
58                 .st_ctim.tv_nsec = stx.stx_ctime.tv_nsec,
59 #if _REDIR_TIME64
60                 .__st_atim32.tv_sec = stx.stx_atime.tv_sec,
61                 .__st_atim32.tv_nsec = stx.stx_atime.tv_nsec,
62                 .__st_mtim32.tv_sec = stx.stx_mtime.tv_sec,
63                 .__st_mtim32.tv_nsec = stx.stx_mtime.tv_nsec,
64                 .__st_ctim32.tv_sec = stx.stx_ctime.tv_sec,
65                 .__st_ctim32.tv_nsec = stx.stx_ctime.tv_nsec,
66 #endif
67         };
68         return 0;
69 }
70
71 #ifdef SYS_fstatat
72
73 #include "kstat.h"
74
75 static int fstatat_kstat(int fd, const char *restrict path, struct stat *restrict st, int flag)
76 {
77         int ret;
78         struct kstat kst;
79
80         if (flag==AT_EMPTY_PATH && fd>=0 && !*path) {
81                 ret = __syscall(SYS_fstat, fd, &kst);
82                 if (ret==-EBADF && __syscall(SYS_fcntl, fd, F_GETFD)>=0) {
83                         ret = __syscall(SYS_fstatat, fd, path, &kst, flag);
84                         if (ret==-EINVAL) {
85                                 char buf[15+3*sizeof(int)];
86                                 __procfdname(buf, fd);
87 #ifdef SYS_stat
88                                 ret = __syscall(SYS_stat, buf, &kst);
89 #else
90                                 ret = __syscall(SYS_fstatat, AT_FDCWD, buf, &kst, 0);
91 #endif
92                         }
93                 }
94         }
95 #ifdef SYS_lstat
96         else if ((fd == AT_FDCWD || *path=='/') && flag==AT_SYMLINK_NOFOLLOW)
97                 ret = __syscall(SYS_lstat, path, &kst);
98 #endif
99 #ifdef SYS_stat
100         else if ((fd == AT_FDCWD || *path=='/') && !flag)
101                 ret = __syscall(SYS_stat, path, &kst);
102 #endif
103         else ret = __syscall(SYS_fstatat, fd, path, &kst, flag);
104
105         if (ret) return ret;
106
107         *st = (struct stat){
108                 .st_dev = kst.st_dev,
109                 .st_ino = kst.st_ino,
110                 .st_mode = kst.st_mode,
111                 .st_nlink = kst.st_nlink,
112                 .st_uid = kst.st_uid,
113                 .st_gid = kst.st_gid,
114                 .st_rdev = kst.st_rdev,
115                 .st_size = kst.st_size,
116                 .st_blksize = kst.st_blksize,
117                 .st_blocks = kst.st_blocks,
118                 .st_atim.tv_sec = kst.st_atime_sec,
119                 .st_atim.tv_nsec = kst.st_atime_nsec,
120                 .st_mtim.tv_sec = kst.st_mtime_sec,
121                 .st_mtim.tv_nsec = kst.st_mtime_nsec,
122                 .st_ctim.tv_sec = kst.st_ctime_sec,
123                 .st_ctim.tv_nsec = kst.st_ctime_nsec,
124 #if _REDIR_TIME64
125                 .__st_atim32.tv_sec = kst.st_atime_sec,
126                 .__st_atim32.tv_nsec = kst.st_atime_nsec,
127                 .__st_mtim32.tv_sec = kst.st_mtime_sec,
128                 .__st_mtim32.tv_nsec = kst.st_mtime_nsec,
129                 .__st_ctim32.tv_sec = kst.st_ctime_sec,
130                 .__st_ctim32.tv_nsec = kst.st_ctime_nsec,
131 #endif
132         };
133
134         return 0;
135 }
136 #endif
137
138 int __fstatat(int fd, const char *restrict path, struct stat *restrict st, int flag)
139 {
140         int ret;
141 #ifdef SYS_fstatat
142         if (sizeof((struct kstat){0}.st_atime_sec) < sizeof(time_t)) {
143                 ret = fstatat_statx(fd, path, st, flag);
144                 if (ret!=-ENOSYS) return __syscall_ret(ret);
145         }
146         ret = fstatat_kstat(fd, path, st, flag);
147 #else
148         ret = fstatat_statx(fd, path, st, flag);
149 #endif
150         return __syscall_ret(ret);
151 }
152
153 weak_alias(__fstatat, fstatat);
154
155 #if !_REDIR_TIME64
156 weak_alias(fstatat, fstatat64);
157 #endif