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

Commit 3cfd5d0

Browse files
committed
Implement path_create_directory, path_filestat_get, path_link.
1 parent f7d095e commit 3cfd5d0

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

src/lib.rs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,16 @@ pub unsafe extern "C" fn path_create_directory(
686686
path_ptr: *const u8,
687687
path_len: usize,
688688
) -> Errno {
689-
unreachable()
689+
let path = slice::from_raw_parts(path_ptr, path_len);
690+
691+
match Descriptor::get(fd) {
692+
Descriptor::File(file) => match wasi_filesystem::create_directory_at(file.fd, path) {
693+
Ok(()) => ERRNO_SUCCESS,
694+
Err(err) => errno_from_wasi_filesystem(err),
695+
},
696+
Descriptor::Closed => ERRNO_BADF,
697+
Descriptor::Log => ERRNO_NOTDIR,
698+
}
690699
}
691700

692701
/// Return the attributes of a file or directory.
@@ -699,7 +708,42 @@ pub unsafe extern "C" fn path_filestat_get(
699708
path_len: usize,
700709
buf: *mut Filestat,
701710
) -> Errno {
702-
unreachable()
711+
let path = slice::from_raw_parts(path_ptr, path_len);
712+
let at_flags = at_flags_from_lookupflags(flags);
713+
714+
match Descriptor::get(fd) {
715+
Descriptor::File(file) => match wasi_filesystem::stat_at(file.fd, at_flags, path) {
716+
Ok(stat) => {
717+
let filetype = match stat.type_ {
718+
wasi_filesystem::Type::Unknown => FILETYPE_UNKNOWN,
719+
wasi_filesystem::Type::Directory => FILETYPE_DIRECTORY,
720+
wasi_filesystem::Type::BlockDevice => FILETYPE_BLOCK_DEVICE,
721+
wasi_filesystem::Type::RegularFile => FILETYPE_REGULAR_FILE,
722+
// TODO: Add a way to disginguish between FILETYPE_SOCKET_STREAM and
723+
// FILETYPE_SOCKET_DGRAM.
724+
wasi_filesystem::Type::Socket => unreachable(),
725+
wasi_filesystem::Type::SymbolicLink => FILETYPE_SYMBOLIC_LINK,
726+
wasi_filesystem::Type::CharacterDevice => FILETYPE_CHARACTER_DEVICE,
727+
// preview1 never had a FIFO code.
728+
wasi_filesystem::Type::Fifo => FILETYPE_UNKNOWN,
729+
};
730+
*buf = Filestat {
731+
dev: stat.dev,
732+
ino: stat.ino,
733+
filetype,
734+
nlink: stat.nlink,
735+
size: stat.size,
736+
atim: stat.atim,
737+
mtim: stat.mtim,
738+
ctim: stat.ctim,
739+
};
740+
ERRNO_SUCCESS
741+
}
742+
Err(err) => errno_from_wasi_filesystem(err),
743+
},
744+
Descriptor::Closed => ERRNO_BADF,
745+
Descriptor::Log => ERRNO_NOTDIR,
746+
}
703747
}
704748

705749
/// Adjust the timestamps of a file or directory.
@@ -741,8 +785,8 @@ pub unsafe extern "C" fn path_filestat_set_times(
741785
Err(err) => errno_from_wasi_filesystem(err),
742786
}
743787
}
744-
Descriptor::Log => ERRNO_SPIPE,
745788
Descriptor::Closed => ERRNO_BADF,
789+
Descriptor::Log => ERRNO_NOTDIR,
746790
}
747791
}
748792

@@ -758,7 +802,20 @@ pub unsafe extern "C" fn path_link(
758802
new_path_ptr: *const u8,
759803
new_path_len: usize,
760804
) -> Errno {
761-
unreachable()
805+
let old_path = slice::from_raw_parts(old_path_ptr, old_path_len);
806+
let new_path = slice::from_raw_parts(new_path_ptr, new_path_len);
807+
let at_flags = at_flags_from_lookupflags(old_flags);
808+
809+
match (Descriptor::get(old_fd), Descriptor::get(new_fd)) {
810+
(Descriptor::File(old_file), Descriptor::File(new_file)) => {
811+
match wasi_filesystem::link_at(old_file.fd, at_flags, old_path, new_file.fd, new_path) {
812+
Ok(()) => ERRNO_SUCCESS,
813+
Err(err) => errno_from_wasi_filesystem(err),
814+
}
815+
}
816+
(_, Descriptor::Closed) | (Descriptor::Closed, _) => ERRNO_BADF,
817+
_ => ERRNO_NOTDIR,
818+
}
762819
}
763820

764821
/// Open a file or directory.
@@ -879,8 +936,8 @@ pub unsafe extern "C" fn path_remove_directory(
879936
Ok(()) => ERRNO_SUCCESS,
880937
Err(err) => errno_from_wasi_filesystem(err),
881938
},
882-
Descriptor::Log => ERRNO_SPIPE,
883939
Descriptor::Closed => ERRNO_BADF,
940+
Descriptor::Log => ERRNO_NOTDIR,
884941
}
885942
}
886943

0 commit comments

Comments
 (0)