Skip to content

Commit

Permalink
prefer to directly return .last_os_error, vs conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarzik committed Mar 23, 2024
1 parent 246af53 commit 6927182
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 28 deletions.
12 changes: 4 additions & 8 deletions fs/src/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ fn stat(filename_str: &str) -> io::Result<libc::stat> {
if rc == 0 {
Ok(st)
} else {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
Err(io::Error::from_raw_os_error(errno))
Err(io::Error::last_os_error())
}
}
}
Expand Down Expand Up @@ -126,8 +125,7 @@ fn read_mount_info() -> io::Result<MountList> {
let mut mounts: *mut libc::statfs = std::ptr::null_mut();
let n_mnt = libc::getmntinfo(&mut mounts, libc::MNT_WAIT);
if n_mnt < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
return Err(io::Error::from_raw_os_error(errno));
return Err(io::Error::last_os_error());
}

let mounts: &[libc::statfs] = std::slice::from_raw_parts(mounts as _, n_mnt as _);
Expand All @@ -150,8 +148,7 @@ fn read_mount_info() -> io::Result<MountList> {
let mnt_mode = CString::new("r").unwrap();
let f = libc::setmntent(path_mnt.as_ptr(), mnt_mode.as_ptr());
if f.is_null() {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
return Err(io::Error::from_raw_os_error(errno));
return Err(io::Error::last_os_error());
}

loop {
Expand All @@ -168,8 +165,7 @@ fn read_mount_info() -> io::Result<MountList> {
let mut mount: libc::statfs = std::mem::zeroed();
let rc = libc::statfs(dirname.as_ptr(), &mut mount);
if rc < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
return Err(io::Error::from_raw_os_error(errno));
return Err(io::Error::last_os_error());
}

info.push(&mount, devname, dirname);
Expand Down
3 changes: 1 addition & 2 deletions process/src/nice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let res = unsafe { libc::nice(args.niceval) };
if res < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
let e = io::Error::from_raw_os_error(errno);
let e = io::Error::last_os_error();
eprintln!("nice: {}", e);
return Err(Box::new(e));
}
Expand Down
3 changes: 1 addition & 2 deletions process/src/renice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ fn xsetpriority(which: u32, id: u32, prio: i32) -> io::Result<()> {
let res = unsafe { libc::setpriority(which as i32, id, prio) };

if res < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
let e = io::Error::from_raw_os_error(errno);
let e = io::Error::last_os_error();
eprintln!("setpriority: {}", e);
Err(e)
} else {
Expand Down
18 changes: 6 additions & 12 deletions sys/src/ipcrm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ fn msg_key_lookup(msgkey: i32) -> io::Result<i32> {
let res: i32 = unsafe { msgget(msgkey, 0) };

if res < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
Err(io::Error::from_raw_os_error(errno))
Err(io::Error::last_os_error())
} else {
Ok(res)
}
Expand All @@ -78,8 +77,7 @@ fn msg_rm(msgid: i32) -> io::Result<i32> {
};

if res < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
Err(io::Error::from_raw_os_error(errno))
Err(io::Error::last_os_error())
} else {
Ok(res)
}
Expand All @@ -92,8 +90,7 @@ fn shm_key_lookup(shmkey: i32) -> io::Result<i32> {
let res: i32 = unsafe { shmget(shmkey, 0, 0) };

if res < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
Err(io::Error::from_raw_os_error(errno))
Err(io::Error::last_os_error())
} else {
Ok(res)
}
Expand All @@ -109,8 +106,7 @@ fn shm_rm(shmid: i32) -> io::Result<i32> {
};

if res < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
Err(io::Error::from_raw_os_error(errno))
Err(io::Error::last_os_error())
} else {
Ok(res)
}
Expand All @@ -123,8 +119,7 @@ fn sem_key_lookup(semkey: i32) -> io::Result<i32> {
let res: i32 = unsafe { semget(semkey, 0, 0) };

if res < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
Err(io::Error::from_raw_os_error(errno))
Err(io::Error::last_os_error())
} else {
Ok(res)
}
Expand All @@ -145,8 +140,7 @@ fn sem_rm(semid: i32) -> io::Result<i32> {
let res: i32 = unsafe { semctl(semid, 0, libc::IPC_RMID, arg) };

if res < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
Err(io::Error::from_raw_os_error(errno))
Err(io::Error::last_os_error())
} else {
Ok(res)
}
Expand Down
6 changes: 2 additions & 4 deletions users/src/mesg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ fn stat_tty() -> io::Result<(i32, libc::stat)> {
let ret = libc::fstat(fd, &mut st);
if ret < 0 {
eprintln!("{}", gettext("fstat failed"));
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
return Err(io::Error::from_raw_os_error(errno));
return Err(io::Error::last_os_error());
}

Ok((fd, st))
Expand Down Expand Up @@ -106,8 +105,7 @@ fn set_mesg(fd: i32, st: libc::stat, setting: &str) -> io::Result<()> {
let chres = unsafe { libc::fchmod(fd, mode) };
if chres < 0 {
eprintln!("{}", gettext("failed to change terminal mode"));
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
return Err(io::Error::from_raw_os_error(errno));
return Err(io::Error::last_os_error());
}

Ok(())
Expand Down

0 comments on commit 6927182

Please sign in to comment.