Skip to content

Commit a607034

Browse files
ocadarumaasomers
authored andcommitted
Fix return value of posix_fadvise
libc::posix_fadvise returns errnos directly rather than in the errno variable.
1 parent 759b34a commit a607034

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,16 @@ This project adheres to [Semantic Versioning](https://semver.org/).
8383

8484
### Fixed
8585

86+
- `posix_fadvise` now returns errors in the conventional way, rather than as a
87+
non-zero value in `Ok()`.
88+
(#[1538](https://github.com/nix-rust/nix/pull/1538))
8689
- Added more errno definitions for better backwards compatibility with
8790
Nix 0.21.0.
8891
(#[1467](https://github.com/nix-rust/nix/pull/1467))
8992
- Fixed potential undefined behavior in `Signal::try_from` on some platforms.
9093
(#[1484](https://github.com/nix-rust/nix/pull/1484))
9194

95+
9296
### Removed
9397

9498
- Removed a couple of termios constants on redox that were never actually

src/fcntl.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,9 +667,14 @@ mod posix_fadvise {
667667
offset: libc::off_t,
668668
len: libc::off_t,
669669
advice: PosixFadviseAdvice,
670-
) -> Result<libc::c_int> {
670+
) -> Result<()> {
671671
let res = unsafe { libc::posix_fadvise(fd, offset, len, advice as libc::c_int) };
672-
Errno::result(res)
672+
673+
if res == 0 {
674+
Ok(())
675+
} else {
676+
Err(Errno::from_i32(res))
677+
}
673678
}
674679
}
675680

test/test_fcntl.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,17 +473,16 @@ mod test_posix_fadvise {
473473
fn test_success() {
474474
let tmp = NamedTempFile::new().unwrap();
475475
let fd = tmp.as_raw_fd();
476-
let res = posix_fadvise(fd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED).unwrap();
476+
let res = posix_fadvise(fd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED);
477477

478-
assert_eq!(res, 0);
478+
assert!(res.is_ok());
479479
}
480480

481481
#[test]
482482
fn test_errno() {
483483
let (rd, _wr) = pipe().unwrap();
484-
let errno = posix_fadvise(rd as RawFd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED)
485-
.unwrap();
486-
assert_eq!(errno, Errno::ESPIPE as i32);
484+
let res = posix_fadvise(rd as RawFd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED);
485+
assert_eq!(res, Err(Errno::ESPIPE));
487486
}
488487
}
489488

0 commit comments

Comments
 (0)