Skip to content

Commit

Permalink
Tmpfs fixed issues with openfile seeking, now passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Dec 4, 2023
1 parent 984aa51 commit 9066c0d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
39 changes: 30 additions & 9 deletions lib/fs/src/filesystems/tmpfs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Read for TmpFile {
let reading_size = max_buffer_top - self.seek as usize;
let slice_of_self = &self.file_contents.as_slice()[self.seek as usize..max_buffer_top];

buf[..(max_buffer_top - self.seek as usize)].copy_from_slice(&slice_of_self);
buf[..reading_size].copy_from_slice(&slice_of_self);
self.seek += reading_size as u64;

Ok(reading_size)
Expand All @@ -100,6 +100,7 @@ impl_seek!(TmpFile);

pub struct TmpOpenFile {
pub(crate) file: NonNull<TmpFile>,
pub(crate) seek: u64,
}

impl TmpOpenFile {
Expand All @@ -122,42 +123,62 @@ impl TmpOpenFile {
);
inner
}

// For seek impl
fn seek_current(&self) -> u64 {
self.seek
}

// For seek impl
fn seek_max(&self) -> u64 {
self.get_ref().file_contents.len() as u64
}

// For seek impl
fn set_seek(&mut self, seek: u64) {
self.seek = seek;
}
}

impl From<&mut Box<TmpFile>> for TmpOpenFile {
fn from(value: &mut Box<TmpFile>) -> Self {
value.count_open += 1;
value.seek = 0;

let Some(ptr) = NonNull::new(value.as_ptr()) else {
unreachable!("Cannot have a box with a null ptr!");
};

Self { file: ptr }
Self { file: ptr, seek: 0 }
}
}

impl FileProvider for TmpOpenFile {}
impl Read for TmpOpenFile {
fn read(&mut self, buf: &mut [u8]) -> crate::FsResult<usize> {
self.get_mut().read(buf)
let seek = self.seek;
self.get_mut().seek(crate::io::SeekFrom::Start(seek))?;
let read_len = self.get_mut().read(buf)?;
self.seek += read_len as u64;
Ok(read_len)
}
}

impl Write for TmpOpenFile {
fn write(&mut self, buf: &[u8]) -> crate::FsResult<usize> {
self.get_mut().write(buf)
let seek = self.seek;
self.get_mut().seek(crate::io::SeekFrom::Start(seek))?;
let write_len = self.get_mut().write(buf)?;
self.seek += write_len as u64;
Ok(write_len)
}

fn flush(&mut self) -> crate::FsResult<()> {
Ok(())
}
}

impl Seek for TmpOpenFile {
fn seek(&mut self, pos: crate::io::SeekFrom) -> crate::FsResult<u64> {
self.get_mut().seek(pos)
}
}
impl_seek!(TmpOpenFile);

impl Metadata for TmpOpenFile {
fn permissions(&self) -> Permissions {
Expand Down
34 changes: 33 additions & 1 deletion lib/fs/src/filesystems/tmpfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ mod test {

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

assert_eq!(&read_buff, b"Hello World!");
}
Expand Down Expand Up @@ -324,4 +324,36 @@ mod test {
tmpfs.rmdir("/test/test/".into()).unwrap();
tmpfs.rmdir("/test".into()).unwrap();
}

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

tmpfs.mkdir("/myfiles".into(), Permissions::all()).unwrap();
tmpfs
.mkdir("/myfiles/1".into(), Permissions::all())
.unwrap();
tmpfs
.touch("/myfiles/test.txt".into(), Permissions::all())
.unwrap();
{
let mut file = tmpfs.open_file("/myfiles/test.txt".into()).unwrap();
file.write(b"Hello World").unwrap();
assert_eq!(file.stream_len(), Ok(11));
}

tmpfs.rmdir("/myfiles/1".into()).unwrap();

{
let mut file = tmpfs.open_file("/myfiles/test.txt".into()).unwrap();
assert_eq!(file.stream_len(), Ok(11));
let mut string = [0u8; 11];
assert_eq!(file.read(&mut string), Ok(11));
assert_eq!(&string, b"Hello World");
}

tmpfs.rm("/myfiles/test.txt".into()).unwrap();
tmpfs.rmdir("/myfiles".into()).unwrap();
}
}

0 comments on commit 9066c0d

Please sign in to comment.