This adds `gojs.WithOSUser` which passes through current user IDs so that GOOS=js compiled wasm can read them. This also adds support for reading back the uid and gid on files. In summary, this passes `os.TestChown` except on windows where it will not work due to lack of support. Signed-off-by: Adrian Cole <adrian@tetrate.io>
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package fs
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
func Main() {
|
|
testAdHoc()
|
|
}
|
|
|
|
func testAdHoc() {
|
|
// Ensure stat works, particularly mode.
|
|
for _, path := range []string{"sub", "/animals.txt", "animals.txt"} {
|
|
if stat, err := os.Stat(path); err != nil {
|
|
log.Panicln(err)
|
|
} else {
|
|
fmt.Println(path, "mode", stat.Mode())
|
|
}
|
|
}
|
|
|
|
// Read the full contents of the file using io.Reader
|
|
b, err := os.ReadFile("/animals.txt")
|
|
if err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
fmt.Println("contents:", string(b))
|
|
|
|
// Re-open the same file to test io.ReaderAt
|
|
f, err := os.Open("/animals.txt")
|
|
if err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
defer f.Close()
|
|
|
|
// Seek to an arbitrary position.
|
|
if _, err = f.Seek(4, io.SeekStart); err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
|
|
b1 := make([]byte, len(b))
|
|
// We expect to revert to the original position on ReadAt zero.
|
|
if _, err = f.ReadAt(b1, 0); err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
|
|
if !bytes.Equal(b, b1) {
|
|
log.Panicln("unexpected ReadAt contents: ", string(b1))
|
|
}
|
|
|
|
b, err = os.ReadFile("/empty.txt")
|
|
if err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
fmt.Println("empty:" + string(b))
|
|
}
|