2.6內核支持的inotify
阿新 • • 發佈:2017-06-13
內容 main != fine fit long long highlight dir eat
先上代碼:利用inotify函數實現簡易的tail命令 不僅可以看文件新增內容 也可以看文件夾下的變化。
#include <stdio.h>//printf #include <string.h> //strcmp #include <sys/inotify.h>//inotify_init inotify_add_watch.... #include <sys/select.h>//select timeval #include <unistd.h>//close #include <signal.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) #define ERR_EXIT(msg,flag) {perror(msg);goto flag;} #define PATH "xiecheng.c" FILE *fp = NULL; int fd = 0; int wd = 0; void signal_(int signalID) { if(signalID == SIGINT) printf("ctrl c. \n"); inotify_rm_watch( fd, wd ); close( fd ); if(fp > 0) fclose(fp); _exit(0); } int main( int argc, char **argv ) { int length, i = 0; char buffer[BUF_LEN]; long long curFilePointer = 0; char ch = 0; signal(SIGINT,signal_); if(argc < 2) printf("exe file.\r"); if((fd = inotify_init()) < 0) ERR_EXIT("inotify_init",inotify_init_err); if( (wd = inotify_add_watch( fd, argv[1], IN_MODIFY | IN_CREATE | IN_DELETE ) ) < 0) { printf("%s\n",argv[1]); ERR_EXIT("inofity_add_watch", inotify_add_watch_err); } struct stat statbuf; if (stat(argv[1], &statbuf) == 0) { curFilePointer = statbuf.st_size; fp = fopen(argv[1], "r"); if(fp < 0) { ERR_EXIT("fopen", inotify_add_watch_err); } fseek(fp,curFilePointer,SEEK_SET); } fd_set rfd; while(true) { int retval; FD_ZERO(&rfd); FD_SET(fd, &rfd); retval = select(fd + 1, &rfd, NULL, NULL, NULL); if(retval == 0) continue; else if(retval == -1) ERR_EXIT("select",select_err); // retval > 0 length = read( fd, buffer, BUF_LEN ); if(length < 0) ERR_EXIT("read",read_err); //length >= 0 int i = 0; while ( i < length ) { struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ]; if ( event->len ) //之前文件名還在的現在為空了。 { if ( event->mask & IN_CREATE ) { if ( event->mask & IN_ISDIR ) printf( "The directory %s was created.\n", event->name ); else printf( "The file %s was created.\n", event->name ); //if(strcmp(event->name,"kill") == 0) // ERR_EXIT("success exit",success_exit); } else if ( event->mask & IN_DELETE ) { if ( event->mask & IN_ISDIR ) printf( "The directory %s was deleted.\n", event->name ); else printf( "The file %s was deleted.\n", event->name ); } else if ( event->mask & IN_MODIFY ) { if ( event->mask & IN_ISDIR ) printf( "The directory %s was modified.\n", event->name ); else { printf( "The file %s was modified.\n", event->name ); } } } else { if ( event->mask & IN_MODIFY ) { if ( event->mask & IN_ISDIR ) ;//printf( "The directory %s was modified.\n", event->name ); else if(fp > 0) { fseek(fp,curFilePointer,SEEK_SET); while ((ch=fgetc(fp))!=EOF) { putchar(ch); curFilePointer++; } fflush(stdout); } } } i += EVENT_SIZE + event->len; } } success_exit: ( void ) inotify_rm_watch( fd, wd ); ( void ) close( fd ); if(fp > 0) fclose(fp); return 0; read_err: select_err: inotify_add_watch_err: ( void ) inotify_rm_watch( fd, wd ); inotify_init_err: ( void ) close( fd ); return -1; }
2.6內核支持的inotify