Skip to content

Commit 1956019

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 de3932e commit 1956019

File tree

4 files changed

+116
-4
lines changed

4 files changed

+116
-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

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1212
github.com/dgraph-io/ristretto v1.0.0 h1:SYG07bONKMlFDUYu5pEu3DGAh8c2OFNzKm6G9J4Si84=
1313
github.com/dgraph-io/ristretto v1.0.0/go.mod h1:jTi2FiYEhQ1NsMmA7DeBykizjOuY88NhKBkepyu1jPc=
1414
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WAFKLNi6ZS0675eEUC9y3AlwSbQu1Y=
15+
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
1516
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
1617
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
1718
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -44,6 +45,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
4445
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4546
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4647
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
48+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
4749
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4850
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
4951
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
@@ -108,6 +110,7 @@ golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
108110
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
109111
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
110112
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
113+
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
111114
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
112115
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
113116
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=

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)