| 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 | 	flag |= AT_NO_AUTOMOUNT; |
| 40 | 	int ret = __syscall(SYS_statx, fd, path, flag, 0x7ff, &stx); |
| 41 | 	if (ret) return ret; |
| 42 | |
| 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, |
| 60 | #if _REDIR_TIME64 |
| 61 | 		.__st_atim32.tv_sec = stx.stx_atime.tv_sec, |
| 62 | 		.__st_atim32.tv_nsec = stx.stx_atime.tv_nsec, |
| 63 | 		.__st_mtim32.tv_sec = stx.stx_mtime.tv_sec, |
| 64 | 		.__st_mtim32.tv_nsec = stx.stx_mtime.tv_nsec, |
| 65 | 		.__st_ctim32.tv_sec = stx.stx_ctime.tv_sec, |
| 66 | 		.__st_ctim32.tv_nsec = stx.stx_ctime.tv_nsec, |
| 67 | #endif |
| 68 | 	}; |
| 69 | 	return 0; |
| 70 | } |
| 71 | |
| 72 | #ifdef SYS_fstatat |
| 73 | |
| 74 | #include "kstat.h" |
| 75 | |
| 76 | static int fstatat_kstat(int fd, const char *restrict path, struct stat *restrict st, int flag) |
| 77 | { |
| 78 | 	int ret; |
| 79 | 	struct kstat kst; |
| 80 | |
| 81 | 	if (flag==AT_EMPTY_PATH && fd>=0 && !*path) { |
| 82 | 		ret = __syscall(SYS_fstat, fd, &kst); |
| 83 | 		if (ret==-EBADF && __syscall(SYS_fcntl, fd, F_GETFD)>=0) { |
| 84 | 			ret = __syscall(SYS_fstatat, fd, path, &kst, flag); |
| 85 | 			if (ret==-EINVAL) { |
| 86 | 				char buf[15+3*sizeof(int)]; |
| 87 | 				__procfdname(buf, fd); |
| 88 | #ifdef SYS_stat |
| 89 | 				ret = __syscall(SYS_stat, buf, &kst); |
| 90 | #else |
| 91 | 				ret = __syscall(SYS_fstatat, AT_FDCWD, buf, &kst, 0); |
| 92 | #endif |
| 93 | 			} |
| 94 | 		} |
| 95 | 	} |
| 96 | #ifdef SYS_lstat |
| 97 | 	else if ((fd == AT_FDCWD || *path=='/') && flag==AT_SYMLINK_NOFOLLOW) |
| 98 | 		ret = __syscall(SYS_lstat, path, &kst); |
| 99 | #endif |
| 100 | #ifdef SYS_stat |
| 101 | 	else if ((fd == AT_FDCWD || *path=='/') && !flag) |
| 102 | 		ret = __syscall(SYS_stat, path, &kst); |
| 103 | #endif |
| 104 | 	else ret = __syscall(SYS_fstatat, fd, path, &kst, flag); |
| 105 | |
| 106 | 	if (ret) return ret; |
| 107 | |
| 108 | 	*st = (struct stat){ |
| 109 | 		.st_dev = kst.st_dev, |
| 110 | 		.st_ino = kst.st_ino, |
| 111 | 		.st_mode = kst.st_mode, |
| 112 | 		.st_nlink = kst.st_nlink, |
| 113 | 		.st_uid = kst.st_uid, |
| 114 | 		.st_gid = kst.st_gid, |
| 115 | 		.st_rdev = kst.st_rdev, |
| 116 | 		.st_size = kst.st_size, |
| 117 | 		.st_blksize = kst.st_blksize, |
| 118 | 		.st_blocks = kst.st_blocks, |
| 119 | 		.st_atim.tv_sec = kst.st_atime_sec, |
| 120 | 		.st_atim.tv_nsec = kst.st_atime_nsec, |
| 121 | 		.st_mtim.tv_sec = kst.st_mtime_sec, |
| 122 | 		.st_mtim.tv_nsec = kst.st_mtime_nsec, |
| 123 | 		.st_ctim.tv_sec = kst.st_ctime_sec, |
| 124 | 		.st_ctim.tv_nsec = kst.st_ctime_nsec, |
| 125 | #if _REDIR_TIME64 |
| 126 | 		.__st_atim32.tv_sec = kst.st_atime_sec, |
| 127 | 		.__st_atim32.tv_nsec = kst.st_atime_nsec, |
| 128 | 		.__st_mtim32.tv_sec = kst.st_mtime_sec, |
| 129 | 		.__st_mtim32.tv_nsec = kst.st_mtime_nsec, |
| 130 | 		.__st_ctim32.tv_sec = kst.st_ctime_sec, |
| 131 | 		.__st_ctim32.tv_nsec = kst.st_ctime_nsec, |
| 132 | #endif |
| 133 | 	}; |
| 134 | |
| 135 | 	return 0; |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | int __fstatat(int fd, const char *restrict path, struct stat *restrict st, int flag) |
| 140 | { |
| 141 | 	int ret; |
| 142 | #ifdef SYS_fstatat |
| 143 | 	if (sizeof((struct kstat){0}.st_atime_sec) < sizeof(time_t)) { |
| 144 | 		ret = fstatat_statx(fd, path, st, flag); |
| 145 | 		if (ret!=-ENOSYS) return __syscall_ret(ret); |
| 146 | 	} |
| 147 | 	ret = fstatat_kstat(fd, path, st, flag); |
| 148 | #else |
| 149 | 	ret = fstatat_statx(fd, path, st, flag); |
| 150 | #endif |
| 151 | 	return __syscall_ret(ret); |
| 152 | } |
| 153 | |
| 154 | weak_alias(__fstatat, fstatat); |