littlefs: set file creation/modification timestamps?



  • It's helpful that we can obtain the file creation time via uos.stat(path).

    Of course we all know, the obtained time is often far from being optimal - especially on boot when the system clock is cleared to zero.

    Nevertheless this timestamp could be used to detect if any new file has been freshly uploaded to a Gpy. ... if only we would be able to set the file timestamp to a known magic date.

    We are really missing the reverse of the stat()-command that should be quite easy to implement as it is already present in littlefs.

    What do you think?

    (In case anyone wonders what that tiny feature would be useful for: we store the program file twice (and with CRCs) in the Gpy filespace to guide against erratic flash changes [and correct them if possible] that are not so uncommon when long usage times are to be expected.)



  • @Gijs One can also:

    • open the file in r+ mode
    • read one byte
    • seek to 0
    • write the read byte back
    • close the file.

    That will change the time stamp as well.



  • Ah yes, now I have the functionality you need clear, sorry for the misunderstanding.
    There is indeed no possiblity to change the timestamp after the fact. I've been thinking about other workarounds but could not figure out any 'easy' solutions, other than making / porting the functionality or like syncing the RTC time with NTP in the firmware before we load new files. I'll put it on the feature list, but cannot make any promises about implementation. It's definitly faster to work out a solution comparing the littleFS repo with our adaptation to see how it is implemented.

    Edit: I just thought of something. What you could do, but again somewhat cumbersome, is to re-save the files to a different timestamp, like above (set the RTC to a different timestamp, open the file, read the content, delete the file, save the content to a new file with the same name). This should update the creation time of the file as desired. Though I do not know about your filesizes, and whether this will work with main.py and boot.py.



  • @Gijs: Thanx, but this is not exactly what I intended.

    The aim is to detect whether a file on Gpy is already there for a longer time ("old file") or if it had been uploaded only seconds ago ("new file").

    We know that the file timestamps in littlefs are operational and reflect the current system time when a file is created.

    When a file is uploaded from atom to a Gpy, this process starts with a reset and that resets the RTC of Gpy, too. So, all uploaded files receive timestamps within the first seconds after reboot. The exact value doesn't matter so much as it will always be smaller than, say, 1000 seconds.

    If we would be able to change the timestamp of a file to a different value - say 2011-11-11-11:11:11 -, we would thereby be able to mark a file as "this is no longer new".

    So we don't need stat() to obtain the timestamp but the exact opposite to SET the timestamp.
    This functionality is already present in littlefs but not made available to python users, yet.

    (Of course one can circumvent the problem by setting the system time forth an back at times and copy the files in the suited moment. But this is more than a little bit ugly and needlessly moving around files within the flash is not a good thing either.)



  • Hi,
    I just tried some example like this. When you set the RTC to a time (or sync it using NTP) it changes the file creation timestamp.

    from machine import RTC
    rtc = RTC()
    rtc.init((2014, 5, 1, 4, 13, 0, 0, 0))
    w = open('hello2.txt', 'wb')
    w.write('hello world')
    w.close()
    
    print(os.stat('/flash/hello2.txt'))
    

    the last line returns:

    (32768, 0, 0, 0, 0, 0, 11, 1398917580, 1398917580, 1398917580)
    

    Where the last three values are equal to time.time() for the file creation. The actual order should be accessed - modified - created, but that does not seem to work as expected. More documentation on os.stat() can be found here: https://docs.pycom.io/firmwareapi/micropython/uos/


Log in to reply
 

Pycom on Twitter