Debian Codegraph @

vendor/github.com/coreos/bbolt/bolt_windows.go

raw
1 package bolt
2
3 import (
4 "fmt"
5 "os"
6 "syscall"
7 "time"
8 "unsafe"
9 )
10
11 // LockFileEx code derived from golang build filemutex_windows.go @ v1.5.1
12 var (
13 modkernel32 = syscall.NewLazyDLL("kernel32.dll")
14 procLockFileEx = modkernel32.NewProc("LockFileEx")
15 procUnlockFileEx = modkernel32.NewProc("UnlockFileEx")
16 )
17
18 const (
19 lockExt = ".lock"
20
21 // see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
22 flagLockExclusive = 2
23 flagLockFailImmediately = 1
24
25 // see https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
26 errLockViolation syscall.Errno = 0x21
27 )
28
29 func lockFileEx(h syscall.Handle, flags, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
30 r, _, err := procLockFileEx.Call(uintptr(h), uintptr(flags), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)))
31 if r == 0 {
32 return err
33 }
34 return nil
35 }
36
37 func unlockFileEx(h syscall.Handle, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
38 r, _, err := procUnlockFileEx.Call(uintptr(h), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)), 0)
39 if r == 0 {
40 return err
41 }
42 return nil
43 }
44
45 // fdatasync flushes written data to a file descriptor.
46 func fdatasync(db *DB) error {
47 return db.file.Sync()
48 }
49
50 // flock acquires an advisory lock on a file descriptor.
51 func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
52 // Create a separate lock file on windows because a process
53 // cannot share an exclusive lock on the same file. This is
54 // needed during Tx.WriteTo().
55 f, err := os.OpenFile(db.path+lockExt, os.O_CREATE, mode)
56 if err != nil {
57 return err
58 }
59 db.lockfile = f
60
61 var t time.Time
62 if timeout != 0 {
63 t = time.Now()
64 }
65 fd := f.Fd()
66 var flag uint32 = flagLockFailImmediately
67 if exclusive {
68 flag |= flagLockExclusive
69 }
70 for {
71 // Attempt to obtain an exclusive lock.
72 err := lockFileEx(syscall.Handle(fd), flag, 0, 1, 0, &syscall.Overlapped{})
73 if err == nil {
74 return nil
75 } else if err != errLockViolation {
76 return err
77 }
78
79 // If we timed oumercit then return an error.
80 if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {
81 return ErrTimeout
82 }
83
84 // Wait for a bit and try again.
85 time.Sleep(flockRetryTimeout)
86 }
87 }
88
89 // funlock releases an advisory lock on a file descriptor.
90 func funlock(db *DB) error {
91 err := unlockFileEx(syscall.Handle(db.lockfile.Fd()), 0, 1, 0, &syscall.Overlapped{})
92 db.lockfile.Close()
93 os.Remove(db.path + lockExt)
94 return err
95 }
96
97 // mmap memory maps a DB's data file.
98 // Based on: https://github.com/edsrzf/mmap-go
99 func mmap(db *DB, sz int) error {
100 if !db.readOnly {
101 // Truncate the database to the size of the mmap.
102 if err := db.file.Truncate(int64(sz)); err != nil {
103 return fmt.Errorf("truncate: %s", err)
104 }
105 }
106
107 // Open a file mapping handle.
108 sizelo := uint32(sz >> 32)
109 sizehi := uint32(sz) & 0xffffffff
110 h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizelo, sizehi, nil)
111 if h == 0 {
112 return os.NewSyscallError("CreateFileMapping", errno)
113 }
114
115 // Create the memory map.
116 addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
117 if addr == 0 {
118 return os.NewSyscallError("MapViewOfFile", errno)
119 }
120
121 // Close mapping handle.
122 if err := syscall.CloseHandle(syscall.Handle(h)); err != nil {
123 return os.NewSyscallError("CloseHandle", err)
124 }
125
126 // Convert to a byte array.
127 db.data = ((*[maxMapSize]byte)(unsafe.Pointer(addr)))
128 db.datasz = sz
129
130 return nil
131 }
132
133 // munmap unmaps a pointer from a file.
134 // Based on: https://github.com/edsrzf/mmap-go
135 func munmap(db *DB) error {
136 if db.data == nil {
137 return nil
138 }
139
140 addr := (uintptr)(unsafe.Pointer(&db.data[0]))
141 if err := syscall.UnmapViewOfFile(addr); err != nil {
142 return os.NewSyscallError("UnmapViewOfFile", err)
143 }
144 return nil
145 }