inotify结合select监控目录下文件变动

#include 
#include 
#include 
#include 
#include 
#include 

#include 

#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define BUF_LEN ( 1024 * (EVENT_SIZE + 16) )

#define MAXLEN 1024

char buffer[BUF_LEN];
fd_set set;
fd_set rset;
int check_set[MAXLEN];
int max_fd;
int arrlen = 0;

static void handle_read(int fd)
{
    int length = read(fd, buffer, BUF_LEN);
    if (length < 0) {
        perror("read");
    }

    int i=0; 
    while (i < length) {
        struct inotify_event *event = (struct inotify_event *)&buffer[i];
        if (event->len) {
            if (event->mask & IN_CREATE) {
                if (event->mask & IN_ISDIR) {
                    printf("The directory %s was created.\n", event->name);
                } else {
                    printf("The file %s was created.\n", event->name);
                }
            } else if (event->mask & IN_DELETE) {
                if (event->mask & IN_ISDIR) {
                    printf("The directory %s was deleted.\n", event->name);
                } else {
                    printf("The file %s was deleted.\n", event->name);
                }
            } else if (event->mask & IN_MODIFY) {
                if (event->mask & IN_ISDIR) {
                    printf("The directory %s was modified.\n", event->name);
                } else {
                    printf("The file %s was modified. wd:%d\n", event->name, event->wd);
                }
            }
        }
        i += EVENT_SIZE + event->len;
    }
    memset(buffer, 0, BUF_LEN);
}

static void do_select() 
{
    int i=0;
    while (1) {
        set = rset;
        int nready = select(max_fd+1, &set, NULL, NULL, NULL);
        if (nready == -1) {
            perror("error select !");
            exit(-1);
        } else if (nready == 0) {
            printf("timeout!");
            continue;
        }

        for (i=0; i

 

你可能感兴趣的:(linux)