Note that there are some explanatory texts on larger screens.

plurals
  1. PODocumented SetFileTime() procedure to preserve LastAccessTime not working for UNC paths
    primarykey
    data
    text
    <p>There is a <a href="http://msdn.microsoft.com/en-us/library/ms724933%28v=vs.85%29.aspx" rel="nofollow">documented</a> method from Microsoft on how to prevent the last access time from being updated by operations on a file handle. I have used this method successfully accessing files locally, but not remotely. Should this work for remote files as well?</p> <pre><code>#include &lt;windows.h&gt; #include &lt;tchar.h&gt; #include &lt;stdio.h&gt; #include &lt;assert.h&gt; #include &lt;string&gt; #define FAIL(x) { printf(x" FAILED: %d\n", GetLastError()); exit(-1); } static std::wstring GetDateTimeString(SYSTEMTIME* datetime) { TCHAR buffer[256]; TCHAR* current = buffer; int idx = GetDateFormat(LOCALE_SYSTEM_DEFAULT,NULL, datetime, L"dd-MMM-yyyy", current, 256); if(idx &gt; 0) { buffer[idx -1] = ' '; current += idx; int idx2 = GetTimeFormat(LOCALE_SYSTEM_DEFAULT,NULL, datetime, L"HH:mm:ss", current, 256 - idx); if(idx2 &gt; 0) { current += idx2 - 1; _sntprintf(current, 256 - (idx + idx2), L".%d", datetime-&gt;wMilliseconds); } else FAIL("GetTimeFormat"); } else FAIL("GetDateFormat"); return std::wstring(buffer); } static void ReportLastAccessTime(TCHAR* path) { WIN32_FILE_ATTRIBUTE_DATA data; if(GetFileAttributesEx(path, GetFileExInfoStandard, &amp;data)) { SYSTEMTIME accessSysTime; if(FileTimeToSystemTime(&amp;data.ftLastAccessTime, &amp;accessSysTime)) { SYSTEMTIME nowSysTime; GetSystemTime(&amp;nowSysTime); printf("(%S) LastAccess=%S\n", GetDateTimeString(&amp;nowSysTime).c_str(), GetDateTimeString(&amp;accessSysTime).c_str()); } else FAIL("FileTimeToSystemTime"); } else FAIL("GetFileAttributesEx"); } static void TestPreservingLastAccessTime(TCHAR* path) { printf("Checking times before opening the file handle...\n"); ReportLastAccessTime(path); HANDLE hFile = CreateFile( path, GENERIC_READ | FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if(hFile != INVALID_HANDLE_VALUE) { static const FILETIME ftLeaveUnchanged = { 0xFFFFFFFF, 0xFFFFFFFF }; printf("Handle open, SetFileTime -1...\n"); if(SetFileTime(hFile, NULL, &amp;ftLeaveUnchanged, NULL)) { char buf[512]; DWORD numRead = 0; printf("Checking times after opening handle and SetFileTime...\n"); ReportLastAccessTime(path); printf("Reading the file...\n"); long total = 0; while (ReadFile(hFile, buf, sizeof(buf), &amp;numRead, NULL)) { if (numRead == 0) break; total += numRead; printf("Read: %d bytes (total=%d)\n", numRead, total); Sleep(1000); } printf("Checking times after reading the file...\n"); ReportLastAccessTime(path); printf("Sleeping 30\n"); Sleep(30 * 1000); printf("Checking times after reading the file before closing the file...\n"); ReportLastAccessTime(path); printf("Closing the file\n"); CloseHandle(hFile); printf("Sleeping 30\n"); Sleep(30 * 1000); printf("Checking times after closing the file...\n"); ReportLastAccessTime(path); } else { CloseHandle(hFile); FAIL("SetFileTime"); } } else FAIL("CreateFile"); } int _tmain(int argc, TCHAR* argv[]) { TCHAR* localPath = L"C:\\tmp\\file.txt"; TCHAR* uncPath = L"\\\\localhost\\C$\\tmp\\file.txt"; printf("Testing local path: %S\n", localPath); TestPreservingLastAccessTime(localPath); printf("Sleeping 30\n"); Sleep(30 * 1000); printf("Testing UNC path: %S\n", uncPath); TestPreservingLastAccessTime(uncPath); return 0; } </code></pre> <p>The results of running the above:</p> <pre><code>Testing local path: C:\tmp\file.txt Checking times before opening the file handle... (08-Nov-2011 21:06:18.432) LastAccess=08-Nov-2011 19:50:20.833 Handle open, SetFileTime -1... Checking times after opening handle and SetFileTime... (08-Nov-2011 21:06:18.432) LastAccess=08-Nov-2011 19:50:20.833 Reading the file... Read: 512 bytes (total=512) .... Read: 512 bytes (total=7680) Checking times after reading the file... (08-Nov-2011 21:06:33.432) LastAccess=08-Nov-2011 19:50:20.833 Sleeping 30 Checking times after reading the file before closing the file... (08-Nov-2011 21:07:03.432) LastAccess=08-Nov-2011 19:50:20.833 Closing the file Sleeping 30 Checking times after closing the file... (08-Nov-2011 21:07:33.432) LastAccess=08-Nov-2011 19:50:20.833 Sleeping 30 Testing UNC path: \\localhost\C$\tmp\file.txt Checking times before opening the file handle... (08-Nov-2011 21:08:03.448) LastAccess=08-Nov-2011 19:50:20.833 Handle open, SetFileTime -1... Checking times after opening handle and SetFileTime... (08-Nov-2011 21:08:03.448) LastAccess=08-Nov-2011 19:50:20.833 Reading the file... Read: 512 bytes (total=512) .... Read: 512 bytes (total=7680) Checking times after reading the file... (08-Nov-2011 21:08:18.448) LastAccess=08-Nov-2011 19:50:20.833 Sleeping 30 Checking times after reading the file before closing the file... (08-Nov-2011 21:08:48.448) LastAccess=08-Nov-2011 19:50:20.833 Closing the file Sleeping 30 Checking times after closing the file... (08-Nov-2011 21:09:18.448) LastAccess=08-Nov-2011 21:08:48.448 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload