微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何同时监控源文件夹和目标文件夹中的数据?

如何解决如何同时监控源文件夹和目标文件夹中的数据?

我正在研究文件监控系统项目,其中首先将所有数据复制到目标文件夹,然后它应该监控源文件夹和目标文件夹中的数据。但不幸的是,它只是监控文件中的数据。

我将 inotify 函数 分为两部分,因为我首先使用了 inotifyFunc1 来帮助我复制文件夹。复制数据后,我使用 inotifyFunc2 监视两个文件中的数据。但正如我所说,它只是监控第一个文件夹。

这段代码很大,但我不知道如何简短地理解它。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>

#define STRING_LEN 200
#define MAX_EVENTS 1024
#define NAME_LEN 16
#define EVENT_SIZE (sizeof(struct inotify_event))
#define BUFFER_LEN (MAX_EVENTS * (EVENT_SIZE + NAME_LEN))

typedef struct{
    int fd,wd,result,length;
    uint32_t mask[2];
    char path1[STRING_LEN],path2[STRING_LEN],cmd[STRING_LEN],option,buffer[BUFFER_LEN];
} monitoring;

monitoring monitor;

void sig_handler(int signal){
    printf("\nThe program is closed\n");
    inotify_rm_watch(monitor.fd,monitor.wd);
    close(monitor.fd);
    exit(0);
}

void inotifyFunc1(char *path1,uint32_t *maskPtr1){
    monitor.fd = inotify_init();
    if(fcntl(monitor.fd,F_SETFL,O_NONBLOCK)){
       perror("inotify not initialized: ");
       exit(0);
    }

    monitor.wd = inotify_add_watch(monitor.fd,path1,*maskPtr1);
    if(monitor.wd < 0){
        perror("Sorry");
        exit(1);
    }
}

void inotifyFunc2(char *path2,uint32_t *maskPtr2)
{
    while(1)
    {
        int i = 0;
        monitor.length = read(monitor.fd,monitor.buffer,BUFFER_LEN);
        while(i<monitor.length){
            struct inotify_event *event = (struct inotify_event *)&monitor.buffer[i];
            if(event->len){
                if(event->mask & *maskPtr2){
                    if(event->mask & IN_ISDIR){
                        printf("Directory is created\n");
                        break;
                    }
                    else{
                        printf("File is created\n");
                        break;
                    }
                }
            }
        }
    }
}

void monitoringSystem(char *pathname1,char *pathname2)
{
    /* Closing inotify */
    signal(SIGINT,sig_handler);
    do
    {
        printf("Choose the source path: ");
        scanf("%s",pathname1);

        monitor.mask[0] = ENOENT;
        inotifyFunc1(pathname1,&monitor.mask[0]);

        printf("Choose the destination path: ");
        scanf("%s",pathname2);
        inotifyFunc1(pathname2,&monitor.mask[0]);
        
        monitor.result = strcmp(pathname1,pathname2);
        if(monitor.result == 0){
            printf("Error: Both locations are the same\n");
            exit(3);
        }
        else{
            sprintf(monitor.cmd,"cp -r %s %s",pathname1,pathname2);
            system(monitor.cmd);
            printf("Data is copied from source to destination\n");
        }

        printf("\nBoth locations are being monitored\n");
        monitor.mask[1] = IN_CREATE;
        inotifyFunc1(pathname1,&monitor.mask[1]);
        inotifyFunc2(pathname1,&monitor.mask[1]);

        inotifyFunc1(pathname2,&monitor.mask[1]);
        inotifyFunc2(pathname2,&monitor.mask[1]);
    
        printf("Do you want to give location again? [y/n]: ");
        scanf("%s",&monitor.option);
    } while(monitor.option == 'y');
}

int main(int argc,char *argv[])
{
    printf("DATA RECOVERY SYstem\n");
    printf("WELCOME TO THE MAIN MENU\n\n");

    monitoringSystem(monitor.path1,monitor.path2);
    
    return 0;
}

解决方法

<span><span id="likes">0</span> Likes</span><br>
<button id="likeButton" class="likeClass likeClassQ">Like</button>

您的 void inotifyFunc1(char *path1,uint32_t *maskPtr1){ monitor.fd = inotify_init(); 函数会创建一个全新的 inotifyFunc1 实例并将其句柄存储在 inotify 中。

monitor.fd

但是你调用了两次。第二次,它再次创建一个新的 inotifyFunc1(pathname1,&monitor.mask[0]); printf("Choose the destination path: "); scanf("%s",pathname2); inotifyFunc1(pathname2,&monitor.mask[0]); 实例并将其句柄存储在 inotify 中,从而泄漏了旧句柄。

如果您只想让一个 monitor.fd 实例观看多于一件事,则只需创建一个 inotify 实例。

inotify

这也没有道理。你为什么又打电话给 inotifyFunc1(pathname1,&monitor.mask[1]); inotifyFunc2(pathname1,&monitor.mask[1]); ?如果您认为它已经在监控这两个位置,您认为这会实现什么?如果你不认为它已经在监控这两个位置,为什么在你说它是之后​​呢?

很难解释是什么不正确的推理导致您调用这些函数,因为代码中没有注释。但这毫无意义。

你想:

  1. 仅创建一个 inotifyFunc1 实例。
  2. 每个位置只添加一次。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?