Class WindowsEmulatingFileSystem

java.lang.Object
java.nio.file.FileSystem
com.github.mizosoft.methanol.testing.file.WindowsEmulatingFileSystem
All Implemented Interfaces:
ForwardingObject<FileSystem>, Closeable, AutoCloseable

public final class WindowsEmulatingFileSystem extends FileSystem
A ForwardingFileSystem that emulates Windows' behaviour regarding the deletion of files with open handles. Only file handles acquired through FileChannel and AsynchronousFileChannel are tracked.

On Windows, deleting files while they're open isn't allowed by default. One has to open the file with FILE_SHARE_DELETE for that to work. Luckily, NIO opens file channels as such. The problem is when a file is requested for deletion while having open handles, Windows only guarantees it gets physically deleted after all its open handles are closed. In the meantime, the OS denies access to that file. Since Windows associates handles with file names (https://devblogs.microsoft.com/oldnewthing/20040607-00/?p=38993), the deleted file name is hence retained till all handles to that file are closed. Things like creating a new file with the name of a file marked for deletion, renaming another file to such name or opening any sort of channel to such file all throw AccessDeniedException. This file system emulates that behavior, so we're sure these cases are covered when interacting with files on Windows.