blob: c884e2ed409f73d5f134800cb644c2d35cf4b5b6 [file] [log] [blame]
/*************************************************************
Description:
MBTK fs c file.
Author:
LiuBin
Date:
2020/4/30 13:51:42
*************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <linux/msg.h>
#include <sys/statfs.h>
#include <sys/vfs.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <math.h>
#include <errno.h>
#include <linux/magic.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <pthread.h>
#include <cutils/properties.h>
#include "mbtk_type.h"
#include "mbtk_fs.h"
/*************************************************************
Constants and Macros
*************************************************************/
/*************************************************************
Variables:local
*************************************************************/
int DEF_DIR_S = 0;
int msgid;
/*************************************************************
Variables:public
*************************************************************/
//mbtk_fs
struct my_msg //消息队列结构体
{
long int my_msg_type;
char *ptr;
}mbtk_at_fs_msg;
typedef struct
{
char in_dir_file[MBTK_FS_MAX + 1];
char out_dir_file[MBTK_FS_MAX + 1];
} mbtk_fs_file_dir_info_s;
mbtk_fs_file_dir_info_s mbtk_at_fs_par_at={0};
/*************************************************************
Local Function Declaration
*************************************************************/
int mbtk_at_fs_msgid(int *at_msgid);
void mbtk_at_fs_printf(void *data);
static bool fs_is_space_char(char ch);
static bool fs_str_empty(char *str);
static int read_file_len(char *file);
static char *GET_PWD();
static int pwd_def_dir(char* path_dir);
static int path_pass();
static int creat_filesystem_def_dir();
void fs_ls_at_cb_func(void *file_list_cb);
void fs_ls_at_cb_func_num(void *file_list_cb);
void mbtk_at_fs_data_cb(void *data, int data_len);
/*************************************************************
Local Function Definitions
*************************************************************/
/*
//收
void mbtk_at_fs_printf()
{
int msgid;
mbtk_at_msg.my_msg_type=3;
msgid=msgget(12,0666|IPC_CREAT);
MBTK_FUNC(mbtk_at_msgid)(&msgid);
//char read_buf[MAXMSG];
while(1)
{
msgrcv(msgid,&mbtk_at_msg,4,mbtk_at_msg.my_msg_type,0);
if(strncmp(mbtk_at_msg.ptr,"end",3)==0)
break;
ATRESP(IND_REQ_HANDLE , ATCI_RESULT_CODE_NULL, 0, mbtk_at_msg.ptr);
free(mbtk_at_msg.ptr);
mbtk_at_msg.ptr=NULL;
}
msgctl(msgid,IPC_RMID,0);
}
*/
//发
int mbtk_at_fs_msgid(int *at_msgid)
{
msgid = *at_msgid;
return 0;
}
void mbtk_at_fs_printf(void *data)
{
char *ptr = malloc(strlen(data)+1);
memset(ptr,0,strlen(data)+1);
memcpy(ptr, data, strlen(data)+1);
mbtk_at_fs_msg.ptr = ptr;
mbtk_at_fs_msg.my_msg_type=4;
msgsnd(msgid,&mbtk_at_fs_msg,sizeof(&mbtk_at_fs_msg),0); //发送数据到缓冲区
}
static bool fs_is_space_char(char ch)
{
if(ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n')
return TRUE;
return FALSE;
}
static bool fs_str_empty(char *str)
{
if(str == NULL || strlen(str) == 0)
return TRUE;
return FALSE;
}
static int read_file_len(char* file)
{
int size;
FILE* fp;
fp=fopen(file,"r");
if(NULL==fp)
{
fclose(fp);
}
else
{
fseek(fp,0,SEEK_END); //将文件读写指针移动到文件尾
size=ftell(fp); //ftell函数获取从文件尾移动到文件开头的偏移量
fseek(fp,0,SEEK_SET);//恢复指针位置
fclose(fp);
}
return size;
}
//Gets the current working directory
static char *GET_PWD()
{
char dir_buff[1024] = {0};
char *dir = NULL;
dir = getcwd(dir_buff,sizeof(dir_buff));
if (dir == NULL)
{
LOGE(" Not current working directory errno = [%d]\n",errno);
}
//LOGE("getcwd dir=[%s]\n",dir);
return dir;
}
//Default directory determinations
static int pwd_def_dir(char* path_dir)
{
int ret = 0;
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 || \
strncmp(path_dir,DFF_DIR_ETC,3) == 0 || strncmp(path_dir,DFF_DIR_LIB,3) == 0 || strncmp(path_dir,DFF_DIR_LOG,3) == 0 || \
strncmp(path_dir,DFF_DIR_MNT,3) == 0 || strncmp(path_dir,DFF_DIR_OVERLAY,3) == 0 || strncmp(path_dir,DFF_DIR_PROC,3) == 0 || \
strncmp(path_dir,DFF_DIR_ROM,3) == 0 || strncmp(path_dir,DFF_DIR_ROOT,3) == 0 || strncmp(path_dir,DFF_DIR_SBIN,3) == 0 || \
strncmp(path_dir,DFF_DIR_SYS,3) == 0 || strncmp(path_dir,DFF_DIR_TMP,3) == 0 || strncmp(path_dir,DFF_DIR_USR,3) == 0 || \
strncmp(path_dir,DFF_DIR_VAR,3) == 0 || strncmp(path_dir,DFF_DIR_WWW,3) == 0 )
{
ret = -1;
LOGE("Enter the default directory \n");
}
return ret;
}
//
static int path_pass()
{
int at_fs_ret = 0;
char *fs_pwd_path = NULL;
char tmp_path[FS_NAME_LEN] = {0};
fs_pwd_path = GET_PWD();//获取当前路径
strcpy(tmp_path, fs_pwd_path);
if ((at_fs_ret = pwd_def_dir(tmp_path)) == -1)
return at_fs_ret;
if (strncmp(tmp_path, PART_FS, 3) != 0)
{
if (strcmp(tmp_path, ROOT_PATH) == 0)//首次进入,所以不用指定前一个字符匹配,全部匹配
{
at_fs_ret = chdir(PART_FS);//首次进入切换路径
LOGE(">dir == [%s]\n", getcwd(NULL,NULL));
if(-1 == at_fs_ret)
{
LOGE("chdir path error:[%d]\n", errno);
return at_fs_ret;
}
}
else
{
at_fs_ret = -1;
LOGE("Not specified dir %s\n", tmp_path);
return at_fs_ret;
}
}
return at_fs_ret;
}
//创建文件系统的默认操作路径
static int creat_filesystem_def_dir()
{
int ret = 0;
if (DEF_DIR_S == 0)//以全局变量判断是否创建了默认路径
{
if (access(PART_FS, F_OK) == 0)
{
DEF_DIR_S = 1;
}
else
{
ret = mkdir(PART_FS, 0766);//创建路径
if(-1 == ret)
{
LOGE("mkdir path error:[%d]\n", errno);
return ret;
}
ret = chdir(PART_FS);//切换路径
LOGE(">dir == [%s]\n", getcwd(NULL,NULL));
if(-1 == ret)
{
LOGE("chdir path error:[%d]\n", errno);
return ret;
}
DEF_DIR_S = 1;
}
}
else if (DEF_DIR_S == 1)
{
;//不进行处理
}
else
{
LOGE("mkdir path DEF_DIR_S error:[%d]\n", errno);
}
return ret;
}
//拼接传入的路径到默认操作路径
static int path_redirection(char *in_path, char *out_path)
{
int ret = 0, len = 0;
char *tmp_dir = NULL;
char def_fs[FS_NAME_LEN] = {0};
len = strlen(in_path);
if ( len + 5 >= 127 )
{
LOGE("in_path len more than error:[%d]\n", errno);
ret = -1;
return ret;
}
strcpy(def_fs, PART_FS);
tmp_dir = strncat(def_fs, in_path, sizeof(def_fs)-1);//先拼接再判断
if (strncmp(PART_FS_, tmp_dir, 4) == 0)
{
memcpy(out_path, tmp_dir, MBTK_FS_MAX);
}
else
{
ret = -1;
return ret;
}
memset(def_fs, 0x00, sizeof(def_fs));
return ret;
}
void fs_ls_at_cb_func(void *file_list_cb)
{
char resp_buf[1024 + 1] = {0};
printf("+MFSLS::\r\n%s\n", ((mbtk_fs_dir_file_info_s *)file_list_cb)->name);
sprintf(resp_buf, "%s\n",((mbtk_fs_dir_file_info_s *)file_list_cb)->name);
mbtk_at_fs_printf(resp_buf);
//ret = ATRESP(30163, 0, 0, resp_buf);
}
void fs_ls_at_cb_func_num(void *file_list_cb)
{
//不进行处理,只走流程
}
void mbtk_at_fs_data_cb(void *data, int data_len)
{
char resp_buf[1024];
printf("\n=====data_cb data[%s]=====\n=====data_cb data_len[%d]=====\n",data, data_len);
memset(resp_buf,0,1024);
mbtk_at_fs_printf(resp_buf);
if(data_len > 512)
{
memset(resp_buf,0,1024);
memcpy(resp_buf,data,512);
mbtk_at_fs_printf(resp_buf);
memset(resp_buf,0,1024);
sprintf(resp_buf, "DATA: <%d>", 512);
mbtk_at_fs_printf(resp_buf);
memset(resp_buf,0,1024);
sprintf(resp_buf, "+MFSREAD:");
mbtk_at_fs_printf(resp_buf);
memset(resp_buf,0,1024);
memcpy(resp_buf,data+512,data_len-512);
mbtk_at_fs_printf(resp_buf);
memset(resp_buf,0,1024);
sprintf(resp_buf, "DATA: <%d>", data_len-512);
mbtk_at_fs_printf(resp_buf);
}
else
{
memset(resp_buf,0,1024);
memcpy(resp_buf,data,data_len);
mbtk_at_fs_printf(resp_buf);
memset(resp_buf,0,1024);
sprintf(resp_buf, "<%d>", data_len);
mbtk_at_fs_printf(resp_buf);
}
}
//Creating a directory
static int fs_cmd_process_mkd(char *path)
{
int ret = 0;
char Filepath[MBTK_FS_MAX + 1];
char Pathtmp[MBTK_FS_MAX + 1];
char *pDir = NULL;
struct stat FileStat_t;
memset(Filepath, 0x00, sizeof(Filepath));
memset(Pathtmp, 0x00, sizeof(Pathtmp));
memset(&FileStat_t, 0x00, sizeof(FileStat_t));
memcpy(Filepath, path, sizeof(Filepath));
if ((ret = path_redirection(Filepath, Pathtmp)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
return ret;
}
//后期再看那个合适
#if 0
pDir = strtok(Filepath, "/");
strcat(Pathtmp, "/");
strcat(Pathtmp, pDir);
strcat(Pathtmp, "/");
memset(&FileStat_t, 0x00, sizeof(FileStat_t));
stat(Pathtmp, &FileStat_t);
if(!S_ISDIR(FileStat_t.st_mode))
{
ret = mkdir(Pathtmp, 0766);//创建路径
if(-1 == ret)
{
LOGE("mkdir path [%s] error:[%d]\n", __FUNCTION__, errno);
return ret;
}
}
while(NULL != ( pDir=strtok(NULL, "/") ))
{
strcat(Pathtmp, pDir);
strcat(Pathtmp, "/");
memset(&FileStat_t, 0x00, sizeof(FileStat_t));
stat(Pathtmp, &FileStat_t);
if(!S_ISDIR(FileStat_t.st_mode))
{
ret = mkdir(Pathtmp, 0766);//创建文件
if(-1 == ret)
{
LOGE("mkdir path [%s] error:[%d]\n", __FUNCTION__, errno);
return ret;
}
}
}
#else
stat(Pathtmp, &FileStat_t);
if(!S_ISDIR(FileStat_t.st_mode))
{
ret = mkdir(Pathtmp, 0766);//创建路径
if(-1 == ret)
{
LOGE("mkdir path error:[%d]\n", errno);
return ret;
}
}
else
{
ret = -1;
LOGE("not a directory error:[%d]\n", errno);
return ret;
}
#endif
return ret;
}
static int fs_cmd_process_rmkd(char *path)
{
int ret = 0;
char Filepath[MBTK_FS_MAX + 1]={0};
char Pathtmp[MBTK_FS_MAX + 1]={0};
memcpy(Filepath, path, sizeof(Filepath));
if ((ret = path_redirection(Filepath, Pathtmp)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
return ret;
}
if (access(Pathtmp, F_OK) != 0)
{
ret = -1;
LOGE("File does not exist error:[%d]\n", errno);
return ret;
}
//ret = rmdir(Filepath);
ret = rmdir(Pathtmp);
if (ret == -1)
{
LOGE("rmkdir path error:[%d]\n", errno);
}
return ret;
}
static int fs_cmd_process_list_dir(char *path,mbtk_fs_dir_file_info_s *fs_dir_list)
{
int ret = 0, count = 0, dir_count = 0, file_count = 0, len_name;
const char *temp_fs_path = NULL;
struct dirent *files;
struct stat FileStat_t;
DIR *dir;
mbtk_fs_dir_file_info_s fs_dir_file_t, fs_dir_t, fs_file_t;
char Filepath[MBTK_FS_MAX + 1];
memset(fs_dir_list, 0x0, sizeof(mbtk_fs_dir_file_info_s)-sizeof(void *));
temp_fs_path = path;
chdir(temp_fs_path);
dir = opendir(temp_fs_path);
if (dir == NULL)
{
ret = -1;
LOGE("Directory cannot be opened!\n");
return ret;
}
while ((files = readdir(dir)) != NULL)
{
if(strcmp(files->d_name, ".") == 0 || strcmp(files->d_name, "..") == 0)
{
continue;
}//跳过隐藏文件
memset(&FileStat_t, 0x00, sizeof(FileStat_t));
memset(&fs_dir_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s));
memset(&fs_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s));
memset(&fs_dir_t, 0x0, sizeof(mbtk_fs_dir_file_info_s));
len_name = strlen(files->d_name);
//目录或者文件夹名字长度不能超过256,可以的话这里也可以做出错处理;
stat(files->d_name,&FileStat_t);
if(S_ISDIR(FileStat_t.st_mode)) //递归调用解析其子目录下的文件夹
{
memcpy(fs_dir_t.name, files->d_name, sizeof(len_name));
dir_count++;
}
else
{
memcpy(fs_file_t.name, files->d_name, sizeof(len_name));
file_count++;
}
memcpy(fs_dir_file_t.name, files->d_name, sizeof(len_name));
count++;
//每次对一个文件或者目录处理,就加入里面
(fs_dir_list->fs_ls_cb_typedef)(&fs_dir_t);
}
closedir(dir);
//每次显示完成后,把读取的数量传给全局变量方便读取
//mbtk_at_fs_par.all_dir_file = count;
mbtk_at_fs_par.dir_num = dir_count;
//mbtk_at_fs_par.file_num = file_count;
return ret;
}
static int fs_cmd_process_list_file(char *path,mbtk_fs_dir_file_info_s *fs_file_list)
{
int ret = 0, count = 0, dir_count = 0, file_count = 0, len_name;
const char *temp_fs_path = NULL;
struct dirent *files;
struct stat FileStat_t;
DIR *dir;
mbtk_fs_dir_file_info_s fs_dir_file_t, fs_dir_t, fs_file_t;
char Filepath[MBTK_FS_MAX + 1];
memset(fs_file_list, 0x0, sizeof(mbtk_fs_dir_file_info_s)-sizeof(void *));
temp_fs_path = path;
chdir(temp_fs_path);
dir = opendir(temp_fs_path);
if (dir == NULL)
{
ret = -1;
LOGE("Directory cannot be opened!\n");
return ret;
}
while ((files = readdir(dir)) != NULL)
{
if(strcmp(files->d_name, ".") == 0 || strcmp(files->d_name, "..") == 0)
{
continue;
}//跳过隐藏文件
memset(&FileStat_t, 0x00, sizeof(FileStat_t));
memset(&fs_dir_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s));
memset(&fs_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s));
memset(&fs_dir_t, 0x0, sizeof(mbtk_fs_dir_file_info_s));
len_name = strlen(files->d_name);
//目录或者文件夹名字长度不能超过256,可以的话这里也可以做出错处理;
stat(files->d_name,&FileStat_t);
if(S_ISDIR(FileStat_t.st_mode)) //递归调用解析其子目录下的文件夹
{
memcpy(fs_dir_t.name, files->d_name, (len_name)+1);
dir_count++;
}
else
{
memcpy(fs_file_t.name, files->d_name, (len_name)+1);
file_count++;
}
memcpy(fs_dir_file_t.name, files->d_name, (len_name)+1);
count++;
//每次对一个文件或者目录处理,就加入里面
(fs_file_list->fs_ls_cb_typedef)(&fs_file_t);
}
//每次显示完成后,把读取的数量传给全局变量方便读取
//mbtk_at_fs_par.all_dir_file = count;
//mbtk_at_fs_par.dir_num = dir_count;
mbtk_at_fs_par.file_num = file_count;
closedir(dir);
return ret;
}
static int fs_cmd_process_list_all(char *path,mbtk_fs_dir_file_info_s *fs_dir_and_file_list)
{
int ret = 0, count = 0, dir_count = 0, file_count = 0, len_name;
const char *temp_fs_path = NULL;
struct dirent *files;
struct stat FileStat_t;
DIR *dir;
mbtk_fs_dir_file_info_s fs_dir_file_t, fs_dir_t, fs_file_t;
char Filepath[MBTK_FS_MAX + 1];
memset(fs_dir_and_file_list, 0x0, sizeof(mbtk_fs_dir_file_info_s)-sizeof(void *));
temp_fs_path = path;
chdir(temp_fs_path);
dir = opendir(temp_fs_path);
if (dir == NULL)
{
ret = -1;
LOGE("Directory cannot be opened!\n");
return ret;
}
while ((files = readdir(dir)) != NULL)
{
if(strcmp(files->d_name, ".") == 0 || strcmp(files->d_name, "..") == 0)
{
continue;
}//跳过隐藏文件
memset(&FileStat_t, 0x00, sizeof(FileStat_t));
memset(&fs_dir_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s));
memset(&fs_file_t, 0x0, sizeof(mbtk_fs_dir_file_info_s));
memset(&fs_dir_t, 0x0, sizeof(mbtk_fs_dir_file_info_s));
len_name = strlen(files->d_name);
//目录或者文件夹名字长度不能超过256,可以的话这里也可以做出错处理;
stat(files->d_name,&FileStat_t);
if(S_ISDIR(FileStat_t.st_mode)) //递归调用解析其子目录下的文件夹
{
memcpy(fs_dir_t.name, files->d_name, (len_name)+1);
//LOGE("DIR>>>>>>>>>>>>>> [%s]\n", fs_dir_t.name);
dir_count++;
}
else
{
memcpy(fs_file_t.name, files->d_name, (len_name)+1);
//LOGE("FILE>>>>>>>>>>>>>> [%s]\n", fs_file_t.name);
file_count++;
}
memcpy(fs_dir_file_t.name, files->d_name, (len_name)+1);
//LOGE("ALL>>>>>>>>>>>>>> [%s]\n", fs_dir_file_t.name);
count++;
//每次对一个文件或者目录处理,就加入里面
(fs_dir_and_file_list->fs_ls_cb_typedef)(&fs_dir_file_t);
}
//每次显示完成后,把读取的数量传给全局变量方便读取
mbtk_at_fs_par.all_dir_file = count;
mbtk_at_fs_par.dir_num = dir_count;
mbtk_at_fs_par.file_num = file_count;
closedir(dir);
return ret;
}
static int fs_cmd_process_cd(char *path)
{
int ret = 0;
char buf[1024];
char Filepath[MBTK_FS_MAX + 1];
char Pathtmp[MBTK_FS_MAX + 1];
memset(buf, 0x00, sizeof(buf));
memset(Filepath , 0x00, sizeof(Filepath));
memset(Pathtmp , 0x00, sizeof(Pathtmp));
memcpy(Filepath, path, sizeof(Filepath));
if ((ret = path_redirection(Filepath, Pathtmp)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
return ret;
}
ret = chdir(Pathtmp);
// ret = chdir(path);
if (ret == -1)
{
LOGE("cd path error:[%d]\n", errno);
}
char *ptr = getcwd(buf, sizeof(buf));
if (ptr == NULL)
LOGE("cd path error:[%d]\n", errno);
return ret;
}
static int fs_cmd_process_rename(char *old_path, char *new_path)
{
int ret = 0;
char tmp_old[MBTK_FS_MAX + 1];
char tmp_new[MBTK_FS_MAX + 1];
memset(tmp_old , 0x00, sizeof(tmp_old));
memset(tmp_new , 0x00, sizeof(tmp_new));
if ((ret = path_redirection(old_path, tmp_old)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
return ret;
}
if ((ret = path_redirection(new_path, tmp_new)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
return ret;
}
ret = rename(tmp_old, tmp_new);
// ret = rename(old_path, new_path);
if (ret == -1)
{
LOGE("rename error:[%d]\n", errno);
}
return ret;
}
static int fs_cmd_process_copy(char *old_path, char *new_path, int mode)
{
int ret = 0, num = 0;
int old_fd,new_fd;
char *tbuf = NULL;
int tmode = mode;//没有做处理,默认以同步方式
char tmp_old[MBTK_FS_MAX + 1];
char tmp_new[MBTK_FS_MAX + 1];
memset(tmp_old , 0x00, sizeof(tmp_old));
memset(tmp_new , 0x00, sizeof(tmp_new));
if ((ret = path_redirection(old_path, tmp_old)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
return ret;
}
if ((ret = path_redirection(new_path, tmp_new)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
return ret;
}
if (access(tmp_old, F_OK) != 0) //旧文件必须存在
{
ret = -1;
LOGE("old_file access errn = [%d]\n",errno);
goto exit;
}
if (access(tmp_new, F_OK) == 0) //新文件存在,则错误
{
ret = -1;
LOGE("new_file access errn = [%d]\n",errno);
goto exit;
}
if ( (old_fd=file_open(tmp_old, O_RDWR)) == -1 )
{
LOGE("open old_path error:[%d]\n", errno);
ret = -1;
goto exit;
}
if ( (new_fd=file_open(tmp_new, O_RDWR | O_CREAT)) == -1 )
{
LOGE("open new_path error:[%d]\n", errno);
ret = -1;
goto exit;
}
int len = lseek(old_fd,0,SEEK_END);//获取长度
if (len == -1)//获取文件大小时出错
{
LOGE("lseek error:[%d]\n", errno);
ret = -1;
goto exit;
}
lseek(old_fd,0,SEEK_SET);//还原指针指到开头处
tbuf = malloc(len + 1);
if (len+1 > BUFFERSIZE)
LOGE("Out of length\n");
num = file_read(old_fd, tbuf, len);
if (file_write(new_fd, tbuf, len) != num)
{
LOGE("The write does not match the read out\n");
ret = -1;
goto exit;
}
fsync(old_fd);
fsync(new_fd);
exit:
file_close(old_fd);
file_close(new_fd);
free(tbuf);
return ret;
}
static int fs_cmd_process_memory(uint64 *total, uint64 *available)
{
int ret = 0;
struct statfs buf;
uint64 blocksize, totalsize, availablesize, freesize;
ret = statfs(PART_FS_OERLAY, &buf);
if (ret == -1)
{
LOGE("statfs error:[%d]\n", errno);
}
else
{
blocksize = buf.f_bsize; // 每个block里包含的字节数
totalsize = blocksize * buf.f_blocks; // 总的字节数,f_blocks为block的数目
//freesize = buf.f_bfree * blocksize; // 剩余空间的大小
availablesize = buf.f_bavail * blocksize; // 可用空间大小
*total = totalsize;
*available = availablesize;
}
return ret;
}
static int fs_cmd_process_delfile(char *path)
{
int ret = 0;
char tmp[MBTK_FS_MAX + 1];
memset(tmp , 0x00, sizeof(tmp));
if ((ret = path_redirection(path, tmp)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
return ret;
}
ret = remove(tmp);
// ret = remove(path);
if (ret == -1)
{
LOGE("rm file error:[%d]\n", errno);
}
return ret;
}
static int fs_cmd_process_creatfile(char *path)
{
int ret = 0 ,fd;
char tmp[MBTK_FS_MAX + 1];
memset(tmp , 0x00, sizeof(tmp));
if ((ret = path_redirection(path, tmp)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
return ret;
}
fd = file_open(tmp, O_RDWR | O_CREAT | O_TRUNC);
if (fd == -1)
{
ret = -1;
LOGE("creat file error:[%d]\n", errno);
}
file_close(fd);
return ret;
}
int mbtk_at_fs_dir(int cmd, char *fs_path)
{
int at_fs_ret = 0;
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
switch (cmd)
{
case 0:
{
at_fs_ret = fs_cmd_process_mkd(fs_path);
break;
}
case 1:
{
at_fs_ret = fs_cmd_process_rmkd(fs_path);
break;
}
default:
{
LOGE("ERROR cmd \n");
at_fs_ret = -1;
break;
}
}
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_list_num_get(int32* dir_num, int32* file_num)
{
int at_fs_ret = 0;
mbtk_fs_dir_file_info_s fs_dir_file;
char tmp_path[FS_NAME_LEN] = {0};
char *fs_pwd_path = NULL;
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
fs_pwd_path = GET_PWD();//获取当前路径
strcpy(tmp_path, fs_pwd_path);
if (at_fs_ret == 0)
{
fs_dir_file.fs_ls_cb_typedef = fs_ls_at_cb_func_num;
at_fs_ret = fs_cmd_process_list_all(tmp_path, &fs_dir_file);
*file_num = mbtk_at_fs_par.file_num;
*dir_num = mbtk_at_fs_par.dir_num;
}
else
{
at_fs_ret = -1;
LOGE("OPEN this dir errno [%s]\n", tmp_path);
goto exit;
}
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_list_dir_file_get()
{
int at_fs_ret = 0;
char send_buf[SENDBUFF] = {0};
char *fs_pwd_path = NULL;
mbtk_fs_dir_file_info_s fs_dir, fs_file;
char tmp_path[FS_NAME_LEN] = {0};
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
fs_pwd_path = GET_PWD();//获取当前路径
strcpy(tmp_path, fs_pwd_path);
if ((at_fs_ret = pwd_def_dir(tmp_path)) == -1)
goto exit;
memset(send_buf, 0x00, sizeof(send_buf));
mbtk_at_fs_printf("+MFSLS: SUBDIRECTORIES:");
fs_dir.fs_ls_cb_typedef = fs_ls_at_cb_func;
at_fs_ret = fs_cmd_process_list_dir(tmp_path,&fs_dir);
mbtk_at_fs_printf("+MFSLS: FILES:");
fs_file.fs_ls_cb_typedef = fs_ls_at_cb_func;
at_fs_ret = fs_cmd_process_list_file(tmp_path,&fs_file);
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
memset(send_buf, 0x00, sizeof(send_buf));
return at_fs_ret;
}
int mbtk_at_fs_list_set(int type)
{
int at_fs_ret = 0;
char send_buf[SENDBUFF] = {0};
char *fs_pwd_path = NULL;
mbtk_fs_dir_file_info_s fs_dir_file, fs_dir, fs_file;
char tmp_path[FS_NAME_LEN] = {0};
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
fs_pwd_path = GET_PWD();//获取当前路径
strcpy(tmp_path, fs_pwd_path);
if ((at_fs_ret = pwd_def_dir(tmp_path)) == -1)
goto exit;
if (tmp_path != NULL)
{
LOGE("OPEN this dir [%s]\n", tmp_path);
}
else
{
at_fs_ret = -1;
LOGE("OPEN this dir errno [%s]\n", tmp_path);
goto exit;
}
memset(send_buf, 0x00, sizeof(send_buf));
mbtk_at_fs_par.at_fs_type = type;
switch (mbtk_at_fs_par.at_fs_type)
{
case 0://显示目录和文件名称
{
mbtk_at_fs_printf("+MFSLS:\n");
fs_dir_file.fs_ls_cb_typedef = fs_ls_at_cb_func;
at_fs_ret = fs_cmd_process_list_all(tmp_path, &fs_dir_file);
if(at_fs_ret != 0)
{
printf("+MFSLS: FAIL,<%d>\n",at_fs_ret);
sprintf(send_buf,"+MFSLS: FAIL,<%d>\n",at_fs_ret);
}
else
{
printf("+MFSLS:SUBDIRECTORIES: %d FILES: %d \r\n", mbtk_at_fs_par.dir_num, mbtk_at_fs_par.file_num);
sprintf(send_buf,"+MFSLS: SUBDIRECTORIES: %d FILES: %d \n", mbtk_at_fs_par.dir_num, mbtk_at_fs_par.file_num);
}
break;
}
case 1://显示目录名称
{
mbtk_at_fs_printf("+MFSLS:");
fs_dir.fs_ls_cb_typedef = fs_ls_at_cb_func;
at_fs_ret = fs_cmd_process_list_dir(tmp_path,&fs_dir);
if(at_fs_ret != 0)
{
printf("+MFSLS: FAIL,<%d>\n",at_fs_ret);
sprintf(send_buf,"+MFSRMDIR: FAIL,<%d>\n",at_fs_ret);
}
else
{
printf("+MFSLS:SUBDIRECTORIES: %d \r\n", mbtk_at_fs_par.dir_num);
sprintf(send_buf,"+MFSLS: SUBDIRECTORIES: %d \n", mbtk_at_fs_par.dir_num);
}
break;
}
case 2://显示文件名称
{
mbtk_at_fs_printf("+MFSLS:");
fs_file.fs_ls_cb_typedef = fs_ls_at_cb_func;
at_fs_ret = fs_cmd_process_list_file(tmp_path,&fs_file);
if(at_fs_ret != 0)
{
printf("+MFSLS: FAIL,<%d>\n",at_fs_ret);
sprintf(send_buf,"+MFSRMDIR: FAIL,<%d>\n",at_fs_ret);
}
else
{
printf("+MFSLS:FILES: %d \r\n",mbtk_at_fs_par.file_num);
sprintf(send_buf,"+MFSLS: FILES: %d \n",mbtk_at_fs_par.file_num);
}
break;
}
default:
{
LOGE("ERROR mbtk_at_fs_par.at_fs_type \n");
at_fs_ret = -1;
break;
}
}
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
memset(send_buf, 0x00, sizeof(send_buf));
return at_fs_ret;
}
int mbtk_at_fs_cd(char *fs_dir)
{
int at_fs_ret = 0;
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
at_fs_ret = fs_cmd_process_cd(fs_dir);
if(at_fs_ret == 0)
{
sprintf(send_buf,"+MFSCD: %s\r\n",fs_dir);
}
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_delfile(char *fs_dir_file)
{
int at_fs_ret = 0;
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
at_fs_ret = fs_cmd_process_delfile(fs_dir_file);
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_attri(char *fs_file)
{
int at_fs_ret = 0, len_size;
char Filepath[MBTK_FS_MAX + 1]={0};
char Pathtmp[MBTK_FS_MAX + 1]={0};
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
memcpy(Filepath, fs_file, sizeof(Filepath));
if ((at_fs_ret = path_redirection(Filepath, Pathtmp)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
goto exit;
}
if (0 != (access(Pathtmp, F_OK)))
{
LOGE("access file is null\n");
at_fs_ret = -1;
goto exit;
}
len_size = read_file_len(Pathtmp);
if(at_fs_ret != 0)
{
printf("+MFSATTRI : FAIL,<%d>\n",at_fs_ret);
sprintf(send_buf,"+MFSATTRI : FAIL,<%d>\n",at_fs_ret);
goto exit;
}
else
{
printf("+MFSATTRI: <%d>\n",len_size);
sprintf(send_buf,"+MFSATTRI: %d\n",len_size);
goto exit;
}
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_writefile(mbtk_fs_data_info_t *fs_w_data)
{
int at_fs_ret = 0, len_size, fd, write_mode, write_size;
char get_buf[FS_DATA_LEN];
char Filepath[MBTK_FS_MAX + 1]={0};
char Pathtmp[MBTK_FS_MAX + 1]={0};
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
memset(get_buf, 0x00, sizeof(get_buf));
write_size = fs_w_data->fs_data_size;
memcpy(get_buf, fs_w_data->fs_data, write_size);
memcpy(Filepath, fs_w_data->fs_data_name, strlen(fs_w_data->fs_data_name));
write_mode = fs_w_data->fs_data_mode;
if ((at_fs_ret = path_redirection(Filepath, Pathtmp)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
goto exit;
}
if (0 != (access(Pathtmp, F_OK)))
{
LOGE("access file is null\n");
at_fs_ret = -1;
goto exit;
}
fd=file_open(Pathtmp,O_WRONLY);
if(-1 == fd)
{
LOGE("Write file error\n");
at_fs_ret = -1;
file_close(fd);
goto exit;
}
else
{
LOGE("Open file SUCCESS\r\n");
if (write_mode == 0)
{ //将文件读写指针移动到文件头
lseek(fd,0,SEEK_SET);
}
else if (write_mode == 1)
{ //将文件读写指针移动到文件尾
lseek(fd,0,SEEK_END);
}
else
{
LOGE("write_mode error\n");
at_fs_ret = -1;
file_close(fd);
goto exit;
}
//这两步可以加出错处理
file_write(fd, get_buf, write_size);
fsync(fd);
file_close(fd);
}
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
//int mbtk_at_fs_readfile(void *dir_file, int read_offset, int read_size, char* read_buff)
int mbtk_at_fs_readfile(void *dir_file, int read_offset, int read_size)
{
int at_fs_ret = 0, fd, count = 0,file_size, len_count = 0, len = 0;
char read_buf[1024];
char Filepath[MBTK_FS_MAX + 1]={0};
char Pathtmp[MBTK_FS_MAX + 1]={0};
char send_buf[1024] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
memset(read_buf, 0x00, sizeof(read_buf));
memcpy(Filepath, dir_file, sizeof(Filepath));
if ((at_fs_ret = path_redirection(Filepath, Pathtmp)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
goto exit;
}
if (0 != (access(Pathtmp, F_OK)))
{
LOGE("access file is null\n");
at_fs_ret = -1;
goto exit;
}
file_size = read_file_len(Pathtmp);
fd = file_open(Pathtmp, O_RDONLY);
if (fd == -1)
{
LOGE("Read file error\n");
at_fs_ret = -1;
goto exit;
}
else
{
LOGE("Open file SUCCESS\r\n");
if (read_offset >= file_size)
{
LOGE("read_offset > file_size \n");
at_fs_ret = -1;
goto exit;
}
lseek(fd,read_offset,SEEK_SET);
//由于at客户端缓存才1k,切片512字节大小传输
if (read_size > 0)
{
if (0 < read_size && read_size <= 512) //单次发生:文件大小-偏移量 < 发送大小
{
len = read(fd, read_buf, read_size); //返回实际读取的大小
LOGE("len = [%d]\n",len);
if (len > 0)
{
//LOGE("read_buff = [%s]\n",read_buf);
sprintf(send_buf,"+MFSREAD: DATA: %d, %s\n",len, read_buf);
}
}
else if(read_size <= 1024 && read_size > 512) //多次发送:
{
len = read(fd, read_buf, read_size);
count++;
LOGE("count = [%d]\n",count);
mbtk_at_fs_data_cb(read_buf, len);
sprintf(send_buf, "+MFSREAD: DATA: [%d] count: [%d]\r\n",len,count);
mbtk_at_fs_printf(send_buf);
memset(send_buf, 0x00, sizeof(send_buf));
}
else
{
LOGE("read_size errno\n");
}
}
else if (read_size == 0)//全部读取完成
{
if ((file_size-read_offset) < 512)
{
len = read(fd, read_buf, 1024); //返回实际读取的大小
LOGE("len = [%d]\n",len);
if (len > 0)
{
//LOGE("read_buff = [%s]\n",read_buf);
sprintf(send_buf,"+MFSREAD: DATA: %d, %s\n",len, read_buf);
}
}
else
{
while((file_size-read_offset) > len_count)
{
len = read(fd, read_buf, 1024);
count++;
LOGE("count = [%d]\n",count);
mbtk_at_fs_data_cb(read_buf, len);
len_count += len;
sprintf(send_buf, "+MFSREAD: DATA: [%d] count: [%d]\r\n",len_count, count);
mbtk_at_fs_printf(send_buf);
memset(send_buf, 0x00, sizeof(send_buf));
}
}
}
else
{
LOGE("read_size errno =[%d]\n",errno);
}
file_close(fd);
}
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_rename(char *old_file, char *new_file)
{
int at_fs_ret = 0;
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
if (old_file == NULL || new_file == NULL)
{
at_fs_ret = -1;
LOGE("old_file or new_file is null\n");
goto exit;
}
at_fs_ret = fs_cmd_process_rename(old_file, new_file);
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_creatfile(char *fs_file)
{
int at_fs_ret = 0;
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
at_fs_ret = fs_cmd_process_creatfile(fs_file);
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_memory(uint64 *total_len,uint64 *available_len)
{
int at_fs_ret = 0;
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
at_fs_ret = fs_cmd_process_memory(total_len, available_len);
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_copy(char *old_file, char *new_file, int mode)
{
int at_fs_ret = 0;
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
if (old_file == NULL || new_file == NULL)
{
at_fs_ret = -1;
LOGE("old_file or new_file is null\n");
goto exit;
}
at_fs_ret = fs_cmd_process_copy(old_file, new_file, mode);
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_certload(mbtk_fs_data_certmode_t *certmode)
{
int at_fs_ret = 0, fd, cert_mode, write_size;
char tmp_buf[FS_NAME_LEN];
char get_buf[FS_DATA_LEN];
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
memset(get_buf, 0x00, sizeof(get_buf));
memset(tmp_buf, 0x00, sizeof(tmp_buf));
write_size = certmode->fs_data_size;
memcpy(get_buf, certmode->fs_data, write_size);
cert_mode = certmode->fs_data_certmode;
switch(cert_mode)
{
case 0:
{
memcpy(tmp_buf, FS_CERT_0, sizeof(tmp_buf));
break;
}
case 1:
{
memcpy(tmp_buf, FS_CERT_1, sizeof(tmp_buf));
break;
}
case 2:
{
memcpy(tmp_buf, FS_CERT_2, sizeof(tmp_buf));
break;
}
case 3:
{
memcpy(tmp_buf, FS_CERT_3, sizeof(tmp_buf));
break;
}
case 4:
{
memcpy(tmp_buf, FS_CERT_4, sizeof(tmp_buf));
break;
}
default:
{
LOGE("ERROR cert_mode \n");
at_fs_ret = -1;
break;
}
}
if (at_fs_ret == -1)
goto exit;
if (fd = file_open(tmp_buf, O_RDWR | O_CREAT | O_TRUNC) == -1)
{
LOGE("Open file is err [%d]\n",errno);
at_fs_ret = -1;
goto exit;
}
else
{
//通过at客户端往里面写数据
LOGE("Open file SUCCESS\r\n");
if (lseek(fd,0,SEEK_SET) == -1 )
{
LOGE("lseek error = [%d]\n",errno);
at_fs_ret = -1;
file_close(fd);
goto exit;
}
//这两步可以加出错处理
file_write(fd, get_buf, write_size);
fsync(fd);
file_close(fd);
}
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}
int mbtk_at_fs_fileload(mbtk_fs_data_file_t *file)
{
int at_fs_ret = 0, fd, write_size;
char get_buf[FS_DATA_LEN];
char Filepath[MBTK_FS_MAX + 1]={0};
char Pathtmp[MBTK_FS_MAX + 1]={0};
char send_buf[SENDBUFF] = {0};
memset(send_buf, 0x00, sizeof(send_buf));
if ((at_fs_ret = creat_filesystem_def_dir()) == -1)
{
LOGE("creat_filesystem_def_dir error:[%d]\n", errno);
return at_fs_ret;
}
if ((at_fs_ret = path_pass()) == -1)
goto exit;
memset(get_buf, 0x00, sizeof(get_buf));
if (file == NULL)
{
at_fs_ret = -1;
LOGE("file is null\n");
goto exit;
}
write_size = file->fs_data_size;
memcpy(get_buf, file->fs_data, write_size);
memcpy(Filepath, file->fs_data_name, strlen(file->fs_data_name));
if ((at_fs_ret = path_redirection(Filepath, Pathtmp)) == -1)
{
LOGE("input path_redirection error:[%d]\n", errno);
goto exit;
}
if (fd = file_open(Pathtmp, O_RDWR | O_CREAT | O_TRUNC) == -1)
{
LOGE("open file is err [%d]\n",errno);
}
else
{
//通过at客户端往里面写数据
LOGE("Open file SUCCESS\r\n");
if (lseek(fd,0,SEEK_SET) == -1 )
{
LOGE("lseek error = [%d]\n",errno);
at_fs_ret = -1;
file_close(fd);
goto exit;
}
//这两步可以加出错处理
file_write(fd, get_buf, write_size);
fsync(fd);
file_close(fd);
}
exit:
mbtk_at_fs_printf(send_buf);
mbtk_at_fs_printf("end");
return at_fs_ret;
}