| 1 |
package wasi_snapshot_preview1 |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"io" |
| 6 |
"io/fs" |
| 7 |
"math" |
| 8 |
"path" |
| 9 |
"strings" |
| 10 |
"unsafe" |
| 11 |
|
| 12 |
"github.com/tetratelabs/wazero/api" |
| 13 |
experimentalsys "github.com/tetratelabs/wazero/experimental/sys" |
| 14 |
socketapi "github.com/tetratelabs/wazero/internal/sock" |
| 15 |
"github.com/tetratelabs/wazero/internal/sys" |
| 16 |
"github.com/tetratelabs/wazero/internal/wasip1" |
| 17 |
"github.com/tetratelabs/wazero/internal/wasm" |
| 18 |
sysapi "github.com/tetratelabs/wazero/sys" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
var fdAdvise = newHostFunc( |
| 26 |
wasip1.FdAdviseName, fdAdviseFn, |
| 27 |
[]wasm.ValueType{i32, i64, i64, i32}, |
| 28 |
"fd", "offset", "len", "advice", |
| 29 |
) |
| 30 |
|
| 31 |
func fdAdviseFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 32 |
fd := int32(params[0]) |
| 33 |
_ = params[1] |
| 34 |
_ = params[2] |
| 35 |
advice := byte(params[3]) |
| 36 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 37 |
|
| 38 |
_, ok := fsc.LookupFile(fd) |
| 39 |
if !ok { |
| 40 |
return experimentalsys.EBADF |
| 41 |
} |
| 42 |
|
| 43 |
switch advice { |
| 44 |
case wasip1.FdAdviceNormal, |
| 45 |
wasip1.FdAdviceSequential, |
| 46 |
wasip1.FdAdviceRandom, |
| 47 |
wasip1.FdAdviceWillNeed, |
| 48 |
wasip1.FdAdviceDontNeed, |
| 49 |
wasip1.FdAdviceNoReuse: |
| 50 |
default: |
| 51 |
return experimentalsys.EINVAL |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
return 0 |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
var fdAllocate = newHostFunc( |
| 69 |
wasip1.FdAllocateName, fdAllocateFn, |
| 70 |
[]wasm.ValueType{i32, i64, i64}, |
| 71 |
"fd", "offset", "len", |
| 72 |
) |
| 73 |
|
| 74 |
func fdAllocateFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 75 |
fd := int32(params[0]) |
| 76 |
offset := params[1] |
| 77 |
length := params[2] |
| 78 |
|
| 79 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 80 |
f, ok := fsc.LookupFile(fd) |
| 81 |
if !ok { |
| 82 |
return experimentalsys.EBADF |
| 83 |
} |
| 84 |
|
| 85 |
tail := int64(offset + length) |
| 86 |
if tail < 0 { |
| 87 |
return experimentalsys.EINVAL |
| 88 |
} |
| 89 |
|
| 90 |
st, errno := f.File.Stat() |
| 91 |
if errno != 0 { |
| 92 |
return errno |
| 93 |
} |
| 94 |
|
| 95 |
if st.Size >= tail { |
| 96 |
return 0 |
| 97 |
} |
| 98 |
|
| 99 |
return f.File.Truncate(tail) |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
var fdClose = newHostFunc(wasip1.FdCloseName, fdCloseFn, []wasm.ValueType{i32}, "fd") |
| 119 |
|
| 120 |
func fdCloseFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 121 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 122 |
fd := int32(params[0]) |
| 123 |
|
| 124 |
return fsc.CloseFile(fd) |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
var fdDatasync = newHostFunc(wasip1.FdDatasyncName, fdDatasyncFn, []wasm.ValueType{i32}, "fd") |
| 132 |
|
| 133 |
func fdDatasyncFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 134 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 135 |
fd := int32(params[0]) |
| 136 |
|
| 137 |
|
| 138 |
if f, ok := fsc.LookupFile(fd); !ok { |
| 139 |
return experimentalsys.EBADF |
| 140 |
} else { |
| 141 |
return f.File.Datasync() |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
var fdFdstatGet = newHostFunc(wasip1.FdFdstatGetName, fdFdstatGetFn, []wasm.ValueType{i32, i32}, "fd", "result.stat") |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
func fdFdstatGetFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 187 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 188 |
|
| 189 |
fd, resultFdstat := int32(params[0]), uint32(params[1]) |
| 190 |
|
| 191 |
|
| 192 |
buf, ok := mod.Memory().Read(resultFdstat, 24) |
| 193 |
if !ok { |
| 194 |
return experimentalsys.EFAULT |
| 195 |
} |
| 196 |
|
| 197 |
var fdflags uint16 |
| 198 |
var st sysapi.Stat_t |
| 199 |
var errno experimentalsys.Errno |
| 200 |
f, ok := fsc.LookupFile(fd) |
| 201 |
if !ok { |
| 202 |
return experimentalsys.EBADF |
| 203 |
} else if st, errno = f.File.Stat(); errno != 0 { |
| 204 |
return errno |
| 205 |
} else if f.File.IsAppend() { |
| 206 |
fdflags |= wasip1.FD_APPEND |
| 207 |
} |
| 208 |
|
| 209 |
if isNonblock(f.File) { |
| 210 |
fdflags |= wasip1.FD_NONBLOCK |
| 211 |
} |
| 212 |
|
| 213 |
var fsRightsBase uint32 |
| 214 |
var fsRightsInheriting uint32 |
| 215 |
fileType := getExtendedWasiFiletype(f.File, st.Mode) |
| 216 |
|
| 217 |
switch fileType { |
| 218 |
case wasip1.FILETYPE_DIRECTORY: |
| 219 |
|
| 220 |
|
| 221 |
fsRightsBase = dirRightsBase |
| 222 |
fsRightsInheriting = fileRightsBase | dirRightsBase |
| 223 |
case wasip1.FILETYPE_CHARACTER_DEVICE: |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
fsRightsBase = fileRightsBase &^ wasip1.RIGHT_FD_SEEK &^ wasip1.RIGHT_FD_TELL |
| 228 |
default: |
| 229 |
fsRightsBase = fileRightsBase |
| 230 |
} |
| 231 |
|
| 232 |
writeFdstat(buf, fileType, fdflags, fsRightsBase, fsRightsInheriting) |
| 233 |
return 0 |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
func isPreopenedStdio(fd int32, f *sys.FileEntry) bool { |
| 250 |
return fd <= sys.FdStderr && f.IsPreopen |
| 251 |
} |
| 252 |
|
| 253 |
const fileRightsBase = wasip1.RIGHT_FD_DATASYNC | |
| 254 |
wasip1.RIGHT_FD_READ | |
| 255 |
wasip1.RIGHT_FD_SEEK | |
| 256 |
wasip1.RIGHT_FDSTAT_SET_FLAGS | |
| 257 |
wasip1.RIGHT_FD_SYNC | |
| 258 |
wasip1.RIGHT_FD_TELL | |
| 259 |
wasip1.RIGHT_FD_WRITE | |
| 260 |
wasip1.RIGHT_FD_ADVISE | |
| 261 |
wasip1.RIGHT_FD_ALLOCATE | |
| 262 |
wasip1.RIGHT_FD_FILESTAT_GET | |
| 263 |
wasip1.RIGHT_FD_FILESTAT_SET_SIZE | |
| 264 |
wasip1.RIGHT_FD_FILESTAT_SET_TIMES | |
| 265 |
wasip1.RIGHT_POLL_FD_READWRITE |
| 266 |
|
| 267 |
const dirRightsBase = wasip1.RIGHT_FD_DATASYNC | |
| 268 |
wasip1.RIGHT_FDSTAT_SET_FLAGS | |
| 269 |
wasip1.RIGHT_FD_SYNC | |
| 270 |
wasip1.RIGHT_PATH_CREATE_DIRECTORY | |
| 271 |
wasip1.RIGHT_PATH_CREATE_FILE | |
| 272 |
wasip1.RIGHT_PATH_LINK_SOURCE | |
| 273 |
wasip1.RIGHT_PATH_LINK_TARGET | |
| 274 |
wasip1.RIGHT_PATH_OPEN | |
| 275 |
wasip1.RIGHT_FD_READDIR | |
| 276 |
wasip1.RIGHT_PATH_READLINK | |
| 277 |
wasip1.RIGHT_PATH_RENAME_SOURCE | |
| 278 |
wasip1.RIGHT_PATH_RENAME_TARGET | |
| 279 |
wasip1.RIGHT_PATH_FILESTAT_GET | |
| 280 |
wasip1.RIGHT_PATH_FILESTAT_SET_SIZE | |
| 281 |
wasip1.RIGHT_PATH_FILESTAT_SET_TIMES | |
| 282 |
wasip1.RIGHT_FD_FILESTAT_GET | |
| 283 |
wasip1.RIGHT_FD_FILESTAT_SET_TIMES | |
| 284 |
wasip1.RIGHT_PATH_SYMLINK | |
| 285 |
wasip1.RIGHT_PATH_REMOVE_DIRECTORY | |
| 286 |
wasip1.RIGHT_PATH_UNLINK_FILE |
| 287 |
|
| 288 |
func writeFdstat(buf []byte, fileType uint8, fdflags uint16, fsRightsBase, fsRightsInheriting uint32) { |
| 289 |
b := (*[24]byte)(buf) |
| 290 |
le.PutUint16(b[0:], uint16(fileType)) |
| 291 |
le.PutUint16(b[2:], fdflags) |
| 292 |
le.PutUint32(b[4:], 0) |
| 293 |
le.PutUint64(b[8:], uint64(fsRightsBase)) |
| 294 |
le.PutUint64(b[16:], uint64(fsRightsInheriting)) |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
var fdFdstatSetFlags = newHostFunc(wasip1.FdFdstatSetFlagsName, fdFdstatSetFlagsFn, []wasm.ValueType{i32, i32}, "fd", "flags") |
| 300 |
|
| 301 |
func fdFdstatSetFlagsFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 302 |
fd, wasiFlag := int32(params[0]), uint16(params[1]) |
| 303 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 304 |
|
| 305 |
|
| 306 |
if wasip1.FD_DSYNC&wasiFlag != 0 || wasip1.FD_RSYNC&wasiFlag != 0 || wasip1.FD_SYNC&wasiFlag != 0 { |
| 307 |
return experimentalsys.EINVAL |
| 308 |
} |
| 309 |
|
| 310 |
if f, ok := fsc.LookupFile(fd); !ok { |
| 311 |
return experimentalsys.EBADF |
| 312 |
} else { |
| 313 |
nonblock := wasip1.FD_NONBLOCK&wasiFlag != 0 |
| 314 |
if pf, ok := f.File.(experimentalsys.PollableFile); ok { |
| 315 |
if errno := pf.SetNonblock(nonblock); errno != 0 { |
| 316 |
return errno |
| 317 |
} |
| 318 |
} else if isDir, errno := f.File.IsDir(); errno == 0 && isDir { |
| 319 |
return experimentalsys.EISDIR |
| 320 |
} else if nonblock { |
| 321 |
return experimentalsys.ENOSYS |
| 322 |
} |
| 323 |
if stat, err := f.File.Stat(); err == 0 && stat.Mode.IsRegular() { |
| 324 |
|
| 325 |
append := wasip1.FD_APPEND&wasiFlag != 0 |
| 326 |
return f.File.SetAppend(append) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return 0 |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
var fdFdstatSetRights = stubFunction( |
| 337 |
wasip1.FdFdstatSetRightsName, |
| 338 |
[]wasm.ValueType{i32, i64, i64}, |
| 339 |
"fd", "fs_rights_base", "fs_rights_inheriting", |
| 340 |
) |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
var fdFilestatGet = newHostFunc(wasip1.FdFilestatGetName, fdFilestatGetFn, []wasm.ValueType{i32, i32}, "fd", "result.filestat") |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
func fdFilestatGetFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 394 |
return fdFilestatGetFunc(mod, int32(params[0]), uint32(params[1])) |
| 395 |
} |
| 396 |
|
| 397 |
func fdFilestatGetFunc(mod api.Module, fd int32, resultBuf uint32) experimentalsys.Errno { |
| 398 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 399 |
|
| 400 |
|
| 401 |
buf, ok := mod.Memory().Read(resultBuf, 64) |
| 402 |
if !ok { |
| 403 |
return experimentalsys.EFAULT |
| 404 |
} |
| 405 |
|
| 406 |
f, ok := fsc.LookupFile(fd) |
| 407 |
if !ok { |
| 408 |
return experimentalsys.EBADF |
| 409 |
} |
| 410 |
|
| 411 |
st, errno := f.File.Stat() |
| 412 |
if errno != 0 { |
| 413 |
return errno |
| 414 |
} |
| 415 |
|
| 416 |
filetype := getExtendedWasiFiletype(f.File, st.Mode) |
| 417 |
return writeFilestat(buf, &st, filetype) |
| 418 |
} |
| 419 |
|
| 420 |
func getExtendedWasiFiletype(file experimentalsys.File, fm fs.FileMode) (ftype uint8) { |
| 421 |
ftype = getWasiFiletype(fm) |
| 422 |
if ftype == wasip1.FILETYPE_UNKNOWN { |
| 423 |
if _, ok := file.(socketapi.TCPSock); ok { |
| 424 |
ftype = wasip1.FILETYPE_SOCKET_STREAM |
| 425 |
} else if _, ok = file.(socketapi.TCPConn); ok { |
| 426 |
ftype = wasip1.FILETYPE_SOCKET_STREAM |
| 427 |
} |
| 428 |
} |
| 429 |
return |
| 430 |
} |
| 431 |
|
| 432 |
func getWasiFiletype(fm fs.FileMode) uint8 { |
| 433 |
switch { |
| 434 |
case fm.IsRegular(): |
| 435 |
return wasip1.FILETYPE_REGULAR_FILE |
| 436 |
case fm.IsDir(): |
| 437 |
return wasip1.FILETYPE_DIRECTORY |
| 438 |
case fm&fs.ModeSymlink != 0: |
| 439 |
return wasip1.FILETYPE_SYMBOLIC_LINK |
| 440 |
case fm&fs.ModeDevice != 0: |
| 441 |
|
| 442 |
|
| 443 |
if fm&fs.ModeCharDevice != 0 { |
| 444 |
return wasip1.FILETYPE_CHARACTER_DEVICE |
| 445 |
} |
| 446 |
return wasip1.FILETYPE_BLOCK_DEVICE |
| 447 |
default: |
| 448 |
return wasip1.FILETYPE_UNKNOWN |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
func writeFilestat(buf []byte, st *sysapi.Stat_t, ftype uint8) (errno experimentalsys.Errno) { |
| 453 |
le.PutUint64(buf, st.Dev) |
| 454 |
le.PutUint64(buf[8:], st.Ino) |
| 455 |
le.PutUint64(buf[16:], uint64(ftype)) |
| 456 |
le.PutUint64(buf[24:], st.Nlink) |
| 457 |
le.PutUint64(buf[32:], uint64(st.Size)) |
| 458 |
le.PutUint64(buf[40:], uint64(st.Atim)) |
| 459 |
le.PutUint64(buf[48:], uint64(st.Mtim)) |
| 460 |
le.PutUint64(buf[56:], uint64(st.Ctim)) |
| 461 |
return |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
var fdFilestatSetSize = newHostFunc(wasip1.FdFilestatSetSizeName, fdFilestatSetSizeFn, []wasm.ValueType{i32, i64}, "fd", "size") |
| 469 |
|
| 470 |
func fdFilestatSetSizeFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 471 |
fd := int32(params[0]) |
| 472 |
size := int64(params[1]) |
| 473 |
|
| 474 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 475 |
|
| 476 |
|
| 477 |
if f, ok := fsc.LookupFile(fd); !ok { |
| 478 |
return experimentalsys.EBADF |
| 479 |
} else { |
| 480 |
return f.File.Truncate(size) |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
var fdFilestatSetTimes = newHostFunc( |
| 489 |
wasip1.FdFilestatSetTimesName, fdFilestatSetTimesFn, |
| 490 |
[]wasm.ValueType{i32, i64, i64, i32}, |
| 491 |
"fd", "atim", "mtim", "fst_flags", |
| 492 |
) |
| 493 |
|
| 494 |
func fdFilestatSetTimesFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 495 |
fd := int32(params[0]) |
| 496 |
atim := int64(params[1]) |
| 497 |
mtim := int64(params[2]) |
| 498 |
fstFlags := uint16(params[3]) |
| 499 |
|
| 500 |
sys := mod.(*wasm.ModuleInstance).Sys |
| 501 |
fsc := sys.FS() |
| 502 |
|
| 503 |
f, ok := fsc.LookupFile(fd) |
| 504 |
if !ok { |
| 505 |
return experimentalsys.EBADF |
| 506 |
} |
| 507 |
|
| 508 |
atim, mtim, errno := toTimes(sys.WalltimeNanos, atim, mtim, fstFlags) |
| 509 |
if errno != 0 { |
| 510 |
return errno |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
errno = f.File.Utimens(atim, mtim) |
| 515 |
|
| 516 |
|
| 517 |
switch errno { |
| 518 |
case experimentalsys.EPERM, experimentalsys.ENOSYS: |
| 519 |
errno = f.FS.Utimens(f.Name, atim, mtim) |
| 520 |
} |
| 521 |
|
| 522 |
return errno |
| 523 |
} |
| 524 |
|
| 525 |
func toTimes(walltime func() int64, atim, mtim int64, fstFlags uint16) (int64, int64, experimentalsys.Errno) { |
| 526 |
|
| 527 |
|
| 528 |
var nowTim int64 |
| 529 |
|
| 530 |
|
| 531 |
if set, now := fstFlags&wasip1.FstflagsAtim != 0, fstFlags&wasip1.FstflagsAtimNow != 0; set && now { |
| 532 |
return 0, 0, experimentalsys.EINVAL |
| 533 |
} else if set { |
| 534 |
|
| 535 |
} else if now { |
| 536 |
nowTim = walltime() |
| 537 |
atim = nowTim |
| 538 |
} else { |
| 539 |
atim = experimentalsys.UTIME_OMIT |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
if set, now := fstFlags&wasip1.FstflagsMtim != 0, fstFlags&wasip1.FstflagsMtimNow != 0; set && now { |
| 544 |
return 0, 0, experimentalsys.EINVAL |
| 545 |
} else if set { |
| 546 |
|
| 547 |
} else if now { |
| 548 |
if nowTim != 0 { |
| 549 |
mtim = nowTim |
| 550 |
} else { |
| 551 |
mtim = walltime() |
| 552 |
} |
| 553 |
} else { |
| 554 |
mtim = experimentalsys.UTIME_OMIT |
| 555 |
} |
| 556 |
return atim, mtim, 0 |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
var fdPread = newHostFunc( |
| 566 |
wasip1.FdPreadName, fdPreadFn, |
| 567 |
[]wasm.ValueType{i32, i32, i32, i64, i32}, |
| 568 |
"fd", "iovs", "iovs_len", "offset", "result.nread", |
| 569 |
) |
| 570 |
|
| 571 |
func fdPreadFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 572 |
return fdReadOrPread(mod, params, true) |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
var fdPrestatGet = newHostFunc(wasip1.FdPrestatGetName, fdPrestatGetFn, []wasm.ValueType{i32, i32}, "fd", "result.prestat") |
| 607 |
|
| 608 |
func fdPrestatGetFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 609 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 610 |
fd, resultPrestat := int32(params[0]), uint32(params[1]) |
| 611 |
|
| 612 |
name, errno := preopenPath(fsc, fd) |
| 613 |
if errno != 0 { |
| 614 |
return errno |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
prestat := uint64(len(name)) << 32 |
| 620 |
if !mod.Memory().WriteUint64Le(resultPrestat, prestat) { |
| 621 |
return experimentalsys.EFAULT |
| 622 |
} |
| 623 |
return 0 |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
var fdPrestatDirName = newHostFunc( |
| 657 |
wasip1.FdPrestatDirNameName, fdPrestatDirNameFn, |
| 658 |
[]wasm.ValueType{i32, i32, i32}, |
| 659 |
"fd", "result.path", "result.path_len", |
| 660 |
) |
| 661 |
|
| 662 |
func fdPrestatDirNameFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 663 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 664 |
fd, path, pathLen := int32(params[0]), uint32(params[1]), uint32(params[2]) |
| 665 |
|
| 666 |
name, errno := preopenPath(fsc, fd) |
| 667 |
if errno != 0 { |
| 668 |
return errno |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
if uint32(len(name)) < pathLen { |
| 673 |
return experimentalsys.ENAMETOOLONG |
| 674 |
} |
| 675 |
|
| 676 |
if !mod.Memory().Write(path, []byte(name)[:pathLen]) { |
| 677 |
return experimentalsys.EFAULT |
| 678 |
} |
| 679 |
return 0 |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
var fdPwrite = newHostFunc( |
| 689 |
wasip1.FdPwriteName, fdPwriteFn, |
| 690 |
[]wasm.ValueType{i32, i32, i32, i64, i32}, |
| 691 |
"fd", "iovs", "iovs_len", "offset", "result.nwritten", |
| 692 |
) |
| 693 |
|
| 694 |
func fdPwriteFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 695 |
return fdWriteOrPwrite(mod, params, true) |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
var fdRead = newHostFunc( |
| 748 |
wasip1.FdReadName, fdReadFn, |
| 749 |
[]wasm.ValueType{i32, i32, i32, i32}, |
| 750 |
"fd", "iovs", "iovs_len", "result.nread", |
| 751 |
) |
| 752 |
|
| 753 |
|
| 754 |
type preader struct { |
| 755 |
f experimentalsys.File |
| 756 |
offset int64 |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
func (w *preader) Read(buf []byte) (n int, errno experimentalsys.Errno) { |
| 761 |
if len(buf) == 0 { |
| 762 |
return 0, 0 |
| 763 |
} |
| 764 |
|
| 765 |
n, err := w.f.Pread(buf, w.offset) |
| 766 |
w.offset += int64(n) |
| 767 |
return n, err |
| 768 |
} |
| 769 |
|
| 770 |
func fdReadFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 771 |
return fdReadOrPread(mod, params, false) |
| 772 |
} |
| 773 |
|
| 774 |
func fdReadOrPread(mod api.Module, params []uint64, isPread bool) experimentalsys.Errno { |
| 775 |
mem := mod.Memory() |
| 776 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 777 |
|
| 778 |
fd := int32(params[0]) |
| 779 |
iovs := uint32(params[1]) |
| 780 |
iovsCount := uint32(params[2]) |
| 781 |
|
| 782 |
var resultNread uint32 |
| 783 |
var reader func(buf []byte) (n int, errno experimentalsys.Errno) |
| 784 |
if f, ok := fsc.LookupFile(fd); !ok { |
| 785 |
return experimentalsys.EBADF |
| 786 |
} else if isPread { |
| 787 |
offset := int64(params[3]) |
| 788 |
reader = (&preader{f: f.File, offset: offset}).Read |
| 789 |
resultNread = uint32(params[4]) |
| 790 |
} else { |
| 791 |
reader = f.File.Read |
| 792 |
resultNread = uint32(params[3]) |
| 793 |
} |
| 794 |
|
| 795 |
nread, errno := readv(mem, iovs, iovsCount, reader) |
| 796 |
if errno != 0 { |
| 797 |
return errno |
| 798 |
} |
| 799 |
if !mem.WriteUint32Le(resultNread, nread) { |
| 800 |
return experimentalsys.EFAULT |
| 801 |
} else { |
| 802 |
return 0 |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
func readv(mem api.Memory, iovs uint32, iovsCount uint32, reader func(buf []byte) (nread int, errno experimentalsys.Errno)) (uint32, experimentalsys.Errno) { |
| 807 |
var nread uint32 |
| 808 |
iovsStop := iovsCount << 3 |
| 809 |
iovsBuf, ok := mem.Read(iovs, iovsStop) |
| 810 |
if !ok { |
| 811 |
return 0, experimentalsys.EFAULT |
| 812 |
} |
| 813 |
|
| 814 |
for iovsPos := uint32(0); iovsPos < iovsStop; iovsPos += 8 { |
| 815 |
offset := le.Uint32(iovsBuf[iovsPos:]) |
| 816 |
l := le.Uint32(iovsBuf[iovsPos+4:]) |
| 817 |
|
| 818 |
if l == 0 { |
| 819 |
continue |
| 820 |
} |
| 821 |
|
| 822 |
b, ok := mem.Read(offset, l) |
| 823 |
if !ok { |
| 824 |
return 0, experimentalsys.EFAULT |
| 825 |
} |
| 826 |
|
| 827 |
n, errno := reader(b) |
| 828 |
nread += uint32(n) |
| 829 |
|
| 830 |
if errno == experimentalsys.ENOSYS { |
| 831 |
return 0, experimentalsys.EBADF |
| 832 |
} else if errno != 0 { |
| 833 |
return 0, errno |
| 834 |
} else if n < int(l) { |
| 835 |
break |
| 836 |
} |
| 837 |
} |
| 838 |
return nread, 0 |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
var fdReaddir = newHostFunc( |
| 862 |
wasip1.FdReaddirName, fdReaddirFn, |
| 863 |
[]wasm.ValueType{i32, i32, i32, i64, i32}, |
| 864 |
"fd", "buf", "buf_len", "cookie", "result.bufused", |
| 865 |
) |
| 866 |
|
| 867 |
func fdReaddirFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 868 |
mem := mod.Memory() |
| 869 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 870 |
|
| 871 |
fd := int32(params[0]) |
| 872 |
buf := uint32(params[1]) |
| 873 |
bufLen := uint32(params[2]) |
| 874 |
cookie := params[3] |
| 875 |
resultBufused := uint32(params[4]) |
| 876 |
|
| 877 |
|
| 878 |
if bufLen < wasip1.DirentSize { |
| 879 |
|
| 880 |
|
| 881 |
return experimentalsys.EINVAL |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
dir, errno := direntCache(fsc, fd) |
| 886 |
if errno != 0 { |
| 887 |
return errno |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
maxDirEntries := bufLen/wasip1.DirentSize + 1 |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
maxDirEntries += 1 |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
dirents, errno := dir.Read(cookie, maxDirEntries) |
| 908 |
if errno != 0 { |
| 909 |
return errno |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
bufToWrite, direntCount, truncatedLen := maxDirents(dirents, bufLen) |
| 915 |
|
| 916 |
|
| 917 |
if bufToWrite > 0 { |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
d_next := cookie + 1 |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
buf, ok := mem.Read(buf, bufToWrite) |
| 926 |
if !ok { |
| 927 |
return experimentalsys.EFAULT |
| 928 |
} |
| 929 |
|
| 930 |
writeDirents(buf, dirents, d_next, direntCount, truncatedLen) |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
bufused := bufToWrite |
| 936 |
if truncatedLen > 0 { |
| 937 |
bufused = bufLen |
| 938 |
} |
| 939 |
|
| 940 |
if !mem.WriteUint32Le(resultBufused, bufused) { |
| 941 |
return experimentalsys.EFAULT |
| 942 |
} |
| 943 |
return 0 |
| 944 |
} |
| 945 |
|
| 946 |
const largestDirent = int64(math.MaxUint32 - wasip1.DirentSize) |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
func maxDirents(dirents []experimentalsys.Dirent, bufLen uint32) (bufToWrite uint32, direntCount int, truncatedLen uint32) { |
| 953 |
lenRemaining := bufLen |
| 954 |
for i := range dirents { |
| 955 |
if lenRemaining == 0 { |
| 956 |
break |
| 957 |
} |
| 958 |
d := dirents[i] |
| 959 |
direntCount++ |
| 960 |
|
| 961 |
|
| 962 |
nameLen := int64(len(d.Name)) |
| 963 |
var entryLen uint32 |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
if el := int64(wasip1.DirentSize) + nameLen; el < 0 || el > largestDirent { |
| 968 |
|
| 969 |
|
| 970 |
panic("invalid filename: too large") |
| 971 |
} else { |
| 972 |
entryLen = uint32(el) |
| 973 |
} |
| 974 |
|
| 975 |
if entryLen > lenRemaining { |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
if lenRemaining >= wasip1.DirentSize { |
| 983 |
truncatedLen = wasip1.DirentSize |
| 984 |
} else { |
| 985 |
truncatedLen = lenRemaining |
| 986 |
} |
| 987 |
bufToWrite += truncatedLen |
| 988 |
break |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
lenRemaining -= entryLen |
| 993 |
bufToWrite += entryLen |
| 994 |
} |
| 995 |
return |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
func writeDirents(buf []byte, dirents []experimentalsys.Dirent, d_next uint64, direntCount int, truncatedLen uint32) { |
| 1002 |
pos := uint32(0) |
| 1003 |
skipNameI := -1 |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
if truncatedLen > 0 { |
| 1008 |
if truncatedLen < wasip1.DirentSize { |
| 1009 |
direntCount-- |
| 1010 |
} else { |
| 1011 |
skipNameI = direntCount - 1 |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
for i := 0; i < direntCount; i++ { |
| 1016 |
e := dirents[i] |
| 1017 |
nameLen := uint32(len(e.Name)) |
| 1018 |
writeDirent(buf[pos:], d_next, e.Ino, nameLen, e.Type) |
| 1019 |
d_next++ |
| 1020 |
pos += wasip1.DirentSize |
| 1021 |
|
| 1022 |
if i != skipNameI { |
| 1023 |
copy(buf[pos:], e.Name) |
| 1024 |
pos += nameLen |
| 1025 |
} |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
func writeDirent(buf []byte, dNext uint64, ino sysapi.Inode, dNamlen uint32, dType fs.FileMode) { |
| 1031 |
le.PutUint64(buf, dNext) |
| 1032 |
le.PutUint64(buf[8:], ino) |
| 1033 |
le.PutUint32(buf[16:], dNamlen) |
| 1034 |
filetype := getWasiFiletype(dType) |
| 1035 |
le.PutUint32(buf[20:], uint32(filetype)) |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
func direntCache(fsc *sys.FSContext, fd int32) (*sys.DirentCache, experimentalsys.Errno) { |
| 1041 |
if f, ok := fsc.LookupFile(fd); !ok { |
| 1042 |
return nil, experimentalsys.EBADF |
| 1043 |
} else if dir, errno := f.DirentCache(); errno == 0 { |
| 1044 |
return dir, 0 |
| 1045 |
} else if errno == experimentalsys.ENOTDIR { |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
return nil, experimentalsys.EBADF |
| 1053 |
} else { |
| 1054 |
return nil, errno |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
var fdRenumber = newHostFunc(wasip1.FdRenumberName, fdRenumberFn, []wasm.ValueType{i32, i32}, "fd", "to") |
| 1063 |
|
| 1064 |
func fdRenumberFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1065 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1066 |
|
| 1067 |
from := int32(params[0]) |
| 1068 |
to := int32(params[1]) |
| 1069 |
|
| 1070 |
if errno := fsc.Renumber(from, to); errno != 0 { |
| 1071 |
return errno |
| 1072 |
} |
| 1073 |
return 0 |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
var fdSeek = newHostFunc( |
| 1115 |
wasip1.FdSeekName, fdSeekFn, |
| 1116 |
[]wasm.ValueType{i32, i64, i32, i32}, |
| 1117 |
"fd", "offset", "whence", "result.newoffset", |
| 1118 |
) |
| 1119 |
|
| 1120 |
func fdSeekFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1121 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1122 |
fd := int32(params[0]) |
| 1123 |
offset := params[1] |
| 1124 |
whence := uint32(params[2]) |
| 1125 |
resultNewoffset := uint32(params[3]) |
| 1126 |
|
| 1127 |
if f, ok := fsc.LookupFile(fd); !ok { |
| 1128 |
return experimentalsys.EBADF |
| 1129 |
} else if isDir, _ := f.File.IsDir(); isDir { |
| 1130 |
return experimentalsys.EISDIR |
| 1131 |
} else if newOffset, errno := f.File.Seek(int64(offset), int(whence)); errno != 0 { |
| 1132 |
return errno |
| 1133 |
} else if !mod.Memory().WriteUint64Le(resultNewoffset, uint64(newOffset)) { |
| 1134 |
return experimentalsys.EFAULT |
| 1135 |
} |
| 1136 |
return 0 |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
var fdSync = newHostFunc(wasip1.FdSyncName, fdSyncFn, []wasm.ValueType{i32}, "fd") |
| 1144 |
|
| 1145 |
func fdSyncFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1146 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1147 |
fd := int32(params[0]) |
| 1148 |
|
| 1149 |
|
| 1150 |
if f, ok := fsc.LookupFile(fd); !ok { |
| 1151 |
return experimentalsys.EBADF |
| 1152 |
} else { |
| 1153 |
return f.File.Sync() |
| 1154 |
} |
| 1155 |
} |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
var fdTell = newHostFunc(wasip1.FdTellName, fdTellFn, []wasm.ValueType{i32, i32}, "fd", "result.offset") |
| 1162 |
|
| 1163 |
func fdTellFn(ctx context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1164 |
fd := params[0] |
| 1165 |
offset := uint64(0) |
| 1166 |
whence := uint64(io.SeekCurrent) |
| 1167 |
resultNewoffset := params[1] |
| 1168 |
|
| 1169 |
fdSeekParams := []uint64{fd, offset, whence, resultNewoffset} |
| 1170 |
return fdSeekFn(ctx, mod, fdSeekParams) |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
var fdWrite = newHostFunc( |
| 1232 |
wasip1.FdWriteName, fdWriteFn, |
| 1233 |
[]wasm.ValueType{i32, i32, i32, i32}, |
| 1234 |
"fd", "iovs", "iovs_len", "result.nwritten", |
| 1235 |
) |
| 1236 |
|
| 1237 |
func fdWriteFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1238 |
return fdWriteOrPwrite(mod, params, false) |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
type pwriter struct { |
| 1243 |
f experimentalsys.File |
| 1244 |
offset int64 |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
func (w *pwriter) Write(buf []byte) (n int, errno experimentalsys.Errno) { |
| 1249 |
if len(buf) == 0 { |
| 1250 |
return 0, 0 |
| 1251 |
} |
| 1252 |
|
| 1253 |
n, err := w.f.Pwrite(buf, w.offset) |
| 1254 |
w.offset += int64(n) |
| 1255 |
return n, err |
| 1256 |
} |
| 1257 |
|
| 1258 |
func fdWriteOrPwrite(mod api.Module, params []uint64, isPwrite bool) experimentalsys.Errno { |
| 1259 |
mem := mod.Memory() |
| 1260 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1261 |
|
| 1262 |
fd := int32(params[0]) |
| 1263 |
iovs := uint32(params[1]) |
| 1264 |
iovsCount := uint32(params[2]) |
| 1265 |
|
| 1266 |
var resultNwritten uint32 |
| 1267 |
var writer func(buf []byte) (n int, errno experimentalsys.Errno) |
| 1268 |
if f, ok := fsc.LookupFile(fd); !ok { |
| 1269 |
return experimentalsys.EBADF |
| 1270 |
} else if isPwrite { |
| 1271 |
offset := int64(params[3]) |
| 1272 |
writer = (&pwriter{f: f.File, offset: offset}).Write |
| 1273 |
resultNwritten = uint32(params[4]) |
| 1274 |
} else { |
| 1275 |
writer = f.File.Write |
| 1276 |
resultNwritten = uint32(params[3]) |
| 1277 |
} |
| 1278 |
|
| 1279 |
nwritten, errno := writev(mem, iovs, iovsCount, writer) |
| 1280 |
if errno != 0 { |
| 1281 |
return errno |
| 1282 |
} |
| 1283 |
|
| 1284 |
if !mod.Memory().WriteUint32Le(resultNwritten, nwritten) { |
| 1285 |
return experimentalsys.EFAULT |
| 1286 |
} |
| 1287 |
return 0 |
| 1288 |
} |
| 1289 |
|
| 1290 |
func writev(mem api.Memory, iovs uint32, iovsCount uint32, writer func(buf []byte) (n int, errno experimentalsys.Errno)) (uint32, experimentalsys.Errno) { |
| 1291 |
var nwritten uint32 |
| 1292 |
iovsStop := iovsCount << 3 |
| 1293 |
iovsBuf, ok := mem.Read(iovs, iovsStop) |
| 1294 |
if !ok { |
| 1295 |
return 0, experimentalsys.EFAULT |
| 1296 |
} |
| 1297 |
|
| 1298 |
for iovsPos := uint32(0); iovsPos < iovsStop; iovsPos += 8 { |
| 1299 |
offset := le.Uint32(iovsBuf[iovsPos:]) |
| 1300 |
l := le.Uint32(iovsBuf[iovsPos+4:]) |
| 1301 |
|
| 1302 |
b, ok := mem.Read(offset, l) |
| 1303 |
if !ok { |
| 1304 |
return 0, experimentalsys.EFAULT |
| 1305 |
} |
| 1306 |
n, errno := writer(b) |
| 1307 |
nwritten += uint32(n) |
| 1308 |
if errno == experimentalsys.ENOSYS { |
| 1309 |
return 0, experimentalsys.EBADF |
| 1310 |
} else if errno != 0 { |
| 1311 |
return 0, errno |
| 1312 |
} |
| 1313 |
} |
| 1314 |
return nwritten, 0 |
| 1315 |
} |
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
var pathCreateDirectory = newHostFunc( |
| 1339 |
wasip1.PathCreateDirectoryName, pathCreateDirectoryFn, |
| 1340 |
[]wasm.ValueType{i32, i32, i32}, |
| 1341 |
"fd", "path", "path_len", |
| 1342 |
) |
| 1343 |
|
| 1344 |
func pathCreateDirectoryFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1345 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1346 |
|
| 1347 |
fd := int32(params[0]) |
| 1348 |
path := uint32(params[1]) |
| 1349 |
pathLen := uint32(params[2]) |
| 1350 |
|
| 1351 |
preopen, pathName, errno := atPath(fsc, mod.Memory(), fd, path, pathLen) |
| 1352 |
if errno != 0 { |
| 1353 |
return errno |
| 1354 |
} |
| 1355 |
|
| 1356 |
if errno = preopen.Mkdir(pathName, 0o700); errno != 0 { |
| 1357 |
return errno |
| 1358 |
} |
| 1359 |
|
| 1360 |
return 0 |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
var pathFilestatGet = newHostFunc( |
| 1392 |
wasip1.PathFilestatGetName, pathFilestatGetFn, |
| 1393 |
[]wasm.ValueType{i32, i32, i32, i32, i32}, |
| 1394 |
"fd", "flags", "path", "path_len", "result.filestat", |
| 1395 |
) |
| 1396 |
|
| 1397 |
func pathFilestatGetFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1398 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1399 |
|
| 1400 |
fd := int32(params[0]) |
| 1401 |
flags := uint16(params[1]) |
| 1402 |
path := uint32(params[2]) |
| 1403 |
pathLen := uint32(params[3]) |
| 1404 |
|
| 1405 |
preopen, pathName, errno := atPath(fsc, mod.Memory(), fd, path, pathLen) |
| 1406 |
if errno != 0 { |
| 1407 |
return errno |
| 1408 |
} |
| 1409 |
|
| 1410 |
|
| 1411 |
var st sysapi.Stat_t |
| 1412 |
|
| 1413 |
if (flags & wasip1.LOOKUP_SYMLINK_FOLLOW) == 0 { |
| 1414 |
st, errno = preopen.Lstat(pathName) |
| 1415 |
} else { |
| 1416 |
st, errno = preopen.Stat(pathName) |
| 1417 |
} |
| 1418 |
if errno != 0 { |
| 1419 |
return errno |
| 1420 |
} |
| 1421 |
|
| 1422 |
|
| 1423 |
resultBuf := uint32(params[4]) |
| 1424 |
buf, ok := mod.Memory().Read(resultBuf, 64) |
| 1425 |
if !ok { |
| 1426 |
return experimentalsys.EFAULT |
| 1427 |
} |
| 1428 |
|
| 1429 |
filetype := getWasiFiletype(st.Mode) |
| 1430 |
return writeFilestat(buf, &st, filetype) |
| 1431 |
} |
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
var pathFilestatSetTimes = newHostFunc( |
| 1438 |
wasip1.PathFilestatSetTimesName, pathFilestatSetTimesFn, |
| 1439 |
[]wasm.ValueType{i32, i32, i32, i32, i64, i64, i32}, |
| 1440 |
"fd", "flags", "path", "path_len", "atim", "mtim", "fst_flags", |
| 1441 |
) |
| 1442 |
|
| 1443 |
func pathFilestatSetTimesFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1444 |
fd := int32(params[0]) |
| 1445 |
flags := uint16(params[1]) |
| 1446 |
path := uint32(params[2]) |
| 1447 |
pathLen := uint32(params[3]) |
| 1448 |
atim := int64(params[4]) |
| 1449 |
mtim := int64(params[5]) |
| 1450 |
fstFlags := uint16(params[6]) |
| 1451 |
|
| 1452 |
sys := mod.(*wasm.ModuleInstance).Sys |
| 1453 |
fsc := sys.FS() |
| 1454 |
|
| 1455 |
atim, mtim, errno := toTimes(sys.WalltimeNanos, atim, mtim, fstFlags) |
| 1456 |
if errno != 0 { |
| 1457 |
return errno |
| 1458 |
} |
| 1459 |
|
| 1460 |
preopen, pathName, errno := atPath(fsc, mod.Memory(), fd, path, pathLen) |
| 1461 |
if errno != 0 { |
| 1462 |
return errno |
| 1463 |
} |
| 1464 |
|
| 1465 |
symlinkFollow := flags&wasip1.LOOKUP_SYMLINK_FOLLOW != 0 |
| 1466 |
if symlinkFollow { |
| 1467 |
return preopen.Utimens(pathName, atim, mtim) |
| 1468 |
} |
| 1469 |
|
| 1470 |
if f, errno := preopen.OpenFile(pathName, experimentalsys.O_WRONLY, 0); errno != 0 { |
| 1471 |
return errno |
| 1472 |
} else { |
| 1473 |
defer f.Close() |
| 1474 |
return f.Utimens(atim, mtim) |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
var pathLink = newHostFunc( |
| 1483 |
wasip1.PathLinkName, pathLinkFn, |
| 1484 |
[]wasm.ValueType{i32, i32, i32, i32, i32, i32, i32}, |
| 1485 |
"old_fd", "old_flags", "old_path", "old_path_len", "new_fd", "new_path", "new_path_len", |
| 1486 |
) |
| 1487 |
|
| 1488 |
func pathLinkFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1489 |
mem := mod.Memory() |
| 1490 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1491 |
|
| 1492 |
oldFD := int32(params[0]) |
| 1493 |
|
| 1494 |
_ = uint32(params[1]) |
| 1495 |
oldPath := uint32(params[2]) |
| 1496 |
oldPathLen := uint32(params[3]) |
| 1497 |
|
| 1498 |
oldFS, oldName, errno := atPath(fsc, mem, oldFD, oldPath, oldPathLen) |
| 1499 |
if errno != 0 { |
| 1500 |
return errno |
| 1501 |
} |
| 1502 |
|
| 1503 |
newFD := int32(params[4]) |
| 1504 |
newPath := uint32(params[5]) |
| 1505 |
newPathLen := uint32(params[6]) |
| 1506 |
|
| 1507 |
newFS, newName, errno := atPath(fsc, mem, newFD, newPath, newPathLen) |
| 1508 |
if errno != 0 { |
| 1509 |
return errno |
| 1510 |
} |
| 1511 |
|
| 1512 |
if oldFS != newFS { |
| 1513 |
return experimentalsys.ENOSYS |
| 1514 |
} |
| 1515 |
|
| 1516 |
return oldFS.Link(oldName, newName) |
| 1517 |
} |
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
var pathOpen = newHostFunc( |
| 1573 |
wasip1.PathOpenName, pathOpenFn, |
| 1574 |
[]wasm.ValueType{i32, i32, i32, i32, i32, i64, i64, i32, i32}, |
| 1575 |
"fd", "dirflags", "path", "path_len", "oflags", "fs_rights_base", "fs_rights_inheriting", "fdflags", "result.opened_fd", |
| 1576 |
) |
| 1577 |
|
| 1578 |
func pathOpenFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1579 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1580 |
|
| 1581 |
preopenFD := int32(params[0]) |
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
dirflags := uint16(params[1]) |
| 1586 |
|
| 1587 |
path := uint32(params[2]) |
| 1588 |
pathLen := uint32(params[3]) |
| 1589 |
|
| 1590 |
oflags := uint16(params[4]) |
| 1591 |
|
| 1592 |
rights := uint32(params[5]) |
| 1593 |
|
| 1594 |
_ = params[6] |
| 1595 |
|
| 1596 |
fdflags := uint16(params[7]) |
| 1597 |
resultOpenedFD := uint32(params[8]) |
| 1598 |
|
| 1599 |
preopen, pathName, errno := atPath(fsc, mod.Memory(), preopenFD, path, pathLen) |
| 1600 |
if errno != 0 { |
| 1601 |
return errno |
| 1602 |
} |
| 1603 |
|
| 1604 |
if pathLen == 0 { |
| 1605 |
return experimentalsys.EINVAL |
| 1606 |
} |
| 1607 |
|
| 1608 |
fileOpenFlags := openFlags(dirflags, oflags, fdflags, rights) |
| 1609 |
isDir := fileOpenFlags&experimentalsys.O_DIRECTORY != 0 |
| 1610 |
|
| 1611 |
if isDir && oflags&wasip1.O_CREAT != 0 { |
| 1612 |
return experimentalsys.EINVAL |
| 1613 |
} |
| 1614 |
|
| 1615 |
newFD, errno := fsc.OpenFile(preopen, pathName, fileOpenFlags, 0o600) |
| 1616 |
if errno != 0 { |
| 1617 |
return errno |
| 1618 |
} |
| 1619 |
|
| 1620 |
|
| 1621 |
if isDir { |
| 1622 |
if f, ok := fsc.LookupFile(newFD); !ok { |
| 1623 |
return experimentalsys.EBADF |
| 1624 |
} else if isDir, errno := f.File.IsDir(); errno != 0 { |
| 1625 |
_ = fsc.CloseFile(newFD) |
| 1626 |
return errno |
| 1627 |
} else if !isDir { |
| 1628 |
_ = fsc.CloseFile(newFD) |
| 1629 |
return experimentalsys.ENOTDIR |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
if !mod.Memory().WriteUint32Le(resultOpenedFD, uint32(newFD)) { |
| 1634 |
_ = fsc.CloseFile(newFD) |
| 1635 |
return experimentalsys.EFAULT |
| 1636 |
} |
| 1637 |
return 0 |
| 1638 |
} |
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
func atPath(fsc *sys.FSContext, mem api.Memory, fd int32, p, pathLen uint32) (experimentalsys.FS, string, experimentalsys.Errno) { |
| 1657 |
b, ok := mem.Read(p, pathLen) |
| 1658 |
if !ok { |
| 1659 |
return nil, "", experimentalsys.EFAULT |
| 1660 |
} |
| 1661 |
pathName := string(b) |
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
hasTrailingSlash := strings.HasSuffix(pathName, "/") |
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
pathName = path.Clean(pathName) |
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
if !fs.ValidPath(pathName) { |
| 1674 |
return nil, "", experimentalsys.EPERM |
| 1675 |
} |
| 1676 |
|
| 1677 |
|
| 1678 |
if hasTrailingSlash { |
| 1679 |
pathName = pathName + "/" |
| 1680 |
} |
| 1681 |
|
| 1682 |
if f, ok := fsc.LookupFile(fd); !ok { |
| 1683 |
return nil, "", experimentalsys.EBADF |
| 1684 |
} else if isDir, errno := f.File.IsDir(); errno != 0 { |
| 1685 |
return nil, "", errno |
| 1686 |
} else if !isDir { |
| 1687 |
return nil, "", experimentalsys.ENOTDIR |
| 1688 |
} else if f.IsPreopen { |
| 1689 |
return f.FS, pathName, 0 |
| 1690 |
} else { |
| 1691 |
|
| 1692 |
return f.FS, f.Name + "/" + pathName, 0 |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
func preopenPath(fsc *sys.FSContext, fd int32) (string, experimentalsys.Errno) { |
| 1697 |
if f, ok := fsc.LookupFile(fd); !ok { |
| 1698 |
return "", experimentalsys.EBADF |
| 1699 |
} else if !f.IsPreopen { |
| 1700 |
return "", experimentalsys.EBADF |
| 1701 |
} else if isDir, errno := f.File.IsDir(); errno != 0 || !isDir { |
| 1702 |
|
| 1703 |
|
| 1704 |
return "", errno |
| 1705 |
} else { |
| 1706 |
return f.Name, 0 |
| 1707 |
} |
| 1708 |
} |
| 1709 |
|
| 1710 |
func openFlags(dirflags, oflags, fdflags uint16, rights uint32) (openFlags experimentalsys.Oflag) { |
| 1711 |
if dirflags&wasip1.LOOKUP_SYMLINK_FOLLOW == 0 { |
| 1712 |
openFlags |= experimentalsys.O_NOFOLLOW |
| 1713 |
} |
| 1714 |
if oflags&wasip1.O_DIRECTORY != 0 { |
| 1715 |
openFlags |= experimentalsys.O_DIRECTORY |
| 1716 |
} else if oflags&wasip1.O_EXCL != 0 { |
| 1717 |
openFlags |= experimentalsys.O_EXCL |
| 1718 |
} |
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
defaultMode := experimentalsys.O_RDONLY |
| 1727 |
if oflags&wasip1.O_TRUNC != 0 { |
| 1728 |
openFlags |= experimentalsys.O_TRUNC |
| 1729 |
defaultMode = experimentalsys.O_RDWR |
| 1730 |
} |
| 1731 |
if oflags&wasip1.O_CREAT != 0 { |
| 1732 |
openFlags |= experimentalsys.O_CREAT |
| 1733 |
defaultMode = experimentalsys.O_RDWR |
| 1734 |
} |
| 1735 |
if fdflags&wasip1.FD_NONBLOCK != 0 { |
| 1736 |
openFlags |= experimentalsys.O_NONBLOCK |
| 1737 |
} |
| 1738 |
if fdflags&wasip1.FD_APPEND != 0 { |
| 1739 |
openFlags |= experimentalsys.O_APPEND |
| 1740 |
defaultMode = experimentalsys.O_RDWR |
| 1741 |
} |
| 1742 |
if fdflags&wasip1.FD_DSYNC != 0 { |
| 1743 |
openFlags |= experimentalsys.O_DSYNC |
| 1744 |
} |
| 1745 |
if fdflags&wasip1.FD_RSYNC != 0 { |
| 1746 |
openFlags |= experimentalsys.O_RSYNC |
| 1747 |
} |
| 1748 |
if fdflags&wasip1.FD_SYNC != 0 { |
| 1749 |
openFlags |= experimentalsys.O_SYNC |
| 1750 |
} |
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
const r = wasip1.RIGHT_FD_READ |
| 1757 |
const w = wasip1.RIGHT_FD_WRITE |
| 1758 |
const rw = r | w |
| 1759 |
switch { |
| 1760 |
case (rights & rw) == rw: |
| 1761 |
openFlags |= experimentalsys.O_RDWR |
| 1762 |
case (rights & w) == w: |
| 1763 |
openFlags |= experimentalsys.O_WRONLY |
| 1764 |
case (rights & r) == r: |
| 1765 |
openFlags |= experimentalsys.O_RDONLY |
| 1766 |
default: |
| 1767 |
openFlags |= defaultMode |
| 1768 |
} |
| 1769 |
return |
| 1770 |
} |
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
var pathReadlink = newHostFunc( |
| 1777 |
wasip1.PathReadlinkName, pathReadlinkFn, |
| 1778 |
[]wasm.ValueType{i32, i32, i32, i32, i32, i32}, |
| 1779 |
"fd", "path", "path_len", "buf", "buf_len", "result.bufused", |
| 1780 |
) |
| 1781 |
|
| 1782 |
func pathReadlinkFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1783 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1784 |
|
| 1785 |
fd := int32(params[0]) |
| 1786 |
path := uint32(params[1]) |
| 1787 |
pathLen := uint32(params[2]) |
| 1788 |
buf := uint32(params[3]) |
| 1789 |
bufLen := uint32(params[4]) |
| 1790 |
resultBufused := uint32(params[5]) |
| 1791 |
|
| 1792 |
if pathLen == 0 || bufLen == 0 { |
| 1793 |
return experimentalsys.EINVAL |
| 1794 |
} |
| 1795 |
|
| 1796 |
mem := mod.Memory() |
| 1797 |
preopen, p, errno := atPath(fsc, mem, fd, path, pathLen) |
| 1798 |
if errno != 0 { |
| 1799 |
return errno |
| 1800 |
} |
| 1801 |
|
| 1802 |
dst, errno := preopen.Readlink(p) |
| 1803 |
if errno != 0 { |
| 1804 |
return errno |
| 1805 |
} |
| 1806 |
|
| 1807 |
if len(dst) > int(bufLen) { |
| 1808 |
return experimentalsys.ERANGE |
| 1809 |
} |
| 1810 |
|
| 1811 |
if ok := mem.WriteString(buf, dst); !ok { |
| 1812 |
return experimentalsys.EFAULT |
| 1813 |
} |
| 1814 |
|
| 1815 |
if !mem.WriteUint32Le(resultBufused, uint32(len(dst))) { |
| 1816 |
return experimentalsys.EFAULT |
| 1817 |
} |
| 1818 |
return 0 |
| 1819 |
} |
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
var pathRemoveDirectory = newHostFunc( |
| 1844 |
wasip1.PathRemoveDirectoryName, pathRemoveDirectoryFn, |
| 1845 |
[]wasm.ValueType{i32, i32, i32}, |
| 1846 |
"fd", "path", "path_len", |
| 1847 |
) |
| 1848 |
|
| 1849 |
func pathRemoveDirectoryFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1850 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1851 |
|
| 1852 |
fd := int32(params[0]) |
| 1853 |
path := uint32(params[1]) |
| 1854 |
pathLen := uint32(params[2]) |
| 1855 |
|
| 1856 |
preopen, pathName, errno := atPath(fsc, mod.Memory(), fd, path, pathLen) |
| 1857 |
if errno != 0 { |
| 1858 |
return errno |
| 1859 |
} |
| 1860 |
|
| 1861 |
return preopen.Rmdir(pathName) |
| 1862 |
} |
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
var pathRename = newHostFunc( |
| 1890 |
wasip1.PathRenameName, pathRenameFn, |
| 1891 |
[]wasm.ValueType{i32, i32, i32, i32, i32, i32}, |
| 1892 |
"fd", "old_path", "old_path_len", "new_fd", "new_path", "new_path_len", |
| 1893 |
) |
| 1894 |
|
| 1895 |
func pathRenameFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1896 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1897 |
|
| 1898 |
fd := int32(params[0]) |
| 1899 |
oldPath := uint32(params[1]) |
| 1900 |
oldPathLen := uint32(params[2]) |
| 1901 |
|
| 1902 |
newFD := int32(params[3]) |
| 1903 |
newPath := uint32(params[4]) |
| 1904 |
newPathLen := uint32(params[5]) |
| 1905 |
|
| 1906 |
oldFS, oldPathName, errno := atPath(fsc, mod.Memory(), fd, oldPath, oldPathLen) |
| 1907 |
if errno != 0 { |
| 1908 |
return errno |
| 1909 |
} |
| 1910 |
|
| 1911 |
newFS, newPathName, errno := atPath(fsc, mod.Memory(), newFD, newPath, newPathLen) |
| 1912 |
if errno != 0 { |
| 1913 |
return errno |
| 1914 |
} |
| 1915 |
|
| 1916 |
if oldFS != newFS { |
| 1917 |
return experimentalsys.ENOSYS |
| 1918 |
} |
| 1919 |
|
| 1920 |
return oldFS.Rename(oldPathName, newPathName) |
| 1921 |
} |
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
var pathSymlink = newHostFunc( |
| 1928 |
wasip1.PathSymlinkName, pathSymlinkFn, |
| 1929 |
[]wasm.ValueType{i32, i32, i32, i32, i32}, |
| 1930 |
"old_path", "old_path_len", "fd", "new_path", "new_path_len", |
| 1931 |
) |
| 1932 |
|
| 1933 |
func pathSymlinkFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 1934 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 1935 |
|
| 1936 |
oldPath := uint32(params[0]) |
| 1937 |
oldPathLen := uint32(params[1]) |
| 1938 |
fd := int32(params[2]) |
| 1939 |
newPath := uint32(params[3]) |
| 1940 |
newPathLen := uint32(params[4]) |
| 1941 |
|
| 1942 |
mem := mod.Memory() |
| 1943 |
|
| 1944 |
dir, ok := fsc.LookupFile(fd) |
| 1945 |
if !ok { |
| 1946 |
return experimentalsys.EBADF |
| 1947 |
} else if isDir, errno := dir.File.IsDir(); errno != 0 { |
| 1948 |
return errno |
| 1949 |
} else if !isDir { |
| 1950 |
return experimentalsys.ENOTDIR |
| 1951 |
} |
| 1952 |
|
| 1953 |
if oldPathLen == 0 || newPathLen == 0 { |
| 1954 |
return experimentalsys.EINVAL |
| 1955 |
} |
| 1956 |
|
| 1957 |
oldPathBuf, ok := mem.Read(oldPath, oldPathLen) |
| 1958 |
if !ok { |
| 1959 |
return experimentalsys.EFAULT |
| 1960 |
} |
| 1961 |
|
| 1962 |
_, newPathName, errno := atPath(fsc, mod.Memory(), fd, newPath, newPathLen) |
| 1963 |
if errno != 0 { |
| 1964 |
return errno |
| 1965 |
} |
| 1966 |
|
| 1967 |
return dir.FS.Symlink( |
| 1968 |
|
| 1969 |
|
| 1970 |
unsafe.String(&oldPathBuf[0], int(oldPathLen)), |
| 1971 |
newPathName, |
| 1972 |
) |
| 1973 |
} |
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
var pathUnlinkFile = newHostFunc( |
| 1997 |
wasip1.PathUnlinkFileName, pathUnlinkFileFn, |
| 1998 |
[]wasm.ValueType{i32, i32, i32}, |
| 1999 |
"fd", "path", "path_len", |
| 2000 |
) |
| 2001 |
|
| 2002 |
func pathUnlinkFileFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { |
| 2003 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 2004 |
|
| 2005 |
fd := int32(params[0]) |
| 2006 |
path := uint32(params[1]) |
| 2007 |
pathLen := uint32(params[2]) |
| 2008 |
|
| 2009 |
preopen, pathName, errno := atPath(fsc, mod.Memory(), fd, path, pathLen) |
| 2010 |
if errno != 0 { |
| 2011 |
return errno |
| 2012 |
} |
| 2013 |
|
| 2014 |
return preopen.Unlink(pathName) |
| 2015 |
} |