哪些操作会更新上次访问时间?

时间:2020-04-10 20:23:50

标签: windows winapi filesystems nfs ntfs

假设给定的文件系统正在跟踪上次访问时间(又名atime)-文件上的哪些操作导致atime更新?

据我所知:

  • 打开现有文件(以及随后关闭相关的handle / fd)不会更新atime
  • 读取/写入文件将更新atime(我想知道read-0-bytes操作是否会这样做)
  • (通过相关的Win32 API)读取文件安全描述符不会更新atime或其他文件属性

是否有详尽的操作列表来更新atime

1 个答案:

答案 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();
}

enter image description here

enter image description here

因此,读/写0字节不会更新上次访问时间