Skip to content

Commit

Permalink
Port a subset of basic std::fs functionality (theseus-os#510)
Browse files Browse the repository at this point in the history
* Add a Theseus-specific port of select `std::fs` functionality, including basic file I/O (read, write, seek) 
* Add our own fork of `core2` with expanded `std::io` traits
  • Loading branch information
kevinaboos authored Apr 28, 2022
1 parent 1bff1f6 commit fa85796
Show file tree
Hide file tree
Showing 15 changed files with 8,593 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
[submodule "libs/thiserror-core2"]
path = libs/thiserror-core2
url = https://github.com/theseus-os/thiserror-core2.git
[submodule "libs/core2"]
path = libs/core2
url = https://github.com/theseus-os/core2.git
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
members = [
"kernel/[!.]*/",
"applications/[!.]*/",
"ports/theseus_std",
]


Expand Down Expand Up @@ -59,6 +60,8 @@ smoltcp = { git = "https://github.com/m-labs/smoltcp" }

### Patch `libc` so we can use libc-specific types when using `cfg(target_os = "theseus")`.
libc = { path = "ports/libc" }
### Patch `core2` with newer functions from `std::io`, e.g., additional `Seek` trait functions
core2 = { path = "libs/core2" }

##############################################################################################
#################### Below are patches for wasmtime-related crates. ##########################
Expand Down
21 changes: 17 additions & 4 deletions kernel/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern crate lockable;
#[cfg(test)]
mod test;

use core::{borrow::Borrow, cmp::min, marker::PhantomData, ops::{Deref, Range}};
use core::{borrow::Borrow, cmp::min, marker::PhantomData, ops::{Deref, DerefMut, Range}};
use alloc::{boxed::Box, string::String, vec::Vec};
use lockable::Lockable;
use core2::io::{Seek, SeekFrom};
Expand Down Expand Up @@ -478,9 +478,11 @@ impl<RW> BlockWriter for ByteWriterWrapper<RW> where RW: BlockReader + BlockWrit
/// A readable and writable "stateful" I/O stream that keeps track
/// of its current offset within its internal stateless I/O stream.
///
/// This implements the [`core2::io::Read`] and [`core2::io::Write`] traits for read and write access,
/// as well as the [`core2::io::Seek`] trait if the underlying I/O stream implements [`KnownLength`].
/// It also forwards all other I/O-related traits implemented by the underlying I/O stream.
/// ## Trait implementations
/// * This implements the [`core2::io::Read`] and [`core2::io::Write`] traits for read and write access.
/// * This implements the [`core2::io::Seek`] trait if the underlying I/O stream implements [`KnownLength`].
/// * This also forwards all other I/O-related traits implemented by the underlying I/O stream.
/// * This derefs into the inner `IO` type, via both [`Deref`] and [`DerefMut`].
pub struct ReaderWriter<IO> {
io: IO,
offset: u64,
Expand All @@ -491,6 +493,17 @@ impl<IO> ReaderWriter<IO> where IO: ByteReader + ByteWriter {
ReaderWriter { io, offset: 0 }
}
}
impl<IO> Deref for ReaderWriter<IO> {
type Target = IO;
fn deref(&self) -> &Self::Target {
&self.io
}
}
impl<IO> DerefMut for ReaderWriter<IO> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.io
}
}
impl<IO> core2::io::Read for ReaderWriter<IO> where IO: ByteReader {
fn read(&mut self, buf: &mut [u8]) -> core2::io::Result<usize> {
let bytes_read = self.io.read_at(buf, self.offset as usize)
Expand Down
1 change: 1 addition & 0 deletions libs/core2
Submodule core2 added at 699809
Loading

0 comments on commit fa85796

Please sign in to comment.