r.xiao | 21da0e5 | 2023-11-13 04:05:27 -0800 | [diff] [blame] | 1 | /************************************************************* |
| 2 | Description: |
| 3 | MBTK fs c file. |
| 4 | Author: |
| 5 | LiuBin |
| 6 | Date: |
| 7 | 2020/4/30 13:51:42 |
| 8 | *************************************************************/ |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <ctype.h> |
| 12 | #include <string.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <unistd.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <dirent.h> |
| 18 | #include <linux/msg.h> |
| 19 | #include <sys/statfs.h> |
| 20 | #include <sys/vfs.h> |
| 21 | #include <sys/param.h> |
| 22 | #include <sys/mount.h> |
| 23 | #include <math.h> |
| 24 | #include <errno.h> |
| 25 | #include <linux/magic.h> |
| 26 | #include <time.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <signal.h> |
| 29 | #include <pthread.h> |
| 30 | #include <cutils/properties.h> |
| 31 | #include "mbtk_type.h" |
| 32 | #include "mbtk_fs.h" |
| 33 | |
| 34 | /************************************************************* |
| 35 | Constants and Macros |
| 36 | *************************************************************/ |
| 37 | |
| 38 | |
| 39 | /************************************************************* |
| 40 | Variables:local |
| 41 | *************************************************************/ |
| 42 | int DEF_DIR_S = 0; |
| 43 | |
| 44 | int msgid; |
| 45 | |
| 46 | /************************************************************* |
| 47 | Variables:public |
| 48 | *************************************************************/ |
| 49 | //mbtk_fs |
| 50 | struct my_msg //消息队列结构体 |
| 51 | { |
| 52 | long int my_msg_type; |
| 53 | char *ptr; |
| 54 | }mbtk_at_fs_msg; |
| 55 | |
| 56 | typedef struct |
| 57 | { |
| 58 | char in_dir_file[MBTK_FS_MAX + 1]; |
| 59 | char out_dir_file[MBTK_FS_MAX + 1]; |
| 60 | } mbtk_fs_file_dir_info_s; |
| 61 | mbtk_fs_file_dir_info_s mbtk_at_fs_par_at={0}; |
| 62 | |
| 63 | /************************************************************* |
| 64 | Local Function Declaration |
| 65 | *************************************************************/ |
| 66 | int mbtk_at_fs_msgid(int *at_msgid); |
| 67 | void mbtk_at_fs_printf(void *data); |
| 68 | static bool fs_is_space_char(char ch); |
| 69 | static bool fs_str_empty(char *str); |
| 70 | static int read_file_len(char *file); |
| 71 | static char *GET_PWD(); |
| 72 | static int pwd_def_dir(char* path_dir); |
| 73 | static int path_pass(); |
| 74 | static int creat_filesystem_def_dir(); |
| 75 | void fs_ls_at_cb_func(void *file_list_cb); |
| 76 | void fs_ls_at_cb_func_num(void *file_list_cb); |
| 77 | void mbtk_at_fs_data_cb(void *data, int data_len); |
| 78 | |
| 79 | |
| 80 | /************************************************************* |
| 81 | Local Function Definitions |
| 82 | *************************************************************/ |
| 83 | |
| 84 | /* |
| 85 | //收 |
| 86 | void mbtk_at_fs_printf() |
| 87 | { |
| 88 | int msgid; |
| 89 | mbtk_at_msg.my_msg_type=3; |
| 90 | msgid=msgget(12,0666|IPC_CREAT); |
| 91 | MBTK_FUNC(mbtk_at_msgid)(&msgid); |
| 92 | //char read_buf[MAXMSG]; |
| 93 | while(1) |
| 94 | { |
| 95 | msgrcv(msgid,&mbtk_at_msg,4,mbtk_at_msg.my_msg_type,0); |
| 96 | if(strncmp(mbtk_at_msg.ptr,"end",3)==0) |
| 97 | break; |
| 98 | ATRESP(IND_REQ_HANDLE , ATCI_RESULT_CODE_NULL, 0, mbtk_at_msg.ptr); |
| 99 | free(mbtk_at_msg.ptr); |
| 100 | mbtk_at_msg.ptr=NULL; |
| 101 | } |
| 102 | msgctl(msgid,IPC_RMID,0); |
| 103 | } |
| 104 | */ |
| 105 | //发 |
| 106 | |
| 107 | int mbtk_at_fs_msgid(int *at_msgid) |
| 108 | { |
| 109 | msgid = *at_msgid; |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | void mbtk_at_fs_printf(void *data) |
| 114 | { |
| 115 | char *ptr = malloc(strlen(data)+1); |
| 116 | memset(ptr,0,strlen(data)+1); |
| 117 | memcpy(ptr, data, strlen(data)+1); |
| 118 | mbtk_at_fs_msg.ptr = ptr; |
| 119 | mbtk_at_fs_msg.my_msg_type=4; |
| 120 | msgsnd(msgid,&mbtk_at_fs_msg,sizeof(&mbtk_at_fs_msg),0); //发送数据到缓冲区 |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static bool fs_is_space_char(char ch) |
| 125 | { |
| 126 | if(ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n') |
| 127 | return TRUE; |
| 128 | |
| 129 | return FALSE; |
| 130 | } |
| 131 | |
| 132 | static bool fs_str_empty(char *str) |
| 133 | { |
| 134 | if(str == NULL || strlen(str) == 0) |
| 135 | return TRUE; |
| 136 | |
| 137 | return FALSE; |
| 138 | } |
| 139 | |
| 140 | static int read_file_len(char* file) |
| 141 | { |
| 142 | int size; |
| 143 | FILE* fp; |
| 144 | |
| 145 | fp=fopen(file,"r"); |
| 146 | if(NULL==fp) |
| 147 | { |
| 148 | fclose(fp); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | fseek(fp,0,SEEK_END); //将文件读写指针移动到文件尾 |
| 153 | size=ftell(fp); //ftell函数获取从文件尾移动到文件开头的偏移量 |
| 154 | fseek(fp,0,SEEK_SET);//恢复指针位置 |
| 155 | fclose(fp); |
| 156 | } |
| 157 | return size; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | //Gets the current working directory |
| 162 | static char *GET_PWD() |
| 163 | { |
| 164 | char dir_buff[1024] = {0}; |
| 165 | char *dir = NULL; |
| 166 | |
| 167 | dir = getcwd(dir_buff,sizeof(dir_buff)); |
| 168 | |
| 169 | if (dir == NULL) |
| 170 | { |
| 171 | LOGE(" Not current working directory errno = [%d]\n",errno); |
| 172 | } |
| 173 | //LOGE("getcwd dir=[%s]\n",dir); |
| 174 | |
| 175 | return dir; |
| 176 | } |
| 177 | |
| 178 | //Default directory determinations |
| 179 | static int pwd_def_dir(char* path_dir) |
| 180 | { |
| 181 | int ret = 0; |
| 182 | |
| 183 | if (strncmp(path_dir,DFF_DIR_NVM,3) == 0 || strncmp(path_dir,DFF_DIR_BIN,3) == 0 || strncmp(path_dir,DFF_DIR_DEV,3) == 0 || \ |
| 184 | strncmp(path_dir,DFF_DIR_ETC,3) == 0 || strncmp(path_dir,DFF_DIR_LIB,3) == 0 || strncmp(path_dir,DFF_DIR_LOG,3) == 0 || \ |
| 185 | strncmp(path_dir,DFF_DIR_MNT,3) == 0 || strncmp(path_dir,DFF_DIR_OVERLAY,3) == 0 || strncmp(path_dir,DFF_DIR_PROC,3) == 0 || \ |
| 186 | strncmp(path_dir,DFF_DIR_ROM,3) == 0 || strncmp(path_dir,DFF_DIR_ROOT,3) == 0 || strncmp(path_dir,DFF_DIR_SBIN,3) == 0 || \ |
| 187 | strncmp(path_dir,DFF_DIR_SYS,3) == 0 || strncmp(path_dir,DFF_DIR_TMP,3) == 0 || strncmp(path_dir,DFF_DIR_USR,3) == 0 || \ |
| 188 | strncmp(path_dir,DFF_DIR_VAR,3) == 0 || strncmp(path_dir,DFF_DIR_WWW,3) == 0 ) |
| 189 | { |
| 190 | ret = -1; |
| 191 | LOGE("Enter the default directory \n"); |
| 192 | } |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | // |
| 198 | static int path_pass() |
| 199 | { |
| 200 | int at_fs_ret = 0; |
| 201 | char *fs_pwd_path = NULL; |
| 202 | char tmp_path[FS_NAME_LEN] = {0}; |
| 203 | |
| 204 | fs_pwd_path = GET_PWD();//获取当前路径 |
| 205 | |
| 206 | strcpy(tmp_path, fs_pwd_path); |
| 207 | |
| 208 | if ((at_fs_ret = pwd_def_dir(tmp_path)) == -1) |
| 209 | return at_fs_ret; |
| 210 | |
| 211 | if (strncmp(tmp_path, PART_FS, 3) != 0) |
| 212 | { |
| 213 | if (strcmp(tmp_path, ROOT_PATH) == 0)//首次进入,所以不用指定前一个字符匹配,全部匹配 |
| 214 | { |
| 215 | at_fs_ret = chdir(PART_FS);//首次进入切换路径 |
| 216 | LOGE(">dir == [%s]\n", getcwd(NULL,NULL)); |
| 217 | if(-1 == at_fs_ret) |
| 218 | { |
| 219 | LOGE("chdir path error:[%d]\n", errno); |
| 220 | return at_fs_ret; |
| 221 | } |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | at_fs_ret = -1; |
| 226 | LOGE("Not specified dir %s\n", tmp_path); |
| 227 | return at_fs_ret; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return at_fs_ret; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | |
| 236 | //创建文件系统的默认操作路径 |
| 237 | static int creat_filesystem_def_dir() |
| 238 | { |
| 239 | int ret = 0; |
| 240 | |
| 241 | if (DEF_DIR_S == 0)//以全局变量判断是否创建了默认路径 |
| 242 | { |
| 243 | if (access(PART_FS, F_OK) == 0) |
| 244 | { |
| 245 | DEF_DIR_S = 1; |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | ret = mkdir(PART_FS, 0766);//创建路径 |
| 250 | if(-1 == ret) |
| 251 | { |
| 252 | LOGE("mkdir path error:[%d]\n", errno); |
| 253 | return ret; |
| 254 | } |
| 255 | ret = chdir(PART_FS);//切换路径 |
| 256 | LOGE(">dir == [%s]\n", getcwd(NULL,NULL)); |
| 257 | if(-1 == ret) |
| 258 | { |
| 259 | LOGE("chdir path error:[%d]\n", errno); |
| 260 | return ret; |
| 261 | } |
| 262 | DEF_DIR_S = 1; |
| 263 | } |
| 264 | } |
| 265 | else if (DEF_DIR_S == 1) |
| 266 | { |
| 267 | ;//不进行处理 |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | LOGE("mkdir path DEF_DIR_S error:[%d]\n", errno); |
| 272 | } |
| 273 | |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | //拼接传入的路径到默认操作路径 |
| 278 | static int path_redirection(char *in_path, char *out_path) |
| 279 | { |
| 280 | int ret = 0, len = 0; |
| 281 | char *tmp_dir = NULL; |
| 282 | char def_fs[FS_NAME_LEN] = {0}; |
| 283 | |
| 284 | len = strlen(in_path); |
| 285 | if ( len + 5 >= 127 ) |
| 286 | { |
| 287 | LOGE("in_path len more than error:[%d]\n", errno); |
| 288 | ret = -1; |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | strcpy(def_fs, PART_FS); |
| 293 | |
| 294 | tmp_dir = strncat(def_fs, in_path, sizeof(def_fs)-1);//先拼接再判断 |
| 295 | |
| 296 | if (strncmp(PART_FS_, tmp_dir, 4) == 0) |
| 297 | { |
| 298 | memcpy(out_path, tmp_dir, MBTK_FS_MAX); |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | ret = -1; |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | memset(def_fs, 0x00, sizeof(def_fs)); |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | void fs_ls_at_cb_func(void *file_list_cb) |
| 312 | { |
| 313 | char resp_buf[1024 + 1] = {0}; |
| 314 | printf("+MFSLS::\r\n%s\n", ((mbtk_fs_dir_file_info_s *)file_list_cb)->name); |
| 315 | sprintf(resp_buf, "%s\n",((mbtk_fs_dir_file_info_s *)file_list_cb)->name); |
| 316 | mbtk_at_fs_printf(resp_buf); |
| 317 | //ret = ATRESP(30163, 0, 0, resp_buf); |
| 318 | } |
| 319 | |
| 320 | void fs_ls_at_cb_func_num(void *file_list_cb) |
| 321 | { |
| 322 | //不进行处理,只走流程 |
| 323 | } |
| 324 | |
| 325 | void mbtk_at_fs_data_cb(void *data, int data_len) |
| 326 | { |
| 327 | char resp_buf[1024]; |
| 328 | printf("\n=====data_cb data[%s]=====\n=====data_cb data_len[%d]=====\n",data, data_len); |
| 329 | |
| 330 | memset(resp_buf,0,1024); |
| 331 | mbtk_at_fs_printf(resp_buf); |
| 332 | |
| 333 | if(data_len > 512) |
| 334 | { |
| 335 | memset(resp_buf,0,1024); |
| 336 | memcpy(resp_buf,data,512); |
| 337 | mbtk_at_fs_printf(resp_buf); |
| 338 | memset(resp_buf,0,1024); |
| 339 | sprintf(resp_buf, "DATA: <%d>", 512); |
| 340 | mbtk_at_fs_printf(resp_buf); |
| 341 | |
| 342 | memset(resp_buf,0,1024); |
| 343 | sprintf(resp_buf, "+MFSREAD:"); |
| 344 | mbtk_at_fs_printf(resp_buf); |
| 345 | memset(resp_buf,0,1024); |
| 346 | memcpy(resp_buf,data+512,data_len-512); |
| 347 | mbtk_at_fs_printf(resp_buf); |
| 348 | memset(resp_buf,0,1024); |
| 349 | sprintf(resp_buf, "DATA: <%d>", data_len-512); |
| 350 | mbtk_at_fs_printf(resp_buf); |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | memset(resp_buf,0,1024); |
| 355 | memcpy(resp_buf,data,data_len); |
| 356 | mbtk_at_fs_printf(resp_buf); |
| 357 | |
| 358 | memset(resp_buf,0,1024); |
| 359 | sprintf(resp_buf, "<%d>", data_len); |
| 360 | mbtk_at_fs_printf(resp_buf); |
| 361 | } |
| 362 | |
| 363 | } |
| 364 | |
| 365 | |
| 366 | //Creating a directory |
| 367 | static int fs_cmd_process_mkd(char *path) |
| 368 | { |
| 369 | int ret = 0; |
| 370 | char Filepath[MBTK_FS_MAX + 1]; |
| 371 | char Pathtmp[MBTK_FS_MAX + 1]; |
| 372 | char *pDir = NULL; |
| 373 | struct stat FileStat_t; |
| 374 | |
| 375 | memset(Filepath, 0x00, sizeof(Filepath)); |
| 376 | memset(Pathtmp, 0x00, sizeof(Pathtmp)); |
| 377 | memset(&FileStat_t, 0x00, sizeof(FileStat_t)); |
| 378 | |
| 379 | memcpy(Filepath, path, sizeof(Filepath)); |
| 380 | |
| 381 | if ((ret = path_redirection(Filepath, Pathtmp)) == -1) |
| 382 | { |
| 383 | LOGE("input path_redirection error:[%d]\n", errno); |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | //后期再看那个合适 |
| 388 | #if 0 |
| 389 | pDir = strtok(Filepath, "/"); |
| 390 | strcat(Pathtmp, "/"); |
| 391 | strcat(Pathtmp, pDir); |
| 392 | strcat(Pathtmp, "/"); |
| 393 | |
| 394 | memset(&FileStat_t, 0x00, sizeof(FileStat_t)); |
| 395 | stat(Pathtmp, &FileStat_t); |
| 396 | |
| 397 | if(!S_ISDIR(FileStat_t.st_mode)) |
| 398 | { |
| 399 | ret = mkdir(Pathtmp, 0766);//创建路径 |
| 400 | if(-1 == ret) |
| 401 | { |
| 402 | LOGE("mkdir path [%s] error:[%d]\n", __FUNCTION__, errno); |
| 403 | return ret; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | while(NULL != ( pDir=strtok(NULL, "/") )) |
| 408 | { |
| 409 | strcat(Pathtmp, pDir); |
| 410 | strcat(Pathtmp, "/"); |
| 411 | |
| 412 | memset(&FileStat_t, 0x00, sizeof(FileStat_t)); |
| 413 | stat(Pathtmp, &FileStat_t); |
| 414 | |
| 415 | if(!S_ISDIR(FileStat_t.st_mode)) |
| 416 | { |
| 417 | ret = mkdir(Pathtmp, 0766);//创建文件 |
| 418 | if(-1 == ret) |
| 419 | { |
| 420 | LOGE("mkdir path [%s] error:[%d]\n", __FUNCTION__, errno); |
| 421 | return ret; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | #else |
| 426 | stat(Pathtmp, &FileStat_t); |
| 427 | |
| 428 | if(!S_ISDIR(FileStat_t.st_mode)) |
| 429 | { |
| 430 | ret = mkdir(Pathtmp, 0766);//创建路径 |
| 431 | if(-1 == ret) |
| 432 | { |
| 433 | LOGE("mkdir path error:[%d]\n", errno); |
| 434 | return ret; |
| 435 | } |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | ret = -1; |
| 440 | LOGE("not a directory error:[%d]\n", errno); |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | #endif |
| 445 | |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | static int fs_cmd_process_rmkd(char *path) |
| 450 | { |
| 451 | int ret = 0; |
| 452 | char Filepath[MBTK_FS_MAX + 1]={0}; |
| 453 | char Pathtmp[MBTK_FS_MAX + 1]={0}; |
| 454 | |
| 455 | memcpy(Filepath, path, sizeof(Filepath)); |
| 456 | |
| 457 | if ((ret = path_redirection(Filepath, Pathtmp)) == -1) |
| 458 | { |
| 459 | LOGE("input path_redirection error:[%d]\n", errno); |
| 460 | return ret; |
| 461 | } |
| 462 | |
| 463 | if (access(Pathtmp, F_OK) != 0) |
| 464 | { |
| 465 | ret = -1; |
| 466 | LOGE("File does not exist error:[%d]\n", errno); |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | //ret = rmdir(Filepath); |
| 471 | ret = rmdir(Pathtmp); |
| 472 | if (ret == -1) |
| 473 | { |
| 474 | LOGE("rmkdir path error:[%d]\n", errno); |
| 475 | } |
| 476 | |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | static int fs_cmd_process_list_dir(char *path,mbtk_fs_dir_file_info_s *fs_dir_list) |
| 481 | { |
| 482 | int ret = 0, count = 0, dir_count = 0, file_count = 0, len_name; |
| 483 | const char *temp_fs_path = NULL; |
| 484 | struct dirent *files; |
| 485 | struct stat FileStat_t; |
| 486 | DIR *dir; |
| 487 | mbtk_fs_dir_file_info_s fs_dir_file_t, fs_dir_t, fs_file_t; |
| 488 | char Filepath[MBTK_FS_MAX + 1]; |
| 489 | |
| 490 | memset(fs_dir_list, 0x0, sizeof(mbtk_fs_dir_file_info_s)-sizeof(void *)); |
| 491 | |
| 492 | temp_fs_path = path; |
| 493 | |
| 494 | chdir(temp_fs_path); |
| 495 | |
| 496 | dir = opendir(temp_fs_path); |
| 497 | |
| 498 | if (dir == NULL) |
| 499 | { |
| 500 | ret = -1; |
| 501 | LOGE("Directory cannot be opened!\n"); |
| 502 | return ret; |
| 503 | } |
| 504 | while ((files = readdir(dir)) != NULL) |
| 505 | { |
| 506 | if(strcmp(files->d_name, ".") == 0 || strcmp(files->d_name, "..") == 0) |
| 507 | { |
| 508 | continue; |
| 509 | }//跳过隐藏文件 |
| 510 | |
| 511 | memset(&FileStat_t, 0x00, sizeof(FileStat_t)); |
| 512 | memset(&fs_dir_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s)); |
| 513 | memset(&fs_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s)); |
| 514 | memset(&fs_dir_t, 0x0, sizeof(mbtk_fs_dir_file_info_s)); |
| 515 | |
| 516 | len_name = strlen(files->d_name); |
| 517 | //目录或者文件夹名字长度不能超过256,可以的话这里也可以做出错处理; |
| 518 | stat(files->d_name,&FileStat_t); |
| 519 | |
| 520 | if(S_ISDIR(FileStat_t.st_mode)) //递归调用解析其子目录下的文件夹 |
| 521 | { |
| 522 | memcpy(fs_dir_t.name, files->d_name, sizeof(len_name)); |
| 523 | dir_count++; |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | memcpy(fs_file_t.name, files->d_name, sizeof(len_name)); |
| 528 | file_count++; |
| 529 | } |
| 530 | |
| 531 | memcpy(fs_dir_file_t.name, files->d_name, sizeof(len_name)); |
| 532 | count++; |
| 533 | |
| 534 | //每次对一个文件或者目录处理,就加入里面 |
| 535 | (fs_dir_list->fs_ls_cb_typedef)(&fs_dir_t); |
| 536 | } |
| 537 | closedir(dir); |
| 538 | |
| 539 | //每次显示完成后,把读取的数量传给全局变量方便读取 |
| 540 | //mbtk_at_fs_par.all_dir_file = count; |
| 541 | mbtk_at_fs_par.dir_num = dir_count; |
| 542 | //mbtk_at_fs_par.file_num = file_count; |
| 543 | |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | static int fs_cmd_process_list_file(char *path,mbtk_fs_dir_file_info_s *fs_file_list) |
| 548 | { |
| 549 | int ret = 0, count = 0, dir_count = 0, file_count = 0, len_name; |
| 550 | const char *temp_fs_path = NULL; |
| 551 | struct dirent *files; |
| 552 | struct stat FileStat_t; |
| 553 | DIR *dir; |
| 554 | mbtk_fs_dir_file_info_s fs_dir_file_t, fs_dir_t, fs_file_t; |
| 555 | char Filepath[MBTK_FS_MAX + 1]; |
| 556 | |
| 557 | memset(fs_file_list, 0x0, sizeof(mbtk_fs_dir_file_info_s)-sizeof(void *)); |
| 558 | |
| 559 | temp_fs_path = path; |
| 560 | |
| 561 | chdir(temp_fs_path); |
| 562 | |
| 563 | dir = opendir(temp_fs_path); |
| 564 | |
| 565 | if (dir == NULL) |
| 566 | { |
| 567 | ret = -1; |
| 568 | LOGE("Directory cannot be opened!\n"); |
| 569 | return ret; |
| 570 | } |
| 571 | while ((files = readdir(dir)) != NULL) |
| 572 | { |
| 573 | if(strcmp(files->d_name, ".") == 0 || strcmp(files->d_name, "..") == 0) |
| 574 | { |
| 575 | continue; |
| 576 | }//跳过隐藏文件 |
| 577 | |
| 578 | memset(&FileStat_t, 0x00, sizeof(FileStat_t)); |
| 579 | memset(&fs_dir_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s)); |
| 580 | memset(&fs_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s)); |
| 581 | memset(&fs_dir_t, 0x0, sizeof(mbtk_fs_dir_file_info_s)); |
| 582 | |
| 583 | len_name = strlen(files->d_name); |
| 584 | //目录或者文件夹名字长度不能超过256,可以的话这里也可以做出错处理; |
| 585 | stat(files->d_name,&FileStat_t); |
| 586 | |
| 587 | if(S_ISDIR(FileStat_t.st_mode)) //递归调用解析其子目录下的文件夹 |
| 588 | { |
| 589 | memcpy(fs_dir_t.name, files->d_name, (len_name)+1); |
| 590 | dir_count++; |
| 591 | } |
| 592 | else |
| 593 | { |
| 594 | memcpy(fs_file_t.name, files->d_name, (len_name)+1); |
| 595 | file_count++; |
| 596 | } |
| 597 | |
| 598 | memcpy(fs_dir_file_t.name, files->d_name, (len_name)+1); |
| 599 | count++; |
| 600 | |
| 601 | //每次对一个文件或者目录处理,就加入里面 |
| 602 | (fs_file_list->fs_ls_cb_typedef)(&fs_file_t); |
| 603 | } |
| 604 | |
| 605 | //每次显示完成后,把读取的数量传给全局变量方便读取 |
| 606 | //mbtk_at_fs_par.all_dir_file = count; |
| 607 | //mbtk_at_fs_par.dir_num = dir_count; |
| 608 | mbtk_at_fs_par.file_num = file_count; |
| 609 | |
| 610 | closedir(dir); |
| 611 | |
| 612 | return ret; |
| 613 | } |
| 614 | |
| 615 | static int fs_cmd_process_list_all(char *path,mbtk_fs_dir_file_info_s *fs_dir_and_file_list) |
| 616 | { |
| 617 | int ret = 0, count = 0, dir_count = 0, file_count = 0, len_name; |
| 618 | const char *temp_fs_path = NULL; |
| 619 | struct dirent *files; |
| 620 | struct stat FileStat_t; |
| 621 | DIR *dir; |
| 622 | mbtk_fs_dir_file_info_s fs_dir_file_t, fs_dir_t, fs_file_t; |
| 623 | char Filepath[MBTK_FS_MAX + 1]; |
| 624 | |
| 625 | memset(fs_dir_and_file_list, 0x0, sizeof(mbtk_fs_dir_file_info_s)-sizeof(void *)); |
| 626 | |
| 627 | temp_fs_path = path; |
| 628 | |
| 629 | chdir(temp_fs_path); |
| 630 | |
| 631 | dir = opendir(temp_fs_path); |
| 632 | |
| 633 | if (dir == NULL) |
| 634 | { |
| 635 | ret = -1; |
| 636 | LOGE("Directory cannot be opened!\n"); |
| 637 | return ret; |
| 638 | } |
| 639 | while ((files = readdir(dir)) != NULL) |
| 640 | { |
| 641 | if(strcmp(files->d_name, ".") == 0 || strcmp(files->d_name, "..") == 0) |
| 642 | { |
| 643 | continue; |
| 644 | }//跳过隐藏文件 |
| 645 | |
| 646 | memset(&FileStat_t, 0x00, sizeof(FileStat_t)); |
| 647 | memset(&fs_dir_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s)); |
| 648 | memset(&fs_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s)); |
| 649 | memset(&fs_dir_t, 0x0, sizeof(mbtk_fs_dir_file_info_s)); |
| 650 | |
| 651 | len_name = strlen(files->d_name); |
| 652 | //目录或者文件夹名字长度不能超过256,可以的话这里也可以做出错处理; |
| 653 | stat(files->d_name,&FileStat_t); |
| 654 | |
| 655 | if(S_ISDIR(FileStat_t.st_mode)) //递归调用解析其子目录下的文件夹 |
| 656 | { |
| 657 | memcpy(fs_dir_t.name, files->d_name, (len_name)+1); |
| 658 | //LOGE("DIR>>>>>>>>>>>>>> [%s]\n", fs_dir_t.name); |
| 659 | dir_count++; |
| 660 | } |
| 661 | else |
| 662 | { |
| 663 | memcpy(fs_file_t.name, files->d_name, (len_name)+1); |
| 664 | //LOGE("FILE>>>>>>>>>>>>>> [%s]\n", fs_file_t.name); |
| 665 | file_count++; |
| 666 | } |
| 667 | |
| 668 | memcpy(fs_dir_file_t.name, files->d_name, (len_name)+1); |
| 669 | //LOGE("ALL>>>>>>>>>>>>>> [%s]\n", fs_dir_file_t.name); |
| 670 | count++; |
| 671 | |
| 672 | //每次对一个文件或者目录处理,就加入里面 |
| 673 | (fs_dir_and_file_list->fs_ls_cb_typedef)(&fs_dir_file_t); |
| 674 | } |
| 675 | |
| 676 | //每次显示完成后,把读取的数量传给全局变量方便读取 |
| 677 | mbtk_at_fs_par.all_dir_file = count; |
| 678 | mbtk_at_fs_par.dir_num = dir_count; |
| 679 | mbtk_at_fs_par.file_num = file_count; |
| 680 | |
| 681 | closedir(dir); |
| 682 | |
| 683 | return ret; |
| 684 | } |
| 685 | |
| 686 | static int fs_cmd_process_cd(char *path) |
| 687 | { |
| 688 | int ret = 0; |
| 689 | char buf[1024]; |
| 690 | char Filepath[MBTK_FS_MAX + 1]; |
| 691 | char Pathtmp[MBTK_FS_MAX + 1]; |
| 692 | |
| 693 | memset(buf, 0x00, sizeof(buf)); |
| 694 | memset(Filepath , 0x00, sizeof(Filepath)); |
| 695 | memset(Pathtmp , 0x00, sizeof(Pathtmp)); |
| 696 | |
| 697 | memcpy(Filepath, path, sizeof(Filepath)); |
| 698 | |
| 699 | if ((ret = path_redirection(Filepath, Pathtmp)) == -1) |
| 700 | { |
| 701 | LOGE("input path_redirection error:[%d]\n", errno); |
| 702 | return ret; |
| 703 | } |
| 704 | |
| 705 | ret = chdir(Pathtmp); |
| 706 | |
| 707 | // ret = chdir(path); |
| 708 | |
| 709 | if (ret == -1) |
| 710 | { |
| 711 | LOGE("cd path error:[%d]\n", errno); |
| 712 | } |
| 713 | |
| 714 | char *ptr = getcwd(buf, sizeof(buf)); |
| 715 | if (ptr == NULL) |
| 716 | LOGE("cd path error:[%d]\n", errno); |
| 717 | |
| 718 | return ret; |
| 719 | } |
| 720 | |
| 721 | static int fs_cmd_process_rename(char *old_path, char *new_path) |
| 722 | { |
| 723 | int ret = 0; |
| 724 | char tmp_old[MBTK_FS_MAX + 1]; |
| 725 | char tmp_new[MBTK_FS_MAX + 1]; |
| 726 | |
| 727 | memset(tmp_old , 0x00, sizeof(tmp_old)); |
| 728 | memset(tmp_new , 0x00, sizeof(tmp_new)); |
| 729 | |
| 730 | if ((ret = path_redirection(old_path, tmp_old)) == -1) |
| 731 | { |
| 732 | LOGE("input path_redirection error:[%d]\n", errno); |
| 733 | return ret; |
| 734 | } |
| 735 | |
| 736 | if ((ret = path_redirection(new_path, tmp_new)) == -1) |
| 737 | { |
| 738 | LOGE("input path_redirection error:[%d]\n", errno); |
| 739 | return ret; |
| 740 | } |
| 741 | |
| 742 | ret = rename(tmp_old, tmp_new); |
| 743 | // ret = rename(old_path, new_path); |
| 744 | |
| 745 | if (ret == -1) |
| 746 | { |
| 747 | LOGE("rename error:[%d]\n", errno); |
| 748 | } |
| 749 | |
| 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | static int fs_cmd_process_copy(char *old_path, char *new_path, int mode) |
| 754 | { |
| 755 | int ret = 0, num = 0; |
| 756 | int old_fd,new_fd; |
| 757 | char *tbuf = NULL; |
| 758 | int tmode = mode;//没有做处理,默认以同步方式 |
| 759 | char tmp_old[MBTK_FS_MAX + 1]; |
| 760 | char tmp_new[MBTK_FS_MAX + 1]; |
| 761 | |
| 762 | memset(tmp_old , 0x00, sizeof(tmp_old)); |
| 763 | memset(tmp_new , 0x00, sizeof(tmp_new)); |
| 764 | |
| 765 | if ((ret = path_redirection(old_path, tmp_old)) == -1) |
| 766 | { |
| 767 | LOGE("input path_redirection error:[%d]\n", errno); |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | if ((ret = path_redirection(new_path, tmp_new)) == -1) |
| 772 | { |
| 773 | LOGE("input path_redirection error:[%d]\n", errno); |
| 774 | return ret; |
| 775 | } |
| 776 | |
| 777 | if (access(tmp_old, F_OK) != 0) //旧文件必须存在 |
| 778 | { |
| 779 | ret = -1; |
| 780 | LOGE("old_file access errn = [%d]\n",errno); |
| 781 | goto exit; |
| 782 | } |
| 783 | |
| 784 | if (access(tmp_new, F_OK) == 0) //新文件存在,则错误 |
| 785 | { |
| 786 | ret = -1; |
| 787 | LOGE("new_file access errn = [%d]\n",errno); |
| 788 | goto exit; |
| 789 | } |
| 790 | |
| 791 | if ( (old_fd=file_open(tmp_old, O_RDWR)) == -1 ) |
| 792 | { |
| 793 | LOGE("open old_path error:[%d]\n", errno); |
| 794 | ret = -1; |
| 795 | goto exit; |
| 796 | } |
| 797 | |
| 798 | if ( (new_fd=file_open(tmp_new, O_RDWR | O_CREAT)) == -1 ) |
| 799 | { |
| 800 | LOGE("open new_path error:[%d]\n", errno); |
| 801 | ret = -1; |
| 802 | goto exit; |
| 803 | } |
| 804 | |
| 805 | int len = lseek(old_fd,0,SEEK_END);//获取长度 |
| 806 | |
| 807 | if (len == -1)//获取文件大小时出错 |
| 808 | { |
| 809 | LOGE("lseek error:[%d]\n", errno); |
| 810 | ret = -1; |
| 811 | goto exit; |
| 812 | } |
| 813 | |
| 814 | lseek(old_fd,0,SEEK_SET);//还原指针指到开头处 |
| 815 | |
| 816 | tbuf = malloc(len + 1); |
| 817 | |
| 818 | if (len+1 > BUFFERSIZE) |
| 819 | LOGE("Out of length\n"); |
| 820 | |
| 821 | num = file_read(old_fd, tbuf, len); |
| 822 | |
| 823 | if (file_write(new_fd, tbuf, len) != num) |
| 824 | { |
| 825 | LOGE("The write does not match the read out\n"); |
| 826 | ret = -1; |
| 827 | goto exit; |
| 828 | } |
| 829 | |
| 830 | fsync(old_fd); |
| 831 | fsync(new_fd); |
| 832 | |
| 833 | exit: |
| 834 | file_close(old_fd); |
| 835 | file_close(new_fd); |
| 836 | free(tbuf); |
| 837 | return ret; |
| 838 | } |
| 839 | |
| 840 | static int fs_cmd_process_memory(uint64 *total, uint64 *available) |
| 841 | { |
| 842 | int ret = 0; |
| 843 | struct statfs buf; |
| 844 | uint64 blocksize, totalsize, availablesize, freesize; |
| 845 | |
| 846 | ret = statfs(PART_FS_OERLAY, &buf); |
| 847 | if (ret == -1) |
| 848 | { |
| 849 | LOGE("statfs error:[%d]\n", errno); |
| 850 | } |
| 851 | else |
| 852 | { |
| 853 | blocksize = buf.f_bsize; // 每个block里包含的字节数 |
| 854 | totalsize = blocksize * buf.f_blocks; // 总的字节数,f_blocks为block的数目 |
| 855 | //freesize = buf.f_bfree * blocksize; // 剩余空间的大小 |
| 856 | availablesize = buf.f_bavail * blocksize; // 可用空间大小 |
| 857 | |
| 858 | *total = totalsize; |
| 859 | *available = availablesize; |
| 860 | } |
| 861 | |
| 862 | return ret; |
| 863 | } |
| 864 | |
| 865 | static int fs_cmd_process_delfile(char *path) |
| 866 | { |
| 867 | int ret = 0; |
| 868 | char tmp[MBTK_FS_MAX + 1]; |
| 869 | |
| 870 | memset(tmp , 0x00, sizeof(tmp)); |
| 871 | |
| 872 | if ((ret = path_redirection(path, tmp)) == -1) |
| 873 | { |
| 874 | LOGE("input path_redirection error:[%d]\n", errno); |
| 875 | return ret; |
| 876 | } |
| 877 | |
| 878 | ret = remove(tmp); |
| 879 | // ret = remove(path); |
| 880 | |
| 881 | if (ret == -1) |
| 882 | { |
| 883 | LOGE("rm file error:[%d]\n", errno); |
| 884 | } |
| 885 | |
| 886 | return ret; |
| 887 | } |
| 888 | |
| 889 | static int fs_cmd_process_creatfile(char *path) |
| 890 | { |
| 891 | int ret = 0 ,fd; |
| 892 | char tmp[MBTK_FS_MAX + 1]; |
| 893 | |
| 894 | memset(tmp , 0x00, sizeof(tmp)); |
| 895 | |
| 896 | if ((ret = path_redirection(path, tmp)) == -1) |
| 897 | { |
| 898 | LOGE("input path_redirection error:[%d]\n", errno); |
| 899 | return ret; |
| 900 | } |
| 901 | |
| 902 | fd = file_open(tmp, O_RDWR | O_CREAT | O_TRUNC); |
| 903 | if (fd == -1) |
| 904 | { |
| 905 | ret = -1; |
| 906 | LOGE("creat file error:[%d]\n", errno); |
| 907 | } |
| 908 | |
| 909 | file_close(fd); |
| 910 | |
| 911 | return ret; |
| 912 | } |
| 913 | |
| 914 | int mbtk_at_fs_dir(int cmd, char *fs_path) |
| 915 | { |
| 916 | int at_fs_ret = 0; |
| 917 | char send_buf[SENDBUFF] = {0}; |
| 918 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 919 | |
| 920 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 921 | { |
| 922 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 923 | return at_fs_ret; |
| 924 | } |
| 925 | |
| 926 | if ((at_fs_ret = path_pass()) == -1) |
| 927 | goto exit; |
| 928 | |
| 929 | switch (cmd) |
| 930 | { |
| 931 | case 0: |
| 932 | { |
| 933 | at_fs_ret = fs_cmd_process_mkd(fs_path); |
| 934 | break; |
| 935 | } |
| 936 | case 1: |
| 937 | { |
| 938 | at_fs_ret = fs_cmd_process_rmkd(fs_path); |
| 939 | break; |
| 940 | } |
| 941 | default: |
| 942 | { |
| 943 | LOGE("ERROR cmd \n"); |
| 944 | at_fs_ret = -1; |
| 945 | break; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | exit: |
| 950 | mbtk_at_fs_printf(send_buf); |
| 951 | mbtk_at_fs_printf("end"); |
| 952 | return at_fs_ret; |
| 953 | } |
| 954 | |
| 955 | int mbtk_at_fs_list_num_get(int32* dir_num, int32* file_num) |
| 956 | { |
| 957 | int at_fs_ret = 0; |
| 958 | mbtk_fs_dir_file_info_s fs_dir_file; |
| 959 | char tmp_path[FS_NAME_LEN] = {0}; |
| 960 | char *fs_pwd_path = NULL; |
| 961 | char send_buf[SENDBUFF] = {0}; |
| 962 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 963 | |
| 964 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 965 | { |
| 966 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 967 | return at_fs_ret; |
| 968 | } |
| 969 | |
| 970 | if ((at_fs_ret = path_pass()) == -1) |
| 971 | goto exit; |
| 972 | |
| 973 | fs_pwd_path = GET_PWD();//获取当前路径 |
| 974 | |
| 975 | strcpy(tmp_path, fs_pwd_path); |
| 976 | |
| 977 | if (at_fs_ret == 0) |
| 978 | { |
| 979 | fs_dir_file.fs_ls_cb_typedef = fs_ls_at_cb_func_num; |
| 980 | at_fs_ret = fs_cmd_process_list_all(tmp_path, &fs_dir_file); |
| 981 | |
| 982 | *file_num = mbtk_at_fs_par.file_num; |
| 983 | *dir_num = mbtk_at_fs_par.dir_num; |
| 984 | } |
| 985 | else |
| 986 | { |
| 987 | at_fs_ret = -1; |
| 988 | LOGE("OPEN this dir errno [%s]\n", tmp_path); |
| 989 | goto exit; |
| 990 | } |
| 991 | |
| 992 | exit: |
| 993 | mbtk_at_fs_printf(send_buf); |
| 994 | mbtk_at_fs_printf("end"); |
| 995 | return at_fs_ret; |
| 996 | } |
| 997 | |
| 998 | int mbtk_at_fs_list_dir_file_get() |
| 999 | { |
| 1000 | int at_fs_ret = 0; |
| 1001 | char send_buf[SENDBUFF] = {0}; |
| 1002 | char *fs_pwd_path = NULL; |
| 1003 | mbtk_fs_dir_file_info_s fs_dir, fs_file; |
| 1004 | char tmp_path[FS_NAME_LEN] = {0}; |
| 1005 | |
| 1006 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1007 | { |
| 1008 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1009 | return at_fs_ret; |
| 1010 | } |
| 1011 | |
| 1012 | if ((at_fs_ret = path_pass()) == -1) |
| 1013 | goto exit; |
| 1014 | |
| 1015 | fs_pwd_path = GET_PWD();//获取当前路径 |
| 1016 | |
| 1017 | strcpy(tmp_path, fs_pwd_path); |
| 1018 | |
| 1019 | if ((at_fs_ret = pwd_def_dir(tmp_path)) == -1) |
| 1020 | goto exit; |
| 1021 | |
| 1022 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1023 | |
| 1024 | mbtk_at_fs_printf("+MFSLS: SUBDIRECTORIES:"); |
| 1025 | fs_dir.fs_ls_cb_typedef = fs_ls_at_cb_func; |
| 1026 | at_fs_ret = fs_cmd_process_list_dir(tmp_path,&fs_dir); |
| 1027 | |
| 1028 | mbtk_at_fs_printf("+MFSLS: FILES:"); |
| 1029 | fs_file.fs_ls_cb_typedef = fs_ls_at_cb_func; |
| 1030 | at_fs_ret = fs_cmd_process_list_file(tmp_path,&fs_file); |
| 1031 | |
| 1032 | exit: |
| 1033 | mbtk_at_fs_printf(send_buf); |
| 1034 | mbtk_at_fs_printf("end"); |
| 1035 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1036 | return at_fs_ret; |
| 1037 | } |
| 1038 | |
| 1039 | int mbtk_at_fs_list_set(int type) |
| 1040 | { |
| 1041 | int at_fs_ret = 0; |
| 1042 | char send_buf[SENDBUFF] = {0}; |
| 1043 | char *fs_pwd_path = NULL; |
| 1044 | mbtk_fs_dir_file_info_s fs_dir_file, fs_dir, fs_file; |
| 1045 | char tmp_path[FS_NAME_LEN] = {0}; |
| 1046 | |
| 1047 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1048 | { |
| 1049 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1050 | return at_fs_ret; |
| 1051 | } |
| 1052 | |
| 1053 | if ((at_fs_ret = path_pass()) == -1) |
| 1054 | goto exit; |
| 1055 | |
| 1056 | fs_pwd_path = GET_PWD();//获取当前路径 |
| 1057 | |
| 1058 | strcpy(tmp_path, fs_pwd_path); |
| 1059 | |
| 1060 | if ((at_fs_ret = pwd_def_dir(tmp_path)) == -1) |
| 1061 | goto exit; |
| 1062 | |
| 1063 | if (tmp_path != NULL) |
| 1064 | { |
| 1065 | LOGE("OPEN this dir [%s]\n", tmp_path); |
| 1066 | } |
| 1067 | else |
| 1068 | { |
| 1069 | at_fs_ret = -1; |
| 1070 | LOGE("OPEN this dir errno [%s]\n", tmp_path); |
| 1071 | goto exit; |
| 1072 | } |
| 1073 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1074 | mbtk_at_fs_par.at_fs_type = type; |
| 1075 | |
| 1076 | switch (mbtk_at_fs_par.at_fs_type) |
| 1077 | { |
| 1078 | case 0://显示目录和文件名称 |
| 1079 | { |
| 1080 | mbtk_at_fs_printf("+MFSLS:\n"); |
| 1081 | fs_dir_file.fs_ls_cb_typedef = fs_ls_at_cb_func; |
| 1082 | at_fs_ret = fs_cmd_process_list_all(tmp_path, &fs_dir_file); |
| 1083 | |
| 1084 | if(at_fs_ret != 0) |
| 1085 | { |
| 1086 | printf("+MFSLS: FAIL,<%d>\n",at_fs_ret); |
| 1087 | sprintf(send_buf,"+MFSLS: FAIL,<%d>\n",at_fs_ret); |
| 1088 | } |
| 1089 | else |
| 1090 | { |
| 1091 | printf("+MFSLS:SUBDIRECTORIES: %d FILES: %d \r\n", mbtk_at_fs_par.dir_num, mbtk_at_fs_par.file_num); |
| 1092 | sprintf(send_buf,"+MFSLS: SUBDIRECTORIES: %d FILES: %d \n", mbtk_at_fs_par.dir_num, mbtk_at_fs_par.file_num); |
| 1093 | } |
| 1094 | break; |
| 1095 | } |
| 1096 | case 1://显示目录名称 |
| 1097 | { |
| 1098 | mbtk_at_fs_printf("+MFSLS:"); |
| 1099 | fs_dir.fs_ls_cb_typedef = fs_ls_at_cb_func; |
| 1100 | at_fs_ret = fs_cmd_process_list_dir(tmp_path,&fs_dir); |
| 1101 | |
| 1102 | if(at_fs_ret != 0) |
| 1103 | { |
| 1104 | printf("+MFSLS: FAIL,<%d>\n",at_fs_ret); |
| 1105 | sprintf(send_buf,"+MFSRMDIR: FAIL,<%d>\n",at_fs_ret); |
| 1106 | } |
| 1107 | else |
| 1108 | { |
| 1109 | printf("+MFSLS:SUBDIRECTORIES: %d \r\n", mbtk_at_fs_par.dir_num); |
| 1110 | sprintf(send_buf,"+MFSLS: SUBDIRECTORIES: %d \n", mbtk_at_fs_par.dir_num); |
| 1111 | } |
| 1112 | break; |
| 1113 | } |
| 1114 | case 2://显示文件名称 |
| 1115 | { |
| 1116 | mbtk_at_fs_printf("+MFSLS:"); |
| 1117 | fs_file.fs_ls_cb_typedef = fs_ls_at_cb_func; |
| 1118 | at_fs_ret = fs_cmd_process_list_file(tmp_path,&fs_file); |
| 1119 | |
| 1120 | if(at_fs_ret != 0) |
| 1121 | { |
| 1122 | printf("+MFSLS: FAIL,<%d>\n",at_fs_ret); |
| 1123 | sprintf(send_buf,"+MFSRMDIR: FAIL,<%d>\n",at_fs_ret); |
| 1124 | } |
| 1125 | else |
| 1126 | { |
| 1127 | printf("+MFSLS:FILES: %d \r\n",mbtk_at_fs_par.file_num); |
| 1128 | sprintf(send_buf,"+MFSLS: FILES: %d \n",mbtk_at_fs_par.file_num); |
| 1129 | } |
| 1130 | break; |
| 1131 | } |
| 1132 | default: |
| 1133 | { |
| 1134 | LOGE("ERROR mbtk_at_fs_par.at_fs_type \n"); |
| 1135 | at_fs_ret = -1; |
| 1136 | break; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | exit: |
| 1141 | mbtk_at_fs_printf(send_buf); |
| 1142 | mbtk_at_fs_printf("end"); |
| 1143 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1144 | return at_fs_ret; |
| 1145 | } |
| 1146 | |
| 1147 | int mbtk_at_fs_cd(char *fs_dir) |
| 1148 | { |
| 1149 | int at_fs_ret = 0; |
| 1150 | char send_buf[SENDBUFF] = {0}; |
| 1151 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1152 | |
| 1153 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1154 | { |
| 1155 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1156 | return at_fs_ret; |
| 1157 | } |
| 1158 | |
| 1159 | if ((at_fs_ret = path_pass()) == -1) |
| 1160 | goto exit; |
| 1161 | |
| 1162 | at_fs_ret = fs_cmd_process_cd(fs_dir); |
| 1163 | |
| 1164 | if(at_fs_ret == 0) |
| 1165 | { |
| 1166 | sprintf(send_buf,"+MFSCD: %s\r\n",fs_dir); |
| 1167 | } |
| 1168 | |
| 1169 | exit: |
| 1170 | mbtk_at_fs_printf(send_buf); |
| 1171 | mbtk_at_fs_printf("end"); |
| 1172 | return at_fs_ret; |
| 1173 | } |
| 1174 | |
| 1175 | int mbtk_at_fs_delfile(char *fs_dir_file) |
| 1176 | { |
| 1177 | int at_fs_ret = 0; |
| 1178 | char send_buf[SENDBUFF] = {0}; |
| 1179 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1180 | |
| 1181 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1182 | { |
| 1183 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1184 | return at_fs_ret; |
| 1185 | } |
| 1186 | |
| 1187 | if ((at_fs_ret = path_pass()) == -1) |
| 1188 | goto exit; |
| 1189 | |
| 1190 | at_fs_ret = fs_cmd_process_delfile(fs_dir_file); |
| 1191 | |
| 1192 | exit: |
| 1193 | mbtk_at_fs_printf(send_buf); |
| 1194 | mbtk_at_fs_printf("end"); |
| 1195 | return at_fs_ret; |
| 1196 | } |
| 1197 | |
| 1198 | int mbtk_at_fs_attri(char *fs_file) |
| 1199 | { |
| 1200 | int at_fs_ret = 0, len_size; |
| 1201 | char Filepath[MBTK_FS_MAX + 1]={0}; |
| 1202 | char Pathtmp[MBTK_FS_MAX + 1]={0}; |
| 1203 | char send_buf[SENDBUFF] = {0}; |
| 1204 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1205 | |
| 1206 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1207 | { |
| 1208 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1209 | return at_fs_ret; |
| 1210 | } |
| 1211 | |
| 1212 | if ((at_fs_ret = path_pass()) == -1) |
| 1213 | goto exit; |
| 1214 | |
| 1215 | memcpy(Filepath, fs_file, sizeof(Filepath)); |
| 1216 | |
| 1217 | if ((at_fs_ret = path_redirection(Filepath, Pathtmp)) == -1) |
| 1218 | { |
| 1219 | LOGE("input path_redirection error:[%d]\n", errno); |
| 1220 | goto exit; |
| 1221 | } |
| 1222 | |
| 1223 | if (0 != (access(Pathtmp, F_OK))) |
| 1224 | { |
| 1225 | LOGE("access file is null\n"); |
| 1226 | at_fs_ret = -1; |
| 1227 | goto exit; |
| 1228 | } |
| 1229 | |
| 1230 | len_size = read_file_len(Pathtmp); |
| 1231 | |
| 1232 | if(at_fs_ret != 0) |
| 1233 | { |
| 1234 | printf("+MFSATTRI : FAIL,<%d>\n",at_fs_ret); |
| 1235 | sprintf(send_buf,"+MFSATTRI : FAIL,<%d>\n",at_fs_ret); |
| 1236 | goto exit; |
| 1237 | } |
| 1238 | else |
| 1239 | { |
| 1240 | printf("+MFSATTRI: <%d>\n",len_size); |
| 1241 | sprintf(send_buf,"+MFSATTRI: %d\n",len_size); |
| 1242 | goto exit; |
| 1243 | } |
| 1244 | |
| 1245 | exit: |
| 1246 | mbtk_at_fs_printf(send_buf); |
| 1247 | mbtk_at_fs_printf("end"); |
| 1248 | return at_fs_ret; |
| 1249 | } |
| 1250 | |
| 1251 | int mbtk_at_fs_writefile(mbtk_fs_data_info_t *fs_w_data) |
| 1252 | { |
| 1253 | int at_fs_ret = 0, len_size, fd, write_mode, write_size; |
| 1254 | char get_buf[FS_DATA_LEN]; |
| 1255 | char Filepath[MBTK_FS_MAX + 1]={0}; |
| 1256 | char Pathtmp[MBTK_FS_MAX + 1]={0}; |
| 1257 | char send_buf[SENDBUFF] = {0}; |
| 1258 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1259 | |
| 1260 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1261 | { |
| 1262 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1263 | return at_fs_ret; |
| 1264 | } |
| 1265 | |
| 1266 | if ((at_fs_ret = path_pass()) == -1) |
| 1267 | goto exit; |
| 1268 | |
| 1269 | memset(get_buf, 0x00, sizeof(get_buf)); |
| 1270 | |
| 1271 | write_size = fs_w_data->fs_data_size; |
| 1272 | memcpy(get_buf, fs_w_data->fs_data, write_size); |
| 1273 | memcpy(Filepath, fs_w_data->fs_data_name, strlen(fs_w_data->fs_data_name)); |
| 1274 | write_mode = fs_w_data->fs_data_mode; |
| 1275 | |
| 1276 | if ((at_fs_ret = path_redirection(Filepath, Pathtmp)) == -1) |
| 1277 | { |
| 1278 | LOGE("input path_redirection error:[%d]\n", errno); |
| 1279 | goto exit; |
| 1280 | } |
| 1281 | |
| 1282 | if (0 != (access(Pathtmp, F_OK))) |
| 1283 | { |
| 1284 | LOGE("access file is null\n"); |
| 1285 | at_fs_ret = -1; |
| 1286 | goto exit; |
| 1287 | } |
| 1288 | |
| 1289 | fd=file_open(Pathtmp,O_WRONLY); |
| 1290 | if(-1 == fd) |
| 1291 | { |
| 1292 | LOGE("Write file error\n"); |
| 1293 | at_fs_ret = -1; |
| 1294 | file_close(fd); |
| 1295 | goto exit; |
| 1296 | } |
| 1297 | else |
| 1298 | { |
| 1299 | LOGE("Open file SUCCESS\r\n"); |
| 1300 | |
| 1301 | if (write_mode == 0) |
| 1302 | { //将文件读写指针移动到文件头 |
| 1303 | lseek(fd,0,SEEK_SET); |
| 1304 | } |
| 1305 | else if (write_mode == 1) |
| 1306 | { //将文件读写指针移动到文件尾 |
| 1307 | lseek(fd,0,SEEK_END); |
| 1308 | } |
| 1309 | else |
| 1310 | { |
| 1311 | LOGE("write_mode error\n"); |
| 1312 | at_fs_ret = -1; |
| 1313 | file_close(fd); |
| 1314 | goto exit; |
| 1315 | } |
| 1316 | //这两步可以加出错处理 |
| 1317 | file_write(fd, get_buf, write_size); |
| 1318 | fsync(fd); |
| 1319 | file_close(fd); |
| 1320 | } |
| 1321 | |
| 1322 | exit: |
| 1323 | mbtk_at_fs_printf(send_buf); |
| 1324 | mbtk_at_fs_printf("end"); |
| 1325 | return at_fs_ret; |
| 1326 | } |
| 1327 | |
| 1328 | //int mbtk_at_fs_readfile(void *dir_file, int read_offset, int read_size, char* read_buff) |
| 1329 | int mbtk_at_fs_readfile(void *dir_file, int read_offset, int read_size) |
| 1330 | { |
r.xiao | 5e04028 | 2023-11-13 18:46:28 -0800 | [diff] [blame] | 1331 | int at_fs_ret = 0, fd, count = 0,file_size, len_count = 0, len = 0; |
r.xiao | 21da0e5 | 2023-11-13 04:05:27 -0800 | [diff] [blame] | 1332 | char read_buf[1024]; |
| 1333 | char Filepath[MBTK_FS_MAX + 1]={0}; |
| 1334 | char Pathtmp[MBTK_FS_MAX + 1]={0}; |
| 1335 | char send_buf[1024] = {0}; |
| 1336 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1337 | |
| 1338 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1339 | { |
| 1340 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1341 | return at_fs_ret; |
| 1342 | } |
| 1343 | |
| 1344 | if ((at_fs_ret = path_pass()) == -1) |
| 1345 | goto exit; |
| 1346 | |
| 1347 | memset(read_buf, 0x00, sizeof(read_buf)); |
| 1348 | |
| 1349 | memcpy(Filepath, dir_file, sizeof(Filepath)); |
| 1350 | |
| 1351 | if ((at_fs_ret = path_redirection(Filepath, Pathtmp)) == -1) |
| 1352 | { |
| 1353 | LOGE("input path_redirection error:[%d]\n", errno); |
| 1354 | goto exit; |
| 1355 | } |
| 1356 | |
| 1357 | if (0 != (access(Pathtmp, F_OK))) |
| 1358 | { |
| 1359 | LOGE("access file is null\n"); |
| 1360 | at_fs_ret = -1; |
| 1361 | goto exit; |
| 1362 | } |
| 1363 | |
| 1364 | file_size = read_file_len(Pathtmp); |
| 1365 | |
| 1366 | |
| 1367 | fd = file_open(Pathtmp, O_RDONLY); |
| 1368 | if (fd == -1) |
| 1369 | { |
| 1370 | LOGE("Read file error\n"); |
| 1371 | at_fs_ret = -1; |
| 1372 | goto exit; |
| 1373 | } |
| 1374 | else |
| 1375 | { |
| 1376 | LOGE("Open file SUCCESS\r\n"); |
| 1377 | if (read_offset >= file_size) |
| 1378 | { |
| 1379 | LOGE("read_offset > file_size \n"); |
| 1380 | at_fs_ret = -1; |
| 1381 | goto exit; |
| 1382 | } |
| 1383 | |
| 1384 | lseek(fd,read_offset,SEEK_SET); |
| 1385 | |
| 1386 | //由于at客户端缓存才1k,切片512字节大小传输 |
| 1387 | if (read_size > 0) |
| 1388 | { |
| 1389 | if (0 < read_size && read_size <= 512) //单次发生:文件大小-偏移量 < 发送大小 |
| 1390 | { |
| 1391 | len = read(fd, read_buf, read_size); //返回实际读取的大小 |
| 1392 | LOGE("len = [%d]\n",len); |
| 1393 | if (len > 0) |
| 1394 | { |
| 1395 | //LOGE("read_buff = [%s]\n",read_buf); |
| 1396 | sprintf(send_buf,"+MFSREAD: DATA: %d, %s\n",len, read_buf); |
| 1397 | } |
| 1398 | } |
| 1399 | else if(read_size <= 1024 && read_size > 512) //多次发送: |
| 1400 | { |
| 1401 | len = read(fd, read_buf, read_size); |
| 1402 | count++; |
| 1403 | LOGE("count = [%d]\n",count); |
| 1404 | mbtk_at_fs_data_cb(read_buf, len); |
| 1405 | sprintf(send_buf, "+MFSREAD: DATA: [%d] count: [%d]\r\n",len,count); |
| 1406 | mbtk_at_fs_printf(send_buf); |
| 1407 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1408 | } |
| 1409 | else |
| 1410 | { |
| 1411 | LOGE("read_size errno\n"); |
| 1412 | } |
| 1413 | } |
| 1414 | else if (read_size == 0)//全部读取完成 |
| 1415 | { |
| 1416 | if ((file_size-read_offset) < 512) |
| 1417 | { |
| 1418 | len = read(fd, read_buf, 1024); //返回实际读取的大小 |
| 1419 | LOGE("len = [%d]\n",len); |
| 1420 | if (len > 0) |
| 1421 | { |
| 1422 | //LOGE("read_buff = [%s]\n",read_buf); |
| 1423 | sprintf(send_buf,"+MFSREAD: DATA: %d, %s\n",len, read_buf); |
| 1424 | } |
| 1425 | } |
| 1426 | else |
| 1427 | { |
| 1428 | while((file_size-read_offset) > len_count) |
| 1429 | { |
| 1430 | len = read(fd, read_buf, 1024); |
| 1431 | count++; |
| 1432 | LOGE("count = [%d]\n",count); |
| 1433 | mbtk_at_fs_data_cb(read_buf, len); |
| 1434 | len_count += len; |
| 1435 | sprintf(send_buf, "+MFSREAD: DATA: [%d] count: [%d]\r\n",len_count, count); |
| 1436 | mbtk_at_fs_printf(send_buf); |
| 1437 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | else |
| 1442 | { |
| 1443 | LOGE("read_size errno =[%d]\n",errno); |
| 1444 | } |
| 1445 | |
| 1446 | file_close(fd); |
| 1447 | } |
| 1448 | |
| 1449 | exit: |
| 1450 | mbtk_at_fs_printf(send_buf); |
| 1451 | mbtk_at_fs_printf("end"); |
| 1452 | return at_fs_ret; |
| 1453 | } |
| 1454 | |
| 1455 | int mbtk_at_fs_rename(char *old_file, char *new_file) |
| 1456 | { |
| 1457 | int at_fs_ret = 0; |
| 1458 | char send_buf[SENDBUFF] = {0}; |
| 1459 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1460 | |
| 1461 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1462 | { |
| 1463 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1464 | return at_fs_ret; |
| 1465 | } |
| 1466 | |
| 1467 | if ((at_fs_ret = path_pass()) == -1) |
| 1468 | goto exit; |
| 1469 | |
| 1470 | if (old_file == NULL || new_file == NULL) |
| 1471 | { |
| 1472 | at_fs_ret = -1; |
| 1473 | LOGE("old_file or new_file is null\n"); |
| 1474 | goto exit; |
| 1475 | } |
| 1476 | |
| 1477 | at_fs_ret = fs_cmd_process_rename(old_file, new_file); |
| 1478 | |
| 1479 | exit: |
| 1480 | mbtk_at_fs_printf(send_buf); |
| 1481 | mbtk_at_fs_printf("end"); |
| 1482 | return at_fs_ret; |
| 1483 | } |
| 1484 | |
| 1485 | int mbtk_at_fs_creatfile(char *fs_file) |
| 1486 | { |
| 1487 | int at_fs_ret = 0; |
| 1488 | char send_buf[SENDBUFF] = {0}; |
| 1489 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1490 | |
| 1491 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1492 | { |
| 1493 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1494 | return at_fs_ret; |
| 1495 | } |
| 1496 | |
| 1497 | if ((at_fs_ret = path_pass()) == -1) |
| 1498 | goto exit; |
| 1499 | |
| 1500 | at_fs_ret = fs_cmd_process_creatfile(fs_file); |
| 1501 | |
| 1502 | exit: |
| 1503 | mbtk_at_fs_printf(send_buf); |
| 1504 | mbtk_at_fs_printf("end"); |
| 1505 | return at_fs_ret; |
| 1506 | } |
| 1507 | |
| 1508 | int mbtk_at_fs_memory(uint64 *total_len,uint64 *available_len) |
| 1509 | { |
| 1510 | int at_fs_ret = 0; |
| 1511 | char send_buf[SENDBUFF] = {0}; |
| 1512 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1513 | |
| 1514 | at_fs_ret = fs_cmd_process_memory(total_len, available_len); |
| 1515 | |
| 1516 | exit: |
| 1517 | mbtk_at_fs_printf(send_buf); |
| 1518 | mbtk_at_fs_printf("end"); |
| 1519 | return at_fs_ret; |
| 1520 | } |
| 1521 | |
| 1522 | int mbtk_at_fs_copy(char *old_file, char *new_file, int mode) |
| 1523 | { |
| 1524 | int at_fs_ret = 0; |
| 1525 | char send_buf[SENDBUFF] = {0}; |
| 1526 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1527 | |
| 1528 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1529 | { |
| 1530 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1531 | return at_fs_ret; |
| 1532 | } |
| 1533 | |
| 1534 | if ((at_fs_ret = path_pass()) == -1) |
| 1535 | goto exit; |
| 1536 | |
| 1537 | if (old_file == NULL || new_file == NULL) |
| 1538 | { |
| 1539 | at_fs_ret = -1; |
| 1540 | LOGE("old_file or new_file is null\n"); |
| 1541 | goto exit; |
| 1542 | } |
| 1543 | |
| 1544 | at_fs_ret = fs_cmd_process_copy(old_file, new_file, mode); |
| 1545 | |
| 1546 | exit: |
| 1547 | mbtk_at_fs_printf(send_buf); |
| 1548 | mbtk_at_fs_printf("end"); |
| 1549 | return at_fs_ret; |
| 1550 | } |
| 1551 | |
| 1552 | int mbtk_at_fs_certload(mbtk_fs_data_certmode_t *certmode) |
| 1553 | { |
| 1554 | int at_fs_ret = 0, fd, cert_mode, write_size; |
| 1555 | char tmp_buf[FS_NAME_LEN]; |
| 1556 | char get_buf[FS_DATA_LEN]; |
| 1557 | char send_buf[SENDBUFF] = {0}; |
| 1558 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1559 | |
| 1560 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1561 | { |
| 1562 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1563 | return at_fs_ret; |
| 1564 | } |
| 1565 | |
| 1566 | if ((at_fs_ret = path_pass()) == -1) |
| 1567 | goto exit; |
| 1568 | |
| 1569 | memset(get_buf, 0x00, sizeof(get_buf)); |
| 1570 | memset(tmp_buf, 0x00, sizeof(tmp_buf)); |
| 1571 | |
| 1572 | write_size = certmode->fs_data_size; |
| 1573 | memcpy(get_buf, certmode->fs_data, write_size); |
| 1574 | cert_mode = certmode->fs_data_certmode; |
| 1575 | |
| 1576 | switch(cert_mode) |
| 1577 | { |
| 1578 | case 0: |
| 1579 | { |
| 1580 | memcpy(tmp_buf, FS_CERT_0, sizeof(tmp_buf)); |
| 1581 | break; |
| 1582 | } |
| 1583 | case 1: |
| 1584 | { |
| 1585 | memcpy(tmp_buf, FS_CERT_1, sizeof(tmp_buf)); |
| 1586 | break; |
| 1587 | } |
| 1588 | case 2: |
| 1589 | { |
| 1590 | memcpy(tmp_buf, FS_CERT_2, sizeof(tmp_buf)); |
| 1591 | break; |
| 1592 | } |
| 1593 | case 3: |
| 1594 | { |
| 1595 | memcpy(tmp_buf, FS_CERT_3, sizeof(tmp_buf)); |
| 1596 | break; |
| 1597 | } |
| 1598 | case 4: |
| 1599 | { |
| 1600 | memcpy(tmp_buf, FS_CERT_4, sizeof(tmp_buf)); |
| 1601 | break; |
| 1602 | } |
| 1603 | default: |
| 1604 | { |
| 1605 | LOGE("ERROR cert_mode \n"); |
| 1606 | at_fs_ret = -1; |
| 1607 | break; |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | if (at_fs_ret == -1) |
| 1612 | goto exit; |
| 1613 | |
| 1614 | if (fd = file_open(tmp_buf, O_RDWR | O_CREAT | O_TRUNC) == -1) |
| 1615 | { |
| 1616 | LOGE("Open file is err [%d]\n",errno); |
| 1617 | at_fs_ret = -1; |
| 1618 | goto exit; |
| 1619 | } |
| 1620 | else |
| 1621 | { |
| 1622 | //通过at客户端往里面写数据 |
| 1623 | LOGE("Open file SUCCESS\r\n"); |
| 1624 | if (lseek(fd,0,SEEK_SET) == -1 ) |
| 1625 | { |
| 1626 | LOGE("lseek error = [%d]\n",errno); |
| 1627 | at_fs_ret = -1; |
| 1628 | file_close(fd); |
| 1629 | goto exit; |
| 1630 | } |
| 1631 | //这两步可以加出错处理 |
| 1632 | file_write(fd, get_buf, write_size); |
| 1633 | fsync(fd); |
| 1634 | file_close(fd); |
| 1635 | } |
| 1636 | |
| 1637 | exit: |
| 1638 | mbtk_at_fs_printf(send_buf); |
| 1639 | mbtk_at_fs_printf("end"); |
| 1640 | return at_fs_ret; |
| 1641 | } |
| 1642 | |
| 1643 | int mbtk_at_fs_fileload(mbtk_fs_data_file_t *file) |
| 1644 | { |
| 1645 | int at_fs_ret = 0, fd, write_size; |
| 1646 | char get_buf[FS_DATA_LEN]; |
| 1647 | char Filepath[MBTK_FS_MAX + 1]={0}; |
| 1648 | char Pathtmp[MBTK_FS_MAX + 1]={0}; |
| 1649 | char send_buf[SENDBUFF] = {0}; |
| 1650 | memset(send_buf, 0x00, sizeof(send_buf)); |
| 1651 | |
| 1652 | if ((at_fs_ret = creat_filesystem_def_dir()) == -1) |
| 1653 | { |
| 1654 | LOGE("creat_filesystem_def_dir error:[%d]\n", errno); |
| 1655 | return at_fs_ret; |
| 1656 | } |
| 1657 | |
| 1658 | if ((at_fs_ret = path_pass()) == -1) |
| 1659 | goto exit; |
| 1660 | |
| 1661 | memset(get_buf, 0x00, sizeof(get_buf)); |
| 1662 | |
| 1663 | if (file == NULL) |
| 1664 | { |
| 1665 | at_fs_ret = -1; |
| 1666 | LOGE("file is null\n"); |
| 1667 | goto exit; |
| 1668 | } |
| 1669 | |
| 1670 | write_size = file->fs_data_size; |
| 1671 | memcpy(get_buf, file->fs_data, write_size); |
| 1672 | memcpy(Filepath, file->fs_data_name, strlen(file->fs_data_name)); |
| 1673 | |
| 1674 | if ((at_fs_ret = path_redirection(Filepath, Pathtmp)) == -1) |
| 1675 | { |
| 1676 | LOGE("input path_redirection error:[%d]\n", errno); |
| 1677 | goto exit; |
| 1678 | } |
| 1679 | |
| 1680 | if (fd = file_open(Pathtmp, O_RDWR | O_CREAT | O_TRUNC) == -1) |
| 1681 | { |
| 1682 | LOGE("open file is err [%d]\n",errno); |
| 1683 | } |
| 1684 | else |
| 1685 | { |
| 1686 | //通过at客户端往里面写数据 |
| 1687 | LOGE("Open file SUCCESS\r\n"); |
| 1688 | if (lseek(fd,0,SEEK_SET) == -1 ) |
| 1689 | { |
| 1690 | LOGE("lseek error = [%d]\n",errno); |
| 1691 | at_fs_ret = -1; |
| 1692 | file_close(fd); |
| 1693 | goto exit; |
| 1694 | } |
| 1695 | //这两步可以加出错处理 |
| 1696 | file_write(fd, get_buf, write_size); |
| 1697 | fsync(fd); |
| 1698 | file_close(fd); |
| 1699 | } |
| 1700 | |
| 1701 | exit: |
| 1702 | mbtk_at_fs_printf(send_buf); |
| 1703 | mbtk_at_fs_printf("end"); |
| 1704 | return at_fs_ret; |
| 1705 | } |
| 1706 | |
| 1707 | |