I'm working on the file monitoring system project in which, first all the data will be copied to the destination folder then it should monitor the data in both source and destination folders. But unfortunately, it is just monitoring the data from the source folder.
I divided the inotify function into two parts because I used inotifyFunc1
to help me in copying the folders first. And once the data is copied then I used inotifyFunc2
to monitor the data in both folders. But as I said it is just monitoring the first source folder.
This code is big but I don't know how to make it understand in a short way.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#define STRING_LEN 200
#define MAX_EVENTS 1024
#define NAME_LEN 16
#define EVENT_SIZE (sizeof(struct inotify_event))
#define BUFFER_LEN (MAX_EVENTS * (EVENT_SIZE + NAME_LEN))
typedef struct{
int fd, wd, result, length;
uint32_t mask[2];
char path1[STRING_LEN], path2[STRING_LEN], cmd[STRING_LEN], option, buffer[BUFFER_LEN];
} monitoring;
monitoring monitor;
void sig_handler(int signal){
printf("\nThe program is closed\n");
inotify_rm_watch(monitor.fd, monitor.wd);
close(monitor.fd);
exit(0);
}
void inotifyFunc1(char *path1, uint32_t *maskPtr1){
monitor.fd = inotify_init();
if(fcntl(monitor.fd, F_SETFL, O_NONBLOCK)){
perror("inotify not initialized: ");
exit(0);
}
monitor.wd = inotify_add_watch(monitor.fd, path1, *maskPtr1);
if(monitor.wd < 0){
perror("Sorry");
exit(1);
}
}
void inotifyFunc2(char *path2, uint32_t *maskPtr2)
{
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 & *maskPtr2){
if(event->mask & IN_ISDIR){
printf("Directory is created\n");
break;
}
else{
printf("File is created\n");
break;
}
}
}
}
}
}
void monitoringSystem(char *pathname1, char *pathname2)
{
/* Closing inotify */
signal(SIGINT,sig_handler);
do
{
printf("Choose the source path: ");
scanf("%s", pathname1);
monitor.mask[0] = ENOENT;
inotifyFunc1(pathname1, &monitor.mask[0]);
printf("Choose the destination path: ");
scanf("%s", pathname2);
inotifyFunc1(pathname2, &monitor.mask[0]);
monitor.result = strcmp(pathname1, pathname2);
if(monitor.result == 0){
printf("Error: Both locations are the same\n");
exit(3);
}
else{
sprintf(monitor.cmd, "cp -r %s %s", pathname1, pathname2);
system(monitor.cmd);
printf("Data is copied from source to destination\n");
}
printf("\nBoth locations are being monitored\n");
monitor.mask[1] = IN_CREATE;
inotifyFunc1(pathname1, &monitor.mask[1]);
inotifyFunc2(pathname1, &monitor.mask[1]);
inotifyFunc1(pathname2, &monitor.mask[1]);
inotifyFunc2(pathname2, &monitor.mask[1]);
printf("Do you want to give location again? [y/n]: ");
scanf("%s", &monitor.option);
} while(monitor.option == 'y');
}
int main(int argc, char *argv[])
{
printf("DATA RECOVERY SYSTEM\n");
printf("WELCOME TO THE MAIN MENU\n\n");
monitoringSystem(monitor.path1, monitor.path2);
return 0;
}
void inotifyFunc1(char *path1, uint32_t *maskPtr1){
monitor.fd = inotify_init();
Your inotifyFunc1
function creates a brand new inotify
instance and stores its handle in monitor.fd
.
inotifyFunc1(pathname1, &monitor.mask[0]);
printf("Choose the destination path: ");
scanf("%s", pathname2);
inotifyFunc1(pathname2, &monitor.mask[0]);
But you call it twice. The second time, it again creates a new inotify
instance and stores its handle in monitor.fd
, leaking the old handle.
If you only want one inotify
instance to watch more than one thing, you need to create only one inotify
instance.
inotifyFunc1(pathname1, &monitor.mask[1]);
inotifyFunc2(pathname1, &monitor.mask[1]);
This also doesn't make sense. Why are you calling inotifyFunc1
again? If you thought it was already monitoring both locations, what do you think this accomplishes? If you didn't think it was already monitoring both locations, why is this after you say it is?
It's hard to explain what incorrect reasoning led you to call those functions because there are no comments in the code. But it makes no sense.
You want to:
inotify
instance.