Skip to content

Commit

Permalink
support for reading and writing to tmpfs files
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Dec 1, 2023
1 parent cba657b commit e40b5e5
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 13 deletions.
150 changes: 137 additions & 13 deletions lib/fs/src/filesystems/tmpfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

use self::{dir::TmpDirectory, file::TmpFile};
use crate::{error::FsError, io::FileSystemProvider};
use self::{
dir::TmpDirectory,
file::{TmpFile, TmpOpenFile},
};
use crate::{
error::FsError, filesystems::tmpfs::dir::TmpOpenDirectory, io::FileSystemProvider, path::Path,
permission::Permissions, FsResult,
};
use qk_alloc::{boxed::Box, vec::Vec};

mod dir;
Expand All @@ -36,49 +42,113 @@ pub struct TmpFs {
}

impl TmpFs {
pub fn new() -> Self {
pub fn new(root_perm: Permissions) -> Self {
let mut dirs = Vec::new();
let root_dir = TmpDirectory::new("/".into(), root_perm);
dirs.push(Box::new(root_dir));

Self {
files: Vec::new(),
dires: Vec::new(),
dires: dirs,
}
}

fn get_dir_index_for_path(&mut self, path: Path) -> FsResult<usize> {
self.dires
.iter()
.enumerate()
.find(|(_, entry)| entry.path == path)
.map(|(index, _)| index)
.ok_or(FsError::new(
crate::error::FsErrorKind::NotFound,
"That directory was not found!",
))
}

fn get_file_index_for_path(&mut self, path: Path) -> FsResult<usize> {
self.files
.iter()
.enumerate()
.find(|(_, entry)| entry.path == path)
.map(|(index, _)| index)
.ok_or(FsError::new(
crate::error::FsErrorKind::NotFound,
"That file was not found!",
))
}

fn does_parent_exist(&self, path: Path) -> bool {
self.dires.iter().any(|entry| path.is_child_of(&entry.path))
}
}

impl FileSystemProvider for TmpFs {
fn open_directory(
&mut self,
path: crate::path::Path,
) -> crate::FsResult<qk_alloc::boxed::Box<dyn crate::io::DirectoryProvider>> {
todo!()
let dir_index = self.get_dir_index_for_path(path)?;
let tmp_open = TmpOpenDirectory::from(self.dires[dir_index].as_ref());

Ok(Box::new(tmp_open))
}

fn open_file(
&mut self,
path: crate::path::Path,
) -> crate::FsResult<qk_alloc::boxed::Box<dyn crate::io::FileProvider>> {
todo!()
let file_index = self.get_file_index_for_path(path)?;
let tmp_file = TmpOpenFile::from(&mut self.files[file_index]);

Ok(Box::new(tmp_file))
}

fn rmdir(&mut self, path: crate::path::Path) -> crate::FsResult<()> {
todo!()
let dir_index = self.get_dir_index_for_path(path)?;

// The scope of this refrence will no longer be valid when we move the entry
// thats why we enclose it in this block so the refrence is dropped before our
// value is deleted.
{
let dir_ref = &self.dires[dir_index];

if dir_ref.entries.len() > 0 {
return Err(FsError::new(
crate::error::FsErrorKind::PermissionDenied,
"Cannot remove a directory with children, remove children before deleting the directory!",
));
}
}

self.dires.remove(dir_index);
Ok(())
}

fn rm(&mut self, path: crate::path::Path) -> crate::FsResult<()> {
todo!()
let file_index = self.get_file_index_for_path(path)?;
self.files.remove(file_index);
Ok(())
}

fn mkdir(
&mut self,
path: crate::path::Path,
permission: crate::permission::Permissions,
) -> crate::FsResult<()> {
if self.dires.iter().any(|entry| entry.path == path) {
if self.get_dir_index_for_path(path.clone()).is_ok() {
return Err(FsError::new(
crate::error::FsErrorKind::AlreadyExists,
"The directory already exists at that path!",
));
}

if !self.does_parent_exist(path.clone()) {
return Err(FsError::new(
crate::error::FsErrorKind::InvalidInput,
"The parent for this directory does not exist!",
));
}

let new_dir = TmpDirectory::new(path, permission);
self.dires.push(Box::new(new_dir));

Expand All @@ -90,7 +160,23 @@ impl FileSystemProvider for TmpFs {
path: crate::path::Path,
permission: crate::permission::Permissions,
) -> crate::FsResult<()> {
todo!()
if self.get_file_index_for_path(path.clone()).is_ok() {
return Err(FsError::new(
crate::error::FsErrorKind::AlreadyExists,
"The file already exists at that path!",
));
}

if !self.does_parent_exist(path.clone()) {
return Err(FsError::new(
crate::error::FsErrorKind::InvalidInput,
"The parent for this file does not exist!",
));
}

let new_file = TmpFile::new(path.clone(), permission);
self.files.push(Box::new(new_file));
Ok(())
}

fn supports_permissions(&self) -> bool {
Expand All @@ -100,19 +186,57 @@ impl FileSystemProvider for TmpFs {

#[cfg(test)]
mod test {
use crate::permission::Permissions;

use super::*;

#[test]
fn test_tmp_dir_new() {
fn setup_test() -> crate::Vfs {
crate::set_example_allocator();

let tmpfs = TmpFs::new();
let tmpfs = TmpFs::new(Permissions::all());
let mut vfs = crate::Vfs::new();
assert_eq!(vfs.mount("/".into(), Box::new(tmpfs)), Ok(0));

vfs
}

#[test]
fn test_tmp_dir_new() {
let mut vfs = setup_test();
assert!(matches!(vfs.unmount_id(0), Ok(_)));
}

#[test]
fn test_tmp_dir_newfile() {
crate::set_example_allocator();
let mut tmpfs = TmpFs::new(Permissions::all());

tmpfs.touch("/test.txt".into(), Permissions::all()).unwrap();
}

#[test]
fn test_tmp_dir_newdir() {
crate::set_example_allocator();
let mut tmpfs = TmpFs::new(Permissions::all());

tmpfs.mkdir("/test/".into(), Permissions::all()).unwrap();
}

#[test]
fn test_tmp_dir_create_file_read_and_write() {
crate::set_example_allocator();
let mut tmpfs = TmpFs::new(Permissions::all());

tmpfs.touch("/test.txt".into(), Permissions::all()).unwrap();
let mut file = tmpfs.open_file("/test.txt".into()).unwrap();

file.write(b"Hello World!").unwrap();
file.flush().unwrap();

file.seek(crate::io::SeekFrom::Start(0)).unwrap();
let mut read_buff = [0_u8; 12];
file.read(&mut read_buff).unwrap();

assert_eq!(&read_buff, b"Hello World!");
}
}
1 change: 1 addition & 0 deletions lib/fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA
use crate::error::FsError;

mod abstract_buffer;
pub mod dir;
pub mod disks;
pub mod error;
pub mod filesystems;
Expand Down

0 comments on commit e40b5e5

Please sign in to comment.