Skip to content

Commit

Permalink
add dummyfs
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed May 2, 2024
1 parent c3c2541 commit fbab2ba
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 25 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions lib/mrf-tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mrf-manifest = { path = "../mrf-manifest", features = [
"encode",
"serialise",
] }
polonius-the-crab = "0.4.1"
serde_json = "1.0.116"
wasmparser = "0.206.0"

Expand Down
99 changes: 99 additions & 0 deletions lib/mrf-tool/src/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use polonius_the_crab::{polonius, polonius_return};
use std::{
collections::HashMap,
fs::{self, File},
io,
path::{Path, PathBuf},
};

pub trait Filesystem {
type File<'a>: io::Write
where
Self: 'a;

fn copy(&mut self, src: &Path, dst: &Path) -> io::Result<()>;
fn read(&mut self, path: &Path) -> io::Result<Vec<u8>>;

fn create_or_truncate(&mut self, path: &Path) -> io::Result<Self::File<'_>>;
fn open_append(&mut self, path: &Path) -> io::Result<Self::File<'_>>;
}

pub struct DummyFs {
inner: HashMap<PathBuf, Vec<u8>>,
}

#[inline]
fn file_not_found() -> io::Error {
io::Error::new(io::ErrorKind::NotFound, "file not found")
}

Check warning on line 28 in lib/mrf-tool/src/fs.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/fs.rs#L26-L28

Added lines #L26 - L28 were not covered by tests

impl Filesystem for DummyFs {
type File<'a> = &'a mut Vec<u8>;

#[inline]
fn copy(&mut self, src: &Path, dst: &Path) -> io::Result<()> {
let value = self.inner.get(src).ok_or_else(file_not_found)?;
self.inner.insert(dst.to_path_buf(), value.clone());

Ok(())
}

Check warning on line 39 in lib/mrf-tool/src/fs.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/fs.rs#L34-L39

Added lines #L34 - L39 were not covered by tests

#[inline]
fn read(&mut self, path: &Path) -> io::Result<Vec<u8>> {
self.inner.get(path).cloned().ok_or_else(file_not_found)
}

Check warning on line 44 in lib/mrf-tool/src/fs.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/fs.rs#L42-L44

Added lines #L42 - L44 were not covered by tests

#[inline]
fn create_or_truncate(&mut self, path: &Path) -> io::Result<Self::File<'_>> {
let mut this = self;

Check warning on line 48 in lib/mrf-tool/src/fs.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/fs.rs#L47-L48

Added lines #L47 - L48 were not covered by tests
// TODO: Remove once we can FINALLY have polonius..
polonius!(|this| -> io::Result<&'polonius mut Vec<u8>> {
if let Some(value) = this.inner.get_mut(path) {
value.clear();
polonius_return!(Ok(value));
}
});

this.inner.insert(path.to_path_buf(), Vec::new());
this.create_or_truncate(path)
}

Check warning on line 59 in lib/mrf-tool/src/fs.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/fs.rs#L57-L59

Added lines #L57 - L59 were not covered by tests

#[inline]
fn open_append(&mut self, path: &Path) -> io::Result<Self::File<'_>> {
self.inner.get_mut(path).ok_or_else(file_not_found)
}

Check warning on line 64 in lib/mrf-tool/src/fs.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/fs.rs#L62-L64

Added lines #L62 - L64 were not covered by tests
}

#[derive(Default)]
pub struct NativeFs {
_priv: (),
}

impl Filesystem for NativeFs {
type File<'a> = File;

#[inline]
fn copy(&mut self, src: &Path, dst: &Path) -> io::Result<()> {
fs::copy(src, dst)?;
Ok(())
}

Check warning on line 79 in lib/mrf-tool/src/fs.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/fs.rs#L76-L79

Added lines #L76 - L79 were not covered by tests

#[inline]
fn read(&mut self, path: &Path) -> io::Result<Vec<u8>> {
fs::read(path)
}

Check warning on line 84 in lib/mrf-tool/src/fs.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/fs.rs#L82-L84

Added lines #L82 - L84 were not covered by tests

#[inline]
fn create_or_truncate(&mut self, path: &Path) -> io::Result<Self::File<'_>> {
File::options()
.create(true)
.truncate(true)
.write(true)
.open(path)
}

Check warning on line 93 in lib/mrf-tool/src/fs.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/fs.rs#L87-L93

Added lines #L87 - L93 were not covered by tests

#[inline]
fn open_append(&mut self, path: &Path) -> io::Result<Self::File<'_>> {
File::options().append(true).open(path)
}

Check warning on line 98 in lib/mrf-tool/src/fs.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/fs.rs#L96-L98

Added lines #L96 - L98 were not covered by tests
}
48 changes: 24 additions & 24 deletions lib/mrf-tool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use self::args::{ManifestSubcommand, ModuleSubcommand, ToolArgs, ToolSubcommand};
use clap::Parser;
use color_eyre::{eyre::bail, Result};
use std::{
ffi::OsString,
fs::{self, File},
io::Write,
path::Path,
};
use std::{ffi::OsString, io::Write, path::Path};

pub use self::fs::{Filesystem, NativeFs};

