mirror of
https://github.com/luxfi/zapdb.git
synced 2026-10-30 15:12:20 +00:00
The module has been github.com/luxfi/zapdb for a while, but the package inside it still declared itself badger, so every consumer had to write zapdb "github.com/luxfi/zapdb" // package badger and the alias existed only to paper over the mismatch. 56 root files and the 134 qualified references inside the repo now say zapdb. Backward compatible in both directions: an existing consumer with the alias keeps compiling (the alias is now merely redundant), and one without it starts compiling instead of failing on an undefined zapdb. Directory badger/ and its cmd package are untouched — that is the CLI, and its binary is named badger. Build and vet clean; suite green except TestZRestore, which wants a /tmp/zb/src benchmark fixture and fails identically without this change. Co-authored-by: Hanzo Dev <[email protected]>
109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
//go:build !windows && !plan9 && !js && !wasip1 && !aix
|
|
// +build !windows,!plan9,!js,!wasip1,!aix
|
|
|
|
/*
|
|
* SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
package zapdb
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"github.com/luxfi/zapdb/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)
|
|
}
|
|
opts := unix.LOCK_EX | unix.LOCK_NB
|
|
if readOnly {
|
|
opts = unix.LOCK_SH | unix.LOCK_NB
|
|
}
|
|
|
|
err = unix.Flock(int(f.Fd()), opts)
|
|
if err != nil {
|
|
f.Close()
|
|
return nil, y.Wrapf(err,
|
|
"Cannot acquire directory lock on %q. Another process is using this Badger database.",
|
|
dirPath)
|
|
}
|
|
|
|
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)
|
|
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)
|
|
}
|