Skip to content

Commit 5583631

Browse files
committed
Merge branch 'js-api-wasi' into feat-vfs-unified-fd
2 parents f211130 + 2240fa9 commit 5583631

File tree

7 files changed

+69
-243
lines changed

7 files changed

+69
-243
lines changed

lib/c-api/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ wasmer-engine-universal = { version = "2.0.0", path = "../engine-universal", opt
3232
wasmer-engine-dylib = { version = "2.0.0", path = "../engine-dylib", optional = true }
3333
wasmer-engine-staticlib = { version = "2.0.0", path = "../engine-staticlib", optional = true }
3434
wasmer-middlewares = { version = "2.0.0", path = "../middlewares", optional = true }
35-
wasmer-wasi = { version = "2.0.0", path = "../wasi", default-features = false, features = ["host_fs"], optional = true }
35+
wasmer-wasi = { version = "2.0.0", path = "../wasi", optional = true }
3636
wasmer-types = { version = "2.0.0", path = "../types" }
3737
enumset = "1.0"
3838
cfg-if = "1.0"

lib/cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ wasmer-engine-universal = { version = "2.0.0", path = "../engine-universal", opt
3535
wasmer-engine-dylib = { version = "2.0.0", path = "../engine-dylib", optional = true }
3636
wasmer-engine-staticlib = { version = "2.0.0", path = "../engine-staticlib", optional = true }
3737
wasmer-vm = { version = "2.0.0", path = "../vm" }
38-
wasmer-wasi = { version = "2.0.0", path = "../wasi", default-features = false, features = ["host_fs"], optional = true }
38+
wasmer-wasi = { version = "2.0.0", path = "../wasi", optional = true }
3939
wasmer-wasi-experimental-io-devices = { version = "2.0.0", path = "../wasi-experimental-io-devices", optional = true }
4040
wasmer-wast = { version = "2.0.0", path = "../../tests/lib/wast", optional = true }
4141
wasmer-cache = { version = "2.0.0", path = "../cache", optional = true }

lib/vfs/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typetag = { version = "0.1", optional = true }
1414
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
1515

1616
[features]
17+
default = ["host_fs"]
1718
host_fs = ["libc"]
1819
mem_fs = []
1920
enable-serde = [

lib/vfs/src/host_fs.rs

+60-56
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ use crate::*;
33
use serde::{de, Deserialize, Serialize};
44
use std::convert::TryInto;
55
use std::fs;
6+
use std::io::{self, Read, Seek, Write};
67
use std::io::{Read, Seek, Write};
78
#[cfg(unix)]
89
use std::os::unix::io::{AsRawFd, RawFd};
910
#[cfg(windows)]
1011
use std::os::windows::io::{AsRawHandle, RawHandle};
1112
use std::path::{Path, PathBuf};
12-
use std::time::SystemTime;
13+
use std::time::{SystemTime, UNIX_EPOCH};
1314

1415
impl FileDescriptor {
1516
#[cfg(unix)]
@@ -69,17 +70,17 @@ pub struct HostFileSystem;
6970

7071
impl FileSystem for HostFileSystem {
7172
fn read_dir(&self, path: &Path) -> Result<ReadDir, FsError> {
72-
let read_dir = std::fs::read_dir(path)?;
73+
let read_dir = fs::read_dir(path)?;
7374
let data = read_dir
7475
.map(|entry| -> Result<DirEntry, _> {
7576
let entry = entry?;
7677
let metadata = entry.metadata()?;
7778
Ok(DirEntry {
7879
path: entry.path(),
79-
metadata: Ok(fs_metadata_to_metadata(metadata)?),
80+
metadata: Ok(metadata.try_into()?),
8081
})
8182
})
82-
.collect::<Result<Vec<DirEntry>, std::io::Error>>()
83+
.collect::<Result<Vec<DirEntry>, io::Error>>()
8384
.map_err::<FsError, _>(Into::into)?;
8485
Ok(ReadDir::new(data))
8586
}
@@ -103,58 +104,61 @@ impl FileSystem for HostFileSystem {
103104

104105
fn metadata(&self, path: &Path) -> Result<Metadata, FsError> {
105106
fs::metadata(path)
106-
.and_then(fs_metadata_to_metadata)
107+
.and_then(TryInto::try_into)
107108
.map_err(Into::into)
108109
}
109110
}
110111

111-
fn fs_metadata_to_metadata(metadata: fs::Metadata) -> Result<Metadata, std::io::Error> {
112-
use std::time::UNIX_EPOCH;
113-
let filetype = metadata.file_type();
114-
let (char_device, block_device, socket, fifo) = {
115-
#[cfg(unix)]
116-
{
117-
use std::os::unix::fs::FileTypeExt;
118-
(
119-
filetype.is_char_device(),
120-
filetype.is_block_device(),
121-
filetype.is_socket(),
122-
filetype.is_fifo(),
123-
)
124-
}
125-
#[cfg(not(unix))]
126-
{
127-
(false, false, false, false)
128-
}
129-
};
130-
131-
Ok(Metadata {
132-
ft: FileType {
133-
dir: filetype.is_dir(),
134-
file: filetype.is_file(),
135-
symlink: filetype.is_symlink(),
136-
char_device,
137-
block_device,
138-
socket,
139-
fifo,
140-
},
141-
accessed: metadata
142-
.accessed()?
143-
.duration_since(UNIX_EPOCH)
144-
.unwrap()
145-
.as_nanos() as u64,
146-
created: metadata
147-
.created()?
148-
.duration_since(UNIX_EPOCH)
149-
.unwrap()
150-
.as_nanos() as u64,
151-
modified: metadata
152-
.modified()?
153-
.duration_since(UNIX_EPOCH)
154-
.unwrap()
155-
.as_nanos() as u64,
156-
len: metadata.len(),
157-
})
112+
impl TryInto<Metadata> for fs::Metadata {
113+
type Error = io::Error;
114+
115+
fn try_into(self) -> Result<Metadata, Self::Error> {
116+
let filetype = self.file_type();
117+
let (char_device, block_device, socket, fifo) = {
118+
#[cfg(unix)]
119+
{
120+
use std::os::unix::fs::FileTypeExt;
121+
(
122+
filetype.is_char_device(),
123+
filetype.is_block_device(),
124+
filetype.is_socket(),
125+
filetype.is_fifo(),
126+
)
127+
}
128+
#[cfg(not(unix))]
129+
{
130+
(false, false, false, false)
131+
}
132+
};
133+
134+
Ok(Metadata {
135+
ft: FileType {
136+
dir: filetype.is_dir(),
137+
file: filetype.is_file(),
138+
symlink: filetype.is_symlink(),
139+
char_device,
140+
block_device,
141+
socket,
142+
fifo,
143+
},
144+
accessed: self
145+
.accessed()?
146+
.duration_since(UNIX_EPOCH)
147+
.unwrap()
148+
.as_nanos() as u64,
149+
created: self
150+
.created()?
151+
.duration_since(UNIX_EPOCH)
152+
.unwrap()
153+
.as_nanos() as u64,
154+
modified: self
155+
.modified()?
156+
.duration_since(UNIX_EPOCH)
157+
.unwrap()
158+
.as_nanos() as u64,
159+
len: self.len(),
160+
})
161+
}
158162
}
159163

160164
#[derive(Debug, Clone)]
@@ -170,7 +174,7 @@ impl FileOpener for HostFileOpener {
170174
let read = conf.read();
171175
let write = conf.write();
172176
let append = conf.append();
173-
let mut oo = std::fs::OpenOptions::new();
177+
let mut oo = fs::OpenOptions::new();
174178
oo.read(conf.read())
175179
.write(conf.write())
176180
.create_new(conf.create_new())
@@ -228,7 +232,7 @@ impl<'de> Deserialize<'de> for HostFile {
228232
let flags = seq
229233
.next_element()?
230234
.ok_or_else(|| de::Error::invalid_length(1, &self))?;
231-
let inner = std::fs::OpenOptions::new()
235+
let inner = fs::OpenOptions::new()
232236
.read(flags & HostFile::READ != 0)
233237
.write(flags & HostFile::WRITE != 0)
234238
.append(flags & HostFile::APPEND != 0)
@@ -265,7 +269,7 @@ impl<'de> Deserialize<'de> for HostFile {
265269
}
266270
let host_path = host_path.ok_or_else(|| de::Error::missing_field("host_path"))?;
267271
let flags = flags.ok_or_else(|| de::Error::missing_field("flags"))?;
268-
let inner = std::fs::OpenOptions::new()
272+
let inner = fs::OpenOptions::new()
269273
.read(flags & HostFile::READ != 0)
270274
.write(flags & HostFile::WRITE != 0)
271275
.append(flags & HostFile::APPEND != 0)
@@ -385,7 +389,7 @@ impl VirtualFile for HostFile {
385389
}
386390

387391
fn unlink(&mut self) -> Result<(), FsError> {
388-
std::fs::remove_file(&self.host_path).map_err(Into::into)
392+
fs::remove_file(&self.host_path).map_err(Into::into)
389393
}
390394
fn sync_to_disk(&self) -> Result<(), FsError> {
391395
self.inner.sync_all().map_err(Into::into)

lib/vfs/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ use std::path::{Path, PathBuf};
66
use thiserror::Error;
77
use tracing::debug;
88

9+
#[cfg(all(not(feature = "host_fs"), not(feature = "mem_fs")))]
10+
compile_error!("At least the `host_fs` or the `mem_fs` feature must be enabled. Please, pick one.");
11+
912
#[cfg(feature = "host_fs")]
1013
pub mod host_fs;
11-
//pub mod vfs_fs;
1214
#[cfg(feature = "mem_fs")]
1315
pub mod mem_fs;
1416

0 commit comments

Comments
 (0)