inotify file in C

前端 未结 4 1413
悲哀的现实
悲哀的现实 2020-12-20 15:38

I am trying to run an example of inotify in C..but it\'s not working. I want to monitor modifications to a file (the file is tmp.cfg), but it doesn\'t work..I don\'t know if

相关标签:
4条回答
  • 2020-12-20 16:09

    I think you're not using your user name, which is your home directory, and you're not checking the return of inotify_add_watch which probably fails:

    "/home/name/tmp.cfg"
    

    Edit: okay second problem, you shouldn't print name because

    The name field is only present when an event is returned for a file inside a watched directory;

    Edit2: third problem, the file must exist before you run the program because you add a watch on the file, I suggest you check the error from inotify_add_watch

    0 讨论(0)
  • 2020-12-20 16:09

    In watching a file, if the file is manipulated by an editor which you might do to edit it and create a change, it is likely to be doing some operations that results in the original file you asked to watch being deleted. Hence the notifications will stop if you only watch the one file.

    0 讨论(0)
  • 2020-12-20 16:19

    It doesn't work on a single file because, when we use a editor to modify file, the editor opens a copy of the file and when we save the edited version from the text editor, the existing file is deleted and a new file of the same name is created with the modifications.

    When the old file is deleted, the watch created on that file becomes invalid and it is deleted automatically.

    You can see the old file being replaced by the new file if you monitor the parent directory.

    There are two ways to solve it, monitor the parent directory and print the message when modifications is done to the particular that you want to watch.

    Else create a new watch on the file whenever modifications are made. When the old file is deleted, IN_DELETE_SELF event is triggered.

    event->name will be non-empty only when you watch a directory, as it will contain the name of the file on which the event has occurred in the watched directory.

    0 讨论(0)
  • 2020-12-20 16:21

    You have 2 issues. First, as far as I can tell, inotify does not really work on files - it needs directory name to watch.

    Second, you missed if (event->len) { inside while loop.

    This code works for me for creating, deleting and modifying files in current directory:

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/inotify.h>
    #include <unistd.h>
    
    #define EVENT_SIZE  (sizeof(struct inotify_event))
    #define BUF_LEN     (1024 * (EVENT_SIZE + 16))
    
    int main(int argc, char **argv) {
        int length, i = 0;
        int fd;
        int wd;
        char buffer[BUF_LEN];
    
        fd = inotify_init();
    
        if (fd < 0) {
            perror("inotify_init");
        }
    
        wd = inotify_add_watch(fd, ".",
            IN_MODIFY | IN_CREATE | IN_DELETE);
        length = read(fd, buffer, BUF_LEN);
    
        if (length < 0) {
            perror("read");
        }
    
        while (i < length) {
            struct inotify_event *event =
                (struct inotify_event *) &buffer[i];
            if (event->len) {
                if (event->mask & IN_CREATE) {
                    printf("The file %s was created.\n", event->name);
                } else if (event->mask & IN_DELETE) {
                    printf("The file %s was deleted.\n", event->name);
                } else if (event->mask & IN_MODIFY) {
                    printf("The file %s was modified.\n", event->name);
                }
            }
            i += EVENT_SIZE + event->len;
        }
    
        (void) inotify_rm_watch(fd, wd);
        (void) close(fd);
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题