Skip to content

Commit 16b6270

Browse files
committed
fix(wasix): report /dev/null as a character device
1 parent a6e5902 commit 16b6270

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

lib/virtual-fs/src/mem_fs/file_opener.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,11 @@ impl FileSystem {
256256
file: Mutex::new(file),
257257
metadata: {
258258
let time = time();
259+
let is_char_device = path.starts_with("/dev");
259260
Metadata {
260261
ft: FileType {
261-
file: true,
262+
file: !is_char_device,
263+
char_device: is_char_device,
262264
..Default::default()
263265
},
264266
accessed: time,

lib/wasix/src/fs/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2434,11 +2434,18 @@ impl FileSystem for FallbackFileSystem {
24342434
}
24352435

24362436
pub fn virtual_file_type_to_wasi_file_type(file_type: virtual_fs::FileType) -> Filetype {
2437-
// TODO: handle other file types
24382437
if file_type.is_dir() {
24392438
Filetype::Directory
24402439
} else if file_type.is_file() {
24412440
Filetype::RegularFile
2441+
} else if file_type.is_char_device() {
2442+
Filetype::CharacterDevice
2443+
} else if file_type.is_block_device() {
2444+
Filetype::BlockDevice
2445+
} else if file_type.is_socket() {
2446+
Filetype::SocketStream
2447+
} else if file_type.is_fifo() {
2448+
Filetype::Unknown
24422449
} else if file_type.is_symlink() {
24432450
Filetype::SymbolicLink
24442451
} else {

tests/wasix/dev-null-stat/main.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <fcntl.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <sys/stat.h>
5+
#include <unistd.h>
6+
7+
static void fail(const char* msg) {
8+
perror(msg);
9+
exit(1);
10+
}
11+
12+
int main(void) {
13+
struct stat st;
14+
15+
if (stat("/dev/null", &st) != 0) {
16+
fail("stat /dev/null");
17+
}
18+
if (!S_ISCHR(st.st_mode)) {
19+
fprintf(stderr, "stat(/dev/null) mode %#o is not char device\n",
20+
st.st_mode);
21+
return 1;
22+
}
23+
24+
int fd = open("/dev/null", O_RDWR);
25+
if (fd < 0) {
26+
fail("open /dev/null");
27+
}
28+
if (fstat(fd, &st) != 0) {
29+
fail("fstat /dev/null");
30+
}
31+
if (!S_ISCHR(st.st_mode)) {
32+
fprintf(stderr, "fstat(/dev/null) mode %#o is not char device\n",
33+
st.st_mode);
34+
close(fd);
35+
return 1;
36+
}
37+
38+
close(fd);
39+
printf("0");
40+
return 0;
41+
}

tests/wasix/dev-null-stat/run.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
set -e
3+
4+
$WASMER_RUN ./main.wasm > output
5+
6+
printf "0" | diff -u output - 1>/dev/null

0 commit comments

Comments
 (0)