Browse Source

add: 添加luatos自身的vfs实现,当前仅bsp/win32启用

Wendal Chen 4 years ago
parent
commit
da5ec7928d

+ 6 - 1
bsp/win32/CMakeLists.txt

@@ -3,7 +3,7 @@
 # CMake 最低版本号要求
 cmake_minimum_required (VERSION 3.12)
 
-set(CMAKE_BUILD_TYPE "Release")
+set(CMAKE_BUILD_TYPE "Debug")
 set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
 set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O2 -Wall")
 
@@ -12,6 +12,9 @@ set(TOPROOT "../..")
 # 项目信息
 project (luatos)
 
+# 一定一定要先添加本地的头文件
+include_directories(./include)
+
 include_directories(${TOPROOT}/lua/include ${TOPROOT}/luat/include ./freertos/include ./freertos/portable/MSVC-MingW)
 
 aux_source_directory(./port PORT_SRCS)
@@ -37,6 +40,8 @@ add_library(luat ${TOPROOT}/luat/modules/luat_main.c
                  ${TOPROOT}/luat/modules/luat_lib_libcoap.c
                  ${TOPROOT}/luat/modules/luat_lib_crypto.c
                  ${TOPROOT}/luat/modules/crc.c
+                 ${TOPROOT}/luat/modules/luat_vfs.c
+                 ${TOPROOT}/luat/modules/luat_luadb.c
             )
 
 #-----------------------

+ 30 - 1
bsp/win32/port/luat_base_win32.c

@@ -60,7 +60,36 @@ const char* luat_os_bsp(void) {
     return "win32";
 }
 
-int luat_fs_init(void) {return 0;}
+extern const struct luat_vfs_filesystem vfs_fs_posix;
+extern const struct luat_vfs_filesystem vfs_fs_luadb;
+
+int luat_fs_init(void) {
+	#ifdef LUAT_USE_FS_VFS
+	// vfs进行必要的初始化
+	luat_vfs_init(NULL);
+	// 注册vfs for posix 实现
+	luat_vfs_reg(&vfs_fs_posix);
+	luat_vfs_reg(&vfs_fs_luadb);
+
+	luat_fs_conf_t conf = {
+		.busname = "",
+		.type = "posix",
+		.filesystem = "posix",
+		.mount_point = "", // window环境下, 需要支持任意路径的读取,不能强制要求必须是/
+	};
+	luat_fs_mount(&conf);
+	luat_fs_conf_t conf2 = {
+		.busname = "",
+		.type = "luadb",
+		.filesystem = "luadb",
+		.mount_point = "/luadb/",
+	};
+	luat_fs_mount(&conf2);
+	return 0;
+	#else
+	return 0;
+	#endif
+}
 
 void vConfigureTimerForRunTimeStats( void ) {}
 

+ 117 - 0
bsp/win32/port/luat_fs_win32.c

@@ -1,4 +1,5 @@
 
+#include "luat_base.h"
 #include "luat_fs.h"
 #define LUAT_LOG_TAG "luat.fs"
 #include "luat_log.h"
@@ -6,6 +7,7 @@
 #define TAG "luat.fs"
 
 // fs的默认实现, 指向poisx的stdio.h声明的方法
