| |
| #include <unistd.h> |
| #include <errno.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <dirent.h> |
| #include <string.h> |
| #include <sys/file.h> |
| #include <sys/types.h> |
| #include <sys/stat.h> |
| #include <sys/ipc.h> |
| #include <sys/msg.h> |
| #include "nvserver.h" |
| #include "nv_typedef.h" |
| #include <message.h> |
| #ifdef FOTA_AB |
| #include "pub_flags.h" |
| #include "flags_api.h" |
| #endif |
| #ifdef __cplusplus |
| extern"C"{ |
| #endif |
| static void nvConfig();static void nvInit();static int nvDirInit();static void |
| analyMsg(T_NV_MSG_INFO*msgrecv,T_NV_MSG_RESULT*msgsnd);static bool checkNvFs( |
| char*file);static bool isCfgConfiged(char*configFile);static bool isNvConfiged( |
| char*configFile);static uint getSum(const char*s,int len);static int |
| loadFactroyParam(T_NV_NODE*list);static int restoreNvFs(char*dstFile,char* |
| srcFile);static int loadNvFs(char*file);static int addConfigFile(char*nvFile, |
| char*configFile);static int saveNvFs(char*nvName,char*nvFile);static int nvset( |
| char*file,const char*key,const char*value,int saveFlag);static int nvget(char* |
| file,char*key,char*value);static int nvunset(char*file,char*key);static int |
| nvclear(char*file);static int nvreset(char*file);static int nvcommit(char*file); |
| T_NV_NODE*nv_list;int nvserver_main(int argc,char*argv[]){int msgId= |
| (0x18f3+603-0x1b4e);T_NV_MSG_INFO rcvBuf;T_NV_MSG_RESULT sndBuf;struct msqid_ds |
| msgInfo;prctl(PR_SET_NAME,"\x6e\x76\x73\x65\x72\x76\x65\x72",(0x17c+7964-0x2098) |
| ,(0x1b2+3384-0xeea),(0x1b33+42-0x1b5d));memset(&rcvBuf,(0xfe5+3425-0x1d46), |
| sizeof(rcvBuf));memset(&sndBuf,(0x49f+3594-0x12a9),sizeof(sndBuf));memset(& |
| msgInfo,(0x596+5416-0x1abe),sizeof(msgInfo));msgId=msgget(MODULE_ID_NV,IPC_CREAT |
| |(0xb41+1212-0xe7d));if(-(0xb8b+3681-0x19eb)==msgId){printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x6d\x73\x67\x67\x65\x74\x20\x6d\x73\x67\x49\x64\x20\x66\x61\x69\x6c\x2c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| ,errno);return-(0x74c+3721-0x15d4);}if(-(0x1956+1177-0x1dee)!=msgctl(msgId, |
| IPC_STAT,&msgInfo)){msgInfo.msg_qbytes=262144;if(-(0x638+2633-0x1080)==msgctl( |
| msgId,IPC_SET,&msgInfo))printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x6d\x73\x67\x63\x74\x6c\x20\x6d\x73\x67\x49\x64\x20\x66\x61\x69\x6c\x2c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| ,errno);}if(nvDirInit()!=(0x922+2462-0x12c0)){printf( |
| "\x6e\x76\x44\x69\x72\x49\x6e\x69\x74\x20\x66\x61\x69\x6c\x65\x21" "\n");return- |
| (0x5ca+6642-0x1fbb);}nvConfig();nvInit();while((0x5ea+2487-0xfa0)){if(- |
| (0xb2+4408-0x11e9)==msgrcv(msgId,&rcvBuf,sizeof(T_NV_MSG_INFO)-sizeof(long), |
| MSG_TYPE_NV,(0x1879+2826-0x2383))){printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x6d\x73\x67\x72\x63\x76\x20\x66\x61\x69\x6c\x2c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64\x21" "\n" |
| ,errno);continue;}analyMsg(&rcvBuf,&sndBuf);if(-(0x4c9+939-0x873)==msgsnd(msgId, |
| &sndBuf,sizeof(T_NV_MSG_RESULT)-sizeof(long),(0x9c9+5912-0x20e1))){printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x6d\x73\x67\x73\x6e\x64\x20\x66\x61\x69\x6c\x2c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64\x21" "\n" |
| ,errno);continue;}}return((0x51d+4365-0x162a));}static void configdir(char*dir){ |
| DIR*dp;int ret;struct dirent*entry;struct stat statbuf;if((dp=opendir(dir))== |
| NULL){fprintf(stderr, |
| "\x63\x61\x6e\x6e\x6f\x74\x20\x6f\x70\x65\x6e\x20\x64\x69\x72\x65\x63\x74\x6f\x72\x79\x3a\x20\x25\x73" "\n" |
| ,dir);return;}chdir(dir);while((entry=readdir(dp))!=NULL){ret=lstat(entry-> |
| d_name,&statbuf);if(ret<(0x23b5+574-0x25f3)){fprintf(stderr, |
| "\x6c\x73\x74\x61\x74\x20\x65\x72\x72\x6f\x72\x3a\x20\x25\x73" "\n",strerror( |
| errno));chdir("\x2e\x2e");closedir(dp);return;}if(!S_ISDIR(statbuf.st_mode)){if( |
| strcmp("\x2e",entry->d_name)==(0x6a8+3535-0x1477)||strcmp("\x2e\x2e",entry-> |
| d_name)==(0xbc2+2073-0x13db))continue;if(!isNvConfiged(entry->d_name)){if( |
| addConfigFile(entry->d_name,NULL)!=RESULT_SUCCESS)printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x63\x6f\x6e\x66\x69\x67\x20\x25\x73\x20\x65\x72\x72\x6f\x72\x21" "\n" |
| ,entry->d_name);}}}chdir("\x2e\x2e");closedir(dp);}static void nvConfig(){char* |
| val=NULL;FILE*fp=NULL;char buf[NV_MAX_CONFIG_LEN]={(0x71b+4664-0x1953)};fp=fopen |
| (NV_CONFIG_FILE,"\x72\x6f");if(!fp){printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x6f\x70\x65\x6e\x20\x25\x73\x20\x66\x69\x6c\x65\x20\x66\x61\x69\x6c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64\x21" "\n" |
| ,NV_CONFIG_FILE,errno);return;}while(fgets(buf,NV_MAX_CONFIG_LEN,fp)){if(buf[ |
| (0xadc+1540-0x10e0)]=='\n'||buf[(0x237+3452-0xfb3)]==((char)(0xd14+1114-0x114b)) |
| )continue;val=strchr(buf,((char)(0x69d+6638-0x204e)));if(!val){printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x25\x73\x20\x66\x69\x6c\x65\x20\x66\x6f\x72\x6d\x61\x74\x20\x65\x72\x72\x6f\x72\x3a\x20\x73\x74\x72\x20\x3d\x20\x25\x73\x21" "\n" |
| ,NV_CONFIG_FILE,buf);continue;}buf[strlen(buf)-(0x516+6614-0x1eeb)]='\0';*val++= |
| '\0';if(!isCfgConfiged(buf)){if(addConfigFile(val,buf)!=RESULT_SUCCESS)printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x63\x6f\x6e\x66\x69\x67\x20\x25\x73\x20\x65\x72\x72\x6f\x72\x21" "\n" |
| ,buf);}}fclose(fp);configdir(NV_FS_MAIN_PATH);}static int nvDirInit(){if(access( |
| NV_FS_PATH,F_OK)!=(0x862+6253-0x20cf)){if(mkdir(NV_FS_PATH,(0x6da+6986-0x2037)) |
| !=(0x7a0+2567-0x11a7)){printf( |
| "\x6e\x65\x72\x76\x65\x72\x20\x6d\x6b\x64\x69\x72\x20\x25\x73\x20\x66\x61\x6c\x69\x2c\x65\x72\x72\x6e\x6f\x3d\x25\x64" "\n" |
| ,NV_FS_PATH,errno);return-(0x116d+2075-0x1987);}if(mkdir(NV_FS_MAIN_PATH, |
| (0x989+7455-0x24bb))!=(0x4b5+7496-0x21fd)){printf( |
| "\x6e\x65\x72\x76\x65\x72\x20\x6d\x6b\x64\x69\x72\x20\x25\x73\x20\x66\x61\x6c\x69\x2c\x65\x72\x72\x6e\x6f\x3d\x25\x64" "\n" |
| ,NV_FS_MAIN_PATH,errno);return-(0x19fb+224-0x1ada);}if(mkdir(NV_FS_BACKUP_PATH, |
| (0x156c+2000-0x1b4f))!=(0xc4+9751-0x26db)){printf( |
| "\x6e\x65\x72\x76\x65\x72\x20\x6d\x6b\x64\x69\x72\x20\x25\x73\x20\x66\x61\x6c\x69\x2c\x65\x72\x72\x6e\x6f\x3d\x25\x64" "\n" |
| ,NV_FS_BACKUP_PATH,errno);return-(0xffc+1393-0x156c);}}else{if(access( |
| NV_FS_MAIN_PATH,F_OK)!=(0xbf3+4426-0x1d3d)){if(mkdir(NV_FS_MAIN_PATH, |
| (0xa28+4-0x83f))!=(0x3ca+3818-0x12b4)){printf( |
| "\x6e\x65\x72\x76\x65\x72\x20\x6d\x6b\x64\x69\x72\x20\x25\x73\x20\x66\x61\x6c\x69\x2c\x65\x72\x72\x6e\x6f\x3d\x25\x64" "\n" |
| ,NV_FS_MAIN_PATH,errno);return-(0x3f+2678-0xab4);}}if(access(NV_FS_BACKUP_PATH, |
| F_OK)!=(0x2000+1094-0x2446)){if(mkdir(NV_FS_BACKUP_PATH,(0x14b4+1007-0x16b6))!= |
| (0xd04+4607-0x1f03)){printf( |
| "\x6e\x65\x72\x76\x65\x72\x20\x6d\x6b\x64\x69\x72\x20\x25\x73\x20\x66\x61\x6c\x69\x2c\x65\x72\x72\x6e\x6f\x3d\x25\x64" "\n" |
| ,NV_FS_BACKUP_PATH,errno);return-(0x4cc+7388-0x21a7);}}}return |
| (0x126f+2767-0x1d3e);}static void nvInit(){T_NV_NODE*list=NULL;char nvMainFile[ |
| NV_PATH_LEN]={(0xe69+885-0x11de)};char nvBackupFile[NV_PATH_LEN]={ |
| (0xa1b+5459-0x1f6e)}; |
| #ifdef FOTA_AB |
| T_FLAGS_INFO flags_info={(0x157d+1382-0x1ae3)};int ret=(0xc67+5925-0x238c); |
| #endif |
| for(list=nv_list;list;list=list->next){snprintf(nvMainFile,NV_PATH_LEN, |
| "\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,list->nvFile);snprintf(nvBackupFile, |
| NV_PATH_LEN,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,list->nvFile);if(checkNvFs( |
| nvMainFile)){if(!checkNvFs(nvBackupFile))restoreNvFs(nvBackupFile,nvMainFile);} |
| else if(checkNvFs(nvBackupFile)){restoreNvFs(nvMainFile,nvBackupFile);}else{ |
| loadFactroyParam(list);nvcommit(list->nvFile);continue;}loadNvFs(list->nvFile); |
| if(!strcmp(list->nvFile,NV_CFG)&&get_update_status()==(0x8ad+626-0xb1d)){ |
| reloadFactroyParam(list);delete_not_needed(list);nvcommit(list->nvFile); |
| #ifdef FOTA_AB |
| ret=flags_get(&flags_info);flags_info.boot_fota_flag.fota_status= |
| (0x381+3675-0x11dc);ret=flags_set(&flags_info); |
| #endif |
| }}}uint hash(const char*s){uint hash=(0x4d3+2345-0xdfc);while(*s){hash= |
| NV_HASH_MUL*hash+*s++;}return hash;}static int loadFactroyParam(T_NV_NODE*list){ |
| char*val=NULL;FILE*fp=NULL;T_NV_CONFIG*config=NULL;char buf[NV_MAX_ITEM_LEN]={ |
| (0x861+3985-0x17f2)};for(config=list->fileList;config;config=config->next){fp= |
| fopen(config->configFile,"\x72\x6f");if(!fp){printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x6f\x70\x65\x6e\x20\x25\x73\x20\x66\x69\x6c\x65\x20\x66\x61\x69\x6c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64\x21" "\n" |
| ,config->configFile,errno);return RESULT_FILE_OPEN_FAIL;}while(fgets(buf, |
| NV_MAX_ITEM_LEN,fp)){if(buf[(0x14f0+680-0x1798)]=='\n'||buf[(0x1c3+1200-0x673)] |
| ==((char)(0x345+8945-0x2613)))continue;val=strchr(buf, |
| ((char)(0x1961+2653-0x2381)));if(!val){printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x25\x73\x20\x66\x69\x6c\x65\x20\x66\x6f\x72\x6d\x61\x74\x20\x65\x72\x72\x6f\x72\x3a\x73\x74\x72\x69\x6e\x67\x20\x3d\x20\x25\x73" "\n" |
| ,config->configFile,buf);continue;}if(buf[strlen(buf)-(0xfbb+4254-0x2058)]=='\n' |
| )buf[strlen(buf)-(0x46d+7913-0x2355)]='\0';*val++='\0';nvset(list->nvFile,buf, |
| val,(0x118c+5421-0x26b8));}printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x6c\x6f\x61\x64\x46\x61\x63\x74\x72\x6f\x79\x50\x61\x72\x61\x6d\x20\x25\x73\x21" "\n" |
| ,config->configFile);fclose(fp);}return RESULT_SUCCESS;}static bool checkNvFs( |
| char*file){int len=(0xc4c+2991-0x17fb);int cnt=(0x1104+958-0x14c2);FILE*fp=NULL; |
| char*buf=NULL;struct stat statbuff={(0x1afc+2002-0x22ce)};if(stat(file,&statbuff |
| )<(0x12d1+3330-0x1fd3))return false;len=statbuff.st_size;if(len<NV_CHECK_SIZE) |
| return false;fp=fopen(file,"\x72\x6f");if(!fp)return false;buf=(char*)malloc(len |
| );if(!buf){fclose(fp);return false;}cnt=(0x1a8c+2886-0x25d2);while(cnt<len){cnt= |
| cnt+fread(buf+cnt,(0x1b96+975-0x1f64),len-cnt,fp);if(ferror(fp)){clearerr(fp); |
| free(buf);fclose(fp);return false;}}if(len!=cnt){free(buf);fclose(fp);return |
| false;}if(getSum(buf,len-NV_CHECK_SIZE)+NV_FILE_FLAG!=*(uint*)(buf+len- |
| NV_CHECK_SIZE)){free(buf);fclose(fp);return false;}free(buf);fclose(fp);return |
| true;}static int copyfile(const char*from,const char*to){int fd_to;int fd_from; |
| char buf[(0x14ba+4601-0x16b3)];ssize_t nread;int ret=-(0x165c+3363-0x237e); |
| fd_from=open(from,O_RDONLY);if(fd_from<(0xa9d+1732-0x1161))return- |
| (0x688+3633-0x14b7);fd_to=open(to,O_RDWR|O_CREAT|O_TRUNC|O_SYNC, |
| (0x1fea+283-0x1f65));if(fd_to<(0xd16+3668-0x1b6a)){ret=-(0x1999+2606-0x23c4); |
| goto out_error;}while((0x1308+2090-0x1b31)){char*out_ptr;ssize_t nwritten;nread= |
| read(fd_from,buf,sizeof(buf));if(nread==(0x10e8+3398-0x1e2e)){break;}else{if( |
| nread<(0x171+2782-0xc4f)){if(errno==EINTR||errno==EAGAIN){continue;}else{ret=- |
| (0x7ca+6506-0x2130);goto out_error;}}}out_ptr=buf;do{nwritten=write(fd_to, |
| out_ptr,nread);if(nwritten>(0x10a6+2317-0x19b3)){nread-=nwritten;out_ptr+= |
| nwritten;}else{if(nwritten<(0x8fb+2056-0x1103)){if(errno==EINTR||errno==EAGAIN){ |
| continue;}else{ret=-(0x1b64+1017-0x1f58);goto out_error;}}}}while(nread> |
| (0x1495+1492-0x1a69));}ret=fsync(fd_to);if(ret<(0x854+2878-0x1392)){printf( |
| "\x53\x79\x6e\x63\x20\x46\x61\x69\x6c\x65\x64\x3a\x25\x73\x2c\x20\x66\x69\x6c\x65\x20\x70\x61\x74\x68\x3a\x25\x73" |
| ,strerror(errno),to);goto out_error;}if(close(fd_to)<(0x16a1+3500-0x244d)){fd_to |
| =-(0x18cd+1578-0x1ef6);ret=-(0x2330+772-0x262e);goto out_error;}close(fd_from); |
| return(0x4b1+1400-0xa29);out_error:printf( |
| "\x63\x6f\x70\x79\x66\x69\x6c\x65\x20\x25\x73\x20\x74\x6f\x20\x25\x73\x20\x65\x72\x72\x6f\x72\x3a\x25\x64" "\n" |
| ,from,to,ret);close(fd_from);if(fd_to>=(0x3e4+1712-0xa94))close(fd_to);return |
| ret;}static int restoreNvFs(char*dstFile,char*srcFile){if(copyfile(srcFile, |
| dstFile)!=(0x516+8438-0x260c))return RESULT_FAIL;return RESULT_SUCCESS;}static |
| int loadNvFs(char*file){int len=(0x1d4f+2199-0x25e6);int cnt=(0x811+2087-0x1038) |
| ;FILE*fp=NULL;char*buf=NULL;char*name=NULL;char*value=NULL;char*eq=NULL;struct |
| stat statbuff={(0x1913+3040-0x24f3)};char nvFile[NV_PATH_LEN]={ |
| (0x3c5+4050-0x1397)};sprintf(nvFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file) |
| ;if(stat(nvFile,&statbuff)<(0x1dcc+46-0x1dfa))return RESULT_FAIL;len=statbuff. |
| st_size;if(NV_CHECK_SIZE>len)return RESULT_FAIL;fp=fopen(nvFile,"\x72\x6f");if(! |
| fp)return RESULT_FILE_OPEN_FAIL;len=len-NV_CHECK_SIZE;buf=(char*)malloc(len+ |
| (0x4d5+6495-0x1e33));if(!buf){fclose(fp);return RESULT_MALLOC_FAIL;}memset(buf, |
| (0x7aa+4280-0x1862),len+(0x191f+2874-0x2458));cnt=(0x162+8919-0x2439);while(cnt< |
| len){cnt=cnt+fread(buf+cnt,(0xfd6+5620-0x25c9),len-cnt,fp);if(ferror(fp)){ |
| clearerr(fp);fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}}if(cnt!=len){ |
| fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}buf[len]='\0';name=buf;while( |
| *name){if(!(eq=strchr(name,((char)(0xa82+1630-0x10a3))))){break;}*eq='\0';value= |
| eq+(0x8+2302-0x905);nvset(file,name,value,(0x674+4192-0x16d3));*eq= |
| ((char)(0x2545+484-0x26ec));name=value+strlen(value)+(0x1054+5776-0x26e3);}free( |
| buf);fclose(fp);return RESULT_SUCCESS;}static void analyMsg(T_NV_MSG_INFO* |
| msgrecv,T_NV_MSG_RESULT*msgsnd){switch(msgrecv->nvType){case MSG_GET:msgsnd-> |
| result=nvget(msgrecv->file,msgrecv->key,msgsnd->value);break;case MSG_SET:msgsnd |
| ->result=nvset(msgrecv->file,msgrecv->key,msgrecv->value,msgrecv->saveflag); |
| break;case MSG_UNSET:msgsnd->result=nvunset(msgrecv->file,msgrecv->key);break; |
| case MSG_CLEAR:msgsnd->result=nvclear(msgrecv->file);break;case MSG_RESET:msgsnd |
| ->result=nvreset(msgrecv->file);break;case MSG_SHOW:msgsnd->result=saveNvFs( |
| msgrecv->file,NV_FS_SHOW);;break;case MSG_COMMIT:msgsnd->result=nvcommit(msgrecv |
| ->file);break;default:break;}msgsnd->msgType=msgrecv->pid;msgsnd->msgIndex= |
| msgrecv->msgIndex;}static int addConfigFile(char*nvFile,char*configFile){ |
| T_NV_NODE*list=NULL;T_NV_NODE*newList=NULL;T_NV_CONFIG*newConfig=NULL;if(!nvFile |
| ){printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x70\x61\x72\x61\x6d\x20\x69\x6c\x6c\x65\x67\x61\x6c\x21" "\n" |
| );return RESULT_INVAL;}if(configFile){newConfig=(T_NV_CONFIG*)malloc(sizeof( |
| T_NV_CONFIG));if(!newConfig)return RESULT_MALLOC_FAIL;strncpy(newConfig-> |
| configFile,configFile,NV_PATH_LEN-(0xd44+42-0xd6d));newConfig->configFile[ |
| NV_PATH_LEN-(0xedd+6091-0x26a7)]='\0';newConfig->next=NULL;}for(list=nv_list; |
| list;list=list->next){if(strcmp(list->nvFile,nvFile)==(0x1659+932-0x19fd))break; |
| }if(!list){newList=(T_NV_NODE*)malloc(sizeof(T_NV_NODE));if(!newList){if( |
| newConfig)free(newConfig);return RESULT_MALLOC_FAIL;}newList->next=NULL;strncpy( |
| newList->nvFile,nvFile,NV_PATH_LEN-(0x184d+2942-0x23ca));newList->nvFile[ |
| NV_PATH_LEN-(0x167b+2415-0x1fe9)]='\0';memset(newList->nvTable, |
| (0x19cd+2692-0x2451),NV_HASH_LEN*(0x19bf+51-0x19ee));newList->fileList=newConfig |
| ;if(!nv_list)nv_list=newList;else{newList->next=nv_list->next;nv_list->next= |
| newList;}}else if(!list->fileList)list->fileList=newConfig;else{if(newConfig== |
| NULL)return RESULT_FAIL;newConfig->next=list->fileList->next;list->fileList-> |
| next=newConfig;}return RESULT_SUCCESS;}static bool isCfgConfiged(char*configFile |
| ){T_NV_NODE*list=NULL;T_NV_CONFIG*config=NULL;for(list=nv_list;list;list=list-> |
| next){for(config=list->fileList;config;config=config->next){if(!strcmp(config-> |
| configFile,configFile))return true;}}return false;}static bool isNvConfiged(char |
| *nvFile){T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(!strcmp( |
| list->nvFile,nvFile))return true;}return false;}static uint getSum(const char*s, |
| int len){uint sum=(0x16+891-0x391);char*data=(char*)s;while(len-- > |
| (0x13b+1801-0x844)){sum+=(*data++);}return sum;}static int saveNvFs(char*nvName, |
| char*nvFile){int i=(0x143b+2502-0x1e01);int sum=(0xed1+2051-0x16d4);int bufSize= |
| (0xbfd+3993-0x1b96);int itemSize=(0x1ce0+2251-0x25ab);int ret= |
| (0x1422+3782-0x22e8);int fp=(0x427+8674-0x2609);char*buf=NULL;T_NV_NODE*list= |
| NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list;list=list->next){if(strcmp(list-> |
| nvFile,nvName))continue;fp=open(nvFile,O_SYNC|O_RDWR|O_CREAT|O_TRUNC, |
| (0xe5f+3305-0x19a8));if(fp==-(0x134b+2222-0x1bf8)){printf( |
| "\x6f\x70\x65\x6e\x20\x25\x73\x20\x66\x61\x69\x6c\x2c\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| ,nvFile,errno);return RESULT_FILE_OPEN_FAIL;}buf=(char*)malloc(NV_BLOCK_SIZE);if |
| (!buf){close(fp);return RESULT_MALLOC_FAIL;}for(i=(0x1469+4354-0x256b);i< |
| NV_HASH_LEN;i++){for(item=list->nvTable[i];item;item=item->next){if(strcmp( |
| nvFile,NV_FS_SHOW)&&!item->saveFlag)continue;itemSize=strlen(item->key)+strlen( |
| item->value)+(0x16da+469-0x18ad);if(bufSize+itemSize>NV_BLOCK_SIZE){if(write(fp, |
| buf,bufSize)<(0x138b+1889-0x1aec)){printf( |
| "\x65\x72\x72\x6f\x72\x20\x25\x73\x20\x25\x64\x3a\x20\x77\x72\x69\x74\x65\x20\x66\x61\x69\x6c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| ,__FILE__,__LINE__,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}sum |
| +=getSum(buf,bufSize);bufSize=(0x440+1244-0x91c);}sprintf(buf+bufSize, |
| "\x25\x73\x3d\x25\x73",item->key,item->value);bufSize+=itemSize;}}if(bufSize!= |
| (0x4d2+3780-0x1396)){if(write(fp,buf,bufSize)<(0x13c1+1299-0x18d4)){printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x77\x72\x69\x74\x65\x20\x66\x61\x69\x6c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| ,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}sum+=getSum(buf, |
| bufSize);}sum+=NV_FILE_FLAG;if(write(fp,&sum,NV_CHECK_SIZE)<(0x144c+1015-0x1843) |
| ){printf( |
| "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x77\x72\x69\x74\x65\x20\x66\x61\x69\x6c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| ,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}ret=fsync(fp);free( |
| buf);close(fp);if(ret<(0x14ea+4419-0x262d)){printf( |
| "\x53\x79\x6e\x63\x20\x46\x61\x69\x6c\x65\x64\x3a\x25\x73\x2c\x20\x66\x69\x6c\x65\x20\x70\x61\x74\x68\x3a\x25\x73" |
| ,strerror(errno),nvFile);return ret;}return RESULT_SUCCESS;}return |
| RESULT_NO_FILE;}static int nvget(char*file,char*key,char*value){int index= |
| (0x84c+255-0x94b);T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list; |
| list=list->next){if(strcmp(list->nvFile,file))continue;index=hash(key)% |
| NV_HASH_LEN;for(item=list->nvTable[index];item;item=item->next){if(strcmp(item-> |
| key,key))continue;strncpy(value,item->value,NV_MAX_VAL_LEN-(0x9db+3631-0x1809)); |
| value[NV_MAX_VAL_LEN-(0x24e7+478-0x26c4)]='\0';return RESULT_SUCCESS;}}return |
| RESULT_NO_ITEM;}static int nvset(char*file,const char*key,const char*value,int |
| saveFlag){int index=(0xae3+3051-0x16ce);int ret=(0x1a7+2511-0xb76);int |
| key_buf_len=(0x1b7a+229-0x1c5f);int value_buf_len=(0x1a66+2644-0x24ba);T_NV_NODE |
| *list=NULL;T_NV_ITEM*item=NULL;T_NV_ITEM*newItem=NULL;if(NULL==key||NULL==value) |
| return RESULT_FAIL;key_buf_len=strlen(key)+(0x2070+190-0x212d);value_buf_len= |
| strlen(value)+(0x1f7c+1176-0x2413);for(list=nv_list;list;list=list->next){if( |
| strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for(item=list-> |
| nvTable[index];item;item=item->next){if(strcmp(item->key,key))continue;if( |
| saveFlag)item->saveFlag=saveFlag;if(!strcmp(item->value,value))return |
| RESULT_SUCCESS;free(item->value);item->value=(char*)malloc(value_buf_len);if(! |
| item->value)return RESULT_MALLOC_FAIL;strncpy(item->value,value,value_buf_len- |
| (0x2f1+8640-0x24b0));item->value[value_buf_len-(0x12d+6454-0x1a62)]='\0';return |
| RESULT_SUCCESS;}newItem=(T_NV_ITEM*)malloc(sizeof(T_NV_ITEM));if(!newItem)return |
| RESULT_MALLOC_FAIL;newItem->key=(char*)malloc(key_buf_len);if(!newItem->key){ |
| free(newItem);return RESULT_MALLOC_FAIL;}newItem->value=(char*)malloc( |
| value_buf_len);if(!newItem->value){free(newItem->key);free(newItem);return |
| RESULT_MALLOC_FAIL;}strncpy(newItem->key,key,key_buf_len-(0x6e1+7150-0x22ce)); |
| newItem->key[key_buf_len-(0x157a+3690-0x23e3)]='\0';strncpy(newItem->value,value |
| ,value_buf_len-(0x125a+4317-0x2336));newItem->value[value_buf_len- |
| (0x2354+343-0x24aa)]='\0';newItem->next=NULL;newItem->saveFlag=saveFlag;newItem |
| ->update_flag=(0xe45+3698-0x1cb7);if(!list->nvTable[index])list->nvTable[index]= |
| newItem;else{newItem->next=list->nvTable[index]->next;list->nvTable[index]->next |
| =newItem;}return RESULT_SUCCESS;}ret=addConfigFile(file,NULL);if(ret== |
| RESULT_SUCCESS)return nvset(file,key,value,saveFlag);else return ret;}static int |
| nvunset(char*file,char*key){int index=(0x1756+584-0x199e);T_NV_NODE*list=NULL; |
| T_NV_ITEM*item=NULL;T_NV_ITEM*prev=NULL;for(list=nv_list;list;list=list->next){ |
| if(strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for(item=list |
| ->nvTable[index];item;prev=item,item=item->next){if(strcmp(item->key,key)) |
| continue;if(!prev)list->nvTable[index]=item->next;else prev->next=item->next; |
| free(item->key);free(item->value);free(item);return RESULT_SUCCESS;}}return |
| RESULT_NO_ITEM;}static int nvreset(char*file){int ret=(0xf1a+4235-0x1fa5); |
| T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(strcmp(list-> |
| nvFile,file))continue;ret=nvclear(file);if(ret!=RESULT_SUCCESS)return ret;if( |
| loadFactroyParam(list)!=RESULT_SUCCESS)return RESULT_FAIL;return nvcommit(file); |
| }return RESULT_NO_FILE;}static int nvclear(char*file){int i=(0x7ab+1449-0xd54); |
| T_NV_NODE*list=NULL;T_NV_ITEM*cur=NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list |
| ;list=list->next){if(strcmp(list->nvFile,file))continue;for(i=(0x77b+1983-0xf3a) |
| ;i<NV_HASH_LEN;i++){for(item=list->nvTable[i];item;){cur=item;item=item->next; |
| free(cur->key);free(cur->value);free(cur);}list->nvTable[i]=NULL;}return |
| RESULT_SUCCESS;}return RESULT_NO_FILE;}static int nvcommit(char*file){int ret= |
| (0x1ffc+1070-0x242a);char nvMainFile[NV_PATH_LEN]={(0x10dd+4579-0x22c0)};char |
| nvBackupFile[NV_PATH_LEN]={(0x16e9+728-0x19c1)};sprintf(nvMainFile, |
| "\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);sprintf(nvBackupFile, |
| "\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,file);ret=saveNvFs(file,nvMainFile);if( |
| ret!=RESULT_SUCCESS)return ret;return restoreNvFs(nvBackupFile,nvMainFile);} |
| #ifdef __cplusplus |
| } |
| #endif |
| |