1c6e6c2c4SThomas Weißschuh /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2c6e6c2c4SThomas Weißschuh /*
3c6e6c2c4SThomas Weißschuh * stat definition for NOLIBC
4c6e6c2c4SThomas Weißschuh * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
5c6e6c2c4SThomas Weißschuh */
6c6e6c2c4SThomas Weißschuh
73785289fSThomas Weißschuh /* make sure to include all global symbols */
83785289fSThomas Weißschuh #include "../nolibc.h"
93785289fSThomas Weißschuh
10c6e6c2c4SThomas Weißschuh #ifndef _NOLIBC_SYS_STAT_H
11c6e6c2c4SThomas Weißschuh #define _NOLIBC_SYS_STAT_H
12c6e6c2c4SThomas Weißschuh
13c6e6c2c4SThomas Weißschuh #include "../arch.h"
14c6e6c2c4SThomas Weißschuh #include "../types.h"
15c6e6c2c4SThomas Weißschuh #include "../sys.h"
16c6e6c2c4SThomas Weißschuh
17c6e6c2c4SThomas Weißschuh /*
18c6e6c2c4SThomas Weißschuh * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf);
19c6e6c2c4SThomas Weißschuh * int stat(const char *path, struct stat *buf);
20*2337d39fSThomas Weißschuh * int fstatat(int fd, const char *path, struct stat *buf, int flag);
21*2337d39fSThomas Weißschuh * int fstat(int fildes, struct stat *buf);
22*2337d39fSThomas Weißschuh * int lstat(const char *path, struct stat *buf);
23c6e6c2c4SThomas Weißschuh */
24c6e6c2c4SThomas Weißschuh
25c6e6c2c4SThomas Weißschuh static __attribute__((unused))
sys_statx(int fd,const char * path,int flags,unsigned int mask,struct statx * buf)26c6e6c2c4SThomas Weißschuh int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
27c6e6c2c4SThomas Weißschuh {
28c6e6c2c4SThomas Weißschuh #ifdef __NR_statx
29c6e6c2c4SThomas Weißschuh return my_syscall5(__NR_statx, fd, path, flags, mask, buf);
30c6e6c2c4SThomas Weißschuh #else
31c6e6c2c4SThomas Weißschuh return __nolibc_enosys(__func__, fd, path, flags, mask, buf);
32c6e6c2c4SThomas Weißschuh #endif
33c6e6c2c4SThomas Weißschuh }
34c6e6c2c4SThomas Weißschuh
35c6e6c2c4SThomas Weißschuh static __attribute__((unused))
statx(int fd,const char * path,int flags,unsigned int mask,struct statx * buf)36c6e6c2c4SThomas Weißschuh int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
37c6e6c2c4SThomas Weißschuh {
38c6e6c2c4SThomas Weißschuh return __sysret(sys_statx(fd, path, flags, mask, buf));
39c6e6c2c4SThomas Weißschuh }
40c6e6c2c4SThomas Weißschuh
41c6e6c2c4SThomas Weißschuh
42c6e6c2c4SThomas Weißschuh static __attribute__((unused))
fstatat(int fd,const char * path,struct stat * buf,int flag)43*2337d39fSThomas Weißschuh int fstatat(int fd, const char *path, struct stat *buf, int flag)
44c6e6c2c4SThomas Weißschuh {
45c6e6c2c4SThomas Weißschuh struct statx statx;
46c6e6c2c4SThomas Weißschuh long ret;
47c6e6c2c4SThomas Weißschuh
48*2337d39fSThomas Weißschuh ret = __sysret(sys_statx(fd, path, flag | AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
49c6e6c2c4SThomas Weißschuh if (ret == -1)
50c6e6c2c4SThomas Weißschuh return ret;
51c6e6c2c4SThomas Weißschuh
52c6e6c2c4SThomas Weißschuh buf->st_dev = ((statx.stx_dev_minor & 0xff)
53c6e6c2c4SThomas Weißschuh | (statx.stx_dev_major << 8)
54c6e6c2c4SThomas Weißschuh | ((statx.stx_dev_minor & ~0xff) << 12));
55c6e6c2c4SThomas Weißschuh buf->st_ino = statx.stx_ino;
56c6e6c2c4SThomas Weißschuh buf->st_mode = statx.stx_mode;
57c6e6c2c4SThomas Weißschuh buf->st_nlink = statx.stx_nlink;
58c6e6c2c4SThomas Weißschuh buf->st_uid = statx.stx_uid;
59c6e6c2c4SThomas Weißschuh buf->st_gid = statx.stx_gid;
60c6e6c2c4SThomas Weißschuh buf->st_rdev = ((statx.stx_rdev_minor & 0xff)
61c6e6c2c4SThomas Weißschuh | (statx.stx_rdev_major << 8)
62c6e6c2c4SThomas Weißschuh | ((statx.stx_rdev_minor & ~0xff) << 12));
63c6e6c2c4SThomas Weißschuh buf->st_size = statx.stx_size;
64c6e6c2c4SThomas Weißschuh buf->st_blksize = statx.stx_blksize;
65c6e6c2c4SThomas Weißschuh buf->st_blocks = statx.stx_blocks;
66c6e6c2c4SThomas Weißschuh buf->st_atim.tv_sec = statx.stx_atime.tv_sec;
67c6e6c2c4SThomas Weißschuh buf->st_atim.tv_nsec = statx.stx_atime.tv_nsec;
68c6e6c2c4SThomas Weißschuh buf->st_mtim.tv_sec = statx.stx_mtime.tv_sec;
69c6e6c2c4SThomas Weißschuh buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec;
70c6e6c2c4SThomas Weißschuh buf->st_ctim.tv_sec = statx.stx_ctime.tv_sec;
71c6e6c2c4SThomas Weißschuh buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec;
72c6e6c2c4SThomas Weißschuh
73c6e6c2c4SThomas Weißschuh return 0;
74c6e6c2c4SThomas Weißschuh }
75c6e6c2c4SThomas Weißschuh
76*2337d39fSThomas Weißschuh static __attribute__((unused))
stat(const char * path,struct stat * buf)77*2337d39fSThomas Weißschuh int stat(const char *path, struct stat *buf)
78*2337d39fSThomas Weißschuh {
79*2337d39fSThomas Weißschuh return fstatat(AT_FDCWD, path, buf, 0);
80*2337d39fSThomas Weißschuh }
81*2337d39fSThomas Weißschuh
82*2337d39fSThomas Weißschuh static __attribute__((unused))
fstat(int fildes,struct stat * buf)83*2337d39fSThomas Weißschuh int fstat(int fildes, struct stat *buf)
84*2337d39fSThomas Weißschuh {
85*2337d39fSThomas Weißschuh return fstatat(fildes, "", buf, AT_EMPTY_PATH);
86*2337d39fSThomas Weißschuh }
87*2337d39fSThomas Weißschuh
88*2337d39fSThomas Weißschuh static __attribute__((unused))
lstat(const char * path,struct stat * buf)89*2337d39fSThomas Weißschuh int lstat(const char *path, struct stat *buf)
90*2337d39fSThomas Weißschuh {
91*2337d39fSThomas Weißschuh return fstatat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
92*2337d39fSThomas Weißschuh }
93*2337d39fSThomas Weißschuh
94c6e6c2c4SThomas Weißschuh #endif /* _NOLIBC_SYS_STAT_H */
95