Detect when a file has been recreated using a stat watcher

Marc Lehmann schmorp at
Wed Dec 10 23:26:07 CET 2008

On Wed, Dec 10, 2008 at 09:12:35PM +0200, Graham Leggett <minfrin at> wrote:
> Given a stat watcher, how do I detect whether a file has been deleted  
> and recreated?

When a path is not there, st_nlink becomes zero, when it gets recreated
nlink goes to one again.

> I can detect when a file has been deleted by looking at stat.st_nlink,  
> but if the file is recreated st_nlink goes back to 1.


> I would have thought that st_ino would change if the file is recreated,
> but it doesn't seem to do so.

Then your filesystem reuses the same inode number.

> Can anyone confirm what the correct way is to handle this?

What do you perceive as incorrect? If you are concerned about races, when
you are not looking fast enough, then there is no way to avoid those.

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_    
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      pcg at
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list