Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,12 @@ simple-shell = { version = "0.0.1", optional = true }
smallvec = { version = "1", features = ["const_new"] }
take-static = "0.1"
talc = { version = "5" }
tar-no-std = { version = "0.4", features = ["alloc"] }
thiserror = { version = "2", default-features = false }
time = { version = "0.3", default-features = false }
volatile = "0.6"
uhyve-interface = "0.1.4"
hex = { version = "0.4", default-features = false, features = ["alloc"] }

[dependencies.smoltcp]
version = "0.13"
Expand Down
92 changes: 77 additions & 15 deletions src/fs/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,6 @@ pub(crate) struct RamFileInner {
pub attr: FileAttr,
}

impl RamFileInner {
pub fn new(attr: FileAttr) -> Self {
Self {
data: Vec::new(),
attr,
}
}
}

pub struct RamFileInterface {
/// Position within the file
pos: Mutex<usize>,
Expand Down Expand Up @@ -354,6 +345,10 @@ impl VfsNode for RamFile {

impl RamFile {
pub fn new(mode: AccessPermission) -> Self {
Self::new_with_data(Vec::new(), mode)
}

fn new_with_data(data: Vec<u8>, mode: AccessPermission) -> Self {
let microseconds = arch::kernel::systemtime::now_micros();
let t = timespec::from_usec(microseconds as i64);
let attr = FileAttr {
Expand All @@ -365,7 +360,7 @@ impl RamFile {
};

Self {
data: Arc::new(RwLock::new(RamFileInner::new(attr))),
data: Arc::new(RwLock::new(RamFileInner { data, attr })),
}
}
}
Expand Down Expand Up @@ -464,6 +459,68 @@ impl MemDirectory {
}
}

#[allow(unused)]
pub fn try_from_image(image: &'static [u8]) -> io::Result<Self> {
let this = Self::new(AccessPermission::S_IRUSR);

for i in image.chunks(1024) {
debug!("[DUMP] {}", hex::encode(i));
}

let taref = tar_no_std::TarArchiveRef::new(image).map_err(|e| {
error!("[Hermit image] Tar file has invalid format: {e:?}");
Errno::Inval
})?;

for i in taref.entries() {
let filename = i.filename();
let filename = filename.as_str().map_err(|e| {
error!(
"[Hermit image] Tar entry has not supported filename (non UTF-8): {filename:?}; {e}",
);
Errno::Inval
})?;
if filename.is_empty() {
continue;
}
debug!("[Hermit image] Processing tar entry: {filename}");

let mode = i.posix_header().mode.to_flags().map_err(|e| {
error!(
"[Hermit image] Tar entry {filename:?} has invalid mode: {:?}; {e}",
i.posix_header().mode,
);
Errno::Inval
})?;
let mode = AccessPermission::from_bits(mode.bits() as u32).ok_or_else(|| {
error!("[Hermit image] Tar entry {filename:?} has invalid mode: {mode:?}");
Errno::Inval
})?;

for (i, _) in filename.match_indices("/") {
let part = &filename[..i];
if this.traverse_lstat(part).is_err() {
this.traverse_mkdir(
part,
AccessPermission::S_IRUSR
| AccessPermission::S_IWUSR
| AccessPermission::S_IRGRP,
)
.inspect_err(|e| {
error!("[Hermit image] Unable to mkdir {part:?}: {e}");
})?;
}
}

this.traverse_create_file(filename, i.data(), mode)
.inspect_err(|e| {
error!("[Hermit image] Unable to write entry {filename:?}: {e}");
})?;
}

Ok(this)
}

async fn async_traverse_open(
&self,
path: &str,
Expand Down Expand Up @@ -693,11 +750,16 @@ impl VfsNode for MemDirectory {
return directory.traverse_create_file(rest, data, mode);
}

let file = RomFile::new(data, mode);
self.inner
.write()
.await
.insert(component.to_owned(), Box::new(file));
let file: Box<dyn VfsNode> = if mode.contains(AccessPermission::S_IWUSR)
|| mode.contains(AccessPermission::S_IWGRP)
|| mode.contains(AccessPermission::S_IWOTH)
{
Box::new(RamFile::new_with_data(data.to_vec(), mode))
} else {
Box::new(RomFile::new(data, mode))
};

self.inner.write().await.insert(component.to_owned(), file);
Ok(())
},
None,
Expand Down
Loading