fix(fsdrv): skip the path format if LV_FS_xxx_PATH not defined (#2726)

to save the stack space and fix some minor code style issue

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao
2021-10-27 03:19:59 -05:00
committed by GitHub
parent 91cf82d081
commit ec2be7e53a
5 changed files with 55 additions and 46 deletions

View File

@@ -57,7 +57,7 @@ void lv_fs_fatfs_init(void)
* Register the file system interface in LittlevGL
*--------------------------------------------------*/
/* Add a simple drive to open images */
/*Add a simple drive to open images*/
static lv_fs_drv_t fs_drv; /*A driver descriptor*/
lv_fs_drv_init(&fs_drv);
@@ -81,11 +81,11 @@ void lv_fs_fatfs_init(void)
* STATIC FUNCTIONS
**********************/
/* Initialize your Storage device and File system. */
/*Initialize your Storage device and File system.*/
static void fs_init(void)
{
/* Initialize the SD card and FatFS itself.
* Better to do it in your code to keep this library utouched for easy updating*/
/*Initialize the SD card and FatFS itself.
*Better to do it in your code to keep this library untouched for easy updating*/
}
/**
@@ -108,7 +108,6 @@ static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode)
if(f == NULL) return NULL;
FRESULT res = f_open(f, path, flags);
if(res == FR_OK) {
f_lseek(f, 0);
return f;
@@ -146,7 +145,7 @@ static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p)
static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br)
{
LV_UNUSED(drv);
FRESULT res = f_read(file_p, buf, btr, (UINT*)br);
FRESULT res = f_read(file_p, buf, btr, (UINT *)br);
if(res == FR_OK) return LV_FS_RES_OK;
else return LV_FS_RES_UNKNOWN;
}
@@ -163,7 +162,7 @@ static lv_fs_res_t fs_read(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_
static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw)
{
LV_UNUSED(drv);
FRESULT res = f_write(file_p, buf, btw, (UINT*)bw);
FRESULT res = f_write(file_p, buf, btw, (UINT *)bw);
if(res == FR_OK) return LV_FS_RES_OK;
else return LV_FS_RES_UNKNOWN;
}
@@ -232,7 +231,7 @@ static void * fs_dir_open(lv_fs_drv_t * drv, const char * path)
}
/**
* Read the next filename form a directory.
* Read the next filename from a directory.
* The name of the directories will begin with '/'
* @param drv pointer to a driver where this function belongs
* @param dir_p pointer to an initialized 'DIR' variable

View File

@@ -11,7 +11,6 @@
#if LV_USE_FS_POSIX != '\0'
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#ifndef WIN32
#include <dirent.h>
@@ -23,13 +22,6 @@
/*********************
* DEFINES
*********************/
#ifndef LV_FS_POSIX_PATH
# ifndef WIN32
# define LV_FS_POSIX_PATH "./" /*Project root*/
# else
# define LV_FS_POSIX_PATH ".\\" /*Project root*/
# endif
#endif /*LV_FS_POSIX_PATH*/
/**********************
* TYPEDEFS
@@ -66,10 +58,10 @@ static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p);
void lv_fs_posix_init(void)
{
/*---------------------------------------------------
* Register the file system interface in LittlevGL
* Register the file system interface in LittlevGL
*--------------------------------------------------*/
/* Add a simple drive to open images */
/*Add a simple drive to open images*/
static lv_fs_drv_t fs_drv; /*A driver descriptor*/
lv_fs_drv_init(&fs_drv);
@@ -103,18 +95,21 @@ void lv_fs_posix_init(void)
static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode)
{
LV_UNUSED(drv);
errno = 0;
uint32_t flags = 0;
if(mode == LV_FS_MODE_WR) flags = O_WRONLY;
else if(mode == LV_FS_MODE_RD) flags = O_RDONLY;
else if(mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) flags = O_RDWR;
#ifdef LV_FS_POSIX_PATH
/*Make the path relative to the current directory (the projects root folder)*/
char buf[256];
sprintf(buf, LV_FS_POSIX_PATH "%s", path);
int f = open(buf, flags);
#else
int f = open(path, flags);
#endif
if(f < 0) return NULL;
/*Be sure we are the beginning of the file*/
@@ -215,17 +210,25 @@ static void * fs_dir_open(lv_fs_drv_t * drv, const char * path)
LV_UNUSED(drv);
#ifndef WIN32
# ifdef LV_FS_POSIX_PATH
/*Make the path relative to the current directory (the projects root folder)*/
char buf[256];
sprintf(buf, LV_FS_POSIX_PATH "%s", path);
return opendir(buf);
# else
return opendir(path);
# endif
#else
HANDLE d = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA fdata;
/*Make the path relative to the current directory (the projects root folder)*/
char buf[256];
# ifdef LV_FS_POSIX_PATH
sprintf(buf, LV_FS_POSIX_PATH "%s\\*", path);
# else
sprintf(buf, "%s\\*", path);
# endif
strcpy(next_fn, "");
d = FindFirstFile(buf, &fdata);
@@ -247,7 +250,7 @@ static void * fs_dir_open(lv_fs_drv_t * drv, const char * path)
}
/**
* Read the next filename form a directory.
* Read the next filename from a directory.
* The name of the directories will begin with '/'
* @param drv pointer to a driver where this function belongs
* @param dir_p pointer to an initialized 'DIR' or 'HANDLE' variable
@@ -262,7 +265,6 @@ static lv_fs_res_t fs_dir_read(lv_fs_drv_t * drv, void * dir_p, char * fn)
struct dirent *entry;
do {
entry = readdir(dir_p);
if(entry) {
if(entry->d_type == DT_DIR) sprintf(fn, "/%s", entry->d_name);
else strcpy(fn, entry->d_name);
@@ -311,4 +313,4 @@ static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p)
return LV_FS_RES_OK;
}
#endif /*LV_USE_FS_POSIX*/
#endif /*LV_USE_FS_POSIX*/

