luat_soc_sdk_2024 V1.0
luat_fs.h
浏览该文件的文档.
1/******************************************************************************
2 * 文件系统操作抽象层
3 * @author wendal
4 * @since 0.1.5
5 *****************************************************************************/
6#ifndef LUAT_FS_H
7#define LUAT_FS_H
8//#include "luat_base.h"
9#include "stdio.h"
10
11#ifndef LUAT_WEAK
12#define LUAT_WEAK __attribute__((weak))
13#endif
18typedef struct luat_fs_conf {
19 char* busname;
20 char* type;
22 const char* mount_point;
24
25typedef struct luat_fs_info
26{
27 char filesystem[8]; // 文件系统类型
28 unsigned char type; // 连接方式, 片上,spi flash, tf卡等
30 size_t block_used;
31 size_t block_size;
33
38int luat_fs_init(void);
54
61int luat_fs_info(const char* path, luat_fs_info_t *conf);
62
70FILE* luat_fs_fopen(const char *filename, const char *mode);
71
72
79int luat_fs_getc(FILE* stream);
80
88int luat_fs_fseek(FILE* stream, long int offset, int origin);
94int luat_fs_ftell(FILE* stream);
95
101int luat_fs_fclose(FILE* stream);
107int luat_fs_feof(FILE* stream);
113int luat_fs_ferror(FILE *stream);
122size_t luat_fs_fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
132size_t luat_fs_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
133
134int luat_fs_fflush(FILE *stream);
135
141int luat_fs_remove(const char *filename);
149int luat_fs_rename(const char *old_filename, const char *new_filename);
156size_t luat_fs_fsize(const char *filename);
157
163int luat_fs_fexist(const char *filename);
171int luat_fs_readline(char * buf, int bufsize, FILE * stream);
172
178void* luat_fs_mmap(FILE * stream);
179
180// TODO 文件夹相关的API
181//int luat_fs_diropen(char const* _FileName);
182
183typedef struct luat_fs_dirent
184{
185 unsigned char d_type; //0:文件;1:文件夹
186 char d_name[255];
187 size_t d_size;
189
190
196int luat_fs_mkdir(char const* _DirName);
202int luat_fs_rmdir(char const* _DirName);
203
213int luat_fs_lsdir(char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len);
214
215
222int luat_fs_truncate(const char* filename, size_t len);
228int luat_fs_dexist(const char *dir);
230#ifdef LUAT_USE_FS_VFS
231
232#ifndef LUAT_VFS_FILESYSTEM_MAX
233#define LUAT_VFS_FILESYSTEM_MAX 8
234#endif
235
236#ifndef LUAT_VFS_FILESYSTEM_MOUNT_MAX
237#define LUAT_VFS_FILESYSTEM_MOUNT_MAX 8
238#endif
239
240#ifndef LUAT_VFS_FILESYSTEM_FD_MAX
241#define LUAT_VFS_FILESYSTEM_FD_MAX 16
242#endif
243
244struct luat_vfs_file_opts {
245 FILE* (*fopen)(void* fsdata, const char *filename, const char *mode);
246 int (*getc)(void* fsdata, FILE* stream);
247 int (*fseek)(void* fsdata, FILE* stream, long int offset, int origin);
248 int (*ftell)(void* fsdata, FILE* stream);
249 int (*fclose)(void* fsdata, FILE* stream);
250 int (*feof)(void* fsdata, FILE* stream);
251 int (*ferror)(void* fsdata, FILE *stream);
252 size_t (*fread)(void* fsdata, void *ptr, size_t size, size_t nmemb, FILE *stream);
253 size_t (*fwrite)(void* fsdata, const void *ptr, size_t size, size_t nmemb, FILE *stream);
254 void* (*mmap)(void* fsdata, FILE *stream);
255 int (*fflush)(void* fsdata, FILE *stream);
256};
257
258struct luat_vfs_filesystem_opts {
259 int (*remove)(void* fsdata, const char *filename);
260 int (*rename)(void* fsdata, const char *old_filename, const char *new_filename);
261 size_t (*fsize)(void* fsdata, const char *filename);
262 int (*fexist)(void* fsdata, const char *filename);
263 int (*mkfs)(void* fsdata, luat_fs_conf_t *conf);
264
265 int (*mount)(void** fsdata, luat_fs_conf_t *conf);
266 int (*umount)(void* fsdata, luat_fs_conf_t *conf);
267 int (*info)(void* fsdata, const char* path, luat_fs_info_t *conf);
268
269 int (*mkdir)(void* fsdata, char const* _DirName);
270 int (*rmdir)(void* fsdata, char const* _DirName);
271 int (*lsdir)(void* fsdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len);
272
273 int (*truncate)(void* fsdata, char const* _DirName, size_t nsize);
274};
275
276typedef struct luat_vfs_filesystem {
277 char name[16];
278 struct luat_vfs_filesystem_opts opts;
279 struct luat_vfs_file_opts fopts;
280}luat_vfs_filesystem_t;
281
282typedef struct luat_vfs_mount {
283 struct luat_vfs_filesystem *fs;
284 void *userdata;
285 char prefix[16];
286 int ok;
287} luat_vfs_mount_t;
288
289typedef struct luat_vfs_fd{
290 FILE* fd;
291 luat_vfs_mount_t *fsMount;
292}luat_vfs_fd_t;
293
294
295typedef struct luat_vfs
296{
297 struct luat_vfs_filesystem* fsList[LUAT_VFS_FILESYSTEM_MAX];
298 luat_vfs_mount_t mounted[LUAT_VFS_FILESYSTEM_MOUNT_MAX];
299 luat_vfs_fd_t fds[LUAT_VFS_FILESYSTEM_FD_MAX+1];
300}luat_vfs_t;
301
302int luat_vfs_init(void* params);
303int luat_vfs_reg(const struct luat_vfs_filesystem* fs);
304FILE* luat_vfs_add_fd(FILE* fd, luat_vfs_mount_t * mount);
305int luat_vfs_rm_fd(FILE* fd);
306const char* luat_vfs_mmap(FILE* fd);
307#endif
308
309#endif
struct luat_fs_conf luat_fs_conf_t
int luat_fs_init(void)
文件系统初始化
int luat_fs_fexist(const char *filename)
文件是否存在,类似于fexist
struct luat_fs_info luat_fs_info_t
int luat_fs_fclose(FILE *stream)
关闭句柄位置,类似于fclose
int luat_fs_fflush(FILE *stream)
int luat_fs_umount(luat_fs_conf_t *conf)
文件系统取消挂载
int luat_fs_ferror(FILE *stream)
是否有文件系统错误,类似于ferror
int luat_fs_dexist(const char *dir)
文件夹是否存在
int luat_fs_feof(FILE *stream)
是否已经到文件结尾,类似于feof
size_t luat_fs_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
读取文件,类似于fread
int luat_fs_readline(char *buf, int bufsize, FILE *stream)
行读取
size_t luat_fs_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
写入文件,类似于fwrite
int luat_fs_fseek(FILE *stream, long int offset, int origin)
设置句柄位置,类似于fseek
struct luat_fs_dirent luat_fs_dirent_t
int luat_fs_rmdir(char const *_DirName)
删除文件夹,必须为空文件夹
int luat_fs_ftell(FILE *stream)
获取句柄位置,类似于ftell
int luat_fs_mount(luat_fs_conf_t *conf)
文件系统挂载
size_t luat_fs_fsize(const char *filename)
文件大小,类似于fsize
int luat_fs_rename(const char *old_filename, const char *new_filename)
文件改名,类似于rename
int luat_fs_getc(FILE *stream)
读到单个字节,类似于getc
FILE * luat_fs_fopen(const char *filename, const char *mode)
打开文件,类似于fopen
int luat_fs_lsdir(char const *_DirName, luat_fs_dirent_t *ents, size_t offset, size_t len)
遍历文件夹
void * luat_fs_mmap(FILE *stream)
获取文件映射地址
int luat_fs_truncate(const char *filename, size_t len)
文件截断
int luat_fs_info(const char *path, luat_fs_info_t *conf)
获取文件系统状态
int luat_fs_mkdir(char const *_DirName)
创建文件夹
int luat_fs_remove(const char *filename)
删除文件,类似于remove
int luat_fs_mkfs(luat_fs_conf_t *conf)
文件系统格式化
char * filesystem
const char * mount_point
unsigned char d_type
char filesystem[8]
size_t block_used
size_t total_block
unsigned char type
size_t block_size