github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/wasip1/fs.go (about)

     1  package wasip1
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  const (
     8  	FdAdviseName           = "fd_advise"
     9  	FdAllocateName         = "fd_allocate"
    10  	FdCloseName            = "fd_close"
    11  	FdDatasyncName         = "fd_datasync"
    12  	FdFdstatGetName        = "fd_fdstat_get"
    13  	FdFdstatSetFlagsName   = "fd_fdstat_set_flags"
    14  	FdFdstatSetRightsName  = "fd_fdstat_set_rights"
    15  	FdFilestatGetName      = "fd_filestat_get"
    16  	FdFilestatSetSizeName  = "fd_filestat_set_size"
    17  	FdFilestatSetTimesName = "fd_filestat_set_times"
    18  	FdPreadName            = "fd_pread"
    19  	FdPrestatGetName       = "fd_prestat_get"
    20  	FdPrestatDirNameName   = "fd_prestat_dir_name"
    21  	FdPwriteName           = "fd_pwrite"
    22  	FdReadName             = "fd_read"
    23  	FdReaddirName          = "fd_readdir"
    24  	FdRenumberName         = "fd_renumber"
    25  	FdSeekName             = "fd_seek"
    26  	FdSyncName             = "fd_sync"
    27  	FdTellName             = "fd_tell"
    28  	FdWriteName            = "fd_write"
    29  
    30  	PathCreateDirectoryName  = "path_create_directory"
    31  	PathFilestatGetName      = "path_filestat_get"
    32  	PathFilestatSetTimesName = "path_filestat_set_times"
    33  	PathLinkName             = "path_link"
    34  	PathOpenName             = "path_open"
    35  	PathReadlinkName         = "path_readlink"
    36  	PathRemoveDirectoryName  = "path_remove_directory"
    37  	PathRenameName           = "path_rename"
    38  	PathSymlinkName          = "path_symlink"
    39  	PathUnlinkFileName       = "path_unlink_file"
    40  )
    41  
    42  // oflags are open flags used by path_open
    43  // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-oflags-flagsu16
    44  const (
    45  	// O_CREAT creates a file if it does not exist.
    46  	O_CREAT uint16 = 1 << iota //nolint
    47  	// O_DIRECTORY fails if not a directory.
    48  	O_DIRECTORY
    49  	// O_EXCL fails if file already exists.
    50  	O_EXCL //nolint
    51  	// O_TRUNC truncates the file to size 0.
    52  	O_TRUNC //nolint
    53  )
    54  
    55  func OflagsString(oflags int) string {
    56  	return flagsString(oflagNames[:], oflags)
    57  }
    58  
    59  var oflagNames = [...]string{
    60  	"CREAT",
    61  	"DIRECTORY",
    62  	"EXCL",
    63  	"TRUNC",
    64  }
    65  
    66  // file descriptor flags
    67  // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fdflags
    68  const (
    69  	FD_APPEND uint16 = 1 << iota //nolint
    70  	FD_DSYNC
    71  	FD_NONBLOCK
    72  	FD_RSYNC
    73  	FD_SYNC
    74  )
    75  
    76  func FdFlagsString(fdflags int) string {
    77  	return flagsString(fdflagNames[:], fdflags)
    78  }
    79  
    80  var fdflagNames = [...]string{
    81  	"APPEND",
    82  	"DSYNC",
    83  	"NONBLOCK",
    84  	"RSYNC",
    85  	"SYNC",
    86  }
    87  
    88  // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#lookupflags
    89  const (
    90  	// LOOKUP_SYMLINK_FOLLOW expands a path if it resolves into a symbolic
    91  	// link.
    92  	LOOKUP_SYMLINK_FOLLOW uint16 = 1 << iota //nolint
    93  )
    94  
    95  var lookupflagNames = [...]string{
    96  	"SYMLINK_FOLLOW",
    97  }
    98  
    99  func LookupflagsString(lookupflags int) string {
   100  	return flagsString(lookupflagNames[:], lookupflags)
   101  }
   102  
   103  // DirentSize is the size of the dirent struct, which should be followed by the
   104  // length of a file name.
   105  const DirentSize = uint32(24)
   106  
   107  const (
   108  	FILETYPE_UNKNOWN uint8 = iota
   109  	FILETYPE_BLOCK_DEVICE
   110  	FILETYPE_CHARACTER_DEVICE
   111  	FILETYPE_DIRECTORY
   112  	FILETYPE_REGULAR_FILE
   113  	FILETYPE_SOCKET_DGRAM
   114  	FILETYPE_SOCKET_STREAM
   115  	FILETYPE_SYMBOLIC_LINK
   116  )
   117  
   118  // FiletypeName returns string name of the file type.
   119  func FiletypeName(filetype uint8) string {
   120  	if int(filetype) < len(filetypeToString) {
   121  		return filetypeToString[filetype]
   122  	}
   123  	return fmt.Sprintf("filetype(%d)", filetype)
   124  }
   125  
   126  var filetypeToString = [...]string{
   127  	"UNKNOWN",
   128  	"BLOCK_DEVICE",
   129  	"CHARACTER_DEVICE",
   130  	"DIRECTORY",
   131  	"REGULAR_FILE",
   132  	"SOCKET_DGRAM",
   133  	"SOCKET_STREAM",
   134  	"SYMBOLIC_LINK",
   135  }
   136  
   137  // https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fstflags
   138  const (
   139  	FstflagsAtim uint16 = 1 << iota
   140  	FstflagsAtimNow
   141  	FstflagsMtim
   142  	FstflagsMtimNow
   143  )
   144  
   145  var fstflagNames = [...]string{
   146  	"ATIM",
   147  	"ATIM_NOW",
   148  	"MTIM",
   149  	"MTIM_NOW",
   150  }
   151  
   152  func FstflagsString(fdflags int) string {
   153  	return flagsString(fstflagNames[:], fdflags)
   154  }
   155  
   156  // https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-advice-enumu8
   157  const (
   158  	FdAdviceNormal byte = iota
   159  	FdAdviceSequential
   160  	FdAdviceRandom
   161  	FdAdviceWillNeed
   162  	FdAdviceDontNeed
   163  	FdAdviceNoReuse
   164  )