1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
int md5_check_list(const char *md5list,const char *rootpath) { int ret = 0; int fd_md5; int count_success = 0; int count_fail = 0; FILE *listFile; char md5_correct[MD5_LENTH+1] = {0}; char filename[PATH_LENTH] = {0}; char entryname[PATH_LENTH] = {0}; char record_file[PATH_LENTH] = {0}; char check_success_msg[MSG_LENTH] = {0}; char check_failed_msg[MSG_LENTH] = {0}; const char* note = "md5 check failed files:\n"; const char* none_msg = "[File not exist] "; const char* wrong_msg = "[Md5 not match] ";
listFile = fopen(md5list,"r"); if(NULL == listFile){ printf("[md5_check] Open %s failed!\n",md5list); return -1; } if( NULL != get_current_time() ) { sprintf(record_file,"%s%s%s%s",RECORD_FILE_PATH,RECORD_FILE_PREFIX,get_current_time(),".log"); }else { sprintf(record_file,"%s%s%s%s",RECORD_FILE_PATH,RECORD_FILE_PREFIX,"timenull",".log"); } fd_md5 = open(record_file,O_RDWR O_CREAT O_TRUNC, S_IRUSR S_IWUSR); if( fd_md5 > 0 ){ write(fd_md5,note,strlen(note)); write(fd_md5,"\n",1); } else { printf("[md5_check] Open %s failed! md5 check quit-----------------\n",record_file); return -1; } while( fscanf(listFile,"%s",md5_correct) != EOF ){ fscanf(listFile,"%s",entryname ); if (rootpath == NULL) { snprintf(filename, PATH_LENTH, "%s%s", DEFAULT_ROOT_PATH, entryname); } else { snprintf(filename, PATH_LENTH, "%s%s", rootpath, entryname); } ret = is_file(filename); if( -1 == ret ){ write(fd_md5,none_msg,strlen(none_msg)); write(fd_md5,filename,strlen(filename)); write(fd_md5,"\n",1); count_fail ++; continue; }else if( 1 == ret ) { continue; } if( 0 == strncmp(md5_correct,md5_check(filename),MD5_LENTH) ) { count_success ++; } else { write(fd_md5,wrong_msg,strlen(none_msg)); write(fd_md5,filename,strlen(filename)); write(fd_md5,"\n",1); count_fail ++; } } write(fd_md5,"\n",1); sprintf(check_failed_msg,"%d%s",count_fail," files update failed!"); write(fd_md5,check_failed_msg,strlen(check_failed_msg)); write(fd_md5,"\n",1); sprintf(check_success_msg,"%d%s",count_success," files update success!"); write(fd_md5,check_success_msg,strlen(check_success_msg)); write(fd_md5,"\n",1); close(fd_md5);
return 0; }
|