如何成功使 IN_DELETE 事件起作用?

问题描述

在这代码中,我试图使 IN_DELETE 事件起作用。虽然 IN_CREATE 和 IN_DELETE 事件与 IN_DELETE 相同,但是当我删除文件或目录时,终端什么也不显示。这段代码有什么问题吗?请帮助改进此代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/inotify.h>
#include <errno.h>
#include <limits.h>
    
typedef struct{
    int fd,watchFd;
    char buffer[100];
} monitoring;
    
int main(int argc,char *argv[])
{
    monitoring monitor;
    monitor.fd = inotify_init();
    monitor.buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
    struct inotify_event *evp;
    
    if(monitor.fd<0){
        perror("inotify_init not initialized: ");
        return -1;
    }
    
    monitor.watchFd = inotify_add_watch(monitor.fd,"/Projects/Project1",IN_CREATE|IN_MODIFY|IN_DELETE);

    while(1){
        read(monitor.fd,monitor.buffer,sizeof(monitor.buffer));
        evp = (struct inotify_event*)monitor.buffer;
        if(evp->len > 0){
            if(evp->mask & IN_CREATE){
                if(evp->mask & IN_ISDIR){
                    printf("Directory created: * %s\n",evp->name);
                }
                else{
                    printf("File created: * %s\n",evp->name);  
                }
            }
            if(evp->mask & IN_MODIFY){
                if(evp->mask & IN_ISDIR){
                    printf("Directory modified: ");
                }
                else{
                    printf("File modified: ");  
                }
            }
            if(evp->mask & IN_DELETE){
                if(evp->mask & IN_ISDIR){
                    printf("Directory deleted: * %s\n",evp->name);
                }
                else{
                    printf("File deleted: * %s\n",evp->name);  
                }
            }
        }
    }
    inotify_rm_watch(monitor.fd,monitor.watchFd);
    close(monitor.fd);
    
    return (EXIT_SUCCESS);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...