Skip to content

Commit 2279be1

Browse files
committed
syscall: libc_wasip2: mkdir/rmdir/rename/symlink/readlink/unlink
1 parent 401d1f1 commit 2279be1

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

src/syscall/libc_wasip2.go

+76-2
Original file line numberDiff line numberDiff line change
@@ -420,27 +420,76 @@ func chmod(pathname *byte, mode uint32) int32 {
420420
//
421421
//go:export mkdir
422422
func mkdir(pathname *byte, mode uint32) int32 {
423+
path := goString(pathname)
424+
dir, relPath := findPreopenForPath(path)
425+
426+
result := dir.CreateDirectoryAt(relPath)
427+
if err := result.Err(); err != nil {
428+
libcErrno = uintptr(errorCodeToErrno(*err))
429+
return -1
430+
}
431+
423432
return 0
424433
}
425434

426435
// int rmdir(const char *pathname);
427436
//
428437
//go:export rmdir
429438
func rmdir(pathname *byte) int32 {
439+
path := goString(pathname)
440+
dir, relPath := findPreopenForPath(path)
441+
442+
result := dir.RemoveDirectoryAt(relPath)
443+
if err := result.Err(); err != nil {
444+
libcErrno = uintptr(errorCodeToErrno(*err))
445+
return -1
446+
}
447+
430448
return 0
431449
}
432450

433451
// int rename(const char *from, *to);
434452
//
435453
//go:export rename
436454
func rename(from, to *byte) int32 {
455+
fromPath := goString(from)
456+
fromDir, fromRelPath := findPreopenForPath(fromPath)
457+
458+
toPath := goString(to)
459+
toDir, toRelPath := findPreopenForPath(toPath)
460+
461+
result := fromDir.RenameAt(fromRelPath, toDir, toRelPath)
462+
if err := result.Err(); err != nil {
463+
libcErrno = uintptr(errorCodeToErrno(*err))
464+
return -1
465+
}
466+
437467
return 0
438468
}
439469

440470
// int symlink(const char *from, *to);
441471
//
442472
//go:export symlink
443473
func symlink(from, to *byte) int32 {
474+
fromPath := goString(from)
475+
fromDir, fromRelPath := findPreopenForPath(fromPath)
476+
477+
toPath := goString(to)
478+
toDir, toRelPath := findPreopenForPath(toPath)
479+
480+
if fromDir != toDir {
481+
libcErrno = uintptr(EACCES)
482+
return -1
483+
}
484+
485+
// TODO(dgryski): check fromDir == toDir?
486+
487+
result := fromDir.SymlinkAt(fromRelPath, toRelPath)
488+
if err := result.Err(); err != nil {
489+
libcErrno = uintptr(errorCodeToErrno(*err))
490+
return -1
491+
}
492+
444493
return 0
445494
}
446495

@@ -455,14 +504,39 @@ func fsync(fd int32) int32 {
455504
// ssize_t readlink(const char *path, void *buf, size_t count);
456505
//
457506
//go:export readlink
458-
func readlink(path *byte, buf *byte, count uint) int {
459-
return 0
507+
func readlink(pathname *byte, buf *byte, count uint) int {
508+
path := goString(pathname)
509+
dir, relPath := findPreopenForPath(path)
510+
511+
result := dir.ReadLinkAt(relPath)
512+
if err := result.Err(); err != nil {
513+
libcErrno = uintptr(errorCodeToErrno(*err))
514+
return -1
515+
}
516+
517+
s := *result.OK()
518+
size := uintptr(count)
519+
if size > uintptr(len(s)) {
520+
size = uintptr(len(s))
521+
}
522+
523+
memcpy(unsafe.Pointer(buf), unsafe.Pointer(unsafe.StringData(s)), size)
524+
return int(size)
460525
}
461526

462527
// int unlink(const char *pathname);
463528
//
464529
//go:export unlink
465530
func unlink(pathname *byte) int32 {
531+
path := goString(pathname)
532+
dir, relPath := findPreopenForPath(path)
533+
534+
result := dir.UnlinkFileAt(relPath)
535+
if err := result.Err(); err != nil {
536+
libcErrno = uintptr(errorCodeToErrno(*err))
537+
return -1
538+
}
539+
466540
return 0
467541
}
468542

0 commit comments

Comments
 (0)