dir_unix.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // +build !windows
  2. /*
  3. * Copyright 2017 Dgraph Labs, Inc. and Contributors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package badger
  18. import (
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "path/filepath"
  23. "github.com/pkg/errors"
  24. "golang.org/x/sys/unix"
  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) (*directoryLockGuard, error) {
  40. // Convert to absolute path so that Release still works even if we do an unbalanced
  41. // chdir in the meantime.
  42. absPidFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))
  43. if err != nil {
  44. return nil, errors.Wrap(err, "cannot get absolute path for pid lock file")
  45. }
  46. f, err := os.Open(dirPath)
  47. if err != nil {
  48. return nil, errors.Wrapf(err, "cannot open directory %q", dirPath)
  49. }
  50. opts := unix.LOCK_EX | unix.LOCK_NB
  51. if readOnly {
  52. opts = unix.LOCK_SH | unix.LOCK_NB
  53. }
  54. err = unix.Flock(int(f.Fd()), opts)
  55. if err != nil {
  56. f.Close()
  57. return nil, errors.Wrapf(err,
  58. "Cannot acquire directory lock on %q. Another process is using this Badger database.",
  59. dirPath)
  60. }
  61. if !readOnly {
  62. // Yes, we happily overwrite a pre-existing pid file. We're the
  63. // only read-write badger process using this directory.
  64. err = ioutil.WriteFile(absPidFilePath, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666)
  65. if err != nil {
  66. f.Close()
  67. return nil, errors.Wrapf(err,
  68. "Cannot write pid file %q", absPidFilePath)
  69. }
  70. }
  71. return &directoryLockGuard{f, absPidFilePath, readOnly}, nil
  72. }
  73. // Release deletes the pid file and releases our lock on the directory.
  74. func (guard *directoryLockGuard) release() error {
  75. var err error
  76. if !guard.readOnly {
  77. // It's important that we remove the pid file first.
  78. err = os.Remove(guard.path)
  79. }
  80. if closeErr := guard.f.Close(); err == nil {
  81. err = closeErr
  82. }
  83. guard.path = ""
  84. guard.f = nil
  85. return err
  86. }
  87. // openDir opens a directory for syncing.
  88. func openDir(path string) (*os.File, error) { return os.Open(path) }