This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients Cezary.Wagner, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Date 2020-06-24.20:42:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1593031320.77.0.230433671555.issue41106@roundup.psfhosted.org>
In-reply-to
Content
In FSBO [1] section 6 "Time Stamps", note that the LastWriteTime value gets updated when an IRP_MJ_FLUSH_BUFFERS is processed. In the Windows API, this is a FlushFileBuffers [2] call. In the C runtime, it's a _commit [3] call, which is an os.fsync [4] call in Python. Calling the latter will update the directory entry for the file. 

For an example implementation in the FAT32 filesystem, see FatCommonFlushBuffers [5]. Note in the UserFileOpen case that it flushes any cached data via FatFlushFile and then updates the directory entry from the file control block (FCB) via FatUpdateDirentFromFcb, and finally it  flushes the parent directory control blocks (DCBs) -- and possibly also the volume.

Example with os.fsync:

    import os
    import time
    import datetime

    UPDATE_DIR = True

    FILEPATH = 'C:/Temp/test/spam.txt'

    def scan(filepath):
        dir_path, filename = os.path.split(filepath)
        with os.scandir(dir_path) as iter_dir:
            for entry in iter_dir:
                if entry.name == filename:
                    return entry

    with open(FILEPATH, 'w') as f:
        while True:
            print('spam', file=f, flush=True)
            if UPDATE_DIR:
                os.fsync(f.fileno())
            entry = scan(FILEPATH)
            stat_result = entry.stat()
            now = datetime.datetime.now()
            print(f'st_mtime: {stat_result.st_mtime:0.3f}, '
                  f'delta_t: {now.timestamp() - stat_result.st_mtime:0.3f}')
            time.sleep(1.0)


[1] https://go.microsoft.com/fwlink/?LinkId=140636
[2] https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers
[3] https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/commit?view=vs-2019
[4] https://docs.python.org/3/library/os.html#os.fsync
[5] https://github.com/microsoft/Windows-driver-samples/blob/9afd93066dfd9db12f66099cf9ec44b6fd734b2d/filesys/fastfat/flush.c#L145
History
Date User Action Args
2020-06-24 20:42:00eryksunsetrecipients: + eryksun, paul.moore, tim.golden, zach.ware, steve.dower, Cezary.Wagner
2020-06-24 20:42:00eryksunsetmessageid: <1593031320.77.0.230433671555.issue41106@roundup.psfhosted.org>
2020-06-24 20:42:00eryksunlinkissue41106 messages
2020-06-24 20:42:00eryksuncreate