github.com/tetratelabs/wazero@v1.2.1/internal/fsapi/dir.go (about)

     1  package fsapi
     2  
     3  import (
     4  	"fmt"
     5  	"io/fs"
     6  	"syscall"
     7  	"time"
     8  )
     9  
    10  // Dirent is an entry read from a directory.
    11  //
    12  // This is a portable variant of syscall.Dirent containing fields needed for
    13  // WebAssembly ABI including WASI snapshot-01 and wasi-filesystem. Unlike
    14  // fs.DirEntry, this may include the Ino.
    15  type Dirent struct {
    16  	// ^^ Dirent name matches syscall.Dirent
    17  
    18  	// Name is the base name of the directory entry.
    19  	Name string
    20  
    21  	// Ino is the file serial number, or zero if not available.
    22  	Ino uint64
    23  
    24  	// Type is fs.FileMode masked on fs.ModeType. For example, zero is a
    25  	// regular file, fs.ModeDir is a directory and fs.ModeIrregular is unknown.
    26  	Type fs.FileMode
    27  }
    28  
    29  func (d *Dirent) String() string {
    30  	return fmt.Sprintf("name=%s, type=%v, ino=%d", d.Name, d.Type, d.Ino)
    31  }
    32  
    33  // IsDir returns true if the Type is fs.ModeDir.
    34  func (d *Dirent) IsDir() bool {
    35  	return d.Type == fs.ModeDir
    36  }
    37  
    38  // DirFile is embeddable to reduce the amount of functions to implement a file.
    39  type DirFile struct{}
    40  
    41  // IsAppend implements File.IsAppend
    42  func (DirFile) IsAppend() bool {
    43  	return false
    44  }
    45  
    46  // SetAppend implements File.SetAppend
    47  func (DirFile) SetAppend(bool) syscall.Errno {
    48  	return syscall.EISDIR
    49  }
    50  
    51  // IsNonblock implements File.IsNonblock
    52  func (DirFile) IsNonblock() bool {
    53  	return false
    54  }
    55  
    56  // SetNonblock implements File.SetNonblock
    57  func (DirFile) SetNonblock(bool) syscall.Errno {
    58  	return syscall.EISDIR
    59  }
    60  
    61  // IsDir implements File.IsDir
    62  func (DirFile) IsDir() (bool, syscall.Errno) {
    63  	return true, 0
    64  }
    65  
    66  // Read implements File.Read
    67  func (DirFile) Read([]byte) (int, syscall.Errno) {
    68  	return 0, syscall.EISDIR
    69  }
    70  
    71  // Pread implements File.Pread
    72  func (DirFile) Pread([]byte, int64) (int, syscall.Errno) {
    73  	return 0, syscall.EISDIR
    74  }
    75  
    76  // Seek implements File.Seek
    77  func (DirFile) Seek(int64, int) (int64, syscall.Errno) {
    78  	return 0, syscall.EISDIR
    79  }
    80  
    81  // PollRead implements File.PollRead
    82  func (DirFile) PollRead(*time.Duration) (ready bool, errno syscall.Errno) {
    83  	return false, syscall.ENOSYS
    84  }
    85  
    86  // Write implements File.Write
    87  func (DirFile) Write([]byte) (int, syscall.Errno) {
    88  	return 0, syscall.EISDIR
    89  }
    90  
    91  // Pwrite implements File.Pwrite
    92  func (DirFile) Pwrite([]byte, int64) (int, syscall.Errno) {
    93  	return 0, syscall.EISDIR
    94  }
    95  
    96  // Truncate implements File.Truncate
    97  func (DirFile) Truncate(int64) syscall.Errno {
    98  	return syscall.EISDIR
    99  }