implement fstatat with SYS_statx, conditional on undersized kstat time
authorRich Felker <dalias@aerifal.cx>
Fri, 19 Jul 2019 01:46:33 +0000 (21:46 -0400)
committerRich Felker <dalias@aerifal.cx>
Fri, 19 Jul 2019 01:46:33 +0000 (21:46 -0400)
this commit adds a new backend for fstatat (and thereby the whole stat
family) using the SYS_statx syscall, but conditions the new code on
the kernel stat structure's time fields being smaller than time_t. in
principle that should make it all dead code at present, but mips64 has
a broken stat structure with 32-bit time fields despite having 64-bit
time_t elsewhere, so on mips64 it is a functional change that makes
post-Y2038 filesystem timestamps accessible.

whenever the 32-bit archs end up getting 64-bit time_t, regardless of
how that happens, the changes in this commit will automatically take
effect for them too.

src/stat/fstatat.c

index 29d6334..31907e9 100644 (file)
@@ -3,10 +3,65 @@
 #include <string.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <stdint.h>
+#include <sys/sysmacros.h>
 #include "syscall.h"
 #include "kstat.h"
 
-int fstatat(int fd, const char *restrict path, struct stat *restrict st, int flag)
+struct statx {
+       uint32_t stx_mask;
+       uint32_t stx_blksize;
+       uint64_t stx_attributes;
+       uint32_t stx_nlink;
+       uint32_t stx_uid;
+       uint32_t stx_gid;
+       uint16_t stx_mode;
+       uint16_t pad1;
+       uint64_t stx_ino;
+       uint64_t stx_size;
+       uint64_t stx_blocks;
+       uint64_t stx_attributes_mask;
+       struct {
+               int64_t tv_sec;
+               uint32_t tv_nsec;
+               int32_t pad;
+       } stx_atime, stx_btime, stx_ctime, stx_mtime;
+       uint32_t stx_rdev_major;
+       uint32_t stx_rdev_minor;
+       uint32_t stx_dev_major;
+       uint32_t stx_dev_minor;
+       uint64_t spare[14];
+} stx;
+
+static int fstatat_statx(int fd, const char *restrict path, struct stat *restrict st, int flag)
+{
+       struct statx stx;
+
+       int ret = __syscall(SYS_statx, fd, path, flag, 0x7ff, &stx);
+       if (ret) return ret;
+
+       *st = (struct stat){
+               .st_dev = makedev(stx.stx_dev_major, stx.stx_dev_minor),
+               .st_ino = stx.stx_ino,
+               .st_mode = stx.stx_mode,
+               .st_nlink = stx.stx_nlink,
+               .st_uid = stx.stx_uid,
+               .st_gid = stx.stx_gid,
+               .st_rdev = makedev(stx.stx_rdev_major, stx.stx_rdev_minor),
+               .st_size = stx.stx_size,
+               .st_blksize = stx.stx_blksize,
+               .st_blocks = stx.stx_blocks,
+               .st_atim.tv_sec = stx.stx_atime.tv_sec,
+               .st_atim.tv_nsec = stx.stx_atime.tv_nsec,
+               .st_mtim.tv_sec = stx.stx_mtime.tv_sec,
+               .st_mtim.tv_nsec = stx.stx_mtime.tv_nsec,
+               .st_ctim.tv_sec = stx.stx_ctime.tv_sec,
+               .st_ctim.tv_nsec = stx.stx_ctime.tv_nsec,
+       };
+       return 0;
+}
+
+static int fstatat_kstat(int fd, const char *restrict path, struct stat *restrict st, int flag)
 {
        int ret;
        struct kstat kst;
@@ -36,7 +91,7 @@ int fstatat(int fd, const char *restrict path, struct stat *restrict st, int fla
 #endif
        else ret = __syscall(SYS_fstatat, fd, path, &kst, flag);
 
-       if (ret) return __syscall_ret(ret);
+       if (ret) return ret;
 
        *st = (struct stat){
                .st_dev = kst.st_dev,
@@ -60,4 +115,15 @@ int fstatat(int fd, const char *restrict path, struct stat *restrict st, int fla
        return 0;
 }
 
+int fstatat(int fd, const char *restrict path, struct stat *restrict st, int flag)
+{
+       int ret;
+       if (sizeof((struct kstat){0}.st_atime_sec) < sizeof(time_t)) {
+               ret = fstatat_statx(fd, path, st, flag);
+               if (ret!=-ENOSYS) return __syscall_ret(ret);
+       }
+       ret = fstatat_kstat(fd, path, st, flag);
+       return __syscall_ret(ret);
+}
+
 weak_alias(fstatat, fstatat64);