What is the best way to check if a file exists on a network share in Windows Vista?

My question is a slight variation in the question What is the best way to check if a file exists on Windows? with some specific reservations. In particular, the data is located on a mapped drive, and the SMB 2.0 protocol is used. (By definition, this requires that the drive be mapped to a Vista machine on a Vista or Server 2008 machine.)

The problem with the answers received in response to the above question is that SMB 2.0 caches a bunch of metadata, including the file names in this directory. As a result, if you test the existence of the file you just created, then the _access, access, GetFileAttributes and CreateFile functions (and possibly others) will use cached information to answer the question “does this file exist?” If the file was recently created by another user, the cache indicates that the file is missing, despite the fact that it really exists. I installed test environments to test this, and I can confirm that the SMB2 client does not generate traffic for a few seconds [presumably, the cache expires in 5 seconds or so].

Has anyone else seen this? (If so, have you found a workaround other than adding a delay / snooze?) Does anyone know of an API similar to the one described above that can check for a file without using the SMB cache? Or, better yet, does anyone know of a Windows API that simply flushes metadata in the SMB cache?

+3
source share
3 answers

Although this is not a software solution, I found an effective solution (using Microsoft support). I wanted to post a workaround here if someone else is as upset about it as I am. Because this behavior is a “feature” of SMB2, Microsoft has provided the following registry entries to override the default metadata cache lifetime:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters\FileInfoCacheLifetime 
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters\FileNotFoundCacheLifetime 
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters\DirectoryCacheLifetime

- DWORD, . ( 0 SMB2 .)

+1

open(path, O_CREAT | O_EXCL, mode) . , . , SMB , . , , , , ( ). , .

0

.

-1

Source: https://habr.com/ru/post/1703182/


All Articles