Additional Microsoft Defender tools and services that provide security across various platforms and environments
How to prevent random ACCESS DENIED errors on folder delete/rename when mssense is active
Hi,
I am working on a C++ application running on Windows installations (10+) where Windows Defender might be active. To achieve serialized data integrity, it need an atomic folder creation.
Since there is no such thing, it simulates it through the standard following steps:
- It creates a new folder on the same volume.
- Files are saved inside that folder and flushed to disk.
- Only then, the folder is renamed to its final location.
This sequence seems to randomly fails because mssense open handles on files inside the folder during the folder creation and prevent to rename the folder when the creation is completed.
Here are some thoughts:
- I do not control the Windows Defender configuration, nor the Windows installations. I can't exclude the folder's location. Anyway, from what I understand, the exclusion list does not prevent mssense to access new files anyway. It just prevent Defender to scan them.
- I don't use NTFS transactional API since it is deprecated and will be removed.
- The user-mode Windows API does not allow to open a folder and obtains an HANDLE with an exclusive access as an atomic operation. I know it is possible to do so with NtFileCreate, but this is not encouraged since Microsoft may break this API in futures versions. This road also implies that I have to use NtFileCreate to create each files inside the folder too since it will be accessible only through the HANDLE returned by NtFileCreate. To my knowledge, there is no supported API allowing to create a file in a folder using the folder's HANDLE (NtFileCreate again).
- I don't like the idea to just "retry" until the operation might complete successfully on the filesystem. How long should I wait? How many time should I retry? Should I consider the system's load? Should I consider the location could be on a network drive, or a slow drive?
- Since it is more important to successfully "graph" the folder to its destination in an atomic way than to delete it from the source location, I considered something like a creating a hard link and removing the source folder only later if an access denied occurs. But, to my knowledge, there is no such thing as hardlinks for folders on Windows.
What you guys are doing in that situation?
Any way to delay mssense interferences until I completed my write operations?
Thanks,
Jeff