Repository: wasmerio/wasmer (language: rust)

Issue:
WASI `path_create_directory` recusively creates directories
### Describe the bug

`path_create_directory` on Wasmer recursively creates directories if a directory component in path does not exist. It should return something like `ENOENT`.

```sh
wasmer -vV; rustc -vV
wasmer 4.4.0 (8b97cfe 2024-10-18)
binary: wasmer-cli
commit-hash: 8b97cfe3992ae3c2f0002f9955fcb23057f666a9
commit-date: 2024-10-18
host: x86_64-unknown-linux-gnu
compiler: singlepass,cranelift
c_api backend: 
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7
```


### Steps to reproduce

This code snippet, when compiled with `wasi` crate v0.11, should pass when run. Other runtimes like Wasmtime, WasmEdge, Wazero, and WAMR do pass. Wasmer however, fails, creating the directory in the virtual filesystem.

```rust
fn main() {
    unsafe {
        assert_eq!(
            wasi::path_create_directory(5, "not-exist/dir").unwrap_err(),
            wasi::ERRNO_NOENT
        );
    }
}
```




Discussion / hints:

Fix patch:
diff --git a/lib/wasix/src/syscalls/wasi/path_create_directory.rs b/lib/wasix/src/syscalls/wasi/path_create_directory.rs
index 99944f7992c..fe07a65b40a 100644
--- a/lib/wasix/src/syscalls/wasi/path_create_directory.rs
+++ b/lib/wasix/src/syscalls/wasi/path_create_directory.rs
@@ -82,7 +82,7 @@ pub(crate) fn path_create_directory_internal(
 
     let mut cur_dir_inode = working_dir.inode;
     let mut created_directory = false;
-    for comp in &path_vec {
+    for (comp_idx, comp) in path_vec.iter().enumerate() {
         let processing_cur_dir_inode = cur_dir_inode.clone();
         let mut guard = processing_cur_dir_inode.write();
         match guard.deref_mut() {
@@ -122,6 +122,10 @@ pub(crate) fn path_create_directory_internal(
                             return Err(Errno::Notdir);
                         }
                     } else {
+                        if comp_idx != path_vec.len() - 1 {
+                            return Err(Errno::Noent);
+                        }
+
                         created_directory = true;
                         state.fs_create_dir(&adjusted_path)?;
                     }


Test patch:
diff --git a/tests/wasi-fyi/fs_create_dir-non_existent.rs b/tests/wasi-fyi/fs_create_dir-non_existent.rs
new file mode 100644
index 00000000000..278c96909a6
--- /dev/null
+++ b/tests/wasi-fyi/fs_create_dir-non_existent.rs
@@ -0,0 +1,5 @@
+use std::fs;
+
+fn main() {
+    assert!(fs::create_dir("/fyi/fs_create_dir-non_existent.dir/not-exist/new-directory").is_err());
+}
diff --git a/tests/wasi-fyi/test_fs/fyi/fs_create_dir-non_existent.rs/.ignore b/tests/wasi-fyi/test_fs/fyi/fs_create_dir-non_existent.rs/.ignore
new file mode 100644
index 00000000000..e69de29bb2d