upgrade.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/ipc.h>
  7. #include <sys/msg.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include <dirent.h>
  15. #include "upgrade.h"
  16. #include "md5.h"
  17. #include "cfg.h"
  18. #if 0
  19. #define LOGD log_print
  20. #define LOGE log_print
  21. #else
  22. #define LOGD printf
  23. #define LOGE printf
  24. #endif
  25. typedef struct {
  26. char *pbuf;
  27. int dlen;
  28. int blen;
  29. }node_t;
  30. typedef struct {
  31. uint32_t src; //
  32. uint8_t obj; //升级对象,解压后扫描文件,赋值到该变量
  33. uint8_t stat; //当前升级所处状态
  34. uint8_t step; //当前升级所处阶段
  35. pthread_t tid;
  36. FILE* log;
  37. }upg_handle_t;
  38. static upg_handle_t upgHandle;
  39. static int log_print(const char *fmt, ...);
  40. static int node_free(node_t *nd)
  41. {
  42. if(!nd) {
  43. return -1;
  44. }
  45. free(nd->pbuf);
  46. nd->pbuf = NULL;
  47. nd->dlen = 0;
  48. nd->blen = 0;
  49. return 0;
  50. }
  51. static int exe_cmd(char *cmd, node_t *nd)
  52. {
  53. int r=0;
  54. FILE *fp=NULL;
  55. char *pbuf=NULL;
  56. int onelen=1000;
  57. int dlen=0,blen=onelen;
  58. pbuf = malloc(onelen);
  59. if(!pbuf) {
  60. return -1;
  61. }
  62. fp = popen(cmd, "r");
  63. if (fp == NULL) {
  64. return -1;
  65. }
  66. while(1) {
  67. r = fread(pbuf+dlen, sizeof(char), onelen, fp);
  68. if(r<=0) {
  69. LOGE("upgrade relloac %d failed\n", dlen+onelen);
  70. break;
  71. }
  72. dlen += r;
  73. if(dlen+onelen>blen) {
  74. char* p = realloc(pbuf, dlen+onelen);
  75. if(!p) {
  76. LOGE("upgrade relloac %d failed\n", dlen+onelen);
  77. free(pbuf); pclose(fp);
  78. return -1;
  79. }
  80. pbuf = p;
  81. blen = dlen+onelen;
  82. }
  83. }
  84. pclose(fp);
  85. nd->pbuf = pbuf;
  86. nd->dlen = dlen;
  87. nd->blen = blen;
  88. return 0;
  89. }
  90. static int exe_cmd2(char *cmd)
  91. {
  92. return system(cmd);
  93. }
  94. //https://blog.csdn.net/qq_38650447/article/details/136284071
  95. static int log_init(void)
  96. {
  97. char *path=UPG_LOG_PATH;
  98. char tmp[200];
  99. if (upgHandle.log) {
  100. return -1;
  101. }
  102. snprintf(tmp, sizeof(tmp), "mkdir -p %s", path);
  103. system(tmp);
  104. upgHandle.log = fopen(path, "wt");
  105. if (!upgHandle.log) {
  106. LOGE("%s open failed\n", path);
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static int log_print(const char *fmt, ...)
  112. {
  113. int r;
  114. va_list va;
  115. char buf[1000];
  116. va_start(va, fmt);
  117. r = vsprintf(buf, fmt, va);
  118. va_end(va);
  119. if (upgHandle.log) {
  120. fprintf(upgHandle.log, "%s", buf);
  121. }
  122. else {
  123. printf("%s", buf);
  124. }
  125. return 0;
  126. }
  127. static int log_flush(void)
  128. {
  129. if (upgHandle.log) {
  130. return -1;
  131. }
  132. fflush(upgHandle.log);
  133. return 0;
  134. }
  135. static int file_exist(char* path)
  136. {
  137. int r;
  138. struct stat st;
  139. r = stat(path, &st);
  140. if (r == 0) {
  141. return 1;
  142. }
  143. else {
  144. return 0;
  145. }
  146. }
  147. static int file_length(char* path)
  148. {
  149. int r;
  150. struct stat st;
  151. r = stat(path, &st);
  152. if (r) {
  153. return -1;
  154. }
  155. return st.st_size;
  156. }
  157. static int file_touch(int id)
  158. {
  159. int r;
  160. char tmp[200];
  161. snprintf(tmp, sizeof(tmp), "touch %s/s%d", UPG_FOLDER, id);
  162. return system(tmp); // 使用写入模式创建一个新文件
  163. }
  164. static int file_untouch(int id)
  165. {
  166. int r;
  167. char tmp[200];
  168. snprintf(tmp, sizeof(tmp), "rm %s/s%d", UPG_FOLDER, id);
  169. return system(tmp); // 使用写入模式创建一个新文件
  170. }
  171. static int folder_init(void)
  172. {
  173. char tmp[200];
  174. snprintf(tmp, sizeof(tmp), "mkdir -p %s", RUN_FOLDER);
  175. system(tmp);
  176. snprintf(tmp, sizeof(tmp), "mkdir -p %s", BAK_FOLDER);
  177. system(tmp);
  178. snprintf(tmp, sizeof(tmp), "mkdir -p %s", UPG_FOLDER);
  179. system(tmp);
  180. snprintf(tmp, sizeof(tmp), "mkdir -p %s", DOWN_FOLDER);
  181. system(tmp);
  182. return 0;
  183. }
  184. static int file_save(char *path, void *buf, int len)
  185. {
  186. int r;
  187. FILE *fp;
  188. fp = fopen(path, "wb");
  189. if(!fp) {
  190. LOGE("file_save, fopen %s failed\n", path);
  191. return -1;
  192. }
  193. r = fwrite(buf, 1, len, fp);
  194. if(r!=len) {
  195. LOGE("file_save %s failed\n", path);
  196. r = -1;
  197. }
  198. else {
  199. r = 0;
  200. }
  201. fclose(fp);
  202. return r;
  203. }
  204. static int file_load(char* path, void* buf, int len)
  205. {
  206. int r;
  207. FILE* fp;
  208. fp = fopen(path, "rb");
  209. if (!fp) {
  210. LOGE("file_load, fopen %s failed\n", path);
  211. return -1;
  212. }
  213. r = fread(buf, 1, len, fp);
  214. if (r != len) {
  215. LOGE("file_load %s failed\n", path);
  216. r = -1;
  217. }
  218. else {
  219. r = 0;
  220. }
  221. fclose(fp);
  222. return r;
  223. }
  224. static int file_remove(char *path)
  225. {
  226. return remove(path);
  227. }
  228. /*
  229. tar pdutar to run/
  230. */
  231. static int file_cover(char *path)
  232. {
  233. int r;
  234. char cmd[200];
  235. snprintf(cmd, sizeof(cmd), "tar -xpf %s -C / > /dev/null 2>&1", path);
  236. r = system(cmd);
  237. if(r) {
  238. LOGE("%s failed\n", cmd);
  239. return -1;
  240. }
  241. return 0;
  242. }
  243. static int dir_is_empty(char *path)
  244. {
  245. struct dirent *entry;
  246. int is_empty = 1;
  247. DIR *dir = opendir(path);
  248. if (dir == NULL) {
  249. perror("opendir failed\n");
  250. return -1; // 错误处理
  251. }
  252. while ((entry = readdir(dir)) != NULL) {
  253. // 忽略'.'和'..'
  254. if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
  255. is_empty = 0;
  256. break;
  257. }
  258. }
  259. closedir(dir);
  260. return is_empty;
  261. }
  262. //when app run success, copy run/* to bak/
  263. static int file_backup(void)
  264. {
  265. int r;
  266. char tmp[100];
  267. char cmd[200];
  268. //dl目录有tar压缩包,则拷贝到bak目录
  269. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  270. if(file_exist(tmp)) {
  271. LOGD("%s exist\n", tmp);
  272. snprintf(cmd, sizeof(cmd), "cp -f %s %s/", tmp, BAK_FOLDER);
  273. r = system(cmd);
  274. if(r==0) {
  275. return 0;
  276. }
  277. LOGD("cp %s to %s failed\n", tmp, BAK_FOLDER);
  278. }
  279. else {
  280. LOGD("%s not exist\n", tmp);
  281. }
  282. //bak目录有tar压缩包,则退出
  283. snprintf(tmp, sizeof(tmp), "%s/%s", BAK_FOLDER, UPG_TAR_NAME);
  284. if(file_exist(tmp)) {
  285. LOGD("%s exist\n", tmp);
  286. return 0;
  287. }
  288. else {
  289. LOGD("%s not exist\n", tmp);
  290. }
  291. //bak目录没有tar压缩包,则将run目录打包存进bak目录
  292. LOGD("tar %s to %s/%s\n", RUN_FOLDER, BAK_FOLDER, UPG_TAR_NAME);
  293. snprintf(cmd, sizeof(cmd), "tar -czpPf %s/%s %s > /dev/null 2>&1", BAK_FOLDER, UPG_TAR_NAME, RUN_FOLDER);
  294. r = system(cmd);
  295. if(r) {
  296. LOGE("%s failed, %d\n", cmd, r);
  297. return -1;
  298. }
  299. return 0;
  300. }
  301. /*
  302. when app run failed, copy to bak/ run/
  303. */
  304. static int file_restore(void)
  305. {
  306. int r;
  307. char cmd[200];
  308. snprintf(cmd, sizeof(cmd), "tar -xpf %s/%s -C / > /dev/null 2>&1", DOWN_FOLDER, UPG_TAR_NAME);
  309. r = system(cmd);
  310. if(r) {
  311. LOGE("%s failed, %d\n", cmd, r);
  312. return -1;
  313. }
  314. return 0;
  315. }
  316. #define CHAR(b) (((b)<=9)?((b)+0x30):((b)-10+0x61));
  317. static void byte2char(char *chr, uint8_t b)
  318. {
  319. chr[0] = CHAR(b>>4);
  320. chr[1] = CHAR(b&0x0f);
  321. }
  322. static void print_md5(char *s, md5_t *m)
  323. {
  324. int i;
  325. LOGD("__%s", s);
  326. for(i=0; i<32; i++) {
  327. LOGD("%c", m->data[i]);
  328. }
  329. LOGD("\n");
  330. }
  331. //md5相同返回0,否则返回1
  332. static int md5_check(upgrade_file_t *h)
  333. {
  334. md5_ctx_t ctx;
  335. uint8_t i,tmp[16];
  336. md5_t m1,m2;
  337. md5_init(&ctx);
  338. md5_update(&ctx, (uint8_t*)h, h->dlen+sizeof(upgrade_file_t));
  339. md5_final(&ctx, tmp);
  340. for (i=0; i<16; i++) {
  341. byte2char(&m2.data[i*2], tmp[i]);
  342. }
  343. memcpy(&m1, h->data+h->dlen, sizeof(md5_t));
  344. print_md5("ori md5: ", &m1);
  345. print_md5("cal md5: ", &m2);
  346. if(memcmp(&m1, &m2, sizeof(md5_t))) {
  347. return -1;
  348. }
  349. return 0;
  350. }
  351. static int file_split(void)
  352. {
  353. int r=0,flen;
  354. char *fbuf=NULL;
  355. char path[200];
  356. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  357. flen = file_length(path);
  358. if(flen<=0) {
  359. LOGE("file_length %s failed\n", path);
  360. return -1;
  361. }
  362. fbuf = (char*)malloc(flen);
  363. if(!fbuf) {
  364. LOGE("malloc %d failed\n", flen);
  365. return -1;
  366. }
  367. r = file_load(path, fbuf, flen);
  368. if(r) {
  369. LOGE("%s load failed\n", path);
  370. return -1;
  371. }
  372. upgrade_file_t *hfile = (upgrade_file_t*)fbuf;
  373. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_HDR_NAME);
  374. r = file_save(path, hfile, sizeof(upgrade_file_t));
  375. if(r) {
  376. LOGE("file_save %s failed\n", path);
  377. return -1;
  378. }
  379. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  380. r = file_save(path, hfile->data, hfile->dlen);
  381. if(r) {
  382. LOGE("file_save %s failed\n", path);
  383. return -1;
  384. }
  385. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  386. r = file_remove(path);
  387. if(r) {
  388. LOGE("file_remove %s failed\n", path);
  389. return -1;
  390. }
  391. return 0;
  392. }
  393. static int file_check(char *path)
  394. {
  395. int r=0,flen;
  396. char *fbuf=NULL;
  397. upgrade_file_t *hfile=NULL;
  398. flen = file_length(path);
  399. if(flen<=0) {
  400. LOGE("file_length %s failed\n", path);
  401. return -1;
  402. }
  403. fbuf = (char*)malloc(flen);
  404. if(!fbuf) {
  405. LOGE("malloc %d failed\n", flen);
  406. return -1;
  407. }
  408. r = file_load(path, fbuf, flen);
  409. if(r) {
  410. LOGE("%s load failed\n", path);
  411. return -1;
  412. }
  413. hfile = (upgrade_file_t*)fbuf;
  414. if(hfile->magic!=UPG_FILE_MAGIC ||
  415. hfile->dlen!=flen-sizeof(upgrade_file_t)-sizeof(md5_t)) {
  416. r = -1;
  417. LOGE("magic: 0x%08x, should be: 0x%08x\n", hfile->magic, UPG_FILE_MAGIC);
  418. LOGE("num: %d, should < %d\n", hfile->obj.num, UPGRADE_NUM_MAX);
  419. LOGE("stra: %d, should < %d\n", hfile->obj.stra, UPGRADE_STRA_MAX);
  420. LOGE("file hdr check failed\n");
  421. goto fail;
  422. }
  423. r = md5_check(hfile);
  424. if(r) {
  425. LOGE("md5 check failed\n");
  426. goto fail;
  427. }
  428. fail:
  429. free(fbuf);
  430. return r;
  431. }
  432. static int file_scan(upg_handle_t *h)
  433. {
  434. //检查DOWN目录,如有升级文件
  435. //udisk检查,如有升级文件,拷贝到DOWN_FOLDER目录,并设置step为UPGRADE_STEP_DOWN
  436. //tcard检查,如有升级文件,拷贝到DOWN_FOLDER目录,并设置step为UPGRADE_STEP_DOWN
  437. return 0;
  438. }
  439. static int file_clear(void)
  440. {
  441. char tmp[200];
  442. snprintf(tmp, sizeof(tmp), "rm -r %s/* %s/*", DOWN_FOLDER, UPG_FOLDER);
  443. return system(tmp);
  444. }
  445. //////////////////////////////////////////////////
  446. static int is_running(char *prog)
  447. {
  448. FILE *fp;
  449. int pid=0;
  450. char buf[20]={0};
  451. char command[200];
  452. snprintf(command, sizeof(command), "ps -ef | grep -v grep | grep -w -c %s", prog);
  453. fp = popen(command, "r");
  454. if (fp == NULL) {
  455. LOGE("execute %s failed: %s", command, strerror(errno));
  456. return 0;
  457. }
  458. if (fgets(buf, sizeof(buf), fp)) {
  459. pid = atoi(buf);
  460. }
  461. pclose(fp);
  462. return (pid)?1:0;
  463. }
  464. static int app_start(void)
  465. {
  466. LOGD("____ app_start\n");
  467. if(is_running("smartMonitor.sh")) {
  468. return -1;
  469. }
  470. return system("/usr/sbin/smartMonitor.sh &");
  471. }
  472. static int app_stop(void)
  473. {
  474. LOGD("____ app_stop\n");
  475. return system("killall smartMonitor.sh; /etc/init.d/S90PDUApp stop");
  476. }
  477. static int sys_reboot(void)
  478. {
  479. return system("reboot");
  480. }
  481. //////////////////////////////////////////////////
  482. static void* upg_thread(void *arg)
  483. {
  484. int r=0;
  485. int msgid;
  486. key_t key;
  487. msgbuf_t mb;
  488. upg_handle_t *h=(upg_handle_t*)arg;
  489. LOGD("upg_thread running\n");
  490. key = ftok(UPG_KEY_PATH, UPG_KEY_SN);
  491. if(key<0) {
  492. LOGE("ftok %s failed, errno: %d\n", UPG_KEY_PATH, errno);
  493. pthread_exit(NULL);
  494. }
  495. msgid = msgget(key, 0666|IPC_CREAT);
  496. LOGD("___ key: %d, msgid: %d\n", key, msgid);
  497. //msgctl(msgid, IPC_RMID, NULL);
  498. while(1) {
  499. r = msgrcv(msgid, &mb, sizeof(mb.evt), 0, 0);
  500. if(r<0) {
  501. LOGE("___msgrcv failed, %d\n", errno);
  502. continue;
  503. }
  504. LOGD("___ msgrcv evt: 0x%02x\n", mb.evt);
  505. switch(mb.evt) {
  506. case UPGRADE_EVT_APP_RUNNING:
  507. {
  508. LOGD("___ smartPDU running!\n");
  509. h->step = UPGRADE_STEP_SUCCESS;
  510. }
  511. break;
  512. case UPGRADE_EVT_UPLOAD_FINISH:
  513. {
  514. LOGD("___ upgrade package upload finished!\n");
  515. h->step = UPGRADE_STEP_DOWN;
  516. file_touch(UPGRADE_STEP_DOWN);
  517. }
  518. break;
  519. }
  520. }
  521. pthread_exit(NULL);
  522. }
  523. static int upg_init(upg_handle_t *h)
  524. {
  525. int r;
  526. folder_init();
  527. h->src = UPGRADE_SRC_NONE;
  528. h->stat = UPGRADE_STAT_NONE;
  529. return pthread_create(&h->tid, NULL, upg_thread, h);
  530. }
  531. static int upg_check(upg_handle_t *h)
  532. {
  533. int i,r;
  534. char tmp[200];
  535. for(i=1; i<UPGRADE_STEP_MAX; i++) {
  536. snprintf(tmp, sizeof(tmp), "%s/s%d", UPG_FOLDER, i);
  537. r = file_exist(tmp);
  538. if(r) {
  539. LOGD("%s exist\n", tmp);
  540. //重启后如果是覆盖完成,则将进度置为SUCCESS
  541. h->step = i+1;
  542. break;
  543. }
  544. LOGD("%s not exist\n", tmp);
  545. }
  546. return r;
  547. }
  548. static int upg_handle(upg_handle_t *h)
  549. {
  550. int r=-1;
  551. char tmp[200];
  552. //LOGD("___h->step: %d\n", h->step);
  553. while(h->step!=UPGRADE_STEP_NONE) {
  554. switch(h->step) {
  555. case UPGRADE_STEP_DOWN:
  556. {
  557. LOGD("___ UPGRADE_STEP_DOWN\n");
  558. //先检查下载目录是否存在pdupkg文件
  559. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  560. if(file_exist(tmp)) {
  561. file_touch(UPGRADE_STEP_DOWN);
  562. h->step = UPGRADE_STEP_SPLIT;
  563. }
  564. else {
  565. LOGD("___ %s not exist, ", tmp);
  566. //下载目录不存在pdupkg文件,则继续检查是否存在pdutar文件
  567. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  568. LOGD("read %s\n", tmp);
  569. if(file_exist(tmp)) {
  570. file_touch(UPGRADE_STEP_DOWN);
  571. h->step = UPGRADE_STEP_COVER;
  572. }
  573. else {
  574. file_untouch(UPGRADE_STEP_DOWN);
  575. h->step = UPGRADE_STEP_NONE;
  576. }
  577. }
  578. }
  579. break;
  580. case UPGRADE_STEP_SPLIT:
  581. {
  582. LOGD("___ UPGRADE_STEP_SPLIT\n");
  583. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  584. r = file_check(tmp);
  585. if(r==0) {
  586. r = file_split();
  587. if(r==0) {
  588. file_touch(UPGRADE_STEP_SPLIT);
  589. h->step = UPGRADE_STEP_COVER;
  590. }
  591. else {
  592. file_untouch(UPGRADE_STEP_SPLIT);
  593. h->step = UPGRADE_STEP_NONE;
  594. }
  595. }
  596. else {
  597. file_remove(tmp);
  598. h->step = UPGRADE_STEP_NONE;
  599. }
  600. file_untouch(UPGRADE_STEP_DOWN);
  601. }
  602. break;
  603. case UPGRADE_STEP_COVER:
  604. {
  605. LOGD("___ UPGRADE_STEP_COVER\n");
  606. app_stop();
  607. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  608. r = file_cover(tmp);
  609. if(r==0) {
  610. file_touch(UPGRADE_STEP_COVER);
  611. h->step = UPGRADE_STEP_FINISH;
  612. sys_reboot();
  613. }
  614. else {
  615. LOGD("file_cover, delete %s!\n", tmp);
  616. file_remove(tmp);
  617. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_HDR_NAME);
  618. file_remove(tmp);
  619. h->step = UPGRADE_STEP_NONE;
  620. file_untouch(UPGRADE_STEP_COVER);
  621. file_restore();
  622. app_start();
  623. }
  624. file_untouch(UPGRADE_STEP_DOWN);
  625. file_untouch(UPGRADE_STEP_SPLIT);
  626. }
  627. break;
  628. case UPGRADE_STEP_FINISH:
  629. {
  630. LOGD("___ UPGRADE_STEP_FINISH\n");
  631. h->step = UPGRADE_STEP_NONE;
  632. }
  633. break;
  634. case UPGRADE_STEP_SUCCESS:
  635. {
  636. LOGD("___ UPGRADE_STEP_SUCCESS\n");
  637. h->step = UPGRADE_STEP_NONE;
  638. file_backup(); //启动成功才证明程序有效,可进行备份, 备份成功,删除下载目录中的文件以节省空间
  639. file_clear();
  640. //app_start();
  641. }
  642. break;
  643. default:
  644. LOGD("___ wrong step: %d\n", h->step);
  645. r = 1;
  646. break;
  647. }
  648. }
  649. return r;
  650. }
  651. /////////////////////////////////////////////////////////////////////////
  652. int main(void)
  653. {
  654. int id,r=0;
  655. upg_handle_t *h=&upgHandle;
  656. LOGD("___upgrade running ...\n");
  657. upg_init(h);
  658. upg_check(h);
  659. while(1) {
  660. upg_handle(h);
  661. sleep(1);
  662. }
  663. return 0;
  664. }