码迷,mamicode.com
首页 > 其他好文 > 详细

2.6内核支持的inotify

时间:2017-06-13 17:20:48      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:let   tin   ret   pointer   success   sys   val   eof   type   

先上代码:利用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

标签:let   tin   ret   pointer   success   sys   val   eof   type   

原文地址:http://www.cnblogs.com/jcj-520/p/7000960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!