From 83fcb52d80334292c32a39f4ab7753c7ea78da93 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Mon, 13 Feb 2023 00:41:58 -0500 Subject: [PATCH] Use `errno` workspace crate for `mount` and `stat` workspace crates --- Cargo.lock | 1 + mount/Cargo.toml | 4 ++++ mount/src/lib.rs | 19 ++++++++++--------- mount/src/mntent.rs | 4 ++-- src/stat/mod.rs | 11 ++++++----- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bb82fcd..cb92e58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -428,6 +428,7 @@ name = "mount" version = "0.1.0" dependencies = [ "blkid", + "errno 0.1.0", "sc", ] diff --git a/mount/Cargo.toml b/mount/Cargo.toml index f457bd2..53cfad8 100644 --- a/mount/Cargo.toml +++ b/mount/Cargo.toml @@ -8,3 +8,7 @@ edition = "2021" [dependencies] blkid = "1.0" sc = { workspace = true } + +[dependencies.errno] +package = "errno" +path = "../errno" diff --git a/mount/src/lib.rs b/mount/src/lib.rs index 3226451..74dc247 100644 --- a/mount/src/lib.rs +++ b/mount/src/lib.rs @@ -1,5 +1,6 @@ +use errno::Errno; use sc::*; -use std::{ffi::CString, io}; +use std::{ffi::CString, error::Error}; mod mntent; mod mntentries; @@ -18,7 +19,7 @@ pub fn mount( fstype: &str, flags: u32, opts: Option<&str>, -) -> io::Result<()> { +) -> Result<(), Box> { let ret = unsafe { if let Some(opts) = opts { syscall!( @@ -42,33 +43,33 @@ pub fn mount( if ret == 0 { Ok(()) } else { - Err(io::Error::last_os_error()) + Err(Errno::from(ret).into()) } } -pub fn swapon(dev: &str, flags: usize) -> io::Result<()> { +pub fn swapon(dev: &str, flags: usize) -> Result<(), Box> { let ret = unsafe { syscall!(SWAPON, CString::new(dev)?.as_ptr(), flags) }; if ret == 0 { Ok(()) } else { - Err(io::Error::last_os_error()) + Err(Errno::from(ret).into()) } } -pub fn swapoff(path: &str) -> io::Result<()> { +pub fn swapoff(path: &str) -> Result<(), Box> { let ret = unsafe { syscall!(SWAPOFF, CString::new(path)?.as_ptr()) }; if ret == 0 { Ok(()) } else { - Err(io::Error::last_os_error()) + Err(Errno::from(ret).into()) } } -pub fn umount(special: &str, flags: u32) -> io::Result<()> { +pub fn umount(special: &str, flags: u32) -> Result<(), Box> { let ret = unsafe { syscall!(UMOUNT2, CString::new(special)?.as_ptr(), flags) }; if ret == 0 { Ok(()) } else { - Err(io::Error::last_os_error()) + Err(Errno::from(ret).into()) } } diff --git a/mount/src/mntent.rs b/mount/src/mntent.rs index 6f23413..16eee83 100644 --- a/mount/src/mntent.rs +++ b/mount/src/mntent.rs @@ -10,7 +10,7 @@ use std::{ fs::File, io::{self, BufRead, BufReader}, path::PathBuf, - str::FromStr, + str::FromStr, error::Error, }; /// The information for a mount broken out into a struct @@ -26,7 +26,7 @@ pub struct MntEntry { impl MntEntry { /// Mount the mount specified by this entry - pub fn mount(&self) -> io::Result<()> { + pub fn mount(&self) -> Result<(), Box> { let dev = self.device()?; let dev = dev.to_string_lossy(); let (flags, opts) = self diff --git a/src/stat/mod.rs b/src/stat/mod.rs index 887da4b..3addc57 100644 --- a/src/stat/mod.rs +++ b/src/stat/mod.rs @@ -1,8 +1,9 @@ +use errno::Errno; use sc::*; -use std::{ffi::CString, io}; +use std::{ffi::CString, error::Error}; #[inline(always)] -pub fn mknod(path: &str, mode: u32, dev: u64) -> io::Result<()> { +pub fn mknod(path: &str, mode: u32, dev: u64) -> Result<(), Box> { let ret = unsafe { syscall!( MKNODAT, @@ -15,12 +16,12 @@ pub fn mknod(path: &str, mode: u32, dev: u64) -> io::Result<()> { if ret == 0 { Ok(()) } else { - Err(io::Error::last_os_error()) + Err(Errno::from(ret).into()) } } #[inline(always)] -pub fn mkfifo(path: &str, mode: u32) -> io::Result<()> { +pub fn mkfifo(path: &str, mode: u32) -> Result<(), Box> { let ret = unsafe { syscall!( MKNODAT, @@ -33,6 +34,6 @@ pub fn mkfifo(path: &str, mode: u32) -> io::Result<()> { if ret == 0 { Ok(()) } else { - Err(io::Error::last_os_error()) + Err(Errno::from(ret).into()) } }