github.com/primecitizens/pcz/std@v0.2.1/ffi/wasm/wasi/readdir.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/assert"
    16  	"github.com/primecitizens/pcz/std/core/mark"
    17  )
    18  
    19  type Dirent struct {
    20  	// The offset of the next directory entry stored in this directory.
    21  	Next Dircookie
    22  	// The serial number of the file referred to by this directory entry.
    23  	Ino uint64
    24  	// The length of the name of the directory entry.
    25  	Namlen uint32
    26  
    27  	// The type of the file referred to by this directory entry.
    28  	Type Filetype
    29  	_    [3]byte
    30  }
    31  
    32  func (ent *Dirent) Decode(buf []byte) (name string, next []byte, ok bool) {
    33  	const (
    34  		direntOffsetsOK = true &&
    35  			unsafe.Offsetof(Dirent{}.Next) == 0 &&
    36  			unsafe.Offsetof(Dirent{}.Ino) == 8 &&
    37  			unsafe.Offsetof(Dirent{}.Namlen) == 16 &&
    38  			unsafe.Offsetof(Dirent{}.Type) == 20
    39  
    40  		direntSize = unsafe.Sizeof(Dirent{})
    41  	)
    42  
    43  	if !direntOffsetsOK || direntSize != 24 {
    44  		assert.Throw("dirent", "size", "or", "offsets", "not", "match")
    45  	}
    46  
    47  	if uintptr(len(buf)) < direntSize {
    48  		next = buf[:0]
    49  		return
    50  	}
    51  
    52  	*ent = *(*Dirent)(mark.NoEscapeSliceDataPointer(buf))
    53  	buf = buf[direntSize:]
    54  	if len(buf) < int(ent.Namlen) {
    55  		next = buf[:0]
    56  		return
    57  	}
    58  
    59  	name = mark.NoEscapeBytesString(buf)[:ent.Namlen]
    60  	next = buf[ent.Namlen:]
    61  	ok = true
    62  	return
    63  }
    64  
    65  //go:wasmimport wasi_snapshot_preview1 fd_readdir
    66  //go:noescape
    67  func Readdir(
    68  	fd FD,
    69  	buf unsafe.Pointer,
    70  	bufLen Size,
    71  	cookie Dircookie,
    72  	nwritten unsafe.Pointer,
    73  ) Errno