On Mon, 26 Nov 2012 07:49:29 -0800, In a Very Dark Blue, China Blue
Condition wrote:
> Is st_mtime potentially updated with every write, when st_size is? Or is
> it only updated on major operations like open or close or etc?
It is supposed to update st_mtime with every write(), but there may be
filesystems (e.g. networked filesystems, particularly those designed for
other OSes) where this doesn't happen.
> I need to monitor if a file is being modified in any other process.
> Currently I check if st_mtime or st_size change, but I was wonderring if
> it is enough to check st_mtime.
Compared to the cost of a stat() call, the time required to check both
fields is negligible.
While any write() should update both st_size and st_mtime (if the
filesystem doesn't always report changes immediately, it will probably
update either both neither), st_mtime isn't guaranteed to have better than
one second resolution, so it isn't guaranteed that st_mtime will actually
change for each write(). OTOH, when a write() overwrites existing
data rather than extending the file, st_size won't change.