Is there anyway to modify stat information like mtime or ctime manually in Python or any language at all?
Asked Answered
T

4

2

I am trying the following code:

os.stat(path_name)[stat.ST_CTIME] = ctime

However, this gives the following error:

exceptions.TypeError: 'posix.stat_result' object does not support item assignment

Is there anyway to modify ctime?

Thanks!

Took answered 27/4, 2011 at 12:2 Comment(2)
Change the system time, create a new hard link to the file and rename it. Change the system time back.Mayotte
You'd be modifying the copy of the values you got from the OS, not what the OS actually uses.Plagiarize
M
9

os.utime(filename, timetuple) can be used to set the atime and mtime of a file. As far as I know there is no way to modify the ctime from userland without resorting to hacks such as playing with the clock or direct edition of the filesystem (which I really do not recommend), and this is true for any programming language (Python, Perl, C, C++...) : it's internal OS stuff, and you don't want to touch it.

See for example in the documentation of the touch command (http://www.delorie.com/gnu/docs/fileutils/fileutils_54.html):

Although touch provides options for changing two of the times -- the times of last access and modification -- of a file, there is actually a third one as well: the inode change time. This is often referred to as a file's ctime. The inode change time represents the time when the file's meta-information last changed. One common example of this is when the permissions of a file change. Changing the permissions doesn't access the file, so the atime doesn't change, nor does it modify the file, so the mtime doesn't change. Yet, something about the file itself has changed, and this must be noted somewhere. This is the job of the ctime field. This is necessary, so that, for example, a backup program can make a fresh copy of the file, including the new permissions value. Another operation that modifies a file's ctime without affecting the others is renaming. In any case, it is not possible, in normal operations, for a user to change the ctime field to a user-specified value.

Mouse answered 27/4, 2011 at 13:22 Comment(0)
F
3

GNU stroke implements the change-system-time trick to change ctime of a file. If that's what you want, GNU stroke does it for you: http://stroke.sourceforge.net/.

Festive answered 11/10, 2011 at 0:40 Comment(0)
I
2

There is no direct way to set change time, it gets updated whenever inode information changes, like ownership, link count, mode, etc..

Try setting the mode to the already set mode:

os.chmod(path_name, os.stat(path_name)[stat.ST_MODE])
Ideologist answered 27/4, 2011 at 12:26 Comment(3)
ctime is creation time, not "change time". You can set the mtime (and atime) arbitrarily, not the ctime.Vanbuskirk
@Wooble: from man fstat "time_t st_ctime; /* time of last status change */"Ideologist
@Wooble: ctime: time of last status change [en.wikipedia.org/wiki/Stat_(Unix)]Strophic
R
0

Ran into this lately, this is how I ended up doing it:

def _update_ctime(filename, cdatetime): 
    args = [ 
        'sudo', 
        'touch', 
        '-d', str(cdatetime)[:19], 
        filename, 
    ] 
    proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
    if proc.returncode != 0: print('=> Error changing time on file {}'.format(cdatetime)) 

Not error proof (depending on locales, etc, but can be improved...

Return answered 14/1, 2023 at 5:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.