View File

@@ -11,7 +11,6 @@
#if LV_USE_FS_STDIO != '\0'
#include <stdio.h>
#include <errno.h>
#ifndef WIN32
#include <dirent.h>
#include <unistd.h>
@@ -22,13 +21,6 @@
/*********************
* DEFINES
*********************/
#ifndef LV_FS_STDIO_PATH
# ifndef WIN32
# define LV_FS_STDIO_PATH "./" /*Project root*/
# else
# define LV_FS_STDIO_PATH ".\\" /*Project root*/
# endif
#endif /*LV_FS_STDIO_PATH*/
/**********************
* TYPEDEFS
@@ -68,7 +60,7 @@ void lv_fs_stdio_init(void)
* Register the file system interface in LittlevGL
*--------------------------------------------------*/
/* Add a simple drive to open images */
/*Add a simple drive to open images*/
static lv_fs_drv_t fs_drv; /*A driver descriptor*/
lv_fs_drv_init(&fs_drv);
@@ -102,7 +94,6 @@ void lv_fs_stdio_init(void)
static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode)
{
LV_UNUSED(drv);
errno = 0;
const char * flags = "";
@@ -110,12 +101,16 @@ static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode)
else if(mode == LV_FS_MODE_RD) flags = "rb";
else if(mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) flags = "rb+";
#ifdef LV_FS_STDIO_PATH
/*Make the path relative to the current directory (the projects root folder)*/
char buf[256];
sprintf(buf, LV_FS_STDIO_PATH "%s", path);
FILE * f = fopen(buf, flags);
#else
FILE * f = fopen(path, flags);
#endif
if(f == NULL) return NULL;
/*Be sure we are the beginning of the file*/
@@ -215,17 +210,25 @@ static void * fs_dir_open(lv_fs_drv_t * drv, const char * path)
{
LV_UNUSED(drv);
#ifndef WIN32
# ifdef LV_FS_STDIO_PATH
/*Make the path relative to the current directory (the projects root folder)*/
char buf[256];
sprintf(buf, LV_FS_STDIO_PATH "%s", path);
return opendir(buf);
# else
return opendir(path);
# endif
#else
HANDLE d = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA fdata;
/*Make the path relative to the current directory (the projects root folder)*/
char buf[256];
# ifdef LV_FS_STDIO_PATH
sprintf(buf, LV_FS_STDIO_PATH "%s\\*", path);
# else
sprintf(buf, "%s\\*", path);
# endif
strcpy(next_fn, "");
d = FindFirstFile(buf, &fdata);
@@ -262,7 +265,6 @@ static lv_fs_res_t fs_dir_read(lv_fs_drv_t * drv, void * dir_p, char * fn)
struct dirent *entry;
do {
entry = readdir(dir_p);
if(entry) {
if(entry->d_type == DT_DIR) sprintf(fn, "/%s", entry->d_name);
else strcpy(fn, entry->d_name);
@@ -311,4 +313,4 @@ static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p)
return LV_FS_RES_OK;
}
#endif /*LV_USE_FS_STDIO*/
#endif /*LV_USE_FS_STDIO*/

View File

@@ -15,9 +15,6 @@
/*********************
* DEFINES
*********************/
#ifndef LV_FS_WIN32_PATH
#define LV_FS_WIN32_PATH ".\\" /*Project root*/
#endif /*LV_FS_WIN32_PATH*/
/**********************
* TYPEDEFS
@@ -27,7 +24,7 @@
* STATIC PROTOTYPES
**********************/
static bool is_dots_name(LPCSTR name);
static bool is_dots_name(const char * name);
static lv_fs_res_t fs_error_from_win32(DWORD error);
static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode);
static lv_fs_res_t fs_close(lv_fs_drv_t * drv, void * file_p);
@@ -60,7 +57,7 @@ void lv_fs_win32_init(void)
* Register the file system interface in LittlevGL
*--------------------------------------------------*/
/* Add a simple drive to open images */
/*Add a simple drive to open images*/
static lv_fs_drv_t fs_drv; /*A driver descriptor*/
lv_fs_drv_init(&fs_drv);
@@ -86,12 +83,12 @@ void lv_fs_win32_init(void)
/**
* Check the dots name
* @param name Win32 error code
* @param name file or dir name
* @return true if the name is dots name
*/
static bool is_dots_name(LPCSTR name)
static bool is_dots_name(const char * name)
{
return name[0] == L'.' && (!name[1] || (name[1] == L'.' && !name[2]));
return name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2]));
}
/**
@@ -210,13 +207,19 @@ static void * fs_open(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode)
desired_access |= GENERIC_WRITE;
}
#ifdef LV_FS_WIN32_PATH
/*Make the path relative to the current directory (the projects root folder)*/
char buf[MAX_PATH];
sprintf(buf, LV_FS_WIN32_PATH "%s", path);
#endif
return (void*)CreateFileA(
#ifdef LV_FS_WIN32_PATH
buf,
#else
path,
#endif
desired_access,
FILE_SHARE_READ,
NULL,
@@ -320,7 +323,6 @@ static lv_fs_res_t fs_tell(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p)
if (!pos_p) {
return LV_FS_RES_INV_PARAM;
}
*pos_p = (uint32_t)-1;
LARGE_INTEGER file_pointer;
file_pointer.QuadPart = 0;
@@ -363,7 +365,11 @@ static void * fs_dir_open(lv_fs_drv_t * drv, const char * path)
/*Make the path relative to the current directory (the projects root folder)*/
char buf[256];
#ifdef LV_FS_WIN32_PATH
sprintf(buf, LV_FS_WIN32_PATH "%s\\*", path);
#else
sprintf(buf, "%s\\*", path);
#endif
strcpy(next_fn, "");
d = FindFirstFileA(buf, &fdata);
@@ -384,12 +390,11 @@ static void * fs_dir_open(lv_fs_drv_t * drv, const char * path)
} while (FindNextFileA(d, &fdata));
next_error = fs_error_from_win32(GetLastError());
return d;
}
/**
* Read the next filename form a directory.
* Read the next filename from a directory.
* The name of the directories will begin with '/'
* @param drv pointer to a driver where this function belongs
* @param dir_p pointer to an initialized 'DIR' or 'HANDLE' variable
@@ -443,4 +448,4 @@ static lv_fs_res_t fs_dir_close(lv_fs_drv_t * drv, void * dir_p)
: fs_error_from_win32(GetLastError());
}
#endif /*LV_USE_FS_WIN32*/
#endif /*LV_USE_FS_WIN32*/

View File

@@ -13,6 +13,7 @@ extern "C" {
/*********************
* INCLUDES
*********************/
#include "../../../lv_conf_internal.h"
/*********************
* DEFINES