+#ifndef LUAT_USE_FS_VFS
 
 FILE* luat_fs_fopen(const char *filename, const char *mode) {
     //LLOGD("fopen %s %s", filename + (filename[0] == '/' ? 1 : 0), mode);
@@ -87,3 +89,118 @@ int luat_fs_rmdir(char const* _DirName) {
     LLOGE("not support yet : rmdir");
     return -1;
 }
+
+#else
+
+FILE* luat_vfs_posix_fopen(void* userdata, const char *filename, const char *mode) {
+    //LLOGD("fopen %s %s", filename + (filename[0] == '/' ? 1 : 0), mode);
+    return fopen(filename + (filename[0] == '/' ? 1 : 0), mode);
+}
+
+char luat_vfs_posix_getc(void* userdata, FILE* stream) {
+    return getc(stream);
+}
+
+int luat_vfs_posix_fseek(void* userdata, FILE* stream, long int offset, int origin) {
+    return fseek(stream, offset, origin);
+}
+
+int luat_vfs_posix_ftell(void* userdata, FILE* stream) {
+    return ftell(stream);
+}
+
+int luat_vfs_posix_fclose(void* userdata, FILE* stream) {
+    return fclose(stream);
+}
+int luat_vfs_posix_feof(void* userdata, FILE* stream) {
+    return feof(stream);
+}
+int luat_vfs_posix_ferror(void* userdata, FILE *stream) {
+    return ferror(stream);
+}
+size_t luat_vfs_posix_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream) {
+    return fread(ptr, size, nmemb, stream);
+}
+size_t luat_vfs_posix_fwrite(void* userdata, const void *ptr, size_t size, size_t nmemb, FILE *stream) {
+    return fwrite(ptr, size, nmemb, stream);
+}
+int luat_vfs_posix_remove(void* userdata, const char *filename) {
+    return remove(filename + (filename[0] == '/' ? 1 : 0));
+}
+int luat_vfs_posix_rename(void* userdata, const char *old_filename, const char *new_filename) {
+    return rename(old_filename + (old_filename[0] == '/' ? 1 : 0), new_filename + (new_filename[0] == '/' ? 1 : 0));
+}
+int luat_vfs_posix_fexist(void* userdata, const char *filename) {
+    FILE* fd = luat_vfs_posix_fopen(userdata, filename, "rb");
+    if (fd) {
+        luat_vfs_posix_fclose(userdata, fd);
+        return 1;
+    }
+    return 0;
+}
+
+size_t luat_vfs_posix_fsize(void* userdata, const char *filename) {
+    FILE *fd;
+    size_t size = 0;
+    fd = luat_vfs_posix_fopen(userdata, filename, "rb");
+    if (fd) {
+        luat_vfs_posix_fseek(userdata, fd, 0, SEEK_END);
+        size = luat_vfs_posix_ftell(userdata, fd); 
+        luat_vfs_posix_fclose(userdata, fd);
+    }
+    return size;
+}
+
+int luat_vfs_posix_mkfs(void* userdata, luat_fs_conf_t *conf) {
+    LLOGE("not support yet : mkfs");
+    return -1;
+}
+int luat_vfs_posix_mount(void** userdata, luat_fs_conf_t *conf) {
+    //LLOGE("not support yet : mount");
+    return 0;
+}
+int luat_vfs_posix_umount(void* userdata, luat_fs_conf_t *conf) {
+    //LLOGE("not support yet : umount");
+    return 0;
+}
+
+int luat_vfs_posix_mkdir(void* userdata, char const* _DirName) {
+    LLOGE("not support yet : mkdir");
+    return -1;
+}
+int luat_vfs_posix_rmdir(void* userdata, char const* _DirName) {
+    LLOGE("not support yet : rmdir");
+    return -1;
+}
+
+#define T(name) .name = luat_vfs_posix_##name
+const struct luat_vfs_filesystem vfs_fs_posix = {
+    .name = "posix",
+    .opts = {
+        T(mkfs),
+        T(mount),
+        T(umount),
+        T(mkdir),
+        T(rmdir),
+        T(remove),
+        T(rename),
+        T(fsize),
+        T(fexist)
+    },
+    .fopts = {
+        T(fopen),
+        T(getc),
+        T(fseek),
+        T(ftell),
+        T(fclose),
+        T(feof),
+        T(ferror),
+        T(fread),
+        T(fwrite)
+    }
+};
+
+#endif
+
+
+

+ 1 - 1
lua/include/luaconf.h

@@ -11,7 +11,7 @@
 #include <limits.h>
 #include <stddef.h>
 
