dir_unix.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //go:build !windows && !plan9 && !js && !wasip1
  2. // +build !windows,!plan9,!js,!wasip1
  3. /*
  4. * Copyright 2017 Dgraph Labs, Inc. and Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package badger
  19. import (
  20. "fmt"
  21. "os"
  22. "path/filepath"
  23. "golang.org/x/sys/unix"
  24. "github.com/dgraph-io/badger/v4/y"
  25. )
  26. // directoryLockGuard holds a lock on a directory and a pid file inside. The pid file isn't part
  27. // of the locking mechanism, it's just advisory.
  28. type directoryLockGuard struct {
  29. // File handle on the directory, which we've flocked.
  30. f *os.File
  31. // The absolute path to our pid file.
  32. path string
  33. // Was this a shared lock for a read-only database?
  34. readOnly bool
  35. }
  36. // acquireDirectoryLock gets a lock on the directory (using flock). If
  37. // this is not read-only, it will also write our pid to
  38. // dirPath/pidFileName for convenience.
  39. func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) (
  40. *directoryLockGuard, error) {
  41. // Convert to absolute path so that Release still works even if we do an unbalanced
  42. // chdir in the meantime.
  43. absPidFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))
  44. if err != nil {
  45. return nil, y.Wrapf(err, "cannot get absolute path for pid lock file")
  46. }
  47. f, err := os.Open(dirPath)
  48. if err != nil {
  49. return nil, y.Wrapf(err, "cannot open directory %q", dirPath)
  50. }
  51. opts := unix.LOCK_EX | unix.LOCK_NB
  52. if readOnly {
  53. opts = unix.LOCK_SH | unix.LOCK_NB
  54. }
  55. err = unix.Flock(int(f.Fd()), opts)
  56. if err != nil {
  57. f.Close()
  58. return nil, y.Wrapf(err,
  59. "Cannot acquire directory lock on %q. Another process is using this Badger database.",
  60. dirPath)
  61. }
  62. if !readOnly {
  63. // Yes, we happily overwrite a pre-existing pid file. We're the
  64. // only read-write badger process using this directory.
  65. err = os.WriteFile(absPidFilePath, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666)
  66. if err != nil {
  67. f.Close()
  68. return nil, y.Wrapf(err,
  69. "Cannot write pid file %q", absPidFilePath)
  70. }
  71. }
  72. return &directoryLockGuard{f, absPidFilePath, readOnly}, nil
  73. }
  74. // Release deletes the pid file and releases our lock on the directory.
  75. func (guard *directoryLockGuard) release() error {
  76. var err error
  77. if !guard.readOnly {
  78. // It's important that we remove the pid file first.
  79. err = os.Remove(guard.path)
  80. }
  81. if closeErr := guard.f.Close(); err == nil {
  82. err = closeErr
  83. }
  84. guard.path = ""
  85. guard.f = nil
  86. return err
  87. }
  88. // openDir opens a directory for syncing.
  89. func openDir(path string) (*os.File, error) { return os.Open(path) }
  90. // When you create or delete a file, you have to ensure the directory entry for the file is synced
  91. // in order to guarantee the file is visible (if the system crashes). (See the man page for fsync,
  92. // or see https://github.com/coreos/etcd/issues/6368 for an example.)
  93. func syncDir(dir string) error {
  94. f, err := openDir(dir)
  95. if err != nil {
  96. return y.Wrapf(err, "While opening directory: %s.", dir)
  97. }
  98. err = f.Sync()
  99. closeErr := f.Close()
  100. if err != nil {
  101. return y.Wrapf(err, "While syncing directory: %s.", dir)
  102. }
  103. return y.Wrapf(closeErr, "While closing directory: %s.", dir)
  104. }