Why does the change time(ctime) of a directory change when creating a new file in it?

Roland :

According to the documentation:

Change time (ctime) This marks when a file's metadata was changed, such as permissions or ownership. This is also known as the "update" time in some documentation.

Yet when I create a new file in a directory and run the istat command on the directory afterwards I notice that the ctime aka "update" time of the directory has changed. I thought that the ctime should only change if you change the metadata of the directory?

Alex :

According to stat() system call specification:

The stat() function shall update any time-related fields (as described in XBD File Times Update), before writing into the stat structure.

In the corresponding File Times Update document:

Each function or utility in POSIX.1-2017 that reads or writes data (even if the data does not change) or performs an operation to change file status (even if the file status does not change) indicates which of the appropriate timestamps shall be marked for update.

The list of POSIX system calls contains the following calls related to creation of objects inside a directory:

  • link()

    Upon successful completion, link() shall mark for update the last file status change timestamp of the file. Also, the last data modification and last file status change timestamps of the directory that contains the new entry shall be marked for update.

  • mkdir()

    Upon successful completion, mkdir() shall mark for update the last data access, last data modification, and last file status change timestamps of the directory. Also, the last data modification and last file status change timestamps of the directory that contains the new entry shall be marked for update.

  • mkfifo()

    Upon successful completion, mkfifo() shall mark for update the last data access, last data modification, and last file status change timestamps of the file. Also, the last data modification and last file status change timestamps of the directory that contains the new entry shall be marked for update.

  • mknod()

    Upon successful completion, mknod() shall mark for update the last data access, last data modification, and last file status change timestamps of the file. Also, the last data modification and last file status change timestamps of the directory that contains the new entry shall be marked for update.

  • open()

    If O_CREAT is set and the file did not previously exist, upon successful completion, open() shall mark for update the last data access, last data modification, and last file status change timestamps of the file and the last data modification and last file status change timestamps of the parent directory.

  • symlink()

    Upon successful completion, symlink() shall mark for update the last data access, last data modification, and last file status change timestamps of the symbolic link. Also, the last data modification and last file status change timestamps of the directory that contains the new entry shall be marked for update.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=24358&siteId=1