blob: b5d7bf87add77ee60a6c5a176d990ab80c4ca918 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include "wsIntrn.h"
2#include <sys/stat.h>
3#include <unistd.h>
4#ifdef _USE_WEBUI_ZIP
5#include "unzip.h"
6#define WEB_ZIP_PATH "etc_ro/web.zip"
7#define WEB_ZIP_PATH_HEAD "web/"
8#endif
9
10int websPageOpen(webs_t wp, char_t *lpath, char_t *path, int mode, int perm)
11{
12 a_assert(websValid(wp));
13
14#ifdef WEBS_PAGE_ROM
15 return websRomPageOpen(wp, path, mode, perm);
16#else
17#ifdef _USE_WEBUI_ZIP
18 char *filename = strstr(lpath,WEB_ZIP_PATH_HEAD);
19 if(filename != NULL)
20 {
21 unzFile uzFile = unzOpen(WEB_ZIP_PATH);
22 if(unzLocateFile(uzFile,filename,0) == UNZ_OK
23 && unzOpenCurrentFile(uzFile) == UNZ_OK)
24 {
25 wp->docfd = uzFile;
26 }
27 else
28 {
29 unzClose(uzFile);
30 wp->docfd = -1;
31 }
32 return wp->docfd;
33 }
34 else
35 {
36 printf("[zpr]erropen %s\n", lpath);
37 }
38#endif
39 //printf("[httpshare]lpath->%s\n",lpath);
40
41 if(strstr(lpath,"/mmc2") != NULL)
42 {
43#if 0 // cov M
44 unsigned long filesize = -1;
45#else
46 long filesize = -1;
47#endif
48 struct stat statbuff;
49
50 if(stat(lpath, &statbuff) < 0)
51 {
52 return filesize;
53 }
54 else
55 {
56 filesize = statbuff.st_size;
57 }
58
59 printf("[httpshare]websPageOpen:size->%d\n", filesize);
60
61#if 0 // cov M
62 if(filesize<2*1024*1024*1024)
63 {
64 return (wp->docfd = gopen(lpath, mode, perm));
65 }
66 else
67 return -1;
68#else
69 if(filesize > 0 && filesize <= LONG_MAX-1)
70 {
71 return (wp->docfd = gopen(lpath, mode, perm));
72 }
73 else
74 return -1;
75
76#endif
77 }
78
79 return (wp->docfd = gopen(lpath, mode, perm));
80#endif /* WEBS_PAGE_ROM */
81}
82
83void websPageClose(webs_t wp)
84{
85 a_assert(websValid(wp));
86
87#ifdef WEBS_PAGE_ROM
88 websRomPageClose(wp->docfd);
89#else
90#ifdef _USE_WEBUI_ZIP
91 if(wp->docfd > 0 && wp->lpath != NULL && strstr(wp->lpath,WEB_ZIP_PATH_HEAD) != NULL)
92 {
93 unzCloseCurrentFile(wp->docfd);
94 unzClose(wp->docfd);
95 wp->docfd = -1;
96 return;
97 }
98#endif
99 if (wp->docfd >= 0) {
100 close(wp->docfd);
101 wp->docfd = -1;
102 //printf("[httpsahre]lpath->%s,path->%s\n",wp->lpath,wp->path);
103 if(strstr(wp->path,"/mmc2") != NULL)
104 {
105 zte_del_download_file();
106 }
107 }
108#endif
109}
110
111int websPageStat(webs_t wp, char_t *lpath, char_t *path, websStatType* sbuf)
112{
113 if(lpath == NULL)
114 return -1;
115#ifdef WEBS_PAGE_ROM
116 return websRomPageStat(path, sbuf);
117#else
118#ifdef _USE_WEBUI_ZIP
119 if(strstr(lpath,WEB_ZIP_PATH_HEAD) != NULL)
120 {
121 unz_file_info file_info = {0};
122 if(unzGetCurrentFileInfo(wp->docfd, &file_info, NULL, 0, NULL, 0, NULL, 0)< 0)
123 {
124 printf("[webzip]errStat %s\n", lpath);
125 return -1;
126 }
127 sbuf->size = file_info.uncompressed_size;
128 sbuf->mtime = file_info.dosDate;
129 sbuf->isDir = websPageIsDirectory(lpath);
130 return 0;
131 }
132#endif
133 gstat_t s;
134
135 if (gstat(lpath, &s) < 0) {
136 return -1;
137 }
138 sbuf->size = s.st_size;
139 sbuf->mtime = s.st_mtime;
140 sbuf->isDir = s.st_mode & S_IFDIR;
141 return 0;
142#endif
143}
144
145int websPageIsDirectory(char_t *lpath)
146{
147 if(lpath == NULL)
148 return 0;
149#ifdef WEBS_PAGE_ROM
150 websStatType sbuf;
151
152 if (websRomPageStat(lpath, &sbuf) >= 0) {
153 return(sbuf.isDir);
154 } else {
155 return 0;
156 }
157#else
158#ifdef _USE_WEBUI_ZIP
159 if(strstr(lpath,WEB_ZIP_PATH_HEAD) != NULL)
160 {
161 char *p = lpath;
162 int is_dir = 0;
163 while ((*p) != '\0') {
164 p++;
165 }
166 p--;
167
168 if (((*p) == '/') || ((*p) == '\\')) {
169 is_dir = 1;
170 }
171 return is_dir;
172 }
173#endif
174 gstat_t sbuf;
175
176 if (gstat(lpath, &sbuf) >= 0) {
177 return(sbuf.st_mode & S_IFDIR);
178 } else {
179 return 0;
180 }
181#endif
182}
183
184int websPageReadData(webs_t wp, char *buf, int nBytes)
185{
186 a_assert(websValid(wp));
187#ifdef WEBS_PAGE_ROM
188 return websRomPageReadData(wp, buf, nBytes);
189#else
190#ifdef _USE_WEBUI_ZIP
191 if(wp->lpath != NULL && strstr(wp->lpath,WEB_ZIP_PATH_HEAD) != NULL)
192 {
193 return unzReadCurrentFile(wp->docfd, buf, nBytes);
194 }
195#endif
196 return read(wp->docfd, buf, nBytes);
197#endif
198}
199
200void websPageSeek(webs_t wp, long offset)
201{
202 a_assert(websValid(wp));
203#ifdef WEBS_PAGE_ROM
204 websRomPageSeek(wp, offset, SEEK_CUR);
205#else
206#ifdef _USE_WEBUI_ZIP
207 if(wp->lpath != NULL && strstr(wp->lpath,WEB_ZIP_PATH_HEAD) != NULL)
208 {
209 printf("[webzip]seek %s\n",wp->lpath);
210 return;
211 }
212#endif
213 if(lseek(wp->docfd, offset, SEEK_CUR) < 0)
214 {
215 return;
216 }
217#endif
218}
219