Skip to content

Commit

Permalink
feat(lua): add 'mkdir' and 'rename' functions (#4870)
Browse files Browse the repository at this point in the history
  • Loading branch information
philmoz authored Apr 28, 2024
1 parent ea8a0bc commit dc2998e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 30 deletions.
73 changes: 69 additions & 4 deletions radio/src/lua/api_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int luaDir(lua_State* L)

FRESULT res = f_opendir(dir, path);
if (res != FR_OK) {
TRACE("luaDir cannot open %s\n", path);
TRACE("luaDir cannot open %s", path);
return 0;
}

Expand Down Expand Up @@ -130,7 +130,7 @@ int luaFstat(lua_State* L)

res = f_stat(path, &info);
if (res != FR_OK) {
TRACE("luaFstat cannot open %s\n", path);
TRACE("luaFstat cannot open %s", path);
return 0;
}

Expand Down Expand Up @@ -175,14 +175,76 @@ int luaDelete(lua_State* L)

FRESULT res = f_unlink(filename);
if (res != FR_OK) {
TRACE("luaDelete cannot delete file/folder %s\n", filename);
return 0;
TRACE("luaDelete cannot delete file/folder %s", filename);
}

lua_pushunsigned(L, res);
return 1;
}

/*luadoc
@function chdir(directory)
Change the working directory
@param directory (string) New working directory
@status current Introduced in 2.3.0
*/

static int luaChdir(lua_State * L)
{
const char * directory = luaL_optstring(L, 1, nullptr);
f_chdir(directory);
return 0;
}

/*luadoc
@function mkdir(directory)
Create a directory
@param directory (string) Directory to create
@status current Introduced in 2.11.0
Returns FRESULT (e.g. 0=OK, 6=Path invalid, 8=Directory exists)
*/

static int luaMkdir(lua_State * L)
{
const char * directory = luaL_checkstring(L, 1);
FRESULT res = f_mkdir(directory);
lua_pushunsigned(L, res);
return 1;
}

/*luadoc
@function rename(from_path, to_path)
Rename a file or directory
If the file or directory is being moved to a new parent directory, then the new parent
directory must already exist.
@param from_path (string) Current path to rename
@param to_path (string) Path to rename to
@status current Introduced in 2.11.0
Returns FRESULT (e.g. 0=OK, 6=Path invalid, 8=Directory exists)
*/

static int luaRename(lua_State * L)
{
const char * from_path = luaL_checkstring(L, 1);
const char * to_path = luaL_checkstring(L, 2);
FRESULT res = f_rename(from_path, to_path);
lua_pushunsigned(L, res);
return 1;
}

LROT_BEGIN(dir_handle, NULL, LROT_MASK_GC)
LROT_FUNCENTRY( __gc, dir_gc )
Expand All @@ -192,6 +254,9 @@ LROT_BEGIN(etxdir, NULL, 0)
LROT_FUNCENTRY( dir, luaDir )
LROT_FUNCENTRY( fstat, luaFstat )
LROT_FUNCENTRY( del, luaDelete )
LROT_FUNCENTRY( chdir, luaChdir )
LROT_FUNCENTRY( mkdir, luaMkdir )
LROT_FUNCENTRY( rename, luaRename )
LROT_END(etxdir, NULL, 0)

extern "C" {
Expand Down
19 changes: 0 additions & 19 deletions radio/src/lua/api_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2061,24 +2061,6 @@ static int luaGetRSSI(lua_State * L)
return 3;
}

/*luadoc
@function chdir(directory)
Change the working directory
@param directory (string) New working directory
@status current Introduced in 2.3.0
*/

static int luaChdir(lua_State * L)
{
const char * directory = luaL_optstring(L, 1, nullptr);
f_chdir(directory);
return 0;
}

/*luadoc
@function loadScript(file [, mode], [,env])
Expand Down Expand Up @@ -2932,7 +2914,6 @@ LROT_BEGIN(etxlib, NULL, 0)
LROT_FUNCENTRY( defaultChannel, luaDefaultChannel )
LROT_FUNCENTRY( getRSSI, luaGetRSSI )
LROT_FUNCENTRY( killEvents, luaKillEvents )
LROT_FUNCENTRY( chdir, luaChdir )
LROT_FUNCENTRY( loadScript, luaLoadScript )
LROT_FUNCENTRY( getUsage, luaGetUsage )
LROT_FUNCENTRY( getAvailableMemory, luaGetAvailableMemory )
Expand Down
25 changes: 18 additions & 7 deletions radio/src/targets/simu/simufatfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,13 +511,24 @@ FRESULT f_mkdir (const TCHAR * name)
FRESULT f_unlink (const TCHAR * name)
{
std::string path = convertToSimuPath(name);
if (unlink(path.c_str())) {
TRACE_SIMPGMSPACE("f_unlink(%s) = error %d (%s)", path.c_str(), errno, strerror(errno));
return FR_INVALID_NAME;
}
else {
TRACE_SIMPGMSPACE("f_unlink(%s) = OK", path.c_str());
return FR_OK;
if (isFile(path)) {
if (unlink(path.c_str())) {
TRACE_SIMPGMSPACE("f_unlink(%s) = error %d (%s)", path.c_str(), errno, strerror(errno));
return FR_INVALID_NAME;
}
else {
TRACE_SIMPGMSPACE("f_unlink(%s) = OK", path.c_str());
return FR_OK;
}
} else {
if (rmdir(path.c_str())) {
TRACE_SIMPGMSPACE("f_unlink(%s) = error %d (%s)", path.c_str(), errno, strerror(errno));
return FR_INVALID_NAME;
}
else {
TRACE_SIMPGMSPACE("f_unlink(%s) = OK", path.c_str());
return FR_OK;
}
}
}

Expand Down

0 comments on commit dc2998e

Please sign in to comment.