use the header

This commit is contained in:
Mackenzie Clark 2019-03-14 13:32:47 -07:00
parent 93b602d8ea
commit 8fc2a13828
6 changed files with 70 additions and 3 deletions

View File

@ -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,
};

View File

@ -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);
}

View File

@ -1 +1,2 @@
pub mod vfs;
pub mod vfs_header;

View File

@ -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<Self, failure::Error> {
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<Reader: Read>(tar_bytes: Reader) -> Result<Self, failure::Error> {
let mut ar = tar::Archive::new(tar_bytes);

View File

@ -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,
}