11cb0ef41Sopenharmony_ci#include "uv.h"
21cb0ef41Sopenharmony_ci#include "wasi_rights.h"
31cb0ef41Sopenharmony_ci#include "wasi_types.h"
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciuvwasi_errno_t uvwasi__get_rights(uv_file fd,
71cb0ef41Sopenharmony_ci                                  int flags,
81cb0ef41Sopenharmony_ci                                  uvwasi_filetype_t type,
91cb0ef41Sopenharmony_ci                                  uvwasi_rights_t* rights_base,
101cb0ef41Sopenharmony_ci                                  uvwasi_rights_t* rights_inheriting) {
111cb0ef41Sopenharmony_ci  int read_or_write_only;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  if (type == UVWASI_FILETYPE_UNKNOWN)
141cb0ef41Sopenharmony_ci    return UVWASI_EINVAL;
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  switch (type) {
171cb0ef41Sopenharmony_ci    case UVWASI_FILETYPE_REGULAR_FILE:
181cb0ef41Sopenharmony_ci      *rights_base = UVWASI__RIGHTS_REGULAR_FILE_BASE;
191cb0ef41Sopenharmony_ci      *rights_inheriting = UVWASI__RIGHTS_REGULAR_FILE_INHERITING;
201cb0ef41Sopenharmony_ci      break;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci    case UVWASI_FILETYPE_DIRECTORY:
231cb0ef41Sopenharmony_ci      *rights_base = UVWASI__RIGHTS_DIRECTORY_BASE;
241cb0ef41Sopenharmony_ci      *rights_inheriting = UVWASI__RIGHTS_DIRECTORY_INHERITING;
251cb0ef41Sopenharmony_ci      break;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    case UVWASI_FILETYPE_SOCKET_STREAM:
281cb0ef41Sopenharmony_ci    case UVWASI_FILETYPE_SOCKET_DGRAM:
291cb0ef41Sopenharmony_ci      *rights_base = UVWASI__RIGHTS_SOCKET_BASE;
301cb0ef41Sopenharmony_ci      *rights_inheriting = UVWASI__RIGHTS_SOCKET_INHERITING;
311cb0ef41Sopenharmony_ci      break;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    case UVWASI_FILETYPE_CHARACTER_DEVICE:
341cb0ef41Sopenharmony_ci      if (uv_guess_handle(fd) == UV_TTY) {
351cb0ef41Sopenharmony_ci        *rights_base = UVWASI__RIGHTS_TTY_BASE;
361cb0ef41Sopenharmony_ci        *rights_inheriting = UVWASI__RIGHTS_TTY_INHERITING;
371cb0ef41Sopenharmony_ci      } else {
381cb0ef41Sopenharmony_ci        *rights_base = UVWASI__RIGHTS_CHARACTER_DEVICE_BASE;
391cb0ef41Sopenharmony_ci        *rights_inheriting = UVWASI__RIGHTS_CHARACTER_DEVICE_INHERITING;
401cb0ef41Sopenharmony_ci      }
411cb0ef41Sopenharmony_ci      break;
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    case UVWASI_FILETYPE_BLOCK_DEVICE:
441cb0ef41Sopenharmony_ci      *rights_base = UVWASI__RIGHTS_BLOCK_DEVICE_BASE;
451cb0ef41Sopenharmony_ci      *rights_inheriting = UVWASI__RIGHTS_BLOCK_DEVICE_INHERITING;
461cb0ef41Sopenharmony_ci      break;
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci    default:
491cb0ef41Sopenharmony_ci      *rights_base = 0;
501cb0ef41Sopenharmony_ci      *rights_inheriting = 0;
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  /* Disable read/write bits depending on access mode. */
541cb0ef41Sopenharmony_ci  read_or_write_only = flags & (UV_FS_O_RDONLY | UV_FS_O_WRONLY | UV_FS_O_RDWR);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  if (read_or_write_only == UV_FS_O_RDONLY)
571cb0ef41Sopenharmony_ci    *rights_base &= ~UVWASI_RIGHT_FD_WRITE;
581cb0ef41Sopenharmony_ci  else if (read_or_write_only == UV_FS_O_WRONLY)
591cb0ef41Sopenharmony_ci    *rights_base &= ~UVWASI_RIGHT_FD_READ;
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  return UVWASI_ESUCCESS;
621cb0ef41Sopenharmony_ci}
63