xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * °æÈ¨ËùÓÐ (C)2016, ÖÐÐËͨѶ¹É·ÝÓÐÏÞ¹«Ë¾¡£ |
| 3 | * |
| 4 | * ÎļþÃû³Æ: nvserver.c |
| 5 | * Îļþ±êʶ: nvserver.c |
| 6 | * ÄÚÈÝÕªÒª: nvºǫ́ӦÓÃʵÏÖÎļþ |
| 7 | * |
| 8 | * ÐÞ¸ÄÈÕÆÚ °æ±¾ºÅ Ð޸ıê¼Ç ÐÞ¸ÄÈË ÐÞ¸ÄÄÚÈÝ |
| 9 | * ------------------------------------------------------------------------------ |
| 10 | * 2016/06/13 V1.0 Create ÁõÑÇÄÏ ´´½¨ |
| 11 | * |
| 12 | *******************************************************************************/ |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 13 | |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 14 | /******************************************************************************* |
| 15 | * Í·Îļþ * |
| 16 | *******************************************************************************/ |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 17 | #include <unistd.h> |
| 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <dirent.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/file.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/ipc.h> |
| 27 | #include <sys/msg.h> |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 28 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 29 | #include "nvserver.h" |
| 30 | #include "nv_typedef.h" |
| 31 | #include <message.h> |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 32 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 33 | #ifdef FOTA_AB |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 34 | #include "pub_flags.h" |
| 35 | #include "flags_api.h" |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 36 | #endif |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 37 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 38 | #ifdef __cplusplus |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 39 | extern "C" { |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 40 | #endif |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 41 | |
| 42 | /******************************************************************************* |
| 43 | * ³£Á¿¶¨Òå * |
| 44 | *******************************************************************************/ |
| 45 | |
| 46 | /******************************************************************************* |
| 47 | * ºê¶¨Òå * |
| 48 | *******************************************************************************/ |
| 49 | |
| 50 | /******************************************************************************* |
| 51 | * Êý¾ÝÀàÐͶ¨Òå * |
| 52 | *******************************************************************************/ |
| 53 | |
| 54 | /******************************************************************************* |
| 55 | * ¾Ö²¿º¯ÊýÉùÃ÷ * |
| 56 | *******************************************************************************/ |
| 57 | static void nvConfig(); |
| 58 | static void nvInit(); |
| 59 | static int nvDirInit(); |
| 60 | static void analyMsg(T_NV_MSG_INFO *msgrecv, T_NV_MSG_RESULT *msgsnd); |
| 61 | static bool checkNvFs(char *file); |
| 62 | static bool isCfgConfiged(char *configFile); |
| 63 | static bool isNvConfiged(char *configFile); |
| 64 | static uint getSum(const char *s, int len); |
| 65 | static int loadFactroyParam(T_NV_NODE *list); |
| 66 | static int restoreNvFs(char *dstFile, char *srcFile); |
| 67 | static int loadNvFs(char *file); |
| 68 | static int addConfigFile(char *nvFile, char *configFile); |
| 69 | static int saveNvFs(char *nvName, char *nvFile); |
| 70 | static int nvset(char *file, const char *key, const char *value, int saveFlag); |
| 71 | static int nvget(char *file, char *key, char *value); |
| 72 | static int nvunset(char *file, char *key); |
| 73 | static int nvclear(char *file); |
| 74 | static int nvreset(char *file); |
| 75 | static int nvcommit(char *file); |
| 76 | |
| 77 | /******************************************************************************* |
| 78 | * ¾Ö²¿¾²Ì¬±äÁ¿¶¨Òå * |
| 79 | *******************************************************************************/ |
| 80 | |
| 81 | /******************************************************************************* |
| 82 | * È«¾Ö±äÁ¿¶¨Òå * |
| 83 | *******************************************************************************/ |
| 84 | T_NV_NODE *nv_list; |
| 85 | |
| 86 | /******************************************************************************* |
| 87 | * ¾Ö²¿º¯ÊýʵÏÖ * |
| 88 | *******************************************************************************/ |
| 89 | |
| 90 | /******************************************************************************* |
| 91 | * È«¾Öº¯ÊýʵÏÖ * |
| 92 | *******************************************************************************/ |
| 93 | |
| 94 | /******************************************************************************* |
| 95 | * ¹¦ÄÜÃèÊö: main |
| 96 | * ²ÎÊý˵Ã÷: |
| 97 | * (´«Èë²ÎÊý) void |
| 98 | * (´«³ö²ÎÊý) void |
| 99 | * ·µ »Ø Öµ: void |
| 100 | * ÆäËü˵Ã÷: void |
| 101 | *******************************************************************************/ |
| 102 | int nvserver_main(int argc, char *argv[]) |
| 103 | { |
| 104 | int msgId = 0; |
| 105 | T_NV_MSG_INFO rcvBuf; |
| 106 | T_NV_MSG_RESULT sndBuf; |
| 107 | struct msqid_ds msgInfo; |
| 108 | |
| 109 | prctl(PR_SET_NAME, "nvserver", 0, 0, 0); |
| 110 | |
| 111 | memset( &rcvBuf, 0, sizeof(rcvBuf)); |
| 112 | memset( &sndBuf, 0, sizeof(sndBuf)); |
| 113 | memset( &msgInfo, 0, sizeof(msgInfo)); |
| 114 | |
| 115 | msgId = msgget(MODULE_ID_NV, IPC_CREAT | 0600); |
| 116 | if(-1 == msgId) |
| 117 | { |
| 118 | printf("nvserver error: msgget msgId fail, errno = %d\n", errno); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | if(-1 != msgctl(msgId, IPC_STAT, &msgInfo)) |
| 123 | { |
| 124 | msgInfo.msg_qbytes = 262144; // 256k |
| 125 | if(-1 == msgctl(msgId, IPC_SET, &msgInfo)) |
| 126 | printf("nvserver error: msgctl msgId fail, errno = %d\n", errno); |
| 127 | } |
| 128 | |
| 129 | if(nvDirInit() != 0) |
| 130 | { |
| 131 | printf("nvDirInit faile!\n"); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | nvConfig(); |
| 136 | |
| 137 | nvInit(); |
| 138 | |
| 139 | // ÏûÏ¢½»»¥ |
| 140 | while(1) |
| 141 | { |
| 142 | if(-1 == msgrcv(msgId, &rcvBuf, sizeof(T_NV_MSG_INFO) - sizeof(long), MSG_TYPE_NV, 0)) |
| 143 | { |
| 144 | printf("nvserver error: nvserver msgrcv fail, errno = %d!\n", errno); |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | analyMsg(&rcvBuf, &sndBuf); |
xf.li | abe372f | 2025-05-13 02:44:32 -0700 | [diff] [blame^] | 149 | msgsnd_retry: |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 150 | if(-1 == msgsnd(msgId, &sndBuf, sizeof(T_NV_MSG_RESULT) - sizeof(long), 0)) |
| 151 | { |
xf.li | abe372f | 2025-05-13 02:44:32 -0700 | [diff] [blame^] | 152 | if (errno == EINTR) |
| 153 | { |
| 154 | printf("nvserver error: msgsnd fail, errno = EINTR!\n"); |
| 155 | goto msgsnd_retry; |
| 156 | } |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 157 | printf("nvserver error: nvserver msgsnd fail, errno = %d!\n", errno); |
| 158 | continue; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return (0); |
| 163 | } |
| 164 | |
| 165 | /******************************************************************************* |
| 166 | * ¹¦ÄÜÃèÊö: configdir |
| 167 | * ²ÎÊý˵Ã÷: |
| 168 | * (´«Èë²ÎÊý) dir |
| 169 | * (´«³ö²ÎÊý) void |
| 170 | * ·µ »Ø Öµ: void |
| 171 | * ÆäËü˵Ã÷: void |
| 172 | *******************************************************************************/ |
| 173 | static void configdir(char *dir) |
| 174 | { |
| 175 | DIR *dp; |
| 176 | int ret; |
| 177 | |
| 178 | struct dirent *entry; |
| 179 | struct stat statbuf; |
| 180 | if((dp = opendir(dir)) == NULL) |
| 181 | { |
| 182 | fprintf(stderr,"cannot open directory: %s\n", dir); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | chdir(dir); |
| 187 | while((entry = readdir(dp)) != NULL) |
| 188 | { |
| 189 | ret = lstat(entry->d_name,&statbuf); |
| 190 | if (ret < 0) |
| 191 | { |
| 192 | fprintf(stderr,"lstat error: %s\n", strerror(errno)); |
| 193 | chdir(".."); |
| 194 | closedir(dp); |
| 195 | return; |
| 196 | } |
| 197 | if(!S_ISDIR(statbuf.st_mode)) |
| 198 | { |
| 199 | |
| 200 | if(strcmp(".",entry->d_name) == 0 ||strcmp("..",entry->d_name) == 0) |
| 201 | continue; |
| 202 | |
| 203 | if(!isNvConfiged(entry->d_name)) |
| 204 | { |
| 205 | if(addConfigFile(entry->d_name, NULL) != RESULT_SUCCESS) |
| 206 | printf("nvserver error:config %s error!\n", entry->d_name); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | chdir(".."); |
| 211 | closedir(dp); |
| 212 | } |
| 213 | |
| 214 | /******************************************************************************* |
| 215 | * ¹¦ÄÜÃèÊö: nvConfig |
| 216 | * ²ÎÊý˵Ã÷: |
| 217 | * (´«Èë²ÎÊý) void |
| 218 | * (´«³ö²ÎÊý) void |
| 219 | * ·µ »Ø Öµ: void |
| 220 | * ÆäËü˵Ã÷: void |
| 221 | *******************************************************************************/ |
| 222 | static void nvConfig() |
| 223 | { |
| 224 | char *val = NULL; |
| 225 | FILE *fp = NULL; |
| 226 | char buf[NV_MAX_CONFIG_LEN] = {0}; |
| 227 | |
| 228 | fp = fopen(NV_CONFIG_FILE, "ro"); |
| 229 | if(!fp) |
| 230 | { |
| 231 | printf("nvserver error:open %s file fail errno = %d!\n", NV_CONFIG_FILE, errno); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | while (fgets(buf, NV_MAX_CONFIG_LEN, fp)) |
| 236 | { |
| 237 | if (buf[0] == '\n' || buf[0] == '#') |
| 238 | continue; |
| 239 | |
| 240 | val = strchr(buf, '='); |
| 241 | if (!val) |
| 242 | { |
| 243 | printf("nvserver error:%s file format error: str = %s!\n", NV_CONFIG_FILE, buf); |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | // buf=nvconfig val=nvfile |
| 248 | buf[strlen(buf) - 1] = '\0'; |
| 249 | *val++ = '\0'; |
| 250 | |
| 251 | if(!isCfgConfiged(buf)) |
| 252 | { |
| 253 | if(addConfigFile(val, buf) != RESULT_SUCCESS) |
| 254 | printf("nvserver error:config %s error!\n", buf); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | fclose(fp); |
| 259 | |
| 260 | configdir(NV_FS_MAIN_PATH); |
| 261 | } |
| 262 | |
| 263 | /******************************************************************************* |
| 264 | * ¹¦ÄÜÃèÊö: nvDirInit |
| 265 | * ²ÎÊý˵Ã÷: |
| 266 | * (´«Èë²ÎÊý) void |
| 267 | * (´«³ö²ÎÊý) void |
| 268 | * ·µ »Ø Öµ: void |
| 269 | * ÆäËü˵Ã÷: void |
| 270 | *******************************************************************************/ |
| 271 | static int nvDirInit() |
| 272 | { |
| 273 | if(access(NV_FS_PATH, F_OK) != 0) |
| 274 | { |
| 275 | if(mkdir(NV_FS_PATH, 0755) != 0) |
| 276 | { |
| 277 | printf("nerver mkdir %s fali,errno=%d\n", NV_FS_PATH, errno); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | if(mkdir(NV_FS_MAIN_PATH, 0755) != 0) |
| 282 | { |
| 283 | printf("nerver mkdir %s fali,errno=%d\n", NV_FS_MAIN_PATH, errno); |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | if(mkdir(NV_FS_BACKUP_PATH, 0755) != 0) |
| 288 | { |
| 289 | printf("nerver mkdir %s fali,errno=%d\n", NV_FS_BACKUP_PATH, errno); |
| 290 | return -1; |
| 291 | } |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | if(access(NV_FS_MAIN_PATH, F_OK) != 0) |
| 296 | { |
| 297 | if(mkdir(NV_FS_MAIN_PATH, 0755) != 0) |
| 298 | { |
| 299 | printf("nerver mkdir %s fali,errno=%d\n", NV_FS_MAIN_PATH, errno); |
| 300 | return -1; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if(access(NV_FS_BACKUP_PATH, F_OK) != 0) |
| 305 | { |
| 306 | if(mkdir(NV_FS_BACKUP_PATH, 0755) != 0) |
| 307 | { |
| 308 | printf("nerver mkdir %s fali,errno=%d\n", NV_FS_BACKUP_PATH, errno); |
| 309 | return -1; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /******************************************************************************* |
| 319 | * ¹¦ÄÜÃèÊö: nvInit |
| 320 | * ²ÎÊý˵Ã÷: |
| 321 | * (´«Èë²ÎÊý) void |
| 322 | * (´«³ö²ÎÊý) void |
| 323 | * ·µ »Ø Öµ: void |
| 324 | * ÆäËü˵Ã÷: void |
| 325 | *******************************************************************************/ |
| 326 | static void nvInit() |
| 327 | { |
| 328 | T_NV_NODE *list = NULL; |
| 329 | char nvMainFile[NV_PATH_LEN] = {0}; |
| 330 | char nvBackupFile[NV_PATH_LEN] = {0}; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 331 | #ifdef FOTA_AB |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 332 | T_FLAGS_INFO flags_info = {0}; |
| 333 | int ret = 0; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 334 | #endif |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 335 | |
| 336 | for(list = nv_list; list; list = list->next) |
| 337 | { |
| 338 | snprintf(nvMainFile, NV_PATH_LEN, "%s/%s", NV_FS_MAIN_PATH, list->nvFile); |
| 339 | snprintf(nvBackupFile, NV_PATH_LEN, "%s/%s", NV_FS_BACKUP_PATH, list->nvFile); |
| 340 | |
| 341 | if(checkNvFs(nvMainFile)) |
| 342 | { |
| 343 | if(!checkNvFs(nvBackupFile)) |
| 344 | restoreNvFs(nvBackupFile, nvMainFile); |
| 345 | } |
| 346 | else if(checkNvFs(nvBackupFile)) |
| 347 | { |
| 348 | restoreNvFs(nvMainFile, nvBackupFile); |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | loadFactroyParam(list); |
| 353 | nvcommit(list->nvFile); |
| 354 | continue; |
| 355 | } |
| 356 | |
| 357 | loadNvFs(list->nvFile); |
| 358 | if(!strcmp(list->nvFile, NV_CFG) && get_update_status() == 2 ){ |
| 359 | reloadFactroyParam(list); |
| 360 | delete_not_needed(list); |
| 361 | nvcommit(list->nvFile); |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 362 | #ifdef FOTA_AB |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 363 | ret = flags_get(&flags_info); |
| 364 | flags_info.boot_fota_flag.fota_status = 0; |
| 365 | ret = flags_set(&flags_info); |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 366 | #endif |
xf.li | a7d2a2b | 2025-02-10 01:44:22 -0800 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /******************************************************************************* |
| 372 | * ¹¦ÄÜÃèÊö: hash |
| 373 | * ²ÎÊý˵Ã÷: |
| 374 | * (´«Èë²ÎÊý) void |
| 375 | * (´«³ö²ÎÊý) void |
| 376 | * ·µ »Ø Öµ: hash value |
| 377 | * ÆäËü˵Ã÷: void |
| 378 | *******************************************************************************/ |
| 379 | uint hash(const char *s) |
| 380 | { |
| 381 | uint hash = 0; |
| 382 | |
| 383 | while (*s) |
| 384 | { |
| 385 | hash = NV_HASH_MUL * hash + *s++; |
| 386 | } |
| 387 | |
| 388 | return hash; |
| 389 | } |
| 390 | |
| 391 | /******************************************************************************* |
| 392 | * ¹¦ÄÜÃèÊö: loadFactroyParam |
| 393 | * ²ÎÊý˵Ã÷: |
| 394 | * (´«Èë²ÎÊý) list |
| 395 | * (´«³ö²ÎÊý) void |
| 396 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ -1±íʾʧ°Ü |
| 397 | * ÆäËü˵Ã÷: void |
| 398 | *******************************************************************************/ |
| 399 | static int loadFactroyParam(T_NV_NODE *list) |
| 400 | { |
| 401 | char *val = NULL; |
| 402 | FILE *fp = NULL; |
| 403 | T_NV_CONFIG *config = NULL; |
| 404 | char buf[NV_MAX_ITEM_LEN] = {0}; |
| 405 | |
| 406 | for(config = list->fileList; config; config = config->next) |
| 407 | { |
| 408 | fp = fopen(config->configFile, "ro"); |
| 409 | if(!fp) |
| 410 | { |
| 411 | printf("nvserver error:open %s file fail errno = %d!\n", config->configFile, errno); |
| 412 | return RESULT_FILE_OPEN_FAIL; |
| 413 | } |
| 414 | |
| 415 | while (fgets(buf, NV_MAX_ITEM_LEN, fp)) |
| 416 | { |
| 417 | if (buf[0] == '\n' || buf[0] == '#') |
| 418 | continue; |
| 419 | |
| 420 | val = strchr(buf, '='); |
| 421 | if (!val) |
| 422 | { |
| 423 | printf("nvserver error:%s file format error:string = %s\n", config->configFile, buf); |
| 424 | continue; |
| 425 | } |
| 426 | |
| 427 | /*strip line break,the last line may not have a line break*/ |
| 428 | if (buf[strlen(buf) - 1] == '\n') |
| 429 | buf[strlen(buf) - 1] = '\0'; |
| 430 | |
| 431 | *val++ = '\0'; |
| 432 | |
| 433 | nvset(list->nvFile, buf, val, 1); |
| 434 | } |
| 435 | printf("nvserver loadFactroyParam %s!\n", config->configFile); |
| 436 | fclose(fp); |
| 437 | } |
| 438 | |
| 439 | return RESULT_SUCCESS; |
| 440 | } |
| 441 | |
| 442 | /******************************************************************************* |
| 443 | * ¹¦ÄÜÃèÊö: checkNvFs |
| 444 | * ²ÎÊý˵Ã÷: |
| 445 | * (´«Èë²ÎÊý) void |
| 446 | * (´«³ö²ÎÊý) void |
| 447 | * ·µ »Ø Öµ: bool |
| 448 | * ÆäËü˵Ã÷: void |
| 449 | *******************************************************************************/ |
| 450 | static bool checkNvFs(char *file) |
| 451 | { |
| 452 | int len = 0; |
| 453 | int cnt = 0; |
| 454 | FILE * fp = NULL; |
| 455 | char * buf = NULL; |
| 456 | struct stat statbuff = {0}; |
| 457 | |
| 458 | if(stat(file, &statbuff) < 0) |
| 459 | return false; |
| 460 | |
| 461 | len = statbuff.st_size; |
| 462 | if(len < NV_CHECK_SIZE) |
| 463 | return false; |
| 464 | |
| 465 | fp = fopen(file, "ro"); |
| 466 | if (!fp) |
| 467 | return false; |
| 468 | |
| 469 | buf = (char *)malloc(len); |
| 470 | if(!buf) |
| 471 | { |
| 472 | fclose(fp); |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | cnt = 0; |
| 477 | while (cnt < len) |
| 478 | { |
| 479 | cnt = cnt + fread(buf + cnt, 1, len - cnt, fp); |
| 480 | if (ferror(fp)) |
| 481 | { |
| 482 | clearerr(fp); |
| 483 | free(buf); |
| 484 | fclose(fp); |
| 485 | return false; |
| 486 | } |
| 487 | } |
| 488 | if (len != cnt) |
| 489 | { |
| 490 | free(buf); |
| 491 | fclose(fp); |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | if(getSum(buf, len - NV_CHECK_SIZE) + NV_FILE_FLAG != *(uint *)(buf + len - NV_CHECK_SIZE)) |
| 496 | { |
| 497 | free(buf); |
| 498 | fclose(fp); |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | free(buf); |
| 503 | fclose(fp); |
| 504 | |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | /******************************************************************************* |
| 509 | * ¹¦ÄÜÃèÊö: copyfile |
| 510 | * ²ÎÊý˵Ã÷: |
| 511 | * (´«Èë²ÎÊý) to:Ä¿±êÎļþ |
| 512 | * (´«Èë²ÎÊý) from:Ô´Îļþ |
| 513 | * ·µ »Ø Öµ: 0±íʾ³É¹¦,¸ºÖµÊ§°Ü |
| 514 | * ÆäËü˵Ã÷: |
| 515 | *******************************************************************************/ |
| 516 | static int copyfile(const char *from, const char *to) |
| 517 | { |
| 518 | int fd_to; |
| 519 | int fd_from; |
| 520 | char buf[4096]; |
| 521 | ssize_t nread; |
| 522 | int ret = -1; |
| 523 | |
| 524 | fd_from = open(from, O_RDONLY); |
| 525 | if (fd_from < 0) |
| 526 | return -2; |
| 527 | |
| 528 | fd_to = open(to, O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0640); |
| 529 | if (fd_to < 0) { |
| 530 | ret = -3; |
| 531 | goto out_error; |
| 532 | } |
| 533 | |
| 534 | while (1) |
| 535 | { |
| 536 | char *out_ptr; |
| 537 | ssize_t nwritten; |
| 538 | |
| 539 | nread = read(fd_from, buf, sizeof(buf)); |
| 540 | if (nread == 0) |
| 541 | { |
| 542 | break; /* read file done*/ |
| 543 | } |
| 544 | else |
| 545 | { |
| 546 | if (nread < 0 ) |
| 547 | { |
| 548 | if (errno == EINTR || errno == EAGAIN) |
| 549 | { |
| 550 | continue; |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | ret = -4; |
| 555 | goto out_error; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | out_ptr = buf; |
| 561 | do |
| 562 | { |
| 563 | nwritten = write(fd_to, out_ptr, nread); |
| 564 | if (nwritten > 0) |
| 565 | { |
| 566 | nread -= nwritten; |
| 567 | out_ptr += nwritten; |
| 568 | } |
| 569 | else |
| 570 | { |
| 571 | if (nwritten < 0) |
| 572 | { |
| 573 | if (errno == EINTR || errno == EAGAIN) |
| 574 | { |
| 575 | continue; |
| 576 | } |
| 577 | else |
| 578 | { |
| 579 | ret = -5; |
| 580 | goto out_error; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | } while (nread > 0); |
| 585 | } |
| 586 | |
| 587 | ret = fsync(fd_to); |
| 588 | if (ret < 0) { |
| 589 | printf("Sync Failed:%s, file path:%s", strerror(errno), to); |
| 590 | goto out_error; |
| 591 | } |
| 592 | |
| 593 | if (close(fd_to) < 0) |
| 594 | { |
| 595 | fd_to = -1; |
| 596 | ret = -6; |
| 597 | goto out_error; |
| 598 | } |
| 599 | close(fd_from); |
| 600 | |
| 601 | /* Success! */ |
| 602 | return 0; |
| 603 | |
| 604 | out_error: |
| 605 | printf("copyfile %s to %s error:%d\n", from, to, ret); |
| 606 | close(fd_from); |
| 607 | if (fd_to >= 0) |
| 608 | close(fd_to); |
| 609 | |
| 610 | return ret; |
| 611 | } |
| 612 | |
| 613 | /******************************************************************************* |
| 614 | * ¹¦ÄÜÃèÊö: restoreNvFs |
| 615 | * ²ÎÊý˵Ã÷: |
| 616 | * (´«Èë²ÎÊý) dstFile:Ä¿±êÎļþ |
| 617 | * (´«Èë²ÎÊý) srcFile:Ô´Îļþ |
| 618 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 619 | * ÆäËü˵Ã÷: void |
| 620 | *******************************************************************************/ |
| 621 | static int restoreNvFs(char *dstFile, char *srcFile) |
| 622 | { |
| 623 | if (copyfile(srcFile, dstFile) != 0) |
| 624 | return RESULT_FAIL; |
| 625 | |
| 626 | return RESULT_SUCCESS; |
| 627 | } |
| 628 | |
| 629 | /******************************************************************************* |
| 630 | * ¹¦ÄÜÃèÊö: loadNvFs |
| 631 | * ²ÎÊý˵Ã÷: |
| 632 | * (´«Èë²ÎÊý) file:nvÎļþ |
| 633 | * (´«³ö²ÎÊý) void |
| 634 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 635 | * ÆäËü˵Ã÷: void |
| 636 | *******************************************************************************/ |
| 637 | static int loadNvFs(char *file) |
| 638 | { |
| 639 | int len = 0; |
| 640 | int cnt = 0; |
| 641 | FILE *fp = NULL; |
| 642 | char *buf = NULL; |
| 643 | char *name = NULL; |
| 644 | char *value = NULL; |
| 645 | char *eq = NULL; |
| 646 | |
| 647 | struct stat statbuff = {0}; |
| 648 | char nvFile[NV_PATH_LEN] = {0}; |
| 649 | |
| 650 | sprintf(nvFile, "%s/%s", NV_FS_MAIN_PATH, file); |
| 651 | |
| 652 | if(stat(nvFile, &statbuff) < 0) |
| 653 | return RESULT_FAIL; |
| 654 | |
| 655 | len = statbuff.st_size; |
| 656 | if(NV_CHECK_SIZE > len) |
| 657 | return RESULT_FAIL; |
| 658 | |
| 659 | fp = fopen(nvFile, "ro"); |
| 660 | if (!fp) |
| 661 | return RESULT_FILE_OPEN_FAIL; |
| 662 | |
| 663 | len = len - NV_CHECK_SIZE; |
| 664 | buf = (char *)malloc(len + 1); |
| 665 | if(!buf) |
| 666 | { |
| 667 | fclose(fp); |
| 668 | return RESULT_MALLOC_FAIL; |
| 669 | } |
| 670 | memset(buf, 0, len + 1); |
| 671 | |
| 672 | cnt = 0; |
| 673 | while (cnt < len) |
| 674 | { |
| 675 | cnt = cnt + fread(buf + cnt, 1, len - cnt, fp); |
| 676 | if (ferror(fp)) |
| 677 | { |
| 678 | clearerr(fp); |
| 679 | fclose(fp); |
| 680 | free(buf); |
| 681 | return RESULT_FILE_READ_FAIL; |
| 682 | } |
| 683 | } |
| 684 | if (cnt != len) |
| 685 | { |
| 686 | fclose(fp); |
| 687 | free(buf); |
| 688 | return RESULT_FILE_READ_FAIL; |
| 689 | } |
| 690 | |
| 691 | buf[len] = '\0'; /* fix warning for coverity */ |
| 692 | name = buf; |
| 693 | while(*name) |
| 694 | { |
| 695 | if (!(eq = strchr(name, '='))) |
| 696 | { |
| 697 | break; |
| 698 | } |
| 699 | *eq = '\0'; |
| 700 | value = eq + 1; |
| 701 | |
| 702 | nvset(file, name, value, 1); |
| 703 | |
| 704 | *eq = '='; |
| 705 | name = value + strlen(value) + 1; |
| 706 | } |
| 707 | |
| 708 | free(buf); |
| 709 | fclose(fp); |
| 710 | |
| 711 | return RESULT_SUCCESS; |
| 712 | } |
| 713 | |
| 714 | /******************************************************************************* |
| 715 | * ¹¦ÄÜÃèÊö: analyMsg |
| 716 | * ²ÎÊý˵Ã÷: |
| 717 | * (´«Èë²ÎÊý) msgrecv:½ÓÊÕÏûÏ¢ |
| 718 | * (´«³ö²ÎÊý) msgsnd:·¢ËÍÏûÏ¢ |
| 719 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 720 | * ÆäËü˵Ã÷: void |
| 721 | *******************************************************************************/ |
| 722 | static void analyMsg(T_NV_MSG_INFO *msgrecv, T_NV_MSG_RESULT *msgsnd) |
| 723 | { |
| 724 | switch(msgrecv->nvType) |
| 725 | { |
| 726 | case MSG_GET: |
| 727 | msgsnd->result = nvget(msgrecv->file, msgrecv->key, msgsnd->value); |
| 728 | break; |
| 729 | case MSG_SET: |
| 730 | msgsnd->result = nvset(msgrecv->file, msgrecv->key, msgrecv->value, msgrecv->saveflag); |
| 731 | break; |
| 732 | case MSG_UNSET: |
| 733 | msgsnd->result = nvunset(msgrecv->file, msgrecv->key); |
| 734 | break; |
| 735 | case MSG_CLEAR: |
| 736 | msgsnd->result = nvclear(msgrecv->file); |
| 737 | break; |
| 738 | case MSG_RESET: |
| 739 | msgsnd->result = nvreset(msgrecv->file); |
| 740 | break; |
| 741 | case MSG_SHOW: |
| 742 | msgsnd->result = saveNvFs(msgrecv->file, NV_FS_SHOW);; |
| 743 | break; |
| 744 | case MSG_COMMIT: |
| 745 | msgsnd->result = nvcommit(msgrecv->file); |
| 746 | break; |
| 747 | default: |
| 748 | break; |
| 749 | } |
| 750 | |
| 751 | msgsnd->msgType = msgrecv->pid; |
| 752 | msgsnd->msgIndex = msgrecv->msgIndex; |
| 753 | } |
| 754 | |
| 755 | /******************************************************************************* |
| 756 | * ¹¦ÄÜÃèÊö: addConfigFile |
| 757 | * ²ÎÊý˵Ã÷: |
| 758 | * (´«Èë²ÎÊý) nvFile:nvÎļþ |
| 759 | * (´«Èë²ÎÊý) configFile:ÅäÖÃÎļþ |
| 760 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 761 | * ÆäËü˵Ã÷: void |
| 762 | *******************************************************************************/ |
| 763 | static int addConfigFile(char *nvFile, char *configFile) |
| 764 | { |
| 765 | T_NV_NODE *list = NULL; |
| 766 | T_NV_NODE *newList = NULL; |
| 767 | T_NV_CONFIG *newConfig = NULL; |
| 768 | |
| 769 | if(!nvFile) |
| 770 | { |
| 771 | printf("nvserver error: param illegal!\n"); |
| 772 | return RESULT_INVAL; |
| 773 | } |
| 774 | |
| 775 | if(configFile)// nv has no config file |
| 776 | { |
| 777 | newConfig = (T_NV_CONFIG * )malloc(sizeof(T_NV_CONFIG)); |
| 778 | if(!newConfig) |
| 779 | return RESULT_MALLOC_FAIL; |
| 780 | |
| 781 | strncpy(newConfig->configFile, configFile, NV_PATH_LEN - 1); |
| 782 | newConfig->configFile[NV_PATH_LEN - 1] = '\0'; |
| 783 | newConfig->next = NULL; |
| 784 | } |
| 785 | |
| 786 | for(list = nv_list; list; list =list->next) |
| 787 | { |
| 788 | if(strcmp(list->nvFile, nvFile) == 0) |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | if(!list) //can't fine the nv |
| 793 | { |
| 794 | newList = (T_NV_NODE *)malloc(sizeof(T_NV_NODE)); |
| 795 | if(!newList) |
| 796 | { |
| 797 | if(newConfig) |
| 798 | free(newConfig); |
| 799 | return RESULT_MALLOC_FAIL; |
| 800 | } |
| 801 | |
| 802 | newList->next = NULL; |
| 803 | strncpy(newList->nvFile, nvFile, NV_PATH_LEN - 1); |
| 804 | newList->nvFile[NV_PATH_LEN - 1] = '\0'; |
| 805 | memset(newList->nvTable, 0, NV_HASH_LEN * 4); |
| 806 | |
| 807 | newList->fileList = newConfig; |
| 808 | |
| 809 | if(!nv_list) |
| 810 | nv_list = newList; |
| 811 | else |
| 812 | { |
| 813 | newList->next = nv_list->next; |
| 814 | nv_list->next = newList; |
| 815 | } |
| 816 | } |
| 817 | else if(!list->fileList) //can't fine the filelist |
| 818 | list->fileList = newConfig; |
| 819 | else //can't fine the file |
| 820 | { |
| 821 | if(newConfig == NULL) |
| 822 | return RESULT_FAIL; |
| 823 | newConfig->next = list->fileList->next; |
| 824 | list->fileList->next = newConfig; |
| 825 | } |
| 826 | |
| 827 | return RESULT_SUCCESS; |
| 828 | } |
| 829 | |
| 830 | /******************************************************************************* |
| 831 | * ¹¦ÄÜÃèÊö: isConfiged |
| 832 | * ²ÎÊý˵Ã÷: |
| 833 | * (´«Èë²ÎÊý) configFile:ÅäÖÃÎļþÃû |
| 834 | * ·µ »Ø Öµ: void |
| 835 | * ÆäËü˵Ã÷: void |
| 836 | *******************************************************************************/ |
| 837 | static bool isCfgConfiged(char *configFile) |
| 838 | { |
| 839 | T_NV_NODE *list = NULL; |
| 840 | T_NV_CONFIG *config = NULL; |
| 841 | |
| 842 | for(list = nv_list; list; list = list->next) |
| 843 | { |
| 844 | for(config = list->fileList; config; config = config->next) |
| 845 | { |
| 846 | if(!strcmp(config->configFile, configFile)) |
| 847 | return true; |
| 848 | } |
| 849 | } |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | /******************************************************************************* |
| 854 | * ¹¦ÄÜÃèÊö: isNvConfiged |
| 855 | * ²ÎÊý˵Ã÷: |
| 856 | * (´«Èë²ÎÊý) configFile:ÅäÖÃÎļþÃû |
| 857 | * ·µ »Ø Öµ: void |
| 858 | * ÆäËü˵Ã÷: void |
| 859 | *******************************************************************************/ |
| 860 | static bool isNvConfiged(char *nvFile) |
| 861 | { |
| 862 | T_NV_NODE *list = NULL; |
| 863 | |
| 864 | for(list = nv_list; list; list = list->next) |
| 865 | { |
| 866 | if(!strcmp(list->nvFile, nvFile)) |
| 867 | return true; |
| 868 | } |
| 869 | return false; |
| 870 | } |
| 871 | |
| 872 | /******************************************************************************* |
| 873 | * ¹¦ÄÜÃèÊö: getSum |
| 874 | * ²ÎÊý˵Ã÷: |
| 875 | * (´«Èë²ÎÊý) s:×Ö·û´®»º³å |
| 876 | * ·µ »Ø Öµ: sumÊýÖµ |
| 877 | * ÆäËü˵Ã÷: void |
| 878 | *******************************************************************************/ |
| 879 | static uint getSum(const char *s, int len) |
| 880 | { |
| 881 | uint sum = 0; |
| 882 | char *data = (char*)s; |
| 883 | |
| 884 | while (len-- > 0) |
| 885 | { |
| 886 | sum += (*data++); |
| 887 | } |
| 888 | |
| 889 | return sum; |
| 890 | } |
| 891 | |
| 892 | /******************************************************************************* |
| 893 | * ¹¦ÄÜÃèÊö: saveNvFs |
| 894 | * ²ÎÊý˵Ã÷: |
| 895 | * (´«Èë²ÎÊý) nvName:nvÎļþÃû |
| 896 | * (´«Èë²ÎÊý) nvFile:nv±£´æÎļþ |
| 897 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 898 | * ÆäËü˵Ã÷: void |
| 899 | *******************************************************************************/ |
| 900 | static int saveNvFs(char *nvName, char *nvFile) |
| 901 | { |
| 902 | int i = 0; |
| 903 | int sum = 0; |
| 904 | int bufSize = 0; |
| 905 | int itemSize = 0; |
| 906 | int ret = 0; |
| 907 | int fp = 0; |
| 908 | char *buf = NULL; |
| 909 | T_NV_NODE * list = NULL; |
| 910 | T_NV_ITEM * item = NULL; |
| 911 | |
| 912 | for(list = nv_list; list; list = list->next) |
| 913 | { |
| 914 | if(strcmp(list->nvFile, nvName)) |
| 915 | continue; |
| 916 | |
| 917 | fp= open(nvFile, O_SYNC | O_RDWR | O_CREAT | O_TRUNC, 0640); |
| 918 | if(fp == -1) |
| 919 | { |
| 920 | printf("open %s fail,errno = %d\n", nvFile, errno); |
| 921 | return RESULT_FILE_OPEN_FAIL; |
| 922 | } |
| 923 | |
| 924 | buf = (char *)malloc(NV_BLOCK_SIZE); |
| 925 | if(!buf) |
| 926 | { |
| 927 | close(fp); |
| 928 | return RESULT_MALLOC_FAIL; |
| 929 | } |
| 930 | |
| 931 | for(i = 0; i < NV_HASH_LEN; i ++) |
| 932 | { |
| 933 | for(item = list->nvTable[i]; item; item = item->next) |
| 934 | { |
| 935 | if(strcmp(nvFile, NV_FS_SHOW) && !item->saveFlag) |
| 936 | continue; |
| 937 | |
| 938 | itemSize = strlen(item->key) + strlen(item->value) + 2; |
| 939 | if(bufSize + itemSize > NV_BLOCK_SIZE) |
| 940 | { |
| 941 | if(write(fp, buf, bufSize) < 0) |
| 942 | { |
| 943 | printf("error %s %d: write fail errno = %d\n", __FILE__, __LINE__, errno); |
| 944 | close(fp); |
| 945 | free(buf); |
| 946 | return RESULT_FILE_WRITE_FAIL; |
| 947 | } |
| 948 | sum += getSum(buf, bufSize); |
| 949 | bufSize = 0; |
| 950 | } |
| 951 | |
| 952 | sprintf(buf + bufSize, "%s=%s", item->key, item->value); |
| 953 | bufSize += itemSize; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | // last block |
| 958 | if(bufSize != 0) |
| 959 | { |
| 960 | if(write(fp, buf, bufSize) < 0) |
| 961 | { |
| 962 | printf("nvserver error: write fail errno = %d\n", errno); |
| 963 | close(fp); |
| 964 | free(buf); |
| 965 | return RESULT_FILE_WRITE_FAIL; |
| 966 | } |
| 967 | sum += getSum(buf, bufSize); |
| 968 | } |
| 969 | sum += NV_FILE_FLAG; |
| 970 | |
| 971 | // write sum |
| 972 | if(write(fp, &sum, NV_CHECK_SIZE) < 0) |
| 973 | { |
| 974 | printf("nvserver error: write fail errno = %d\n", errno); |
| 975 | close(fp); |
| 976 | free(buf); |
| 977 | return RESULT_FILE_WRITE_FAIL; |
| 978 | } |
| 979 | |
| 980 | ret = fsync(fp); |
| 981 | |
| 982 | free(buf); |
| 983 | close(fp); |
| 984 | |
| 985 | if (ret < 0) { |
| 986 | printf("Sync Failed:%s, file path:%s", strerror(errno), nvFile); |
| 987 | return ret; |
| 988 | } |
| 989 | |
| 990 | return RESULT_SUCCESS; |
| 991 | } |
| 992 | |
| 993 | return RESULT_NO_FILE; |
| 994 | } |
| 995 | |
| 996 | |
| 997 | /******************************************************************************* |
| 998 | * ¹¦ÄÜÃèÊö: nvget |
| 999 | * ²ÎÊý˵Ã÷: |
| 1000 | * (´«Èë²ÎÊý) file:nvÎļþ |
| 1001 | * (´«Èë²ÎÊý) key:nvÏîÃû |
| 1002 | * (´«³ö²ÎÊý) value:nvÖµ |
| 1003 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 1004 | * ÆäËü˵Ã÷: void |
| 1005 | *******************************************************************************/ |
| 1006 | static int nvget(char *file, char *key, char *value) |
| 1007 | { |
| 1008 | int index = 0; |
| 1009 | T_NV_NODE *list = NULL; |
| 1010 | T_NV_ITEM *item = NULL; |
| 1011 | |
| 1012 | for(list = nv_list; list; list = list->next) |
| 1013 | { |
| 1014 | if(strcmp(list->nvFile, file)) |
| 1015 | continue; |
| 1016 | |
| 1017 | index = hash(key) % NV_HASH_LEN; |
| 1018 | for(item = list->nvTable[index]; item; item = item->next) |
| 1019 | { |
| 1020 | if(strcmp(item->key, key)) |
| 1021 | continue; |
| 1022 | |
| 1023 | strncpy(value, item->value, NV_MAX_VAL_LEN - 1); |
| 1024 | value[NV_MAX_VAL_LEN - 1] = '\0'; |
| 1025 | return RESULT_SUCCESS; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | return RESULT_NO_ITEM; |
| 1030 | } |
| 1031 | |
| 1032 | /******************************************************************************* |
| 1033 | * ¹¦ÄÜÃèÊö: nvset |
| 1034 | * ²ÎÊý˵Ã÷: |
| 1035 | * (´«Èë²ÎÊý) file:nvÎļþ |
| 1036 | * (´«Èë²ÎÊý) key:nvÏîÃû |
| 1037 | * (´«Èë²ÎÊý) value:nvÖµ |
| 1038 | * (´«Èë²ÎÊý) saveFlag:±£´æ±êÖ¾ |
| 1039 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 1040 | * ÆäËü˵Ã÷: void |
| 1041 | *******************************************************************************/ |
| 1042 | static int nvset(char *file, const char *key, const char *value, int saveFlag) |
| 1043 | { |
| 1044 | int index = 0; |
| 1045 | int ret = 0; |
| 1046 | int key_buf_len = 0; |
| 1047 | int value_buf_len = 0; |
| 1048 | T_NV_NODE *list = NULL; |
| 1049 | T_NV_ITEM *item = NULL; |
| 1050 | T_NV_ITEM *newItem = NULL; |
| 1051 | |
| 1052 | if (NULL == key || NULL == value) |
| 1053 | return RESULT_FAIL; |
| 1054 | |
| 1055 | key_buf_len = strlen(key) + 1; |
| 1056 | value_buf_len = strlen(value) + 1; |
| 1057 | |
| 1058 | for(list = nv_list; list; list = list->next) |
| 1059 | { |
| 1060 | if(strcmp(list->nvFile, file)) |
| 1061 | continue; |
| 1062 | |
| 1063 | index = hash(key) % NV_HASH_LEN; |
| 1064 | for(item = list->nvTable[index]; item; item = item->next) |
| 1065 | { |
| 1066 | // change item |
| 1067 | if(strcmp(item->key, key)) |
| 1068 | continue; |
| 1069 | |
| 1070 | if(saveFlag) |
| 1071 | item->saveFlag = saveFlag; |
| 1072 | |
| 1073 | if(!strcmp(item->value, value)) |
| 1074 | return RESULT_SUCCESS; |
| 1075 | |
| 1076 | free(item->value); |
| 1077 | item->value = (char *)malloc(value_buf_len); |
| 1078 | if(!item->value) |
| 1079 | return RESULT_MALLOC_FAIL; |
| 1080 | |
| 1081 | strncpy(item->value, value, value_buf_len - 1); |
| 1082 | item->value[value_buf_len - 1] = '\0'; |
| 1083 | |
| 1084 | return RESULT_SUCCESS; |
| 1085 | } |
| 1086 | |
| 1087 | // add item |
| 1088 | newItem = (T_NV_ITEM *)malloc(sizeof(T_NV_ITEM )); |
| 1089 | if(!newItem) |
| 1090 | return RESULT_MALLOC_FAIL; |
| 1091 | |
| 1092 | newItem->key = (char *)malloc(key_buf_len); |
| 1093 | if(!newItem->key) |
| 1094 | { |
| 1095 | free(newItem); |
| 1096 | return RESULT_MALLOC_FAIL; |
| 1097 | } |
| 1098 | |
| 1099 | newItem->value = (char *)malloc(value_buf_len); |
| 1100 | if(!newItem->value) |
| 1101 | { |
| 1102 | free(newItem->key); |
| 1103 | free(newItem); |
| 1104 | return RESULT_MALLOC_FAIL; |
| 1105 | } |
| 1106 | |
| 1107 | strncpy(newItem->key, key, key_buf_len - 1); |
| 1108 | newItem->key[key_buf_len - 1] = '\0'; |
| 1109 | strncpy(newItem->value, value, value_buf_len - 1); |
| 1110 | newItem->value[value_buf_len - 1] = '\0'; |
| 1111 | |
| 1112 | newItem->next = NULL; |
| 1113 | newItem->saveFlag = saveFlag; |
| 1114 | newItem->update_flag=0;// 0: not update , 1: updated |
| 1115 | |
| 1116 | if(!list->nvTable[index]) |
| 1117 | list->nvTable[index] = newItem; |
| 1118 | else |
| 1119 | { |
| 1120 | newItem->next = list->nvTable[index]->next; |
| 1121 | list->nvTable[index]->next = newItem; |
| 1122 | } |
| 1123 | |
| 1124 | return RESULT_SUCCESS; |
| 1125 | } |
| 1126 | |
| 1127 | // add nv |
| 1128 | ret = addConfigFile(file, NULL); |
| 1129 | if(ret == RESULT_SUCCESS) |
| 1130 | return nvset(file, key, value, saveFlag); |
| 1131 | else |
| 1132 | return ret; |
| 1133 | } |
| 1134 | |
| 1135 | /******************************************************************************* |
| 1136 | * ¹¦ÄÜÃèÊö: nvunset |
| 1137 | * ²ÎÊý˵Ã÷: |
| 1138 | * (´«Èë²ÎÊý) file:nvÎļþ |
| 1139 | * (´«Èë²ÎÊý) key:nvÏîÃû |
| 1140 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 1141 | * ÆäËü˵Ã÷: void |
| 1142 | *******************************************************************************/ |
| 1143 | static int nvunset(char * file, char *key) |
| 1144 | { |
| 1145 | int index = 0; |
| 1146 | T_NV_NODE *list = NULL; |
| 1147 | T_NV_ITEM *item = NULL; |
| 1148 | T_NV_ITEM *prev = NULL; |
| 1149 | |
| 1150 | for(list = nv_list; list; list = list->next) |
| 1151 | { |
| 1152 | if(strcmp(list->nvFile, file)) |
| 1153 | continue; |
| 1154 | |
| 1155 | index = hash(key) % NV_HASH_LEN; |
| 1156 | for(item = list->nvTable[index]; item; prev = item, item = item->next) |
| 1157 | { |
| 1158 | if(strcmp(item->key, key)) |
| 1159 | continue; |
| 1160 | |
| 1161 | if(!prev) |
| 1162 | list->nvTable[index] = item->next; |
| 1163 | else |
| 1164 | prev->next = item->next; |
| 1165 | |
| 1166 | free(item->key); |
| 1167 | free(item->value); |
| 1168 | free(item); |
| 1169 | |
| 1170 | return RESULT_SUCCESS; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | return RESULT_NO_ITEM; |
| 1175 | } |
| 1176 | |
| 1177 | /******************************************************************************* |
| 1178 | * ¹¦ÄÜÃèÊö: nvreset |
| 1179 | * ²ÎÊý˵Ã÷: |
| 1180 | * (´«Èë²ÎÊý) file:nvÎļþ |
| 1181 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 1182 | * ÆäËü˵Ã÷: ɾ³ýnvÎļþ |
| 1183 | *******************************************************************************/ |
| 1184 | static int nvreset(char *file) |
| 1185 | { |
| 1186 | int ret = 0; |
| 1187 | T_NV_NODE *list = NULL; |
| 1188 | |
| 1189 | for(list = nv_list; list; list = list->next) |
| 1190 | { |
| 1191 | if(strcmp(list->nvFile, file)) |
| 1192 | continue; |
| 1193 | |
| 1194 | ret = nvclear(file); |
| 1195 | if(ret != RESULT_SUCCESS) |
| 1196 | return ret; |
| 1197 | |
| 1198 | if(loadFactroyParam(list) != RESULT_SUCCESS) |
| 1199 | return RESULT_FAIL; |
| 1200 | |
| 1201 | return nvcommit(file); |
| 1202 | } |
| 1203 | |
| 1204 | return RESULT_NO_FILE; |
| 1205 | } |
| 1206 | |
| 1207 | /******************************************************************************* |
| 1208 | * ¹¦ÄÜÃèÊö: nvclear |
| 1209 | * ²ÎÊý˵Ã÷: |
| 1210 | * (´«Èë²ÎÊý) file:nvÎļþ |
| 1211 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 1212 | * ÆäËü˵Ã÷: ²Á³ýÄÚ´ænv |
| 1213 | *******************************************************************************/ |
| 1214 | static int nvclear(char *file) |
| 1215 | { |
| 1216 | int i = 0; |
| 1217 | T_NV_NODE *list = NULL; |
| 1218 | T_NV_ITEM *cur = NULL; |
| 1219 | T_NV_ITEM *item = NULL; |
| 1220 | |
| 1221 | for(list = nv_list; list; list = list->next) |
| 1222 | { |
| 1223 | if(strcmp(list->nvFile, file)) |
| 1224 | continue; |
| 1225 | |
| 1226 | for(i = 0; i < NV_HASH_LEN; i ++) |
| 1227 | { |
| 1228 | for(item = list->nvTable[i]; item; ) |
| 1229 | { |
| 1230 | cur = item; |
| 1231 | item = item->next; |
| 1232 | |
| 1233 | free(cur->key); |
| 1234 | free(cur->value); |
| 1235 | free(cur); |
| 1236 | } |
| 1237 | list->nvTable[i] = NULL; |
| 1238 | } |
| 1239 | |
| 1240 | return RESULT_SUCCESS; |
| 1241 | } |
| 1242 | |
| 1243 | return RESULT_NO_FILE; |
| 1244 | } |
| 1245 | |
| 1246 | /******************************************************************************* |
| 1247 | * ¹¦ÄÜÃèÊö: nvcommit |
| 1248 | * ²ÎÊý˵Ã÷: |
| 1249 | * (´«Èë²ÎÊý) file:nvÎļþ |
| 1250 | * ·µ »Ø Öµ: RESULT_SUCCESS±íʾ³É¹¦ |
| 1251 | * ÆäËü˵Ã÷: void |
| 1252 | *******************************************************************************/ |
| 1253 | static int nvcommit(char *file) |
| 1254 | { |
| 1255 | int ret = 0; |
| 1256 | char nvMainFile[NV_PATH_LEN] = {0}; |
| 1257 | char nvBackupFile[NV_PATH_LEN] = {0}; |
| 1258 | |
| 1259 | sprintf(nvMainFile, "%s/%s", NV_FS_MAIN_PATH, file); |
| 1260 | sprintf(nvBackupFile, "%s/%s", NV_FS_BACKUP_PATH, file); |
| 1261 | |
| 1262 | ret = saveNvFs(file, nvMainFile); |
| 1263 | if(ret != RESULT_SUCCESS) |
| 1264 | return ret; |
| 1265 | |
| 1266 | return restoreNvFs(nvBackupFile, nvMainFile); |
| 1267 | } |
| 1268 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1269 | #ifdef __cplusplus |
| 1270 | } |
| 1271 | #endif |
| 1272 | |