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