1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use crate::convert_res;
use errno::{set_errno, Errno};
use libc::{c_int, off64_t, off_t};
use rustix::fd::BorrowedFd;
use rustix::fs::Advice;

#[no_mangle]
unsafe extern "C" fn posix_fadvise64(
    fd: c_int,
    offset: off64_t,
    len: off64_t,
    advice: c_int,
) -> c_int {
    libc!(libc::posix_fadvise64(fd, offset, len, advice));

    let advice = match advice {
        libc::POSIX_FADV_NORMAL => Advice::Normal,
        libc::POSIX_FADV_SEQUENTIAL => Advice::Sequential,
        libc::POSIX_FADV_RANDOM => Advice::Random,
        libc::POSIX_FADV_NOREUSE => Advice::NoReuse,
        libc::POSIX_FADV_WILLNEED => Advice::WillNeed,
        libc::POSIX_FADV_DONTNEED => Advice::DontNeed,
        _ => {
            set_errno(Errno(libc::EINVAL));
            return -1;
        }
    };
    match convert_res(rustix::fs::fadvise(
        BorrowedFd::borrow_raw(fd),
        offset as u64,
        len as u64,
        advice,
    )) {
        Some(()) => 0,
        None => -1,
    }
}

#[no_mangle]
unsafe extern "C" fn posix_fadvise(fd: c_int, offset: off_t, len: off_t, advice: c_int) -> c_int {
    libc!(libc::posix_fadvise(fd, offset, len, advice));

    posix_fadvise64(fd, offset.into(), len.into(), advice)
}