Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 37ac3ad

Browse files
committed
Implement path_rename, path_symlink, and path_unlink_file.
1 parent 3cfd5d0 commit 37ac3ad

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

src/lib.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -945,14 +945,26 @@ pub unsafe extern "C" fn path_remove_directory(
945945
/// Note: This is similar to `renameat` in POSIX.
946946
#[no_mangle]
947947
pub unsafe extern "C" fn path_rename(
948-
fd: Fd,
948+
old_fd: Fd,
949949
old_path_ptr: *const u8,
950950
old_path_len: usize,
951951
new_fd: Fd,
952952
new_path_ptr: *const u8,
953953
new_path_len: usize,
954954
) -> Errno {
955-
unreachable()
955+
let old_path = slice::from_raw_parts(old_path_ptr, old_path_len);
956+
let new_path = slice::from_raw_parts(new_path_ptr, new_path_len);
957+
958+
match (Descriptor::get(old_fd), Descriptor::get(new_fd)) {
959+
(Descriptor::File(old_file), Descriptor::File(new_file)) => {
960+
match wasi_filesystem::rename_at(old_file.fd, old_path, new_file.fd, new_path) {
961+
Ok(()) => ERRNO_SUCCESS,
962+
Err(err) => errno_from_wasi_filesystem(err),
963+
}
964+
}
965+
(_, Descriptor::Closed) | (Descriptor::Closed, _) => ERRNO_BADF,
966+
_ => ERRNO_NOTDIR,
967+
}
956968
}
957969

958970
/// Create a symbolic link.
@@ -965,15 +977,34 @@ pub unsafe extern "C" fn path_symlink(
965977
new_path_ptr: *const u8,
966978
new_path_len: usize,
967979
) -> Errno {
968-
unreachable()
980+
let old_path = slice::from_raw_parts(old_path_ptr, old_path_len);
981+
let new_path = slice::from_raw_parts(new_path_ptr, new_path_len);
982+
983+
match Descriptor::get(fd) {
984+
Descriptor::File(file) => match wasi_filesystem::symlink_at(file.fd, old_path, new_path) {
985+
Ok(()) => ERRNO_SUCCESS,
986+
Err(err) => errno_from_wasi_filesystem(err),
987+
},
988+
Descriptor::Closed => ERRNO_BADF,
989+
_ => ERRNO_NOTDIR,
990+
}
969991
}
970992

971993
/// Unlink a file.
972994
/// Return `errno::isdir` if the path refers to a directory.
973995
/// Note: This is similar to `unlinkat(fd, path, 0)` in POSIX.
974996
#[no_mangle]
975997
pub unsafe extern "C" fn path_unlink_file(fd: Fd, path_ptr: *const u8, path_len: usize) -> Errno {
976-
unreachable()
998+
let path = slice::from_raw_parts(path_ptr, path_len);
999+
1000+
match Descriptor::get(fd) {
1001+
Descriptor::File(file) => match wasi_filesystem::unlink_file_at(file.fd, path) {
1002+
Ok(()) => ERRNO_SUCCESS,
1003+
Err(err) => errno_from_wasi_filesystem(err),
1004+
},
1005+
Descriptor::Closed => ERRNO_BADF,
1006+
_ => ERRNO_NOTDIR,
1007+
}
9771008
}
9781009

9791010
/// Concurrently poll for the occurrence of a set of events.

0 commit comments

Comments
 (0)