From e14bf1decca6464b9f31c37b0efe66074aa8443e Mon Sep 17 00:00:00 2001 From: Kevin Wern Date: Fri, 21 Jun 2019 00:13:02 -0400 Subject: [PATCH] implement posix_fadvise --- CHANGELOG.md | 2 ++ src/fcntl.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ test/test_fcntl.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb440cab40..b11eb7d2a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#1069](https://github.com/nix-rust/nix/pull/1069)) - Add `mkdirat`. ([#1084](https://github.com/nix-rust/nix/pull/1084)) +- Add `posix_fadvise`. + ([#1089](https://github.com/nix-rust/nix/pull/1089)) ### Changed - Support for `ifaddrs` now present when building for Android. diff --git a/src/fcntl.rs b/src/fcntl.rs index 3d932a5398..ee8c2205d9 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -12,6 +12,15 @@ use std::ptr; // For splice and copy_file_range #[cfg(any(target_os = "android", target_os = "linux"))] use sys::uio::IoVec; // For vmsplice +#[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"))] +pub use self::posix_fadvise::*; + libc_bitflags!{ pub struct AtFlags: c_int { AT_SYMLINK_NOFOLLOW; @@ -448,3 +457,36 @@ pub fn fallocate(fd: RawFd, mode: FallocateFlags, offset: libc::off_t, len: libc let res = unsafe { libc::fallocate(fd, mode.bits(), offset, len) }; Errno::result(res) } + +#[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"))] +mod posix_fadvise { + use Result; + use libc; + use errno::Errno; + use std::os::unix::io::RawFd; + + libc_bitflags!{ + pub struct PosixFadviseFlags: libc::c_int { + POSIX_FADV_NORMAL; + POSIX_FADV_SEQUENTIAL; + POSIX_FADV_RANDOM; + POSIX_FADV_NOREUSE; + POSIX_FADV_WILLNEED; + POSIX_FADV_DONTNEED; + } + } + + pub fn posix_fadvise(fd: RawFd, + offset: libc::off_t, + len: libc::off_t, + advice: PosixFadviseFlags) -> Result { + let res = unsafe { libc::posix_fadvise(fd, offset, len, advice.bits()) }; + Errno::result(res) + } +} diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 8d02f147b4..2b54b2681b 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -180,3 +180,36 @@ mod linux_android { assert_eq!(100, read(fd, &mut buf).unwrap()); } } + +#[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"))] +mod test_posix_fadvise { + + use tempfile::NamedTempFile; + use std::os::unix::io::{RawFd, AsRawFd}; + use nix::errno::Errno; + use nix::fcntl::*; + use nix::unistd::pipe; + + #[test] + fn test_success() { + let tmp = NamedTempFile::new().unwrap(); + let fd = tmp.as_raw_fd(); + let res = posix_fadvise(fd, 0, 100, PosixFadviseFlags::POSIX_FADV_WILLNEED).unwrap(); + + assert_eq!(res, 0); + } + + #[test] + fn test_errno() { + let (rd, _wr) = pipe().unwrap(); + let errno = posix_fadvise(rd as RawFd, 0, 100, PosixFadviseFlags::POSIX_FADV_WILLNEED) + .unwrap(); + assert_eq!(errno, Errno::ESPIPE as i32); + } +}