dir_unix.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //go:build !windows && !plan9 && !js && !wasip1
  2. // +build !windows,!plan9,!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. "golang.org/x/sys/unix"
  13. "github.com/dgraph-io/badger/v4/y"
  14. )
  15. // directoryLockGuard holds a lock on a directory and a pid file inside. The pid file isn't part
  16. // of the locking mechanism, it's just advisory.
  17. type directoryLockGuard struct {
  18. // File handle on the directory, which we've flocked.
  19. f *os.File
  20. // The absolute path to our pid file.
  21. path string
  22. // Was this a shared lock for a read-only database?
  23. readOnly bool
  24. }
  25. // acquireDirectoryLock gets a lock on the directory (using flock). If
  26. // this is not read-only, it will also write our pid to
  27. // dirPath/pidFileName for convenience.
  28. func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) (
  29. *directoryLockGuard, error) {
  30. // Convert to absolute path so that Release still works even if we do an unbalanced
  31. // chdir in the meantime.
  32. absPidFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))
  33. if err != nil {
  34. return nil, y.Wrapf(err, "cannot get absolute path for pid lock file")
  35. }
  36. f, err := os.Open(dirPath)
  37. if err != nil {
  38. return nil, y.Wrapf(err, "cannot open directory %q", dirPath)
  39. }
  40. opts := unix.LOCK_EX | unix.LOCK_NB
  41. if readOnly {
  42. opts = unix.LOCK_SH | unix.LOCK_NB
  43. }
  44. err = unix.Flock(int(f.Fd()), opts)
  45. if err != nil {
  46. f.Close()
  47. return nil, y.Wrapf(err,
  48. "Cannot acquire directory lock on %q. Another process is using this Badger database.",
  49. dirPath)
  50. }
  51. if !readOnly {
  52. // Yes, we happily overwrite a pre-existing pid file. We're the
  53. // only read-write badger process using this directory.
  54. err = os.WriteFile(absPidFilePath, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666)
  55. if err != nil {
  56. f.Close()
  57. return nil, y.Wrapf(err,
  58. "Cannot write pid file %q", absPidFilePath)
  59. }
  60. }
  61. return &directoryLockGuard{f, absPidFilePath, readOnly}, nil
  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. if closeErr := guard.f.Close(); err == nil {
  71. err = closeErr
  72. }
  73. guard.path = ""
  74. guard.f = nil
  75. return err
  76. }
  77. // openDir opens a directory for syncing.
  78. func openDir(path string) (*os.File, error) { return os.Open(path) }
  79. // When you create or delete a file, you have to ensure the directory entry for the file is synced
  80. // in order to guarantee the file is visible (if the system crashes). (See the man page for fsync,
  81. // or see https://github.com/coreos/etcd/issues/6368 for an example.)
  82. func syncDir(dir string) error {
  83. f, err := openDir(dir)
  84. if err != nil {
  85. return y.Wrapf(err, "While opening directory: %s.", dir)
  86. }
  87. err = f.Sync()
  88. closeErr := f.Close()
  89. if err != nil {
  90. return y.Wrapf(err, "While syncing directory: %s.", dir)
  91. }
  92. return y.Wrapf(closeErr, "While closing directory: %s.", dir)
  93. }