| 
 | 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::clocks::wall_clock::Datetime;  | 
 | 20 | +use wasi::filesystem::types::Descriptor;  | 
 | 21 | +use wasi::filesystem::types::NewTimestamp;  | 
 | 22 | +use wasi::filesystem::types::{DescriptorFlags, ErrorCode, OpenFlags, PathFlags};  | 
 | 23 | +use wasi::filesystem::types::{DescriptorStat, DescriptorType};  | 
 | 24 | + | 
 | 25 | +fn check_timestamp(t: Datetime) {  | 
 | 26 | +    assert!(t.nanoseconds < 1_000_000_000);  | 
 | 27 | +}  | 
 | 28 | + | 
 | 29 | +fn check_stat(stat: &DescriptorStat, type_: DescriptorType) {  | 
 | 30 | +    assert_eq!(stat.type_, type_);  | 
 | 31 | +    // assert_eq!(stat.link_count, 0) ?  | 
 | 32 | +    // assert_eq!(stat.size, 0) ?  | 
 | 33 | +    if let Some(t) = stat.data_access_timestamp {  | 
 | 34 | +        check_timestamp(t)  | 
 | 35 | +    };  | 
 | 36 | +    if let Some(t) = stat.data_modification_timestamp {  | 
 | 37 | +        check_timestamp(t)  | 
 | 38 | +    };  | 
 | 39 | +    if let Some(t) = stat.status_change_timestamp {  | 
 | 40 | +        check_timestamp(t)  | 
 | 41 | +    }  | 
 | 42 | +}  | 
 | 43 | + | 
 | 44 | +async fn test_stat(dir: &Descriptor) {  | 
 | 45 | +    let afd = dir  | 
 | 46 | +        .open_at(  | 
 | 47 | +            PathFlags::empty(),  | 
 | 48 | +            "a.txt".to_string(),  | 
 | 49 | +            OpenFlags::empty(),  | 
 | 50 | +            DescriptorFlags::READ,  | 
 | 51 | +        )  | 
 | 52 | +        .await  | 
 | 53 | +        .unwrap();  | 
 | 54 | +    let bfd = dir  | 
 | 55 | +        .open_at(  | 
 | 56 | +            PathFlags::empty(),  | 
 | 57 | +            "b.txt".to_string(),  | 
 | 58 | +            OpenFlags::empty(),  | 
 | 59 | +            DescriptorFlags::READ,  | 
 | 60 | +        )  | 
 | 61 | +        .await  | 
 | 62 | +        .unwrap();  | 
 | 63 | +    let stat_with_flags = |flags: PathFlags, path: &str| dir.stat_at(flags, path.to_string());  | 
 | 64 | +    let stat = |path: &str| stat_with_flags(PathFlags::empty(), path);  | 
 | 65 | +    let stat_follow = |path: &str| stat_with_flags(PathFlags::SYMLINK_FOLLOW, path);  | 
 | 66 | + | 
 | 67 | +    // stat: async func() -> result<descriptor-stat, error-code>;  | 
 | 68 | +    check_stat(&dir.stat().await.unwrap(), DescriptorType::Directory);  | 
 | 69 | +    check_stat(&afd.stat().await.unwrap(), DescriptorType::RegularFile);  | 
 | 70 | +    check_stat(&bfd.stat().await.unwrap(), DescriptorType::RegularFile);  | 
 | 71 | + | 
 | 72 | +    // stat-at: async func(path-flags: path-flags, path: string) -> result<descriptor-stat, error-code>;  | 
 | 73 | +    assert_eq!(  | 
 | 74 | +        afd.stat_at(PathFlags::empty(), "z.txt".to_string()).await,  | 
 | 75 | +        Err(ErrorCode::NotDirectory)  | 
 | 76 | +    );  | 
 | 77 | +    assert_eq!(  | 
 | 78 | +        afd.stat_at(PathFlags::empty(), ".".to_string()).await,  | 
 | 79 | +        Err(ErrorCode::NotDirectory)  | 
 | 80 | +    );  | 
 | 81 | + | 
 | 82 | +    assert_eq!(stat("").await, Err(ErrorCode::NoEntry));  | 
 | 83 | +    assert_eq!(stat("..").await, Err(ErrorCode::NotPermitted));  | 
 | 84 | +    assert_eq!(stat_follow("parent").await, Err(ErrorCode::NotPermitted));  | 
 | 85 | +    assert_eq!(  | 
 | 86 | +        stat_follow("parent/fs-tests.dir").await,  | 
 | 87 | +        Err(ErrorCode::NotPermitted)  | 
 | 88 | +    );  | 
 | 89 | +    assert_eq!(stat(".").await, dir.stat().await);  | 
 | 90 | +    // FIXME: https://github.com/bytecodealliance/wasmtime/issues/11606  | 
 | 91 | +    // assert_eq!(stat_at("/").await,  | 
 | 92 | +    //            Err(ErrorCode::NotPermitted));  | 
 | 93 | +    assert_eq!(stat("/etc/passwd").await, Err(ErrorCode::NotPermitted));  | 
 | 94 | +    assert_eq!(stat("z.txt").await, Err(ErrorCode::NoEntry));  | 
 | 95 | + | 
 | 96 | +    // set-times-at: async func(path-flags: path-flags, path: string, data-access-timestamp: new-timestamp, data-modification-timestamp: new-timestamp) -> result<_, error-code>;  | 
 | 97 | +    // set-times: async func(data-access-timestamp: new-timestamp, data-modification-timestamp: new-timestamp) -> result<_, error-code>;  | 
 | 98 | +    let no_flags = PathFlags::empty();  | 
 | 99 | +    let follow_flag = PathFlags::SYMLINK_FOLLOW;  | 
 | 100 | +    let set_times_at = |flags, path: &str, atime, mtime| -> _ {  | 
 | 101 | +        dir.set_times_at(flags, path.to_string(),  | 
 | 102 | +                         NewTimestamp::Timestamp(atime),  | 
 | 103 | +                         NewTimestamp::Timestamp(mtime))  | 
 | 104 | +    };  | 
 | 105 | +    {  | 
 | 106 | +        let atime = Datetime {  | 
 | 107 | +            seconds: 42,  | 
 | 108 | +            nanoseconds: 0,  | 
 | 109 | +        };  | 
 | 110 | +        let mtime = Datetime {  | 
 | 111 | +            seconds: 69,  | 
 | 112 | +            nanoseconds: 0,  | 
 | 113 | +        };  | 
 | 114 | +        assert_eq!(set_times_at(no_flags, "z.txt", atime, mtime).await,  | 
 | 115 | +                   Err(ErrorCode::NoEntry));  | 
 | 116 | +        assert_eq!(set_times_at(follow_flag, "", atime, mtime).await,  | 
 | 117 | +                   Err(ErrorCode::NoEntry));  | 
 | 118 | +        assert_eq!(set_times_at(no_flags, "/", atime, mtime).await,  | 
 | 119 | +                   Err(ErrorCode::NotPermitted));  | 
 | 120 | +        assert_eq!(set_times_at(no_flags, "..", atime, mtime).await,  | 
 | 121 | +                   Err(ErrorCode::NotPermitted));  | 
 | 122 | +        assert_eq!(set_times_at(follow_flag, "parent", atime, mtime).await,  | 
 | 123 | +                   Err(ErrorCode::NotPermitted));  | 
 | 124 | +        assert_eq!(set_times_at(no_flags, "../foo", atime, mtime).await,  | 
 | 125 | +                   Err(ErrorCode::NotPermitted));  | 
 | 126 | +        assert_eq!(set_times_at(no_flags, "parent/foo", atime, mtime).await,  | 
 | 127 | +                   Err(ErrorCode::NotPermitted));  | 
 | 128 | +    }  | 
 | 129 | + | 
 | 130 | +    if let Some(atime) = afd.stat().await.unwrap().data_access_timestamp {  | 
 | 131 | +        let mtime = afd  | 
 | 132 | +            .stat()  | 
 | 133 | +            .await  | 
 | 134 | +            .unwrap()  | 
 | 135 | +            .data_modification_timestamp  | 
 | 136 | +            .unwrap();  | 
 | 137 | +        let new_atime = Datetime {  | 
 | 138 | +            seconds: 42,  | 
 | 139 | +            nanoseconds: 0,  | 
 | 140 | +        };  | 
 | 141 | +        let new_mtime = Datetime {  | 
 | 142 | +            seconds: 69,  | 
 | 143 | +            nanoseconds: 0,  | 
 | 144 | +        };  | 
 | 145 | +        assert_eq!(set_times_at(no_flags, "a.txt", new_atime, new_mtime).await,  | 
 | 146 | +                   Ok(()));  | 
 | 147 | +        assert_eq!(  | 
 | 148 | +            afd.stat().await.unwrap().data_access_timestamp,  | 
 | 149 | +            Some(new_atime)  | 
 | 150 | +        );  | 
 | 151 | +        assert_eq!(  | 
 | 152 | +            afd.stat().await.unwrap().data_modification_timestamp,  | 
 | 153 | +            Some(new_mtime)  | 
 | 154 | +        );  | 
 | 155 | +        assert_eq!(set_times_at(no_flags, "a.txt", atime, mtime).await,  | 
 | 156 | +                   Ok(()));  | 
 | 157 | +        assert_eq!(afd.stat().await.unwrap().data_access_timestamp, Some(atime));  | 
 | 158 | +        assert_eq!(  | 
 | 159 | +            afd.stat().await.unwrap().data_modification_timestamp,  | 
 | 160 | +            Some(mtime)  | 
 | 161 | +        );  | 
 | 162 | + | 
 | 163 | +        assert_eq!(  | 
 | 164 | +            afd.set_times(  | 
 | 165 | +                NewTimestamp::Timestamp(new_atime),  | 
 | 166 | +                NewTimestamp::Timestamp(new_mtime)  | 
 | 167 | +            )  | 
 | 168 | +            .await,  | 
 | 169 | +            Ok(())  | 
 | 170 | +        );  | 
 | 171 | +        assert_eq!(  | 
 | 172 | +            afd.stat().await.unwrap().data_access_timestamp,  | 
 | 173 | +            Some(new_atime)  | 
 | 174 | +        );  | 
 | 175 | +        assert_eq!(  | 
 | 176 | +            afd.stat().await.unwrap().data_modification_timestamp,  | 
 | 177 | +            Some(new_mtime)  | 
 | 178 | +        );  | 
 | 179 | +        assert_eq!(  | 
 | 180 | +            afd.set_times(  | 
 | 181 | +                NewTimestamp::Timestamp(atime),  | 
 | 182 | +                NewTimestamp::Timestamp(mtime)  | 
 | 183 | +            )  | 
 | 184 | +            .await,  | 
 | 185 | +            Ok(())  | 
 | 186 | +        );  | 
 | 187 | +        assert_eq!(afd.stat().await.unwrap().data_access_timestamp, Some(atime));  | 
 | 188 | +        assert_eq!(  | 
 | 189 | +            afd.stat().await.unwrap().data_modification_timestamp,  | 
 | 190 | +            Some(mtime)  | 
 | 191 | +        );  | 
 | 192 | +    }  | 
 | 193 | +}  | 
 | 194 | + | 
 | 195 | +struct Component;  | 
 | 196 | +export!(Component);  | 
 | 197 | +impl exports::wasi::cli::run::Guest for Component {  | 
 | 198 | +    async fn run() -> Result<(), ()> {  | 
 | 199 | +        match &wasi::filesystem::preopens::get_directories()[..] {  | 
 | 200 | +            [(dir, dirname)] if dirname == "fs-tests.dir" => {  | 
 | 201 | +                test_stat(dir).await;  | 
 | 202 | +            }  | 
 | 203 | +            [..] => {  | 
 | 204 | +                eprintln!("usage: run with one open dir named 'fs-tests.dir'");  | 
 | 205 | +                process::exit(1)  | 
 | 206 | +            }  | 
 | 207 | +        };  | 
 | 208 | +        Ok(())  | 
 | 209 | +    }  | 
 | 210 | +}  | 
 | 211 | + | 
 | 212 | +fn main() {  | 
 | 213 | +    unreachable!("main is a stub");  | 
 | 214 | +}  | 
0 commit comments