dir_windows.go 3.3 KB

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