gpt4 book ai didi

c++ - inotify递归怎么做呢?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:25 24 4
gpt4 key购买 nike

我需要在具有多个子文件夹的文件夹上打印事件。如何递归地做到这一点?请打印 C++ 代码。我卡住了!!每次弹出 evet 时,我都需要打开子文件夹,获取文件并将其复制到另一个目录中。我不想每 2 秒列出所有子文件夹并查找文件(如果有)。效率不高。我需要使用一个监视器文件夹。请帮忙

我要监控的导演有多个子文件夹。每个子文件夹都有另一个子文件夹,可以在某个时刻包含一个文件。主文件夹->子文件夹->每个子文件夹->子文件夹->文件。

这是我暂时拥有的代码:

/*


*/
#include <pthread.h>
#include <unistd.h>

#include <iostream>
#include <sys/inotify.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
using namespace std;
vector<string> SS;



void *print_message_function( void *ptr );


int main(int argc, char **argv ){

pthread_t t1;
int fd,fd1,wd,wd1,i=0,i1=0,len=0,len1=0;
int length;
char pathname[100],buf[1024],buf1[1024];
int data;
struct inotify_event *event;
char *message1 = "Thread 1";



FILE *fr;
// fd=inotify_init1(IN_NONBLOCK);//--rewrite
fd = inotify_init();


/* watch /test directory for any activity and report it back to me */
wd=inotify_add_watch(fd,"/home/MainFoder/",IN_ALL_EVENTS);

// int flag=0;
// char*ev="";
//wd=inotifytools_watch_recursively_with_exclude("/home/MainFolder/",IN_ALL_EVENTS);
while(1)
{
//sleep(30);
//read 1024 bytes of events from fd into buf

i=0;
len=read(fd,buf,1024);
while(i<len){

event=(struct inotify_event *) &buf[i];


/* watch /test directory for any activity and report it back to me */


/* check for changes */
{
if((event->mask & IN_OPEN) ||(event->mask & IN_CREATE))

{


printf("\n %s :was opened\n",event->name);
SS.push_back(event->name);



}

}
/* update index to start of next event */
i+=sizeof(struct inotify_event)+event->len;
}

vector<string>::const_iterator cii;
for(cii=SS.begin(); cii!=SS.end(); cii++)
{


wd1 = watch_from_filename(*ci);
}
/*
vector<string>::const_iterator cii;
for(cii=SS.begin(); cii!=SS.end(); cii++)
{
cout <<"HERE:"<< *cii << endl;
}
*/
int iret1, iret2;

/* Create independent threads each of which will execute function */

iret1 = pthread_create( &t1, NULL, print_message_function, (void*) message1);

}

}
void *print_message_function( void *ptr )
{
vector<string>::const_iterator cii;
for(cii=SS.begin(); cii!=SS.end(); cii++)
{

cout <<"HERE:"<< *cii << endl;
std::string path=exec

}
}

最佳答案

Github 上的这个工作示例可以满足您的需求:inotify-example.cpp

在 CREATE 事件中,当前的 wd(watch 描述符)加上 inotify_event wd 和名称组件,被添加到一个 Watch 对象中(参见示例)。该类包括以多种方式查找 wd 和名称的方法。

此片段显示了如何处理 CREATE/DELETE 事件:

            if ( event->mask & IN_CREATE ) {
current_dir = watch.get(event->wd);
if ( event->mask & IN_ISDIR ) {
new_dir = current_dir + "/" + event->name;
wd = inotify_add_watch( fd, new_dir.c_str(), WATCH_FLAGS );
watch.insert( event->wd, event->name, wd );
total_dir_events++;
printf( "New directory %s created.\n", new_dir.c_str() );
} else {
total_file_events++;
printf( "New file %s/%s created.\n", current_dir.c_str(), event->name );
}
} else if ( event->mask & IN_DELETE ) {
if ( event->mask & IN_ISDIR ) {
new_dir = watch.erase( event->wd, event->name, &wd );
inotify_rm_watch( fd, wd );
total_dir_events--;
printf( "Directory %s deleted.\n", new_dir.c_str() );
} else {
current_dir = watch.get(event->wd);
total_file_events--;
printf( "File %s/%s deleted.\n", current_dir.c_str(), event->name );
}
}

关于c++ - inotify递归怎么做呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9147757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com