xref: /linux/tools/include/nolibc/sys/stat.h (revision 3785289f97e2118b157332ffaae9fd2ec71237c8)
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  */
21 
22 static __attribute__((unused))
23 int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
24 {
25 #ifdef __NR_statx
26 	return my_syscall5(__NR_statx, fd, path, flags, mask, buf);
27 #else
28 	return __nolibc_enosys(__func__, fd, path, flags, mask, buf);
29 #endif
30 }
31 
32 static __attribute__((unused))
33 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
34 {
35 	return __sysret(sys_statx(fd, path, flags, mask, buf));
36 }
37 
38 
39 static __attribute__((unused))
40 int stat(const char *path, struct stat *buf)
41 {
42 	struct statx statx;
43 	long ret;
44 
45 	ret = __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
46 	if (ret == -1)
47 		return ret;
48 
49 	buf->st_dev          = ((statx.stx_dev_minor & 0xff)
50 			       | (statx.stx_dev_major << 8)
51 			       | ((statx.stx_dev_minor & ~0xff) << 12));
52 	buf->st_ino          = statx.stx_ino;
53 	buf->st_mode         = statx.stx_mode;
54 	buf->st_nlink        = statx.stx_nlink;
55 	buf->st_uid          = statx.stx_uid;
56 	buf->st_gid          = statx.stx_gid;
57 	buf->st_rdev         = ((statx.stx_rdev_minor & 0xff)
58 			       | (statx.stx_rdev_major << 8)
59 			       | ((statx.stx_rdev_minor & ~0xff) << 12));
60 	buf->st_size         = statx.stx_size;
61 	buf->st_blksize      = statx.stx_blksize;
62 	buf->st_blocks       = statx.stx_blocks;
63 	buf->st_atim.tv_sec  = statx.stx_atime.tv_sec;
64 	buf->st_atim.tv_nsec = statx.stx_atime.tv_nsec;
65 	buf->st_mtim.tv_sec  = statx.stx_mtime.tv_sec;
66 	buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec;
67 	buf->st_ctim.tv_sec  = statx.stx_ctime.tv_sec;
68 	buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec;
69 
70 	return 0;
71 }
72 
73 #endif /* _NOLIBC_SYS_STAT_H */
74