xref: /linux/tools/include/nolibc/sys/stat.h (revision e2683c8868d03382da7e1ce8453b543a043066d1)
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 #include "../sys/sysmacros.h"
17 
18 /*
19  * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf);
20  * int stat(const char *path, struct stat *buf);
21  * int fstatat(int fd, const char *path, struct stat *buf, int flag);
22  * int fstat(int fildes, struct stat *buf);
23  * int lstat(const char *path, struct stat *buf);
24  */
25 
26 static __attribute__((unused))
27 int _sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
28 {
29 #ifdef __NR_statx
30 	return __nolibc_syscall5(__NR_statx, fd, path, flags, mask, buf);
31 #else
32 	return __nolibc_enosys(__func__, fd, path, flags, mask, buf);
33 #endif
34 }
35 
36 static __attribute__((unused))
37 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
38 {
39 	return __sysret(_sys_statx(fd, path, flags, mask, buf));
40 }
41 
42 
43 static __attribute__((unused))
44 int fstatat(int fd, const char *path, struct stat *buf, int flag)
45 {
46 	struct statx statx;
47 	long ret;
48 
49 	ret = __sysret(_sys_statx(fd, path, flag | AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
50 	if (ret == -1)
51 		return ret;
52 
53 	buf->st_dev          = makedev(statx.stx_dev_major, statx.stx_dev_minor);
54 	buf->st_ino          = statx.stx_ino;
55 	buf->st_mode         = statx.stx_mode;
56 	buf->st_nlink        = statx.stx_nlink;
57 	buf->st_uid          = statx.stx_uid;
58 	buf->st_gid          = statx.stx_gid;
59 	buf->st_rdev         = makedev(statx.stx_rdev_major, statx.stx_rdev_minor);
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 static __attribute__((unused))
74 int stat(const char *path, struct stat *buf)
75 {
76 	return fstatat(AT_FDCWD, path, buf, 0);
77 }
78 
79 static __attribute__((unused))
80 int fstat(int fildes, struct stat *buf)
81 {
82 	return fstatat(fildes, "", buf, AT_EMPTY_PATH);
83 }
84 
85 static __attribute__((unused))
86 int lstat(const char *path, struct stat *buf)
87 {
88 	return fstatat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
89 }
90 
91 #endif /* _NOLIBC_SYS_STAT_H */
92