Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #8

Open
gombaniro opened this issue Jan 19, 2022 · 2 comments

Comments

@gombaniro
Copy link

gombaniro commented Jan 19, 2022

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)

}
@tysonmote
Copy link
Owner

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!

@kellypleahy
Copy link
Contributor

kellypleahy commented Mar 21, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants