Skip to content

Commit

Permalink
Fix return value of posix_fadvise
Browse files Browse the repository at this point in the history
libc::posix_fadvise returns errnos directly rather than in the errno
variable.
  • Loading branch information
ocadaruma authored and asomers committed Sep 29, 2021
1 parent 759b34a commit a607034
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,14 @@ mod posix_fadvise {
offset: libc::off_t,
len: libc::off_t,
advice: PosixFadviseAdvice,
) -> Result<libc::c_int> {
) -> 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))
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down

0 comments on commit a607034

Please sign in to comment.