zoukankan      html  css  js  c++  java
  • inotify监听文件

    inotify监听文件并通知

    static int inotify_dbfile(const char *spFromRule, const char *spDevFile)
    {
        int inotifyFd;
        int watchfd1;
        int watchfd2;
        char buf[BUF_LEN];
        size_t numRead;
        char *spfile;
        struct inotify_event *event;
        int ret = 0;
    
        /* 初始化inotify实例 */
        if (-1 == (inotifyFd = inotify_init ()))
        {
            LOG_ERROR("Error: Inotify Initialization is failed..");
            exit(1);
        }
        
        /* 循环所有输入目录,设置监视 */
        if (-1 == (watchfd1 = inotify_add_watch (inotifyFd, spFromRule, event_mask)))
        {
            LOG_ERROR("Error: Inotify Watch [%s] is failed..", spFromRule);
            exit(2);
        }
        
        if (-1 == (watchfd2 = inotify_add_watch (inotifyFd, spDevFile, event_mask)))
        {
            LOG_ERROR("Error: Inotify Watch [%s] is failed..", spDevFile);
            exit(2);
        }
        
        while (1)
        {  
            /* Read events forever */
            if (1 > (numRead = read(inotifyFd, buf, BUF_LEN)))
            {
                LOG_ERROR("read() from inotify fd returned 0!");
                puts("read() from inotify fd returned 0!");
                exit(4);
            }
    
            /* 处理read()返回的缓冲区中的所有事件 */
            for (spfile = buf; spfile < buf + numRead; ) 
            {
                event = (struct inotify_event *) spfile;
                if ((event->mask & IN_MODIFY))
                {
                    LOG_INFO("INFO: Inotify Watch file is modified...");
                    ret = 1;
                    goto end;
                }
    
                spfile += sizeof(struct inotify_event) + event->len;
            }
        }
    
    end:
        inotify_rm_watch(inotifyFd, watchfd1);
        inotify_rm_watch(inotifyFd, watchfd2);
        close(inotifyFd);
        
        return ret;
    }
  • 相关阅读:
    DFS 之 全排列
    蓝桥杯: 标题:第几个幸运数
    第K个幸运数字(4、7)
    C++将十进制数转化为二进制
    C++中数组声名后不初始化,数组里的值都是0吗?
    html和jsp区别
    中缀表达式转换为后缀表达式
    多个Activity之间共享数据的方式
    Jupyter Notebook入门教程
    Android之Activity生命周期详解
  • 原文地址:https://www.cnblogs.com/porkerface/p/12031597.html
Copyright © 2011-2022 走看看