dir_windows.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //go:build windows
  2. // +build windows
  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. // OpenDir opens a directory in windows with write access for syncing.
  20. import (
  21. "os"
  22. "path/filepath"
  23. "syscall"
  24. "github.com/dgraph-io/badger/v4/y"
  25. )
  26. // FILE_ATTRIBUTE_TEMPORARY - A file that is being used for temporary storage.
  27. // FILE_FLAG_DELETE_ON_CLOSE - The file is to be deleted immediately after all of its handles are
  28. // closed, which includes the specified handle and any other open or duplicated handles.
  29. // See: https://docs.microsoft.com/en-us/windows/desktop/FileIO/file-attribute-constants
  30. // NOTE: Added here to avoid importing golang.org/x/sys/windows
  31. const (
  32. FILE_ATTRIBUTE_TEMPORARY = 0x00000100
  33. FILE_FLAG_DELETE_ON_CLOSE = 0x04000000
  34. )
  35. func openDir(path string) (*os.File, error) {
  36. fd, err := openDirWin(path)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return os.NewFile(uintptr(fd), path), nil
  41. }
  42. func openDirWin(path string) (fd syscall.Handle, err error) {
  43. if len(path) == 0 {
  44. return syscall.InvalidHandle, syscall.ERROR_FILE_NOT_FOUND
  45. }
  46. pathp, err := syscall.UTF16PtrFromString(path)
  47. if err != nil {
  48. return syscall.InvalidHandle, err
  49. }
  50. access := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE)
  51. sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE)
  52. createmode := uint32(syscall.OPEN_EXISTING)
  53. fl := uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS)
  54. return syscall.CreateFile(pathp, access, sharemode, nil, createmode, fl, 0)
  55. }
  56. // DirectoryLockGuard holds a lock on the directory.
  57. type directoryLockGuard struct {
  58. h syscall.Handle
  59. path string
  60. }
  61. // AcquireDirectoryLock acquires exclusive access to a directory.
  62. func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) (*directoryLockGuard, error) {
  63. if readOnly {
  64. return nil, ErrWindowsNotSupported
  65. }
  66. // Convert to absolute path so that Release still works even if we do an unbalanced
  67. // chdir in the meantime.
  68. absLockFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))
  69. if err != nil {
  70. return nil, y.Wrap(err, "Cannot get absolute path for pid lock file")
  71. }
  72. // This call creates a file handler in memory that only one process can use at a time. When
  73. // that process ends, the file is deleted by the system.
  74. // FILE_ATTRIBUTE_TEMPORARY is used to tell Windows to try to create the handle in memory.
  75. // FILE_FLAG_DELETE_ON_CLOSE is not specified in syscall_windows.go but tells Windows to delete
  76. // the file when all processes holding the handler are closed.
  77. // XXX: this works but it's a bit klunky. i'd prefer to use LockFileEx but it needs unsafe pkg.
  78. h, err := syscall.CreateFile(
  79. syscall.StringToUTF16Ptr(absLockFilePath), 0, 0, nil,
  80. syscall.OPEN_ALWAYS,
  81. uint32(FILE_ATTRIBUTE_TEMPORARY|FILE_FLAG_DELETE_ON_CLOSE),
  82. 0)
  83. if err != nil {
  84. return nil, y.Wrapf(err,
  85. "Cannot create lock file %q. Another process is using this Badger database",
  86. absLockFilePath)
  87. }
  88. return &directoryLockGuard{h: h, path: absLockFilePath}, nil
  89. }
  90. // Release removes the directory lock.
  91. func (g *directoryLockGuard) release() error {
  92. g.path = ""
  93. return syscall.CloseHandle(g.h)
  94. }
  95. // Windows doesn't support syncing directories to the file system. See
  96. // https://github.com/dgraph-io/badger/issues/699#issuecomment-504133587 for more details.
  97. func syncDir(dir string) error { return nil }