假设给定的文件系统正在跟踪上次访问时间(又名atime
)-文件上的哪些操作导致atime
更新?
据我所知:
atime
atime
(我想知道read-0-bytes
操作是否会这样做)atime
或其他文件属性是否有详尽的操作列表来更新atime
?
答案 0 :(得分:0)
上次访问时间包括上次写入文件或目录,从读取文件的时间,或者对于可执行文件而言,上一次访问运行< / strong>。
其他操作,例如访问文件以检索要在资源管理器中显示的属性 或其他查看器,访问文件以获取其图标等。不会更新上次访问时间。
请参阅“ GetFileTime
- lpLastAccessTime
”,“ How do I access a file without updating its last-access time?”
更新:添加读写0字节和读写1字节的测试结果。
用于测试的代码:
void GetLastAccessTime(HANDLE hFile)
{
FILETIME ftAccess;
SYSTEMTIME stUTC, stLocal;
printf("Get last access time\n");
// Retrieve the file times for the file.
if (!GetFileTime(hFile, NULL, &ftAccess, NULL))
return;
// Convert the last-write time to local time.
FileTimeToSystemTime(&ftAccess, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
// Build a string showing the date and time.
wprintf(
L"%02d/%02d/%d %02d:%02d \n",
stLocal.wMonth, stLocal.wDay, stLocal.wYear,
stLocal.wHour, stLocal.wMinute);
}
int main()
{
HANDLE tFile = INVALID_HANDLE_VALUE;
printf("Open file\n");
// Open file
tFile = CreateFile(L"C:\\Users\\ritah\\Desktop\\test1.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE == tFile)
{
printf("CreateFile fails with error: %d\n", GetLastError());
getchar();
return 0;
}
printf("Sleep 60 seconds\n");
Sleep(60000);
GetLastAccessTime(tFile);
// Read 0 bytes
printf("Read 0 bytes\n");
WCHAR redBuf[10];
DWORD redBytes = 0;
if(!ReadFile(tFile, redBuf, 0, &redBytes, NULL))
{
printf("ReadFile fails with error: %d\n", GetLastError());
getchar();
return 0;
}
printf("Sleep 60 seconds\n");
Sleep(60000);
GetLastAccessTime(tFile);
// Write 0 bytes
printf("Write 0 bytes\n");
WCHAR writeBuf[] = L"write test";
DWORD writeBytes = 0;
if(!WriteFile(tFile, writeBuf, 0, &writeBytes, NULL))
{
printf("WriteFile fails with error: %d\n", GetLastError());
getchar();
return 0;
}
printf("Sleep 60 seconds\n");
Sleep(60000);
GetLastAccessTime(tFile);
getchar();
}
因此,读/写0
字节不会更新上次访问时间。