-#include "luat_conf_bsp.h"
+#include <luat_conf_bsp.h>
 #include "luat_conf_default.h"
 
 /*

+ 0 - 7
luat/include/luat_conf_default.h

@@ -35,13 +35,6 @@
     #define LUAL_BUFFERSIZE LUAT_CONF_LAUX_BUFFSIZE
 #endif
 
-// 如果是windows, 默认打开LUA_COMPAT_BITLIB
-#ifdef LUA_USE_WINDOWS
-#ifndef LUA_COMPAT_BITLIB
-#define LUA_COMPAT_BITLIB 1
-#endif
-#endif
-
 //------------------------------
 // LuatOS 特性
 //-----------------------------

+ 7 - 0
luat/include/luat_crypto.h

@@ -16,3 +16,10 @@ int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* ma
 
 int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr);
 int luat_crypto_hmac_sha1_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr);
+
+int luat_crypto_sha256_simple(const char* str, size_t str_size, void* out_ptr);
+int luat_crypto_hmac_sha256_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) ;
+
+int luat_crypto_sha512_simple(const char* str, size_t str_size, void* out_ptr) ;
+int luat_crypto_hmac_sha512_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) ;
+

+ 72 - 0
luat/include/luat_fs.h

@@ -56,4 +56,76 @@ int luat_fs_fexist(const char *filename);
 int luat_fs_mkdir(char const* _DirName);
 int luat_fs_rmdir(char const* _DirName);
 
+
+#ifdef LUAT_USE_FS_VFS
+
+#ifndef LUAT_VFS_FILESYSTEM_MAX
+#define LUAT_VFS_FILESYSTEM_MAX 4
+#endif
+
+#ifndef LUAT_VFS_FILESYSTEM_MOUNT_MAX
+#define LUAT_VFS_FILESYSTEM_MOUNT_MAX 4
+#endif
+
+#ifndef LUAT_VFS_FILESYSTEM_FD_MAX
+#define LUAT_VFS_FILESYSTEM_FD_MAX 4
+#endif
+
+struct luat_vfs_file_opts {
+    FILE* (*fopen)(void* fsdata, const char *filename, const char *mode);
+    char (*getc)(void* fsdata, FILE* stream);
+    int (*fseek)(void* fsdata, FILE* stream, long int offset, int origin);
+    int (*ftell)(void* fsdata, FILE* stream);
+    int (*fclose)(void* fsdata, FILE* stream);
+    int (*feof)(void* fsdata, FILE* stream);
+    int (*ferror)(void* fsdata, FILE *stream);
+    size_t (*fread)(void* fsdata, void *ptr, size_t size, size_t nmemb, FILE *stream);
+    size_t (*fwrite)(void* fsdata, const void *ptr, size_t size, size_t nmemb, FILE *stream);
+};
+
+struct luat_vfs_filesystem_opts {
+    int (*remove)(void* fsdata, const char *filename);
+    int (*rename)(void* fsdata, const char *old_filename, const char *new_filename);
+    size_t (*fsize)(void* fsdata, const char *filename);
+    int (*fexist)(void* fsdata, const char *filename);
+    int (*mkfs)(void* fsdata, luat_fs_conf_t *conf);
+
+    int (*mount)(void** fsdata, luat_fs_conf_t *conf);
+    int (*umount)(void* fsdata, luat_fs_conf_t *conf);
+    int (*info)(void* fsdata, const char* path, luat_fs_info_t *conf);
+
+    int (*mkdir)(void* fsdata, char const* _DirName);
+    int (*rmdir)(void* fsdata, char const* _DirName);
+};
+
+struct luat_vfs_filesystem {
+    char name[16];
+    struct luat_vfs_filesystem_opts opts;
+    struct luat_vfs_file_opts fopts;
+};
+
+typedef struct luat_vfs_mount {
+    struct luat_vfs_filesystem *fs;
+    void *userdata;
+    char prefix[16];
+    int ok;
+} luat_vfs_mount_t;
+
+typedef struct luat_vfs_fd{
+    FILE* fd;
+    luat_vfs_mount_t *fsMount;
+}luat_vfs_fd_t;
+
+
+typedef struct luat_vfs
+{
+    struct luat_vfs_filesystem* fsList[LUAT_VFS_FILESYSTEM_MAX];
+    luat_vfs_mount_t mounted[LUAT_VFS_FILESYSTEM_MOUNT_MAX];
+    luat_vfs_fd_t fds[LUAT_VFS_FILESYSTEM_FD_MAX];
+}luat_vfs_t;
+
+int luat_vfs_init(void* params);
+int luat_vfs_reg(struct luat_vfs_filesystem* fs);
+#endif
+
 #endif

+ 114 - 0
luat/modules/luat_luadb.c

@@ -2,6 +2,7 @@
 #include "luat_base.h"
 #include "luat_luadb.h"
 #include "luat_malloc.h"
+#include "luat_fs.h"
 
 #define LUAT_LOG_TAG "luadb"
 #include "luat_log.h"
@@ -303,3 +304,116 @@ _after_head:
         return NULL;
     }
 }
+
+#ifdef LUAT_USE_FS_VFS
+
+FILE* luat_vfs_luadb_fopen(void* userdata, const char *filename, const char *mode) {
+    return luat_luadb_open((luadb_fs_t*)userdata, filename, 0, 0);
+}
+
+
+int luat_vfs_luadb_fseek(void* userdata, FILE* stream, long int offset, int origin) {
+    return luat_luadb_lseek((luadb_fs_t*)userdata, (int)stream, offset, origin);
+}
+
+int luat_vfs_luadb_ftell(void* userdata, FILE* stream) {
+    return luat_luadb_lseek((luadb_fs_t*)userdata, (int)stream, 0, SEEK_CUR);
+}
+
+int luat_vfs_luadb_fclose(void* userdata, FILE* stream) {
+    return luat_luadb_close((luadb_fs_t*)userdata, (int)stream);
+}
+int luat_vfs_luadb_feof(void* userdata, FILE* stream) {
+    return feof(stream);
+}
+int luat_vfs_luadb_ferror(void* userdata, FILE *stream) {
+    return 0;
+}
+size_t luat_vfs_luadb_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream) {
+    return luat_luadb_read((luadb_fs_t*)userdata, (int)stream, ptr, size * nmemb);
+}
+
+char luat_vfs_luadb_getc(void* userdata, FILE* stream) {
+    char c = 0;
+    luat_vfs_luadb_fread(userdata, &c, 1, 1, stream);
+    return c;
+}
+size_t luat_vfs_luadb_fwrite(void* userdata, const void *ptr, size_t size, size_t nmemb, FILE *stream) {
+    return 0;
+}
+int luat_vfs_luadb_remove(void* userdata, const char *filename) {
+    return -1;
+}
+int luat_vfs_luadb_rename(void* userdata, const char *old_filename, const char *new_filename) {
+    return -1;
+}
+int luat_vfs_luadb_fexist(void* userdata, const char *filename) {
+    FILE* fd = luat_vfs_luadb_fopen(userdata, filename, "rb");
+    if (fd) {
+        luat_vfs_luadb_fclose(userdata, fd);
+        return 1;
+    }
+    return 0;
+}
+
+size_t luat_vfs_luadb_fsize(void* userdata, const char *filename) {
+    FILE *fd;
+    size_t size = 0;
+    fd = luat_vfs_luadb_fopen(userdata, filename, "rb");
+    if (fd) {
+        luat_vfs_luadb_fseek(userdata, fd, 0, SEEK_END);
+        size = luat_vfs_luadb_ftell(userdata, fd); 
+        luat_vfs_luadb_fclose(userdata, fd);
+    }
+    return size;
+}
+
+int luat_vfs_luadb_mkfs(void* userdata, luat_fs_conf_t *conf) {
+    //LLOGE("not support yet : mkfs");
+    return -1;
+}
+int luat_vfs_luadb_mount(void** userdata, luat_fs_conf_t *conf) {
+    //LLOGE("not support yet : mount");
+    return 0;
+}
+int luat_vfs_luadb_umount(void* userdata, luat_fs_conf_t *conf) {
+    //LLOGE("not support yet : umount");
+    return 0;
+}
+
+int luat_vfs_luadb_mkdir(void* userdata, char const* _DirName) {
+    //LLOGE("not support yet : mkdir");
+    return -1;
+}
+int luat_vfs_luadb_rmdir(void* userdata, char const* _DirName) {
+    //LLOGE("not support yet : rmdir");
+    return -1;
+}
+
+#define T(name) .name = luat_vfs_luadb_##name
+const struct luat_vfs_filesystem vfs_fs_luadb = {
+    .name = "luadb",
+    .opts = {
+        T(mkfs),
+        T(mount),
+        T(umount),
+        T(mkdir),
+        T(rmdir),
+        T(remove),
+        T(rename),
+        T(fsize),
+        T(fexist)
+    },
+    .fopts = {
+        T(fopen),
+        T(getc),
+        T(fseek),
+        T(ftell),
+        T(fclose),
+        T(feof),
+        T(ferror),
+        T(fread),
+        T(fwrite)
+    }
+};
+#endif