1043 lines
31 KiB
C
Raw Normal View History

2025-02-18 15:40:42 +08:00
#ifndef __FS_H__
#define __FS_H__
#include "generic/typedef.h"
#include "generic/list.h"
#include "generic/ioctl.h"
#include "generic/atomic.h"
#include "system/task.h"
#include "system/malloc.h"
#include "system/sys_time.h"
#include "stdarg.h"
#include "fs_file_name.h"
#define SEEK_SET 0 /* Seek from beginning of file. */
#define SEEK_CUR 1 /* Seek from current position. */
#define SEEK_END 2 /* Seek from end of file. */
#include "sdfile.h"
#define F_ATTR_RO 0x01
#define F_ATTR_ARC 0x02
#define F_ATTR_DIR 0x04
#define F_ATTR_VOL 0x08
#if (VFS_ENABLE == 1)
#ifndef FSELECT_MODE
#define FSELECT_MODE
#define FSEL_FIRST_FILE 0
#define FSEL_LAST_FILE 1
#define FSEL_NEXT_FILE 2
#define FSEL_PREV_FILE 3
#define FSEL_CURR_FILE 4
#define FSEL_BY_NUMBER 5
#define FSEL_BY_SCLUST 6
#define FSEL_AUTO_FILE 7
#define FSEL_NEXT_FOLDER_FILE 8
#define FSEL_PREV_FOLDER_FILE 9
#define FSEL_BY_PATH 10
#endif
#ifndef FCYCLE_MODE
#define FCYCLE_MODE
#define FCYCLE_LIST 0
#define FCYCLE_ALL 1
#define FCYCLE_ONE 2
#define FCYCLE_FOLDER 3
#define FCYCLE_RANDOM 4
#define FCYCLE_MAX 5
#endif
enum {
FS_IOCTL_GET_FILE_NUM,
FS_IOCTL_FILE_CHECK,
FS_IOCTL_GET_ERR_CODE, //暂不支持
FS_IOCTL_FREE_CACHE,
FS_IOCTL_SET_NAME_FILTER, //设置文件过滤
FS_IOCTL_GET_FOLDER_INFO, //获取文件夹序号和文件夹内文件数目
FS_IOCTL_SET_LFN_BUF, // 512
FS_IOCTL_SET_LDN_BUF, // 512
FS_IOCTL_SET_EXT_TYPE, //设置后缀类型
FS_IOCTL_OPEN_DIR, //打开目录
FS_IOCTL_ENTER_DIR, //进入目录
FS_IOCTL_EXIT_DIR, //退出
FS_IOCTL_GET_DIR_INFO, //获取目录信息
FS_IOCTL_GETFILE_BYNAME_INDIR, //由歌曲名称获得歌词
FS_IOCTL_GET_DISP_INFO, //用于长文件名获取
FS_IOCTL_MK_DIR, //创建文件夹
FS_IOCTL_GET_ENCFOLDER_INFO, //获取录音文件信息
FS_IOCTL_GET_OUTFLASH_ADDR, //获取外置flash实际物理地址暂时用于手表case,特殊fat系统
FS_IOCTL_FLUSH_WBUF, //刷新wbuf
FS_IOCTL_SAVE_FAT_TABLE, //seek加速处理
FS_IOCTL_INSERT_FILE, //插入文件
FS_IOCTL_DIVISION_FILE, //分割文件
FS_IOCTL_STORE_CLUST_RANG, //存储CLUST_RANG 信息
};
struct vfs_devinfo;
struct vfscan;
struct vfs_operations;
struct vfs_devinfo {
void *fd;
u32 sector_size;
void *private_data;
};
#define VFS_PART_DIR_MAX 16
struct vfs_partition {
struct vfs_partition *next;
u32 offset;
u32 clust_size;
u32 total_size;
u8 fs_attr;
u8 fs_type;
char dir[VFS_PART_DIR_MAX];
void *private_data;
};
struct fiter {
u32 index;
};
struct ffolder {
u16 fileStart;
u16 fileTotal;
};
struct imount {
int fd;
const char *path;
struct vfs_operations *ops;
struct vfs_devinfo dev;
struct vfs_partition part;
struct list_head entry;
atomic_t ref;
OS_MUTEX mutex;
u8 avaliable;
u8 part_num;
};
struct vfs_attr {
u8 attr;
u32 fsize;
u32 sclust;
struct sys_time crt_time;
struct sys_time wrt_time;
struct sys_time acc_time;
};
typedef struct {
struct imount *mt;
struct vfs_devinfo *dev;
struct vfs_partition *part;
void *private_data;
} FILE;
struct vfscan {
u8 scan_file;
u8 subpath; //子目录,设置是否只扫描一层
u8 scan_dir;
u8 attr;
u8 cycle_mode;
char sort;
char ftype[20 * 3 + 1];
u16 file_number;
u16 file_counter;
u16 dir_totalnumber; // 文件夹总数
u16 musicdir_counter; // 播放文件所在文件夹序号
u16 fileTotalInDir; //文件夹下的文件数目
void *priv;
struct vfs_devinfo *dev;
struct vfs_partition *part;
char filt_dir[12];
};
struct vfs_operations {
const char *fs_type;
int (*mount)(struct imount *, int);
int (*unmount)(struct imount *);
int (*format)(struct vfs_devinfo *, struct vfs_partition *);
int (*fset_vol)(struct vfs_partition *, const char *name);
int (*fget_free_space)(struct vfs_devinfo *, struct vfs_partition *, u32 *space);
int (*fopen)(FILE *, const char *path, const char *mode);
int (*fread)(FILE *, void *buf, u32 len);
int (*fread_fast)(FILE *, void *buf, u32 len);
int (*fwrite)(FILE *, void *buf, u32 len);
int (*fseek)(FILE *, int offset, int);
int (*fseek_fast)(FILE *, int offset, int);
int (*flen)(FILE *);
int (*fpos)(FILE *);
int (*fcopy)(FILE *, FILE *);
int (*fget_name)(FILE *, u8 *name, int len);
int (*fget_path)(FILE *, struct vfscan *, u8 *name, int len, u8 is_relative_path);
int (*frename)(FILE *, const char *path);
int (*fclose)(FILE *);
int (*fdelete)(FILE *);
int (*fscan)(struct vfscan *, const char *path, u8 max_deepth);
int (*fscan_interrupt)(struct vfscan *, const char *path, u8 max_deepth, int (*callback)(void));
void (*fscan_release)(struct vfscan *);
int (*fsel)(struct vfscan *, int sel_mode, FILE *, int);
int (*fget_attr)(FILE *, int *attr);
int (*fset_attr)(FILE *, int attr);
int (*fget_attrs)(FILE *, struct vfs_attr *);
int (*fmove)(FILE *file, const char *path_dst, FILE *, int clr_attr, int path_len);
int (*ioctl)(void *, int cmd, int arg);
};
#define REGISTER_VFS_OPERATIONS(ops) \
const struct vfs_operations ops SEC(.vfs_operations)
static inline struct vfs_partition *vfs_partition_next(struct vfs_partition *p)
{
struct vfs_partition *n = (struct vfs_partition *)zalloc(sizeof(*n));
if (n) {
p->next = n;
}
return n;
}
static inline void vfs_partition_free(struct vfs_partition *p)
{
struct vfs_partition *n = p->next;
while (n) {
p = n->next;
free(n);
n = p;
}
}
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param dev_name
* @param path
* @param fs_type
* @param cache_num
* @param dev_arg
*
* @return mt结构句柄
*/
/* ----------------------------------------------------------------------------*/
struct imount *mount(const char *dev_name, const char *path, const char *fs_type,
int cache_num, void *dev_arg); //挂载
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param path
*
* @return 0
*/
/* ----------------------------------------------------------------------------*/
int unmount(const char *path);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param path
* @param fs_type
* @param clust_size ,0
*
* @return 00
*/
/* ----------------------------------------------------------------------------*/
int f_format(const char *path, const char *fs_type, u32 clust_size); //格式化接口
int f_free_cache(const char *path);
#if 0
/*----------------------------------------------------------------------------*/
/** @brief:
@param: fopen
@note: fopen自动打开
1. +:"music/test/1/2/3/pk*.wav" "JL_REC/AC69****.wav" "JL_REC/AC690000.wav"
2. **8+3XXX0001,XXXX002这样得格式*
@date: 2020-07-22
*/
/*----------------------------------------------------------------------------*/
#endif
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param path
* @param mode ("r")("w")("w+")
*
* @return
*/
/* ----------------------------------------------------------------------------*/
FILE *fopen(const char *path, const char *mode);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param buf buf
* @param len buf长度
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fread(FILE *file, void *buf, u32 len);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param buf buf
* @param len buf长度
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fwrite(FILE *file, void *buf, u32 len);
/* --------------------------------------------------------------------------*/
/**
* @brief Seek到相应得位置
*
* @param file
* @param offset
* @param orig
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fseek(FILE *file, int offset, int orig);
/* --------------------------------------------------------------------------*/
/**
* @brief seek
*
* @param file
* @param offset
* @param orig
*
* @note case使用,,ram里面跑
* @return
*/
/* ----------------------------------------------------------------------------*/
int fseek_fast(FILE *file, int offset, int orig);// 快速seek
/* --------------------------------------------------------------------------*/
/**
* @brief read
*
* @param file
* @param buf buf
* @param len buf长度
*
* @note case使用,,ram里面跑
* @return
*/
/* ----------------------------------------------------------------------------*/
int fread_fast(FILE *file, void *buf, u32 len); //快速read
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int flen(FILE *file);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fpos(FILE *file);
int fcopy(const char *format, ...);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param name buf
* @param len
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fget_name(FILE *file, u8 *name, int len);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param path
*
* @return 00
*/
/* ----------------------------------------------------------------------------*/
int frename(FILE *file, const char *path);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fclose(FILE *file);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fdelete(FILE *file);
int fdelete_by_name(const char *fname);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param path
* @param space K
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fget_free_space(const char *path, u32 *space);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param fscan
* @param name buf
* @param len buf长度
* @param is_relative_path
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fget_path(FILE *file, struct vfscan *fscan, u8 *name, int len, u8 is_relative_path);
/* arg:
* -t
* -r
* -d
* -a r: /:
* -s t: n:
*/
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param path
* @param arg
* @param max_deepth 9
*
* @return vfscan句柄
*/
/* ----------------------------------------------------------------------------*/
struct vfscan *fscan(const char *path, const char *arg, u8 max_deepth); //扫描接口,参数配置如上。
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param path
* @param arg
* @param max_deepth 9
* @param callback
*
* @return vfscan句柄
*/
/* ----------------------------------------------------------------------------*/
struct vfscan *fscan_interrupt(const char *path, const char *arg, u8 max_deepth, int (*callback)(void)); //可打断扫描
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param fs vfscan句柄
* @param path
*
* @return vfscan句柄
*/
/* ----------------------------------------------------------------------------*/
struct vfscan *fscan_enterdir(struct vfscan *fs, const char *path);//进入指定子目录,只扫此目录下文件信息
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param fs vfscan句柄
*
* @return vfscan句柄
*/
/* ----------------------------------------------------------------------------*/
struct vfscan *fscan_exitdir(struct vfscan *fs); //返回上一层
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param fs vfscan句柄
*/
/* ----------------------------------------------------------------------------*/
void fscan_release(struct vfscan *fs);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param fs vfscan句柄
* @param selt_mode ()
* @param arg
*
* @return
*/
/* ----------------------------------------------------------------------------*/
FILE *fselect(struct vfscan *fs, int selt_mode, int arg); //选择指定文件
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param dir
*
* @return 0 1
*/
/* ----------------------------------------------------------------------------*/
int fdir_exist(const char *dir); //check 目录是否存在
int fdir(FILE *file, const char *arg, char *name, int len, struct fiter *iter);//暂无此接口
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param attr
*
* @return 0 0
*/
/* ----------------------------------------------------------------------------*/
int fget_attr(FILE *file, int *attr); //获取文件属性
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param attr
*
* @return 0 0
*/
/* ----------------------------------------------------------------------------*/
int fset_attr(FILE *file, int attr); //设置文件属性
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param attr
*
* @return 0 0
*/
/* ----------------------------------------------------------------------------*/
int fget_attrs(FILE *file, struct vfs_attr *attr);//获得文件相关信息如属性、簇号、大小等
/* --------------------------------------------------------------------------*/
/**
* @brief part
*
* @param path
*
* @return vfs_partition句柄
*/
/* ----------------------------------------------------------------------------*/
struct vfs_partition *fget_partition(const char *path);//获得分区part
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param path
* @param name
*
* @return 0 0
*/
/* ----------------------------------------------------------------------------*/
int fset_vol(const char *path, const char *name);//设置卷标
int fmove(FILE *file, const char *path_dst, FILE **newFile, int clr_attr, int path_len);
int fcheck(FILE *file);//暂不支持
int fget_err_code(const char *path); //暂不支持
int fset_name_filter(const char *path, void *name_filter);//暂不支持
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param fs vfscan
* @param arg
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fget_folder(struct vfscan *fs, struct ffolder *arg); //获取文件夹序号和文件夹内文件数目
/* --------------------------------------------------------------------------*/
/**
* @brief Buf ()
*
* @param fs vfscan
* @param arg buf
*
* @return 0 0
*/
/* ----------------------------------------------------------------------------*/
int fset_lfn_buf(struct vfscan *fs, void *arg);//设置长文件名buf
/* --------------------------------------------------------------------------*/
/**
* @brief Buf ()
*
* @param fs vfscan
* @param arg buf
*
* @return 0 0
*/
/* ----------------------------------------------------------------------------*/
int fset_ldn_buf(struct vfscan *fs, void *arg);//设置长文件夹名buf
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param path
* @param ext_type
*
* @return 0 0
*/
/* ----------------------------------------------------------------------------*/
int fset_ext_type(const char *path, void *ext_type);//设置后缀类型
/* --------------------------------------------------------------------------*/
/**
* @brief 使
*
* @param path
* @param pp_file
* @param dir_dj
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fopen_dir_info(const char *path, FILE **pp_file, void *dir_dj); //打开目录
/* --------------------------------------------------------------------------*/
/**
* @brief 使
*
* @param file
* @param dir_dj
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fenter_dir_info(FILE *file, void *dir_dj); //进入目录
/* --------------------------------------------------------------------------*/
/**
* @brief 使退
*
* @param file
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fexit_dir_info(FILE *file); //退出
/* --------------------------------------------------------------------------*/
/**
* @brief 使
*
* @param file
* @param start_num
* @param total_num
* @param buf_info
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int fget_dir_info(FILE *file, u32 start_num, u32 total_num, void *buf_info); ////获取目录信息
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
*
* @note case使用, fget_fat_outflash_addr()fget_fat_outflash_addr()
* @return 0 0
*/
/* ----------------------------------------------------------------------------*/
int fstore_clust_rang(FILE *file);
/* --------------------------------------------------------------------------*/
/**
* @brief flash实际物理地址
*
* @param file
* @param name sdfile格式文件名
* @param buf_info buf指针
* @param buf_len buf
*
* @note case使用
* @return 0buf不够 0
*/
/* ----------------------------------------------------------------------------*/
int fget_fat_outflash_addr(FILE *file, char *name, void *buf_info, int buf_len); //获取外置flash实际物理地址
/* --------------------------------------------------------------------------*/
/**
* @brief 使
*
* @param file
* @param newFile
* @param ext_name
*
* @return 0 0
*/
/* ----------------------------------------------------------------------------*/
int fget_file_byname_indir(FILE *file, FILE **newFile, void *ext_name); //由歌曲名称获得歌词
/* --------------------------------------------------------------------------*/
/**
* @brief 使
*
* @param file
* @param arg
*
* @note buf进去
* @return 0 0
*/
/* ----------------------------------------------------------------------------*/
int fget_disp_info(FILE *file, void *arg); //用于长文件名获取
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param path
* @param folder , /
* @param mode 1 0
*
* @return 00
*/
/* ----------------------------------------------------------------------------*/
int fmk_dir(const char *path, char *folder, u8 mode); //创建目录
/* --------------------------------------------------------------------------*/
/**
* @brief ()
*
* @param path
* @param folder
* @param ext
* @param last_num
* @param total_num
*
* @return 00
*/
/* ----------------------------------------------------------------------------*/
int fget_encfolder_info(const char *path, char *folder, char *ext, u32 *last_num, u32 *total_num); //获取录音文件信息
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param result
* @param path
* @param fname
* @param len fname长度
*
* @return 00
*/
/* ----------------------------------------------------------------------------*/
int fname_to_path(char *result, const char *path, const char *fname, int len); //把路径和文件名拼接
/* --------------------------------------------------------------------------*/
/**
* @brief path中根目录之后的文件名
*
* @param path
*
* @return
*/
/* ----------------------------------------------------------------------------*/
const char *fname_after_root(const char *path); // 截取path中根目录之后的文件名
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param path
*
* @return
*/
/* ----------------------------------------------------------------------------*/
const char *fget_fs_type(const char *path); // 获取文件系统类型
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int get_last_num(void); //录音获取最后序号。
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param clust
* @param fsize
* @param flag
*
* @note 1.
* 2.使put_bp_info对应释放buf
*/
/* ----------------------------------------------------------------------------*/
void set_bp_info(u32 clust, u32 fsize, u32 *flag); //扫描前设置断点参数需要put_bp_info对应释放buf.
/* --------------------------------------------------------------------------*/
/**
* @brief
*/
/* ----------------------------------------------------------------------------*/
void put_bp_info(void);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param enable
* @note 1.enable 1 00
* 2.
*/
/* ----------------------------------------------------------------------------*/
void ff_set_FileInDir_enable(u8 enable); // 优化文件打开速度如果不需要切换文件夹的操作可置0关闭
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param buf buf ( 12 * n)
* @param n
* @note 1.
* 2.buf使用
*/
/* ----------------------------------------------------------------------------*/
void ff_set_DirBaseInfo(void *buf, u16 n);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param year
* @param month
* @param day
* @param hour
* @param minute
* @param second
*/
/* ----------------------------------------------------------------------------*/
void fat_set_datetime_info(u16 year, u8 month, u8 day, u8 hour, u8 minute, u8 second);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param flag 1
*/
/* ----------------------------------------------------------------------------*/
void hidden_file(u8 flag);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param enable 10
*/
/* ----------------------------------------------------------------------------*/
void fat_save_already_size_enable(char enable);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param num 1-0x1fe
*/
/* ----------------------------------------------------------------------------*/
void fat_set_pre_create_chain_num(u16 num);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param btr buf
* @param buf buf指针
*
* @note seek加速4
* @return 00
*/
/* ----------------------------------------------------------------------------*/
int fsave_fat_table(FILE *file, u16 btr, u8 *buf);
/* --------------------------------------------------------------------------*/
/**
* @brief buf
*
* @param path
*
* @return 00
*/
/* ----------------------------------------------------------------------------*/
int f_flush_wbuf(const char *path);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param i_file
* @param fptr
*
* @return 00
*/
/* ----------------------------------------------------------------------------*/
int finsert_file(FILE *file, FILE *i_file, u32 fptr);
/* --------------------------------------------------------------------------*/
/**
* @brief
*
* @param file
* @param file_name
* @param fptr
*
* @return 00
*/
/* ----------------------------------------------------------------------------*/
int fdicvision_file(FILE *file, char *file_name, u32 fptr);
/* --------------------------------------------------------------------------*/
/**
* @brief 00 00
*
* @param str buff
* @param len buff长度
*
* @return
*/
/* ----------------------------------------------------------------------------*/
int long_name_fix(u8 *str, u16 len);
/* --------------------------------------------------------------------------*/
/**
* @brief UFT8 Unicode
*
* @param utf8_buf UTF8编码的字符串
* @param pUniBuf short Unicode字符串
* @param utf8_len utf8数据长度
*
* @return unicode码大小
*/
/* ----------------------------------------------------------------------------*/
int UTF82Unicode(const char *utf8_buf, u16 *pUniBuf, int utf8_len);
/* --------------------------------------------------------------------------*/
/**
* @brief Unicode UTF8
*
*
* @param utf8_buf UTF8编码的字符串
* @param pUniBuf short Unicode字符串
* @param uni_len unicode数据长度
*
* @return UTF8码大小
*/
/* ----------------------------------------------------------------------------*/
int Unicode2UTF8(char *utf8_buf, u16 *pUniBuf, int uni_len);
/* --------------------------------------------------------------------------*/
/**
* @brief UTF8
*
* @param str buff
* @param length buff长度
*
* @return 1:UTF8 0:
*/
/* ----------------------------------------------------------------------------*/
bool utf8_check(const char *str, int length);
#endif /* VFS_ENABLE */
#endif /* __FS_H__ */