[PATCH v2 2/5] tools/nolibc: Add statx() and make stat() rely on statx() when available

From: chris . chenfeiyang
Date: Wed Feb 08 2023 - 05:19:08 EST


From: Feiyang Chen <chenfeiyang@xxxxxxxxxxx>

loongarch and riscv32 only have statx(). arc, hexagon, nios2 and
openrisc have statx() and stat64() but not stat() or newstat().
Add statx() and make stat() rely on statx() to make them happy.

Signed-off-by: Feiyang Chen <chenfeiyang@xxxxxxxxxxx>
---
tools/include/nolibc/sys.h | 51 ++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index c4818a9c8823..46b6b3bb3b4e 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -20,6 +20,7 @@
#include <linux/time.h>
#include <linux/auxvec.h>
#include <linux/fcntl.h> // for O_* and AT_*
+#include <linux/stat.h> // for statx()

#include "arch.h"
#include "errno.h"
@@ -1048,12 +1049,61 @@ pid_t setsid(void)
return ret;
}

+/*
+ * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf);
+ */
+
+static __attribute__((unused))
+int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
+{
+ return my_syscall5(__NR_statx, fd, path, flags, mask, buf);
+}
+
+static __attribute__((unused))
+int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
+{
+ int ret = sys_statx(fd, path, flags, mask, buf);
+
+ if (ret < 0) {
+ SET_ERRNO(-ret);
+ ret = -1;
+ }
+ return ret;
+}

/*
* int stat(const char *path, struct stat *buf);
* Warning: the struct stat's layout is arch-dependent.
*/

+#ifdef __NR_statx
+static __attribute__((unused))
+int sys_stat(const char *path, struct stat *buf)
+{
+ struct statx stat;
+ long ret;
+
+ ret = sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &stat);
+ buf->st_dev = ((stat.stx_dev_minor & 0xff)
+ | (stat.stx_dev_major << 8)
+ | ((stat.stx_dev_minor & ~0xff) << 12));
+ buf->st_ino = stat.stx_ino;
+ buf->st_mode = stat.stx_mode;
+ buf->st_nlink = stat.stx_nlink;
+ buf->st_uid = stat.stx_uid;
+ buf->st_gid = stat.stx_gid;
+ buf->st_rdev = ((stat.stx_rdev_minor & 0xff)
+ | (stat.stx_rdev_major << 8)
+ | ((stat.stx_rdev_minor & ~0xff) << 12));
+ buf->st_size = stat.stx_size;
+ buf->st_blksize = stat.stx_blksize;
+ buf->st_blocks = stat.stx_blocks;
+ buf->st_atime = stat.stx_atime.tv_sec;
+ buf->st_mtime = stat.stx_mtime.tv_sec;
+ buf->st_ctime = stat.stx_ctime.tv_sec;
+ return ret;
+}
+#else
static __attribute__((unused))
int sys_stat(const char *path, struct stat *buf)
{
@@ -1083,6 +1133,7 @@ int sys_stat(const char *path, struct stat *buf)
buf->st_ctime = stat.st_ctime;
return ret;
}
+#endif /* __NR_statx */

static __attribute__((unused))
int stat(const char *path, struct stat *buf)
--
2.39.0