gommap icon indicating copy to clipboard operation
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

Open gombaniro opened this issue 3 years ago • 2 comments

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)

}

gombaniro avatar Jan 19 '22 10:01 gombaniro

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!

tysonmote avatar Jan 20 '22 21:01 tysonmote

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.

kellypleahy avatar Mar 21 '22 04:03 kellypleahy