use the correct stat structure in the fstat path
[musl.git] / src / stat / fstatat.c
blobd915fa1060ee99dda509f953da70a22381209643
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 #include "kstat.h"
11 struct statx {
12 uint32_t stx_mask;
13 uint32_t stx_blksize;
14 uint64_t stx_attributes;
15 uint32_t stx_nlink;
16 uint32_t stx_uid;
17 uint32_t stx_gid;
18 uint16_t stx_mode;
19 uint16_t pad1;
20 uint64_t stx_ino;
21 uint64_t stx_size;
22 uint64_t stx_blocks;
23 uint64_t stx_attributes_mask;
24 struct {
25 int64_t tv_sec;
26 uint32_t tv_nsec;
27 int32_t pad;
28 } stx_atime, stx_btime, stx_ctime, stx_mtime;
29 uint32_t stx_rdev_major;
30 uint32_t stx_rdev_minor;
31 uint32_t stx_dev_major;
32 uint32_t stx_dev_minor;
33 uint64_t spare[14];
36 static int fstatat_statx(int fd, const char *restrict path, struct stat *restrict st, int flag)
38 struct statx stx;
40 int ret = __syscall(SYS_statx, fd, path, flag, 0x7ff, &stx);
41 if (ret) return ret;
43 *st = (struct stat){
44 .st_dev = makedev(stx.stx_dev_major, stx.stx_dev_minor),
45 .st_ino = stx.stx_ino,
46 .st_mode = stx.stx_mode,
47 .st_nlink = stx.stx_nlink,
48 .st_uid = stx.stx_uid,
49 .st_gid = stx.stx_gid,
50 .st_rdev = makedev(stx.stx_rdev_major, stx.stx_rdev_minor),
51 .st_size = stx.stx_size,
52 .st_blksize = stx.stx_blksize,
53 .st_blocks = stx.stx_blocks,
54 .st_atim.tv_sec = stx.stx_atime.tv_sec,
55 .st_atim.tv_nsec = stx.stx_atime.tv_nsec,
56 .st_mtim.tv_sec = stx.stx_mtime.tv_sec,
57 .st_mtim.tv_nsec = stx.stx_mtime.tv_nsec,
58 .st_ctim.tv_sec = stx.stx_ctime.tv_sec,
59 .st_ctim.tv_nsec = stx.stx_ctime.tv_nsec,
61 return 0;
64 static int fstatat_kstat(int fd, const char *restrict path, struct stat *restrict st, int flag)
66 int ret;
67 struct kstat kst;
69 if (flag==AT_EMPTY_PATH && fd>=0 && !*path) {
70 ret = __syscall(SYS_fstat, fd, &kst);
71 if (ret==-EBADF && __syscall(SYS_fcntl, fd, F_GETFD)>=0) {
72 ret = __syscall(SYS_fstatat, fd, path, &kst, flag);
73 if (ret==-EINVAL) {
74 char buf[15+3*sizeof(int)];
75 __procfdname(buf, fd);
76 #ifdef SYS_stat
77 ret = __syscall(SYS_stat, buf, &kst);
78 #else
79 ret = __syscall(SYS_fstatat, AT_FDCWD, buf, &kst, 0);
80 #endif
84 #ifdef SYS_lstat
85 else if ((fd == AT_FDCWD || *path=='/') && flag==AT_SYMLINK_NOFOLLOW)
86 ret = __syscall(SYS_lstat, path, &kst);
87 #endif
88 #ifdef SYS_stat
89 else if ((fd == AT_FDCWD || *path=='/') && !flag)
90 ret = __syscall(SYS_stat, path, &kst);
91 #endif
92 else ret = __syscall(SYS_fstatat, fd, path, &kst, flag);
94 if (ret) return ret;
96 *st = (struct stat){
97 .st_dev = kst.st_dev,
98 .st_ino = kst.st_ino,
99 .st_mode = kst.st_mode,
100 .st_nlink = kst.st_nlink,
101 .st_uid = kst.st_uid,
102 .st_gid = kst.st_gid,
103 .st_rdev = kst.st_rdev,
104 .st_size = kst.st_size,
105 .st_blksize = kst.st_blksize,
106 .st_blocks = kst.st_blocks,
107 .st_atim.tv_sec = kst.st_atime_sec,
108 .st_atim.tv_nsec = kst.st_atime_nsec,
109 .st_mtim.tv_sec = kst.st_mtime_sec,
110 .st_mtim.tv_nsec = kst.st_mtime_nsec,
111 .st_ctim.tv_sec = kst.st_ctime_sec,
112 .st_ctim.tv_nsec = kst.st_ctime_nsec,
115 return 0;
118 int fstatat(int fd, const char *restrict path, struct stat *restrict st, int flag)
120 int ret;
121 if (sizeof((struct kstat){0}.st_atime_sec) < sizeof(time_t)) {
122 ret = fstatat_statx(fd, path, st, flag);
123 if (ret!=-ENOSYS) return __syscall_ret(ret);
125 ret = fstatat_kstat(fd, path, st, flag);
126 return __syscall_ret(ret);
129 weak_alias(fstatat, fstatat64);