描述
Inotify API用于檢測文件系統變化的機制。Inotify可用于檢測單個文件,也可以檢測整個目錄。當檢測的對象是一個目錄的時候,目錄本身和目錄里的內容都會成為檢測的對象。
此種機制的出現的目的是當內核空間發生某種事件之后,可以立即通知到用戶空間。方便用戶做出具體的操作。
Inotify API
- inotify_init(void)
用于創建一個inotify的實例,然后返回inotify事件隊列的文件描述符。 同樣內核也提供了inotify_init1(int flags)接口函數,當flag等于0的時候,該函數等價于inotify_init(void)函數。
- inotify_add_watch(int fd, const char* pathname, uint32_t? mask)
該函數用于添加“watch list”,也就是檢測列表。 可以是一個新的watch,也可以是一個已經存在的watch。其中fd就是inotify_init的返回值,pathname是要檢測目錄或者文件的路徑,mask就是要檢測的事件類型。該函數成功返回的是一個unique的watch描述符。
- inotify_rm_watch(int fd, int wd)
用于從watch list種移除檢測的對象。
?
數據結構
內核使用struct inotify_event代表一個文件事件。當檢測的文件對象發生變化時,使用read系統調用就會返回一個或者多個inotify_event的文件事件對象。
-
struct inotify_event {
-
int wd; /* Watch descriptor */
-
uint32_t mask; /* Mask of events */
-
uint32_t cookie; /* Unique cookie associating related
-
events (for rename(2)) */
-
uint32_t len; /* Size of name field */
-
char name[]; /* Optional null-terminated name */
-
};
.wd:??????? 就是檢測的對象的watch descriptor
.mask:??? 檢測事件的mask
.cookie:? 和rename事件相關。
.len:??????? name字段的長度。
.name:??? 檢測對象的name。
可以看到name字段的長度是0,也就是變長的。因為檢測的對象的name不定,使用變長可以方便記錄檢測對象的name。
?
有關檢測的事件類型分為好幾種,如下:
-
IN_ACCESS File was accessed (read) (*).
-
IN_ATTRIB Metadata changed, e.g., permissions, timestamps, extended
-
attributes, link count (since Linux 2.6.25), UID, GID, etc.(*).
-
IN_CLOSE_WRITE File opened for writing was closed (*).
-
IN_CLOSE_NOWRITE File not opened for writing was closed (*).
-
IN_CREATE File/directory created in watched directory (*).
-
IN_DELETE File/directory deleted from watched directory (*).
-
IN_DELETE_SELF Watched file/directory was itself deleted.
-
IN_MODIFY File was modified (*).
-
IN_MOVE_SELF Watched file/directory was itself moved.
-
IN_MOVED_FROM File moved out of watched directory (*).
-
IN_MOVED_TO File moved into watched directory (*).
-
IN_OPEN File was opened (*).
注釋寫的很清楚,不再一一解釋了。
?
實例分析
-
#include <sys/inotify.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <stdio.h>
- ?
-
/*
-
struct inotify_event {
-
int wd; // Watch descriptor
-
uint32_t mask; // Mask of events
-
uint32_t cookie; // Unique cookie associating related events (for rename(2))
-
uint32_t len; // Size of name field
-
char name[]; // Optional null-terminated name
-
};
- ?
-
*/
- ?
-
int watch_inotify_events(int fd)
-
{
-
char event_buf[512];
-
int ret;
-
int event_pos = 0;
-
int event_size = 0;
-
struct inotify_event *event;
- ?
-
/*讀事件是否發生,沒有發生就會阻塞*/
-
ret = read(fd, event_buf, sizeof(event_buf));
- ?
-
/*如果read的返回值,小于inotify_event大小出現錯誤*/
-
if(ret < (int)sizeof(struct inotify_event))
-
{
-
printf("counld not get event!\n");
-
return -1;
-
}
- ?
-
/*因為read的返回值存在一個或者多個inotify_event對象,需要一個一個取出來處理*/
-
while( ret >= (int)sizeof(struct inotify_event) )
-
{
-
event = (struct inotify_event*)(event_buf + event_pos);
-
if(event->len)
-
{
-
if(event->mask & IN_CREATE)
-
{
-
printf("create file: %s\n",event->name);
-
}
-
else
-
{
-
printf("delete file: %s\n",event->name);
-
}
-
}
- ?
-
/*event_size就是一個事件的真正大小*/
-
event_size = sizeof(struct inotify_event) + event->len;
-
ret -= event_size;
-
event_pos += event_size;
-
}
- ?
-
return 0;
-
}
- ?
-
int main(int argc, char** argv)
-
{
-
int InotifyFd;
-
int ret;
- ?
-
if (argc != 2)
-
{
-
printf("Usage: %s <dir>\n", argv[0]);
-
return -1;
-
}
- ?
-
/*inotify初始化*/
-
InotifyFd = inotify_init();
-
if( InotifyFd == -1)
-
{
-
printf("inotify_init error!\n");
-
return -1;
-
}
- ?
-
/*添加watch對象*/
-
ret = inotify_add_watch(InotifyFd, argv[1], IN_CREATE | IN_DELETE);
- ?
-
/*處理事件*/
-
watch_inotify_events(InotifyFd);
- ?
-
/*刪除inotify的watch對象*/
-
if ( inotify_rm_watch(InotifyFd, ret) == -1)
-
{
-
printf("notify_rm_watch error!\n");
-
return -1;
-
}
- ?
-
/*關閉inotify描述符*/
-
close(InotifyFd);
- ?
-
return 0;
-
}
?
1. ?編譯代碼
gcc inotify.c -o inotify
2. 在tmp目錄下創建test目錄
mkdir /tmp/test
3. ?檢測/tmp/test目錄,使用inotify機制
./inotify /tmp/test &
4. ?在/tmp/test下創建1.txt文件
-
test$ touch /tmp/test/1.txt
-
create file: 1.txt