dotnet/runtime

Unix: LastWriteTime doesn't return correct value when is updated too fast

Open

#55,951 建立於 2021年7月19日

在 GitHub 查看
 (4 留言) (0 反應) (0 負責人)C# (5,445 fork)batch import
area-System.IOhelp wantedneeds-further-triageos-linux

倉庫指標

Star
 (17,886 star)
PR 合併指標
 (平均合併 12天 11小時) (30 天內合併 661 個 PR)

描述

Description

static void Main(string[] args)
{
    string fileName = Path.GetRandomFileName();
    File.Create(fileName).Dispose();
    Console.WriteLine("File created.");
    Console.WriteLine($"LastWriteTime: {File.GetLastWriteTime(fileName):o}");
    Console.WriteLine($"LastWriteTimeUtc: {File.GetLastWriteTimeUtc(fileName):o}");

    File.WriteAllText(fileName, "v1.1");
    Console.WriteLine();
    Console.WriteLine("File updated 1.");
    Console.WriteLine($"LastWriteTime: {File.GetLastWriteTime(fileName):o}");
    Console.WriteLine($"LastWriteTimeUtc: {File.GetLastWriteTimeUtc(fileName):o}");

    Console.WriteLine();
    Console.WriteLine($"Current time: {DateTime.Now:o}");
    Console.WriteLine($"Current time: {DateTime.UtcNow:o}");

    File.WriteAllText(fileName, "v1.2");
    Console.WriteLine();
    Console.WriteLine("File updated 2.");
    Console.WriteLine($"LastWriteTime: {File.GetLastWriteTime(fileName):o}");
    Console.WriteLine($"LastWriteTimeUtc: {File.GetLastWriteTimeUtc(fileName):o}");
}

Prints:

File created.
LastWriteTime: 2021-07-19T11:33:14.5400000-07:00
LastWriteTimeUtc: 2021-07-19T18:33:14.5400000Z

File updated 1.
LastWriteTime: 2021-07-19T11:33:14.5600000-07:00
LastWriteTimeUtc: 2021-07-19T18:33:14.5600000Z

Current time: 2021-07-19T11:33:14.5767122-07:00
Current time: 2021-07-19T18:33:14.5767587Z

File updated 2.
LastWriteTime: 2021-07-19T11:33:14.5600000-07:00
LastWriteTimeUtc: 2021-07-19T18:33:14.5600000Z

Note how after the second File.WriteAllText the LastWriteTime is the same as the first and the DateTime.Now in between is larger.

I see this behavior on WSL2 Ubuntu, but it probably affects any Unix. This works correctly on Windows.

Regression?

No

Other information

Related issues: https://github.com/dotnet/runtime/issues/26964 https://github.com/dotnet/runtime/issues/18870

貢獻者指南