Skip to content

Commit f723b18

Browse files
committed
implement posix_fadvise
1 parent 112438b commit f723b18

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)