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
45
46
47
48
49
50
51
52
53
54
55
use crate::zero_ok;
#[cfg(not(any(target_os = "linux", target_os = "emscripten", target_os = "l4re")))]
use libc::posix_fadvise as libc_posix_fadvise;
#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "l4re"))]
use libc::posix_fadvise64 as libc_posix_fadvise;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, RawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, RawFd};
use std::{convert::TryInto, io};
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(i32)]
pub enum Advice {
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,
}
#[inline]
pub fn fadvise<Fd: AsRawFd>(fd: &Fd, offset: u64, len: u64, advice: Advice) -> io::Result<()> {
let fd = fd.as_raw_fd();
unsafe { _fadvise(fd, offset, len, advice) }
}
unsafe fn _fadvise(fd: RawFd, offset: u64, len: u64, advice: Advice) -> io::Result<()> {
if let (Ok(offset), Ok(len)) = (offset.try_into(), len.try_into()) {
zero_ok(libc_posix_fadvise(
fd as libc::c_int,
offset,
len,
advice as libc::c_int,
))?;
}
Ok(())
}