mod args;
mod fs;

pub fn read_manifest<W>(sink: &mut W, module: &[u8]) -> Result<()>
where
Expand All @@ -24,62 +22,64 @@ where
Ok(())
}

Check warning on line 23 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L22-L23

Added lines #L22 - L23 were not covered by tests

pub fn remove_manifest(module_path: &Path, output_path: &Path) -> Result<()> {
let module = fs::read(module_path)?;
pub fn remove_manifest<F>(fs: &mut F, module_path: &Path, output_path: &Path) -> Result<()>
where
F: Filesystem,
{
let module = fs.read(module_path)?;
let Some((_manifest, section_range)) = mrf_manifest::decode(&module)? else {
bail!("missing manifest in module");

Check warning on line 31 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L25-L31

Added lines #L25 - L31 were not covered by tests
};

let mut module_file = File::options()
.create(true)
.truncate(true)
.write(true)
.open(output_path)?;

let mut module_file = fs.create_or_truncate(output_path)?;
module_file.write_all(&module[..section_range.start])?;
module_file.write_all(&module[section_range.end..])?;

Check warning on line 36 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L34-L36

Added lines #L34 - L36 were not covered by tests

Ok(())
}

Check warning on line 39 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L38-L39

Added lines #L38 - L39 were not covered by tests

pub fn write_manifest(manifest: &[u8], module_path: &Path) -> Result<()> {
pub fn write_manifest<F>(fs: &mut F, manifest: &[u8], module_path: &Path) -> Result<()>
where
F: Filesystem,
{

Check warning on line 44 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L41-L44

Added lines #L41 - L44 were not covered by tests
// Parse the manifest and re-encode it in canonical JSON
let parsed_manifest = serde_json::from_slice(manifest)?;
let custom_section = mrf_manifest::encode(&parsed_manifest)?;

Check warning on line 47 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L46-L47

Added lines #L46 - L47 were not covered by tests

let mut file = File::options().append(true).open(module_path)?;
let mut file = fs.open_append(module_path)?;
file.write_all(&custom_section)?;

Check warning on line 50 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L49-L50

Added lines #L49 - L50 were not covered by tests

Ok(())
}

Check warning on line 53 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L52-L53

Added lines #L52 - L53 were not covered by tests

pub fn handle<I, W>(sink: &mut W, input: I) -> Result<()>
pub fn handle<F, W, I>(fs: &mut F, sink: &mut W, input: I) -> Result<()>
where
F: Filesystem,
W: Write,
I: IntoIterator,
<I as IntoIterator>::Item: Into<OsString> + Clone,
W: Write,
{
let args = ToolArgs::try_parse_from(input)?;
match args.command {
ToolSubcommand::Manifest(ManifestSubcommand::Add(args)) => {
let manifest = fs::read(args.manifest_path)?;
let manifest = fs.read(&args.manifest_path)?;

Check warning on line 65 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L55-L65

Added lines #L55 - L65 were not covered by tests

// Only copy if the paths are distinct
if args.module_path != args.output {
fs::copy(&args.module_path, &args.output)?;
fs.copy(&args.module_path, &args.output)?;
}

Check warning on line 70 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L68-L70

Added lines #L68 - L70 were not covered by tests

write_manifest(&manifest, &args.output)?;
write_manifest(fs, &manifest, &args.output)?;

Check warning on line 72 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L72

Added line #L72 was not covered by tests
}
ToolSubcommand::Manifest(ManifestSubcommand::Read(args)) => {
let data = fs::read(args.module_path)?;
let data = fs.read(&args.module_path)?;
read_manifest(sink, &data)?;

Check warning on line 76 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L74-L76

Added lines #L74 - L76 were not covered by tests
}
ToolSubcommand::Manifest(ManifestSubcommand::Remove(args)) => {
remove_manifest(&args.module_path, &args.output)?;
remove_manifest(fs, &args.module_path, &args.output)?;

Check warning on line 79 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L78-L79

Added lines #L78 - L79 were not covered by tests
}
ToolSubcommand::Module(ModuleSubcommand::Validate(args)) => {
let data = fs::read(args.module_path)?;
let data = fs.read(&args.module_path)?;
wasmparser::validate(&data)?;

Check warning on line 83 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L81-L83

Added lines #L81 - L83 were not covered by tests
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/mrf-tool/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use color_eyre::Result;
use mrf_tool::NativeFs;
use std::{env, io};

fn main() -> Result<()> {
let mut fs = NativeFs::default();
let stdout = io::stdout();

Check warning on line 7 in lib/mrf-tool/src/main.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/main.rs#L6-L7

Added lines #L6 - L7 were not covered by tests
if let Err(error) = mrf_tool::handle(&mut stdout.lock(), env::args_os()) {

if let Err(error) = mrf_tool::handle(&mut fs, &mut stdout.lock(), env::args_os()) {
if let Some(error) = error.downcast_ref::<clap::Error>() {
error.exit();

Check warning on line 11 in lib/mrf-tool/src/main.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/main.rs#L9-L11

Added lines #L9 - L11 were not covered by tests
}
Expand Down

0 comments on commit fbab2ba

Please sign in to comment.