?? readdir.c
字號:
#include <stdio.h>#include <dirent.h>#include <string.h>#include <errno.h>#include <ctype.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/types.h>#include <stdlib.h>#include <unistd.h>#include <ftw.h>#include <time.h>#include <signal.h>#include <mysql.h>#define IPSET_PATH "/etc/IPset"#define S_NAME "xtkj"#define ROOT_PATH "/home/nfs/video/"#define DB_HOSTNAME "10.191.0.7"#define DB_USERNAME "root"#define DB_PASSWORD "root1234"#define DB_DATABASE "xtdb"#define SQL_SIZE 1024char year_day_buf[10];int count;char passwd_buf[50];char username_buf[50];struct IPbuf { char s_name[20]; char user_name[20]; char pass_wd[20]; char ipset_buf[50];} ip_buf[10];struct nount_payh { char mount_point[50];}m_point[] = {{"/mnt/usb0"},{"/mnt/usb1"},{"/mnt/usb2"},{"/mnt/usb3"},{"/mnt/usb4"},{"/mnt/usb5"},{"/mnt/usb6"},{"/mnt/usb7"},{"/mnt/usb8"}};int getwords(char *line,char *word[],int maxword){ char *p = line; int nwords = 0; while(1) { while(isspace(*p)) p++; if (*p == '\0') return nwords; word[nwords++] = p; while(!isspace(*p) && *p!='\0') p++; if (*p=='\0') return nwords; if (nwords >= maxword) return nwords; *p++ ='\0'; }}int get_ipset_buf(){ FILE *fd_ip; char line_buf[128]; int i; char *tmp[128]; int nword; fd_ip = fopen(IPSET_PATH,"r"); if (fd_ip == NULL) { printf("open IPset error\n"); return -1; } i=count=0; while(fgets(line_buf,sizeof(line_buf),fd_ip) != NULL) { if (*line_buf == '#') continue; nword = getwords(line_buf,tmp,5); if (nword != 4) continue; if (nword >= 128) break; strcpy(ip_buf[count].s_name,tmp[0]); strcpy(ip_buf[count].user_name,tmp[1]); strcpy(ip_buf[count].pass_wd,tmp[2]); strcpy(ip_buf[count].ipset_buf,tmp[3]); count++; } fclose(fd_ip); return 0;}void timeprint(time_t t){ struct tm *tp = localtime(&t); strftime(year_day_buf,sizeof(year_day_buf),"%Y%m%d", tp);}int main(){ struct dirent *dir; DIR *dp; char tmpfile[32]="/tmp/video.log"; FILE *fd; char pn[128]; char cam_no[16]; char hbuf[16]; char mbuf[16]; char sbuf[16]; char ybuf[16]; char mobuf[16]; char dbuf[16]; char ymdbuf[16]; char star_time[128]; char dvr_name[128]; char tmp[20]; char path[128]; char cam_path[128]; time_t mtime; int i,j,n,m,ij; int ipno; char dir_path[128]; char num_buf[10]; char day_path[50]; char sv_path[128]; char mount_ml[128]; MYSQL *connection, mysql; char sql[SQL_SIZE]; char cmd[128]; char cmd1[128]; int state = 0; int flagfd; char old_name[128]; if (fork() != 0) exit(0); if (fork() != 0) exit(0); memset(day_path,0,50); memset(dir_path,0,128); memset(mount_ml,0,128); mtime = time(NULL)-3600*24; timeprint(mtime); fd = fopen(tmpfile,"wr"); if (get_ipset_buf() == -1) fprintf(fd,"get SETip file is error\n"); mysql_init(&mysql); connection = mysql_real_connect(&mysql, DB_HOSTNAME, DB_USERNAME,DB_PASSWORD, DB_DATABASE, 0, 0, 0); if (connection == NULL) { fprintf(fd,"mysql connection error\n"); return -1; } strcpy(day_path,ROOT_PATH); strcat(day_path,year_day_buf); if(opendir(day_path) == NULL) { if(mkdir(day_path, 0777)) { fprintf(fd,"makedir %s is failed \n", day_path); } for(m=1;m < 9;m++) { strcpy(dir_path,day_path); strcat(dir_path,"/"); sprintf(tmp,"%s%d",S_NAME,m); strcat(dir_path,tmp); if(mkdir(dir_path, 0777)) { fprintf(fd,"makedir %s is failed \n", dir_path); } } memset(tmp,0,20); for(m=1;m < 9;m++) { strcpy(dir_path,day_path); strcat(dir_path,"/"); sprintf(tmp,"%s%d",S_NAME,m); strcat(dir_path,tmp); strcat(dir_path,"/"); for(n=1;n<17;n++) { strcpy(sv_path,dir_path); sprintf(num_buf,"%02d",n); strcat(sv_path,num_buf); if(mkdir(sv_path, 0777)) { fprintf(fd,"makedir %s is failed \n", sv_path); } } } } for(ipno=0;ipno < count;ipno++) { memset(tmp,0,20); strcpy(tmp,m_point[ipno+1].mount_point); strcat(tmp,"/"); strcat(tmp,"flag.txt"); flagfd = open(tmp,O_RDONLY); if (flagfd != -1) { close(flagfd); goto HERE; } strcpy(mount_ml,"username="); strcat(mount_ml,ip_buf[ipno].user_name); strcat(mount_ml,",password="); strcat(mount_ml,ip_buf[ipno].pass_wd); if(fork()==0) { fprintf(fd,"ipset_buf=%s mount_point=%s\n", ip_buf[ipno].ipset_buf,m_point[ipno+1].mount_point); execl("/bin/mount","mount","-o", mount_ml, ip_buf[ipno].ipset_buf,m_point[ipno+1].mount_point,NULL); exit(0); } else { sleep(8); HERE: if((dp = opendir(m_point[ipno+1].mount_point)) == NULL) { fprintf(fd,"Open Directory Error\n"); } while ((dir = readdir(dp)) != NULL) { if (dir->d_ino==0) continue; if (strcmp(dir->d_name,".") == 0 || strcmp(dir->d_name,"..") == 0 ||strcmp(dir->d_name,"flag.txt") == 0) continue; if (strcmp(dir->d_name,old_name) == 0) continue; memset(pn,0,128); memset(ymdbuf,0,16); memset(hbuf,0,16); memset(cam_no,0,16); memset(mbuf,0,16); memset(sbuf,0,16); memset(ybuf,0,16); memset(mobuf,0,16); memset(dbuf,0,16); memset(star_time,0,128); memset(cam_path,0,128); memset(dvr_name,0,128); memset(path,0,128); memset(old_name,0,128); strcpy(pn,dir->d_name); strcpy(old_name,dir->d_name); for(i=1,j=0;i<3;i++,j++) cam_no[j] = pn[i]; for(i=4,j=0;i<8;i++,j++) ybuf[j] = pn[i]; for(i=8,j=0;i<10;i++,j++) mobuf[j] = pn[i]; for(i=10,j=0;i<12;i++,j++) dbuf[j]= pn[i]; strcpy(ymdbuf,ybuf); strcat(ymdbuf,mobuf); strcat(ymdbuf,dbuf); for(i=13,j=0;i<15;i++,j++) hbuf[j] = pn[i]; for(i=15,j=0;i<17;i++,j++) mbuf[j] = pn[i]; for(i=17,j=0;i<19;i++,j++) sbuf[j] = pn[i]; sprintf(star_time,"%d-%d-%d %02d:%02d:%02d",atoi(ybuf),atoi(mobuf),atoi(dbuf),atoi(hbuf),atoi(mbuf),atoi(sbuf)); strcpy(path,day_path); strcat(path,"/"); strcat(path,ip_buf[ipno].s_name); strcat(path,"/"); strcat(path,cam_no); strcpy(dvr_name,m_point[ipno+1].mount_point); strcat(dvr_name,"/"); strcat(dvr_name,pn); strcpy(cam_path,"/"); strcat(cam_path,year_day_buf); strcat(cam_path,"/"); strcat(cam_path,ip_buf[ipno].s_name); strcat(cam_path,"/"); strcat(cam_path,cam_no); strcat(cam_path,"/"); strcat(cam_path,pn); if(atoi(ymdbuf) < atoi(year_day_buf)) { sprintf(cmd,"/bin/mv %s %s ",dvr_name,path); ij = system(cmd); fprintf(fd,"result %d cmd %s\n",ij,cmd); if(ij!=0) continue; bzero(sql,SQL_SIZE); sprintf(sql,"INSERT INTO dvr_watch (dvr_no,cam_no, start_time,path )\ VALUES ('%s',%d,'%s','%s')",\ ip_buf[ipno].s_name,atoi(cam_no),star_time,cam_path); state = mysql_query(connection,sql); if (state != 0) { fprintf(fd,"*********************sql %s\n",sql); fprintf(fd,"ysql error:%s\n",mysql_error(connection)); } } } if(!closedir(dp)) fprintf(fd,"has close dp\n"); } } mysql_close(connection); fprintf(fd,"***********after close mysql\n"); fclose(fd); sleep(20); for(i=1;i<9;i++) { sprintf(cmd1,"/usr/bin/smbumount %s",m_point[i].mount_point); system(cmd1); } exit(0); return 0; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -