Move fd_advise on Linux to nix

This commit is contained in:
Marcin Mielniczuk
2019-08-23 18:34:04 +02:00
committed by Jakub Konka
parent aa28a6d697
commit 947eb1fb53

View File

@@ -4,7 +4,7 @@ use super::fs_helpers::*;
use crate::helpers::systemtime_to_timestamp;
use crate::hostcalls_impl::PathGet;
use crate::sys::errno_from_ioerror;
use crate::sys::host_impl;
use crate::sys::host_impl::{self, errno_from_nix};
use crate::{host, Result};
use nix::libc::{self, c_long, c_void};
use std::convert::TryInto;
@@ -42,45 +42,50 @@ pub(crate) fn fd_fdstat_set_flags(fd: &File, fdflags: host::__wasi_fdflags_t) ->
}
}
#[cfg(target_os = "linux")]
pub(crate) fn fd_advise(
file: &File,
advice: host::__wasi_advice_t,
offset: host::__wasi_filesize_t,
len: host::__wasi_filesize_t,
) -> Result<()> {
#[cfg(target_os = "linux")]
{
use nix::libc::off_t;
use nix::fcntl::{posix_fadvise, PosixFadviseAdvice};
let offset = offset.try_into().map_err(|_| host::__WASI_EOVERFLOW)?;
let len = len.try_into().map_err(|_| host::__WASI_EOVERFLOW)?;
let host_advice = match advice {
host::__WASI_ADVICE_DONTNEED => libc::POSIX_FADV_DONTNEED,
host::__WASI_ADVICE_SEQUENTIAL => libc::POSIX_FADV_SEQUENTIAL,
host::__WASI_ADVICE_WILLNEED => libc::POSIX_FADV_DONTNEED,
host::__WASI_ADVICE_NOREUSE => libc::POSIX_FADV_NOREUSE,
host::__WASI_ADVICE_RANDOM => libc::POSIX_FADV_RANDOM,
host::__WASI_ADVICE_NORMAL => libc::POSIX_FADV_NORMAL,
host::__WASI_ADVICE_DONTNEED => PosixFadviseAdvice::POSIX_FADV_DONTNEED,
host::__WASI_ADVICE_SEQUENTIAL => PosixFadviseAdvice::POSIX_FADV_SEQUENTIAL,
host::__WASI_ADVICE_WILLNEED => PosixFadviseAdvice::POSIX_FADV_DONTNEED,
host::__WASI_ADVICE_NOREUSE => PosixFadviseAdvice::POSIX_FADV_NOREUSE,
host::__WASI_ADVICE_RANDOM => PosixFadviseAdvice::POSIX_FADV_RANDOM,
host::__WASI_ADVICE_NORMAL => PosixFadviseAdvice::POSIX_FADV_NORMAL,
_ => return Err(host::__WASI_EINVAL),
};
let res = unsafe {
libc::posix_fadvise(file.as_raw_fd(), offset as off_t, len as off_t, host_advice)
};
if res != 0 {
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
}
posix_fadvise(file.as_raw_fd(), offset, len, host_advice)
.map_err(|err| errno_from_nix(err.as_errno().unwrap()))?;
}
#[cfg(not(target_os = "linux"))]
{
let _ = (file, offset, len);
match advice {
host::__WASI_ADVICE_DONTNEED
| host::__WASI_ADVICE_SEQUENTIAL
| host::__WASI_ADVICE_WILLNEED
| host::__WASI_ADVICE_NOREUSE
| host::__WASI_ADVICE_RANDOM
| host::__WASI_ADVICE_NORMAL => {}
_ => return Err(host::__WASI_EINVAL),
}
Ok(())
}
#[cfg(not(target_os = "linux"))]
pub(crate) fn fd_advise(
_file: &File,
advice: host::__wasi_advice_t,
_offset: host::__wasi_filesize_t,
_len: host::__wasi_filesize_t,
) -> Result<()> {
match advice {
host::__WASI_ADVICE_DONTNEED
| host::__WASI_ADVICE_SEQUENTIAL
| host::__WASI_ADVICE_WILLNEED
| host::__WASI_ADVICE_NOREUSE
| host::__WASI_ADVICE_RANDOM
| host::__WASI_ADVICE_NORMAL => {}
_ => return Err(host::__WASI_EINVAL),
}
Ok(())