Search code examples
cstat

force a created file or folder to have a specific stat struct


I have a stat struct and I want to create a folder or a file and want to force it to have the same information as in the stat struct. For instance:

create file: test.txt
The file's last modification date is today 16.01.2014
change file's stat so that it shows:
last modified on 01.01.2050

How can I do that?


Solution

  • If you want to set the mtime/atime fields, you can use the utime() function. Use it after creating the file/directory.

    The mode field is set with chmod()

    The uid/gid, if you are root, are set with chown().

    The fields size and nlinks will have the correct value if you set the information they refer to (the data of the file and the directory structure that contains the file).

    But the ino, dev and ctime fields are set internally by the system and cannot be changed. No reason you would like to set them, anyway.