lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include "zte_web_interface.h" |
| 2 | #include "zte_web_httpshare.h" |
| 3 | #include "../server/webs.h" |
| 4 | #include "sqlite3.h" |
| 5 | #include <sys/wait.h> |
| 6 | #include "cfg_api.h" |
| 7 | |
| 8 | USER_COMMON_INFOR pp_header; |
| 9 | |
| 10 | static char file_buf_memory[FILE_BUFFER_LEN] = {0}; |
| 11 | static char *file_buf_memory_ptr = NULL; |
| 12 | static int tcard_file_size = 0; |
| 13 | static BOOL file_end = FALSE; |
| 14 | char download_file[ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1] = {0}; |
| 15 | |
| 16 | void zte_httpshare_check_upload_file(); |
| 17 | |
| 18 | static char* neutralize(char *path); |
| 19 | |
| 20 | int zte_httpshare_call_system(char * cmd) |
| 21 | { |
| 22 | if (NULL == cmd) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 23 | slog(MISC_PRINT,SLOG_DEBUG,"[httpshare]httpshare_call_system: NULL-------------------------------\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 24 | return -1; |
| 25 | } |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 26 | slog(MISC_PRINT,SLOG_NORMAL,"[httpshare]httpshare_call_system: [%s] \n", cmd); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 27 | #if 0 |
| 28 | return system(cmd); |
| 29 | #else |
| 30 | return zxic_system(cmd); |
| 31 | #endif |
| 32 | } |
| 33 | |
| 34 | static int zte_httpshare_call_system_echo(char * cmd) |
| 35 | { |
| 36 | if (NULL == cmd) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 37 | slog(MISC_PRINT,SLOG_DEBUG,"[httpshare]httpshare_call_system_echo: NULL-------------------------------\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 38 | return -1; |
| 39 | } |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 40 | slog(MISC_PRINT,SLOG_NORMAL,"[httpshare]httpshare_call_system_echo: [%s] \n", cmd); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 41 | |
| 42 | return soft_system(cmd); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | int zte_system(char *cmd[]) |
| 47 | { |
| 48 | pid_t pid; |
| 49 | int status; |
| 50 | pid_t ret; |
| 51 | char *newenviron[] = { NULL }; |
| 52 | |
| 53 | // cmd array's size is unknown |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 54 | // slog(MISC_PRINT,SLOG_NORMAL,"[httpshare]httpshare_call_system: [%s %s %s] \n", cmd[0],cmd[1],cmd[2]); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 55 | |
| 56 | pid = fork(); |
| 57 | if (pid == -1) { |
| 58 | return -1; |
| 59 | } else if (pid == 0) { |
| 60 | /*child process */ |
| 61 | execve(cmd[0], cmd, newenviron); |
| 62 | /* for kw |
| 63 | if (execve(cmd[0], cmd, newenviron) == -1) |
| 64 | _exit(127);*/ |
| 65 | } else { |
| 66 | /*father process */ |
| 67 | while ((ret = waitpid(pid, &status, 0)) == -1) { |
| 68 | if (errno != EINTR) |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | if (ret != -1 && WIFEXITED(status)) |
| 73 | return WEXITSTATUS(status); |
| 74 | } |
| 75 | |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | zte_httpshare_db_result_e_type zte_httpshare_db_open(sqlite3**db) |
| 80 | { |
| 81 | sqlite3* tmp_db; |
| 82 | int rc = 0; |
| 83 | |
| 84 | if (NULL == db) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 85 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_db_open:invalide inputs.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 86 | return ZTE_HTTPSHARE_DB_ERROR_INVAILD_PTR; |
| 87 | } |
| 88 | |
| 89 | rc = sqlite3_open(ZTE_HTTPSHARE_DB_PATH, &tmp_db); |
| 90 | |
| 91 | if (rc) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 92 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_db_open:can not open db,sqlite3_errmsg:%s\n", sqlite3_errmsg(tmp_db)); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 93 | (void)sqlite3_close(tmp_db); |
| 94 | return ZTE_HTTPSHARE_DB_ERROR_NOT_OPEN_DB; |
| 95 | } |
| 96 | *db = tmp_db; |
| 97 | return ZTE_HTTPSHARE_DB_OK; |
| 98 | } |
| 99 | |
| 100 | zte_httpshare_db_result_e_type zte_httpshare_db_close(sqlite3*db) |
| 101 | { |
| 102 | int rc = 0; |
| 103 | |
| 104 | if (NULL == db) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 105 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_db_close:invalide inputs.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 106 | return ZTE_HTTPSHARE_DB_ERROR_INVAILD_PTR; |
| 107 | } |
| 108 | rc = sqlite3_close(db); |
| 109 | if (rc) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 110 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_db_close:can not close db.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 111 | return ZTE_HTTPSHARE_DB_ERROR; |
| 112 | } |
| 113 | return ZTE_HTTPSHARE_DB_OK; |
| 114 | } |
| 115 | zte_httpshare_db_result_e_type zte_httpshare_db_exec_sql(const char *sql, sqlite3_callback callback, void *fvarg) |
| 116 | { |
| 117 | sqlite3* db; |
| 118 | int rc = 0; |
| 119 | |
| 120 | if (NULL == sql) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 121 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_db_exec_sql:invalide inputs.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 122 | return ZTE_HTTPSHARE_DB_ERROR_INVAILD_PTR; |
| 123 | } |
| 124 | if (0 != zte_httpshare_db_open(&db)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 125 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_db_exec_sql:open httpshare.db failed.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 126 | return ZTE_HTTPSHARE_DB_ERROR_NOT_OPEN_DB; |
| 127 | } |
| 128 | rc = sqlite3_exec(db, sql, callback, fvarg, NULL); |
| 129 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 130 | slog(MISC_PRINT, SLOG_NORMAL,"[httpshare]httpshare_db_exec_sql:%s rc=%d\n", sql, rc); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 131 | if (rc) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 132 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_db_exec_sql:can not exec sql,sqlite3_errmsg:%s.\n", sqlite3_errmsg(db)); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 133 | (void)zte_httpshare_db_close(db); |
| 134 | return ZTE_HTTPSHARE_DB_ERROR; |
| 135 | } |
| 136 | (void)zte_httpshare_db_close(db); |
| 137 | |
| 138 | return ZTE_HTTPSHARE_DB_OK; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | int zte_insert_download_file(char *path) |
| 143 | { |
| 144 | memset(download_file, 0, sizeof(download_file)); |
| 145 | strncpy(download_file, path, sizeof(download_file)-1); |
| 146 | slog(MISC_PRINT, SLOG_NORMAL,"[httpshare]download_file->%s\n", download_file); |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | int zte_del_download_file() |
| 151 | { |
| 152 | slog(MISC_PRINT, SLOG_NORMAL,"[httpshare]del download_file->%s\n", download_file); |
| 153 | memset(download_file, 0, sizeof(download_file)); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | int zte_check_download_file(char *path)//0: ²»´æÔڼǼ 1:¼Ç¼´æÔÚ |
| 159 | { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 160 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]check_download_file:path->%s\n", path); |
| 161 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]check_download_file:download_file->%s\n", download_file); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 162 | |
| 163 | if (!strncmp(path, download_file, strlen(path))) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 164 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]check_download_file:find used file->%s\n", path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 165 | return 1; |
| 166 | } else { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 167 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]check_download_file:not find file->%s\n", path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | int zte_check_downloading_file()//0: ²»´æÔڼǼ 1:¼Ç¼´æÔÚ |
| 173 | { |
| 174 | slog(MISC_PRINT, SLOG_DEBUG,"[zyl]strlen(download_file)->%d\n", strlen(download_file)); |
| 175 | return strlen(download_file); |
| 176 | } |
| 177 | |
| 178 | void zte_change_rootpath_time() |
| 179 | { |
| 180 | struct utimbuf times = {0}; |
| 181 | utime(SD_CARD_PATH, ×); //timesΪ¿Õʱ£¬utime ½«·ÃÎÊʱ¼äºÍÐÞ¸Äʱ¼äÉèÖÃΪµ±Ç°Ê±¼ä |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | void zte_init_user_list() |
| 186 | { |
| 187 | memset(&pp_header, 0, sizeof(USER_COMMON_INFOR)); |
| 188 | memset(&download_file, 0, sizeof(download_file)); |
| 189 | } |
| 190 | |
| 191 | int zte_del_file(char *path) |
| 192 | { |
| 193 | //char *cmd = NULL; |
| 194 | |
| 195 | if (!path) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 196 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]del_file null\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | /* |
| 200 | cmd = (char *)malloc(strlen(path) + 10); |
| 201 | //char cmd[4*ZTE_HTTPSHARE_FILE_NAME_MAX_LEN+1] = {0}; |
| 202 | if (NULL == cmd) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 203 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]del_file malloc error!\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | snprintf(cmd, strlen(path) + 10, "rm -rf \"%s\"", path); |
| 207 | |
| 208 | zte_httpshare_call_system(cmd); |
| 209 | */ |
| 210 | char *cmd[] = { "/bin/rm", "-rf", path, NULL }; |
| 211 | zte_system(cmd); |
| 212 | if (access(path, F_OK) == 0) { |
| 213 | if(remove(path) < 0){ |
| 214 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]remove file %s fail!\n", path); |
| 215 | return 0; |
| 216 | } |
| 217 | if (access(path, F_OK) == 0) { |
| 218 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]delfile %s fail!\n", path); |
| 219 | //free(cmd); |
| 220 | return 0; |
| 221 | } |
| 222 | } |
| 223 | //free(cmd); |
| 224 | slog(MISC_PRINT, SLOG_NORMAL,"[httpshare]delfile %s OK!\n", path); |
| 225 | return 1; |
| 226 | |
| 227 | } |
| 228 | |
| 229 | int zte_umount_dev() |
| 230 | { |
| 231 | char cmd[ZTE_HTTPSHARE_DEFAULT_LEN] = {0}; |
| 232 | char sd_path[20] = {0}; |
| 233 | char *mount_path = "/bin/umount"; |
| 234 | |
| 235 | sc_cfg_get ("cur_tcard_blk", sd_path, sizeof(sd_path)); |
| 236 | if (strlen(sd_path) != 0) { |
| 237 | snprintf(cmd, sizeof(cmd), "%s %s", mount_path, sd_path); |
| 238 | } |
| 239 | else { |
| 240 | snprintf(cmd, sizeof(cmd), "%s %s", mount_path, USB_DEV_SDCARD_PATH); |
| 241 | } |
| 242 | zte_httpshare_call_system(cmd); |
| 243 | |
| 244 | memset(cmd, 0, sizeof(cmd)); |
| 245 | snprintf(cmd, sizeof(cmd), "%s %s", mount_path, USB_DEV_SDCARD_PATH_BACK); |
| 246 | zte_httpshare_call_system(cmd); |
| 247 | |
| 248 | memset(cmd, 0, sizeof(cmd)); |
| 249 | snprintf(cmd, sizeof(cmd), "%s %s", mount_path, SD_CARD_PATH); |
| 250 | zte_httpshare_call_system(cmd); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /*¶ÁдÎļþÏà¹Ø²Ù×÷begin*/ |
| 256 | static int filelength(FILE *fp) |
| 257 | { |
| 258 | int num; |
| 259 | fseek(fp,0,SEEK_END); |
| 260 | num=ftell(fp); |
| 261 | fseek(fp,0,SEEK_SET); |
| 262 | return num; |
| 263 | } |
| 264 | |
| 265 | static int readfile(char *path, char* buf, unsigned len) |
| 266 | { |
| 267 | FILE *fp; |
| 268 | unsigned int length; |
| 269 | size_t read_len = 0; |
| 270 | |
| 271 | if((fp=fopen(path,"r"))==NULL) |
| 272 | { |
| 273 | slog(USBCFGMNG_PRINT,SLOG_ERR, "[usbCfgMng] open file %s error.\n",path); |
| 274 | return -1; |
| 275 | } |
| 276 | length=filelength(fp); |
| 277 | length = length > len? len: length; |
| 278 | //ch=(char *)malloc(length+1); |
| 279 | read_len = fread(buf,length,1,fp); |
| 280 | if(read_len != 1) |
| 281 | { |
| 282 | slog(USBCFGMNG_PRINT,SLOG_ERR, "read len:%d.\n",read_len); |
| 283 | } |
| 284 | |
| 285 | fclose(fp); |
| 286 | *(buf+length)='\0'; |
| 287 | return (int)length; |
| 288 | } |
| 289 | int zte_get_cdrom() |
| 290 | { |
| 291 | char cdrom0[8] = {0}; |
| 292 | char cdrom1[8] = {0}; |
| 293 | int rtv = 0; |
| 294 | int cdrom_state0 = -1; |
| 295 | int cdrom_state1 = -1; |
| 296 | rtv = readfile("/sys/devices/platform/zx29_hsotg.0/gadget/lun0/cdrom", cdrom0, 8); |
| 297 | cdrom_state0 = atoi(cdrom0); |
| 298 | rtv = readfile("/sys/devices/platform/zx29_hsotg.0/gadget/lun1/cdrom", cdrom1, 8); |
| 299 | cdrom_state1 = atoi(cdrom1); |
| 300 | if(cdrom_state0 == 0) //lun0ΪuÅÌ |
| 301 | { |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | if(cdrom_state1 == 0) //lun1ΪuÅÌ |
| 306 | { |
| 307 | return 1; |
| 308 | } |
| 309 | |
| 310 | return -1; |
| 311 | |
| 312 | } |
| 313 | |
| 314 | void zte_mount_usb() |
| 315 | { |
| 316 | int cdrom = -1; |
| 317 | cdrom = zte_get_cdrom(); |
| 318 | |
| 319 | if(cdrom == 0) |
| 320 | { |
| 321 | zte_umount_dev(); |
| 322 | zte_httpshare_call_system_echo("/bin/echo /dev/mmcblk0 > /sys/devices/platform/zx29_hsotg.0/gadget/lun0/file"); |
| 323 | } |
| 324 | else if(cdrom == 1) |
| 325 | { |
| 326 | zte_umount_dev(); |
| 327 | zte_httpshare_call_system_echo("/bin/echo /dev/mmcblk0 > /sys/devices/platform/zx29_hsotg.0/gadget/lun1/file"); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
| 332 | |
| 333 | int zte_httpshare_mount_sd() |
| 334 | { |
| 335 | char *cmd[] = { "/bin/mount", "-t", "vfat",USB_DEV_SDCARD_PATH,SD_CARD_PATH, NULL }; |
| 336 | char *cmd1[] = { "/bin/mount", "-t", "vfat",USB_DEV_SDCARD_PATH_BACK,SD_CARD_PATH, NULL }; |
| 337 | |
| 338 | char sd_path[20] = {0}; |
| 339 | int i = 0; |
| 340 | |
| 341 | zte_umount_dev(); |
| 342 | //ÂÖѯ·ÖÇø1-10 |
| 343 | for (i = 1; i <= 10; i++) { |
| 344 | memset(sd_path, 0, sizeof(sd_path)); |
| 345 | snprintf(sd_path, sizeof(sd_path), "/dev/mmcblk0p%d", i); |
| 346 | cmd[3] = sd_path; |
| 347 | if (0 == zte_system(cmd)) { |
| 348 | sc_cfg_set("cur_tcard_blk", sd_path); |
| 349 | sleep(2); |
| 350 | return 1; |
| 351 | } |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 352 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_mount_sd %s error!\n", sd_path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (0 != zte_system(cmd1)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 356 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_mount_sd %s error!\n", USB_DEV_SDCARD_PATH_BACK); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | sleep(2); |
| 360 | return 1; |
| 361 | } |
| 362 | |
| 363 | int zte_mount_httpshare() |
| 364 | { |
| 365 | char cmd[ZTE_HTTPSHARE_DEFAULT_LEN] = {0}; |
| 366 | |
| 367 | int cdrom = -1; |
| 368 | |
| 369 | cdrom = zte_get_cdrom(); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 370 | slog(MISC_PRINT, SLOG_ERR,"mount_httpshare:cdrom=%d\n",cdrom); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 371 | if(cdrom == 0) |
| 372 | { |
| 373 | memset(cmd, 0, sizeof(cmd)); |
| 374 | zte_httpshare_call_system_echo("/bin/echo NULL > /sys/devices/platform/zx29_hsotg.0/gadget/lun0/file"); |
| 375 | if (-1 == access(SD_CARD_PATH, F_OK)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 376 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]mount_httpshare mmc2 no exist!\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 377 | if(mkdir(SD_CARD_PATH, 0777) < 0) |
| 378 | { |
| 379 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]mkdir(%s) fail!\n", SD_CARD_PATH); |
| 380 | } |
| 381 | return zte_httpshare_mount_sd(); |
| 382 | } else { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 383 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]mount_httpshare %s exist\n", SD_CARD_PATH); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 384 | return zte_httpshare_mount_sd(); |
| 385 | } |
| 386 | } |
| 387 | else if(cdrom == 1) |
| 388 | { |
| 389 | memset(cmd, 0, sizeof(cmd)); |
| 390 | zte_httpshare_call_system_echo("/bin/echo NULL > /sys/devices/platform/zx29_hsotg.0/gadget/lun1/file"); |
| 391 | if (-1 == access(SD_CARD_PATH, F_OK)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 392 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]mount_httpshare mmc2 no exist!\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 393 | if(mkdir(SD_CARD_PATH, 0777) < 0){ |
| 394 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]mkdir fail %s exist\n", SD_CARD_PATH); |
| 395 | } |
| 396 | return zte_httpshare_mount_sd(); |
| 397 | } else { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 398 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]mount_httpshare %s exist\n", SD_CARD_PATH); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 399 | return zte_httpshare_mount_sd(); |
| 400 | } |
| 401 | } |
| 402 | else |
| 403 | return 0; |
| 404 | |
| 405 | } |
| 406 | |
| 407 | |
| 408 | void zte_write_auth_to_web(webs_t wp) |
| 409 | { |
| 410 | |
| 411 | if ((NULL == wp)) { |
| 412 | return; |
| 413 | } |
| 414 | char HTTP_SHARE_WR_AUTH[10] = {0}; |
| 415 | char HTTP_SHARE_FILE[1024] = {0}; |
| 416 | |
| 417 | (void)zte_web_read(NV_HTTPSHARE_WR_AUTH, HTTP_SHARE_WR_AUTH); |
| 418 | (void)zte_web_read(NV_HTTPSHARE_FILE, HTTP_SHARE_FILE); |
| 419 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 420 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]write_auth_to_web: value is [%s]\n", HTTP_SHARE_WR_AUTH); |
| 421 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]write_auth_to_web: value is [%s]\n", HTTP_SHARE_FILE); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 422 | web_feedback_header(wp); |
| 423 | (void)websWrite(wp, T("{\"HTTP_SHARE_WR_AUTH\":\"%s\",\"HTTP_SHARE_FILE\":\"%s\"}"), |
| 424 | HTTP_SHARE_WR_AUTH, HTTP_SHARE_FILE); |
| 425 | } |
| 426 | void zte_write_tcard_name_to_web(webs_t wp) |
| 427 | { |
| 428 | if ((NULL == wp)) { |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | web_feedback_header(wp); |
| 433 | (void)websWrite(wp, T("{\"sd_card_name\":\"%s\"}"), "MicroSD Card"); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | void write_sd_card_total_size(uint32 total_size) |
| 438 | { |
| 439 | char sd_card_total_size[NV_ITEM_VALUE_STRING_LEN] = {0}; |
| 440 | |
| 441 | snprintf(sd_card_total_size, sizeof(sd_card_total_size), "%lu", total_size); |
| 442 | (void)zte_web_write("sd_card_total_size", sd_card_total_size); |
| 443 | } |
| 444 | |
| 445 | void write_sd_card_avi_space_to_nv(uint32 free_size) |
| 446 | { |
| 447 | char sd_card_avi_space[NV_ITEM_VALUE_STRING_LEN] = {0}; |
| 448 | snprintf(sd_card_avi_space, sizeof(sd_card_avi_space), "%lu", free_size); |
| 449 | (void)zte_web_write("sd_card_avi_space", sd_card_avi_space); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | void get_used_space_size() |
| 454 | { |
| 455 | |
| 456 | struct statvfs file_stat; |
| 457 | uint32 total_size = 0; |
| 458 | uint32 b_used = 0 ; |
| 459 | if (zte_check_file_exist(SD_CARD_PATH)) { |
| 460 | (void)statvfs(SD_CARD_PATH, &file_stat); |
| 461 | total_size = file_stat.f_blocks * (file_stat.f_bsize / 512) / 64; |
| 462 | b_used = file_stat.f_bfree * (file_stat.f_bsize / 512) / 64; |
| 463 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]get_used_space_size dirsize = %ld, %ld\n", total_size, b_used); |
| 464 | if (total_size / 1024 / 1024 > 32) { |
| 465 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]get_used_space_size total size abnormal >1024G !!!!\n"); |
| 466 | total_size = total_size / 1024; |
| 467 | } |
| 468 | if (b_used / 1024 / 1024 > 32) { |
| 469 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]get_used_space_size b_used size abnormal >1024G !!!!\n"); |
| 470 | b_used = b_used / 1024; |
| 471 | } |
| 472 | if (total_size < b_used) { |
| 473 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]get_used_space_size sdcard size abnormal : total_size<b_used !!!!\n"); |
| 474 | } |
| 475 | write_sd_card_total_size(total_size); |
| 476 | write_sd_card_avi_space_to_nv(b_used); |
| 477 | } |
| 478 | |
| 479 | } |
| 480 | |
| 481 | |
| 482 | |
| 483 | void zte_write_space_info_to_web(webs_t wp) |
| 484 | { |
| 485 | if ((NULL == wp)) { |
| 486 | return; |
| 487 | } |
| 488 | char sd_card_total_size[NV_ITEM_VALUE_STRING_LEN] = {0}; |
| 489 | char sd_card_avi_space[NV_ITEM_VALUE_STRING_LEN] = {0}; |
| 490 | |
| 491 | (void)zte_web_read("sd_card_total_size", sd_card_total_size); |
| 492 | (void)zte_web_read("sd_card_avi_space", sd_card_avi_space); |
| 493 | |
| 494 | |
| 495 | web_feedback_header(wp); |
| 496 | (void)websWrite(wp, T("{\"sd_card_total_size\":\"%s\",\"sd_card_avi_space\":\"%s\"}"), |
| 497 | sd_card_total_size, sd_card_avi_space); |
| 498 | } |
| 499 | |
| 500 | |
| 501 | /**** *****/ |
| 502 | //check whether the dir exist or not |
| 503 | zte_httpshare_return_e_type zte_httpshare_check_and_creat_dir(char *path) |
| 504 | { |
| 505 | if (!path) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 506 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_check_and_creat_dir: check dir path null.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 507 | return ZTE_HTTPSHARE_FAILURE; |
| 508 | } |
| 509 | if (-1 == access(path, F_OK)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 510 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]httpshare_check_and_creat_dir:%s does not exist,socreate it.\n", ZTE_HTTPSHARE_DB_DIR); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 511 | if (-1 == mkdir(path, 0777)) { /*lint !e1055*/ |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 512 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_check_and_creat_dir:failed to create db dir.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 513 | return ZTE_HTTPSHARE_FAILURE; |
| 514 | } |
| 515 | } |
| 516 | return ZTE_HTTPSHARE_SUCCESS; |
| 517 | } |
| 518 | |
| 519 | zte_httpshare_db_result_e_type zte_httpshare_create_table() |
| 520 | { |
| 521 | zte_httpshare_db_result_e_type result = ZTE_HTTPSHARE_DB_OK; |
| 522 | |
| 523 | //create httpshare table |
| 524 | result = zte_httpshare_db_exec_sql(ZTE_CREATE_TABLE_HTTPSHARE_SQL, NULL, NULL); |
| 525 | |
| 526 | if (ZTE_HTTPSHARE_DB_OK != result) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 527 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_create_table:create httpshare table result is %d\n", result); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 528 | return result; |
| 529 | } |
| 530 | return result; |
| 531 | } |
| 532 | |
| 533 | //based path,check the file wether in Tcard |
| 534 | int zte_check_file_exist(char * path) |
| 535 | { |
| 536 | |
| 537 | if (!path) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 538 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]check_file_exist:file path null.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | if (-1 == access(path, F_OK)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 543 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]check_file_exist path=%s (file not exist)\n", path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 544 | return 0; |
| 545 | |
| 546 | } else { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 547 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]check_file_exist path=%s (file exist)\n", path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 548 | return 1; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | |
| 553 | int zte_check_sdcard_exist() |
| 554 | { |
| 555 | int ret = 0; |
| 556 | |
| 557 | FILE *fd = NULL; |
| 558 | |
| 559 | char size_sd[ZTE_HTTPSHARE_LEN_12] = {0}; |
| 560 | |
| 561 | //system("echo /dev/mmcblk0>/proc/proc_sd/file"); |
| 562 | |
| 563 | if(access("/sys/kernel/debug/mmc1/present", R_OK) != 0) |
| 564 | { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 565 | slog(MISC_PRINT, SLOG_ERR, "[httpshare] check_sdcard_exist file not exist!\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 566 | return ret; |
| 567 | } |
| 568 | |
| 569 | //fd = popen("cat /proc/proc_sd/size","r"); |
| 570 | fd = popen("cat /sys/kernel/debug/mmc1/present", "r"); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 571 | //printf("[httpshare]check_sdcard_exist cat /sys/kernel/debug/mmc1/present\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 572 | |
| 573 | if (fd == NULL) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 574 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]check_sdcard_exist popen file = NULL\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 575 | return ret; |
| 576 | } |
| 577 | if (!feof(fd) && fgets(size_sd, sizeof(size_sd), fd) != NULL) { |
| 578 | ret = atoi(size_sd); |
| 579 | } |
| 580 | |
| 581 | pclose(fd); |
| 582 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 583 | //printf("[httpshare]check_sdcard_exist :get size data = %d\n",ret); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 584 | |
| 585 | return ret; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | // Get current mode from nv |
| 590 | zte_httpshare_current_mode_type zte_httpshare_get_current_mode() |
| 591 | { |
| 592 | char nv_item[ZTE_HTTPSHARE_DEFAULT_LEN] = {0}; |
| 593 | (void)zte_web_read(STR_SDCARD_MODE_OPT, nv_item); |
| 594 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]sdcard_mode_option:%s\n", nv_item); |
| 595 | |
| 596 | if (0 == strcmp("1", nv_item)) { |
| 597 | return ZTE_HTTPSHARE_HTTPSHARE; |
| 598 | } else { |
| 599 | return ZTE_HTTPSHARE_USB; |
| 600 | } |
| 601 | |
| 602 | } |
| 603 | int zte_httpshare_change_current_mode(zte_httpshare_current_mode_type mode) |
| 604 | { |
| 605 | char cmd[ZTE_HTTPSHARE_DEFAULT_LEN] = {0}; |
| 606 | int cdrom = -1; |
| 607 | |
| 608 | if (ZTE_HTTPSHARE_HTTPSHARE == mode) { |
| 609 | if (!zte_mount_httpshare()) { //½«dev½Úµã¹ÒÔØÎª/mnt/jffs2/mmc2 |
| 610 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]change to %d mode faile\n", mode); |
| 611 | (void)zte_web_write(STR_SDCARD_MODE_OPT, "0"); |
| 612 | |
| 613 | cdrom = zte_get_cdrom(); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 614 | slog(MISC_PRINT, SLOG_ERR,"mount_httpshare:cdrom=%d\n",cdrom); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 615 | if(cdrom == 0) |
| 616 | zte_httpshare_call_system_echo("/bin/echo dev/mmcblk0 > /sys/devices/platform/zx29_hsotg.0/gadget/lun0/file"); |
| 617 | else if(cdrom == 1) |
| 618 | zte_httpshare_call_system_echo("/bin/echo dev/mmcblk0 > /sys/devices/platform/zx29_hsotg.0/gadget/lun1/file"); |
| 619 | return ZTE_CHANGE_MODE_ERROR; |
| 620 | } |
| 621 | slog(MISC_PRINT, SLOG_NORMAL,"[httpshare]change to %d mode suc.\n", mode); |
| 622 | (void)zte_web_write(STR_SDCARD_MODE_OPT, "1"); |
| 623 | } else { |
| 624 | zte_httpshare_check_upload_file(); |
| 625 | |
| 626 | zte_mount_usb(); |
| 627 | slog(MISC_PRINT, SLOG_NORMAL,"[httpshare]change to %d mode suc.\n", mode); |
| 628 | |
| 629 | (void)zte_web_write(STR_SDCARD_MODE_OPT, "0"); |
| 630 | } |
| 631 | return ZTE_CHANGE_MODE_OK; |
| 632 | |
| 633 | } |
| 634 | |
| 635 | int zte_init_sdcard_mode() |
| 636 | { |
| 637 | if (zte_check_sdcard_exist() > 0) { |
| 638 | (void)zte_web_write(NV_SD_CARD_STATE, "1"); |
| 639 | |
| 640 | if (ZTE_HTTPSHARE_USB == zte_httpshare_get_current_mode()) { |
| 641 | return zte_httpshare_change_current_mode(ZTE_HTTPSHARE_USB); |
| 642 | } else { |
| 643 | return zte_httpshare_change_current_mode(ZTE_HTTPSHARE_HTTPSHARE); |
| 644 | } |
| 645 | } else { |
| 646 | (void)zte_web_write(NV_SD_CARD_STATE, "0"); |
| 647 | zte_del_file(SD_CARD_PATH); |
| 648 | return ZTE_CHANGE_MODE_ERROR; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | void zte_httpshare_init() |
| 653 | { |
| 654 | |
| 655 | //zte_init_user_list(&pp_header); //creat user nod |
| 656 | zte_init_user_list(); |
| 657 | (void)zte_del_file(ZTE_HTTPSHARE_DB_PATH); |
| 658 | |
| 659 | if (ZTE_HTTPSHARE_SUCCESS != zte_httpshare_check_and_creat_dir(ZTE_HTTPSHARE_DB_DIR)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 660 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_init:httpshare_check_and_creat_dir HTTPSHARE_DB_DIR fail!\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 661 | return; |
| 662 | } |
| 663 | if (ZTE_HTTPSHARE_DB_OK != zte_httpshare_create_table()) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 664 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_init:httpshare_create_table fail!\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 665 | return; |
| 666 | } |
| 667 | |
| 668 | zte_init_sdcard_mode(); |
| 669 | } |
| 670 | |
| 671 | |
| 672 | int sd_card_isExist() |
| 673 | { |
| 674 | char sdcard_state[ZTE_HTTPSHARE_DEFAULT_LEN] = {0}; |
| 675 | (void)zte_web_read(NV_SD_CARD_STATE, sdcard_state); |
| 676 | if (0 == strcmp("0", sdcard_state)) { |
| 677 | printf("[httpshare]sd_card_isExist:sdcard no exist.\n"); |
| 678 | return 0; |
| 679 | } else if (0 == strcmp("1", sdcard_state)) { |
| 680 | return 1; |
| 681 | } else { |
| 682 | printf("[httpshare]sd_card_isExist:sdcard state error.\n"); |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | } |
| 687 | |
| 688 | boolean zte_httpshare_check_patch_inlegal(const char *path_source) |
| 689 | { |
| 690 | char *token = NULL; |
| 691 | |
| 692 | if (0 == strncmp(path_source, HTTPSHARE_PATH_INLEGAL, 8)) { |
| 693 | if (path_source[8] == '/' || path_source[8] == '\0') { |
| 694 | return TRUE; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | if ((token = strchr(path_source, 39)) != NULL) { // 39 means ' |
| 699 | while (*token++ == 32); // 32 means (space) |
| 700 | |
| 701 | switch (*token) { |
| 702 | case 59: // 59 means ; |
| 703 | case 124: // 124 means | |
| 704 | return TRUE; |
| 705 | case 38: // 38 means & |
| 706 | if (*(++token) == 38) |
| 707 | return TRUE; |
| 708 | default: |
| 709 | return FALSE; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | return FALSE; |
| 714 | } |
| 715 | |
| 716 | int zte_httpshare_db_id_cb(void *fvarg, int line, char **zresult, char **lname) |
| 717 | { |
| 718 | if (1 > line) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 719 | printf("[httpshare]httpshare_db_id_cb:record no data.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 720 | return -1; |
| 721 | } |
| 722 | *(int*)fvarg = atoi(zresult[0]); |
| 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | int zte_httpshare_check_record(char *ip) |
| 728 | { |
| 729 | if (!ip) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 730 | printf("[httpshare]httpshare_check_record:para null.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 731 | return -1; |
| 732 | } |
| 733 | |
| 734 | int id = 0; |
| 735 | zte_httpshare_db_result_e_type result = ZTE_HTTPSHARE_DB_OK; |
| 736 | |
| 737 | char sql[ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 40] = {0}; |
| 738 | |
| 739 | snprintf(sql, sizeof(sql), "select id from %s where ip = '%s' ", ZTE_HTTPSHARE_DB_NAME, ip); |
| 740 | |
| 741 | result = zte_httpshare_db_exec_sql(sql, zte_httpshare_db_id_cb, &id); |
| 742 | (void)sleep(2); |
| 743 | if (ZTE_HTTPSHARE_DB_OK != result) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 744 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_check_record:result %d\n", result); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | return id; |
| 748 | |
| 749 | } |
| 750 | |
| 751 | |
| 752 | zte_httpshare_db_result_e_type zte_httpshare_update_record(char *ip, char *path) |
| 753 | { |
| 754 | |
| 755 | if ((!ip) || (!path)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 756 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_update_record:para null.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 757 | return ZTE_HTTPSHARE_DB_ERROR; |
| 758 | } |
| 759 | |
| 760 | zte_httpshare_db_result_e_type result = ZTE_HTTPSHARE_DB_OK; |
| 761 | |
| 762 | char sql[ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 40] = {0}; |
| 763 | snprintf(sql, sizeof(sql), "update %s set path =\"%s\" where ip = \"%s\" ", ZTE_HTTPSHARE_DB_NAME, path, ip); |
| 764 | |
| 765 | result = zte_httpshare_db_exec_sql(sql, NULL, NULL); |
| 766 | if (ZTE_HTTPSHARE_DB_OK != result) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 767 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_update_record:update record result %d\n", result); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 768 | return result; |
| 769 | } |
| 770 | |
| 771 | return result; |
| 772 | } |
| 773 | |
| 774 | int zte_httpshare_getpath_cb(void *fvarg, int line, char **zresult, char **lname) |
| 775 | { |
| 776 | if (1 > line) return -1; |
| 777 | memcpy(fvarg, zresult[0], strlen(zresult[0])); |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | zte_httpshare_db_result_e_type zte_httpshare_get_record(char *ip, char *path) |
| 782 | { |
| 783 | if ((!ip) || (!path)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 784 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_get_record:para null.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 785 | return ZTE_HTTPSHARE_DB_ERROR; |
| 786 | } |
| 787 | zte_httpshare_db_result_e_type result = ZTE_HTTPSHARE_DB_OK; |
| 788 | |
| 789 | char sql[ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 40] = {0}; |
| 790 | snprintf(sql, sizeof(sql), "select path from %s where ip = '%s' ", ZTE_HTTPSHARE_DB_NAME, ip); |
| 791 | |
| 792 | result = zte_httpshare_db_exec_sql(sql, zte_httpshare_getpath_cb, path); |
| 793 | (void)sleep(1); |
| 794 | if (ZTE_HTTPSHARE_DB_OK != result) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 795 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_get_record:update record result %d\n", result); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 796 | return result; |
| 797 | } |
| 798 | |
| 799 | return result; |
| 800 | } |
| 801 | |
| 802 | |
| 803 | zte_httpshare_db_result_e_type zte_httpshare_insert_path_to_db(char * ip, char *path) |
| 804 | { |
| 805 | |
| 806 | if ((!ip) || (!path)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 807 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_insert_path_to_db:para null.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 808 | return ZTE_HTTPSHARE_DB_ERROR; |
| 809 | } |
| 810 | |
| 811 | zte_httpshare_db_result_e_type result = ZTE_HTTPSHARE_DB_OK; |
| 812 | |
| 813 | char sql[ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 40] = {0}; |
| 814 | snprintf(sql, sizeof(sql), "insert into %s (ip,path) values (\"%s\",\"%s\")", ZTE_HTTPSHARE_DB_NAME, ip, path); |
| 815 | |
| 816 | result = zte_httpshare_db_exec_sql(sql, NULL, NULL); |
| 817 | |
| 818 | if (ZTE_HTTPSHARE_DB_OK != result) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 819 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_insert_path_to_db:result %d\n", result); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 820 | return result; |
| 821 | } |
| 822 | return result; |
| 823 | |
| 824 | } |
| 825 | |
| 826 | |
| 827 | void zte_change_file_time(char *path_source, char*new_time) |
| 828 | { |
| 829 | char *ptr = NULL; |
| 830 | int i = 0; |
| 831 | struct utimbuf times; |
| 832 | if ((!new_time) || (!path_source)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 833 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]change_file_time src==NULL\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 834 | |
| 835 | return; |
| 836 | } |
| 837 | |
| 838 | if ((!strlen(path_source)) || (!strlen(new_time))) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 839 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]change_file_time src==empty\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 840 | |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | if (0 == atoi(new_time)) { |
| 845 | times.modtime = 1378807200; //1378807200 2013-09-10 10:00:00 |
| 846 | times.actime = 1378807200; //1631268000 2021-09-10 10:00:00 |
| 847 | } else { |
| 848 | times.modtime = atoi(new_time); |
| 849 | times.actime = atoi(new_time); |
| 850 | } |
| 851 | utime(path_source, ×); |
| 852 | |
| 853 | ptr = path_source + strlen(path_source) - 1; |
| 854 | for (i = strlen(path_source); i > 0; i--) { |
| 855 | if (*ptr == '/') |
| 856 | break; |
| 857 | ptr--; |
| 858 | } |
| 859 | *ptr = '\0'; |
| 860 | utime(path_source, ×); |
| 861 | |
| 862 | return; |
| 863 | } |
| 864 | static void zte_write_file_record_pro(webs_t wp) |
| 865 | { |
| 866 | web_feedback_header(wp); |
| 867 | (void)websWrite(wp, T("{\"result\":")); |
| 868 | (void)websWrite(wp, T("{\"fileInfo\":[")); |
| 869 | |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | struct dirent |
| 874 | { |
| 875 | long d_ino; // inode number Ë÷Òý½ÚµãºÅ |
| 876 | off_t d_off; // offset to this dirent ÔÚĿ¼ÎļþÖÐµÄÆ«ÒÆ |
| 877 | unsigned short d_reclen; // length of this d_name ÎļþÃû³¤ |
| 878 | unsigned char d_type; // the type of d_name ÎļþÀàÐÍ |
| 879 | char d_name [NAME_MAX+1]; /* file name (null-terminated) ÎļþÃû£¬×256×Ö·û |
| 880 | } |
| 881 | */ |
| 882 | |
| 883 | int zte_write_filerecord_alphasort(webs_t wp, char*path) |
| 884 | { |
| 885 | struct dirent **namelist; |
| 886 | int n = 0; |
| 887 | int file_count = 0; |
| 888 | n = scandir(path, &namelist, 0, alphasort); |
| 889 | zte_write_file_record_pro(wp); |
| 890 | if (n < 3) { |
| 891 | (void)websWrite(wp, T("],\"totalRecord\":\"%d\"}}"), file_count); |
| 892 | if (n <= 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 893 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]write_filerecord_alphasort: scandir n=%d.\n", n); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 894 | free(namelist); |
| 895 | return 1; |
| 896 | } |
| 897 | while (n--) { |
| 898 | |
| 899 | free(namelist[n]); |
| 900 | } |
| 901 | free(namelist); |
| 902 | return 1; |
| 903 | } |
| 904 | char *file_path = NULL; |
| 905 | file_path = (char*)malloc(ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1); |
| 906 | if (!file_path) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 907 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]write_filerecord_alphasort, malloc error!\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 908 | (void)websWrite(wp, T("],\"totalRecord\":\"%d\"}}"), file_count); |
| 909 | while (n--) { |
| 910 | slog(MISC_PRINT, SLOG_DEBUG,"namelist[%d]:%s\n", n, namelist[n]->d_name); |
| 911 | free(namelist[n]); |
| 912 | } |
| 913 | free(namelist); |
| 914 | return 1; |
| 915 | } |
| 916 | int i = 0; |
| 917 | zte_file_record_s_type record_ptr; |
| 918 | struct stat fileinfo; |
| 919 | for (i = 0; i < n; i++) { |
| 920 | if (!strcmp(namelist[i]->d_name, ".") || !strcmp(namelist[i]->d_name, "..")) { |
| 921 | continue; |
| 922 | } |
| 923 | memset(file_path, 0, ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1); |
| 924 | snprintf(file_path, ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1, "%s/%s", path, namelist[i]->d_name); |
| 925 | |
| 926 | errno = 0; |
| 927 | if (stat(file_path, &fileinfo) < 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 928 | //printf("[httpshare]write_filerecord_alphasort stat < 0\n"); |
| 929 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]write_filerecord_alphasort stat : %s[%s]\n", strerror(errno),file_path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 930 | continue; |
| 931 | } |
| 932 | file_count++; |
| 933 | memset(&record_ptr, 0, sizeof(record_ptr)); |
| 934 | snprintf(record_ptr.fileName, ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1, "%s", namelist[i]->d_name); |
| 935 | snprintf(record_ptr.lastUpdateTime, ZTE_HTTPSHARE_DEFAULT_LEN, "%lu", fileinfo.st_mtime); |
| 936 | if (S_ISDIR(fileinfo.st_mode)) { |
| 937 | snprintf(record_ptr.attribute, ZTE_HTTPSHARE_DEFAULT_LEN, "%s", ZTE_FOLDER_STR); |
| 938 | } else { |
| 939 | snprintf(record_ptr.attribute, ZTE_HTTPSHARE_DEFAULT_LEN, "%s", ZTE_FILE_STR); |
| 940 | record_ptr.size = fileinfo.st_size; |
| 941 | } |
| 942 | |
| 943 | if (file_count == 1) { |
| 944 | (void)websWrite(wp, T("{\"fileName\":\"%s\",\"attribute\":\"%s\",\"size\":\"%u\",\"lastUpdateTime\":\"%s\"}"), |
| 945 | record_ptr.fileName, record_ptr.attribute, record_ptr.size, record_ptr.lastUpdateTime); |
| 946 | } else { |
| 947 | (void)websWrite(wp, T(",{\"fileName\":\"%s\",\"attribute\":\"%s\",\"size\":\"%u\",\"lastUpdateTime\":\"%s\"}"), |
| 948 | record_ptr.fileName, record_ptr.attribute, record_ptr.size, record_ptr.lastUpdateTime); |
| 949 | } |
| 950 | } |
| 951 | (void)websWrite(wp, T("],\"totalRecord\":\"%d\"}}"), file_count); |
| 952 | free(file_path); |
| 953 | file_path = NULL; |
| 954 | while (n--) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 955 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]write_filerecord_alphasort:namelist[%d]->%s\n", n, namelist[n]->d_name); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 956 | free(namelist[n]); |
| 957 | } |
| 958 | free(namelist); |
| 959 | namelist = NULL; |
| 960 | return 0; |
| 961 | |
| 962 | } |
| 963 | |
| 964 | |
| 965 | int zte_write_filerecord_alphasort_page(webs_t wp, char*path, int page_index) |
| 966 | { |
| 967 | struct dirent **namelist; |
| 968 | int n = 0; |
| 969 | int file_count = 0; |
| 970 | |
| 971 | int i = 0; |
| 972 | zte_file_record_s_type record_ptr; |
| 973 | struct stat fileinfo; |
| 974 | char *file_path = NULL; |
| 975 | int begin_index = (page_index - 1) * ZTE_HTTPSHARE_MAX_NUM_SHOW_RECORD; |
| 976 | int end_index = page_index * ZTE_HTTPSHARE_MAX_NUM_SHOW_RECORD - 1; |
| 977 | |
| 978 | n = scandir(path, &namelist, 0, alphasort); |
| 979 | |
| 980 | zte_write_file_record_pro(wp); |
| 981 | if (n < 3) { |
| 982 | (void)websWrite(wp, T("],\"totalRecord\":\"%d\"}}"), file_count); |
| 983 | |
| 984 | if (n <= 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 985 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]write_filerecord_alphasort_page scandir n=%d.\n", n); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 986 | free(namelist); |
| 987 | return 1; |
| 988 | } |
| 989 | while (n--) { |
| 990 | //printf("%s\n",namelist[n]->d_name); |
| 991 | free(namelist[n]); |
| 992 | } |
| 993 | free(namelist); |
| 994 | return 1; |
| 995 | } |
| 996 | file_path = (char*)malloc(ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1); |
| 997 | if (!file_path) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 998 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]write_filerecord_alphasort_page:list file memory fail!\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 999 | (void)websWrite(wp, T("],\"totalRecord\":\"%d\"}}"), file_count); |
| 1000 | while (n--) free(namelist[n]); |
| 1001 | free(namelist); |
| 1002 | return 1; |
| 1003 | } |
| 1004 | |
| 1005 | for (i = 0; i < n; i++) { |
| 1006 | if (!strcmp(namelist[i]->d_name, ".") || !strcmp(namelist[i]->d_name, "..")) { |
| 1007 | if (i <= begin_index) { |
| 1008 | begin_index += 1; |
| 1009 | end_index += 1; |
| 1010 | } else { |
| 1011 | end_index += 1; |
| 1012 | } |
| 1013 | continue; |
| 1014 | } |
| 1015 | |
| 1016 | if ((i < begin_index))continue; |
| 1017 | |
| 1018 | if (i > end_index)break; |
| 1019 | |
| 1020 | memset(file_path, 0, ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1); |
| 1021 | snprintf(file_path, ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1, "%s/%s", path, namelist[i]->d_name); |
| 1022 | if (stat(file_path, &fileinfo) < 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1023 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]write_filerecord_alphasort_page stat %s[%s]\n", strerror(errno),file_path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1024 | continue; |
| 1025 | } |
| 1026 | |
| 1027 | file_count++; |
| 1028 | memset(&record_ptr, 0, sizeof(record_ptr)); |
| 1029 | snprintf(record_ptr.fileName, ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1, "%s", namelist[i]->d_name); |
| 1030 | snprintf(record_ptr.lastUpdateTime, ZTE_HTTPSHARE_DEFAULT_LEN, "%lu", fileinfo.st_mtime); |
| 1031 | if (S_ISDIR(fileinfo.st_mode)) { |
| 1032 | snprintf(record_ptr.attribute, ZTE_HTTPSHARE_DEFAULT_LEN, "%s", ZTE_FOLDER_STR); |
| 1033 | } else { |
| 1034 | snprintf(record_ptr.attribute, ZTE_HTTPSHARE_DEFAULT_LEN, "%s", ZTE_FILE_STR); |
| 1035 | record_ptr.size = fileinfo.st_size; |
| 1036 | } |
| 1037 | if (file_count == 1) { |
| 1038 | (void)websWrite(wp, T("{\"fileName\":\"%s\",\"attribute\":\"%s\",\"size\":\"%u\",\"lastUpdateTime\":\"%s\"}"), |
| 1039 | record_ptr.fileName, record_ptr.attribute, record_ptr.size, record_ptr.lastUpdateTime); |
| 1040 | } else { |
| 1041 | (void)websWrite(wp, T(",{\"fileName\":\"%s\",\"attribute\":\"%s\",\"size\":\"%u\",\"lastUpdateTime\":\"%s\"}"), |
| 1042 | record_ptr.fileName, record_ptr.attribute, record_ptr.size, record_ptr.lastUpdateTime); |
| 1043 | } |
| 1044 | } |
| 1045 | (void)websWrite(wp, T("],\"totalRecord\":\"%d\"}}"), n - 2); |
| 1046 | free(file_path); |
| 1047 | file_path = NULL; |
| 1048 | while (n--) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1049 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]write_filerecord_alphasort_page:namelist[%d]->%s\n", n, namelist[n]->d_name); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1050 | free(namelist[n]); |
| 1051 | } |
| 1052 | free(namelist); |
| 1053 | namelist = NULL; |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | // creat a new document |
| 1060 | int zte_create_document(char* path) |
| 1061 | { |
| 1062 | if (!path) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1063 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]create_document:path is null\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1064 | return 0; |
| 1065 | } |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1066 | slog(MISC_PRINT, SLOG_NORMAL,"[httpshare]create_document:create new folder->%s\n", path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1067 | |
| 1068 | if (-1 == mkdir(path, 0777)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1069 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]create_documentcreate new folder->%s failed\n", path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | (void)zte_httpshare_call_system("/bin/sync"); |
| 1074 | |
| 1075 | return 1; |
| 1076 | |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | int zte_del_multi_file_record(char* path, char* path1) //0ʧ°Ü£¬1 ³É¹¦£¬2ÓÐÏÂÔØ¼Ç¼ |
| 1081 | { |
| 1082 | int del_res = 1; |
| 1083 | char *p = path1; |
| 1084 | int i = 0; |
| 1085 | char *absolute_path = NULL; |
| 1086 | char *name = NULL; |
| 1087 | absolute_path = (char*)malloc(ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1); |
| 1088 | if (!absolute_path) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1089 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]del_multi_file_record:multi del abusolute path malloc fail.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1090 | return 0; |
| 1091 | } |
| 1092 | name = (char*)malloc(strlen(path1) + 1); |
| 1093 | if (!name) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1094 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]del_multi_file_record:multi del name malloc fail.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1095 | free(absolute_path); |
| 1096 | absolute_path = NULL; |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | memset(absolute_path, 0, ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1); |
| 1101 | memset(name, 0, strlen(path1) + 1); |
| 1102 | |
| 1103 | for (p = path1; *p != '\0'; p++) { |
| 1104 | if (*p != '*') { |
| 1105 | name[i] = *p; |
| 1106 | i++; |
| 1107 | } else { |
| 1108 | //·¾¶Ãû³Æ³¤¶È<4096 ; ÎļþÃû³¤¶È<255 |
| 1109 | if ((strlen(name) > 255) || ((strlen(name) + strlen(path)) > 4095)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1110 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]del_multi_file_record:filename/path too long\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1111 | |
| 1112 | free(name); |
| 1113 | free(absolute_path); |
| 1114 | name = absolute_path = NULL; |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | snprintf(absolute_path, ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1, "%s/%s", path, name); |
| 1119 | |
| 1120 | // added by fenglei for security begin 20141029 |
| 1121 | if (zte_httpshare_check_patch_inlegal(absolute_path)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1122 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]del_multi_file_record:check del path is legal or not %s\n", absolute_path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1123 | free(name); |
| 1124 | free(absolute_path); |
| 1125 | name = absolute_path = NULL; |
| 1126 | return 0; |
| 1127 | } |
| 1128 | // added by fenglei for security end 20141029 |
| 1129 | int check_result = -1; |
| 1130 | if (0 == (check_result = zte_check_download_file(absolute_path))) { //ÎÞÏÂÔØ¼Ç¼ |
| 1131 | (void)zte_del_file(absolute_path); |
| 1132 | } |
| 1133 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1134 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]del_multi_file_record:check result->%d.\n", check_result); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1135 | |
| 1136 | if (access(absolute_path, F_OK) == 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1137 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]del_multi_file_record:del file fail.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1138 | if (0 == check_result) { //²»´æÔڼǼ£¬É¾³ýʧ°Ü£¬·µ»ØÊ§°Ü |
| 1139 | del_res = 0; |
| 1140 | } else { |
| 1141 | del_res = 2; //´æÔڼǼ£¬É¾³ýʧ°Ü£¬·µ»Øprocessing |
| 1142 | } |
| 1143 | free(name); |
| 1144 | free(absolute_path); |
| 1145 | name = absolute_path = NULL; |
| 1146 | return del_res; |
| 1147 | } else { |
| 1148 | zte_httpshare_call_system("/bin/sync"); //remove file from sdcard on time |
| 1149 | } |
| 1150 | memset(absolute_path, 0, ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1); |
| 1151 | memset(name, 0, strlen(path1) + 1); |
| 1152 | i = 0; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | free(name); |
| 1157 | free(absolute_path); |
| 1158 | name = absolute_path = NULL; |
| 1159 | return del_res; |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | /******************* |
| 1165 | |
| 1166 | GOFORM ÒµÎñÏà¹Ø start |
| 1167 | |
| 1168 | *******************/ |
| 1169 | |
| 1170 | |
| 1171 | void zte_httpShare_auth_get(webs_t wp) |
| 1172 | { |
| 1173 | int status = 0; |
| 1174 | status = sd_card_isExist(); |
| 1175 | |
| 1176 | if (0 == status) { |
| 1177 | zte_write_result_to_web(wp, NO_SDCARD); |
| 1178 | return; |
| 1179 | } |
| 1180 | zte_write_auth_to_web(wp); |
| 1181 | } |
| 1182 | |
| 1183 | void zte_httpShare_getcard_name(webs_t wp) |
| 1184 | { |
| 1185 | int status = 0; |
| 1186 | status = sd_card_isExist(); |
| 1187 | if (0 == status) { |
| 1188 | zte_write_result_to_web(wp, NO_SDCARD); |
| 1189 | return; |
| 1190 | } |
| 1191 | zte_write_tcard_name_to_web(wp); |
| 1192 | |
| 1193 | } |
| 1194 | void zte_httpShare_getcard_value(webs_t wp) |
| 1195 | { |
| 1196 | if (!sd_card_isExist()) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1197 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_getcard_value no sdcard.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1198 | zte_write_result_to_web(wp, NO_SDCARD); |
| 1199 | return; |
| 1200 | } |
| 1201 | get_used_space_size(); |
| 1202 | |
| 1203 | zte_write_space_info_to_web(wp); |
| 1204 | } |
| 1205 | |
| 1206 | |
| 1207 | int zte_httpShare_chage_to_mode(webs_t wp, char* mode) |
| 1208 | { |
| 1209 | int set_mode = -1; |
| 1210 | if (!strcmp("http_share_mode", mode)) { |
| 1211 | set_mode = ZTE_HTTPSHARE_HTTPSHARE; |
| 1212 | } else { |
| 1213 | set_mode = ZTE_HTTPSHARE_USB; |
| 1214 | } |
| 1215 | if (set_mode == zte_httpshare_get_current_mode()) { |
| 1216 | zte_write_result_to_web(wp, SUCCESS); |
| 1217 | } else { |
| 1218 | if (zte_httpshare_change_current_mode(set_mode)) { |
| 1219 | zte_write_result_to_web(wp, SUCCESS); |
| 1220 | } else { |
| 1221 | zte_write_result_to_web(wp, FAILURE); |
| 1222 | } |
| 1223 | } |
| 1224 | return 1; |
| 1225 | } |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | /********************************************************************** |
| 1230 | * Function: zte_httpShare_modeset |
| 1231 | * Description: |
| 1232 | * Input: NULL |
| 1233 | * Output: NULL |
| 1234 | * Return: NULL |
| 1235 | * Others: |
| 1236 | * Modify Date Version Author Modification |
| 1237 | * ----------------------------------------------- |
| 1238 | * 2012/09/12 V1.0 |
| 1239 | **********************************************************************/ |
| 1240 | void zte_httpShare_modeset(webs_t wp) |
| 1241 | { |
| 1242 | char *mode = websGetVar(wp, "mode_set", T("")); |
| 1243 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1244 | //printf("[httpshare]httpShare_modeset:set to mode->%s\n", mode); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1245 | |
| 1246 | if (!sd_card_isExist()) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1247 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_modeset:no sdcard.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1248 | zte_write_result_to_web(wp, NO_SDCARD); |
| 1249 | return; |
| 1250 | } |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1251 | //printf("[httpshare]httpShare_modeset:sdcard exist.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1252 | if ((0 != strcmp("http_share_mode", mode)) && (0 != strcmp("usb_mode", mode))) { |
| 1253 | zte_write_result_to_web(wp, FAILURE); |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | if (zte_check_downloading_file() > 0) { |
| 1258 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]downloading file,try later.\n"); |
| 1259 | zte_write_result_to_web(wp, PROCESSING); |
| 1260 | return; |
| 1261 | } |
| 1262 | zte_httpShare_chage_to_mode(wp, mode); |
| 1263 | |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | /********************************************************************** |
| 1269 | * Function: zte_httpShare_enterFold |
| 1270 | * Description: |
| 1271 | * Input: NULL |
| 1272 | * Output: NULL |
| 1273 | * Return: NULL |
| 1274 | * Others: |
| 1275 | * Modify Date Version Author Modification |
| 1276 | * ----------------------------------------------- |
| 1277 | * 2012/09/12 V1.0 |
| 1278 | **********************************************************************/ |
| 1279 | void zte_httpShare_enterFold(webs_t wp) |
| 1280 | { |
| 1281 | char *path_source = NULL; |
| 1282 | char *path_web_tmp = websGetVar(wp, "path_SD_CARD", T("")); |
| 1283 | |
| 1284 | int path_source_len = 0; |
| 1285 | |
| 1286 | char *path_web = neutralize(path_web_tmp); |
| 1287 | if(path_web == NULL){ |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1288 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_enterFold fail %s.\n", path_web_tmp); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1289 | zte_write_result_to_web(wp, FAILURE); |
| 1290 | return; |
| 1291 | } |
| 1292 | path_source_len = strlen(path_web) + 30; |
| 1293 | |
| 1294 | // path_source = (char *)malloc(strlen(path_web)+5); |
| 1295 | path_source = (char *)malloc(path_source_len); //²âÊÔ:SD_CARD_PATH_PR=/mnt/jffs2 |
| 1296 | |
| 1297 | if (!path_source) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1298 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_enterFold malloc fail\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1299 | zte_write_result_to_web(wp, FAILURE); |
| 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | int page_index = 0; |
| 1304 | char* index_page = websGetVar(wp, "indexPage", T("")); |
| 1305 | |
| 1306 | page_index = atoi(index_page); |
| 1307 | if(page_index < 0 || page_index > INT_MAX-1) |
| 1308 | { |
| 1309 | page_index = 1; |
| 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | char * ip = websGetRequestIpaddr(wp); |
| 1314 | memset(path_source, 0, path_source_len); |
| 1315 | snprintf(path_source, path_source_len, "%s%s", SD_CARD_PATH_PR, path_web); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1316 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]httpShare_enterFold: path->%s, page_index->%d, ip->%s.\n", path_source, page_index, ip); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1317 | |
| 1318 | if (!sd_card_isExist()) { |
| 1319 | zte_write_result_to_web(wp, NO_SDCARD); |
| 1320 | goto end; |
| 1321 | } |
| 1322 | |
| 1323 | if (!zte_check_file_exist(path_source)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1324 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_enterFold path inexist\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1325 | zte_write_result_to_web(wp, FAILURE); |
| 1326 | goto end; |
| 1327 | //return; |
| 1328 | } |
| 1329 | //added by fenglei for security begin 20141029 |
| 1330 | if (zte_httpshare_check_patch_inlegal(path_source)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1331 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_enterFold path %s inlegal\n", path_source); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1332 | zte_write_result_to_web(wp, FAILURE); |
| 1333 | goto end; |
| 1334 | } |
| 1335 | //added by fenglei for security end 20141029 |
| 1336 | if (zte_httpshare_check_record(ip)) { |
| 1337 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]db have record \n"); |
| 1338 | (void)zte_httpshare_update_record(ip, path_source); |
| 1339 | } else { |
| 1340 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]it is a new user update httpshare db \n"); |
| 1341 | (void)zte_httpshare_insert_path_to_db(ip, path_source); |
| 1342 | } |
| 1343 | |
| 1344 | zte_httpshare_check_upload_file(); |
| 1345 | |
| 1346 | if ('\0' == (*index_page)) { //ÎļþСÓÚ10¸ö£¬ÔÚÒ»Ò³ÖÐÏÔʾ |
| 1347 | (void)zte_write_filerecord_alphasort(wp, path_source); |
| 1348 | } else { //Îļþ½Ï¶à£¬·ÖÒ³ÏÔʾ£¬Ã¿Ò³×î¶à10¸ö |
| 1349 | (void)zte_write_filerecord_alphasort_page(wp, path_source, page_index); |
| 1350 | } |
| 1351 | |
| 1352 | end: |
| 1353 | free(path_source); |
| 1354 | path_source = NULL; |
| 1355 | return; |
| 1356 | } |
| 1357 | |
| 1358 | |
| 1359 | /********************************************************************** |
| 1360 | * Function: zte_httpShare_new |
| 1361 | * Description: |
| 1362 | * Input: NULL |
| 1363 | * Output: NULL |
| 1364 | * Return: NULL |
| 1365 | * Others: |
| 1366 | * Modify Date Version Author Modification |
| 1367 | * ----------------------------------------------- |
| 1368 | * 2012/09/12 V1.0 |
| 1369 | **********************************************************************/ |
| 1370 | |
| 1371 | void zte_httpShare_new(webs_t wp) |
| 1372 | { |
| 1373 | |
| 1374 | int new_ret = 0; |
| 1375 | char *path_web_tmp = websGetVar(wp, "path_SD_CARD", T("")); |
| 1376 | char *new_time = websGetVar(wp, ZTE_HTTPSHARE_TIME, T("")); |
| 1377 | char *path_source = NULL; |
| 1378 | char *path_web = neutralize(path_web_tmp); |
| 1379 | if(path_web == NULL){ |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1380 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_new fail %s.\n", path_web_tmp); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1381 | zte_write_result_to_web(wp, FAILURE); |
| 1382 | return; |
| 1383 | } |
| 1384 | int path_source_len = strlen(path_web) + 30; |
| 1385 | |
| 1386 | // path_source = (char *)malloc(strlen(path_web)+5); |
| 1387 | path_source = (char *)malloc(path_source_len); |
| 1388 | |
| 1389 | if (!path_source) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1390 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]httpShare_new malloc fail\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1391 | zte_write_result_to_web(wp, FAILURE); |
| 1392 | return; |
| 1393 | } |
| 1394 | memset(path_source, 0, path_source_len); |
| 1395 | snprintf(path_source, path_source_len, "%s%s", SD_CARD_PATH_PR, path_web); |
| 1396 | if (!sd_card_isExist()) { |
| 1397 | zte_write_result_to_web(wp, NO_SDCARD); |
| 1398 | goto end; |
| 1399 | } |
| 1400 | //added by fenglei for security begin 20141029 |
| 1401 | if (zte_httpshare_check_patch_inlegal(path_source) || |
| 1402 | zte_httpshare_check_patch_inlegal(new_time)) { |
| 1403 | zte_write_result_to_web(wp, FAILURE); |
| 1404 | goto end; |
| 1405 | } |
| 1406 | //added by fenglei for security end 20141029 |
| 1407 | if (zte_check_file_exist(path_source)) { |
| 1408 | zte_write_result_to_web(wp, EXIST); |
| 1409 | goto end; |
| 1410 | } |
| 1411 | new_ret = zte_create_document(path_source); |
| 1412 | |
| 1413 | if (!new_ret) { |
| 1414 | zte_write_result_to_web(wp, FAILURE); |
| 1415 | } else { |
| 1416 | zte_change_file_time(path_source, new_time); |
| 1417 | zte_httpshare_call_system("/bin/sync"); |
| 1418 | zte_write_result_to_web(wp, SUCCESS); |
| 1419 | } |
| 1420 | |
| 1421 | end: |
| 1422 | free(path_source); |
| 1423 | path_source = NULL; |
| 1424 | return; |
| 1425 | |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | /********************************************************************** |
| 1432 | * Function: zte_httpShare_del |
| 1433 | * Description: |
| 1434 | * Input: NULL |
| 1435 | * Output: NULL |
| 1436 | * Return: NULL |
| 1437 | * Others: |
| 1438 | * Modify Date Version Author Modification |
| 1439 | * ----------------------------------------------- |
| 1440 | * 2012/09/12 V1.0 |
| 1441 | **********************************************************************/ |
| 1442 | |
| 1443 | void zte_httpShare_del(webs_t wp) |
| 1444 | { |
| 1445 | char *root_web_tmp = websGetVar(wp, "path_SD_CARD", T("")); |
| 1446 | char *del_path = websGetVar(wp, "name_SD_CARD", T("")); |
| 1447 | char *root_path = NULL; |
| 1448 | |
| 1449 | char *root_web = neutralize(root_web_tmp); |
| 1450 | if(root_web == NULL){ |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1451 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_del fail %s.\n", root_web_tmp); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1452 | zte_write_result_to_web(wp, FAILURE); |
| 1453 | return; |
| 1454 | } |
| 1455 | int root_path_len = strlen(root_web) + 30; |
| 1456 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1457 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]httpShare_del:dele_path->%s, name->%s.\n", root_web, del_path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1458 | |
| 1459 | // root_path = (char *)malloc(strlen(root_web)+5); |
| 1460 | root_path = (char *)malloc(root_path_len); |
| 1461 | if (!root_path) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1462 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_del malloc fail.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1463 | zte_write_result_to_web(wp, FAILURE); |
| 1464 | return; |
| 1465 | } |
| 1466 | memset(root_path, 0, root_path_len); |
| 1467 | snprintf(root_path, root_path_len, "%s%s", SD_CARD_PATH_PR, root_web); |
| 1468 | if (!sd_card_isExist()) { |
| 1469 | zte_write_result_to_web(wp, NO_SDCARD); |
| 1470 | goto end; |
| 1471 | } |
| 1472 | //added by fenglei for security begin 20141029 |
| 1473 | if (zte_httpshare_check_patch_inlegal(del_path)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1474 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_del path is inlegal %s\n", del_path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1475 | zte_write_result_to_web(wp, FAILURE); |
| 1476 | goto end; |
| 1477 | } |
| 1478 | //added by fenglei for security end 20141029 |
| 1479 | switch (zte_del_multi_file_record(root_path, del_path)) { |
| 1480 | case 0://ʧ°Ü |
| 1481 | zte_write_result_to_web(wp, FAILURE); |
| 1482 | break; |
| 1483 | case 1://³É¹¦ |
| 1484 | zte_write_result_to_web(wp, SUCCESS); |
| 1485 | break; |
| 1486 | case 2://ÓÐÏÂÔØ¼Ç¼ |
| 1487 | zte_write_result_to_web(wp, PROCESSING); |
| 1488 | break; |
| 1489 | default: |
| 1490 | break; |
| 1491 | } |
| 1492 | |
| 1493 | end: |
| 1494 | free(root_path); |
| 1495 | root_path = NULL; |
| 1496 | return; |
| 1497 | } |
| 1498 | |
| 1499 | |
| 1500 | /********************************************************************** |
| 1501 | * Function: zte_httpShare_auth_set |
| 1502 | * Description: |
| 1503 | * Input: NULL |
| 1504 | * Output: NULL |
| 1505 | * Return: NULL |
| 1506 | * Others: |
| 1507 | * Modify Date Version Author Modification |
| 1508 | * ----------------------------------------------- |
| 1509 | * 2012/09/12 V1.0 |
| 1510 | **********************************************************************/ |
| 1511 | |
| 1512 | void zte_httpShare_auth_set(webs_t wp) |
| 1513 | { |
| 1514 | char *rw_auth = websGetVar(wp, "HTTP_SHARE_WR_AUTH", T("")); |
| 1515 | char *file_name = websGetVar(wp, "HTTP_SHARE_FILE", T("")); |
| 1516 | char *httpshare_status = websGetVar(wp, NV_HTTPSHARE_STATUS, T("")); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1517 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]httpShare_auth_set:HTTP_SHARE_STATUS->%s, HTTP_SHARE_WR_AUTH->%s, HTTP_SHARE_FILE->%s\n", httpshare_status, rw_auth, file_name); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1518 | |
| 1519 | if (!strcmp(httpshare_status, "Disabled")) { |
| 1520 | (void)zte_web_write(NV_HTTPSHARE_STATUS, httpshare_status); |
| 1521 | } else { |
| 1522 | (void)zte_web_write(NV_HTTPSHARE_WR_AUTH, rw_auth); |
| 1523 | (void)zte_web_write(NV_HTTPSHARE_FILE, file_name); |
| 1524 | (void)zte_web_write(NV_HTTPSHARE_STATUS, httpshare_status); |
| 1525 | } |
| 1526 | |
| 1527 | zte_write_result_to_web(wp, SUCCESS); |
| 1528 | return; |
| 1529 | } |
| 1530 | |
| 1531 | |
| 1532 | /********************************************************************** |
| 1533 | * Function: zte_httpShare_rename |
| 1534 | * Description: |
| 1535 | * Input: NULL |
| 1536 | * Output: NULL |
| 1537 | * Return: NULL |
| 1538 | * Others: |
| 1539 | * Modify Date Version Author Modification |
| 1540 | * ----------------------------------------------- |
| 1541 | * 2012/09/12 V1.0 |
| 1542 | **********************************************************************/ |
| 1543 | |
| 1544 | void zte_httpShare_rename(webs_t wp) |
| 1545 | { |
| 1546 | |
| 1547 | char *old_file_name = NULL; |
| 1548 | char *new_file_name = NULL; |
| 1549 | char *old_file_web_tmp = websGetVar(wp, "OLD_NAME_SD_CARD", T("")); |
| 1550 | char *new_file_web_tmp = websGetVar(wp, "NEW_NAME_SD_CARD", T("")); |
| 1551 | |
| 1552 | char *old_file_web = neutralize(old_file_web_tmp); |
| 1553 | char *new_file_web = neutralize(new_file_web_tmp); |
| 1554 | |
| 1555 | int check_result = -1; |
| 1556 | int fd = -1; |
| 1557 | |
| 1558 | if(old_file_web == NULL || new_file_web == NULL){ |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1559 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_rename fail %s %s.\n", old_file_web_tmp, new_file_web_tmp); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1560 | zte_write_result_to_web(wp, FAILURE); |
| 1561 | return; |
| 1562 | } |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1563 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]httpShare_rename:old name->%s\n", old_file_web); |
| 1564 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]httpShare_rename:new name->%s\n", new_file_web); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1565 | // old_file_name= (char *)malloc(strlen(old_file_web)+5); |
| 1566 | old_file_name = (char *)malloc(strlen(old_file_web) + 30); |
| 1567 | |
| 1568 | if (!old_file_name) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1569 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_rename malloc fail\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1570 | zte_write_result_to_web(wp, FAILURE); |
| 1571 | return; |
| 1572 | } |
| 1573 | new_file_name = (char *)malloc(strlen(new_file_web) + 30); |
| 1574 | if (!new_file_name) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1575 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_rename malloc fail\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1576 | zte_write_result_to_web(wp, FAILURE); |
| 1577 | goto end2; |
| 1578 | } |
| 1579 | memset(old_file_name, 0, strlen(old_file_web) + 30); |
| 1580 | memset(new_file_name, 0, strlen(new_file_web) + 30); |
| 1581 | snprintf(old_file_name, strlen(old_file_web) + 30, "%s%s", SD_CARD_PATH_PR, old_file_web); |
| 1582 | snprintf(new_file_name, strlen(new_file_web) + 30, "%s%s", SD_CARD_PATH_PR, new_file_web); |
| 1583 | //added by fenglei for security begin 20141029 |
| 1584 | if (zte_httpshare_check_patch_inlegal(old_file_name) || |
| 1585 | zte_httpshare_check_patch_inlegal(new_file_name)) { |
| 1586 | zte_write_result_to_web(wp, FAILURE); |
| 1587 | goto end; |
| 1588 | } |
| 1589 | if (access(old_file_name, F_OK) != 0) { |
| 1590 | zte_write_result_to_web(wp, NOEXIST); |
| 1591 | goto end; |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | if ((check_result = zte_check_download_file(old_file_name))) { //ÎÞÏÂÔØ¼Ç¼ |
| 1596 | zte_write_result_to_web(wp, PROCESSING); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1597 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_rename:path is using\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1598 | goto end; |
| 1599 | } |
| 1600 | //added by fenglei for security end 20141029 |
| 1601 | fd = rename(old_file_name, new_file_name); |
| 1602 | |
| 1603 | if (fd < 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1604 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_rename:rename fail fd->%d\n", fd); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1605 | zte_write_result_to_web(wp, FAILURE); |
| 1606 | } else { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1607 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_rename:rename success, fd->%d\n", fd); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1608 | zte_httpshare_call_system("/bin/sync"); //write new file from momeroy to sdcard on time |
| 1609 | zte_write_result_to_web(wp, SUCCESS); |
| 1610 | } |
| 1611 | |
| 1612 | end: |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1613 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]httpShare_rename:end2\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1614 | free(new_file_name); |
| 1615 | new_file_name = NULL; |
| 1616 | |
| 1617 | end2: |
| 1618 | free(old_file_name); |
| 1619 | old_file_name = NULL; |
| 1620 | |
| 1621 | return; |
| 1622 | } |
| 1623 | |
| 1624 | |
| 1625 | /********************************************************************** |
| 1626 | * Function: zte_httpShare_enterFold |
| 1627 | * Description: |
| 1628 | * Input: NULL |
| 1629 | * Output: NULL |
| 1630 | * Return: NULL |
| 1631 | * Others: |
| 1632 | * Modify Date Version Author Modification |
| 1633 | * ----------------------------------------------- |
| 1634 | * 2012/09/12 V1.0 |
| 1635 | **********************************************************************/ |
| 1636 | |
| 1637 | void zte_httpShare_check_file(webs_t wp) |
| 1638 | { |
| 1639 | char *path_web_tmp = websGetVar(wp, "path_SD_CARD", T("")); |
| 1640 | char *path_source = NULL; |
| 1641 | char *path_web = neutralize(path_web_tmp); |
| 1642 | if(path_web == NULL){ |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1643 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_check_file fail %s.\n", path_web_tmp); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1644 | zte_write_result_to_web(wp, FAILURE); |
| 1645 | return; |
| 1646 | } |
| 1647 | int path_source_len = strlen(path_web) + 30; |
| 1648 | |
| 1649 | if (!sd_card_isExist()) { |
| 1650 | zte_write_result_to_web(wp, NO_SDCARD); |
| 1651 | return; |
| 1652 | } |
| 1653 | path_source = (char *)malloc(path_source_len); |
| 1654 | if (!path_source) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1655 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpShare_check_file malloc fail\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1656 | zte_write_result_to_web(wp, FAILURE); |
| 1657 | return; |
| 1658 | } |
| 1659 | memset(path_source, 0, path_source_len); |
| 1660 | snprintf(path_source, path_source_len, "%s%s", SD_CARD_PATH_PR, path_web); |
| 1661 | |
| 1662 | if (zte_check_file_exist(path_source)) { |
| 1663 | zte_write_result_to_web(wp, EXIST); //ÎļþÒÑ´æÔÚ |
| 1664 | } else { //Îļþ²»´æÔÚ |
| 1665 | if (zte_check_downloading_file() > 0) { |
| 1666 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]downloading file,try later.\n"); |
| 1667 | zte_write_result_to_web(wp, PROCESSING); |
| 1668 | } else { |
| 1669 | zte_write_result_to_web(wp, NOEXIST); |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | free(path_source); |
| 1674 | path_source = NULL; |
| 1675 | |
| 1676 | return; |
| 1677 | } |
| 1678 | |
| 1679 | |
| 1680 | #if 0 |
| 1681 | USER_COMMON_INFOR* zte_user_list_insert() |
| 1682 | { |
| 1683 | USER_COMMON_INFOR* ptr = NULL; |
| 1684 | USER_COMMON_INFOR* index_ptr = NULL; |
| 1685 | index_ptr = &pp_header; |
| 1686 | |
| 1687 | if (!ptr) { |
| 1688 | ptr = (USER_COMMON_INFOR*)malloc(sizeof(USER_COMMON_INFOR)); |
| 1689 | } |
| 1690 | |
| 1691 | if (!ptr) { |
| 1692 | printf("[httpshare]creat user node fail.\n"); |
| 1693 | |
| 1694 | return NULL; |
| 1695 | } else { |
| 1696 | while ((index_ptr->next) != NULL) { |
| 1697 | index_ptr = index_ptr->next; |
| 1698 | } |
| 1699 | index_ptr->next = ptr; |
| 1700 | ptr->next = NULL; |
| 1701 | pp_header.cnt++; |
| 1702 | return ptr; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | USER_COMMON_INFOR* zte_get_new_user(webs_t wp) |
| 1709 | { |
| 1710 | |
| 1711 | USER_COMMON_INFOR* pp_current = NULL; |
| 1712 | pp_current = zte_user_list_insert(); |
| 1713 | |
| 1714 | if (NULL != pp_current) { |
| 1715 | printf("[httpshare]creat new user node.\n"); |
| 1716 | memset(pp_current, 0, sizeof(USER_COMMON_INFOR)); |
| 1717 | return pp_current; |
| 1718 | } else { |
| 1719 | printf("[httpshare]creat new user node faile.\n"); |
| 1720 | return NULL; |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | |
| 1725 | |
| 1726 | USER_COMMON_INFOR* zte_get_user_position(webs_t wp) |
| 1727 | { |
| 1728 | USER_COMMON_INFOR *pp_current = NULL; |
| 1729 | pp_current = pp_header.next; |
| 1730 | |
| 1731 | a_assert(pp_current->address); |
| 1732 | a_assert(websGetRequestIpaddr(wp)); |
| 1733 | |
| 1734 | while ((pp_current != NULL) && (pp_current->address != NULL) && (websGetRequestIpaddr(wp) != NULL)) { |
| 1735 | if ((0 == strcmp((char *)websGetRequestIpaddr(wp), (char *)pp_current->address)) && (pp_current->requst_sid == (void*)websGetSid(wp))) { |
| 1736 | return pp_current; |
| 1737 | } else { |
| 1738 | pp_current = pp_current->next; |
| 1739 | } |
| 1740 | } |
| 1741 | return NULL; |
| 1742 | } |
| 1743 | |
| 1744 | USER_COMMON_INFOR* zte_process_user_auth(webs_t wp) |
| 1745 | { |
| 1746 | |
| 1747 | USER_COMMON_INFOR* ptr = NULL; |
| 1748 | ptr = zte_get_user_position(wp); |
| 1749 | if (NULL == ptr) { |
| 1750 | ptr = zte_get_new_user(wp); |
| 1751 | return ptr; |
| 1752 | } else { |
| 1753 | return ptr; |
| 1754 | } |
| 1755 | } |
| 1756 | #endif |
| 1757 | int zte_malloc_cgi_buff() |
| 1758 | { |
| 1759 | file_buf_memory_ptr = file_buf_memory; |
| 1760 | return 1; |
| 1761 | } |
| 1762 | |
| 1763 | int zte_path_check(webs_t wp)//¼ì²éÉÏ´«ÎļþURLÊÇ·ñºÏ·¨ |
| 1764 | { |
| 1765 | char path_temp[ZTE_HTTPSHARE_FILE_NAME_MAX_LEN] = {0}; |
| 1766 | char *ptr = NULL; |
| 1767 | |
| 1768 | if (wp->url == NULL) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1769 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]path_check error1\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1770 | return 0; |
| 1771 | } |
| 1772 | |
| 1773 | if (strlen(wp->url) == 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1774 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]path_check error2\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1775 | return 0; |
| 1776 | } |
| 1777 | snprintf(path_temp, ZTE_HTTPSHARE_FILE_NAME_MAX_LEN, "%s", wp->url); |
| 1778 | |
| 1779 | ptr = strstr(path_temp, "/cgi-bin/httpshare"); |
| 1780 | ptr += strlen("/cgi-bin/httpshare")+1;//ptrÖ¸ÏòÃû³ÆÊ××Öĸ |
| 1781 | |
| 1782 | if (strstr(ptr, "/") != NULL) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1783 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]path_check error\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1784 | return 0; |
| 1785 | } |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1786 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]path_check upload name:%s\n", ptr); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1787 | return 1; |
| 1788 | } |
| 1789 | |
| 1790 | |
| 1791 | /**³õʼ»¯ÉÏ´«ÎļþÐÅÏ¢**/ |
| 1792 | void zte_init_up_infor(USER_COMMON_INFOR*pp_co_info) |
| 1793 | { |
| 1794 | |
| 1795 | memset(pp_co_info->infor.file_tail_pool, 0, ZTE_PARSE_CGI_TAIL_LEN); |
| 1796 | pp_co_info->infor.file_head = ZTE_CGI_PARSE_FILE_HEAD_BEGIN; |
| 1797 | pp_co_info->infor.file_tail = ZTE_CGI_PARSE_FILE_TAIL_BEGIN; |
| 1798 | pp_co_info->infor.file_tail_pool_len = 0; |
| 1799 | pp_co_info->infor.file_head_len = 0; |
| 1800 | pp_co_info->infor.file_tail_len = 0; |
| 1801 | pp_co_info->infor.data_no = ZTE_CGI_WRITE_THE_FIRST_DATA; |
| 1802 | pp_co_info->infor.file_tail_addres = ZTE_CGI_PARSE_FILE_TAIL_ADD_BEGIN; |
| 1803 | pp_co_info->infor.file_raw_size = 0; |
| 1804 | } |
| 1805 | |
| 1806 | /****³õʼ»¯Óû§ÐÅÏ¢*****/ |
| 1807 | int zte_init_user_infor(USER_COMMON_INFOR* pp_co_info, webs_t wp) |
| 1808 | { |
| 1809 | pp_co_info->requst_sid = websGetSid(wp); |
| 1810 | pp_co_info->card_full = 0; |
| 1811 | memset(pp_co_info->address, 0, ZTE_IP_ADDRESS_LENGTH); |
| 1812 | memset(pp_co_info->UnixYMDTime, 0, ZTE_IP_ADDRESS_LENGTH); |
| 1813 | memset(pp_co_info->file_name, 0, ZTE_HTTPSHARE_FILE_NAME_MAX_LEN); |
| 1814 | memset(pp_co_info->path, 0, ZTE_HTTPSHARE_FILE_NAME_MAX_LEN); |
| 1815 | memcpy((void*)pp_co_info->address, (void*)websGetRequestIpaddr(wp), ZTE_IP_ADDRESS_LENGTH); |
| 1816 | (void)zte_httpshare_get_record(pp_co_info->address, pp_co_info->path); |
| 1817 | zte_init_up_infor(pp_co_info); |
| 1818 | return 1; |
| 1819 | } |
| 1820 | |
| 1821 | int zte_reset_cgi_state(webs_t wp) |
| 1822 | { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1823 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]reset_cgi_state.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1824 | #if 0 // kw 3 |
| 1825 | if (!zte_malloc_cgi_buff()) { |
| 1826 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]malloc failed.\n"); |
| 1827 | return 0; |
| 1828 | } |
| 1829 | #else |
| 1830 | zte_malloc_cgi_buff(); |
| 1831 | #endif |
| 1832 | if (!zte_path_check(wp)) { |
| 1833 | return 0; |
| 1834 | } |
| 1835 | zte_init_user_infor(&pp_header, wp); |
| 1836 | file_end = FALSE; |
| 1837 | memset(file_buf_memory, 0, FILE_BUFFER_LEN); |
| 1838 | return 1; |
| 1839 | } |
| 1840 | |
| 1841 | uint32 zte_get_sd_free_size(uint32 b_free) |
| 1842 | { |
| 1843 | struct statvfs file_stat; |
| 1844 | (void)statvfs(SD_CARD_PATH, &file_stat); |
| 1845 | b_free = (file_stat.f_bfree) * (file_stat.f_bsize / 512) / 8; |
| 1846 | return b_free; |
| 1847 | } |
| 1848 | |
| 1849 | int zte_str_index(char* s, char* t) //text filename= |
| 1850 | { |
| 1851 | int i = 0; |
| 1852 | int j = 0; |
| 1853 | int strindex = 0; |
| 1854 | int stateindex = 0; |
| 1855 | if (NULL == s || NULL == t) { |
| 1856 | return -1; |
| 1857 | } |
| 1858 | int len = strlen(t); |
| 1859 | stateindex = strlen(s) - strlen(t); |
| 1860 | while ((strindex <= stateindex) && (j < len)) { |
| 1861 | if (s[i] == t[j]) { |
| 1862 | i = i + 1; |
| 1863 | j = j + 1; |
| 1864 | } else { |
| 1865 | i = i - j + 1; |
| 1866 | strindex = i; |
| 1867 | j = 0; |
| 1868 | } |
| 1869 | } |
| 1870 | if (j == len) { |
| 1871 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare] file name index->%d.\n", i - len); |
| 1872 | return (i - len); // filename= µÄÆðʼϱê |
| 1873 | } else { |
| 1874 | return (-1); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | |
| 1879 | int read_upload_info_from_file(const char *file, char *name) |
| 1880 | { |
| 1881 | |
| 1882 | int fd_tcard_upload; |
| 1883 | ssize_t read_len = 0; |
| 1884 | |
| 1885 | if (NULL == name) { |
| 1886 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]name == NULL\n"); |
| 1887 | return -1; |
| 1888 | } |
| 1889 | |
| 1890 | if (-1 == access(TCARD_UPLOAD_FILE, F_OK)) { |
| 1891 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]file is not exist\n"); |
| 1892 | return -1; |
| 1893 | } |
| 1894 | |
| 1895 | errno = 0; |
| 1896 | if ((fd_tcard_upload = open(TCARD_UPLOAD_FILE, O_RDONLY)) < 0) { |
| 1897 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]open file failed : %s\n", strerror(errno)); |
| 1898 | return -1; |
| 1899 | } |
| 1900 | |
| 1901 | errno = 0; |
| 1902 | |
| 1903 | read_len = read(fd_tcard_upload, name, HTTPSHARE_BUF_NORMAL_LEN_MAX + 1); |
| 1904 | if (read_len < 0) { |
| 1905 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]read file failed : %s\n", strerror(errno)); |
| 1906 | } else { |
| 1907 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]read file ,upload name : %s\n", name); |
| 1908 | } |
| 1909 | |
| 1910 | close(fd_tcard_upload); |
| 1911 | |
| 1912 | return 0; |
| 1913 | } |
| 1914 | |
| 1915 | int save_upload_info_to_file(const char *file, char *name) |
| 1916 | { |
| 1917 | int fd_upload = -1; |
| 1918 | |
| 1919 | if (name == NULL) { |
| 1920 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]save_upload_info_to_file error,name == NULL.\n"); |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | if (strlen(name) == 0) { |
| 1925 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]save_upload_info_to_file error,name == empty.\n"); |
| 1926 | return 0; |
| 1927 | } |
| 1928 | |
| 1929 | errno = 0; |
| 1930 | fd_upload = open(TCARD_UPLOAD_FILE, O_CREAT | O_WRONLY, 0666); |
| 1931 | if (-1 == fd_upload) { |
| 1932 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]save_upload_info_to_file open file failed:%s\n", strerror(errno)); |
| 1933 | return -1; |
| 1934 | } |
| 1935 | |
| 1936 | errno = 0; |
| 1937 | if (write(fd_upload, name, strlen(name)) < 0) { |
| 1938 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]save_upload_info_to_file write file failed:%s\n", strerror(errno)); |
| 1939 | } |
| 1940 | |
| 1941 | close(fd_upload); |
| 1942 | slog(MISC_PRINT, SLOG_NORMAL,"[httpshare]save_upload_info_to_file save up info.\n"); |
| 1943 | return 0; |
| 1944 | } |
| 1945 | |
| 1946 | static char* neutralize(char *path) |
| 1947 | { |
| 1948 | if(path == NULL) |
| 1949 | { |
| 1950 | return NULL; |
| 1951 | } |
| 1952 | /* Ïà¶Ô·¾¶·À»¤ */ |
| 1953 | if (strstr(path, "../")) |
| 1954 | { |
| 1955 | return NULL; |
| 1956 | } |
| 1957 | |
| 1958 | /* ϵͳ·¾¶·À»¤ */ |
| 1959 | if (strncmp(path, "/etc/", 5) == 0) |
| 1960 | { |
| 1961 | return NULL; |
| 1962 | } |
| 1963 | |
| 1964 | /* null ·ûºÅ·À»¤*/ |
| 1965 | if (strstr(path, "%00")) |
| 1966 | { |
| 1967 | return NULL; |
| 1968 | } |
| 1969 | |
| 1970 | return path; |
| 1971 | } |
| 1972 | |
| 1973 | void zte_httpshare_check_upload_file() |
| 1974 | { |
| 1975 | char cname[ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1] = {0}; |
| 1976 | char *name = NULL; |
| 1977 | |
| 1978 | if (0 == read_upload_info_from_file(TCARD_UPLOAD_FILE, cname)) { |
| 1979 | cname[ZTE_HTTPSHARE_PATH_NAME_MAX_LEN] = 0; |
| 1980 | name = neutralize(cname); |
| 1981 | if(name){ |
| 1982 | name[ZTE_HTTPSHARE_PATH_NAME_MAX_LEN] = 0; |
| 1983 | zte_del_file(name); |
| 1984 | zte_del_file(TCARD_UPLOAD_FILE); |
| 1985 | if ((access(name, F_OK) == 0) || (access(TCARD_UPLOAD_FILE, F_OK) == 0)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1986 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_check_upload_file del upload file fail!\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1987 | } |
| 1988 | memset(file_buf_memory, 0, FILE_BUFFER_LEN); |
| 1989 | memset(&pp_header, 0, sizeof(USER_COMMON_INFOR)); |
| 1990 | memset(download_file, 0, sizeof(download_file)); |
| 1991 | file_buf_memory_ptr = NULL; |
| 1992 | tcard_file_size = 0; |
| 1993 | file_end = FALSE; |
| 1994 | } |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | int zte_get_upload_filename(webs_t wp, USER_COMMON_INFOR * pp_current) |
| 1999 | { |
| 2000 | char *temp_name = NULL; |
| 2001 | char URL[ZTE_HTTPSHARE_PATH_NAME_MAX_LEN + 1] = {0}; |
| 2002 | |
| 2003 | //snprintf(URL,sizeof(URL),"%s",wp->url); |
| 2004 | //printf("[iphone]zte_get_upload_filename URL:%s\n",URL); |
| 2005 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2006 | slog(MISC_PRINT, SLOG_DEBUG,"[iphone]get_upload_filename url:%s\n", wp->url); |
| 2007 | slog(MISC_PRINT, SLOG_DEBUG,"[iphone]get_upload_filename path:%s\n", wp->path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2008 | |
| 2009 | if ((wp->path == NULL) || (wp->url == NULL)) { |
| 2010 | return -1; |
| 2011 | } |
| 2012 | |
| 2013 | websDecodeUrl(URL, wp->url, gstrlen(wp->url)); |
| 2014 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2015 | slog(MISC_PRINT, SLOG_DEBUG,"[iphone]get_upload_filename URL:%s\n", URL); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2016 | |
| 2017 | if (strlen(URL) <= 0) { |
| 2018 | return -1; |
| 2019 | } |
| 2020 | |
| 2021 | temp_name = strstr(URL, "cgi-bin/httpshare"); |
| 2022 | |
| 2023 | if (temp_name == NULL) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2024 | slog(MISC_PRINT, SLOG_ERR,"[iphone]get_upload_filename url strstr cgi == NULL\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2025 | return -1; |
| 2026 | } |
| 2027 | temp_name += strlen("cgi-bin/httpshare")+1; |
| 2028 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2029 | slog(MISC_PRINT, SLOG_DEBUG,"[iphone]get_upload_filename temp:%s\n", temp_name); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2030 | |
| 2031 | // if (temp_name != NULL) { |
| 2032 | if (strlen(temp_name) == 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2033 | slog(MISC_PRINT, SLOG_ERR,"[iphone]get_upload_filename url name == NULL,get name from file header\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2034 | return 0; |
| 2035 | } |
| 2036 | if (strstr(temp_name, "#") != NULL) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2037 | slog(MISC_PRINT, SLOG_ERR,"[iphone]get_upload_filename url name have #,get name from file header\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2038 | return 0; |
| 2039 | } |
| 2040 | // }else { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2041 | // slog(MISC_PRINT, SLOG_ERR,"[iphone]get_upload_filename temp == NULL\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2042 | // return -1; |
| 2043 | // } |
| 2044 | |
| 2045 | |
| 2046 | memset(pp_current->file_name, 0, sizeof(pp_current->file_name)); |
| 2047 | |
| 2048 | snprintf(pp_current->file_name, ZTE_HTTPSHARE_FILE_NAME_MAX_LEN, "%s", temp_name); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2049 | slog(MISC_PRINT, SLOG_DEBUG,"[iphone]get_upload_filename url:%s, temp:%s, pp_header:%s\n", URL, temp_name, pp_current->file_name); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2050 | |
| 2051 | return 1; |
| 2052 | } |
| 2053 | |
| 2054 | char * zte_get_cgi_filename(webs_t wp, const char* text, USER_COMMON_INFOR * pp_current) |
| 2055 | { |
| 2056 | char * head_index = NULL; |
| 2057 | char *sp_name = NULL; |
| 2058 | char *time_ptr = NULL; |
| 2059 | int i = 0 ; |
| 2060 | int j = 0; |
| 2061 | int k = 0; |
| 2062 | int name_head ; |
| 2063 | sp_name = "filename="; |
| 2064 | if (NULL == text) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2065 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]get_cgi_filename text is null.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2066 | return NULL; |
| 2067 | } |
| 2068 | if (!strlen(pp_current->UnixYMDTime)) { |
| 2069 | time_ptr = strstr(text, ZTE_HTTPSHARE_TIME); |
| 2070 | if (time_ptr) { |
| 2071 | time_ptr = time_ptr + strlen(ZTE_HTTPSHARE_TIME) + 5; |
| 2072 | while (*time_ptr != 13) { // \r\n |
| 2073 | pp_current->UnixYMDTime[k++] = *time_ptr; |
| 2074 | time_ptr++; |
| 2075 | } |
| 2076 | pp_current->UnixYMDTime[k] = '\0'; |
| 2077 | } |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2078 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]get_cgi_filename UnixYMDTime is %s.\n", pp_current->UnixYMDTime); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | name_head = zte_str_index(text, sp_name); //find 'filename=' |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2082 | //printf("[httpshare]get_cgi_filename head is %d.\n",name_head); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2083 | if (name_head > 0) { |
| 2084 | for (i = name_head; * (text + i) != '\0'; i++) { //ÕÒµ½filename="test.doc"µÄµÚÒ»¸öÒýºÅλÖà |
| 2085 | if (*(text + i) == '"') { |
| 2086 | name_head = i + 1;//ÉÏ´«ÎļþÃû³ÆÆðʼϱê |
| 2087 | break; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | for (i = name_head; * (text + i) != '"'; i++) { |
| 2092 | if (*(text + i) == '\\') { |
| 2093 | name_head = i + 1; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | for (i = name_head, j = 0; * (text + i) != '"'; i++, j++) { |
| 2098 | pp_current->file_name[j] = *(text + i); |
| 2099 | } |
| 2100 | pp_current->infor.file_head_len += i; |
| 2101 | |
| 2102 | head_index = text + i;//head_index Ö¸Ïòfilename= " " ÒýºÅ |
| 2103 | pp_current->file_name[j] = '\0'; |
| 2104 | if(strlen(pp_current->path) == 0){ |
| 2105 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]get path NULL\n"); |
| 2106 | return NULL; |
| 2107 | } |
| 2108 | |
| 2109 | if (pp_current->path[strlen(pp_current->path) - 1] != '/') |
| 2110 | strcat(pp_current->path, "/"); |
| 2111 | |
| 2112 | if (zte_get_upload_filename(wp, pp_current) == -1) { |
| 2113 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]get filename error\n"); |
| 2114 | return NULL; |
| 2115 | } |
| 2116 | |
| 2117 | strcat(pp_current->path, pp_current->file_name); |
| 2118 | |
| 2119 | save_upload_info_to_file(TCARD_UPLOAD_FILE, pp_current->path); |
| 2120 | } |
| 2121 | return head_index; |
| 2122 | } |
| 2123 | |
| 2124 | |
| 2125 | |
| 2126 | char *zte_cgi_parse_file_head(char * buf, USER_COMMON_INFOR*user) |
| 2127 | { |
| 2128 | char *fdindex = NULL; |
| 2129 | if (!buf) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2130 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]cgi_parse_file_head file head is null\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2131 | return NULL; |
| 2132 | } |
| 2133 | |
| 2134 | boolean foud_head_first = FALSE; |
| 2135 | |
| 2136 | fdindex = buf; |
| 2137 | #if 1 |
| 2138 | while (*fdindex) { |
| 2139 | if (user->infor.file_head == ZTE_CGI_PARSE_FILE_HEAD_BEGIN) { |
| 2140 | if ((*fdindex) == 13) { //»Ø³µ |
| 2141 | user->infor.file_head = ZTE_CGI_PARSE_FINDE_HEAD_FIRST; |
| 2142 | user->infor.file_head_len++; |
| 2143 | fdindex++; |
| 2144 | } else { |
| 2145 | user->infor.file_head = ZTE_CGI_PARSE_FILE_HEAD_BEGIN; |
| 2146 | user->infor.file_head_len++; |
| 2147 | fdindex++; |
| 2148 | } |
| 2149 | } else if (user->infor.file_head == ZTE_CGI_PARSE_FINDE_HEAD_FIRST) { |
| 2150 | if ((*fdindex) == 10) { //»»ÐÐ |
| 2151 | user->infor.file_head = ZTE_CGI_PARSE_FINDE_HEAD_SEC; |
| 2152 | |
| 2153 | user->infor.file_head_len++; |
| 2154 | fdindex++; |
| 2155 | } else { |
| 2156 | user->infor.file_head = ZTE_CGI_PARSE_FILE_HEAD_BEGIN; |
| 2157 | } |
| 2158 | } else if (user->infor.file_head == ZTE_CGI_PARSE_FINDE_HEAD_SEC) { |
| 2159 | if ((*fdindex) == 13) { |
| 2160 | user->infor.file_head = ZTE_CGI_PARSE_FINDE_HEAD_THIRD; |
| 2161 | |
| 2162 | user->infor.file_head_len++; |
| 2163 | fdindex++; |
| 2164 | } else { |
| 2165 | user->infor.file_head = ZTE_CGI_PARSE_FILE_HEAD_BEGIN; |
| 2166 | } |
| 2167 | } else if (user->infor.file_head == ZTE_CGI_PARSE_FINDE_HEAD_THIRD) { |
| 2168 | if ((*fdindex) == 10) { |
| 2169 | user->infor.file_head = ZTE_CGI_PARSE_FINDE_FILE_HEAD; |
| 2170 | user->infor.file_head_len++; |
| 2171 | fdindex++; |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2172 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]cgi_parse_file_head file_head_len %d.\n", user->infor.file_head_len); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2173 | return fdindex;//Êý¾ÝÆðʼ |
| 2174 | } else { |
| 2175 | user->infor.file_head = ZTE_CGI_PARSE_FILE_HEAD_BEGIN; |
| 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | #endif |
| 2180 | return NULL; |
| 2181 | } |
| 2182 | |
| 2183 | |
| 2184 | |
| 2185 | char *zte_cgi_parse_file_tail_address(char *buf, USER_COMMON_INFOR*user) |
| 2186 | { |
| 2187 | char *fdindex = NULL; |
| 2188 | if (!buf) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2189 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]cgi_parse_file_tail_address file tail is null\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2190 | return NULL; |
| 2191 | } |
| 2192 | |
| 2193 | fdindex = buf + ZTE_PARSE_CGI_TAIL_LEN - 1; |
| 2194 | while ((*fdindex) && (user->infor.file_tail_len < ZTE_PARSE_CGI_TAIL_LEN)) { |
| 2195 | |
| 2196 | if (user->infor.file_tail_addres == ZTE_CGI_PARSE_FILE_TAIL_ADD_BEGIN) { |
| 2197 | if ((*fdindex) == 45) { //'-' |
| 2198 | user->infor.file_tail_addres = ZTE_CGI_PARSE_FINDE_TAIL_ADD_FIRST; |
| 2199 | user->infor.file_tail_len++; |
| 2200 | fdindex--; |
| 2201 | } else { |
| 2202 | user->infor.file_tail_addres = ZTE_CGI_PARSE_FILE_TAIL_ADD_BEGIN; |
| 2203 | user->infor.file_tail_len++; |
| 2204 | fdindex--; |
| 2205 | } |
| 2206 | } else if (user->infor.file_tail_addres == ZTE_CGI_PARSE_FINDE_TAIL_ADD_FIRST) { |
| 2207 | if ((*fdindex) == 10) { |
| 2208 | user->infor.file_tail_addres = ZTE_CGI_PARSE_FINDE_TAIL_ADD_SEC; |
| 2209 | user->infor.file_tail_len++; |
| 2210 | fdindex--; |
| 2211 | } else { |
| 2212 | user->infor.file_tail_addres = ZTE_CGI_PARSE_FILE_TAIL_ADD_BEGIN; |
| 2213 | } |
| 2214 | } else if (user->infor.file_tail_addres == ZTE_CGI_PARSE_FINDE_TAIL_ADD_SEC) { |
| 2215 | if ((*fdindex) == 13) { |
| 2216 | user->infor.file_tail_addres = ZTE_CGI_PARSE_FINDE_FILE_TAIL_ADD; |
| 2217 | user->infor.file_tail_len++; |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2218 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]cgi_parse_file_tail_address found file tail,file_tail_len->%d.\n", user->infor.file_tail_len); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2219 | return fdindex;//file_tail start |
| 2220 | } else { |
| 2221 | user->infor.file_tail_addres = ZTE_CGI_PARSE_FILE_TAIL_ADD_BEGIN; |
| 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | return NULL; |
| 2226 | } |
| 2227 | |
| 2228 | |
| 2229 | char *zte_cgi_parse_file_tail(USER_COMMON_INFOR*pp_current, int left_file_size, char*text, int nbytes) |
| 2230 | { |
| 2231 | char *fdindex = NULL; |
| 2232 | char *file_tail = NULL; |
| 2233 | int tail_record_size = 0; |
| 2234 | |
| 2235 | if (!text) { |
| 2236 | return NULL; |
| 2237 | } |
| 2238 | |
| 2239 | if (left_file_size == 0) { |
| 2240 | if (nbytes >= ZTE_PARSE_CGI_TAIL_LEN) { |
| 2241 | fdindex = text + (nbytes - ZTE_PARSE_CGI_TAIL_LEN); |
| 2242 | |
| 2243 | file_tail = zte_cgi_parse_file_tail_address(fdindex, pp_current); |
| 2244 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]file info: tail len->%d.\n", pp_current->infor.file_tail_len); |
| 2245 | if (file_tail) { |
| 2246 | pp_current->infor.file_tail_len = strlen(file_tail); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2247 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]cgi_parse_file_tail file tail len is %d\n", pp_current->infor.file_tail_len); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2248 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]file info: tail->%s.\n", file_tail); |
| 2249 | pp_current->infor.file_tail = ZTE_CGI_PARSE_FINDE_TAIL_IN_FILE; |
| 2250 | return file_tail; |
| 2251 | } else { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2252 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]cgi_parse_file_tail file_tail not found\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2253 | return NULL; |
| 2254 | } |
| 2255 | } else { |
| 2256 | fdindex = text; |
| 2257 | memcpy((pp_current->infor.file_tail_pool + pp_current->infor.file_tail_pool_len), fdindex, nbytes); |
| 2258 | pp_current->infor.file_tail_pool_len = pp_current->infor.file_tail_pool_len + nbytes; |
| 2259 | fdindex = pp_current->infor.file_tail_pool; |
| 2260 | file_tail = zte_cgi_parse_file_tail_address(fdindex, pp_current); |
| 2261 | if (file_tail) { |
| 2262 | pp_current->infor.file_tail = ZTE_CGI_PARSE_FINDE_TAIL_IN_POOL; |
| 2263 | return file_tail; |
| 2264 | } else { |
| 2265 | return NULL; |
| 2266 | } |
| 2267 | } |
| 2268 | } else { |
| 2269 | if ((nbytes + left_file_size) >= ZTE_PARSE_CGI_TAIL_LEN) { |
| 2270 | tail_record_size = ZTE_PARSE_CGI_TAIL_LEN - left_file_size; |
| 2271 | fdindex = text + (nbytes - tail_record_size); |
| 2272 | memcpy((pp_current->infor.file_tail_pool + pp_current->infor.file_tail_pool_len), fdindex, tail_record_size); |
| 2273 | pp_current->infor.file_tail_pool_len = tail_record_size + pp_current->infor.file_tail_pool_len; |
| 2274 | pp_current->infor.file_tail = ZTE_CGI_PARSE_FINDE_TAIL_IN_FILE; |
| 2275 | return fdindex; |
| 2276 | } else { |
| 2277 | fdindex = text; |
| 2278 | memcpy((pp_current->infor.file_tail_pool + pp_current->infor.file_tail_pool_len), fdindex, nbytes); |
| 2279 | pp_current->infor.file_tail_pool_len = nbytes + pp_current->infor.file_tail_pool_len; |
| 2280 | pp_current->infor.file_tail = ZTE_CGI_PARSE_FINDE_TAIL_PROCESS; |
| 2281 | return NULL; |
| 2282 | |
| 2283 | } |
| 2284 | |
| 2285 | } |
| 2286 | |
| 2287 | } |
| 2288 | |
| 2289 | |
| 2290 | boolean zte_bufer_combination(char *buf, int size) |
| 2291 | { |
| 2292 | if ((tcard_file_size + size) < FILE_BUFFER_LEN - WEBS_SOCKET_BUFSIZ) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2293 | //printf("[httpshare]bufer_combination tcard_file_size->%d,size->%d.\n",tcard_file_size,size); |
| 2294 | //printf("[httpshare]bufer_combination FILE_BUFFER_LEN->%d,WEBS_SOCKET_BUFSIZ->%d.\n",FILE_BUFFER_LEN,WEBS_SOCKET_BUFSIZ); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2295 | memcpy(file_buf_memory_ptr, buf, size); |
| 2296 | file_buf_memory_ptr = file_buf_memory_ptr + size; |
| 2297 | tcard_file_size = tcard_file_size + size; |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2298 | //printf("[httpshare]bufer_combination tcard_file_size->%d,size->%d.\n",tcard_file_size,size); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2299 | return FALSE; |
| 2300 | } else { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2301 | //printf("[httpshare]bufer_combination begin to write file \n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2302 | memcpy(file_buf_memory_ptr, buf, size); |
| 2303 | |
| 2304 | file_buf_memory_ptr = file_buf_memory_ptr + size; |
| 2305 | tcard_file_size = tcard_file_size + size; |
| 2306 | return TRUE; |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | |
| 2311 | int zte_httpshare_write(webs_t wp, USER_COMMON_INFOR*pp_current) // 1 OK;-1 0 error |
| 2312 | { |
| 2313 | int file_close = -1; |
| 2314 | int file_handler = -1; |
| 2315 | //USER_COMMON_INFOR *pp_current = NULL; |
| 2316 | //if(0 == tcard_file_size) |
| 2317 | //{ |
| 2318 | // return -1; |
| 2319 | //} |
| 2320 | |
| 2321 | if (NULL == pp_current) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2322 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_write:get_user_position failed\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2323 | return -1; |
| 2324 | } |
| 2325 | if (1 != zte_check_sdcard_exist()) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2326 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_write:get_user_position failed\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2327 | return -1; |
| 2328 | } |
| 2329 | file_handler = open(pp_current->path, O_CREAT | O_WRONLY | O_APPEND, 0666); |
| 2330 | |
| 2331 | if (file_handler < 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2332 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_write write file open file failed\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2333 | return -1; |
| 2334 | } else { |
| 2335 | (void)write(file_handler, file_buf_memory, tcard_file_size); |
| 2336 | file_buf_memory_ptr = file_buf_memory; |
| 2337 | memset(file_buf_memory, 0, FILE_BUFFER_LEN); |
| 2338 | tcard_file_size = 0; |
| 2339 | file_close = close(file_handler); |
| 2340 | if (file_close < 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2341 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]httpshare_write write file close file failed\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2342 | |
| 2343 | return -1; |
| 2344 | } |
| 2345 | file_handler = -1; |
| 2346 | } |
| 2347 | return 1; |
| 2348 | } |
| 2349 | |
| 2350 | int zte_write_file(webs_t wp, char*buf, int file_size, USER_COMMON_INFOR*pp_current) // 1 OK;-1 0 error |
| 2351 | { |
| 2352 | int result = 0; |
| 2353 | result = zte_bufer_combination(buf, file_size); |
| 2354 | if (result == 1) { |
| 2355 | return zte_httpshare_write(wp, pp_current); |
| 2356 | //return 1; |
| 2357 | } else { |
| 2358 | if (file_end) { |
| 2359 | return zte_httpshare_write(wp, pp_current); |
| 2360 | //return 1; |
| 2361 | } else |
| 2362 | return 1; |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | |
| 2367 | #if 0 |
| 2368 | int zte_process_cgi_write(webs_t wp, char *file_head, uint32 zte_clen, char * text, int nbytes, USER_COMMON_INFOR*pp_current) |
| 2369 | { |
| 2370 | char *file_tail = NULL; |
| 2371 | uint32 left_size = 0; |
| 2372 | int file_close = -1; |
| 2373 | if ((!text) && (!file_head)) { |
| 2374 | return -1; |
| 2375 | } |
| 2376 | left_size = zte_clen - nbytes; |
| 2377 | |
| 2378 | if (left_size < ZTE_PARSE_CGI_TAIL_LEN) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2379 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]process_cgi_write left_size->%d.nbytes->%d\n", left_size, nbytes); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2380 | file_tail = zte_cgi_parse_file_tail(pp_current, left_size, text, nbytes); //ÕÒµ½tail λÖà |
| 2381 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]file_tail->%-*.*s.\n", pp_current->infor.file_tail_len, pp_current->infor.file_tail_len, file_tail); |
| 2382 | } |
| 2383 | |
| 2384 | if (file_tail) { |
| 2385 | if (pp_current->infor.data_no == ZTE_CGI_WRITE_THE_FIRST_DATA) { |
| 2386 | pp_current->infor.file_raw_size = zte_clen; /*get all the data size*/ |
| 2387 | pp_current->infor.data_no = ZTE_CGI_WRITE_THE_SEC_DATA; |
| 2388 | } |
| 2389 | |
| 2390 | if (pp_current->infor.file_tail == ZTE_CGI_PARSE_FINDE_TAIL_IN_FILE) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2391 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]process_cgi_write CGI_PARSE_FINDE_TAIL_IN_FILE.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2392 | |
| 2393 | if (file_tail == file_head) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2394 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]process_cgi_write it is a null file\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2395 | file_close = close(open(pp_current->path, O_CREAT, 0666)); |
| 2396 | if (file_close < 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2397 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]process_cgi_write write file close file failed\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2398 | } |
| 2399 | return -1; |
| 2400 | |
| 2401 | } |
| 2402 | if (file_tail - file_head < 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2403 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]process_cgi_write file_tail-file_head = %d < 0\n", file_tail - file_head); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2404 | return -1; |
| 2405 | } |
| 2406 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]file_tail-file_head->%d.\n", file_tail - file_head); |
| 2407 | (void)zte_write_file(wp, file_head, (file_tail - file_head)); |
| 2408 | } else if (pp_current->infor.file_tail == ZTE_CGI_PARSE_FINDE_TAIL_IN_POOL) { |
| 2409 | (void)zte_write_file(wp, pp_current->infor.file_tail_pool, (file_tail - pp_current->infor.file_tail_pool)); |
| 2410 | } else { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2411 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]process_cgi_write some erro happened\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | } else { |
| 2415 | if (pp_current->infor.file_tail != ZTE_CGI_PARSE_FINDE_TAIL_PROCESS) { |
| 2416 | if (pp_current->infor.data_no == ZTE_CGI_WRITE_THE_FIRST_DATA) { |
| 2417 | pp_current->infor.file_raw_size = zte_clen; /*get all the data size*/ |
| 2418 | |
| 2419 | (void)zte_write_file(wp, file_head, nbytes - pp_current->infor.file_head_len); |
| 2420 | |
| 2421 | pp_current->infor.data_no = ZTE_CGI_WRITE_THE_SEC_DATA; |
| 2422 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2423 | printf("[httpshare]process_cgi_write file head len-> %d\n", pp_current->infor.file_head_len); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2424 | } else { |
| 2425 | (void)zte_write_file(wp, file_head, nbytes); |
| 2426 | } |
| 2427 | } |
| 2428 | } |
| 2429 | return 0; |
| 2430 | } |
| 2431 | |
| 2432 | #endif |
| 2433 | |
| 2434 | |
| 2435 | // 1 OK;-1 0 error |
| 2436 | int zte_process_cgi_write(webs_t wp, char *file_head, uint32 zte_clen, char * text, int nbytes, USER_COMMON_INFOR*pp_current) |
| 2437 | { |
| 2438 | char *file_tail = NULL; |
| 2439 | uint32 left_size = 0; |
| 2440 | int file_close = -1; |
| 2441 | int ret = -1; |
| 2442 | if ((!text) && (!file_head)) { |
| 2443 | return -1; |
| 2444 | } |
| 2445 | left_size = zte_clen - nbytes; |
| 2446 | |
| 2447 | if (left_size < ZTE_PARSE_CGI_TAIL_LEN) { |
| 2448 | file_tail = zte_cgi_parse_file_tail(pp_current, left_size, text, nbytes); //ÕÒµ½tail λÖà |
| 2449 | } |
| 2450 | |
| 2451 | if (file_tail) { |
| 2452 | if (pp_current->infor.data_no == ZTE_CGI_WRITE_THE_FIRST_DATA) { |
| 2453 | pp_current->infor.file_raw_size = zte_clen; /*get all the data size*/ |
| 2454 | pp_current->infor.data_no = ZTE_CGI_WRITE_THE_SEC_DATA; |
| 2455 | } |
| 2456 | |
| 2457 | if (pp_current->infor.file_tail == ZTE_CGI_PARSE_FINDE_TAIL_IN_FILE) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2458 | printf("[httpshare]process_cgi_write CGI_PARSE_FINDE_TAIL_IN_FILE.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2459 | |
| 2460 | //if(file_tail==file_head) |
| 2461 | //{ |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2462 | // printf("[httpshare]process_cgi_write it is a null file\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2463 | // file_close=close(open(pp_current->path, O_CREAT,0666 )); |
| 2464 | // if(file_close<0) |
| 2465 | // { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2466 | // printf("[httpshare]process_cgi_write write file close file failed\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2467 | // return -1; |
| 2468 | // } |
| 2469 | //} |
| 2470 | if (file_tail - file_head < 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2471 | printf("[httpshare]process_cgi_write file_tail-file_head = %d < 0\n", file_tail - file_head); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2472 | return -1; |
| 2473 | } |
| 2474 | |
| 2475 | file_end = TRUE; |
| 2476 | ret = zte_write_file(wp, file_head, (file_tail - file_head), pp_current); |
| 2477 | } else if (pp_current->infor.file_tail == ZTE_CGI_PARSE_FINDE_TAIL_IN_POOL) { |
| 2478 | file_end = TRUE; |
| 2479 | ret = zte_write_file(wp, pp_current->infor.file_tail_pool, (file_tail - pp_current->infor.file_tail_pool), pp_current); |
| 2480 | } else { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2481 | printf("[httpshare]process_cgi_write some erro happened\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2482 | ret = 0; |
| 2483 | } |
| 2484 | |
| 2485 | } else { |
| 2486 | if (pp_current->infor.file_tail != ZTE_CGI_PARSE_FINDE_TAIL_PROCESS) { |
| 2487 | if (pp_current->infor.data_no == ZTE_CGI_WRITE_THE_FIRST_DATA) { |
| 2488 | pp_current->infor.file_raw_size = zte_clen; /*get all the data size*/ |
| 2489 | |
| 2490 | ret = zte_write_file(wp, file_head, nbytes - pp_current->infor.file_head_len, pp_current); |
| 2491 | |
| 2492 | pp_current->infor.data_no = ZTE_CGI_WRITE_THE_SEC_DATA; |
| 2493 | } else { |
| 2494 | ret = zte_write_file(wp, file_head, nbytes, pp_current); |
| 2495 | } |
| 2496 | } |
| 2497 | } |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2498 | //printf("[zyl]process_cgi_write:ret->%d\n",ret); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2499 | return ret; |
| 2500 | } |
| 2501 | |
| 2502 | |
| 2503 | |
| 2504 | |
| 2505 | /*¿ªÊ¼½ÓÊÕÎļþÊý¾Ý*/ |
| 2506 | int zte_process_cgi_recv(webs_t wp, uint32 zte_clen, char *text, int nbytes)//-1,0 error; 1 OK |
| 2507 | { |
| 2508 | uint32 b_free = 0; |
| 2509 | char *file_head = NULL; |
| 2510 | uint32 clen = zte_clen - 200; |
| 2511 | USER_COMMON_INFOR *pp_current = &pp_header; |
| 2512 | //pp_current = zte_process_user_auth(wp);//¶ÁÈ¡zte_reset_cgi_stateÖÐ×¢²áµÄpp_currentÐÅÏ¢ |
| 2513 | //pp_current = zte_get_user_position(wp); |
| 2514 | // if (NULL == pp_current || NULL == text) { // kw 3 pp_current point to pp_aheader's address, can not be NULL |
| 2515 | if (NULL == text) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2516 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]process_cgi_begin recv failed.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2517 | return -1; |
| 2518 | } |
| 2519 | if (pp_current->card_full) { |
| 2520 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]T card full %d \n", pp_current->card_full); |
| 2521 | return 0; |
| 2522 | } |
| 2523 | |
| 2524 | #if 0 |
| 2525 | b_free = zte_get_sd_free_size(b_free); |
| 2526 | if (b_free >= 524288) { //2*1024*1024*1024/512/8 sd card bfree more than 2G |
| 2527 | printf("[httpshare]T card available size: %d \n", b_free * 512 * 8 / 1024 / 1024); //ÉÏ´«Îļþ±ØÐëҪСÓÚ2G |
| 2528 | } else if ((((int)clen) > (b_free * 512 * 8)) && ((int)clen > 0)) { //Ê£Óà¿Õ¼äСÓÚ2G£¬Ôò±È½ÏÉÏ´«Îļþ´óС |
| 2529 | pp_current->card_full = 1; |
| 2530 | printf("[httpshare]upload file too larg.\n");//ÈçºÎ֪ͨwebUI |
| 2531 | return 1; |
| 2532 | } |
| 2533 | #endif |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2534 | //printf("[httpshare]file info: clen->%d.\n",zte_clen); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2535 | |
| 2536 | if (pp_current->infor.file_head != ZTE_CGI_PARSE_FINDE_FILE_HEAD) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2537 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]upload to fild head.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2538 | |
| 2539 | file_head = zte_cgi_parse_file_head(zte_get_cgi_filename(wp, text, pp_current), pp_current); |
| 2540 | //ÕÒµ½Êý¾ÝÆðʼλfile_head |
| 2541 | if (pp_current->infor.file_head == ZTE_CGI_PARSE_FINDE_FILE_HEAD && file_head) { |
| 2542 | return zte_process_cgi_write(wp, file_head, zte_clen, text, nbytes, pp_current); // 1 OK;-1 0 error |
| 2543 | } |
| 2544 | |
| 2545 | return 1; |
| 2546 | } else { |
| 2547 | file_head = text; |
| 2548 | return zte_process_cgi_write(wp, file_head, zte_clen, text, nbytes, pp_current); |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | |
| 2553 | |
| 2554 | void zte_write_httpShare_upload_result_to_web(webs_t wp, char_t *result, char_t*reason) |
| 2555 | { |
| 2556 | if ((NULL == wp) || (NULL == result)) { |
| 2557 | return; |
| 2558 | } |
| 2559 | |
| 2560 | web_feedback_header(wp); |
| 2561 | (void)websWrite(wp, T("{\"result\":\"%s\",\"reason\":\"%s\"}"), |
| 2562 | result, reason); |
| 2563 | } |
| 2564 | |
| 2565 | |
| 2566 | |
| 2567 | int zte_check_file_complete(char *temp_file_name, USER_COMMON_INFOR*user) |
| 2568 | { |
| 2569 | |
| 2570 | int file_should_size = 0; |
| 2571 | //int ret = 0; |
| 2572 | struct stat statbuf; |
| 2573 | |
| 2574 | if (!temp_file_name) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2575 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]check_file_complete:path NULL\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2576 | |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
| 2580 | if (!strlen(temp_file_name)) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2581 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]check_file_complete:strlen(path)==0\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2582 | |
| 2583 | return 0; |
| 2584 | } |
| 2585 | |
| 2586 | |
| 2587 | memset((void*)&statbuf, 0, sizeof(statbuf)); |
| 2588 | |
| 2589 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]path file name is = %s, len is %d\n", temp_file_name, strlen(temp_file_name)); |
| 2590 | |
| 2591 | //ret = stat(temp_file_name, &statbuf); |
| 2592 | |
| 2593 | //slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]ret is %d\n", ret); |
| 2594 | if (stat(temp_file_name, &statbuf) < 0) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2595 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]check_file_complete stat : %s[%s]\n", strerror(errno),temp_file_name); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2596 | return 0; |
| 2597 | } |
| 2598 | file_should_size = user->infor.file_raw_size - user->infor.file_head_len - user->infor.file_tail_len; |
| 2599 | |
| 2600 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]the file size shoud is %d, actul is %d\n", file_should_size, statbuf.st_size); |
| 2601 | if (statbuf.st_size != file_should_size) { |
| 2602 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]file transfer failed:the file size shoud is %d, actul is %d \n", file_should_size, statbuf.st_size); |
| 2603 | return 0; |
| 2604 | } else { |
| 2605 | return 1; |
| 2606 | } |
| 2607 | |
| 2608 | } |
| 2609 | |
| 2610 | |
| 2611 | void zte_release_user(USER_COMMON_INFOR* current) |
| 2612 | { |
| 2613 | if (!current) return; |
| 2614 | |
| 2615 | pp_header.cnt--; |
| 2616 | |
| 2617 | if (pp_header.cnt == 0) { |
| 2618 | file_buf_memory_ptr = NULL; |
| 2619 | #if 0 |
| 2620 | if (file_buf_memory) { |
| 2621 | free(file_buf_memory); |
| 2622 | file_buf_memory = file_buf_memory_ptr = NULL; |
| 2623 | printf("[httpshare]release memory\n"); |
| 2624 | } |
| 2625 | #endif |
| 2626 | } |
| 2627 | free(current); |
| 2628 | current = NULL; |
| 2629 | } |
| 2630 | |
| 2631 | void zte_remove_user() |
| 2632 | { |
| 2633 | zte_del_file(TCARD_UPLOAD_FILE); |
| 2634 | memset(&pp_header, 0, sizeof(pp_header)); |
| 2635 | memset(file_buf_memory, 0, FILE_BUFFER_LEN); |
| 2636 | memset(download_file, 0, sizeof(download_file)); |
| 2637 | file_buf_memory_ptr = NULL; |
| 2638 | tcard_file_size = 0; |
| 2639 | file_end = FALSE; |
| 2640 | } |
| 2641 | |
| 2642 | int zte_httpshare_proc_end(webs_t wp) |
| 2643 | { |
| 2644 | websDone(wp, 200); |
| 2645 | sleep(1); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2646 | slog(MISC_PRINT, SLOG_NORMAL,"[httpshare]process_cgi_end websDone\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2647 | zte_remove_user(); |
| 2648 | return 1; |
| 2649 | } |
| 2650 | |
| 2651 | void zte_pre_process_cgi_end(webs_t wp) |
| 2652 | { |
| 2653 | USER_COMMON_INFOR *pp_current = &pp_header; |
| 2654 | #if 0 // kw 3 pp_current points to pp_haader' address, can not be null |
| 2655 | if (NULL == pp_current) { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2656 | slog(MISC_PRINT, SLOG_ERR,"[httpshare]process_cgi_end pp_current NULL.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2657 | zte_write_result_to_web(wp, FAILURE); |
| 2658 | return; |
| 2659 | } |
| 2660 | #endif |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2661 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]process_cgi_end path =%s\n", pp_current->path); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2662 | if (pp_current->card_full) { |
| 2663 | unlink(pp_current->path); |
| 2664 | zte_write_httpShare_upload_result_to_web(wp, FAILURE, "space_not_enough"); |
| 2665 | return; |
| 2666 | } |
| 2667 | |
| 2668 | if (!zte_check_file_complete(pp_current->path, pp_current)) { |
| 2669 | unlink(pp_current->path); |
| 2670 | zte_write_httpShare_upload_result_to_web(wp, FAILURE, "data_lost"); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 2671 | slog(MISC_PRINT, SLOG_DEBUG,"[httpshare]process_cgi_end data_lost.\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 2672 | return; |
| 2673 | } else { |
| 2674 | (void)zte_httpshare_call_system("/bin/sync"); //write new file from momeroy to sdcard on time |
| 2675 | zte_change_file_time(pp_current->path, pp_current->UnixYMDTime); |
| 2676 | zte_write_result_to_web(wp, SUCCESS); |
| 2677 | return; |
| 2678 | } |
| 2679 | |
| 2680 | } |
| 2681 | |
| 2682 | |
| 2683 | int zte_process_cgi_end(webs_t wp) |
| 2684 | { |
| 2685 | zte_pre_process_cgi_end(wp); |
| 2686 | zte_httpshare_proc_end(wp); |
| 2687 | return 1; |
| 2688 | } |
| 2689 | |
| 2690 | |