Skip to content

Commit

Permalink
Merge #1089
Browse files Browse the repository at this point in the history
1089: implement posix_fadvise r=asomers a=kevinwern

See: 
https://github.com/CraneStation/wasi-common/issues/16
http://man7.org/linux/man-pages/man2/posix_fadvise.2.html

Conditional compilation derived from corresponding libc conditions:
Fuchsia:
https://github.com/rust-lang/libc/blob/0b02c4060a750983cebcccdf1f35a5ec4cdcf516/src/fuchsia/mod.rs#L3807
https://github.com/rust-lang/libc/blob/0e702c1b4e2e56788e8b67d3efd9c242807c3d4b/src/lib.rs#L104-L109

uClibc:
https://github.com/rust-lang/libc/blob/11c762c535cb43dda3d9d87a0845c55201a905fb/src/unix/uclibc/mod.rs#L1676
https://github.com/rust-lang/libc/blob/ce7e3a7e866dd7109a971b694a2bf58bd08f101a/src/unix/mod.rs#L1141-L1143
https://github.com/rust-lang/libc/blob/0e702c1b4e2e56788e8b67d3efd9c242807c3d4b/src/lib.rs#L116-L121

Linux, android, emscripten:
https://github.com/rust-lang/libc/blob/11c762c535cb43dda3d9d87a0845c55201a905fb/src/unix/linux_like/mod.rs#L1303
https://github.com/rust-lang/libc/blob/ce7e3a7e866dd7109a971b694a2bf58bd08f101a/src/unix/mod.rs#L1147-L1151
https://github.com/rust-lang/libc/blob/0e702c1b4e2e56788e8b67d3efd9c242807c3d4b/src/lib.rs#L116-L121

FreeBSD:
https://github.com/rust-lang/libc/blob/e0ff1e68b9e34173e9c4c3217d1b0fc81a7d352d/src/unix/bsd/freebsdlike/freebsd/mod.rs#L1223
https://github.com/rust-lang/libc/blob/ce7e3a7e866dd7109a971b694a2bf58bd08f101a/src/unix/bsd/freebsdlike/mod.rs#L1307-L1309
https://github.com/rust-lang/libc/blob/ce7e3a7e866dd7109a971b694a2bf58bd08f101a/src/unix/bsd/mod.rs#L683-L685
https://github.com/rust-lang/libc/blob/ce7e3a7e866dd7109a971b694a2bf58bd08f101a/src/unix/mod.rs#L1152-L1159
https://github.com/rust-lang/libc/blob/0e702c1b4e2e56788e8b67d3efd9c242807c3d4b/src/lib.rs#L116-L121

WASI:
https://github.com/rust-lang/libc/blob/0e702c1b4e2e56788e8b67d3efd9c242807c3d4b/src/wasi.rs#L1001
https://github.com/rust-lang/libc/blob/0e702c1b4e2e56788e8b67d3efd9c242807c3d4b/src/lib.rs#L134-L139


Co-authored-by: Kevin Wern <[email protected]>
  • Loading branch information
bors[bot] and kevinwern committed Jul 1, 2019
2 parents 7fa4f23 + 841e4af commit 77aeabc
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
43 changes: 43 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -448,3 +457,37 @@ 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_enum! {
#[repr(i32)]
pub enum PosixFadviseAdvice {
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: PosixFadviseAdvice) -> Result<libc::c_int> {
let res = unsafe { libc::posix_fadvise(fd, offset, len, advice as libc::c_int) };
Errno::result(res)
}
}
33 changes: 33 additions & 0 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, PosixFadviseAdvice::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, PosixFadviseAdvice::POSIX_FADV_WILLNEED)
.unwrap();
assert_eq!(errno, Errno::ESPIPE as i32);
}
}

0 comments on commit 77aeabc

Please sign in to comment.