• 使用inotify检测linux目录内文件变化


    #include <unistd.h>
    #include <sys/inotify.h>
    #include <stdio.h>
    #include <error.h>
    #include <errno.h>
    #include <string.h>
    
    #define ERROR(text) error(1, errno, "%s", text)
    
    typedef struct test{
            int flag;
            const char *name;
    }EventMask;
    
    
    int freadsome(void *dest, size_t remain, FILE *file)
    {
            char *offset = (char*)dest;
            while (remain) {
                    int n = fread(offset, 1, remain, file);
                    if (n==0) {
                            return -1;
                    }
                    remain -= n;
                    offset += n;
            }
            return 0;
    }
    
    int main(int argc, char *argv[])
    {
            const char *target;
            if (argc == 1) {
                    target = ".";
            }
            else {
                    target = argv[1];
            }
    
            EventMask event_masks[] = {
               {IN_ACCESS        , "IN_ACCESS"}        ,
               {IN_ATTRIB        , "IN_ATTRIB"}        ,
               {IN_CLOSE_WRITE   , "IN_CLOSE_WRITE"}   ,
               {IN_CLOSE_NOWRITE , "IN_CLOSE_NOWRITE"} ,
               {IN_CREATE        , "IN_CREATE"}        ,
               {IN_DELETE        , "IN_DELETE"}        ,
               {IN_DELETE_SELF   , "IN_DELETE_SELF"}   ,
               {IN_MODIFY        , "IN_MODIFY"}        ,
               {IN_MOVE_SELF     , "IN_MOVE_SELF"}     ,
               {IN_MOVED_FROM    , "IN_MOVED_FROM"}    ,
               {IN_MOVED_TO      , "IN_MOVED_TO"}      ,
               {IN_OPEN          , "IN_OPEN"}          ,
    
               {IN_DONT_FOLLOW   , "IN_DONT_FOLLOW"}   ,
               {IN_EXCL_UNLINK   , "IN_EXCL_UNLINK"}   ,
               {IN_MASK_ADD      , "IN_MASK_ADD"}      ,
               {IN_ONESHOT       , "IN_ONESHOT"}       ,
               {IN_ONLYDIR       , "IN_ONLYDIR"}       ,
    
               {IN_IGNORED       , "IN_IGNORED"}       ,
               {IN_ISDIR         , "IN_ISDIR"}         ,
               {IN_Q_OVERFLOW    , "IN_Q_OVERFLOW"}    ,
               {IN_UNMOUNT       , "IN_UNMOUNT"}       ,
            };
    
            int monitor = inotify_init();
            if ( -1 == monitor ) {
                    ERROR("monitor");
            }
    
            int watcher = inotify_add_watch(monitor, target, IN_ALL_EVENTS);
            if ( -1 == watcher  ) {
                    ERROR("inotify_add_watch");
            }
    
            FILE *monitor_file = fdopen(monitor, "r");
            char last_name[1024];
            char name[1024];
            /* event:inotify_event -> name:char[event.len] */
    
            while (1) {
    
                    struct inotify_event event;
                    if ( -1 == freadsome(&event, sizeof(event), monitor_file) ) {
                            ERROR("freadsome");
                    }
                    if (event.len) {
                            freadsome(name, event.len, monitor_file);
                    } else {
                            sprintf(name, "FD: %d
    ", event.wd);
                    }
    
                    if (strcmp(name, last_name) != 0) {
                            puts(name);
                            strcpy(last_name, name);
                    }
    
                    /* 显示event的mask的含义 */
                    int i = 0;
                    for (i=0; i<sizeof(event_masks)/sizeof(EventMask); ++i)
                    {
                            if (event.mask & event_masks[i].flag) {
                                    printf("	%s
    ", event_masks[i].name);
                            }
                    }
            }
            return 0;
    }
  • 相关阅读:
    JavaScript 随机产生十个整数,放入数组中,对这个数组进行降序排序,并获取到这个数组的最大值和最小值
    JavaScript输出换行
    JavaScript超时调用、间歇调用
    JavaScript内置对象
    JavaScript事件列表
    JavaScript 中 for-in和 for-of 的区别
    JavaScript break指定标签打破多层循环示例
    初识JavaScript
    HTML + CSS CSS设置背景图片后图片没有铺满屏幕等
    设计模式之工厂模式
  • 原文地址:https://www.cnblogs.com/wangzhigang/p/5332128.html
Copyright © 2020-2023  润新知