英文:
Golang to wasm compilation using tinygo. Execution using wasmtime
问题
我有以下测试代码:
func main() {
path, err := os.Getwd()
log.Println(path, err)
files, err := ioutil.ReadDir("/etc/dse")
log.Println(files, err)
}
我使用以下命令将其编译为 wasm:
tinygo build -target wasi -o list.wasm list.go
然后我使用以下命令执行它:
wasmtime list.wasm
输出结果为:
2023/05/05 16:00:50 / <nil>
2023/05/05 16:00:50 [] open /etc/dse: errno 76
同时,目录 /etc/dse 存在且具有权限 777。
这个错误的原因是什么,如何修复它?
英文:
I have the following test code
func main() {
path, err := os.Getwd()
log.Println(path, err)
files, err := ioutil.ReadDir("/etc/dse")
log.Println(files, err)
}
I compile it to wasm using
tinygo build -target wasi -o list.wasm list.go
Then I executeit using
wasmtime list.wasm
The output is
2023/05/05 16:00:50 / <nil>
2023/05/05 16:00:50 [] open /etc/dse: errno 76
At the same time, the directory /etc/dse exists and has permissions 777.
What is the source of this error and how to fix this?
答案1
得分: 1
errno 76
表示 capabilities insufficient
。
你需要给它提供访问所需目录中的文件的能力,使用 wasmtime
的 --dir
选项(参见 wasmtime WASI 教程):
> --dir=
选项指示 wasmtime 预打开一个目录,并将其作为能力提供给程序,以便在该目录中打开文件。
$ wasmtime --dir=/etc/dse list.wasm
2023/05/06 01:36:31 / <nil>
2023/05/06 01:36:31 [] readdir unimplemented : errno 54
不幸的是,TinyGo 中尚未实现 readdir
。
参考资料:
英文:
errno 76
means capabilities insufficient
.
You have to give it capabilities to access files in the requisite directories, with the wasmtime
option --dir
(see wasmtime WASI tutorial):
> The --dir=
option instructs wasmtime to preopen a directory, and make it available to the program as a capability which can be used to open files inside that directory.
$ wasmtime --dir=/etc/dse list.wasm
2023/05/06 01:36:31 / <nil>
2023/05/06 01:36:31 [] readdir unimplemented : errno 54
Unfortunately, readdir
is not implemented in TinyGo yet.
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论