Deleting a directory under Windows in no race mode? discusses potential race conditions that can arise when deleting a directory recursively.

According to , this can be done royalty-free under Linux using openat () . Is there an equivalent mechanism under Windows?


source to share

1 answer

The key difference between Windows file system behavior and Linux file system behavior is blocking and reference counting.

On Windows, if a process has a file open, that file and the path to that file are protected.

So, if someone opened "C: \ a \ b \ c \ d \ file.txt", no one can rename or delete any part of the path "C: \ a \ b \ c \ d \ file.txt".

Linux model is different from others, any part of this path can be changed and even the file can be deleted. The process that contains the file.txt file descriptor still contains a link, and the file will not be removed from the file system until all descriptors are closed.

The Win32 API does not provide a direct way to hold a handle in a directory (although there are APIs for that - see "Zw" functions, FindFirstFile, I'm not sure, backup APIs, etc.). - but your current "current directory" has a handle to that directory.

So you can get the "openat" behavior by changing the working directory and immediately opening the file. Better would be to use something like ZwCreateFile () to open the directory handle - since "current directory" is a global process.

Find Stackoverflow and for ZwCreateFile information.



All Articles