From a60703430ee708f3899b411f7756e65ea44c8f2a Mon Sep 17 00:00:00 2001 From: Haruki Okada Date: Thu, 23 Sep 2021 20:31:17 +0900 Subject: [PATCH] Fix return value of posix_fadvise libc::posix_fadvise returns errnos directly rather than in the errno variable. --- CHANGELOG.md | 4 ++++ src/fcntl.rs | 9 +++++++-- test/test_fcntl.rs | 9 ++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1139cf099d..af4fa9bc67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,12 +83,16 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed +- `posix_fadvise` now returns errors in the conventional way, rather than as a + non-zero value in `Ok()`. + (#[1538](https://github.com/nix-rust/nix/pull/1538)) - Added more errno definitions for better backwards compatibility with Nix 0.21.0. (#[1467](https://github.com/nix-rust/nix/pull/1467)) - Fixed potential undefined behavior in `Signal::try_from` on some platforms. (#[1484](https://github.com/nix-rust/nix/pull/1484)) + ### Removed - Removed a couple of termios constants on redox that were never actually diff --git a/src/fcntl.rs b/src/fcntl.rs index aded27b444..dd8e59a6ec 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -667,9 +667,14 @@ mod posix_fadvise { offset: libc::off_t, len: libc::off_t, advice: PosixFadviseAdvice, - ) -> Result { + ) -> Result<()> { let res = unsafe { libc::posix_fadvise(fd, offset, len, advice as libc::c_int) }; - Errno::result(res) + + if res == 0 { + Ok(()) + } else { + Err(Errno::from_i32(res)) + } } } diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 76252e6e06..db2acfbf52 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -473,17 +473,16 @@ mod test_posix_fadvise { 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(); + let res = posix_fadvise(fd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED); - assert_eq!(res, 0); + assert!(res.is_ok()); } #[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); + let res = posix_fadvise(rd as RawFd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED); + assert_eq!(res, Err(Errno::ESPIPE)); } }