Skip to content

Commit

Permalink
syscall: libc_wasip2: mkdir/rmdir/rename/symlink/readlink/unlink
Browse files Browse the repository at this point in the history
  • Loading branch information
dgryski committed Feb 22, 2024
1 parent 401d1f1 commit 2279be1
Showing 1 changed file with 76 additions and 2 deletions.
78 changes: 76 additions & 2 deletions src/syscall/libc_wasip2.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,27 +420,76 @@ func chmod(pathname *byte, mode uint32) int32 {
//
//go:export mkdir
func mkdir(pathname *byte, mode uint32) int32 {
path := goString(pathname)
dir, relPath := findPreopenForPath(path)

result := dir.CreateDirectoryAt(relPath)
if err := result.Err(); err != nil {
libcErrno = uintptr(errorCodeToErrno(*err))
return -1
}

return 0
}

// int rmdir(const char *pathname);
//
//go:export rmdir
func rmdir(pathname *byte) int32 {
path := goString(pathname)
dir, relPath := findPreopenForPath(path)

result := dir.RemoveDirectoryAt(relPath)
if err := result.Err(); err != nil {
libcErrno = uintptr(errorCodeToErrno(*err))
return -1
}

return 0
}

// int rename(const char *from, *to);
//
//go:export rename
func rename(from, to *byte) int32 {
fromPath := goString(from)
fromDir, fromRelPath := findPreopenForPath(fromPath)

toPath := goString(to)
toDir, toRelPath := findPreopenForPath(toPath)

result := fromDir.RenameAt(fromRelPath, toDir, toRelPath)
if err := result.Err(); err != nil {
libcErrno = uintptr(errorCodeToErrno(*err))
return -1
}

return 0
}

// int symlink(const char *from, *to);
//
//go:export symlink
func symlink(from, to *byte) int32 {
fromPath := goString(from)
fromDir, fromRelPath := findPreopenForPath(fromPath)

toPath := goString(to)
toDir, toRelPath := findPreopenForPath(toPath)

if fromDir != toDir {
libcErrno = uintptr(EACCES)
return -1
}

// TODO(dgryski): check fromDir == toDir?

result := fromDir.SymlinkAt(fromRelPath, toRelPath)
if err := result.Err(); err != nil {
libcErrno = uintptr(errorCodeToErrno(*err))
return -1
}

return 0
}

Expand All @@ -455,14 +504,39 @@ func fsync(fd int32) int32 {
// ssize_t readlink(const char *path, void *buf, size_t count);
//
//go:export readlink
func readlink(path *byte, buf *byte, count uint) int {
return 0
func readlink(pathname *byte, buf *byte, count uint) int {
path := goString(pathname)
dir, relPath := findPreopenForPath(path)

result := dir.ReadLinkAt(relPath)
if err := result.Err(); err != nil {
libcErrno = uintptr(errorCodeToErrno(*err))
return -1
}

s := *result.OK()
size := uintptr(count)
if size > uintptr(len(s)) {
size = uintptr(len(s))
}

memcpy(unsafe.Pointer(buf), unsafe.Pointer(unsafe.StringData(s)), size)
return int(size)
}

// int unlink(const char *pathname);
//
//go:export unlink
func unlink(pathname *byte) int32 {
path := goString(pathname)
dir, relPath := findPreopenForPath(path)

result := dir.UnlinkFileAt(relPath)
if err := result.Err(); err != nil {
libcErrno = uintptr(errorCodeToErrno(*err))
return -1
}

return 0
}

Expand Down

0 comments on commit 2279be1

Please sign in to comment.