Move fd_advise on Linux to nix
This commit is contained in:
committed by
Jakub Konka
parent
aa28a6d697
commit
947eb1fb53
@@ -4,7 +4,7 @@ use super::fs_helpers::*;
|
|||||||
use crate::helpers::systemtime_to_timestamp;
|
use crate::helpers::systemtime_to_timestamp;
|
||||||
use crate::hostcalls_impl::PathGet;
|
use crate::hostcalls_impl::PathGet;
|
||||||
use crate::sys::errno_from_ioerror;
|
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 crate::{host, Result};
|
||||||
use nix::libc::{self, c_long, c_void};
|
use nix::libc::{self, c_long, c_void};
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
@@ -42,36 +42,42 @@ pub(crate) fn fd_fdstat_set_flags(fd: &File, fdflags: host::__wasi_fdflags_t) ->
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
pub(crate) fn fd_advise(
|
pub(crate) fn fd_advise(
|
||||||
file: &File,
|
file: &File,
|
||||||
advice: host::__wasi_advice_t,
|
advice: host::__wasi_advice_t,
|
||||||
offset: host::__wasi_filesize_t,
|
offset: host::__wasi_filesize_t,
|
||||||
len: host::__wasi_filesize_t,
|
len: host::__wasi_filesize_t,
|
||||||
) -> Result<()> {
|
) -> 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 {
|
let host_advice = match advice {
|
||||||
host::__WASI_ADVICE_DONTNEED => libc::POSIX_FADV_DONTNEED,
|
host::__WASI_ADVICE_DONTNEED => PosixFadviseAdvice::POSIX_FADV_DONTNEED,
|
||||||
host::__WASI_ADVICE_SEQUENTIAL => libc::POSIX_FADV_SEQUENTIAL,
|
host::__WASI_ADVICE_SEQUENTIAL => PosixFadviseAdvice::POSIX_FADV_SEQUENTIAL,
|
||||||
host::__WASI_ADVICE_WILLNEED => libc::POSIX_FADV_DONTNEED,
|
host::__WASI_ADVICE_WILLNEED => PosixFadviseAdvice::POSIX_FADV_DONTNEED,
|
||||||
host::__WASI_ADVICE_NOREUSE => libc::POSIX_FADV_NOREUSE,
|
host::__WASI_ADVICE_NOREUSE => PosixFadviseAdvice::POSIX_FADV_NOREUSE,
|
||||||
host::__WASI_ADVICE_RANDOM => libc::POSIX_FADV_RANDOM,
|
host::__WASI_ADVICE_RANDOM => PosixFadviseAdvice::POSIX_FADV_RANDOM,
|
||||||
host::__WASI_ADVICE_NORMAL => libc::POSIX_FADV_NORMAL,
|
host::__WASI_ADVICE_NORMAL => PosixFadviseAdvice::POSIX_FADV_NORMAL,
|
||||||
_ => return Err(host::__WASI_EINVAL),
|
_ => 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)
|
posix_fadvise(file.as_raw_fd(), offset, len, host_advice)
|
||||||
};
|
.map_err(|err| errno_from_nix(err.as_errno().unwrap()))?;
|
||||||
if res != 0 {
|
|
||||||
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
{
|
pub(crate) fn fd_advise(
|
||||||
let _ = (file, offset, len);
|
_file: &File,
|
||||||
|
advice: host::__wasi_advice_t,
|
||||||
|
_offset: host::__wasi_filesize_t,
|
||||||
|
_len: host::__wasi_filesize_t,
|
||||||
|
) -> Result<()> {
|
||||||
match advice {
|
match advice {
|
||||||
host::__WASI_ADVICE_DONTNEED
|
host::__WASI_ADVICE_DONTNEED
|
||||||
| host::__WASI_ADVICE_SEQUENTIAL
|
| host::__WASI_ADVICE_SEQUENTIAL
|
||||||
@@ -81,7 +87,6 @@ pub(crate) fn fd_advise(
|
|||||||
| host::__WASI_ADVICE_NORMAL => {}
|
| host::__WASI_ADVICE_NORMAL => {}
|
||||||
_ => return Err(host::__WASI_EINVAL),
|
_ => return Err(host::__WASI_EINVAL),
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user