Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posix_fadvise doesn't return -1 as sentinel value #1538

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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