gommap
gommap copied to clipboard
Open memory map empty file on window 64, errors CreateFileMapping: The volume for a file has been externally altered so that the opened file is no longer valid
Try to map empty files on window 64, the gommap.Map throws error
CreateFileMapping: The volume for a file has been externally altered so that the opened file is no longer valid
package main
import (
"fmt"
"log"
"os"
"github.com/tysonmote/gommap"
)
func main() {
file, err := os.OpenFile("text2.dat", os.O_RDWR, 0600)
if err != nil {
log.Fatalln("open file failed: ", err)
}
mmap, err := gommap.Map(
file.Fd(),
gommap.PROT_READ|gommap.PROT_WRITE,
gommap.MAP_SHARED,
)
if err != nil {
fmt.Println("map file failed: ", err)
}
log.Println("map object", mmap)
}
Unfortunately I don't have a Windows machine to fix and test this out on. If you open a PR to fix it I'd be happy to merge it!
This is documented by the win32 API as an error condition.
See: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createfilemappinga
From that doc:
An attempt to map a file with a length of 0 (zero) fails with an error code of ERROR_FILE_INVALID. Applications should test for files with a length of 0 (zero) and reject those files.
@tysonmote - you might want to close this one.