at_wasip1.mx raw

   1  // Copyright 2020 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  //go:build wasip1
   6  
   7  package unix
   8  
   9  import (
  10  	"syscall"
  11  	"unsafe"
  12  )
  13  
  14  // The values of these constants are not part of the WASI API.
  15  const (
  16  	// UTIME_OMIT is the sentinel value to indicate that a time value should not
  17  	// be changed. It is useful for example to indicate for example with UtimesNano
  18  	// to avoid changing AccessTime or ModifiedTime.
  19  	// Its value must match syscall/fs_wasip1.go
  20  	UTIME_OMIT = -0x2
  21  
  22  	AT_REMOVEDIR        = 0x200
  23  	AT_SYMLINK_NOFOLLOW = 0x100
  24  )
  25  
  26  func Unlinkat(dirfd int, path string, flags int) error {
  27  	if flags&AT_REMOVEDIR == 0 {
  28  		return errnoErr(path_unlink_file(
  29  			int32(dirfd),
  30  			unsafe.StringData(path),
  31  			size(len(path)),
  32  		))
  33  	} else {
  34  		return errnoErr(path_remove_directory(
  35  			int32(dirfd),
  36  			unsafe.StringData(path),
  37  			size(len(path)),
  38  		))
  39  	}
  40  }
  41  
  42  //go:wasmimport wasi_snapshot_preview1 path_unlink_file
  43  //go:noescape
  44  func path_unlink_file(fd int32, path *byte, pathLen size) syscall.Errno
  45  
  46  //go:wasmimport wasi_snapshot_preview1 path_remove_directory
  47  //go:noescape
  48  func path_remove_directory(fd int32, path *byte, pathLen size) syscall.Errno
  49  
  50  func Openat(dirfd int, path string, flags int, perm uint32) (int, error) {
  51  	return syscall.Openat(dirfd, path, flags, perm)
  52  }
  53  
  54  func Fstatat(dirfd int, path string, stat *syscall.Stat_t, flags int) error {
  55  	var filestatFlags uint32
  56  	if flags&AT_SYMLINK_NOFOLLOW == 0 {
  57  		filestatFlags |= syscall.LOOKUP_SYMLINK_FOLLOW
  58  	}
  59  	return errnoErr(path_filestat_get(
  60  		int32(dirfd),
  61  		uint32(filestatFlags),
  62  		unsafe.StringData(path),
  63  		size(len(path)),
  64  		unsafe.Pointer(stat),
  65  	))
  66  }
  67  
  68  //go:wasmimport wasi_snapshot_preview1 path_filestat_get
  69  //go:noescape
  70  func path_filestat_get(fd int32, flags uint32, path *byte, pathLen size, buf unsafe.Pointer) syscall.Errno
  71  
  72  func Readlinkat(dirfd int, path string, buf []byte) (int, error) {
  73  	var nwritten size
  74  	errno := path_readlink(
  75  		int32(dirfd),
  76  		unsafe.StringData(path),
  77  		size(len(path)),
  78  		&buf[0],
  79  		size(len(buf)),
  80  		&nwritten)
  81  	return int(nwritten), errnoErr(errno)
  82  
  83  }
  84  
  85  type (
  86  	size = uint32
  87  )
  88  
  89  //go:wasmimport wasi_snapshot_preview1 path_readlink
  90  //go:noescape
  91  func path_readlink(fd int32, path *byte, pathLen size, buf *byte, bufLen size, nwritten *size) syscall.Errno
  92  
  93  func Mkdirat(dirfd int, path string, mode uint32) error {
  94  	if path == "" {
  95  		return syscall.EINVAL
  96  	}
  97  	return errnoErr(path_create_directory(
  98  		int32(dirfd),
  99  		unsafe.StringData(path),
 100  		size(len(path)),
 101  	))
 102  }
 103  
 104  //go:wasmimport wasi_snapshot_preview1 path_create_directory
 105  //go:noescape
 106  func path_create_directory(fd int32, path *byte, pathLen size) syscall.Errno
 107  
 108  func Fchmodat(dirfd int, path string, mode uint32, flags int) error {
 109  	// WASI preview 1 doesn't support changing file modes.
 110  	return syscall.ENOSYS
 111  }
 112  
 113  func Fchownat(dirfd int, path string, uid, gid int, flags int) error {
 114  	// WASI preview 1 doesn't support changing file ownership.
 115  	return syscall.ENOSYS
 116  }
 117  
 118  func Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) error {
 119  	if oldpath == "" || newpath == "" {
 120  		return syscall.EINVAL
 121  	}
 122  	return errnoErr(path_rename(
 123  		int32(olddirfd),
 124  		unsafe.StringData(oldpath),
 125  		size(len(oldpath)),
 126  		int32(newdirfd),
 127  		unsafe.StringData(newpath),
 128  		size(len(newpath)),
 129  	))
 130  }
 131  
 132  //go:wasmimport wasi_snapshot_preview1 path_rename
 133  //go:noescape
 134  func path_rename(oldFd int32, oldPath *byte, oldPathLen size, newFd int32, newPath *byte, newPathLen size) syscall.Errno
 135  
 136  func Linkat(olddirfd int, oldpath string, newdirfd int, newpath string, flag int) error {
 137  	if oldpath == "" || newpath == "" {
 138  		return syscall.EINVAL
 139  	}
 140  	return errnoErr(path_link(
 141  		int32(olddirfd),
 142  		0,
 143  		unsafe.StringData(oldpath),
 144  		size(len(oldpath)),
 145  		int32(newdirfd),
 146  		unsafe.StringData(newpath),
 147  		size(len(newpath)),
 148  	))
 149  }
 150  
 151  //go:wasmimport wasi_snapshot_preview1 path_link
 152  //go:noescape
 153  func path_link(oldFd int32, oldFlags uint32, oldPath *byte, oldPathLen size, newFd int32, newPath *byte, newPathLen size) syscall.Errno
 154  
 155  func Symlinkat(oldpath string, newdirfd int, newpath string) error {
 156  	if oldpath == "" || newpath == "" {
 157  		return syscall.EINVAL
 158  	}
 159  	return errnoErr(path_symlink(
 160  		unsafe.StringData(oldpath),
 161  		size(len(oldpath)),
 162  		int32(newdirfd),
 163  		unsafe.StringData(newpath),
 164  		size(len(newpath)),
 165  	))
 166  }
 167  
 168  //go:wasmimport wasi_snapshot_preview1 path_symlink
 169  //go:noescape
 170  func path_symlink(oldPath *byte, oldPathLen size, fd int32, newPath *byte, newPathLen size) syscall.Errno
 171  
 172  func errnoErr(errno syscall.Errno) error {
 173  	if errno == 0 {
 174  		return nil
 175  	}
 176  	return errno
 177  }
 178