File.Exists /_access / GetFileAttributes / FindFirstFile,FindNextFile / _stat behavior over SMB 2.0
Problem Observed: When an application on client creates a file on a shared directory on server or application on server creates a file on shared directory on server, and file is created successfully but when the application on client try to check the existence of the recently created file by _access or other method, it reports that the file does not exists. This behavior is observed when SMB 2.0 is used.
SMB 2.0 was introduced on Windows Vista and Windows Server 2008. The file does exists about after 20 seconds.
Is it a bug with SMB 2.0 ?
No it is not a bug.
Is this by design with SMB 2.0?
Yes, this is a design change with SMB 2.0.
What is really happening?
This is because of the local caching included on the client side with SMB 2.0. When the SMB 2.0 session has been created, the local cache will be available on the client side, which will be refreshed after every 10 seconds by default. Any further request to the file exists will be checked against this local cache rather than going to the server share. So, if a local cache is built on client, and on server share a new file is created, local cache has not been invalidated and not in sync with server share, any further request for checking the new file existence will fail. This is root cause, but by design.
If the local cache is updated and in sync with server share, request will be successful.
How do I control the local cache lifetime?
You can create registry keys below to control the cache lifetime.
They are all REG_DWORD type in seconds.
Register for directory or file change notifications using Win32 API.
Use FindFirstChangeNotification Function (Windows) API to register for changes.
Sample can be found here Obtaining Directory Change Notifications (Windows)
Windows SDK – Microsoft