Skip to content

Commit

Permalink
Merge pull request #307 from fyrchik/fix-windows-readonly
Browse files Browse the repository at this point in the history
Fix readonly file mapping on windows
  • Loading branch information
ahrtr authored Dec 21, 2022
2 parents 038b2b4 + 4504feb commit dd0ab6d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions bolt_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,28 @@ func funlock(db *DB) error {
// mmap memory maps a DB's data file.
// Based on: https://github.com/edsrzf/mmap-go
func mmap(db *DB, sz int) error {
var sizelo, sizehi uint32

if !db.readOnly {
// Truncate the database to the size of the mmap.
if err := db.file.Truncate(int64(sz)); err != nil {
return fmt.Errorf("truncate: %s", err)
}
sizehi = uint32(sz >> 32)
sizelo = uint32(sz) & 0xffffffff
}

// Open a file mapping handle.
sizelo := uint32(sz >> 32)
sizehi := uint32(sz) & 0xffffffff
h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizelo, sizehi, nil)
h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizehi, sizelo, nil)
if h == 0 {
return os.NewSyscallError("CreateFileMapping", errno)
}

// Create the memory map.
addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, 0)
if addr == 0 {
// Do our best and report error returned from MapViewOfFile.
_ = syscall.CloseHandle(h)
return os.NewSyscallError("MapViewOfFile", errno)
}

Expand Down

0 comments on commit dd0ab6d

Please sign in to comment.