Eryk Sun <eryk...@gmail.com> added the comment:

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

----------
nosy: +eryksun

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41106>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to