• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "uv.h"
2 #include "wasi_rights.h"
3 #include "wasi_types.h"
4 
5 
uvwasi__get_rights(uv_file fd,int flags,uvwasi_filetype_t type,uvwasi_rights_t * rights_base,uvwasi_rights_t * rights_inheriting)6 uvwasi_errno_t uvwasi__get_rights(uv_file fd,
7                                   int flags,
8                                   uvwasi_filetype_t type,
9                                   uvwasi_rights_t* rights_base,
10                                   uvwasi_rights_t* rights_inheriting) {
11   int read_or_write_only;
12 
13   if (type == UVWASI_FILETYPE_UNKNOWN)
14     return UVWASI_EINVAL;
15 
16   switch (type) {
17     case UVWASI_FILETYPE_REGULAR_FILE:
18       *rights_base = UVWASI__RIGHTS_REGULAR_FILE_BASE;
19       *rights_inheriting = UVWASI__RIGHTS_REGULAR_FILE_INHERITING;
20       break;
21 
22     case UVWASI_FILETYPE_DIRECTORY:
23       *rights_base = UVWASI__RIGHTS_DIRECTORY_BASE;
24       *rights_inheriting = UVWASI__RIGHTS_DIRECTORY_INHERITING;
25       break;
26 
27     case UVWASI_FILETYPE_SOCKET_STREAM:
28     case UVWASI_FILETYPE_SOCKET_DGRAM:
29       *rights_base = UVWASI__RIGHTS_SOCKET_BASE;
30       *rights_inheriting = UVWASI__RIGHTS_SOCKET_INHERITING;
31       break;
32 
33     case UVWASI_FILETYPE_CHARACTER_DEVICE:
34       if (uv_guess_handle(fd) == UV_TTY) {
35         *rights_base = UVWASI__RIGHTS_TTY_BASE;
36         *rights_inheriting = UVWASI__RIGHTS_TTY_INHERITING;
37       } else {
38         *rights_base = UVWASI__RIGHTS_CHARACTER_DEVICE_BASE;
39         *rights_inheriting = UVWASI__RIGHTS_CHARACTER_DEVICE_INHERITING;
40       }
41       break;
42 
43     case UVWASI_FILETYPE_BLOCK_DEVICE:
44       *rights_base = UVWASI__RIGHTS_BLOCK_DEVICE_BASE;
45       *rights_inheriting = UVWASI__RIGHTS_BLOCK_DEVICE_INHERITING;
46       break;
47 
48     default:
49       *rights_base = 0;
50       *rights_inheriting = 0;
51   }
52 
53   /* Disable read/write bits depending on access mode. */
54   read_or_write_only = flags & (UV_FS_O_RDONLY | UV_FS_O_WRONLY | UV_FS_O_RDWR);
55 
56   if (read_or_write_only == UV_FS_O_RDONLY)
57     *rights_base &= ~UVWASI_RIGHT_FD_WRITE;
58   else if (read_or_write_only == UV_FS_O_WRONLY)
59     *rights_base &= ~UVWASI_RIGHT_FD_READ;
60 
61   return UVWASI_ESUCCESS;
62 }
63