]> git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/cloudlibc/src/libc/unistd/faccessat.c
Update README and add CI-tests for minimal supported LLVM-version (10) (#302)
[wasi-libc.git] / libc-bottom-half / cloudlibc / src / libc / unistd / faccessat.c
1 // Copyright (c) 2016 Nuxi, https://nuxi.nl/
2 //
3 // SPDX-License-Identifier: BSD-2-Clause
4
5 #include <common/errno.h>
6
7 #include <wasi/api.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 int __wasilibc_nocwd_faccessat(int fd, const char *path, int amode, int flag) {
14 // Validate function parameters.
15 if ((amode & ~(F_OK | R_OK | W_OK | X_OK)) != 0 ||
16 (flag & ~AT_EACCESS) != 0) {
17 errno = EINVAL;
18 return -1;
19 }
20
21 // Check for target file existence and obtain the file type.
22 __wasi_lookupflags_t lookup_flags = __WASI_LOOKUPFLAGS_SYMLINK_FOLLOW;
23 __wasi_filestat_t file;
24 __wasi_errno_t error =
25 __wasi_path_filestat_get(fd, lookup_flags, path, &file);
26 if (error != 0) {
27 errno = errno_fixup_directory(fd, error);
28 return -1;
29 }
30
31 // Test whether the requested access rights are present on the
32 // directory file descriptor.
33 if (amode != 0) {
34 __wasi_fdstat_t directory;
35 error = __wasi_fd_fdstat_get(fd, &directory);
36 if (error != 0) {
37 errno = error;
38 return -1;
39 }
40
41 __wasi_rights_t min = 0;
42 if ((amode & R_OK) != 0)
43 min |= file.filetype == __WASI_FILETYPE_DIRECTORY
44 ? __WASI_RIGHTS_FD_READDIR
45 : __WASI_RIGHTS_FD_READ;
46 if ((amode & W_OK) != 0)
47 min |= __WASI_RIGHTS_FD_WRITE;
48
49 if ((min & directory.fs_rights_inheriting) != min) {
50 errno = EACCES;
51 return -1;
52 }
53 }
54 return 0;
55 }