/* ######################################################### # monitor dir or file change. # cc -o mdir -I. mdir.c # Flydragon ( ipv6china at comsenz dot com ) ######################################################### */ #include #include #include #ifndef __NR_inotify_init #if defined(__i386__) #define __NR_inotify_init 291 #define __NR_inotify_add_watch 292 #define __NR_inotify_rm_watch 293 #endif #endif _syscall0(int, inotify_init) _syscall3(int, inotify_add_watch, int, fd, const char *, path, __u32, mask) _syscall2(int, inotify_rm_watch, int, fd, __u32, mask) #define MAX_BUF_SIZE 1024 #define EVENT_NUM 16 #define IN_ACCESS 0x00000001 /* File was accessed */ #define IN_MODIFY 0x00000002 /* File was modified */ #define IN_ATTRIB 0x00000004 /* Metadata changed */ #define IN_CLOSE_WRITE 0x00000008 /* Writtable file was closed */ #define IN_CLOSE_NOWRITE 0x00000010 /* Unwrittable file closed */ #define IN_OPEN 0x00000020 /* File was opened */ #define IN_MOVED_FROM 0x00000040 /* File was moved from X */ #define IN_MOVED_TO 0x00000080 /* File was moved to Y */ #define IN_CREATE 0x00000100 /* Subfile was created */ #define IN_DELETE 0x00000200 /* Subfile was deleted */ #define IN_DELETE_SELF 0x00000400 /* Self was deleted */ #define IN_MOVE_SELF 0x00000800 /* Self was moved */ #define IN_ISDIR 0x40000000 /* event occurred against dir */ char * event_array[] = { "File was accessed", "File was modified", "File attributes were changed", "writtable file closed", "Unwrittable file closed", "File was opened", "File was moved from X", "File was moved to Y", "Subfile was created", "Subfile was deleted", "Self was deleted", "Self was moved", "", "Backing fs was unmounted", "Event queued overflowed", "File was ignored" }; int main(int argc,char **argv){ int fd,wd,i,len,tmp_len; char buffer[1024],strbuf[16]; struct inotify_event * event; char * offset = NULL; if(argc <2){ printf("Usage:%s /path/to/you_wang't_monitor\n",argv[0]); return 0; } //init inotify fd = inotify_init(); if(!fd){ printf("Fail init inotify"); } //add watch wd = inotify_add_watch(fd,argv[1],IN_MODIFY | IN_CREATE | IN_DELETE); if(!wd){ printf("Fail to add watch"); } //progess event while(len=read(fd,buffer,MAX_BUF_SIZE)){ offset=buffer; event = (struct inotify_event *)buffer; if (event->mask & IN_ISDIR) { memcpy(strbuf, "Direcotory", 11); } else { memcpy(strbuf, "File", 5); } printf("Object type: %s\n",strbuf); printf("Object name: %s\n", argv[1]); printf("Event mask: %08X\n", event->mask); for(i=0;imask & (1<len; event = (struct inotify_event *)(offset + tmp_len); offset += tmp_len; } }