WASI & darwin: support basic file io based on libc

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
Takeshi Yoneda
2021-03-18 12:53:32 +09:00
committed by Ron Evans
parent 6d3c11627c
commit 1406453350
16 changed files with 390 additions and 84 deletions
+31 -6
View File
@@ -13,12 +13,15 @@ type sliceHeader struct {
}
func Close(fd int) (err error) {
return ENOSYS // TODO
if libc_close(fd) < 0 {
err = getErrno()
}
return
}
func Write(fd int, p []byte) (n int, err error) {
buf, count := splitSlice(p)
n = libc_write(int32(fd), buf, uint(count))
n = libc_write(fd, buf, uint(count))
if n < 0 {
err = getErrno()
}
@@ -26,15 +29,25 @@ func Write(fd int, p []byte) (n int, err error) {
}
func Read(fd int, p []byte) (n int, err error) {
return 0, ENOSYS // TODO
buf, count := splitSlice(p)
n = libc_read(fd, buf, uint(count))
if n < 0 {
err = getErrno()
}
return
}
func Seek(fd int, offset int64, whence int) (off int64, err error) {
return 0, ENOSYS // TODO
}
func Open(path string, mode int, perm uint32) (fd int, err error) {
return 0, ENOSYS // TODO
func Open(path string, flag int, mode uint32) (fd int, err error) {
data := append([]byte(path), 0)
fd = libc_open(&data[0], flag, mode)
if fd < 0 {
err = getErrno()
}
return
}
func Mkdir(path string, mode uint32) (err error) {
@@ -78,8 +91,20 @@ func splitSlice(p []byte) (buf *byte, len uintptr) {
// ssize_t write(int fd, const void *buf, size_t count)
//export write
func libc_write(fd int32, buf *byte, count uint) int
func libc_write(fd int, buf *byte, count uint) int
// char *getenv(const char *name);
//export getenv
func libc_getenv(name *byte) *byte
// ssize_t read(int fd, void *buf, size_t count);
//export read
func libc_read(fd int, buf *byte, count uint) int
// int open(const char *pathname, int flags, mode_t mode);
//export open
func libc_open(pathname *byte, flags int, mode uint32) int
// int close(int fd)
//export close
func libc_close(fd int) int