4

I'm using inotify to monitor various directories on various partitions (which are possibly located on different hard disks). To be sure to have collected all events which have occurred until a certain point in time T, I'm touching a special file in my home directory and wait for inotify to report this modification. Once I've received this notification, can I be sure that I've also received all events for all modifications before T (for all directories and all partitions)?

mstrap
  • 16,808
  • 10
  • 56
  • 86
  • Out of curiosity, why do you need to know which event actually occurs earlier, and what criteria would you use in order to tell which event actually occurs earlier? – n. m. could be an AI May 21 '14 at 09:47

2 Answers2

3

I'm uncertain about whether this works for watches on different filesystems on the same inotify instance, but can speak with authority that the technique does work in general: we use it in Watchman (we describe it here: https://facebook.github.io/watchman/docs/cookies.html)

We assumed that this wouldn't be ordered correctly across filesystem boundaries and create one instance per watched root; this makes it simpler for us to track and associate events properly. We also have to deal with fsevents, kqueue and other watching implementations, so we try to avoid coupling too closely to the underlying implementation.

Depending on what your precise use case is, you may be able to get away with one instance per filesystem and touch a special file in the root of each at your time T. Provided that you've observed both of your special file changes, you know you've seen everything up to time T, and perhaps a little more. If the "perhaps a little more" part isn't a deal breaker then you're golden.

Wez Furlong
  • 4,727
  • 1
  • 29
  • 34
2

The inotify documentation in the kernel says "that each [inotify] instance is associated with a unique, ordered queue." So, I think that events related to the watches added to a given instance (created with inotify_init()) are received in the same order they occur.

Christophe Vu-Brugier
  • 2,645
  • 26
  • 21
  • I had read that part of the documentation, however it does not answer my question (though I agree, it's likely that it's the case). – mstrap May 21 '14 at 09:35
  • There is this other part in the documentation: "Q: What is the design decision behind using an-fd-per-instance as opposed to an fd-per-watch?" that explains that the inotify instance allows to preserve event ordering. – Christophe Vu-Brugier May 21 '14 at 09:45
  • Thanks, Christophe, I think this is the statement which I was looking for. Let's put it, including the link to https://www.kernel.org/doc/Documentation/filesystems/inotify.txt, to the answer and I'll accept it. – mstrap May 22 '14 at 13:12