Skip to content

Commit

Permalink
Don't use posix_fadvise on Mac
Browse files Browse the repository at this point in the history
Fixes #1.
  • Loading branch information
pkolaczk committed Jun 11, 2020
1 parent 90c6a75 commit 29ace92
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use std::os::unix::io::*;

use bytesize::ByteSize;
use fasthash::{FastHasher, HasherExt, Murmur3Hasher, t1ha2::Hasher128};
#[cfg(unix)]
use nix::fcntl::*;
use serde::*;
use smallvec::alloc::fmt::Formatter;
use smallvec::alloc::str::FromStr;
Expand Down Expand Up @@ -309,9 +307,16 @@ fn to_off_t(offset: u64) -> i64 {
/// Wrapper for `posix_fadvise`. Ignores errors.
/// This method is used to advise the system, so its failure is not critical to the result of
/// the program. At worst, failure could hurt performance.
#[cfg(unix)]
fn fadvise(file: &File, offset: FilePos, len: FileLen, advice: PosixFadviseAdvice) {
let _ = posix_fadvise(file.as_raw_fd(),
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "emscripten",
target_os = "fuchsia",
any(target_os = "wasi", target_env = "wasi"),
target_env = "uclibc",
target_env = "freebsd"))]
fn fadvise(file: &File, offset: FilePos, len: FileLen, advice: nix::fcntl::PosixFadviseAdvice) {
let _ = nix::fcntl::posix_fadvise(file.as_raw_fd(),
to_off_t(offset.into()),
to_off_t(len.into()),
advice);
Expand All @@ -322,7 +327,14 @@ fn fadvise(file: &File, offset: FilePos, len: FileLen, advice: PosixFadviseAdvic
/// On non-Unix systems, does nothing.
/// Failures are not signalled to the caller, but a warning is printed to stderr.
fn configure_readahead(file: &File, offset: FilePos, len: FileLen, cache_policy: Caching) {
if cfg!(unix) {
if cfg!(any(target_os = "linux",
target_os = "android",
target_os = "emscripten",
target_os = "fuchsia",
any(target_os = "wasi", target_env = "wasi"),
target_env = "uclibc",
target_env = "freebsd")) {
use nix::fcntl::*;
let advise = |advice: PosixFadviseAdvice| {
fadvise(file, offset, len, advice)
};
Expand All @@ -337,7 +349,14 @@ fn configure_readahead(file: &File, offset: FilePos, len: FileLen, cache_policy:
/// Tells the system to remove given file fragment from the page cache.
/// On non-Unix systems, does nothing.
fn evict_page_cache(file: &File, offset: FilePos, len: FileLen) {
if cfg!(unix) {
if cfg!(any(target_os = "linux",
target_os = "android",
target_os = "emscripten",
target_os = "fuchsia",
any(target_os = "wasi", target_env = "wasi"),
target_env = "uclibc",
target_env = "freebsd")) {
use nix::fcntl::*;
fadvise(file, offset, len, PosixFadviseAdvice::POSIX_FADV_DONTNEED);
}
}
Expand Down

0 comments on commit 29ace92

Please sign in to comment.