Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
    void inotifyFunc(char *path, uint32_t *maskPtr, int wd[2]){
        monitor.fd = inotify_init();
        if(fcntl(monitor.fd, F_SETFL, O_NONBLOCK)){
           perror("inotify not initialized: ");
           exit(0);
        }
    
        wd[0] = inotify_add_watch(monitor.fd, path, *maskPtr);
        wd[1] = inotify_add_watch(monitor.fd, path, *maskPtr);
        if(wd[0] < 0 || wd[1] < 0){
            perror("Sorry");
            exit(1);
        }
    
        while(1){
            int i = 0;
            monitor.length = read(monitor.fd, monitor.buffer, BUFFER_LEN);
            while(i<monitor.length){
                struct inotify_event *event = (struct inotify_event *)&monitor.buffer[i];
                if(event->len){
                    if(event->mask & *maskPtr){
                        if(event->mask & IN_ISDIR){
                            printf("Directory is created\n");
                            break;
                        }
                        else{
                           printf("File is created\n");
                           break;
                        }
                    }
                }
            }
        }
    }

**Another function**    

    void monitoringSystem(char *pathname1, char *pathname2){
        monitor.mask[0] = ENOENT;
        monitor.mask[1] = IN_CREATE;
    
        printf("Write the source path: ");
        scanf("%s", pathname1);
        inotifyFunc(pathname1, &monitor.mask[0], &monitor.wd[0]);
    
        printf("Write the destination path: ");
        fgets(pathname1, sizeof(pathname1), stdin);
        inotifyFunc(pathname2, &monitor.mask[0], &monitor.wd[1]);

        printf("\nBoth locations are being monitored\n");
        inotifyFunc(pathname1, &monitor.mask[1], &monitor.wd[0]);
        inotifyFunc(pathname2, &monitor.mask[1], &monitor.wd[1]);
    }


What I have tried:

In these two functions, I am trying to monitor two paths at the same time but the problem is that, whenever I run the program and give it the source path location, then it does not show me the next option that is to enter the destination path. Instead, I press the enter button and write something in the terminal but it does not give me any message. What is the problem here?
Posted
Updated 3-Jan-21 6:30am

1 solution

Here is your function rewritten so the looping is a little clearer (at least to me it is) :
while(1)
{
    int i = 0;
    monitor.length = read(monitor.fd, monitor.buffer, BUFFER_LEN);
    while(i<monitor.length)
    {
        struct inotify_event *event = (struct inotify_event *)&monitor.buffer[i];
        if(event->len)
        {
            if(event->mask & *maskPtr)
            {
                if(event->mask & IN_ISDIR)
                {
                    printf("Directory is created\n");
                    break;
                }
                else
                {
                   printf("File is created\n");
                   break;
                }
            }
        }
    }
}
The break will cause execution to jump out of the inner loop but not out of the outer while loop. If you run this code in a debugger you will find the code calling read again after the printf is called.
 
Share this answer
 
Comments
ibilalkayy 4-Jan-21 0:44am    
You just adjusted the code. But the result is the same as before

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900