/*----------------------------------------------------------------------*/ void DoFileCommand(int x) { char szMessage[BUF_LEN]; system(watch_list[x].szCommand); sprintf(szMessage,"executed: %s",watch_list[x].szCommand); locallog(szMessage); }
/*----------------------------------------------------------------------*/ int main(int argc,char *argv[]) { int fd,wd,x; ssize_t len,i; struct inotify_event equeue[QUEUE_LEN]; struct watch_list_t* pwatch_list = watch_list; char szCommand[BUF_LEN]; char szMessage[BUF_LEN]; char* szFile=NULL; char szBuffer[BUF_LEN]; int num_watch_files=0; int nDiff=0; char szMD5new[BUF_LEN]; int sleep_time=60; if(argc>1) { szFile=argv[1]; } else { printf("Use: %s [ini filename]\n",argv[0]); exit(-1); } #ifdef DAEMONIZE daemon(); #endif /* get the number of files to watch */ x=ReadIniArg(szFile, FILEWATCH,"num_files",szBuffer,BUF_LEN); if(x) { num_watch_files=atoi(szBuffer); } /* get the sleep time */ x=ReadIniArg(szFile, FILEWATCH,"sleep_time",szBuffer,BUF_LEN); if(x) { sleep_time=atoi(szBuffer); } sprintf(szMessage,"watching %d files every %d seconds", num_watch_files,sleep_time); locallog(szMessage); /* set up the basic paramters */ ReadFileData(szFile,num_watch_files); memset(equeue,0,sizeof(struct inotify_event)*QUEUE_LEN); fd=inotify_init(); if(fd==-1) { perror("inotify_init"); exit(EXIT_FAILURE); } /* do the work */ for(x=0;x<num_watch_files;x++) { watch_list[x].wd=inotify_add_watch(fd, watch_list[x].szFile, IN_CLOSE_WRITE); sprintf(szMessage,"watching %s",watch_list[x].szFile); locallog(szMessage); } #ifdef EVENT_QUEUE_METHOD while(1) { len = read (fd,equeue,QUEUE_LEN); i=0; while(i<len) { struct inotify_event *event = (struct inotify_event*) &equeue[i]; if(event->mask && IN_CLOSE_WRITE) { print_event(event); for(x=0;x<MAX_WATCH_FILES;x++) { if(event->wd == watch_list[x].wd) { sprintf(szMessage,"file %s closed",watch_list[x].szFile); locallog(szMessage); /* check the MD5 to see if the file changed */ MD5File(watch_list[x].szFile,szMD5new); nDiff=strcmp(watch_list[x].szMD5,szMD5new); if(nDiff) { DoFileCommand(x); strcpy(watch_list[x].szMD5,szMD5new); } } } } i++; } sleep(2); } inotify_rm_watch(fd); #endif #ifdef POLLING_METHOD while(1) { sleep(sleep_time); for(x=0;x<num_watch_files;x++) { /* check the MD5 to see if the file changed */ MD5File(watch_list[x].szFile,szMD5new); nDiff=strcmp(watch_list[x].szMD5,szMD5new); if(nDiff) { DoFileCommand(x); strcpy(watch_list[x].szMD5,szMD5new); sprintf(szMessage,"file %s new MD5: %s", watch_list[x].szFile, watch_list[x].szMD5); locallog(szMessage); } } } #endif }
inline virtual void log() { locallog(); }
inline LocSsrMsg(LocApiBase* locApi) : LocMsg(), mLocApi(locApi) { locallog(); }
inline LocOpenMsg(LocApiBase* locApi, LOC_API_ADAPTER_EVENT_MASK_T mask) : LocMsg(), mLocApi(locApi), mMask(mask) { locallog(); }