On Linux, we can memmap a file, delete the underlying file and still read from the mapped memory. Windows doesn't seem to allow you to delete a file if it's currently mapped.
Is it possible to mimic the behaviour of mem-mapped POSIX files on Windows, so we can delete the file and still read from the map?
I have an example gist here that demonstrates what I'm trying to do.
One workaround I was thinking of was copying the file contents into a
Vec or something on Windows, unmapping and deleting that way, but it'd be great if there was some magic combination of flags that would do it.
Why not just wait until you've dropped the handle to the memmap before you try to remove it?
This is a pattern used by
tantivy. Looks like you can't do this in Windows so we'll look at alternatives.
Because if your program crashes or is killed, the file won't be deleted.
Deleting a file while using it is a common pattern in Linux, and ensures everything's cleaned up by the OS in case of unexpected failure.
If you use the Windows API directly, you can call
CreateFile with the
So what we'll probably end up doing is something like what @mmstick originally suggested.
tantivy keeps track of old index files that are no longer needed, so they can be removed at some point. On Windows this will mean index files that are currently mapped by a process will just hang around for a bit longer.
Surely this isn't a common approach on Linux. If an application is killed, you can have your application delete the stragglers the next time it's launched, and if these temporary files are stored in the temp directory, the files will be erased when the system reboots.