dir_other.go 3.3 KB

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