Use errno workspace crate for mount and stat workspace crates

This commit is contained in:
Nathan Fisher 2023-02-13 00:41:58 -05:00
parent 05ce5422c0
commit 83fcb52d80
5 changed files with 23 additions and 16 deletions

1
Cargo.lock generated
View File

@ -428,6 +428,7 @@ name = "mount"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"blkid", "blkid",
"errno 0.1.0",
"sc", "sc",
] ]

View File

@ -8,3 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
blkid = "1.0" blkid = "1.0"
sc = { workspace = true } sc = { workspace = true }
[dependencies.errno]
package = "errno"
path = "../errno"

View File

@ -1,5 +1,6 @@
use errno::Errno;
use sc::*; use sc::*;
use std::{ffi::CString, io}; use std::{ffi::CString, error::Error};
mod mntent; mod mntent;
mod mntentries; mod mntentries;
@ -18,7 +19,7 @@ pub fn mount(
fstype: &str, fstype: &str,
flags: u32, flags: u32,
opts: Option<&str>, opts: Option<&str>,
) -> io::Result<()> { ) -> Result<(), Box<dyn Error>> {
let ret = unsafe { let ret = unsafe {
if let Some(opts) = opts { if let Some(opts) = opts {
syscall!( syscall!(
@ -42,33 +43,33 @@ pub fn mount(
if ret == 0 { if ret == 0 {
Ok(()) Ok(())
} else { } 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<dyn Error>> {
let ret = unsafe { syscall!(SWAPON, CString::new(dev)?.as_ptr(), flags) }; let ret = unsafe { syscall!(SWAPON, CString::new(dev)?.as_ptr(), flags) };
if ret == 0 { if ret == 0 {
Ok(()) Ok(())
} else { } 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<dyn Error>> {
let ret = unsafe { syscall!(SWAPOFF, CString::new(path)?.as_ptr()) }; let ret = unsafe { syscall!(SWAPOFF, CString::new(path)?.as_ptr()) };
if ret == 0 { if ret == 0 {
Ok(()) Ok(())
} else { } 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<dyn Error>> {
let ret = unsafe { syscall!(UMOUNT2, CString::new(special)?.as_ptr(), flags) }; let ret = unsafe { syscall!(UMOUNT2, CString::new(special)?.as_ptr(), flags) };
if ret == 0 { if ret == 0 {
Ok(()) Ok(())
} else { } else {
Err(io::Error::last_os_error()) Err(Errno::from(ret).into())
} }
} }

View File

@ -10,7 +10,7 @@ use std::{
fs::File, fs::File,
io::{self, BufRead, BufReader}, io::{self, BufRead, BufReader},
path::PathBuf, path::PathBuf,
str::FromStr, str::FromStr, error::Error,
}; };
/// The information for a mount broken out into a struct /// The information for a mount broken out into a struct
@ -26,7 +26,7 @@ pub struct MntEntry {
impl MntEntry { impl MntEntry {
/// Mount the mount specified by this entry /// Mount the mount specified by this entry
pub fn mount(&self) -> io::Result<()> { pub fn mount(&self) -> Result<(), Box<dyn Error>> {
let dev = self.device()?; let dev = self.device()?;
let dev = dev.to_string_lossy(); let dev = dev.to_string_lossy();
let (flags, opts) = self let (flags, opts) = self

View File

@ -1,8 +1,9 @@
use errno::Errno;
use sc::*; use sc::*;
use std::{ffi::CString, io}; use std::{ffi::CString, error::Error};
#[inline(always)] #[inline(always)]
pub fn mknod(path: &str, mode: u32, dev: u64) -> io::Result<()> { pub fn mknod(path: &str, mode: u32, dev: u64) -> Result<(), Box<dyn Error>> {
let ret = unsafe { let ret = unsafe {
syscall!( syscall!(
MKNODAT, MKNODAT,
@ -15,12 +16,12 @@ pub fn mknod(path: &str, mode: u32, dev: u64) -> io::Result<()> {
if ret == 0 { if ret == 0 {
Ok(()) Ok(())
} else { } else {
Err(io::Error::last_os_error()) Err(Errno::from(ret).into())
} }
} }
#[inline(always)] #[inline(always)]
pub fn mkfifo(path: &str, mode: u32) -> io::Result<()> { pub fn mkfifo(path: &str, mode: u32) -> Result<(), Box<dyn Error>> {
let ret = unsafe { let ret = unsafe {
syscall!( syscall!(
MKNODAT, MKNODAT,
@ -33,6 +34,6 @@ pub fn mkfifo(path: &str, mode: u32) -> io::Result<()> {
if ret == 0 { if ret == 0 {
Ok(()) Ok(())
} else { } else {
Err(io::Error::last_os_error()) Err(Errno::from(ret).into())
} }
} }