Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a file's ctime with Python? [duplicate]

How can I set a Unix file's ctime?

(I'd much prefer an answer in terms of Python. If there's no way to do it with standard Python, then I suppose C is OK too.)

(Note: I know that one can use os.utime to set a file's atime and mtime. I am interested in setting ctime.)

(Note2: I hope that there's an answer that works for any POSIXoid Unix, but if not, I'm interested in Darwin and Ubuntu.)

like image 346
kjo Avatar asked May 21 '11 23:05

kjo


1 Answers

It is relatively trivial to set a files ctime to the current time. Just modify its mtime, flip a permission bit or even make a hardlink to it. It is, AFAIK, impossible to set a file's ctime to an arbitrary value using the system call API in any direct sort of way.

If you had root access you could set the system time, do something to the file to set the ctime to the current time, then set the system time back. Also you could bit-twiddle the inode data structure on disk. But both of these are really bad ideas for a whole host of reasons that I don't expect I should have to explain in detail.

like image 159
Omnifarious Avatar answered Oct 02 '22 23:10

Omnifarious