when will the mtime of a directory be changed

Recently I was working on the refactory of the resource building process of our site with Grunt. I came across a problem that the grunt.file.copy did not provide an option to preserve the timestamp(ctime, mtime) of file or directory. So I had to implement it myself. I made a research on when will the mtime of a directory be changed, the conclusion as follows.

A directory is a special kind of file; its contents is a set of (filename, i-node) tuples. Thus directory’s mtime is updated whenever a tuple(file or subdirectory) is added, removed or renamed .

  • Modifying the contents of a file within the directory does not change the directory itself, nor does updating the mtime of a file or a subdirectory.
  • When file’s metadata changes (owner, group, mode, atime, ctime, mtime - hosted in i-node), directory’s mtime is NOT updated (neither on the file nor containing directory) but the file’s ctime does.
  • mtime of a subdirectory will not influence the containing directory unless the subdirectory’s is renamed. So that is said mtime of a subdirectory will not bubbled to the root.

I have made a PR (preserve file timestamp attributes) to the grunt-contrib-copy, and you can use it directly now.


comments powered by Disqus