Skip to content

Commit 245b5b2

Browse files
josephlrnewpavlov
authored andcommitted
util_libc: open_readonly shoud return a Result
Signed-off-by: Joe Richey <[email protected]>
1 parent a913c9c commit 245b5b2

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/util_libc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ cfg_if! {
138138
}
139139

140140
// SAFETY: path must be null terminated, FD must be manually closed.
141-
pub unsafe fn open_readonly(path: &str) -> Option<libc::c_int> {
141+
pub unsafe fn open_readonly(path: &str) -> Result<libc::c_int, Error> {
142142
debug_assert!(path.as_bytes().last() == Some(&0));
143-
let fd = open(path.as_ptr() as *mut _, libc::O_RDONLY | libc::O_CLOEXEC);
143+
let fd = open(path.as_ptr() as *const _, libc::O_RDONLY | libc::O_CLOEXEC);
144144
if fd < 0 {
145-
return None;
145+
return Err(last_os_error());
146146
}
147147
// O_CLOEXEC works on all Unix targets except for older Linux kernels (pre
148148
// 2.6.23), so we also use an ioctl to make sure FD_CLOEXEC is set.
149149
#[cfg(target_os = "linux")]
150150
libc::ioctl(fd, libc::FIOCLEX);
151-
Some(fd)
151+
Ok(fd)
152152
}

0 commit comments

Comments
 (0)