首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >inotify,inotify_add_watch()用于监视多个目录,c++

inotify,inotify_add_watch()用于监视多个目录,c++
EN

Stack Overflow用户
提问于 2015-10-15 10:53:50
回答 1查看 2K关注 0票数 0

我使用inotify表()来监视多个目录上的活动。是否可以使用循环添加多个文件路径(目录)。我有以下示例代码,但没有收到任何通知。

样本代码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <vector>
#include <sstream> 
#include <fstream> 
#include <iostream>
#include <cstring>

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )
using namespace std;
int main( int argc, char **argv ) 
{

    //---------------------------------------------------
    vector<string> sensitive_dir_path;//a vector to store file path(directory)
    const int max_chars_per_line=200000;
    ifstream fin("secure_file_path.txt");//file containing file paths
    if(!fin.good())
    {
        cout<<"File not found: [file name: 'secure_file_path.txt' not found: "<<endl;
        exit(0);
    }
    while(!fin.eof())//open file and push the lines to a vector
    {
        char buf[max_chars_per_line];
        fin.getline(buf, max_chars_per_line);
        string str(buf);

        if(str.compare("")== 0)
        {
            continue;
        }

        else
        {
            sensitive_dir_path.push_back(str);
        }
    }
    //---------------------------------------------------
    int length, i = 0;
    int fd;
    int wd[sensitive_dir_path.size()];
    char buffer[BUF_LEN];

    fd = inotify_init();

    if ( fd < 0 ) 
    {
        perror( "inotify_init" );
    }
    for(int index=0;index<sensitive_dir_path.size();index++)
    {
        string new_path=sensitive_dir_path[index];
        char* fam_sensitive_dir_path=new char[new_path.length()];
        strcpy(fam_sensitive_dir_path,new_path.c_str());

        wd[index] = inotify_add_watch( fd,fam_sensitive_dir_path, IN_CREATE);
        //cout<<"path: "<<fam_sensitive_dir_path<<"  will be monitored\n";
    }

    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->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 );
                }
            }
        }
    }
for(int index=0;index<sensitive_dir_path.size();index++)//
{
    ( void ) inotify_rm_watch( fd, wd[index] );
}
    ( void ) close( fd );

    exit( 0 );
}

要读取的文件:

  • secure_file_path.txt

“/home/Dir-1”

“/home/Dir-2”

“/home/Dir-3”。。。

或者添加多个目录的其他选项?

EN

回答 1

Stack Overflow用户

发布于 2015-10-15 17:22:08

您可以在缓冲区中接收多个事件,但您的代码只查看第一个事件。

您应该将循环改为如下所示:

代码语言:javascript
复制
while (1) {
    struct inotify_event *event, *end;
    length = read(fd, buffer, BUF_LEN);  

    if (length < 0) {
        perror( "read" );
        break;
    }

    // Our current event pointer
    event = (struct inotify_event *) &buffer[0];
    // an end pointer so that we know when to stop looping below
    end = (struct inotify_event *) &buffer[length];

    while (event < end) {
        // Do your printing or whatever here
        printf("name is %s\n", event->name);

        // Now move to the next event
        event = (struct inotify_event *)((char*)event) + sizeof(*event) + event->len;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33146592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档