Monitoring a folder with ReadDirectoryChangesW causes its parent to be locked and can't be deleted.
There is a post about this here:
FindFirstChangeNotification locks parent folder
but the only solution mentioned in it is that we should always listen at the top level.
Has anyone found out a better way to do this instead of watching at the top level?
Sometimes this can go all the way to watching the Drive and that can't take a lot of process time on a machine.
Thanks!
folder can be deleted only in case it empty, otherwise we got error
STATUS_DIRECTORY_NOT_EMPTY
- Indicates that the directory trying to be deleted is not empty.from another side - if you have opening handle for file - it can not be deleted until you not close it handle (something here changed begin from win10 rs1)
so if you monitor some child sub-folder with
ReadDirectoryChangesW
you have opened handle to it, and parent can not (before WIN10_RS1) be deleted until you not close this handle.in general process look like - when somebody try delete folder - it must enumerate all files(sub-folders) inside it and delete it first. when delete operation will be apply on folder for which
ReadDirectoryChangesW
called - the io request will be completed with statusSTATUS_DELETE_PENDING
- A non close operation has been requested of a file object with a delete pending. (it converted to win32 error codeERROR_ACCESS_DENIED
- Access is denied.). when you got this error fromReadDirectoryChangesW
you must close your directory handle used in this call. then is raise - who is first - you close directory handle or another code try delete parent folder...begin from win10 rs1 possible delete parent, even if somebody hold open handle to it child file(folder) by calling
NtSetInformationFile
withFileDispositionInformationEx
orSetFileInformationByHandle
withFileDispositionInfoEx
.the magic here in new flag
FILE_DISPOSITION_POSIX_SEMANTICS
(Specifies the system should perform a POSIX-style delete)so when we use this - file itself of course will be not deleted, until caller of
ReadDirectoryChangesW
not close self handle, but file will be removed from the parent folder. as result parent folder can became empty, after which we can delete it.note that
DeleteFileW
andRemoveDirectoryW
here will be not work here, because they used old information class FileDispositionInformation withFILE_DISPOSITION_INFORMATION
and of course child must be open with
FILE_SHARE_DELETE
in other calls, otherwise we simply can not open it withDELETE
access later