github.com/primecitizens/pcz/std@v0.2.1/ffi/wasm/wasi/fdstat.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  //
     4  // Copyright 2023 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  //go:build wasip1
     9  
    10  package wasi
    11  
    12  import (
    13  	"unsafe"
    14  
    15  	"github.com/primecitizens/pcz/std/core/mark"
    16  )
    17  
    18  // https://github.com/WebAssembly/WASI/blob/a2b96e81c0586125cc4dc79a5be0b78d9a059925/legacy/preview1/docs.md#-FDstat_t-record
    19  // fdflags must be at offset 2, hence the uint16 type rather than the
    20  // fdflags (uint32) type.
    21  type FDstat_t struct {
    22  	Type             Filetype
    23  	FDflags          uint16
    24  	RightsBase       Rights
    25  	RightsInheriting Rights
    26  }
    27  
    28  // https://github.com/WebAssembly/WASI/blob/a2b96e81c0586125cc4dc79a5be0b78d9a059925/legacy/preview1/docs.md#-fd_fdstat_set_rightsfd-fd-fs_rights_base-rights-fs_rights_inheriting-rights---result-errno
    29  //
    30  //go:wasmimport wasi_snapshot_preview1 fd_fdstat_set_rights
    31  //go:noescape
    32  func FDstatSetRights(fd FD, rightsBase, rightsInheriting Rights) Errno
    33  
    34  //go:wasmimport wasi_snapshot_preview1 fd_fdstat_get
    35  //go:noescape
    36  func fdstat_get(fd FD, buf unsafe.Pointer) Errno
    37  
    38  func FDstat(fd FD, out *FDstat_t) Errno {
    39  	return fdstat_get(fd, mark.NoEscapePointer(out))
    40  }
    41  
    42  //go:wasmimport wasi_snapshot_preview1 fd_fdstat_set_flags
    43  //go:noescape
    44  func FDstatSetFlags(fd FD, flags FDflags) Errno
    45  
    46  func FDstatGetFlags(fd FD) (FDflags, Errno) {
    47  	var stat FDstat_t
    48  	errno := fdstat_get(fd, mark.NoEscapePointer(&stat))
    49  	return FDflags(stat.FDflags), errno
    50  }
    51  
    52  func FDstatGetType(fd FD) (Filetype, Errno) {
    53  	var stat FDstat_t
    54  	errno := fdstat_get(fd, mark.NoEscapePointer(&stat))
    55  	return stat.Type, errno
    56  }
    57  
    58  func SetNonblock(fd FD, nonblocking bool) Errno {
    59  	flags, errno := FDstatGetFlags(fd)
    60  	if errno != 0 {
    61  		return errno
    62  	}
    63  	if nonblocking {
    64  		flags |= FDflag_NONBLOCK
    65  	} else {
    66  		flags &^= FDflag_NONBLOCK
    67  	}
    68  	return FDstatSetFlags(fd, flags)
    69  }