Skip to content

Commit e12c4ce

Browse files
committed
Fix build for GOARCH=wasm GOOS=js GOOS=wasip1
Fixes the following build failures: GOOS=js GOARCH=wasm go build ./... GOOS=wasip1 GOARCH=wasm go build ./... Depends on: dgraph-io/ristretto#375 Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent 7f657f8 commit e12c4ce

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

dir_other.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//go:build js || wasip1
2+
// +build js wasip1
3+
4+
/*
5+
* Copyright 2017 Dgraph Labs, Inc. and Contributors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package badger
21+
22+
import (
23+
"fmt"
24+
"os"
25+
"path/filepath"
26+
27+
"github.com/dgraph-io/badger/v4/y"
28+
)
29+
30+
// directoryLockGuard holds a lock on a directory and a pid file inside. The pid file isn't part
31+
// of the locking mechanism, it's just advisory.
32+
type directoryLockGuard struct {
33+
// File handle on the directory, which we've flocked.
34+
f *os.File
35+
// The absolute path to our pid file.
36+
path string
37+
// Was this a shared lock for a read-only database?
38+
readOnly bool
39+
}
40+
41+
// acquireDirectoryLock gets a lock on the directory (using flock). If
42+
// this is not read-only, it will also write our pid to
43+
// dirPath/pidFileName for convenience.
44+
func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) (
45+
*directoryLockGuard, error) {
46+
// Convert to absolute path so that Release still works even if we do an unbalanced
47+
// chdir in the meantime.
48+
absPidFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))
49+
if err != nil {
50+
return nil, y.Wrapf(err, "cannot get absolute path for pid lock file")
51+
}
52+
f, err := os.Open(dirPath)
53+
if err != nil {
54+
return nil, y.Wrapf(err, "cannot open directory %q", dirPath)
55+
}
56+
57+
// NOTE: Here is where we would normally call flock.
58+
// This is not supported in js / wasm, so skip it.
59+
60+
if !readOnly {
61+
// Yes, we happily overwrite a pre-existing pid file. We're the
62+
// only read-write badger process using this directory.
63+
err = os.WriteFile(absPidFilePath, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666)
64+
if err != nil {
65+
f.Close()
66+
return nil, y.Wrapf(err,
67+
"Cannot write pid file %q", absPidFilePath)
68+
}
69+
}
70+
71+
return &directoryLockGuard{f, absPidFilePath, readOnly}, nil
72+
}
73+
74+
// Release deletes the pid file and releases our lock on the directory.
75+
func (guard *directoryLockGuard) release() error {
76+
var err error
77+
if !guard.readOnly {
78+
// It's important that we remove the pid file first.
79+
err = os.Remove(guard.path)
80+
}
81+
82+
if closeErr := guard.f.Close(); err == nil {
83+
err = closeErr
84+
}
85+
guard.path = ""
86+
guard.f = nil
87+
88+
return err
89+
}
90+
91+
// openDir opens a directory for syncing.
92+
func openDir(path string) (*os.File, error) { return os.Open(path) }
93+
94+
// When you create or delete a file, you have to ensure the directory entry for the file is synced
95+
// in order to guarantee the file is visible (if the system crashes). (See the man page for fsync,
96+
// or see https://github.com/coreos/etcd/issues/6368 for an example.)
97+
func syncDir(dir string) error {
98+
f, err := openDir(dir)
99+
if err != nil {
100+
return y.Wrapf(err, "While opening directory: %s.", dir)
101+
}
102+
103+
err = f.Sync()
104+
closeErr := f.Close()
105+
if err != nil {
106+
return y.Wrapf(err, "While syncing directory: %s.", dir)
107+
}
108+
return y.Wrapf(closeErr, "While closing directory: %s.", dir)
109+
}

dir_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !windows && !plan9
2-
// +build !windows,!plan9
1+
//go:build !windows && !plan9 && !js && !wasip1
2+
// +build !windows,!plan9,!js,!wasip1
33

44
/*
55
* Copyright 2017 Dgraph Labs, Inc. and Contributors

y/file_dsync.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !dragonfly && !freebsd && !windows && !plan9
2-
// +build !dragonfly,!freebsd,!windows,!plan9
1+
//go:build !dragonfly && !freebsd && !windows && !plan9 && !js && !wasip1
2+
// +build !dragonfly,!freebsd,!windows,!plan9,!js,!wasip1
33

44
/*
55
* Copyright 2017 Dgraph Labs, Inc. and Contributors

0 commit comments

Comments
 (0)