I want to watch a file created and written in a directory by a process. I don't have access to inotifywait
tool, so I'm using just the inotify library calls. I start with IN_CREATE
to monitor that the file was created. If the file of interest was created, then I drop in a CLOSE_WRITE
event to be monitored.
This scheme kind of works, except in the case that the file was already written and closed by the time my handler processes the IN_CREATE
event, and I miss that write event.
What is the best way to work around this situation? I was considering using the stat
function to use atim
, mtim
, but wasn't sure if that would have any gotchas. For example, I was planning to check if the current timestamp is greater than st_ctim, and if st_ctim is greater than st_atim. These two conditions would indicate that the file status changed (accessed and changed) before the current time.
IN_CLOSE_WRITE
to my add_watch call, because the path I provided was of a directory not a file. – rookie Sep 12 '22 at 02:27IN_CREATE
at all. Just theIN_CLOSE_WRITE
on the directory would be enough. If you need to process the incoming files, create a loop like: if dir is empty run a poll() on it, upon receivingIN_CLOSE_WRITE
move all files from incoming dir to a secondary "processing" dir, process files, check is incoming dir empty, if not repeat to processing, if in-dir is empty - go to poll(). – White Owl Sep 12 '22 at 12:18CLOSE_WRITE
, the file gets closed and I miss that event. I might still have to overcome that race – rookie Sep 12 '22 at 20:23