Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

I don't think you can, but that's OK.

Much of the information in the struct stat is implementation details for the file system; the file "is the same" even if (for instance) it has a different inode.

You need to look at individual file-related library calls to make all the fields that are actually relevant match, though:

  • st_mode can be set by chmod()
  • st_uid and st_gid can be set by chown()
  • st_atime and st_mtime can be set by utime()

There's no single "set all the stat fields" function, since that wouldn't make sense.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top