Skip to content

Commit 5e463aa

Browse files
bors[bot]kevinwern
andcommitted
Merge #1089
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]>
2 parents 074caea + f723b18 commit 5e463aa

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1313
([#1069](https://github.com/nix-rust/nix/pull/1069))
1414
- Add `mkdirat`.
1515
([#1084](https://github.com/nix-rust/nix/pull/1084))
16+
- Add `posix_fadvise`.
17+
([#1089](https://github.com/nix-rust/nix/pull/1089))
1618

1719
### Changed
1820
- Support for `ifaddrs` now present when building for Android.

src/fcntl.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ use std::ptr; // For splice and copy_file_range
1212
#[cfg(any(target_os = "android", target_os = "linux"))]
1313
use sys::uio::IoVec; // For vmsplice
1414

15+
#[cfg(any(target_os = "linux",
16+
target_os = "android",
17+
target_os = "emscripten",
18+
target_os = "fuchsia",
19+
any(target_os = "wasi", target_env = "wasi"),
20+
target_env = "uclibc",
21+
target_env = "freebsd"))]
22+
pub use self::posix_fadvise::*;
23+
1524
libc_bitflags!{
1625
pub struct AtFlags: c_int {
1726
AT_SYMLINK_NOFOLLOW;
@@ -448,3 +457,37 @@ pub fn fallocate(fd: RawFd, mode: FallocateFlags, offset: libc::off_t, len: libc
448457
let res = unsafe { libc::fallocate(fd, mode.bits(), offset, len) };
449458
Errno::result(res)
450459
}
460+
461+
#[cfg(any(target_os = "linux",
462+
target_os = "android",
463+
target_os = "emscripten",
464+
target_os = "fuchsia",
465+
any(target_os = "wasi", target_env = "wasi"),
466+
target_env = "uclibc",
467+
target_env = "freebsd"))]
468+
mod posix_fadvise {
469+
use Result;
470+
use libc;
471+
use errno::Errno;
472+
use std::os::unix::io::RawFd;
473+
474+
libc_enum! {
475+
#[repr(i32)]
476+
pub enum PosixFadviseAdvice {
477+
POSIX_FADV_NORMAL,
478+
POSIX_FADV_SEQUENTIAL,
479+
POSIX_FADV_RANDOM,
480+
POSIX_FADV_NOREUSE,
481+
POSIX_FADV_WILLNEED,
482+
POSIX_FADV_DONTNEED,
483+
}
484+
}
485+
486+
pub fn posix_fadvise(fd: RawFd,
487+
offset: libc::off_t,
488+
len: libc::off_t,
489+
advice: PosixFadviseAdvice) -> Result<libc::c_int> {
490+
let res = unsafe { libc::posix_fadvise(fd, offset, len, advice as libc::c_int) };
491+
Errno::result(res)
492+
}
493+
}

test/test_fcntl.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,36 @@ mod linux_android {
180180
assert_eq!(100, read(fd, &mut buf).unwrap());
181181
}
182182
}
183+
184+
#[cfg(any(target_os = "linux",
185+
target_os = "android",
186+
target_os = "emscripten",
187+
target_os = "fuchsia",
188+
any(target_os = "wasi", target_env = "wasi"),
189+
target_env = "uclibc",
190+
target_env = "freebsd"))]
191+
mod test_posix_fadvise {
192+
193+
use tempfile::NamedTempFile;
194+
use std::os::unix::io::{RawFd, AsRawFd};
195+
use nix::errno::Errno;
196+
use nix::fcntl::*;
197+
use nix::unistd::pipe;
198+
199+
#[test]
200+
fn test_success() {
201+
let tmp = NamedTempFile::new().unwrap();
202+
let fd = tmp.as_raw_fd();
203+
let res = posix_fadvise(fd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED).unwrap();
204+
205+
assert_eq!(res, 0);
206+
}
207+
208+
#[test]
209+
fn test_errno() {
210+
let (rd, _wr) = pipe().unwrap();
211+
let errno = posix_fadvise(rd as RawFd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED)
212+
.unwrap();
213+
assert_eq!(errno, Errno::ESPIPE as i32);
214+
}
215+
}

0 commit comments

Comments
 (0)