1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include "upack_data_file.h"
9 #include "ota_log.h"
10 #include "ota_import.h"
11
data_file_unpack(void * pack_file,unsigned int pack_size,void * upack_path)12 int data_file_unpack(void *pack_file, unsigned int pack_size, void *upack_path)
13 {
14 int ret = -1;
15 int i = 0;
16 char tmp_file_name[128];
17 int tmp_file_name_len = 0;
18 int read_fd = -1;
19 unsigned int upack_path_len = 0;
20 unsigned int read_len = 0;
21 data_file_pack_head_t pack_head;
22 data_file_infor_t file_info;
23 if ((pack_file != NULL) &&
24 (upack_path != NULL) &&
25 (strlen(upack_path) <= 64)) {
26 OTA_LOG_E("upack_path = %d\r\n", strlen(upack_path));
27 memset(tmp_file_name, 0x00, sizeof(tmp_file_name));
28 memset(&pack_head, 0x00, sizeof(data_file_pack_head_t));
29 memset(&file_info, 0x00, sizeof(data_file_infor_t));
30 upack_path_len = strlen(upack_path);
31 strncpy(tmp_file_name, upack_path, upack_path_len);
32 if (tmp_file_name[upack_path_len] != '/') {
33 tmp_file_name[upack_path_len++] = '/';
34 }
35 OTA_LOG_I("upack_path = %s\r\n", (char *)upack_path);
36 read_fd = ota_fopen(pack_file, O_RDONLY);
37 if (read_fd >= 0) {
38 read_len = ota_fread(read_fd, &pack_head, sizeof(data_file_pack_head_t));
39 OTA_LOG_E("read_len = %d\r\n", read_len);
40 if (read_len != sizeof(data_file_pack_head_t) ||
41 pack_head.pack_size != pack_size) {
42 OTA_LOG_E("file: %s is len erro, pack size = %d\r\n", (char *)pack_file, pack_head.pack_size);
43 } else {
44 ret = 0;
45 for (i = 0; i < pack_head.file_numb; i++) {
46 read_len = ota_fread(read_fd, (void *)&file_info, sizeof(data_file_infor_t));
47 if (read_len != sizeof(data_file_infor_t)) {
48 ret = -1;
49 OTA_LOG_E("file:%d read file info len err\r\n", i);
50 break;
51 } else {
52 tmp_file_name_len = file_info.head_size - sizeof(data_file_infor_t);
53 if (tmp_file_name_len > sizeof(tmp_file_name) - upack_path_len) {
54 ret = -1;
55 OTA_LOG_E("file%d name too long\r\n", i);
56 break;
57 } else {
58 read_len = ota_fread(read_fd, (void *)&tmp_file_name[upack_path_len], tmp_file_name_len);
59 if (read_len != tmp_file_name_len) {
60 ret = -1;
61 OTA_LOG_E("read file%d name err\r\n", i);
62 break;
63 } else {
64 int write_fd = -1;
65 int tmp_read_len = 0;
66 int has_read_len = 0;
67 int tmp_write_len = 0;
68 char tmp_buf[128];
69 OTA_LOG_I("file = %s\r\n", tmp_file_name);
70 write_fd = ota_fopen(tmp_file_name, O_WRONLY | O_CREAT | O_TRUNC);
71 while (has_read_len < file_info.file_size) {
72 file_info.file_size - has_read_len > sizeof(tmp_buf) ? (tmp_read_len = sizeof(tmp_buf)) : (tmp_read_len = file_info.file_size - has_read_len);
73 read_len = ota_fread(read_fd, (void *)tmp_buf, tmp_read_len);
74 if (read_len != tmp_read_len) {
75 ota_fclose(write_fd);
76 write_fd = -1;
77 OTA_LOG_E("file%d, read file data err\r\n", i);
78 ret = -1;
79 break;
80 } else {
81 tmp_write_len = ota_fwrite(write_fd, tmp_buf, tmp_read_len);
82 if (tmp_write_len != tmp_read_len) {
83 ota_fclose(write_fd);
84 write_fd = -1;
85 OTA_LOG_E("file%d, write file failed\r\n", i);
86 ret = -1;
87 break;
88 } else {
89 has_read_len += tmp_read_len;
90 }
91 }
92 }
93 memset(&tmp_file_name[upack_path_len], 0x00, sizeof(tmp_file_name) - upack_path_len);
94 if (ret == 0) {
95 ota_fclose(write_fd);
96 write_fd = -1;
97 OTA_LOG_E("file%d,write suc!\r\n", i);
98 } else {
99 OTA_LOG_E("file%d, write err!\r\n", i);
100 break;
101 }
102 }
103 }
104 }
105 }
106 }
107 } else {
108 OTA_LOG_E("pack faile: %s open failed\n", pack_file);
109 }
110 }
111 if (read_fd >= 0) {
112 ota_fclose(read_fd);
113 read_fd = -1;
114 }
115 if (ret < 0) {
116 OTA_LOG_E("unpack failed\n");
117 }
118 return ret;
119 }
120