From 8fc2a1382825c2563546fe9a2b6a43258942cf61 Mon Sep 17 00:00:00 2001 From: Mackenzie Clark Date: Thu, 14 Mar 2019 13:32:47 -0700 Subject: [PATCH] use the header --- ..._vfs_bundled.wasm => test_vfs_bundle.wasm} | Bin 46109 -> 46118 bytes lib/emscripten/src/lib.rs | 4 +- lib/emscripten/tests/emtests/test_vfs.rs | 2 +- lib/runtime-abi/src/vfs/mod.rs | 1 + lib/runtime-abi/src/vfs/vfs.rs | 9 +++ lib/runtime-abi/src/vfs/vfs_header.rs | 57 ++++++++++++++++++ 6 files changed, 70 insertions(+), 3 deletions(-) rename lib/emscripten/emtests/{test_vfs_bundled.wasm => test_vfs_bundle.wasm} (99%) create mode 100644 lib/runtime-abi/src/vfs/vfs_header.rs diff --git a/lib/emscripten/emtests/test_vfs_bundled.wasm b/lib/emscripten/emtests/test_vfs_bundle.wasm similarity index 99% rename from lib/emscripten/emtests/test_vfs_bundled.wasm rename to lib/emscripten/emtests/test_vfs_bundle.wasm index 6df8c13e08109fab3be1e4b7ccf58295cfacbb6d..92f3bbbf95a15687f24e72cfe8cbd3d16c84a6cb 100644 GIT binary patch delta 135 zcmV;20C@kM=mMtb0$f<8Xm$rEo1I+@{D0q#zwRyU gkeh)D0M!G3kN`DM0t~l6;sKBYmIXMhv=t{H2W2=kD*ylh diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 6c34d03d0..5dce8a805 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -240,9 +240,9 @@ pub fn run_emscripten_instance( #[cfg(feature = "vfs")] { data.vfs = match module.info().custom_sections.get("wasmer:fs") { - Some(bytes) => match Vfs::from_tar_zstd_bytes(&bytes[..]) { + Some(bytes) => match Vfs::from_compressed_bytes(&bytes[..]) { Ok(vfs_backing) => Some(vfs_backing), - Err(_) => None, + Err(e) => None, }, None => None, }; diff --git a/lib/emscripten/tests/emtests/test_vfs.rs b/lib/emscripten/tests/emtests/test_vfs.rs index 7cd1007e2..11acd4616 100644 --- a/lib/emscripten/tests/emtests/test_vfs.rs +++ b/lib/emscripten/tests/emtests/test_vfs.rs @@ -2,7 +2,7 @@ use crate::emtests::_common::assert_emscripten_output; #[test] fn test_vfs() { - let wasm_bytes = include_bytes!("../../emtests/test_vfs_bundled.wasm"); + let wasm_bytes = include_bytes!("../../emtests/test_vfs_bundle.wasm"); let expected_str = include_str!("../../emtests/test_vfs.out"); assert_emscripten_output(wasm_bytes, expected_str); } diff --git a/lib/runtime-abi/src/vfs/mod.rs b/lib/runtime-abi/src/vfs/mod.rs index 10d397e7d..92282f44d 100644 --- a/lib/runtime-abi/src/vfs/mod.rs +++ b/lib/runtime-abi/src/vfs/mod.rs @@ -1 +1,2 @@ pub mod vfs; +pub mod vfs_header; diff --git a/lib/runtime-abi/src/vfs/vfs.rs b/lib/runtime-abi/src/vfs/vfs.rs index 273750d7c..a9867810c 100644 --- a/lib/runtime-abi/src/vfs/vfs.rs +++ b/lib/runtime-abi/src/vfs/vfs.rs @@ -1,3 +1,4 @@ +use crate::vfs::vfs_header::{header_from_bytes, ArchiveType, CompressionType}; use std::collections::BTreeMap; use std::io; use std::io::Read; @@ -19,6 +20,14 @@ impl Vfs { Vfs::from_tar_bytes(&decompressed_data[..]) } + pub fn from_compressed_bytes(compressed_data_slice: &[u8]) -> Result { + let data_bytes = &compressed_data_slice[4..]; + match header_from_bytes(compressed_data_slice)? { + (_, CompressionType::ZSTD, ArchiveType::TAR) => Vfs::from_tar_zstd_bytes(data_bytes), + (_, CompressionType::NONE, ArchiveType::TAR) => Vfs::from_tar_bytes(data_bytes), + } + } + /// Create a vfs from raw bytes in tar format pub fn from_tar_bytes(tar_bytes: Reader) -> Result { let mut ar = tar::Archive::new(tar_bytes); diff --git a/lib/runtime-abi/src/vfs/vfs_header.rs b/lib/runtime-abi/src/vfs/vfs_header.rs new file mode 100644 index 000000000..c90c658b0 --- /dev/null +++ b/lib/runtime-abi/src/vfs/vfs_header.rs @@ -0,0 +1,57 @@ +/// Represents the version of this header schema. +#[repr(u8)] +#[derive(Debug, PartialEq)] +pub enum HeaderVersion { + Version1 = 1, +} + +/// Represents the compression type of the file data. Only Zstd or no-compression is supported. +#[repr(u8)] +#[derive(Debug, PartialEq)] +pub enum CompressionType { + NONE = 0, + ZSTD = 1, +} + +/// Represents the type of archive. The only supported archive is the Tar format. +#[repr(u8)] +#[derive(Debug, PartialEq)] +pub enum ArchiveType { + TAR = 0, +} + +// extract the header data from bytes +pub fn header_from_bytes( + bytes: &[u8], +) -> Result<(HeaderVersion, CompressionType, ArchiveType), HeaderError> { + if let Some(bytes) = bytes.get(..4) { + let version = match bytes[0] { + 1 => HeaderVersion::Version1, + x => return Err(HeaderError::UnknownHeaderVersion(x)), + }; + let compression_type = match bytes[1] { + 0 => CompressionType::NONE, + 1 => CompressionType::ZSTD, + x => return Err(HeaderError::UnknownCompressionType(x)), + }; + let archive_type = match bytes[2] { + 0 => ArchiveType::TAR, + x => return Err(HeaderError::UnknownArchiveType(x)), + }; + Ok((version, compression_type, archive_type)) + } else { + Err(HeaderError::HeaderTooSmall) + } +} + +#[derive(Debug, Fail)] +pub enum HeaderError { + #[fail(display = "The version is not supported: \"{}\"", _0)] + UnknownHeaderVersion(u8), + #[fail(display = "The compression type is unknown: \"{}\"", _0)] + UnknownCompressionType(u8), + #[fail(display = "The archive type is unknown: \"{}\"", _0)] + UnknownArchiveType(u8), + #[fail(display = "The header is too small.")] + HeaderTooSmall, +}