|
| 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/imports@0.3.0-rc-2025-09-16; |
| 10 | + include wasi:cli/command@0.3.0-rc-2025-09-16; |
| 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::{DescriptorFlags, ErrorCode, OpenFlags, PathFlags}; |
| 20 | + |
| 21 | +async fn test_symlink() { |
| 22 | + match &wasi::filesystem::preopens::get_directories()[..] { |
| 23 | + [(dir, _)] => { |
| 24 | + dir.create_directory_at("child.cleanup".to_string()) |
| 25 | + .await |
| 26 | + .unwrap(); |
| 27 | + let child = dir |
| 28 | + .open_at( |
| 29 | + PathFlags::empty(), |
| 30 | + "child.cleanup".to_string(), |
| 31 | + OpenFlags::DIRECTORY, |
| 32 | + DescriptorFlags::MUTATE_DIRECTORY, |
| 33 | + ) |
| 34 | + .await |
| 35 | + .unwrap(); |
| 36 | + |
| 37 | + drop( |
| 38 | + dir.open_at( |
| 39 | + PathFlags::empty(), |
| 40 | + "x.cleanup".to_string(), |
| 41 | + OpenFlags::CREATE | OpenFlags::EXCLUSIVE, |
| 42 | + DescriptorFlags::READ, |
| 43 | + ) |
| 44 | + .await |
| 45 | + .unwrap(), |
| 46 | + ); |
| 47 | + |
| 48 | + drop( |
| 49 | + dir.open_at( |
| 50 | + PathFlags::empty(), |
| 51 | + "x.cleanup".to_string(), |
| 52 | + OpenFlags::empty(), |
| 53 | + DescriptorFlags::READ, |
| 54 | + ) |
| 55 | + .await |
| 56 | + .unwrap(), |
| 57 | + ); |
| 58 | + |
| 59 | + // Possibly revisit if |
| 60 | + // https://github.com/WebAssembly/WASI/issues/856 ever |
| 61 | + // changes things. |
| 62 | + assert_eq!( |
| 63 | + child |
| 64 | + .open_at( |
| 65 | + PathFlags::empty(), |
| 66 | + "../x.cleanup".to_string(), |
| 67 | + OpenFlags::empty(), |
| 68 | + DescriptorFlags::READ |
| 69 | + ) |
| 70 | + .await |
| 71 | + .unwrap_err(), |
| 72 | + ErrorCode::NotPermitted |
| 73 | + ); |
| 74 | + |
| 75 | + drop(child); |
| 76 | + dir.remove_directory_at("child.cleanup".to_string()) |
| 77 | + .await |
| 78 | + .unwrap(); |
| 79 | + dir.unlink_file_at("x.cleanup".to_string()).await.unwrap(); |
| 80 | + } |
| 81 | + [..] => { |
| 82 | + eprintln!("usage: run with one open dir"); |
| 83 | + process::exit(1) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +struct Component; |
| 89 | +export!(Component); |
| 90 | +impl exports::wasi::cli::run::Guest for Component { |
| 91 | + async fn run() -> Result<(), ()> { |
| 92 | + test_symlink().await; |
| 93 | + Ok(()) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn main() { |
| 98 | + unreachable!("main is a stub"); |
| 99 | +} |
0 commit comments