-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wasi] Update wasi runtime to use preopens properly
- Loading branch information
Showing
5 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2022 Ben L. Titzer. All rights reserved. | ||
// See LICENSE for details of Apache 2.0 license. | ||
|
||
def main(args: Array<string>) -> int { | ||
for (i = 0; i < args.length; i++) { | ||
var data = System.fileLoad(args[i]); | ||
if (data == null) { | ||
System.puts("cat: "); | ||
System.puts(args[i]); | ||
System.puts(": no such file or directory\n"); | ||
} else { | ||
System.puts(data); | ||
} | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Foobartext | ||
Foobartext2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2024 Virgil authors. All rights reserved. | ||
// See LICENSE for details of Apache 2.0 license. | ||
|
||
def FIRST_PREOPEN = 3; | ||
def MAX_PREOPENS = 10; | ||
var preopenCount = -1; | ||
var relativeFd = -1; | ||
var preopens = Array<(string, int)>.new(MAX_PREOPENS); | ||
def buf = Array<int>.new(2); | ||
|
||
// Scans the preopens and populates the array of (name, fd) pairs. | ||
def init() { | ||
preopenCount = 0; | ||
for (i = FIRST_PREOPEN; i < (FIRST_PREOPEN + MAX_PREOPENS); i++) { | ||
var r = wasi_snapshot_preview1.fd_prestat_get(i, Pointer.atContents(buf)); | ||
if (r != 0) break; | ||
var len = buf[1]; | ||
if (len == 0) break; | ||
var name = Array<byte>.new(len); | ||
r = wasi_snapshot_preview1.fd_prestat_dir_name(i, Pointer.atContents(name), name.length); | ||
if (r != 0) break; | ||
preopens[preopenCount++] = (name, i); | ||
if (len == 1 && name[0] == '.') relativeFd = i; | ||
} | ||
} | ||
|
||
// Lazily scans WASI pre-opened directories and matches paths to preopens. | ||
component WasiPreopens { | ||
def find(path: string) -> int { | ||
if (path.length == 0) return -1; | ||
if (preopenCount < 0) init(); | ||
// If the path is not absolute and there is a '.' preopened, use that. | ||
if (path[0] != '/' && relativeFd >= 0) return relativeFd; | ||
// Search for an absolute or relative match | ||
for (i < preopenCount) { | ||
var t = preopens[i]; | ||
if (checkPrefix(t.0, path)) return t.1; | ||
} | ||
return -1; | ||
} | ||
} | ||
def checkPrefix(prefix: string, path: string) -> bool { | ||
if (prefix.length > path.length) return false; | ||
for (i < prefix.length) { | ||
if (path[i] != prefix[i]) return false; | ||
} | ||
if (prefix[prefix.length - 1] == '/') return true; | ||
if (path.length > prefix.length && path[prefix.length] == '/') return true; | ||
return false; | ||
} |