Skip to content

Commit 38333f7

Browse files
authored
Merge pull request #136 from wingo/filesystem-set-size
wasi:[email protected]: Add tests for set-size
2 parents 1ff3c4f + 171a015 commit 38333f7

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dirs": ["fs-tests.dir"]
3+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
use std::process;
2+
extern crate wit_bindgen;
3+
4+
wit_bindgen::generate!({
5+
inline: r"
6+
package test:test;
7+
8+
world test {
9+
include wasi:filesystem/[email protected];
10+
include wasi:cli/[email protected];
11+
}
12+
",
13+
additional_derives: [PartialEq, Eq, Hash, Clone],
14+
// Work around https://github.com/bytecodealliance/wasm-tools/issues/2285.
15+
features:["clocks-timezone"],
16+
generate_all
17+
});
18+
19+
use wasi::filesystem::types::Descriptor;
20+
use wasi::filesystem::types::{DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
21+
22+
async fn test_set_size(dir: &Descriptor) {
23+
// set-size: async func(size: filesize) -> result<_, error-code>;
24+
let open = |path: &str, oflags: OpenFlags, fdflags: DescriptorFlags| -> _ {
25+
dir.open_at(PathFlags::empty(), path.to_string(), oflags, fdflags)
26+
};
27+
let open_r = |path: &str| -> _ { open(path, OpenFlags::empty(), DescriptorFlags::READ) };
28+
let open_w = |path: &str| -> _ {
29+
open(
30+
path,
31+
OpenFlags::empty(),
32+
DescriptorFlags::READ | DescriptorFlags::WRITE,
33+
)
34+
};
35+
let creat = |path: &str| -> _ {
36+
open(
37+
path,
38+
OpenFlags::CREATE | OpenFlags::EXCLUSIVE,
39+
DescriptorFlags::READ | DescriptorFlags::WRITE,
40+
)
41+
};
42+
let trunc = |path: &str| -> _ {
43+
open(
44+
path,
45+
OpenFlags::TRUNCATE,
46+
DescriptorFlags::READ | DescriptorFlags::WRITE,
47+
)
48+
};
49+
let rm = |path: &str| dir.unlink_file_at(path.to_string());
50+
51+
let c = creat("c.cleanup").await.unwrap();
52+
assert_eq!(c.stat().await.unwrap().size, 0);
53+
c.set_size(42).await.unwrap();
54+
// Setting size is visible immediately.
55+
assert_eq!(c.stat().await.unwrap().size, 42);
56+
57+
let c = open_w("c.cleanup").await.unwrap();
58+
let r = open_r("c.cleanup").await.unwrap();
59+
assert_eq!(c.stat().await.unwrap().size, 42);
60+
assert_eq!(r.stat().await.unwrap().size, 42);
61+
c.set_size(69).await.unwrap();
62+
assert_eq!(c.stat().await.unwrap().size, 69);
63+
assert_eq!(r.stat().await.unwrap().size, 69);
64+
65+
let c = trunc("c.cleanup").await.unwrap();
66+
assert_eq!(c.stat().await.unwrap().size, 0);
67+
assert_eq!(r.stat().await.unwrap().size, 0);
68+
69+
// https://github.com/WebAssembly/WASI/issues/712
70+
match r.set_size(100).await {
71+
Ok(()) => {
72+
panic!("set-size succeeded on read-only descriptor");
73+
}
74+
Err(ErrorCode::Invalid | ErrorCode::BadDescriptor | ErrorCode::Access) => {}
75+
Err(err) => {
76+
panic!("unexpected err: {}", err)
77+
}
78+
};
79+
80+
// https://github.com/WebAssembly/WASI/issues/712
81+
match c.set_size(u64::MAX).await {
82+
Ok(()) => {
83+
panic!("set-size(-1) succeeded");
84+
}
85+
Err(ErrorCode::Invalid | ErrorCode::FileTooLarge) => {}
86+
Err(err) => {
87+
panic!("unexpected err: {}", err)
88+
}
89+
};
90+
91+
match rm("c.cleanup").await {
92+
Ok(()) => {
93+
// We still have `c` and `r` open, which refer to the file,
94+
// but we can still stat our descriptors, call `set-size` on
95+
// it, and so on.
96+
assert_eq!(c.stat().await.unwrap().size, 0);
97+
c.set_size(42).await.unwrap();
98+
assert_eq!(c.stat().await.unwrap().size, 42);
99+
assert_eq!(r.stat().await.unwrap().size, 42);
100+
}
101+
Err(ErrorCode::Busy) => {
102+
// Otherwise if we're on Windows we are unable to remove the
103+
// file while descriptors are open.
104+
}
105+
Err(err) => {
106+
panic!("unexpected err: {}", err)
107+
}
108+
}
109+
}
110+
111+
struct Component;
112+
export!(Component);
113+
impl exports::wasi::cli::run::Guest for Component {
114+
async fn run() -> Result<(), ()> {
115+
match &wasi::filesystem::preopens::get_directories()[..] {
116+
[(dir, dirname)] if dirname == "fs-tests.dir" => {
117+
test_set_size(dir).await;
118+
}
119+
[..] => {
120+
eprintln!("usage: run with one open dir named 'fs-tests.dir'");
121+
process::exit(1)
122+
}
123+
};
124+
Ok(())
125+
}
126+
}
127+
128+
fn main() {
129+
unreachable!("main is a stub");
130+
}

0 commit comments

Comments
 (0)