dir_windows.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // OpenDir opens a directory in windows with write access for syncing.
  19. import (
  20. "fmt"
  21. "os"
  22. "path/filepath"
  23. "syscall"
  24. "github.com/pkg/errors"
  25. )
  26. func openDir(path string) (*os.File, error) {
  27. fd, err := openDirWin(path)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return os.NewFile(uintptr(fd), path), nil
  32. }
  33. func openDirWin(path string) (fd syscall.Handle, err error) {
  34. if len(path) == 0 {
  35. return syscall.InvalidHandle, syscall.ERROR_FILE_NOT_FOUND
  36. }
  37. pathp, err := syscall.UTF16PtrFromString(path)
  38. if err != nil {
  39. return syscall.InvalidHandle, err
  40. }
  41. access := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE)
  42. sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE)
  43. createmode := uint32(syscall.OPEN_EXISTING)
  44. fl := uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS)
  45. return syscall.CreateFile(pathp, access, sharemode, nil, createmode, fl, 0)
  46. }
  47. // DirectoryLockGuard holds a lock on the directory.
  48. type directoryLockGuard struct {
  49. path string
  50. }
  51. // AcquireDirectoryLock acquires exclusive access to a directory.
  52. func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) (*directoryLockGuard, error) {
  53. if readOnly {
  54. return nil, ErrWindowsNotSupported
  55. }
  56. // Convert to absolute path so that Release still works even if we do an unbalanced
  57. // chdir in the meantime.
  58. absLockFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))
  59. if err != nil {
  60. return nil, errors.Wrap(err, "Cannot get absolute path for pid lock file")
  61. }
  62. f, err := os.OpenFile(absLockFilePath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
  63. if err != nil {
  64. return nil, errors.Wrapf(err,
  65. "Cannot create pid lock file %q. Another process is using this Badger database",
  66. absLockFilePath)
  67. }
  68. _, err = fmt.Fprintf(f, "%d\n", os.Getpid())
  69. closeErr := f.Close()
  70. if err != nil {
  71. return nil, errors.Wrap(err, "Cannot write to pid lock file")
  72. }
  73. if closeErr != nil {
  74. return nil, errors.Wrap(closeErr, "Cannot close pid lock file")
  75. }
  76. return &directoryLockGuard{path: absLockFilePath}, nil
  77. }
  78. // Release removes the directory lock.
  79. func (g *directoryLockGuard) release() error {
  80. path := g.path
  81. g.path = ""
  82. return os.Remove(path)
  83. }