栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何监视包含所有子文件夹和文件的文件夹?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何监视包含所有子文件夹和文件的文件夹?

从inotify联机帮助页:

   IN_CREATE         File/directory created in watched directory (*).

可以通过捕获此事件来完成。

再次从手册页:

  Limitations and caveats       Inotify  monitoring  of  directories  is  not recursive: to monitor subdirectories under a directory, additional watches must be created.  This can take a significant       amount time for large directory trees.

因此,您将需要自己执行递归部分。您可以从这里开始看一个例子。您还应该看看项目的通知工具

注释中要求的示例 :它监视

/tmp/inotify1
/tmp/inotify2
查找创建的新文件并显示事件

#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/inotify.h>#define EVENT_SIZE  ( sizeof (struct inotify_event) )#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )int main( int argc, char **argv ) {    int length, i = 0;    int fd;    int wd[2];    char buffer[BUF_LEN];    fd = inotify_init();    if ( fd < 0 ) {        perror( "inotify_init" );    }    wd[0] = inotify_add_watch( fd, "/tmp/inotify1", IN_CREATE);    wd[1] = inotify_add_watch (fd, "/tmp/inotify2", IN_CREATE);    while (1){        struct inotify_event *event;        length = read( fd, buffer, BUF_LEN );        if ( length < 0 ) { perror( "read" );        }        event = ( struct inotify_event * ) &buffer[ i ];        if ( event->len ) { if (event->wd == wd[0]) printf("%sn", "In /tmp/inotify1: "); else printf("%sn", "In /tmp/inotify2: "); 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 );     } }        }    }    ( void ) inotify_rm_watch( fd, wd[0] );    ( void ) inotify_rm_watch( fd, wd[1]);    ( void ) close( fd );    exit( 0 );}

测试运行:

shadyabhi@archlinux ~ $ ./a.out In /tmp/inotify1: The file abhijeet was created.In /tmp/inotify2: The file rastogi was created.^Cshadyabhi@archlinux ~ $


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393016.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号