Recently writing a cloud backup project, one of the modules is to monitor the computer's local files, so I turned over the Linux/unix system Programming manual found INotify this framework for file monitoring
1. Overview
1) The inotify mechanism can be used to monitor files or directories. When the directory is monitored, the directory itself and the files below it are monitored and notified to the application when an event occurs.
2) inotify Monitoring mechanism is non-recursive, if the application intentionally monitors events within the entire directory subtree, it is necessary to initiate a inotify_add_watch () call to each directory in the tree
3) Monitor inotify file descriptors using Select (), poll (), Epoll (), and signal-driven I/O
2.inotify API
1) inotify_init () system call to create a new inotify instance
[CPP]View PlainCopy
- #include <sys/inotify.h>
- int inotify_init (void);
The return value of the function is a file descriptor, and we can simply understand that the file descriptor will save the set of events that occur in a directory similar to the one being monitored.
2) for the monitoring list of inotify instances referred to by FD, the system call Inotify_add_watch () can append new monitoring items
[CPP]View PlainCopy
- #include <sys/inotify.h>
- int inotify_add_watch (int fd,const char *pathname,uint32_t mask);
The parameter pathname is the file for the monitor item that you want to create, paying particular attention to the need to have read access to the file, which checks the file only once, and if the monitored file Read permission is modified at the time of monitoring, it will not affect the continued monitoring of this file
The parameter mask is a mask that defines the event that you want to monitor for pathname, and the return value of this function is a descriptor that uniquely refers to this monitoring item
3.inotify Events
In_access file is accessed
In_attrib file meta data changes
In_close_write closed file opened in order to write
In_create Creating a file or directory under the monitored directory
In_delete deleted files or directories within the monitored directory
in_delete_self deleted the monitored directory/file itself
In_modify file is modified
in_modify_self Mobile Monitored directory or file itself
In_moved_from files removed from monitored directory
In_moved_to moving files to a monitored directory
In_open file is open
In_all_events All of the output events above
In_moveIn_moved_from | The In_moved_to event is collectively
In_oneshot only monitors an event in pathname
In_onlydirpathname will fail when not in the directory
For a description of some of these events:
1) In_attrib event occurs when file metadata (for example, permissions, ownership, link count, extended attribute, user ID, or group ID, etc.) changes
2) in_delete_self occurs when a monitored object is deleted
3) In_more_self event occurs when renaming objects
4) One_shot allows only one event of the pathname to be monitored, and the monitoring item will automatically disappear from the watch list after the event occurs
4. Read the INotify Event
After registering the monitoring item in the monitoring list, the should be shipped program can read the event from the INotify file descriptor using read () to determine which events occurred. If no events have occurred at the time of reading, then read () blocks until an event occurs, and each call to read () returns a buffer containing one or more of the following types of struct
[CPP]View PlainCopy
- struct inotify_event
- {
- <span style="White-space:pre" > </span>int wd;
[CPP]View PlainCopy
- <span style="White-space:pre" > </span>uint32_t mask;
[CPP]View PlainCopy
- <span style="White-space:pre" > </span>uint32_t cookies;
[CPP]View PlainCopy
- <span style="White-space:pre" > </span>uint32_t len;
[CPP]View PlainCopy
- <span style="White-space:pre" > </span>char name[];
- }
The. Field WD named the monitor descriptor for the event, which was returned by a previous call to Inotify_add_watch (). Because read () reads all the events in the INotify file, but how should we differentiate what we read when inotify simultaneously monitor multiple directories or files, which WD has come to use, we can differentiate it with WD
The. Mask field returns a bitmask that describes the event
The. Cookie field ties related events together and is currently only used when renaming
The. Len field represents the number of bytes actually assigned to the name field
The. Name field is the file that is marked
5. Examples of simple procedures
[CPP]View PlainCopy
- #include <stdio.h>
- #include <assert.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/inotify.h>
- #include <limits.h>
- #include <fcntl.h>
- #define Buf_len 1000
- void Displayinotifyevent (struct inotify_event *i)
- {
- printf ("WD =%2d;", I->WD);
- if (I->cookie > 0)
- {
- printf ("Cokkie =%4d;", I->cookie);
- }
- printf ("mask =");
- if (I->mask & in_access) printf ("in_access\n");
- if (I->mask & in_delete_self) printf ("in_delete_self\n");
- if (I->mask & in_modify) printf ("in_modify\n");
- if (I->mask & In_open) printf ("in_open\n");
- if (len > 0)
- {
- printf ("name =%s\n", i->name);
- }
- }
- int main (int argc,char **argv)
- {
- int inotifyfd,wd,j;
- Char Buf[buf_len];
- ssize_t Numread;
- Char *p;
- struct inotify_event *event;
- int flags;
- if (argc < 2)
- {
- printf ("error\n");
- }
- INOTIFYFD = Inotify_init ();
- if (INOTIFYFD = =-1)
- {
- printf ("initialization failed");
- }
- WD = Inotify_add_watch (inotifyfd,argv[1],in_all_events);
- if (wd = =-1)
- {
- printf ("error\n");
- }
- printf ("watching%s using WD%d\n", ARGV[1],WD);
- While (1)
- {
- Numread = Read (Inotifyfd,buf,buf_len);
- if (Numread = =-1)
- {
- printf ("read error\n");
- }
- printf ("Read%ldbytes from INotify fd\n", (long) numread);
- For (P=buf;p < buf+numread;)
- {
- event = (struct inotify_event *) p;
- Displayinotifyevent (event);
- p+=sizeof (struct inotify_event) + event->len;
- }
- }
- return 0;
- }
Basic usage and precautions of inotify under Linux