dir_other.go raw
1 //go:build js || wasip1
2 // +build js wasip1
3
4 /*
5 * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 package badger
10
11 import (
12 "fmt"
13 "os"
14 "path/filepath"
15
16 "github.com/dgraph-io/badger/v4/y"
17 )
18
19 // directoryLockGuard holds a lock on a directory and a pid file inside. The pid file isn't part
20 // of the locking mechanism, it's just advisory.
21 type directoryLockGuard struct {
22 // File handle on the directory, which we've flocked.
23 f *os.File
24 // The absolute path to our pid file.
25 path string
26 // Was this a shared lock for a read-only database?
27 readOnly bool
28 }
29
30 // acquireDirectoryLock gets a lock on the directory (using flock). If
31 // this is not read-only, it will also write our pid to
32 // dirPath/pidFileName for convenience.
33 func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) (
34 *directoryLockGuard, error) {
35 // Convert to absolute path so that Release still works even if we do an unbalanced
36 // chdir in the meantime.
37 absPidFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))
38 if err != nil {
39 return nil, y.Wrapf(err, "cannot get absolute path for pid lock file")
40 }
41 f, err := os.Open(dirPath)
42 if err != nil {
43 return nil, y.Wrapf(err, "cannot open directory %q", dirPath)
44 }
45
46 // NOTE: Here is where we would normally call flock.
47 // This is not supported in js / wasm, so skip it.
48
49 if !readOnly {
50 // Yes, we happily overwrite a pre-existing pid file. We're the
51 // only read-write badger process using this directory.
52 err = os.WriteFile(absPidFilePath, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666)
53 if err != nil {
54 f.Close()
55 return nil, y.Wrapf(err,
56 "Cannot write pid file %q", absPidFilePath)
57 }
58 }
59
60 return &directoryLockGuard{f, absPidFilePath, readOnly}, nil
61 }
62
63 // Release deletes the pid file and releases our lock on the directory.
64 func (guard *directoryLockGuard) release() error {
65 var err error
66 if !guard.readOnly {
67 // It's important that we remove the pid file first.
68 err = os.Remove(guard.path)
69 }
70
71 if closeErr := guard.f.Close(); err == nil {
72 err = closeErr
73 }
74 guard.path = ""
75 guard.f = nil
76
77 return err
78 }
79
80 // openDir opens a directory for syncing.
81 func openDir(path string) (*os.File, error) { return os.Open(path) }
82
83 // When you create or delete a file, you have to ensure the directory entry for the file is synced
84 // in order to guarantee the file is visible (if the system crashes). (See the man page for fsync,
85 // or see https://github.com/coreos/etcd/issues/6368 for an example.)
86 func syncDir(dir string) error {
87 f, err := openDir(dir)
88 if err != nil {
89 return y.Wrapf(err, "While opening directory: %s.", dir)
90 }
91
92 err = f.Sync()
93 closeErr := f.Close()
94 if err != nil {
95 return y.Wrapf(err, "While syncing directory: %s.", dir)
96 }
97 return y.Wrapf(closeErr, "While closing directory: %s.", dir)
98 }
99