dir_other.go 2.8 KB

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