From 2feb5ac8941587d28790350bf3fde13ad9c8f403 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Tue, 27 Oct 2015 23:59:05 +0100 Subject: [PATCH 1/3] Fixes #3001: Lua io.open() could not create new files --- radio/src/thirdparty/Lua/src/liolib.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/radio/src/thirdparty/Lua/src/liolib.c b/radio/src/thirdparty/Lua/src/liolib.c index 3946e7e6d..16e2fb9d3 100644 --- a/radio/src/thirdparty/Lua/src/liolib.c +++ b/radio/src/thirdparty/Lua/src/liolib.c @@ -249,14 +249,14 @@ static int io_open (lua_State *L) { const char *md = luaL_optstring(L, 2, "r"); LStream *p = newfile(L); #if defined(USE_FATFS) - BYTE mode; - if (strchr(md, 'w') || strchr(md, 'a')) - mode = FA_WRITE; - else - mode = FA_READ; + BYTE mode = FA_READ; + if (*md == 'w') + mode = FA_WRITE | FA_CREATE_ALWAYS; // always create file and truncate it + else if (*md == 'a') + mode = FA_WRITE | FA_OPEN_ALWAYS; // always open file (create it if necessary) FRESULT result = f_open(&p->f, filename, mode); - if (result == FR_OK && strchr(md, 'a')) - result = f_lseek(&p->f, f_size(&p->f)); + if (result == FR_OK && *md == 'a') + result = f_lseek(&p->f, f_size(&p->f)); // seek to the end of the file return result == FR_OK ? 1 : 0; #else const char *mode = md; /* to traverse/check mode */ From d3e2a00cd2c49a537a68676a3005e718e9fab389 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Tue, 27 Oct 2015 23:11:35 +0100 Subject: [PATCH 2/3] Re #3001: f_open() did not support append mode (in simulator only) --- radio/src/targets/simu/simpgmspace.cpp | 17 +++++++++++++++-- radio/src/thirdparty/FatFs/ff.h | 6 +++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/radio/src/targets/simu/simpgmspace.cpp b/radio/src/targets/simu/simpgmspace.cpp index f7adb45ae..6481b42c7 100644 --- a/radio/src/targets/simu/simpgmspace.cpp +++ b/radio/src/targets/simu/simpgmspace.cpp @@ -819,10 +819,10 @@ FRESULT f_open (FIL * fil, const TCHAR *name, BYTE flag) fil->fsize = tmp.st_size; fil->fptr = 0; } - fil->fs = (FATFS*)fopen(realPath, (flag & FA_WRITE) ? "wb+" : "rb+"); + fil->fs = (FATFS*)fopen(realPath, (flag & FA_WRITE) ? ((flag & FA_CREATE_ALWAYS) ? "wb+" : "ab+") : "rb+"); fil->fptr = 0; if (fil->fs) { - TRACE("f_open(%s) = %p", path, (FILE*)fil->fs); + TRACE("f_open(%s, %x) = %p", path, flag, (FILE*)fil->fs); return FR_OK; } TRACE("f_open(%s) = error %d (%s)", path, errno, strerror(errno)); @@ -856,6 +856,19 @@ FRESULT f_lseek (FIL* fil, DWORD offset) return FR_OK; } +UINT f_size(FIL* fil) +{ + if (fil && fil->fs) { + long curr = ftell((FILE*)fil->fs); + fseek((FILE*)fil->fs, 0, SEEK_END); + long size = ftell((FILE*)fil->fs); + fseek((FILE*)fil->fs, curr, SEEK_SET); + TRACE("f_size(%p) %u", fil->fs, size); + return size; + } + return 0; +} + FRESULT f_close (FIL * fil) { if (fil && fil->fs) { diff --git a/radio/src/thirdparty/FatFs/ff.h b/radio/src/thirdparty/FatFs/ff.h index 88a0fbaa1..1f50e4867 100644 --- a/radio/src/thirdparty/FatFs/ff.h +++ b/radio/src/thirdparty/FatFs/ff.h @@ -238,7 +238,11 @@ TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the fil #define f_eof(fp) ((int)((fp)->fptr == (fp)->fsize)) #define f_error(fp) ((fp)->err) #define f_tell(fp) ((fp)->fptr) -#define f_size(fp) ((fp)->fsize) +#if !defined(SIMU) + #define f_size(fp) ((fp)->fsize) +#else + UINT f_size(FIL* fil); +#endif #ifndef EOF #define EOF (-1) From 2e068044c6821118bd864d310b0d87bfa3f6ebfd Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Tue, 27 Oct 2015 23:54:08 +0100 Subject: [PATCH 3/3] Fixes #3003: Lua io.write() did not support numbers --- radio/src/thirdparty/Lua/src/liolib.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/radio/src/thirdparty/Lua/src/liolib.c b/radio/src/thirdparty/Lua/src/liolib.c index 16e2fb9d3..d5476d298 100644 --- a/radio/src/thirdparty/Lua/src/liolib.c +++ b/radio/src/thirdparty/Lua/src/liolib.c @@ -19,6 +19,7 @@ #include #include #include +#include #define liolib_c @@ -183,14 +184,10 @@ static LStream *newprefile (lua_State *L) { #if !defined(USE_FATFS) static int aux_close (lua_State *L) { -#if !defined(USE_FATFS) LStream *p = tolstream(L); lua_CFunction cf = p->closef; p->closef = NULL; /* mark stream as closed */ return (*cf)(L); /* close it */ -#else - return 0; -#endif } #endif @@ -559,8 +556,9 @@ static int g_write (lua_State *L, FILE *f, int arg) { for (; nargs--; arg++) { if (lua_type(L, arg) == LUA_TNUMBER) { /* optimization: could be done exactly as for strings */ - status = status && - f_printf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; + char s[LUAI_MAXNUMBER2STR]; + sprintf(s, LUA_NUMBER_FMT, lua_tonumber(L, arg)); + status = status && f_puts(s, f) > 0; } else { size_t l;