]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: fix Fchdir on js/wasm
authorRichard Musiol <mail@richard-musiol.de>
Sun, 1 Mar 2020 16:01:58 +0000 (17:01 +0100)
committerRichard Musiol <neelance@gmail.com>
Sun, 1 Mar 2020 21:02:40 +0000 (21:02 +0000)
NodeJS does not support fchdir so it has to be emulated with chdir by
saving the path when opening a directory.

However, if the path opened is relative, saving this path is not
sufficient, because after changing the working directory the path
does not resolve correctly any more, thus a subsequent fd.Chdir() fails.

This change fixes the issue by resolving a relative path when
opening the directory and saving the absolute path instead.

Fixes #37448

Change-Id: Id6bc8c4232b0019fc11e850599a526336608ce54
Reviewed-on: https://go-review.googlesource.com/c/go/+/221717
Run-TryBot: Richard Musiol <neelance@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
src/os/os_test.go
src/syscall/fs_js.go

index 1d8442d8083e2fa76d49249ed6850278f7c1cef7..cc03b91d72a3c711d0c7335cffbabd45816ca9d2 100644 (file)
@@ -1242,6 +1242,41 @@ func testChtimes(t *testing.T, name string) {
        }
 }
 
+func TestFileChdir(t *testing.T) {
+       // TODO(brainman): file.Chdir() is not implemented on windows.
+       if runtime.GOOS == "windows" {
+               return
+       }
+
+       wd, err := Getwd()
+       if err != nil {
+               t.Fatalf("Getwd: %s", err)
+       }
+       defer Chdir(wd)
+
+       fd, err := Open(".")
+       if err != nil {
+               t.Fatalf("Open .: %s", err)
+       }
+       defer fd.Close()
+
+       if err := Chdir("/"); err != nil {
+               t.Fatalf("Chdir /: %s", err)
+       }
+
+       if err := fd.Chdir(); err != nil {
+               t.Fatalf("fd.Chdir: %s", err)
+       }
+
+       wdNew, err := Getwd()
+       if err != nil {
+               t.Fatalf("Getwd: %s", err)
+       }
+       if wdNew != wd {
+               t.Fatalf("fd.Chdir failed, got %s, want %s", wdNew, wd)
+       }
+}
+
 func TestChdirAndGetwd(t *testing.T) {
        // TODO(brainman): file.Chdir() is not implemented on windows.
        if runtime.GOOS == "windows" {
index c1cac97d91b25578a6c22928693f7ef519c55ff5..262ec28afdd2a087275303f0cf3626a4a7443fbe 100644 (file)
@@ -102,6 +102,10 @@ func Open(path string, openmode int, perm uint32) (int, error) {
                }
        }
 
+       if path[0] != '/' {
+               cwd := jsProcess.Call("cwd").String()
+               path = cwd + "/" + path
+       }
        f := &jsFile{
                path:    path,
                entries: entries,