cbtrfs

API to set the timestamps on files & directories in btrfs


BTRFS files/directories contains the timestamps:

Is there some API where I could set these all these timestamps for a file? I googled a bit but haven't found anything yet.

Programming languages doesn't matter, I would expect there to be some C API, but python is fine too and would be nicer.


Solution

  • From C, the mtime and atime can be set using utime(2) and its relatives. utime(2) itself gives you seconds precision, utimes(2) has microseconds, and utimensat(2) gives you nanoseconds. There are variants like futime if you have a file handle instead of a file name.

    Python can provide the same via the os.utime function.

    Traditionally it is not possible to arbitrarily modify the otime or ctime, other than by manually editing the raw filesystem. I am not aware that Linux has provided any kernel API to modify them. Of course, you can update the ctime to the current time by changing its status in some way, and you can update the otime to the current time by deleting and recreating the file. In principle you can set them to a different time by changing the system clock first (if you are root), but this is likely to mess up lots of other stuff on the system and is probably a bad idea.