Browse Source

Rename `OFlag`/`AtFlag` to `OFlags`/`AtFlags`. (#1951)

* Rename `OFlag`/`AtFlag` to `OFlags`/`AtFlags`.

This makes them consistent with `PollFlags` and common usage of
bitflags types in Rust code in general.

POSIX does tend to use names like `oflag` and `flag`, so this is in mild
disagreement with POSIX style, however I find this particular aspects of
POSIX confusing because these values hold multiple flags.

* rustfmt
pull/1959/head
Dan Gohman 4 years ago
committed by GitHub
parent
commit
b37adbbe31
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/host_impl.rs
  2. 14
      crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/hostcalls_impl.rs
  3. 2
      crates/wasi-common/src/old/snapshot_0/sys/unix/emscripten/host_impl.rs
  4. 8
      crates/wasi-common/src/old/snapshot_0/sys/unix/entry_impl.rs
  5. 36
      crates/wasi-common/src/old/snapshot_0/sys/unix/host_impl.rs
  6. 32
      crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs.rs
  7. 12
      crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs_helpers.rs
  8. 2
      crates/wasi-common/src/old/snapshot_0/sys/unix/linux/host_impl.rs
  9. 4
      crates/wasi-common/src/old/snapshot_0/sys/unix/linux/hostcalls_impl.rs
  10. 2
      crates/wasi-common/src/sys/unix/bsd/mod.rs
  11. 14
      crates/wasi-common/src/sys/unix/bsd/path.rs
  12. 2
      crates/wasi-common/src/sys/unix/emscripten/mod.rs
  13. 2
      crates/wasi-common/src/sys/unix/linux/mod.rs
  14. 4
      crates/wasi-common/src/sys/unix/linux/path.rs
  15. 28
      crates/wasi-common/src/sys/unix/mod.rs
  16. 8
      crates/wasi-common/src/sys/unix/osdir.rs
  17. 8
      crates/wasi-common/src/sys/unix/osfile.rs
  18. 48
      crates/wasi-common/src/sys/unix/path.rs
  19. 14
      crates/wasi-common/yanix/src/fcntl.rs
  20. 18
      crates/wasi-common/yanix/src/file.rs

2
crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/host_impl.rs

@ -1,7 +1,7 @@
use crate::old::snapshot_0::wasi::{self, WasiResult};
use std::convert::TryFrom;
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::SYNC;
pub(crate) const O_RSYNC: yanix::file::OFlags = yanix::file::OFlags::SYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> WasiResult<wasi::__wasi_device_t> {
wasi::__wasi_device_t::try_from(dev).map_err(Into::into)

14
crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/hostcalls_impl.rs

@ -3,12 +3,12 @@ use crate::old::snapshot_0::wasi::{WasiError, WasiResult};
use std::os::unix::prelude::AsRawFd;
pub(crate) fn path_unlink_file(resolved: PathGet) -> WasiResult<()> {
use yanix::file::{unlinkat, AtFlag};
use yanix::file::{unlinkat, AtFlags};
match unsafe {
unlinkat(
resolved.dirfd().as_raw_fd(),
resolved.path(),
AtFlag::empty(),
AtFlags::empty(),
)
} {
Err(err) => {
@ -27,7 +27,7 @@ pub(crate) fn path_unlink_file(resolved: PathGet) -> WasiResult<()> {
fstatat(
resolved.dirfd().as_raw_fd(),
resolved.path(),
AtFlag::SYMLINK_NOFOLLOW,
AtFlags::SYMLINK_NOFOLLOW,
)
} {
Ok(stat) => {
@ -48,7 +48,7 @@ pub(crate) fn path_unlink_file(resolved: PathGet) -> WasiResult<()> {
}
pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> WasiResult<()> {
use yanix::file::{fstatat, symlinkat, AtFlag};
use yanix::file::{fstatat, symlinkat, AtFlags};
log::debug!("path_symlink old_path = {:?}", old_path);
log::debug!("path_symlink resolved = {:?}", resolved);
@ -66,7 +66,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> WasiResult<()>
fstatat(
resolved.dirfd().as_raw_fd(),
new_path,
AtFlag::SYMLINK_NOFOLLOW,
AtFlags::SYMLINK_NOFOLLOW,
)
} {
Ok(_) => return Err(WasiError::EEXIST),
@ -82,7 +82,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> WasiResult<()>
}
pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> WasiResult<()> {
use yanix::file::{fstatat, renameat, AtFlag};
use yanix::file::{fstatat, renameat, AtFlags};
match unsafe {
renameat(
resolved_old.dirfd().as_raw_fd(),
@ -107,7 +107,7 @@ pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> WasiR
fstatat(
resolved_old.dirfd().as_raw_fd(),
resolved_old.path(),
AtFlag::SYMLINK_NOFOLLOW,
AtFlags::SYMLINK_NOFOLLOW,
)
} {
Ok(_) => {

2
crates/wasi-common/src/old/snapshot_0/sys/unix/emscripten/host_impl.rs

@ -1,6 +1,6 @@
use crate::old::snapshot_0::wasi::{self, WasiResult};
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
pub(crate) const O_RSYNC: yanix::file::OFlags = yanix::file::OFlags::RSYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> WasiResult<wasi::__wasi_device_t> {
Ok(wasi::__wasi_device_t::from(dev))

8
crates/wasi-common/src/old/snapshot_0/sys/unix/entry_impl.rs

@ -36,12 +36,12 @@ pub(crate) unsafe fn determine_type_and_access_rights<Fd: AsRawFd>(
)> {
let (file_type, mut rights_base, rights_inheriting) = determine_type_rights(fd)?;
use yanix::{fcntl, file::OFlag};
use yanix::{fcntl, file::OFlags};
let flags = fcntl::get_status_flags(fd.as_raw_fd())?;
let accmode = flags & OFlag::ACCMODE;
if accmode == OFlag::RDONLY {
let accmode = flags & OFlags::ACCMODE;
if accmode == OFlags::RDONLY {
rights_base &= !wasi::__WASI_RIGHTS_FD_WRITE;
} else if accmode == OFlag::WRONLY {
} else if accmode == OFlags::WRONLY {
rights_base &= !wasi::__WASI_RIGHTS_FD_READ;
}

36
crates/wasi-common/src/old/snapshot_0/sys/unix/host_impl.rs

@ -8,7 +8,7 @@ use crate::old::snapshot_0::{helpers, sys::unix::sys_impl};
use std::ffi::OsStr;
use std::io;
use std::os::unix::prelude::OsStrExt;
use yanix::file::OFlag;
use yanix::file::OFlags;
pub(crate) use sys_impl::host_impl::*;
@ -104,59 +104,59 @@ impl From<io::Error> for WasiError {
}
}
pub(crate) fn nix_from_fdflags(fdflags: wasi::__wasi_fdflags_t) -> OFlag {
let mut nix_flags = OFlag::empty();
pub(crate) fn nix_from_fdflags(fdflags: wasi::__wasi_fdflags_t) -> OFlags {
let mut nix_flags = OFlags::empty();
if fdflags & wasi::__WASI_FDFLAGS_APPEND != 0 {
nix_flags.insert(OFlag::APPEND);
nix_flags.insert(OFlags::APPEND);
}
if fdflags & wasi::__WASI_FDFLAGS_DSYNC != 0 {
nix_flags.insert(OFlag::DSYNC);
nix_flags.insert(OFlags::DSYNC);
}
if fdflags & wasi::__WASI_FDFLAGS_NONBLOCK != 0 {
nix_flags.insert(OFlag::NONBLOCK);
nix_flags.insert(OFlags::NONBLOCK);
}
if fdflags & wasi::__WASI_FDFLAGS_RSYNC != 0 {
nix_flags.insert(O_RSYNC);
}
if fdflags & wasi::__WASI_FDFLAGS_SYNC != 0 {
nix_flags.insert(OFlag::SYNC);
nix_flags.insert(OFlags::SYNC);
}
nix_flags
}
pub(crate) fn fdflags_from_nix(oflags: OFlag) -> wasi::__wasi_fdflags_t {
pub(crate) fn fdflags_from_nix(oflags: OFlags) -> wasi::__wasi_fdflags_t {
let mut fdflags = 0;
if oflags.contains(OFlag::APPEND) {
if oflags.contains(OFlags::APPEND) {
fdflags |= wasi::__WASI_FDFLAGS_APPEND;
}
if oflags.contains(OFlag::DSYNC) {
if oflags.contains(OFlags::DSYNC) {
fdflags |= wasi::__WASI_FDFLAGS_DSYNC;
}
if oflags.contains(OFlag::NONBLOCK) {
if oflags.contains(OFlags::NONBLOCK) {
fdflags |= wasi::__WASI_FDFLAGS_NONBLOCK;
}
if oflags.contains(O_RSYNC) {
fdflags |= wasi::__WASI_FDFLAGS_RSYNC;
}
if oflags.contains(OFlag::SYNC) {
if oflags.contains(OFlags::SYNC) {
fdflags |= wasi::__WASI_FDFLAGS_SYNC;
}
fdflags
}
pub(crate) fn nix_from_oflags(oflags: wasi::__wasi_oflags_t) -> OFlag {
let mut nix_flags = OFlag::empty();
pub(crate) fn nix_from_oflags(oflags: wasi::__wasi_oflags_t) -> OFlags {
let mut nix_flags = OFlags::empty();
if oflags & wasi::__WASI_OFLAGS_CREAT != 0 {
nix_flags.insert(OFlag::CREAT);
nix_flags.insert(OFlags::CREAT);
}
if oflags & wasi::__WASI_OFLAGS_DIRECTORY != 0 {
nix_flags.insert(OFlag::DIRECTORY);
nix_flags.insert(OFlags::DIRECTORY);
}
if oflags & wasi::__WASI_OFLAGS_EXCL != 0 {
nix_flags.insert(OFlag::EXCL);
nix_flags.insert(OFlags::EXCL);
}
if oflags & wasi::__WASI_OFLAGS_TRUNC != 0 {
nix_flags.insert(OFlag::TRUNC);
nix_flags.insert(OFlags::TRUNC);
}
nix_flags
}

32
crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs.rs

@ -72,14 +72,14 @@ pub(crate) fn path_create_directory(resolved: PathGet) -> WasiResult<()> {
}
pub(crate) fn path_link(resolved_old: PathGet, resolved_new: PathGet) -> WasiResult<()> {
use yanix::file::{linkat, AtFlag};
use yanix::file::{linkat, AtFlags};
unsafe {
linkat(
resolved_old.dirfd().as_raw_fd(),
resolved_old.path(),
resolved_new.dirfd().as_raw_fd(),
resolved_new.path(),
AtFlag::SYMLINK_FOLLOW,
AtFlags::SYMLINK_FOLLOW,
)
}
.map_err(Into::into)
@ -92,18 +92,18 @@ pub(crate) fn path_open(
oflags: wasi::__wasi_oflags_t,
fs_flags: wasi::__wasi_fdflags_t,
) -> WasiResult<File> {
use yanix::file::{fstatat, openat, AtFlag, FileType, Mode, OFlag};
use yanix::file::{fstatat, openat, AtFlags, FileType, Mode, OFlags};
let mut nix_all_oflags = if read && write {
OFlag::RDWR
OFlags::RDWR
} else if write {
OFlag::WRONLY
OFlags::WRONLY
} else {
OFlag::RDONLY
OFlags::RDONLY
};
// on non-Capsicum systems, we always want nofollow
nix_all_oflags.insert(OFlag::NOFOLLOW);
nix_all_oflags.insert(OFlags::NOFOLLOW);
// convert open flags
nix_all_oflags.insert(host_impl::nix_from_oflags(oflags));
@ -135,7 +135,7 @@ pub(crate) fn path_open(
fstatat(
resolved.dirfd().as_raw_fd(),
resolved.path(),
AtFlag::SYMLINK_NOFOLLOW,
AtFlags::SYMLINK_NOFOLLOW,
)
} {
Ok(stat) => {
@ -151,13 +151,13 @@ pub(crate) fn path_open(
// Linux returns ENOTDIR instead of ELOOP when using O_NOFOLLOW|O_DIRECTORY
// on a symlink.
libc::ENOTDIR
if !(nix_all_oflags & (OFlag::NOFOLLOW | OFlag::DIRECTORY)).is_empty() =>
if !(nix_all_oflags & (OFlags::NOFOLLOW | OFlags::DIRECTORY)).is_empty() =>
{
match unsafe {
fstatat(
resolved.dirfd().as_raw_fd(),
resolved.path(),
AtFlag::SYMLINK_NOFOLLOW,
AtFlags::SYMLINK_NOFOLLOW,
)
} {
Ok(stat) => {
@ -172,7 +172,7 @@ pub(crate) fn path_open(
}
// FreeBSD returns EMLINK instead of ELOOP when using O_NOFOLLOW on
// a symlink.
libc::EMLINK if !(nix_all_oflags & OFlag::NOFOLLOW).is_empty() => {
libc::EMLINK if !(nix_all_oflags & OFlags::NOFOLLOW).is_empty() => {
return Err(WasiError::ELOOP);
}
_ => {}
@ -212,10 +212,10 @@ pub(crate) fn path_filestat_get(
resolved: PathGet,
dirflags: wasi::__wasi_lookupflags_t,
) -> WasiResult<wasi::__wasi_filestat_t> {
use yanix::file::{fstatat, AtFlag};
use yanix::file::{fstatat, AtFlags};
let atflags = match dirflags {
0 => AtFlag::empty(),
_ => AtFlag::SYMLINK_NOFOLLOW,
0 => AtFlags::empty(),
_ => AtFlags::SYMLINK_NOFOLLOW,
};
unsafe { fstatat(resolved.dirfd().as_raw_fd(), resolved.path(), atflags) }
.map_err(Into::into)
@ -270,12 +270,12 @@ pub(crate) fn path_filestat_set_times(
}
pub(crate) fn path_remove_directory(resolved: PathGet) -> WasiResult<()> {
use yanix::file::{unlinkat, AtFlag};
use yanix::file::{unlinkat, AtFlags};
unsafe {
unlinkat(
resolved.dirfd().as_raw_fd(),
resolved.path(),
AtFlag::REMOVEDIR,
AtFlags::REMOVEDIR,
)
}
.map_err(Into::into)

12
crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs_helpers.rs

@ -3,7 +3,7 @@
use crate::old::snapshot_0::sys::host_impl;
use crate::old::snapshot_0::wasi::{self, WasiResult};
use std::fs::File;
use yanix::file::OFlag;
use yanix::file::OFlags;
pub(crate) fn path_open_rights(
rights_base: wasi::__wasi_rights_t,
@ -17,19 +17,19 @@ pub(crate) fn path_open_rights(
// convert open flags
let oflags = host_impl::nix_from_oflags(oflags);
if oflags.contains(OFlag::CREAT) {
if oflags.contains(OFlags::CREAT) {
needed_base |= wasi::__WASI_RIGHTS_PATH_CREATE_FILE;
}
if oflags.contains(OFlag::TRUNC) {
if oflags.contains(OFlags::TRUNC) {
needed_base |= wasi::__WASI_RIGHTS_PATH_FILESTAT_SET_SIZE;
}
// convert file descriptor flags
let fdflags = host_impl::nix_from_fdflags(fs_flags);
if fdflags.contains(OFlag::DSYNC) {
if fdflags.contains(OFlags::DSYNC) {
needed_inheriting |= wasi::__WASI_RIGHTS_FD_DATASYNC;
}
if fdflags.intersects(host_impl::O_RSYNC | OFlag::SYNC) {
if fdflags.intersects(host_impl::O_RSYNC | OFlags::SYNC) {
needed_inheriting |= wasi::__WASI_RIGHTS_FD_SYNC;
}
@ -46,7 +46,7 @@ pub(crate) fn openat(dirfd: &File, path: &str) -> WasiResult<File> {
openat(
dirfd.as_raw_fd(),
path,
OFlag::RDONLY | OFlag::DIRECTORY | OFlag::NOFOLLOW,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW,
Mode::empty(),
)
}

2
crates/wasi-common/src/old/snapshot_0/sys/unix/linux/host_impl.rs

@ -1,7 +1,7 @@
use crate::old::snapshot_0::wasi::{self, WasiResult};
use std::convert::TryInto;
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
pub(crate) const O_RSYNC: yanix::file::OFlags = yanix::file::OFlags::RSYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> WasiResult<wasi::__wasi_device_t> {
Ok(wasi::__wasi_device_t::from(dev))

4
crates/wasi-common/src/old/snapshot_0/sys/unix/linux/hostcalls_impl.rs

@ -3,12 +3,12 @@ use crate::old::snapshot_0::wasi::WasiResult;
use std::os::unix::prelude::AsRawFd;
pub(crate) fn path_unlink_file(resolved: PathGet) -> WasiResult<()> {
use yanix::file::{unlinkat, AtFlag};
use yanix::file::{unlinkat, AtFlags};
unsafe {
unlinkat(
resolved.dirfd().as_raw_fd(),
resolved.path(),
AtFlag::empty(),
AtFlags::empty(),
)
}
.map_err(Into::into)

2
crates/wasi-common/src/sys/unix/bsd/mod.rs

@ -1,4 +1,4 @@
pub(crate) mod osdir;
pub(crate) mod path;
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::SYNC;
pub(crate) const O_RSYNC: yanix::file::OFlags = yanix::file::OFlags::SYNC;

14
crates/wasi-common/src/sys/unix/bsd/path.rs

@ -3,8 +3,8 @@ use crate::wasi::{Errno, Result};
use std::os::unix::prelude::AsRawFd;
pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
use yanix::file::{unlinkat, AtFlag};
match unsafe { unlinkat(dirfd.as_raw_fd(), path, AtFlag::empty()) } {
use yanix::file::{unlinkat, AtFlags};
match unsafe { unlinkat(dirfd.as_raw_fd(), path, AtFlags::empty()) } {
Err(err) => {
let raw_errno = err.raw_os_error().unwrap();
// Non-Linux implementations may return EPERM when attempting to remove a
@ -17,7 +17,7 @@ pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
use yanix::file::{fstatat, FileType};
if raw_errno == libc::EPERM {
match unsafe { fstatat(dirfd.as_raw_fd(), path, AtFlag::SYMLINK_NOFOLLOW) } {
match unsafe { fstatat(dirfd.as_raw_fd(), path, AtFlags::SYMLINK_NOFOLLOW) } {
Ok(stat) => {
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Directory {
return Err(Errno::Isdir);
@ -36,7 +36,7 @@ pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
}
pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path: &str) -> Result<()> {
use yanix::file::{fstatat, symlinkat, AtFlag};
use yanix::file::{fstatat, symlinkat, AtFlags};
log::debug!("path_symlink old_path = {:?}", old_path);
log::debug!(
@ -54,7 +54,7 @@ pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path: &str) -> Resu
// the trailing slash and check if the path exists, and
// adjust the error code appropriately.
let new_path = new_path.trim_end_matches('/');
match unsafe { fstatat(new_dirfd.as_raw_fd(), new_path, AtFlag::SYMLINK_NOFOLLOW) }
match unsafe { fstatat(new_dirfd.as_raw_fd(), new_path, AtFlags::SYMLINK_NOFOLLOW) }
{
Ok(_) => return Err(Errno::Exist),
Err(err) => {
@ -74,7 +74,7 @@ pub(crate) fn rename(
new_dirfd: &OsDir,
new_path: &str,
) -> Result<()> {
use yanix::file::{fstatat, renameat, AtFlag};
use yanix::file::{fstatat, renameat, AtFlags};
match unsafe {
renameat(
old_dirfd.as_raw_fd(),
@ -95,7 +95,7 @@ pub(crate) fn rename(
// Verify on other BSD-based OSes.
if err.raw_os_error().unwrap() == libc::ENOENT {
// check if the source path exists
match unsafe { fstatat(old_dirfd.as_raw_fd(), old_path, AtFlag::SYMLINK_NOFOLLOW) }
match unsafe { fstatat(old_dirfd.as_raw_fd(), old_path, AtFlags::SYMLINK_NOFOLLOW) }
{
Ok(_) => {
// check if destination contains a trailing slash

2
crates/wasi-common/src/sys/unix/emscripten/mod.rs

@ -3,4 +3,4 @@ pub(crate) mod osdir;
#[path = "../linux/path.rs"]
pub(crate) mod path;
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
pub(crate) const O_RSYNC: yanix::file::OFlags = yanix::file::OFlags::RSYNC;

2
crates/wasi-common/src/sys/unix/linux/mod.rs

@ -1,4 +1,4 @@
pub(crate) mod osdir;
pub(crate) mod path;
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
pub(crate) const O_RSYNC: yanix::file::OFlags = yanix::file::OFlags::RSYNC;

4
crates/wasi-common/src/sys/unix/linux/path.rs

@ -3,8 +3,8 @@ use crate::wasi::Result;
use std::os::unix::prelude::AsRawFd;
pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
use yanix::file::{unlinkat, AtFlag};
unsafe { unlinkat(dirfd.as_raw_fd(), path, AtFlag::empty())? };
use yanix::file::{unlinkat, AtFlags};
unsafe { unlinkat(dirfd.as_raw_fd(), path, AtFlags::empty())? };
Ok(())
}

28
crates/wasi-common/src/sys/unix/mod.rs

@ -36,7 +36,7 @@ use std::mem::ManuallyDrop;
use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd};
use std::path::Path;
use yanix::clock::ClockId;
use yanix::file::{AtFlag, OFlag};
use yanix::file::{AtFlags, OFlags};
pub(crate) use sys_impl::*;
@ -80,7 +80,7 @@ pub(super) fn get_file_type(file: &File) -> io::Result<types::Filetype> {
}
pub(super) fn get_rights(file: &File, file_type: &types::Filetype) -> io::Result<HandleRights> {
use yanix::{fcntl, file::OFlag};
use yanix::{fcntl, file::OFlags};
let (base, inheriting) = match file_type {
types::Filetype::BlockDevice => (
types::Rights::block_device_base(),
@ -116,10 +116,10 @@ pub(super) fn get_rights(file: &File, file_type: &types::Filetype) -> io::Result
};
let mut rights = HandleRights::new(base, inheriting);
let flags = unsafe { fcntl::get_status_flags(file.as_raw_fd())? };
let accmode = flags & OFlag::ACCMODE;
if accmode == OFlag::RDONLY {
let accmode = flags & OFlags::ACCMODE;
if accmode == OFlags::RDONLY {
rights.base &= !types::Rights::FD_WRITE;
} else if accmode == OFlag::WRONLY {
} else if accmode == OFlags::WRONLY {
rights.base &= !types::Rights::FD_READ;
}
Ok(rights)
@ -233,7 +233,7 @@ impl From<io::Error> for Errno {
}
}
impl From<types::Fdflags> for OFlag {
impl From<types::Fdflags> for OFlags {
fn from(fdflags: types::Fdflags) -> Self {
let mut nix_flags = Self::empty();
if fdflags.contains(&types::Fdflags::APPEND) {
@ -255,29 +255,29 @@ impl From<types::Fdflags> for OFlag {
}
}
impl From<OFlag> for types::Fdflags {
fn from(oflags: OFlag) -> Self {
impl From<OFlags> for types::Fdflags {
fn from(oflags: OFlags) -> Self {
let mut fdflags = Self::empty();
if oflags.contains(OFlag::APPEND) {
if oflags.contains(OFlags::APPEND) {
fdflags |= Self::APPEND;
}
if oflags.contains(OFlag::DSYNC) {
if oflags.contains(OFlags::DSYNC) {
fdflags |= Self::DSYNC;
}
if oflags.contains(OFlag::NONBLOCK) {
if oflags.contains(OFlags::NONBLOCK) {
fdflags |= Self::NONBLOCK;
}
if oflags.contains(O_RSYNC) {
fdflags |= Self::RSYNC;
}
if oflags.contains(OFlag::SYNC) {
if oflags.contains(OFlags::SYNC) {
fdflags |= Self::SYNC;
}
fdflags
}
}
impl From<types::Oflags> for OFlag {
impl From<types::Oflags> for OFlags {
fn from(oflags: types::Oflags) -> Self {
let mut nix_flags = Self::empty();
if oflags.contains(&types::Oflags::CREAT) {
@ -355,7 +355,7 @@ impl From<yanix::file::FileType> for types::Filetype {
}
}
impl From<types::Lookupflags> for AtFlag {
impl From<types::Lookupflags> for AtFlags {
fn from(flags: types::Lookupflags) -> Self {
match flags {
types::Lookupflags::SYMLINK_FOLLOW => Self::empty(),

8
crates/wasi-common/src/sys/unix/osdir.rs

@ -23,16 +23,16 @@ impl TryFrom<File> for OsDir {
}
fn get_rights(file: &File) -> io::Result<HandleRights> {
use yanix::{fcntl, file::OFlag};
use yanix::{fcntl, file::OFlags};
let mut rights = HandleRights::new(
types::Rights::directory_base(),
types::Rights::directory_inheriting(),
);
let flags = unsafe { fcntl::get_status_flags(file.as_raw_fd())? };
let accmode = flags & OFlag::ACCMODE;
if accmode == OFlag::RDONLY {
let accmode = flags & OFlags::ACCMODE;
if accmode == OFlags::RDONLY {
rights.base &= !types::Rights::FD_WRITE;
} else if accmode == OFlag::WRONLY {
} else if accmode == OFlags::WRONLY {
rights.base &= !types::Rights::FD_READ;
}
Ok(rights)

8
crates/wasi-common/src/sys/unix/osfile.rs

@ -22,16 +22,16 @@ impl TryFrom<File> for OsFile {
}
fn get_rights(file: &File) -> io::Result<HandleRights> {
use yanix::{fcntl, file::OFlag};
use yanix::{fcntl, file::OFlags};
let mut rights = HandleRights::new(
types::Rights::regular_file_base(),
types::Rights::regular_file_inheriting(),
);
let flags = unsafe { fcntl::get_status_flags(file.as_raw_fd())? };
let accmode = flags & OFlag::ACCMODE;
if accmode == OFlag::RDONLY {
let accmode = flags & OFlags::ACCMODE;
if accmode == OFlags::RDONLY {
rights.base &= !types::Rights::FD_WRITE;
} else if accmode == OFlag::WRONLY {
} else if accmode == OFlags::WRONLY {
rights.base &= !types::Rights::FD_READ;
}
Ok(rights)

48
crates/wasi-common/src/sys/unix/path.rs

@ -7,7 +7,7 @@ use std::ffi::OsStr;
use std::fs::File;
use std::os::unix::prelude::{AsRawFd, FromRawFd, OsStrExt};
use std::str;
use yanix::file::OFlag;
use yanix::file::OFlags;
pub(crate) use super::sys_impl::path::*;
@ -30,20 +30,20 @@ pub(crate) fn open_rights(
let mut needed_inheriting = input_rights.base | input_rights.inheriting;
// convert open flags
let oflags: OFlag = oflags.into();
if oflags.contains(OFlag::CREAT) {
let oflags: OFlags = oflags.into();
if oflags.contains(OFlags::CREAT) {
needed_base |= types::Rights::PATH_CREATE_FILE;
}
if oflags.contains(OFlag::TRUNC) {
if oflags.contains(OFlags::TRUNC) {
needed_base |= types::Rights::PATH_FILESTAT_SET_SIZE;
}
// convert file descriptor flags
let fdflags: OFlag = fs_flags.into();
if fdflags.contains(OFlag::DSYNC) {
let fdflags: OFlags = fs_flags.into();
if fdflags.contains(OFlags::DSYNC) {
needed_inheriting |= types::Rights::FD_DATASYNC;
}
if fdflags.intersects(super::O_RSYNC | OFlag::SYNC) {
if fdflags.intersects(super::O_RSYNC | OFlags::SYNC) {
needed_inheriting |= types::Rights::FD_SYNC;
}
@ -74,11 +74,11 @@ pub(crate) fn link(
new_path: &str,
follow_symlinks: bool,
) -> Result<()> {
use yanix::file::{linkat, AtFlag};
use yanix::file::{linkat, AtFlags};
let flags = if follow_symlinks {
AtFlag::SYMLINK_FOLLOW
AtFlags::SYMLINK_FOLLOW
} else {
AtFlag::empty()
AtFlags::empty()
};
unsafe {
linkat(
@ -100,18 +100,18 @@ pub(crate) fn open(
oflags: types::Oflags,
fs_flags: types::Fdflags,
) -> Result<Box<dyn Handle>> {
use yanix::file::{fstatat, openat, AtFlag, FileType, Mode, OFlag};
use yanix::file::{fstatat, openat, AtFlags, FileType, Mode, OFlags};
let mut nix_all_oflags = if read && write {
OFlag::RDWR
OFlags::RDWR
} else if write {
OFlag::WRONLY
OFlags::WRONLY
} else {
OFlag::RDONLY
OFlags::RDONLY
};
// on non-Capsicum systems, we always want nofollow
nix_all_oflags.insert(OFlag::NOFOLLOW);
nix_all_oflags.insert(OFlags::NOFOLLOW);
// convert open flags
nix_all_oflags.insert(oflags.into());
@ -141,7 +141,7 @@ pub(crate) fn open(
match e.raw_os_error().unwrap() {
// Linux returns ENXIO instead of EOPNOTSUPP when opening a socket
libc::ENXIO => {
match unsafe { fstatat(dirfd.as_raw_fd(), path, AtFlag::SYMLINK_NOFOLLOW) } {
match unsafe { fstatat(dirfd.as_raw_fd(), path, AtFlags::SYMLINK_NOFOLLOW) } {
Ok(stat) => {
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Socket {
return Err(Errno::Notsup);
@ -155,9 +155,9 @@ pub(crate) fn open(
// Linux returns ENOTDIR instead of ELOOP when using O_NOFOLLOW|O_DIRECTORY
// on a symlink.
libc::ENOTDIR
if !(nix_all_oflags & (OFlag::NOFOLLOW | OFlag::DIRECTORY)).is_empty() =>
if !(nix_all_oflags & (OFlags::NOFOLLOW | OFlags::DIRECTORY)).is_empty() =>
{
match unsafe { fstatat(dirfd.as_raw_fd(), path, AtFlag::SYMLINK_NOFOLLOW) } {
match unsafe { fstatat(dirfd.as_raw_fd(), path, AtFlags::SYMLINK_NOFOLLOW) } {
Ok(stat) => {
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Symlink {
return Err(Errno::Loop);
@ -170,7 +170,7 @@ pub(crate) fn open(
}
// FreeBSD returns EMLINK instead of ELOOP when using O_NOFOLLOW on
// a symlink.
libc::EMLINK if !(nix_all_oflags & OFlag::NOFOLLOW).is_empty() => {
libc::EMLINK if !(nix_all_oflags & OFlags::NOFOLLOW).is_empty() => {
return Err(Errno::Loop);
}
_ => {}
@ -201,17 +201,17 @@ pub(crate) fn readlink(dirfd: &OsDir, path: &str, buf: &mut [u8]) -> Result<usiz
}
pub(crate) fn remove_directory(dirfd: &OsDir, path: &str) -> Result<()> {
use yanix::file::{unlinkat, AtFlag};
unsafe { unlinkat(dirfd.as_raw_fd(), path, AtFlag::REMOVEDIR)? };
use yanix::file::{unlinkat, AtFlags};
unsafe { unlinkat(dirfd.as_raw_fd(), path, AtFlags::REMOVEDIR)? };
Ok(())
}
pub(crate) fn filestat_get_at(dirfd: &OsDir, path: &str, follow: bool) -> Result<types::Filestat> {
use yanix::file::{fstatat, AtFlag};
use yanix::file::{fstatat, AtFlags};
let flags = if follow {
AtFlag::empty()
AtFlags::empty()
} else {
AtFlag::SYMLINK_NOFOLLOW
AtFlags::SYMLINK_NOFOLLOW
};
let stat = unsafe { fstatat(dirfd.as_raw_fd(), path, flags)? };
let stat = stat.try_into()?;

14
crates/wasi-common/yanix/src/fcntl.rs

@ -1,5 +1,5 @@
use crate::{
file::{FdFlag, OFlag},
file::{FdFlags, OFlags},
from_result, from_success_code,
};
use std::io::Result;
@ -17,18 +17,18 @@ pub unsafe fn dup_fd(fd: RawFd, close_on_exec: bool) -> Result<RawFd> {
})
}
pub unsafe fn get_fd_flags(fd: RawFd) -> Result<FdFlag> {
from_result(libc::fcntl(fd, libc::F_GETFD)).map(FdFlag::from_bits_truncate)
pub unsafe fn get_fd_flags(fd: RawFd) -> Result<FdFlags> {
from_result(libc::fcntl(fd, libc::F_GETFD)).map(FdFlags::from_bits_truncate)
}
pub unsafe fn set_fd_flags(fd: RawFd, flags: FdFlag) -> Result<()> {
pub unsafe fn set_fd_flags(fd: RawFd, flags: FdFlags) -> Result<()> {
from_success_code(libc::fcntl(fd, libc::F_SETFD, flags.bits()))
}
pub unsafe fn get_status_flags(fd: RawFd) -> Result<OFlag> {
from_result(libc::fcntl(fd, libc::F_GETFL)).map(OFlag::from_bits_truncate)
pub unsafe fn get_status_flags(fd: RawFd) -> Result<OFlags> {
from_result(libc::fcntl(fd, libc::F_GETFL)).map(OFlags::from_bits_truncate)
}
pub unsafe fn set_status_flags(fd: RawFd, flags: OFlag) -> Result<()> {
pub unsafe fn set_status_flags(fd: RawFd, flags: OFlags) -> Result<()> {
from_success_code(libc::fcntl(fd, libc::F_SETFL, flags.bits()))
}

18
crates/wasi-common/yanix/src/file.rs

@ -11,13 +11,13 @@ use std::{
pub use crate::sys::file::*;
bitflags! {
pub struct FdFlag: libc::c_int {
pub struct FdFlags: libc::c_int {
const CLOEXEC = libc::FD_CLOEXEC;
}
}
bitflags! {
pub struct AtFlag: libc::c_int {
pub struct AtFlags: libc::c_int {
const REMOVEDIR = libc::AT_REMOVEDIR;
const SYMLINK_FOLLOW = libc::AT_SYMLINK_FOLLOW;
const SYMLINK_NOFOLLOW = libc::AT_SYMLINK_NOFOLLOW;
@ -48,7 +48,7 @@ bitflags! {
}
bitflags! {
pub struct OFlag: libc::c_int {
pub struct OFlags: libc::c_int {
const ACCMODE = libc::O_ACCMODE;
const APPEND = libc::O_APPEND;
const CREAT = libc::O_CREAT;
@ -155,7 +155,7 @@ impl FileType {
pub unsafe fn openat<P: AsRef<OsStr>>(
dirfd: RawFd,
path: P,
oflag: OFlag,
oflag: OFlags,
mode: Mode,
) -> Result<RawFd> {
let path = CString::new(path.as_ref().as_bytes())?;
@ -194,7 +194,7 @@ pub unsafe fn linkat<P: AsRef<OsStr>>(
old_path: P,
new_dirfd: RawFd,
new_path: P,
flags: AtFlag,
flags: AtFlags,
) -> Result<()> {
let old_path = CString::new(old_path.as_ref().as_bytes())?;
let new_path = CString::new(new_path.as_ref().as_bytes())?;
@ -207,7 +207,7 @@ pub unsafe fn linkat<P: AsRef<OsStr>>(
))
}
pub unsafe fn unlinkat<P: AsRef<OsStr>>(dirfd: RawFd, path: P, flags: AtFlag) -> Result<()> {
pub unsafe fn unlinkat<P: AsRef<OsStr>>(dirfd: RawFd, path: P, flags: AtFlags) -> Result<()> {
let path = CString::new(path.as_ref().as_bytes())?;
from_success_code(libc::unlinkat(dirfd, path.as_ptr(), flags.bits()))
}
@ -238,7 +238,11 @@ pub unsafe fn symlinkat<P: AsRef<OsStr>>(old_path: P, new_dirfd: RawFd, new_path
))
}
pub unsafe fn fstatat<P: AsRef<OsStr>>(dirfd: RawFd, path: P, flags: AtFlag) -> Result<libc::stat> {
pub unsafe fn fstatat<P: AsRef<OsStr>>(
dirfd: RawFd,
path: P,
flags: AtFlags,
) -> Result<libc::stat> {
use std::mem::MaybeUninit;
let path = CString::new(path.as_ref().as_bytes())?;
let mut filestat = MaybeUninit::<libc::stat>::uninit();

Loading…
Cancel
Save