2

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?

Roland
  • 7,525
  • 13
  • 61
  • 124
  • 1
    This documentation doesn't cover directories only regular files. – Lorinczy Zsigmond Feb 25 '20 at 04:23
  • @LorinczyZsigmond where are docs concerning directories? – Roland Feb 25 '20 at 07:15
  • 1
    Sorry, that I don't know. By the way, what is the actual problem you wish to solve? (cf: XY-problem) – Lorinczy Zsigmond Feb 26 '20 at 07:55
  • @LorinczyZsigmond no actual problem, I'm just confused. – Roland Feb 26 '20 at 08:26
  • 1
    From here: https://www.unixtutorial.org/atime-ctime-mtime-in-unix-filesystems "ctime shows when your file or directory got metadata changes – typically that's file ownership (username and/or group) and access permissions. ctime will also get updated if the file contents got changed." Check the last sentence. When you add a new file to the directory you modify the directory content. – Alex Sveshnikov Feb 27 '20 at 12:56
  • @Alex great. If you could also provide authoritative docs this would be the answer :) – Roland Feb 27 '20 at 13:07
  • What else would you expect a directory's ctime to indicate?? Oh, the metadata. I don't even know what that is. – Peter - Reinstate Monica Feb 27 '20 at 19:54
  • @Peter-ReinstateMonica Metadata is data about data. Directory is data and its contents are filenames (but not file contents). Directory metadata is its size, timestamps (one of which is when the data last changed), permissions. – Maxim Egorushkin Feb 27 '20 at 20:18

2 Answers2

2

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.

Alex Sveshnikov
  • 4,214
  • 1
  • 10
  • 26
  • Good answer! I would welcome a bit more clarification, i.e. that `mknod` is called when you create a new file. – Roland Feb 28 '20 at 12:15
  • 1
    Updated the answer with all POSIX call which i could find related to open/create/make. I don't know how to prove there are no more system calls like this without just looking at this long list and make sure by yourself :) – Alex Sveshnikov Feb 28 '20 at 12:35
0

The subdirectory listing is contained in a file, so when you update a file in the directory, it updates the metadata in the directory listing for that file (mtime, etc). Hence, the subdirectory itself is a file that has been modified.

Curt Evans
  • 346
  • 2
  • 4
  • Pedantically a directory is a file. Also, the question isn't about updating a file in the directory, but rather about _adding_ a file to the directory, thus necessitating, as you say, the directory file itself being modified to add the new entry to it. Updating an existing file should not change the metadata on the directory file(s) where that existing file is listed. – Greg A. Woods Feb 27 '20 at 20:42