is similar to
inotify_init
with additional flags. If the flags are not specified,it behaves the same as
inotify_init
.
inotify_add_watch
adds a watch for a file or directory and specifies which events are to bewatched. Flags control whether events should be added to an existing watch,whether the watch should be done only if the path represents a directory,whether symbolic links should be followed or not, and whether the watch is aone-shot watch that should be stopped after the first event.
inotify_rm_watch
removes a watched item from a watch list.
read
reads a buffer containing information about one or more events.
close
closes the file descriptor, and removes any watches still remaining on thatdescriptor. When all file descriptors for an instance are closed, the resourcesand underlying object are freed so the kernel can reuse them.So, a typical monitoring program will do the following:1. Use inotify_init to open a file descriptor2. Add one or more watches3. Wait for events4. Process events, then return to wait for more5. When no more watches are active or upon some signal, close the filedescriptor, clean up, and exit.In the next section, you'll see the events you can watch, and how they work in oursample program. Finally you'll see how the event monitoring works.
Notifications
When your application reads a notification, a sequence of one or more events isread into a buffer you provide. Events are returned in a variable length structure asshown in Listing 1. If the amount of data fills your buffer, you may need to handle thecase of partial event information or a partial name for the last entry.
Listing 1. The event structure for inotify