bolt_windows.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package bbolt
  2. import (
  3. "fmt"
  4. "os"
  5. "syscall"
  6. "time"
  7. "unsafe"
  8. "golang.org/x/sys/windows"
  9. )
  10. // fdatasync flushes written data to a file descriptor.
  11. func fdatasync(db *DB) error {
  12. return db.file.Sync()
  13. }
  14. // flock acquires an advisory lock on a file descriptor.
  15. func flock(db *DB, exclusive bool, timeout time.Duration) error {
  16. var t time.Time
  17. if timeout != 0 {
  18. t = time.Now()
  19. }
  20. var flags uint32 = windows.LOCKFILE_FAIL_IMMEDIATELY
  21. if exclusive {
  22. flags |= windows.LOCKFILE_EXCLUSIVE_LOCK
  23. }
  24. for {
  25. // Fix for https://github.com/etcd-io/bbolt/issues/121. Use byte-range
  26. // -1..0 as the lock on the database file.
  27. var m1 uint32 = (1 << 32) - 1 // -1 in a uint32
  28. err := windows.LockFileEx(windows.Handle(db.file.Fd()), flags, 0, 1, 0, &windows.Overlapped{
  29. Offset: m1,
  30. OffsetHigh: m1,
  31. })
  32. if err == nil {
  33. return nil
  34. } else if err != windows.ERROR_LOCK_VIOLATION {
  35. return err
  36. }
  37. // If we timed oumercit then return an error.
  38. if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {
  39. return ErrTimeout
  40. }
  41. // Wait for a bit and try again.
  42. time.Sleep(flockRetryTimeout)
  43. }
  44. }
  45. // funlock releases an advisory lock on a file descriptor.
  46. func funlock(db *DB) error {
  47. var m1 uint32 = (1 << 32) - 1 // -1 in a uint32
  48. return windows.UnlockFileEx(windows.Handle(db.file.Fd()), 0, 1, 0, &windows.Overlapped{
  49. Offset: m1,
  50. OffsetHigh: m1,
  51. })
  52. }
  53. // mmap memory maps a DB's data file.
  54. // Based on: https://github.com/edsrzf/mmap-go
  55. func mmap(db *DB, sz int) error {
  56. var sizelo, sizehi uint32
  57. if !db.readOnly {
  58. // Truncate the database to the size of the mmap.
  59. if err := db.file.Truncate(int64(sz)); err != nil {
  60. return fmt.Errorf("truncate: %s", err)
  61. }
  62. sizehi = uint32(sz >> 32)
  63. sizelo = uint32(sz) & 0xffffffff
  64. }
  65. // Open a file mapping handle.
  66. h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizehi, sizelo, nil)
  67. if h == 0 {
  68. return os.NewSyscallError("CreateFileMapping", errno)
  69. }
  70. // Create the memory map.
  71. addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, 0)
  72. if addr == 0 {
  73. // Do our best and report error returned from MapViewOfFile.
  74. _ = syscall.CloseHandle(h)
  75. return os.NewSyscallError("MapViewOfFile", errno)
  76. }
  77. // Close mapping handle.
  78. if err := syscall.CloseHandle(syscall.Handle(h)); err != nil {
  79. return os.NewSyscallError("CloseHandle", err)
  80. }
  81. // Convert to a byte array.
  82. db.data = ((*[maxMapSize]byte)(unsafe.Pointer(addr)))
  83. db.datasz = sz
  84. return nil
  85. }
  86. // munmap unmaps a pointer from a file.
  87. // Based on: https://github.com/edsrzf/mmap-go
  88. func munmap(db *DB) error {
  89. if db.data == nil {
  90. return nil
  91. }
  92. addr := (uintptr)(unsafe.Pointer(&db.data[0]))
  93. var err1 error
  94. if err := syscall.UnmapViewOfFile(addr); err != nil {
  95. err1 = os.NewSyscallError("UnmapViewOfFile", err)
  96. }
  97. db.data = nil
  98. db.datasz = 0
  99. return err1
  100. }