Skip to main content

c_scape/fs/
fadvise.rs

1use crate::convert_res;
2use core::num::NonZeroU64;
3use errno::{set_errno, Errno};
4use libc::{c_int, off64_t, off_t};
5use rustix::fd::BorrowedFd;
6use rustix::fs::Advice;
7
8#[no_mangle]
9unsafe extern "C" fn posix_fadvise64(
10    fd: c_int,
11    offset: off64_t,
12    len: off64_t,
13    advice: c_int,
14) -> c_int {
15    libc!(libc::posix_fadvise64(fd, offset, len, advice));
16
17    let advice = match advice {
18        libc::POSIX_FADV_NORMAL => Advice::Normal,
19        libc::POSIX_FADV_SEQUENTIAL => Advice::Sequential,
20        libc::POSIX_FADV_RANDOM => Advice::Random,
21        libc::POSIX_FADV_NOREUSE => Advice::NoReuse,
22        libc::POSIX_FADV_WILLNEED => Advice::WillNeed,
23        libc::POSIX_FADV_DONTNEED => Advice::DontNeed,
24        _ => {
25            set_errno(Errno(libc::EINVAL));
26            return -1;
27        }
28    };
29    match convert_res(rustix::fs::fadvise(
30        BorrowedFd::borrow_raw(fd),
31        offset as u64,
32        NonZeroU64::new(len as u64),
33        advice,
34    )) {
35        Some(()) => 0,
36        None => -1,
37    }
38}
39
40#[no_mangle]
41unsafe extern "C" fn posix_fadvise(fd: c_int, offset: off_t, len: off_t, advice: c_int) -> c_int {
42    libc!(libc::posix_fadvise(fd, offset, len, advice));
43
44    posix_fadvise64(fd, offset.into(), len.into(), advice)
45}