Source file src/os/dirent_wasip1.go

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

View as plain text