| |
| #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= |
| (0x133f+1096-0x1787);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", |
| (0x1ba8+243-0x1c9b),(0xa86+5426-0x1fb8),(0x267+2866-0xd99));memset(&rcvBuf, |
| (0x91f+2034-0x1111),sizeof(rcvBuf));memset(&sndBuf,(0xcca+5997-0x2437),sizeof( |
| sndBuf));memset(&msgInfo,(0x976+1531-0xf71),sizeof(msgInfo));msgId=msgget( |
| MODULE_ID_NV,IPC_CREAT|(0x1728+2792-0x2090));if(-(0xe9f+4497-0x202f)==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-(0x3f4+2873-0xf2c);}if(-(0xeda+2925-0x1a46)!=msgctl(msgId, |
| IPC_STAT,&msgInfo)){msgInfo.msg_qbytes=262144;if(-(0xc06+1979-0x13c0)==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()!=(0x71+1755-0x74c)){printf( |
| "\x6e\x76\x44\x69\x72\x49\x6e\x69\x74\x20\x66\x61\x69\x6c\x65\x21" "\n");return- |
| (0x3f0+1133-0x85c);}nvConfig();nvInit();while((0x1f55+427-0x20ff)){if(- |
| (0x98f+6077-0x214b)==msgrcv(msgId,&rcvBuf,sizeof(T_NV_MSG_INFO)-sizeof(long), |
| MSG_TYPE_NV,(0x24c+9081-0x25c5))){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(-(0x447+820-0x77a)==msgsnd(msgId, |
| &sndBuf,sizeof(T_NV_MSG_RESULT)-sizeof(long),(0x1a78+104-0x1ae0))){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((0x480+4075-0x146b));}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<(0x3a7+1353-0x8f0)){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)==(0x74a+2592-0x116a)||strcmp("\x2e\x2e",entry-> |
| d_name)==(0xc92+5592-0x226a))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]={(0x160a+3421-0x2367)};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[ |
| (0x8b0+7567-0x263f)]=='\n'||buf[(0xfcf+5657-0x25e8)]== |
| ((char)(0x7e7+2372-0x1108)))continue;val=strchr(buf,((char)(0x126+680-0x391))); |
| 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)-(0x603+2139-0xe5d)]='\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)!=(0x83d+6713-0x2276)){if(mkdir(NV_FS_PATH,(0x18a9+1796-0x1dc0)) |
| !=(0x1580+1-0x1581)){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-(0x16a6+3141-0x22ea);}if(mkdir(NV_FS_MAIN_PATH, |
| (0xf3d+857-0x10a9))!=(0x53c+7804-0x23b8)){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-(0x821+1150-0xc9e);}if(mkdir(NV_FS_BACKUP_PATH, |
| (0x410+5270-0x16b9))!=(0x1137+5282-0x25d9)){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-(0xb19+921-0xeb1);}}else{if(access( |
| NV_FS_MAIN_PATH,F_OK)!=(0x10c6+4658-0x22f8)){if(mkdir(NV_FS_MAIN_PATH, |
| (0xcc6+1585-0x110a))!=(0x1933+91-0x198e)){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-(0x320+871-0x686);}}if(access(NV_FS_BACKUP_PATH, |
| F_OK)!=(0x1c5+3565-0xfb2)){if(mkdir(NV_FS_BACKUP_PATH,(0x1b4f+1025-0x1d63))!= |
| (0xdbf+237-0xeac)){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-(0x124+3162-0xd7d);}}}return(0x70a+5372-0x1c06) |
| ;}static void nvInit(){T_NV_NODE*list=NULL;char nvMainFile[NV_PATH_LEN]={ |
| (0x16bf+223-0x179e)};char nvBackupFile[NV_PATH_LEN]={(0xd58+5431-0x228f)}; |
| #ifdef FOTA_AB |
| T_FLAGS_INFO flags_info={(0x181+9238-0x2597)};int ret=(0xa12+7136-0x25f2); |
| #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()==(0x4fb+3746-0x139b)){ |
| reloadFactroyParam(list);delete_not_needed(list);nvcommit(list->nvFile); |
| #ifdef FOTA_AB |
| ret=flags_get(&flags_info);flags_info.boot_fota_flag.fota_status= |
| (0x194+5882-0x188e);ret=flags_set(&flags_info); |
| #endif |
| }}}uint hash(const char*s){uint hash=(0x15b7+3958-0x252d);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]={ |
| (0x1b24+1988-0x22e8)};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[(0x354+1271-0x84b)]=='\n'||buf[(0x198c+702-0x1c4a)] |
| ==((char)(0xbe8+6521-0x253e)))continue;val=strchr(buf, |
| ((char)(0x1a0f+197-0x1a97)));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)-(0xeeb+4656-0x211a)]=='\n' |
| )buf[strlen(buf)-(0xde6+5744-0x2455)]='\0';*val++='\0';nvset(list->nvFile,buf, |
| val,(0x36b+8379-0x2425));}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=(0x26d+9064-0x25d5);int cnt=(0xc80+5494-0x21f6);FILE*fp=NULL; |
| char*buf=NULL;struct stat statbuff={(0x3e2+7581-0x217f)};if(stat(file,&statbuff) |
| <(0x164+6032-0x18f4))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=(0x9ca+1937-0x115b);while(cnt<len){cnt= |
| cnt+fread(buf+cnt,(0x4cf+6569-0x1e77),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[4096];ssize_t nread;int ret=-(0x6d4+5231-0x1b42);fd_from=open(from, |
| O_RDONLY);if(fd_from<(0x8ec+7207-0x2513))return-(0xc1f+1389-0x118a);fd_to=open( |
| to,O_RDWR|O_CREAT|O_TRUNC|O_SYNC,(0xf57+3709-0x1c34));if(fd_to< |
| (0x894+6039-0x202b)){ret=-(0x2f3+6573-0x1c9d);goto out_error;}while( |
| (0x151c+4133-0x2540)){char*out_ptr;ssize_t nwritten;nread=read(fd_from,buf, |
| sizeof(buf));if(nread==(0x1380+3993-0x2319)){break;}else{if(nread< |
| (0x1056+2398-0x19b4)){if(errno==EINTR||errno==EAGAIN){continue;}else{ret=- |
| (0xd8d+1145-0x1202);goto out_error;}}}out_ptr=buf;do{nwritten=write(fd_to, |
| out_ptr,nread);if(nwritten>(0x5da+7443-0x22ed)){nread-=nwritten;out_ptr+= |
| nwritten;}else{if(nwritten<(0x62c+53-0x661)){if(errno==EINTR||errno==EAGAIN){ |
| continue;}else{ret=-(0x263b+198-0x26fc);goto out_error;}}}}while(nread> |
| (0x900+4609-0x1b01));}ret=fsync(fd_to);if(ret<(0xbf1+5402-0x210b)){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)<(0xdb5+4755-0x2048)){fd_to= |
| -(0x96f+4558-0x1b3c);ret=-(0x141b+698-0x16cf);goto out_error;}close(fd_from); |
| return(0x67c+364-0x7e8);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>=(0x95a+2163-0x11cd))close(fd_to);return |
| ret;}static int restoreNvFs(char*dstFile,char*srcFile){if(copyfile(srcFile, |
| dstFile)!=(0xb3c+3238-0x17e2))return RESULT_FAIL;return RESULT_SUCCESS;}static |
| int loadNvFs(char*file){int len=(0xf1a+826-0x1254);int cnt=(0x7d9+2617-0x1212); |
| FILE*fp=NULL;char*buf=NULL;char*name=NULL;char*value=NULL;char*eq=NULL;struct |
| stat statbuff={(0x46f+7480-0x21a7)};char nvFile[NV_PATH_LEN]={ |
| (0x1a38+749-0x1d25)};sprintf(nvFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file) |
| ;if(stat(nvFile,&statbuff)<(0xdd2+1243-0x12ad))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+ |
| (0x154f+2766-0x201c));if(!buf){fclose(fp);return RESULT_MALLOC_FAIL;}memset(buf, |
| (0x71+2225-0x922),len+(0x9c7+138-0xa50));cnt=(0x911+1744-0xfe1);while(cnt<len){ |
| cnt=cnt+fread(buf+cnt,(0x1538+4358-0x263d),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)(0x1445+156-0x14a4))))){break;}*eq='\0';value=eq+ |
| (0x1765+265-0x186d);nvset(file,name,value,(0x120c+4181-0x2260));*eq= |
| ((char)(0x1292+4621-0x2462));name=value+strlen(value)+(0x1555+2491-0x1f0f);}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-(0x1ca+5409-0x16ea));newConfig->configFile[ |
| NV_PATH_LEN-(0x191+8715-0x239b)]='\0';newConfig->next=NULL;}for(list=nv_list; |
| list;list=list->next){if(strcmp(list->nvFile,nvFile)==(0x3b+5302-0x14f1))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-(0x858+5283-0x1cfa));newList->nvFile[ |
| NV_PATH_LEN-(0x1168+478-0x1345)]='\0';memset(newList->nvTable, |
| (0x75d+7380-0x2431),NV_HASH_LEN*(0x10ec+3723-0x1f73));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=(0x5d6+3636-0x140a);char*data=(char*)s; |
| while(len-- >(0x1087+4375-0x219e)){sum+=(*data++);}return sum;}static int |
| saveNvFs(char*nvName,char*nvFile){int i=(0x1e6f+1358-0x23bd);int sum= |
| (0x438+8699-0x2633);int bufSize=(0x1b3d+1381-0x20a2);int itemSize= |
| (0x256+7220-0x1e8a);int ret=(0x1415+1220-0x18d9);int fp=(0x1773+1678-0x1e01); |
| 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,(0x1faa+1162-0x2294));if(fp==-(0x3a4+3977-0x132c)){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=(0xe83+3124-0x1ab7);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)+(0x226+9326-0x2692);if(bufSize+itemSize>NV_BLOCK_SIZE){if(write(fp, |
| buf,bufSize)<(0x643+895-0x9c2)){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=(0x5da+3149-0x1227);}sprintf(buf+bufSize, |
| "\x25\x73\x3d\x25\x73",item->key,item->value);bufSize+=itemSize;}}if(bufSize!= |
| (0xc77+3011-0x183a)){if(write(fp,buf,bufSize)<(0x765+5302-0x1c1b)){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)<(0x797+5024-0x1b37)) |
| {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<(0xc87+384-0xe07)){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= |
| (0x243c+58-0x2476);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-(0xbf2+4930-0x1f33)); |
| value[NV_MAX_VAL_LEN-(0x11c2+152-0x1259)]='\0';return RESULT_SUCCESS;}}return |
| RESULT_NO_ITEM;}static int nvset(char*file,const char*key,const char*value,int |
| saveFlag){int index=(0x6f0+4535-0x18a7);int ret=(0x19cc+2115-0x220f);int |
| key_buf_len=(0x13f5+3997-0x2392);int value_buf_len=(0x921+3524-0x16e5);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)+(0x1109+4431-0x2257);value_buf_len= |
| strlen(value)+(0x11b5+2780-0x1c90);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- |
| (0xa5d+5498-0x1fd6));item->value[value_buf_len-(0x22c+4070-0x1211)]='\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-(0x154b+2284-0x1e36)); |
| newItem->key[key_buf_len-(0x16dd+1345-0x1c1d)]='\0';strncpy(newItem->value,value |
| ,value_buf_len-(0x73+7942-0x1f78));newItem->value[value_buf_len- |
| (0x462+1887-0xbc0)]='\0';newItem->next=NULL;newItem->saveFlag=saveFlag;newItem-> |
| update_flag=(0x12a4+4956-0x2600);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=(0x4ef+4495-0x167e);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=(0x3ac+6714-0x1de6); |
| 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=(0xac6+380-0xc42); |
| 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=(0x655+480-0x835); |
| 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= |
| (0xa20+4851-0x1d13);char nvMainFile[NV_PATH_LEN]={(0x1520+476-0x16fc)};char |
| nvBackupFile[NV_PATH_LEN]={(0x91b+5083-0x1cf6)};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 |
| |