What is the proper way to use inotify?

后端 未结 4 959
一生所求
一生所求 2020-11-30 02:22

I want to use the inotify mechanism on Linux. I want my application to know when a file aaa was changed. Can you please provide me with a sample ho

4条回答
  •  情话喂你
    2020-11-30 03:11

    Below is a snippet of how you can use inotify to watch "aaa". Note that I haven't tested this, I haven't even compiled it! You will need to add error checking to it.

    Instead of using a blocking read you can also use poll/select on inotfd.

    const char *filename = "aaa";
    int inotfd = inotify_init();
    
    int watch_desc = inotify_add_watch(inotfd, filename, IN_MODIFY);
    
    size_t bufsiz = sizeof(struct inotify_event) + PATH_MAX + 1;
    struct inotify_event* event = malloc(bufsiz);
    
    /* wait for an event to occur */
    read(inotfd, event, bufsiz);
    
    /* process event struct here */
    

提交回复
热议问题