Added more tests for the dev files and added urandom support

This commit is contained in:
Johnathan Sharratt
2023-03-16 16:46:35 +11:00
parent d7e21293fe
commit 0b8d2b1248
41 changed files with 311 additions and 34 deletions

View File

@@ -24,6 +24,12 @@ tokio = { version = "1", features = [ "io-util", "sync", "macros" ], default_fea
pin-project-lite = "0.2.9"
indexmap = "1.9.2"
[target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dependencies]
getrandom = { version = "0.2" }
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
getrandom = { version = "0.2", features = [ "js" ] }
[dev-dependencies]
tempfile = "3.4.0"
tokio = { version = "1", features = [ "io-util", "rt" ], default_features = false }

View File

@@ -1,3 +1,4 @@
use crate::random_file::RandomFile;
use crate::{FileSystem, VirtualFile};
use std::path::{Path, PathBuf};
use tracing::*;
@@ -81,6 +82,10 @@ impl RootFileSystemBuilder {
let _ = tmp
.new_open_options_ext()
.insert_device_file(PathBuf::from("/dev/zero"), Box::new(ZeroFile::default()));
let _ = tmp.new_open_options_ext().insert_device_file(
PathBuf::from("/dev/urandom"),
Box::new(RandomFile::default()),
);
let _ = tmp.new_open_options_ext().insert_device_file(
PathBuf::from("/dev/stdin"),
self.stdin

View File

@@ -20,6 +20,7 @@ pub mod host_fs;
pub mod mem_fs;
pub mod null_file;
pub mod passthru_fs;
pub mod random_file;
pub mod special_file;
pub mod tmp_fs;
pub mod union_fs;

View File

@@ -0,0 +1,88 @@
//! Used for /dev/zero - infinitely returns zero
//! which is useful for commands like `dd if=/dev/zero of=bigfile.img size=1G`
use std::io::{self, *};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite};
use crate::VirtualFile;
#[derive(Debug, Default)]
pub struct RandomFile {}
impl AsyncSeek for RandomFile {
fn start_seek(self: Pin<&mut Self>, _position: SeekFrom) -> io::Result<()> {
Ok(())
}
fn poll_complete(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
Poll::Ready(Ok(0))
}
}
impl AsyncWrite for RandomFile {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(buf.len()))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_write_vectored(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(bufs.len()))
}
fn is_write_vectored(&self) -> bool {
false
}
}
impl AsyncRead for RandomFile {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let mut data = vec![0u8; buf.remaining()];
getrandom::getrandom(&mut data).ok();
buf.put_slice(&data[..]);
Poll::Ready(Ok(()))
}
}
impl VirtualFile for RandomFile {
fn last_accessed(&self) -> u64 {
0
}
fn last_modified(&self) -> u64 {
0
}
fn created_time(&self) -> u64 {
0
}
fn size(&self) -> u64 {
0
}
fn set_len(&mut self, _new_size: u64) -> crate::Result<()> {
Ok(())
}
fn unlink(&mut self) -> crate::Result<()> {
Ok(())
}
fn poll_read_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(0))
}
fn poll_write_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(0))
}
}