为什么在其中创建新文件时目录的更改时间(ctime)会发生变化?

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

根据 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.

然而,当我在一个目录中创建一个新文件,然后在目录中 运行 istat 命令时,我注意到目录的 ctime aka "update" 时间已经改变。我认为 ctime 应该只在您更改目录的元数据时更改?

子目录列表包含在文件中,因此当您更新目录中的文件时,它会更新目录列表中该文件的元数据(mtime 等)。因此,子目录本身就是一个被修改过的文件。

根据stat()系统调用规范:

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

对应的File Times Update文档中:

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.

POSIX system calls 的列表包含以下与在目录内创建对象相关的调用:

  • 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.