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

    参考:xxxx

    /*************************************************************************
    * Copyright (C) Michael Kerrisk, 2018. *
    * *
    * This program is free software. You may use, modify, and redistribute it *
    * under the terms of the GNU General Public License as published by the *
    * Free Software Foundation, either version 3 or (at your option) any *
    * later version. This program is distributed without any warranty. See *
    * the file COPYING.gpl-v3 for details. *
    *************************************************************************/

    /* Listing 19-1 */

    /* demo_inotify.c

    Demonstrate the use of the inotify API.

    Usage: demo_inotify pathname...

    The program monitors each of the files specified on the command line for all
    possible file events.

    This program is Linux-specific. The inotify API is available in Linux 2.6.13
    and later.
    */
    //#include <sys/inotify.h>
    #include <limits.h>
    #include <linux/inotify.h>
    //#include "tlpi_hdr.h"

    static void /* Display information from inotify_event structure */
    displayInotifyEvent(struct inotify_event *i)
    {
        printf(" wd =%2d; ", i->wd);
        if (i->cookie > 0)
            printf("cookie =%4d; ", i->cookie);

        printf("mask = %0x ", i->mask);
        #if 1
        if (i->mask & IN_ACCESS) printf("IN_ACCESS ");
        if (i->mask & IN_ATTRIB) printf("IN_ATTRIB ");
        if (i->mask & IN_CLOSE_NOWRITE) printf("IN_CLOSE_NOWRITE ");
        if (i->mask & IN_CLOSE_WRITE) printf("IN_CLOSE_WRITE ");
        if (i->mask & IN_CREATE) printf("IN_CREATE ");
        if (i->mask & IN_DELETE) printf("IN_DELETE ");
        if (i->mask & IN_DELETE_SELF) printf("IN_DELETE_SELF ");
        if (i->mask & IN_IGNORED) printf("IN_IGNORED ");
        if (i->mask & IN_ISDIR) printf("IN_ISDIR ");
        if (i->mask & IN_MODIFY) printf("IN_MODIFY ");
        if (i->mask & IN_MOVE_SELF) printf("IN_MOVE_SELF ");
        if (i->mask & IN_MOVED_FROM) printf("IN_MOVED_FROM ");
        if (i->mask & IN_MOVED_TO) printf("IN_MOVED_TO ");
        if (i->mask & IN_OPEN) printf("IN_OPEN ");
        if (i->mask & IN_Q_OVERFLOW) printf("IN_Q_OVERFLOW ");
        if (i->mask & IN_UNMOUNT) printf("IN_UNMOUNT ");
     #endif
        printf(" ");

        if (i->len > 0)
        printf(" name = %s ", i->name);
    }

    #define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))

    int
    main(int argc, char *argv[])
    {
        int inotifyFd, wd, j;
        char buf[BUF_LEN] __attribute__ ((aligned(8)));
        unsigned int numRead;
        char *p;
        struct inotify_event *event;

        if (argc < 2 || strcmp(argv[1], "--help") == 0)
            printf("%s pathname... ", argv[0]);

        inotifyFd = inotify_init(); /* Create inotify instance */
        if (inotifyFd == -1)
            perror("inotify_init");

        /* For each command-line argument, add a watch for all events */

        for (j = 1; j < argc; j++) {
            wd = inotify_add_watch(inotifyFd, argv[j], IN_ALL_EVENTS);
            if (wd == -1)
                perror("inotify_add_watch");

            printf("Watching %s using wd %d ", argv[j], wd);
        }

        for (;;) { /* Read events forever */
            numRead = read(inotifyFd, buf, BUF_LEN);
            if (numRead == 0)
                perror("read() from inotify fd returned 0!");

            if (numRead == -1)
                perror("read");

            printf("Read %ld bytes from inotify fd ", (long) numRead);

            /* Process all of the events in buffer returned by read() */

            for (p = buf; p < buf + numRead; ) {
                event = (struct inotify_event *) p;
                displayInotifyEvent(event);

                p += sizeof(struct inotify_event) + event->len;
            }
        }
       return 0;
    }

  • 相关阅读:
    Leetcode 233 Number of Digit One
    获取各种常见形状的位图
    关于编程
    LintCode-第k大元素
    基于IBM Bluemix的数据缓存应用实例
    LeakCanary:简单粗暴的内存泄漏检測工具
    MFC,C++,VC++,VS2010 之间究竟是什么关系
    我对高考考场制度(比方是否同意迟到、忘带考证、上厕所)优化的点滴思考,不一定非常有道理
    ural 1989(树状数组+多项式hash)
    TI C66x DSP 系统events及其应用
  • 原文地址:https://www.cnblogs.com/newjiang/p/11065326.html
Copyright © 2011-2022 走看看