-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix build for GOARCH=wasm with GOOS=js or GOOS=wasip1 #2048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| //go:build js || wasip1 | ||
| // +build js wasip1 | ||
|
|
||
| /* | ||
| * Copyright 2017 Dgraph Labs, Inc. and Contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package badger | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/dgraph-io/badger/v4/y" | ||
| ) | ||
|
|
||
| // directoryLockGuard holds a lock on a directory and a pid file inside. The pid file isn't part | ||
| // of the locking mechanism, it's just advisory. | ||
| type directoryLockGuard struct { | ||
| // File handle on the directory, which we've flocked. | ||
| f *os.File | ||
| // The absolute path to our pid file. | ||
| path string | ||
| // Was this a shared lock for a read-only database? | ||
| readOnly bool | ||
| } | ||
|
|
||
| // acquireDirectoryLock gets a lock on the directory (using flock). If | ||
| // this is not read-only, it will also write our pid to | ||
| // dirPath/pidFileName for convenience. | ||
| func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) ( | ||
| *directoryLockGuard, error) { | ||
| // Convert to absolute path so that Release still works even if we do an unbalanced | ||
| // chdir in the meantime. | ||
| absPidFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName)) | ||
| if err != nil { | ||
| return nil, y.Wrapf(err, "cannot get absolute path for pid lock file") | ||
| } | ||
| f, err := os.Open(dirPath) | ||
| if err != nil { | ||
| return nil, y.Wrapf(err, "cannot open directory %q", dirPath) | ||
| } | ||
|
|
||
| // NOTE: Here is where we would normally call flock. | ||
| // This is not supported in js / wasm, so skip it. | ||
|
|
||
| if !readOnly { | ||
| // Yes, we happily overwrite a pre-existing pid file. We're the | ||
| // only read-write badger process using this directory. | ||
| err = os.WriteFile(absPidFilePath, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666) | ||
mangalaman93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| f.Close() | ||
| return nil, y.Wrapf(err, | ||
| "Cannot write pid file %q", absPidFilePath) | ||
| } | ||
| } | ||
|
|
||
| return &directoryLockGuard{f, absPidFilePath, readOnly}, nil | ||
| } | ||
|
|
||
| // Release deletes the pid file and releases our lock on the directory. | ||
| func (guard *directoryLockGuard) release() error { | ||
| var err error | ||
| if !guard.readOnly { | ||
| // It's important that we remove the pid file first. | ||
| err = os.Remove(guard.path) | ||
| } | ||
|
|
||
| if closeErr := guard.f.Close(); err == nil { | ||
| err = closeErr | ||
| } | ||
| guard.path = "" | ||
| guard.f = nil | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| // openDir opens a directory for syncing. | ||
| func openDir(path string) (*os.File, error) { return os.Open(path) } | ||
|
|
||
| // When you create or delete a file, you have to ensure the directory entry for the file is synced | ||
| // in order to guarantee the file is visible (if the system crashes). (See the man page for fsync, | ||
| // or see https://github.com/coreos/etcd/issues/6368 for an example.) | ||
| func syncDir(dir string) error { | ||
| f, err := openDir(dir) | ||
| if err != nil { | ||
| return y.Wrapf(err, "While opening directory: %s.", dir) | ||
| } | ||
|
|
||
| err = f.Sync() | ||
| closeErr := f.Close() | ||
| if err != nil { | ||
| return y.Wrapf(err, "While syncing directory: %s.", dir) | ||
| } | ||
| return y.Wrapf(closeErr, "While closing directory: %s.", dir) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.