2015-10-15 532 views
0

正在使用inotifyINOTIFY_ADD_WATCH()來監視多個目錄上的活動。是否可以使用循環添加多個文件路徑(目錄)。我有以下示例代碼,但沒有收到任何通知。inotify,inotify_add_watch()監視多個目錄,C++

示例代碼:

#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 /目錄 - 一個」

「的/ home /風向-two「

「/ home/Dir-three」 。 。 。

或任何其他選項添加多目錄?

+0

你錯過了'刪除[]'。 – MSalters

回答

0

您可以在緩衝區中接收多個事件,但您的代碼僅查看第一個事件。

你應該改變你的循環更是這樣的:

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

hello @ wez-furlong,謝謝你的回答,這很有幫助。但仍然在線上移動到下一個事件,即(事件=((char *)事件)+ sizeof(*事件)+事件 - > len;)它遇到問題/錯誤它說:錯誤:不能轉換'char *'改爲'inotify_event *'。 –