use crate::from_success_code;
use std::{convert::TryInto, io::Result, os::unix::prelude::*};
#[cfg(not(any(target_os = "freebsd", target_os = "netbsd")))]
#[derive(Debug, Copy, Clone)]
#[repr(i32)]
pub enum PosixFadviseAdvice {
Normal,
Sequential,
Random,
NoReuse,
WillNeed,
DontNeed,
}
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
#[derive(Debug, Copy, Clone)]
#[repr(i32)]
pub enum PosixFadviseAdvice {
Normal = libc::POSIX_FADV_NORMAL,
Sequential = libc::POSIX_FADV_SEQUENTIAL,
Random = libc::POSIX_FADV_RANDOM,
NoReuse = libc::POSIX_FADV_NOREUSE,
WillNeed = libc::POSIX_FADV_WILLNEED,
DontNeed = libc::POSIX_FADV_DONTNEED,
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub unsafe fn posix_fadvise(
fd: RawFd,
offset: libc::off_t,
len: libc::off_t,
_advice: PosixFadviseAdvice,
) -> Result<()> {
let ra_count = match len.try_into() {
Ok(ra_count) => ra_count,
Err(_) => {
tracing::warn!(
"`len` too big to fit in the host's command. Returning early with no-op!"
);
return Ok(());
}
};
let advisory = libc::radvisory {
ra_offset: offset,
ra_count,
};
from_success_code(libc::fcntl(fd, libc::F_RDADVISE, &advisory))
}
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
pub unsafe fn posix_fadvise(
fd: RawFd,
offset: libc::off_t,
len: libc::off_t,
advice: PosixFadviseAdvice,
) -> Result<()> {
from_success_code(libc::posix_fadvise(fd, offset, len, advice as libc::c_int))
}
#[cfg(not(any(
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd"
)))]
pub unsafe fn posix_fadvise(
_fd: RawFd,
_offset: libc::off_t,
_len: libc::off_t,
_advice: PosixFadviseAdvice,
) -> Result<()> {
Ok(())
}