Skip to content

Commit ad8b330

Browse files
Zxillygopherbot
authored andcommitted
syscall: disable O_DIRECTORY on Windows for js/wasm
O_DIRECTORY is not available on all platforms, as described at https://nodejs.org/docs/latest/api/fs.html#file-open-constants . On Windows, only O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY, and UV_FS_O_FILEMAP are available. Fixes #71758 Change-Id: Iacc890ba9a30dcd75eb746ec324fa0c3e368048e GitHub-Last-Rev: a0160e8 GitHub-Pull-Request: #71770 Reviewed-on: https://go-review.googlesource.com/c/go/+/650015 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> TryBot-Bypass: Dmitri Shuralyov <[email protected]>
1 parent 0bdc792 commit ad8b330

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/syscall/fs_js.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,26 @@ var constants = jsFS.Get("constants")
2323
var uint8Array = js.Global().Get("Uint8Array")
2424

2525
var (
26-
nodeWRONLY = constants.Get("O_WRONLY").Int()
27-
nodeRDWR = constants.Get("O_RDWR").Int()
28-
nodeCREATE = constants.Get("O_CREAT").Int()
29-
nodeTRUNC = constants.Get("O_TRUNC").Int()
30-
nodeAPPEND = constants.Get("O_APPEND").Int()
31-
nodeEXCL = constants.Get("O_EXCL").Int()
32-
nodeDIRECTORY = constants.Get("O_DIRECTORY").Int()
26+
nodeWRONLY = constants.Get("O_WRONLY").Int()
27+
nodeRDWR = constants.Get("O_RDWR").Int()
28+
nodeCREATE = constants.Get("O_CREAT").Int()
29+
nodeTRUNC = constants.Get("O_TRUNC").Int()
30+
nodeAPPEND = constants.Get("O_APPEND").Int()
31+
nodeEXCL = constants.Get("O_EXCL").Int()
32+
33+
// NodeJS on Windows does not support O_DIRECTORY, so we default
34+
// to -1 and assign it in init if available.
35+
// See https://nodejs.org/docs/latest/api/fs.html#file-open-constants.
36+
nodeDIRECTORY = -1
3337
)
3438

39+
func init() {
40+
oDir := constants.Get("O_DIRECTORY")
41+
if !oDir.IsUndefined() {
42+
nodeDIRECTORY = oDir.Int()
43+
}
44+
}
45+
3546
type jsFile struct {
3647
path string
3748
entries []string
@@ -85,7 +96,11 @@ func Open(path string, openmode int, perm uint32) (int, error) {
8596
return 0, errors.New("syscall.Open: O_SYNC is not supported by js/wasm")
8697
}
8798
if openmode&O_DIRECTORY != 0 {
88-
flags |= nodeDIRECTORY
99+
if nodeDIRECTORY != -1 {
100+
flags |= nodeDIRECTORY
101+
} else {
102+
return 0, errors.New("syscall.Open: O_DIRECTORY is not supported on Windows")
103+
}
89104
}
90105

91106
jsFD, err := fsCall("open", path, flags, perm)

0 commit comments

Comments
 (0)