Skip to content

Commit

Permalink
move all filesystem functions to crate::fs
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed May 26, 2024
1 parent e3ef8f1 commit 8ec1dbd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
24 changes: 2 additions & 22 deletions src/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use smoltcp::wire::{IpEndpoint, IpListenEndpoint};

use crate::arch::kernel::core_local::core_scheduler;
use crate::executor::{block_on, poll_on};
use crate::fs::{self, DirectoryEntry, FileAttr, SeekWhence};
use crate::fs::{DirectoryEntry, FileAttr, SeekWhence};

mod eventfd;
#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "newlib")))]
Expand Down Expand Up @@ -62,7 +62,7 @@ pub(crate) type FileDescriptor = i32;
bitflags! {
/// Options for opening files
#[derive(Debug, Copy, Clone, Default)]
pub(crate) struct OpenOption: i32 {
pub struct OpenOption: i32 {
const O_RDONLY = 0o0000;
const O_WRONLY = 0o0001;
const O_RDWR = 0o0002;
Expand Down Expand Up @@ -286,26 +286,6 @@ pub(crate) trait ObjectInterface: Sync + Send + core::fmt::Debug + DynClone {
}
}

pub(crate) fn open(
name: &str,
flags: OpenOption,
mode: AccessPermission,
) -> Result<FileDescriptor, IoError> {
// mode is 0x777 (0b0111_0111_0111), when flags | O_CREAT, else 0
// flags is bitmask of O_DEC_* defined above.
// (taken from rust stdlib/sys hermit target )

debug!("Open {}, {:?}, {:?}", name, flags, mode);

let fs = fs::FILESYSTEM.get().unwrap();
if let Ok(file) = fs.open(name, flags, mode) {
let fd = insert_object(file)?;
Ok(fd)
} else {
Err(IoError::EINVAL)
}
}

pub(crate) fn read(fd: FileDescriptor, buf: &mut [u8]) -> Result<usize, IoError> {
let obj = get_object(fd)?;

Expand Down
43 changes: 40 additions & 3 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::fd::{
use crate::io::Write;
use crate::time::{timespec, SystemTime};

pub(crate) static FILESYSTEM: OnceCell<Filesystem> = OnceCell::new();
static FILESYSTEM: OnceCell<Filesystem> = OnceCell::new();

#[derive(Debug, Clone)]
pub struct DirectoryEntry {
Expand Down Expand Up @@ -370,6 +370,15 @@ pub unsafe fn create_file(
}
}

/// Removes an empty directory.
pub fn remove_dir(path: &str) -> Result<(), IoError> {
FILESYSTEM.get().unwrap().rmdir(path)
}

pub fn unlink(path: &str) -> Result<(), IoError> {
FILESYSTEM.get().unwrap().unlink(path)
}

/// Creates a new, empty directory at the provided path
pub fn create_dir(path: &str, mode: AccessPermission) -> Result<(), IoError> {
FILESYSTEM.get().unwrap().mkdir(path, mode)
Expand All @@ -382,6 +391,34 @@ pub fn readdir(name: &str) -> Result<Vec<DirectoryEntry>, IoError> {
FILESYSTEM.get().ok_or(IoError::EINVAL)?.readdir(name)
}

pub fn read_stat(name: &str) -> Result<FileAttr, IoError> {
FILESYSTEM.get().unwrap().stat(name)
}

pub fn read_lstat(name: &str) -> Result<FileAttr, IoError> {
FILESYSTEM.get().unwrap().lstat(name)
}

pub fn open(
name: &str,
flags: OpenOption,
mode: AccessPermission,
) -> Result<FileDescriptor, IoError> {
// mode is 0x777 (0b0111_0111_0111), when flags | O_CREAT, else 0
// flags is bitmask of O_DEC_* defined above.
// (taken from rust stdlib/sys hermit target )

debug!("Open {}, {:?}, {:?}", name, flags, mode);

let fs = FILESYSTEM.get().unwrap();
if let Ok(file) = fs.open(name, flags, mode) {
let fd = insert_object(file)?;
Ok(fd)
} else {
Err(IoError::EINVAL)
}
}

/// Open a directory to read the directory entries
pub(crate) fn opendir(name: &str) -> Result<FileDescriptor, IoError> {
let obj = FILESYSTEM.get().unwrap().opendir(name)?;
Expand Down Expand Up @@ -443,7 +480,7 @@ impl File {
/// an error if it does. This way, if the call succeeds, the file
/// returned is guaranteed to be new.
pub fn create(path: &str) -> Result<Self, IoError> {
let fd = fd::open(
let fd = open(
path,
OpenOption::O_CREAT | OpenOption::O_RDWR,
AccessPermission::from_bits(0o666).unwrap(),
Expand All @@ -457,7 +494,7 @@ impl File {

/// Attempts to open a file in read-write mode.
pub fn open(path: &str) -> Result<Self, IoError> {
let fd = fd::open(
let fd = open(
path,
OpenOption::O_RDWR,
AccessPermission::from_bits(0o666).unwrap(),
Expand Down
18 changes: 5 additions & 13 deletions src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,7 @@ pub(crate) fn shutdown(arg: i32) -> ! {
pub unsafe extern "C" fn sys_unlink(name: *const u8) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

fs::FILESYSTEM
.get()
.unwrap()
.unlink(name)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
fs::unlink(name).map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
}

#[hermit_macro::system]
Expand All @@ -325,19 +321,15 @@ pub unsafe extern "C" fn sys_mkdir(name: *const u8, mode: u32) -> i32 {
pub unsafe extern "C" fn sys_rmdir(name: *const c_char) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

fs::FILESYSTEM
.get()
.unwrap()
.rmdir(name)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
crate::fs::remove_dir(name).map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_stat(name: *const c_char, stat: *mut FileAttr) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

match fs::FILESYSTEM.get().unwrap().stat(name) {
match fs::read_stat(name) {
Ok(attr) => unsafe {
*stat = attr;
0
Expand All @@ -351,7 +343,7 @@ pub unsafe extern "C" fn sys_stat(name: *const c_char, stat: *mut FileAttr) -> i
pub unsafe extern "C" fn sys_lstat(name: *const c_char, stat: *mut FileAttr) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

match fs::FILESYSTEM.get().unwrap().lstat(name) {
match fs::read_lstat(name) {
Ok(attr) => unsafe {
*stat = attr;
0
Expand Down Expand Up @@ -399,7 +391,7 @@ pub unsafe extern "C" fn sys_open(name: *const c_char, flags: i32, mode: u32) ->
};

if let Ok(name) = unsafe { CStr::from_ptr(name as _) }.to_str() {
crate::fd::open(name, flags, mode)
crate::fs::open(name, flags, mode)
.unwrap_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap())
} else {
-crate::errno::EINVAL
Expand Down

0 comments on commit 8ec1dbd

Please sign in to comment.