Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Nov 2003 21:28:09 -0500
From:      ari <edelkind-freebsd-hackers@episec.com>
To:        freebsd-hackers@freebsd.org
Subject:   Re: mtime and directories...
Message-ID:  <20031119022809.GF58437@episec.com>
In-Reply-To: <383537BA-1A23-11D8-B220-000A95775140@battleface.com>
References:  <383537BA-1A23-11D8-B220-000A95775140@battleface.com>

next in thread | previous in thread | raw e-mail | index | archive | help
lists@battleface.com said this stuff:

> Changing a file, of course, results in a change to its modification 
> time. Am I correct in determining that the mtime of the enclosing 
> directory is also updated?

The mtime of a directory is updated when the directory "file" changes.
Directory "file"s contain filenames and their associated inode numbers.
Modify any of that information (e.g., create a new file, remove or
rename an existing file), and the mtime will be changed.  Changing the
ctime or mtime of a file within a directory does not modify that of the
directory itself.

> The reason I'm asking this is because I need to be able to determine if 
> any file has changed within a directory from a shell script, Perl, or 
> Python. All I need to know is if at least one file has changed. I don't 
> need to find all such files. [Sidelight: anyone know how, if possible, 
> to stop find after finding one file? I can't figure this one out alone.]

% find /some/dir <criteria> |head -1

In this case, 'head' will exit after reading one line, causing 'find' to
receive a SIGPIPE if it tries writing more data.  The 'find' command
doesn't typically trap SIGPIPE, so the process will exit.  Of course, it
won't receive the signal immediately --- only if it calls write(2)
again, and on descriptor 1.  If 'find' matches only one file, it will
wind up seeking through the entire tree.  This may not be sufficient for
your application.

To truly make find exit after it matches one file, you can do something
along the lines of:

% prescript find /some/dir <criteria> -exec postscript '{}' \;

... where prescript contains a somewhat more error-tolerant version of
the following:

  #!/bin/sh
  echo $$ >/pid/dir/find.pid
  exec $@

... and postscript contains a somewhat more useful version of the
following:

  #!/bin/sh
  dostuffwith $@ &
  pid=`cat /pid/dir/find.pid`
  rm -f /pid/dir/find.pid
  kill $pid


> My initial solution was to use 'find' to find any files newer than a 
> marker file. However, I believe I've determined that the mtime of 
> enclosing directories also changes to reflect the last file that was 
> updated within its hierarchy. So, it makes sense that I simply check 
> the directory that contains the marker file.

Again, a directory's modification time is updated only if the actual
directory contents have changed, not the contents or inodes of the files
within it.

ari



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20031119022809.GF58437>