linux监控目录新文件系统,使用 inotify 监控 Linux 文件系统事件

/* This method does the dirty work of determining what happened,

then allows us to act appropriately

*/

void handle_event (struct inotify_event *event)

{

/* If the event was associated with a filename, we will store it here */

char * cur_event_filename = NULL;

/* This is the watch descriptor the event occurred on */

int cur_event_wd = event->wd;

if (event->len)

{

cur_event_filename = event->filename;

}

printf("FILENAME=%s\n", cur_event_filename);

printf("\n");

/* Perform event dependent handler routines */

/* The mask is the magic that tells us what file operation occurred */

switch (event->mask)

{

/* File was accessed */

case IN_ACCESS:

printf("ACCESS EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* File was modified */

case IN_MODIFY:

printf("MODIFY EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* File changed attributes */

case IN_ATTRIB:

printf("ATTRIB EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* File was closed */

case IN_CLOSE:

printf("CLOSE EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* File was opened */

case IN_OPEN:

printf("OPEN EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* File was moved from X */

case IN_MOVED_FROM:

printf("MOVE_FROM EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* File was moved to X */

case IN_MOVED_TO:

printf("MOVE_TO EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* Subdir was deleted */

case IN_DELETE_SUBDIR:

printf("DELETE_SUBDIR EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* File was deleted */

case IN_DELETE_FILE:

printf("DELETE_FILE EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* Subdir was created */

case IN_CREATE_SUBDIR:

printf("CREATE_SUBDIR EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* File was created */

case IN_CREATE_FILE:

printf("CREATE_FILE EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* Watched entry was deleted */

case IN_DELETE_SELF:

printf("DELETE_SELF EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* Backing FS was unmounted */

case IN_UNMOUNT:

printf("UNMOUNT EVENT OCCURRED: File \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

/* Too many FS events were received without reading them

some event notifications were potentially lost.*/

case IN_Q_OVERFLOW:

printf("Warning: AN OVERFLOW EVENT OCCURRED: \n");

break;

case IN_IGNORED:

printf("IGNORED EVENT OCCURRED: \n");

break;

/* Some unknown message received */

default:

printf ("UNKNOWN EVENT OCCURRED for file \"%s\" on WD #%i\n",

cur_event_filename, cur_event_wd);

break;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值