mirror of
https://github.com/nodemcu/nodemcu-firmware.git
synced 2025-01-16 20:52:57 +08:00
modify loadfile to malloc memory according to file size
This commit is contained in:
parent
7e524f7dd4
commit
6c1c6805b3
@ -652,7 +652,8 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
|
|||||||
typedef struct LoadFSF {
|
typedef struct LoadFSF {
|
||||||
int extraline;
|
int extraline;
|
||||||
int f;
|
int f;
|
||||||
char buff[LUAL_BUFFERSIZE];
|
char *buff;
|
||||||
|
int len;
|
||||||
} LoadFSF;
|
} LoadFSF;
|
||||||
|
|
||||||
|
|
||||||
@ -665,7 +666,7 @@ static const char *getFSF (lua_State *L, void *ud, size_t *size) {
|
|||||||
return "\n";
|
return "\n";
|
||||||
}
|
}
|
||||||
if (fs_eof(lf->f)) return NULL;
|
if (fs_eof(lf->f)) return NULL;
|
||||||
*size = fs_read(lf->f, lf->buff, sizeof(lf->buff));
|
*size = fs_read(lf->f, lf->buff, (lf->len));
|
||||||
return (*size > 0) ? lf->buff : NULL;
|
return (*size > 0) ? lf->buff : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -684,6 +685,9 @@ LUALIB_API int luaL_loadfsfile (lua_State *L, const char *filename) {
|
|||||||
int c;
|
int c;
|
||||||
int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
|
int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
|
||||||
lf.extraline = 0;
|
lf.extraline = 0;
|
||||||
|
// lf.len = LUAL_BUFFERSIZE;
|
||||||
|
lf.len = 0;
|
||||||
|
lf.buff = NULL;
|
||||||
if (filename == NULL) {
|
if (filename == NULL) {
|
||||||
return luaL_error(L, "filename is NULL");
|
return luaL_error(L, "filename is NULL");
|
||||||
}
|
}
|
||||||
@ -692,6 +696,10 @@ LUALIB_API int luaL_loadfsfile (lua_State *L, const char *filename) {
|
|||||||
lf.f = fs_open(filename, FS_RDONLY);
|
lf.f = fs_open(filename, FS_RDONLY);
|
||||||
if (lf.f < FS_OPEN_OK) return errfsfile(L, "open", fnameindex);
|
if (lf.f < FS_OPEN_OK) return errfsfile(L, "open", fnameindex);
|
||||||
}
|
}
|
||||||
|
lf.len = fs_size(lf.f) + 32;
|
||||||
|
lf.buff = c_zalloc(lf.len);
|
||||||
|
if(!lf.buff) return LUA_ERRMEM;
|
||||||
|
|
||||||
c = fs_getc(lf.f);
|
c = fs_getc(lf.f);
|
||||||
if (c == '#') { /* Unix exec. file? */
|
if (c == '#') { /* Unix exec. file? */
|
||||||
lf.extraline = 1;
|
lf.extraline = 1;
|
||||||
@ -701,7 +709,13 @@ LUALIB_API int luaL_loadfsfile (lua_State *L, const char *filename) {
|
|||||||
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
|
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
|
||||||
fs_close(lf.f);
|
fs_close(lf.f);
|
||||||
lf.f = fs_open(filename, FS_RDONLY); /* reopen in binary mode */
|
lf.f = fs_open(filename, FS_RDONLY); /* reopen in binary mode */
|
||||||
if (lf.f < FS_OPEN_OK) return errfsfile(L, "reopen", fnameindex);
|
if (lf.f < FS_OPEN_OK){
|
||||||
|
if(lf.buff)
|
||||||
|
c_free(lf.buff);
|
||||||
|
lf.buff = NULL;
|
||||||
|
lf.len = 0;
|
||||||
|
return errfsfile(L, "reopen", fnameindex);
|
||||||
|
}
|
||||||
/* skip eventual `#!...' */
|
/* skip eventual `#!...' */
|
||||||
while ((c = fs_getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
|
while ((c = fs_getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
|
||||||
lf.extraline = 0;
|
lf.extraline = 0;
|
||||||
@ -711,6 +725,9 @@ LUALIB_API int luaL_loadfsfile (lua_State *L, const char *filename) {
|
|||||||
|
|
||||||
if (filename) fs_close(lf.f); /* close file (even in case of errors) */
|
if (filename) fs_close(lf.f); /* close file (even in case of errors) */
|
||||||
lua_remove(L, fnameindex);
|
lua_remove(L, fnameindex);
|
||||||
|
if(lf.buff)
|
||||||
|
c_free(lf.buff);
|
||||||
|
lf.buff = NULL;
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +69,7 @@
|
|||||||
#define fs_format myspiffs_format
|
#define fs_format myspiffs_format
|
||||||
#define fs_check myspiffs_check
|
#define fs_check myspiffs_check
|
||||||
#define fs_rename myspiffs_rename
|
#define fs_rename myspiffs_rename
|
||||||
|
#define fs_size myspiffs_size
|
||||||
|
|
||||||
#define FS_NAME_MAX_LENGTH SPIFFS_OBJ_NAME_LEN
|
#define FS_NAME_MAX_LENGTH SPIFFS_OBJ_NAME_LEN
|
||||||
|
|
||||||
|
@ -165,6 +165,9 @@ void myspiffs_clearerr( int fd ){
|
|||||||
int myspiffs_rename( const char *old, const char *newname ){
|
int myspiffs_rename( const char *old, const char *newname ){
|
||||||
return SPIFFS_rename(&fs, (char *)old, (char *)newname);
|
return SPIFFS_rename(&fs, (char *)old, (char *)newname);
|
||||||
}
|
}
|
||||||
|
size_t myspiffs_size( int fd ){
|
||||||
|
return SPIFFS_size(&fs, (spiffs_file)fd);
|
||||||
|
}
|
||||||
#if 0
|
#if 0
|
||||||
void test_spiffs() {
|
void test_spiffs() {
|
||||||
char buf[12];
|
char buf[12];
|
||||||
|
@ -423,6 +423,7 @@ s32_t SPIFFS_check(spiffs *fs);
|
|||||||
*/
|
*/
|
||||||
s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);
|
s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);
|
||||||
s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);
|
s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);
|
||||||
|
s32_t SPIFFS_size(spiffs *fs, spiffs_file fh);
|
||||||
|
|
||||||
#if SPIFFS_TEST_VISUALISATION
|
#if SPIFFS_TEST_VISUALISATION
|
||||||
/**
|
/**
|
||||||
|
@ -798,6 +798,25 @@ s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s32_t SPIFFS_size(spiffs *fs, spiffs_file fh) {
|
||||||
|
SPIFFS_API_CHECK_MOUNT(fs);
|
||||||
|
SPIFFS_LOCK(fs);
|
||||||
|
|
||||||
|
spiffs_fd *fd;
|
||||||
|
s32_t res;
|
||||||
|
res = spiffs_fd_get(fs, fh, &fd);
|
||||||
|
SPIFFS_API_CHECK_RES(fs, res);
|
||||||
|
|
||||||
|
#if SPIFFS_CACHE_WR
|
||||||
|
spiffs_fflush_cache(fs, fh);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
res = fd->size;
|
||||||
|
|
||||||
|
SPIFFS_UNLOCK(fs);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
#if SPIFFS_TEST_VISUALISATION
|
#if SPIFFS_TEST_VISUALISATION
|
||||||
s32_t SPIFFS_vis(spiffs *fs) {
|
s32_t SPIFFS_vis(spiffs *fs) {
|
||||||
s32_t res = SPIFFS_OK;
|
s32_t res = SPIFFS_OK;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user