Re: Detect when a file has been recreated using a stat watcher

2008-12-22 Thread Graham Leggett

Marc Lehmann wrote:

I have an fd open to the file, which will never increase in size because  
the file has been deleted, but my app doesn't yet know that.


Is there a way to detect this?


An ev_stat watcher should reliably detect that case, as logfile rotation
is not very often done, so the ctime should reliably change.


I tried this out on MacOSX 10.5, and found that monitoring ctime for 
file rotation changes were giving me lots of false positives (the file 
wasn't rotated at all, but every few seconds I was detecting changes).


Turns out according to the man pages for the stat structure, ctime is 
described as follows:


 struct timespec st_ctimespec;  /* time of last file status 
change */


The clarification of ctime further down is as follows:

 st_ctime Time when file status was last changed (inode 
data modi-
  fication).  Changed by the chmod(2), chown(2), 
link(2),
  mknod(2), rename(2), unlink(2), utimes(2) and 
write(2)

  system calls.

The fact that ctime is updated on write() means that in theory, it 
cannot be used to detect file rotation.


There is also a stat64 structure, with higher precision on the file 
times, as follows:


 struct timespec st_ctimespec; /* time of last status change */
 struct timespec st_birthtimespec; /* time of file 
creation(birth) */



This second stat64 definition would imply that last status change and 
file creation (what I am looking for) are two separate things.


My question is, is libev using the stat64 structure by way of comparison?

Is there a way for me to detect whether the stat or the stat64 structure 
is in use at a given time?


Is there a cross platform method to determine file rotation, by looking 
at the attr and prev variables returned by a stat event?


Regards,
Graham
--


smime.p7s
Description: S/MIME Cryptographic Signature
___
libev mailing list
libev@lists.schmorp.de
http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev

Re: Detect when a file has been recreated using a stat watcher

2008-12-22 Thread Marc Lehmann
On Mon, Dec 22, 2008 at 08:11:57PM +0200, Graham Leggett minf...@sharp.fm 
wrote:
 I tried this out on MacOSX 10.5, and found that monitoring ctime for  
 file rotation changes were giving me lots of false positives (the file  
 wasn't rotated at all, but every few seconds I was detecting changes).

Yes, likely.

  st_ctime Time when file status was last changed (inode data 

 The fact that ctime is updated on write() means that in theory, it  
 cannot be used to detect file rotation.

Just fyi: write *may* update the ctime, but doesn't do so in every
case. It will likely do so in your case, however, as the size changes.

I don't understand why it can't be used to detetc logfile rottaion,
though?  The ctime changes, too, with logfile rotation.

 There is also a stat64 structure, with higher precision on the file  
 times, as follows:

Please note that the *structure* has more space for this, but most
filesystems do not actually support fractional times.

So using stat64 doesn't magically give you higher accuracy.

 This second stat64 definition would imply that last status change and  
 file creation (what I am looking for) are two separate things.

Absolutely. But the ctime likely changes on file creation, or the size, or
something else (such as the inode number).

This all depend son how your logfiles get rotated - the file might get
trucnated, deleted or somethign else might happen.

 My question is, is libev using the stat64 structure by way of comparison?

No. And it wouldn't help your case unless you require the logifle to be on
a specific filesystem supporting higher resolution timestamps. And even
then, the higher resolution might sitll just be centiseconds or so.

All this doesn't help you. The documentation (that you somehow fail to
know, for inexplicable reasons) explains this case in mroe details and
offers ways to implement this properly in your app.

 Is there a way for me to detect whether the stat or the stat64 structure  
 is in use at a given time?

No, that usually depends on how libev was compiled.

Note that stat64 on most unices doesn't have a higher resolution, as stat
already provides that, and stat64 is only for LFS.

some systems (freebsd for example) sanely default their compilation
environment to lfs, others (debian) inexplicably default to 32 bit and
try to patch eahc and every app to separately enable lfs, which would
presumably give you struct stat64.

 Is there a cross platform method to determine file rotation, by looking  
 at the attr and prev variables returned by a stat event?

Yes, but for some reason you claim it wouldn't work.

-- 
The choice of a   Deliantra, the free code+content MORPG
  -==- _GNU_  http://www.deliantra.net
  ==-- _   generation
  ---==---(_)__  __   __  Marc Lehmann
  --==---/ / _ \/ // /\ \/ /  p...@goof.com
  -=/_/_//_/\_,_/ /_/\_\

___
libev mailing list
libev@lists.schmorp.de
http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev