github.com/charlievieth/fastwalk@v1.0.3/internal/dirent/dirent_wasip1.go (about)

     1  // Copyright 2023 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 dirent
     8  
     9  import (
    10  	"os"
    11  	"syscall"
    12  	"unsafe"
    13  )
    14  
    15  // https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-dirent-record
    16  const sizeOfDirent = 24
    17  
    18  func direntIno(buf []byte) (uint64, bool) {
    19  	return readInt(buf, unsafe.Offsetof(syscall.Dirent{}.Ino), unsafe.Sizeof(syscall.Dirent{}.Ino))
    20  }
    21  
    22  func direntReclen(buf []byte) (uint64, bool) {
    23  	namelen, ok := direntNamlen(buf)
    24  	return sizeOfDirent + namelen, ok
    25  }
    26  
    27  func direntNamlen(buf []byte) (uint64, bool) {
    28  	return readInt(buf, unsafe.Offsetof(syscall.Dirent{}.Namlen), unsafe.Sizeof(syscall.Dirent{}.Namlen))
    29  }
    30  
    31  func direntType(buf []byte) os.FileMode {
    32  	off := unsafe.Offsetof(syscall.Dirent{}.Type)
    33  	if off >= uintptr(len(buf)) {
    34  		return ^os.FileMode(0) // unknown
    35  	}
    36  	switch syscall.Filetype(buf[off]) {
    37  	case syscall.FILETYPE_BLOCK_DEVICE:
    38  		return os.ModeDevice
    39  	case syscall.FILETYPE_CHARACTER_DEVICE:
    40  		return os.ModeDevice | os.ModeCharDevice
    41  	case syscall.FILETYPE_DIRECTORY:
    42  		return os.ModeDir
    43  	case syscall.FILETYPE_REGULAR_FILE:
    44  		return 0
    45  	case syscall.FILETYPE_SOCKET_DGRAM:
    46  		return os.ModeSocket
    47  	case syscall.FILETYPE_SOCKET_STREAM:
    48  		return os.ModeSocket
    49  	case syscall.FILETYPE_SYMBOLIC_LINK:
    50  		return os.ModeSymlink
    51  	}
    52  	return ^os.FileMode(0) // unknown
    53  }