linux inotify目录,然后当目录被删除时它反应奇怪

时间:2016-11-24 13:18:30

标签: c linux inotify delete-directory

我的操作系统是Ubuntu。我用C编程为我的系统编程设计做作业。我使用“inotify()”来输入目录及其所有子目录,不包括其他常规文件和符号链接,但是当我删除目录时,它会对性能做出奇怪的反应。但是,它显示一个事件 - >名称为空或出现错误,如非ASCII字符,然后父目录显示该目录已被删除。

我如何解决此案件的问题?我只想要后者才能发生,但前者不可能发生。

 static int begin_hear(csiebox_client *client, hash *hash_ptr, int fd, hash *hash_hdlnk){
    int length, i = 0;
    //int fd;
    int wd;
    char buffer[EVENT_BUF_LEN];
    memset(buffer, 0, EVENT_BUF_LEN);
    //create a instance and returns a file descriptor
    //fd = inotify_init();
    if (fd < 0) {
        perror("inotify_init");
    }
    //add directory "." to watch list with specified events
    //wd = inotify_add_watch(fd, "../cdirs", IN_CREATE | IN_DELETE | IN_ATTRIB | IN_MODIFY);
    while ((length = read(fd, buffer, EVENT_BUF_LEN)) > 0) {
        i = 0;
        while (i < length) {
            struct inotify_event* event = (struct inotify_event*)&buffer[i];
            fprintf(stderr, "event: (%d, %zd, %s)\ntype: ", event->wd, strlen(event->name), event->name);
            char *wd_path;
            char *pathname;
            pathname = (char*)malloc(PATH_MAX);
            memset(pathname, 0, sizeof(pathname));
            if(get_from_hash(hash_ptr, (void**)&wd_path, event->wd) == 0){
                fprintf(stderr, "not in hash\n");
            }
            fprintf(stderr, "wd_path is %s\n", wd_path);
            strcpy(pathname, wd_path);
            //whether it's a dir
            if(event->mask & IN_ISDIR){
                if(event->mask & IN_CREATE){
                    strcat(pathname, "/");
                    strcat(pathname, event->name);
                    int wd;
                    wd = inotify_add_watch(fd, pathname, IN_CREATE | IN_DELETE | IN_ATTRIB | IN_MODIFY);
                    fprintf(stderr, "wd = %d\n", wd);
                    struct stat sb;
                    if(lstat(pathname, &sb) == -1){
                        fprintf(stderr, "lstat fail\n");
                    }
                    //want to sync the create-directory request to server
                }
                else if(event->mask & IN_DELETE){
                    strcat(pathname, "/");
                    strcat(pathname, event->name);
                    //want to sync the delete-directory request to server
                }
            }
            else{  //symbolic link, regular file, or other hardlink
                strcat(pathname, "/");
                strcat(pathname, event->name);
                int hardlinkkey;
                if(event->mask & IN_CREATE){
                    struct stat sb;
                    if(lstat(pathname, &sb) == -1){
                        fprintf(stderr, "pathname is %s\n", pathname);
                        fprintf(stderr, "lstat fail\n");
                    }
                    if(S_ISREG(sb.st_mode)){
                        //want to sync the reg file to server
                    }
                    if(S_ISLNK(sb.st_mode)){
                        //want to sync the symbolic link to server
                    }
                }
                else if(event->mask & IN_ATTRIB || event->mask & IN_MODIFY){
                    if(lstat(pathname, &sb) == -1){
                        fprintf(stderr, "lstat fail\n");
                    }
                    if(S_ISREG(sb.st_mode)){
                        //want to sync the reg file to server
                    }
                    if(S_ISLNK(sb.st_mode)){
                        //want to sync the symbolic link to server
                    }
                }
                else if(event->mask & IN_DELETE){
                    //want to sync the remove request to server
                }
                }
            i += EVENT_SIZE + event->len;
        }
        memset(buffer, 0, EVENT_BUF_LEN);
  //inotify_rm_watch(fd, wd);
    }
    close(fd);
    return 0;
}

0 个答案:

没有答案