Fixes #3001: Lua io.open() could not create new files
This commit is contained in:
+7
-7
@@ -249,14 +249,14 @@ static int io_open (lua_State *L) {
|
|||||||
const char *md = luaL_optstring(L, 2, "r");
|
const char *md = luaL_optstring(L, 2, "r");
|
||||||
LStream *p = newfile(L);
|
LStream *p = newfile(L);
|
||||||
#if defined(USE_FATFS)
|
#if defined(USE_FATFS)
|
||||||
BYTE mode;
|
BYTE mode = FA_READ;
|
||||||
if (strchr(md, 'w') || strchr(md, 'a'))
|
if (*md == 'w')
|
||||||
mode = FA_WRITE;
|
mode = FA_WRITE | FA_CREATE_ALWAYS; // always create file and truncate it
|
||||||
else
|
else if (*md == 'a')
|
||||||
mode = FA_READ;
|
mode = FA_WRITE | FA_OPEN_ALWAYS; // always open file (create it if necessary)
|
||||||
FRESULT result = f_open(&p->f, filename, mode);
|
FRESULT result = f_open(&p->f, filename, mode);
|
||||||
if (result == FR_OK && strchr(md, 'a'))
|
if (result == FR_OK && *md == 'a')
|
||||||
result = f_lseek(&p->f, f_size(&p->f));
|
result = f_lseek(&p->f, f_size(&p->f)); // seek to the end of the file
|
||||||
return result == FR_OK ? 1 : 0;
|
return result == FR_OK ? 1 : 0;
|
||||||
#else
|
#else
|
||||||
const char *mode = md; /* to traverse/check mode */
|
const char *mode = md; /* to traverse/check mode */
|
||||||
|
|||||||
Reference in New Issue
Block a user