* [simu] simpgmspace: Populate file date/time/size in f_stat(); Add f_utime(); Fix f_getcwd() on Windows; Fix f_mkdir() build error on Windows. * [Lua] Add runtime support for script pre-compilation. Changes behavior with LUA_COMPILER (Re: https://github.com/opentx/opentx/issues/3318): All .lua scripts are now compiled and saved to binary "bytecode" file (.luac extension) upon first execution; Scripts are also automatically re-compiled if .lua source file is newer than existing .luac file; The pre-compiled .luac version is loaded if modification time is newer or equivalent to .lua source file; If a .luac version of a script exists, then the .lua version does not need to be present; Guards against bytecode compatibility issues (binaries from 64-bit sim will not run on 32-bit sim/radio); In SIMU and DEBUG builds, the source .lua file is always preferred in order to preserve full debug info (this is is controlled with new LUA_SCRIPT_LOAD_MODE macro, see lua_api.h); GC is now run after each script is loaded. * [Lua] Add loadScript() API function as alternative to loadfile() from Lua base. This can take advantage of the new OTx script pre-compilation features to reduce memory footprint when loading functions dynamically. This is an interface to luaLoadScriptFileToState(). Fully documented. * [SD][Lua] Flexible file extensions support: Allow for variable length file extensions throughout system (no longer hard-coded in LEN_FILE_EXTENSION); Fixes issues with renaming files in SD manager which have file extensions longer than ".ext"; Expand general support for multiple file extensions per file type in sdListFiles() (eg. .lua and .luac for scripts); Lua scripts with .luac extensions can now be selected in custom/telemetry/function menus even if no .lua version exists (duplicates are not shown); .luac files can now also be executed from SD file manager UI. * [Build] Added CMake options for LUA_COMPILER and LUA_SCRIPT_LOAD_MODE. * Cosmetics. * [SD][gui] Improve efficiency of some file name handling routines in sdmanager GUI and sdListFiles() by extending getFileExtension() function. Use shared isExtensionMatching() in place of isImageFileExtension(). Only allow executing .luac files when LUA_COMPILER defined (as per request). * [simpgmspace] Fix f_mkdir() for MSVC build and misc. cleanup. * [Lua] Use getFileExtension() in script loader to determine file type and check for buffer overflow.
This commit is contained in:
committed by
Bertrand Songis
parent
28006d26a9
commit
5d5dc67605
+32
-8
@@ -45,6 +45,8 @@
|
||||
#define SCRIPTS_FUNCS_PATH SCRIPTS_PATH "/FUNCTIONS"
|
||||
#define SCRIPTS_TELEM_PATH SCRIPTS_PATH "/TELEMETRY"
|
||||
|
||||
#define LEN_FILE_PATH_MAX (sizeof(SCRIPTS_TELEM_PATH)+1) // longest + "/"
|
||||
|
||||
#if defined(COLORLCD)
|
||||
const char RADIO_MODELSLIST_PATH[] = RADIO_PATH "/models.txt";
|
||||
const char RADIO_SETTINGS_PATH[] = RADIO_PATH "/radio.bin";
|
||||
@@ -56,18 +58,27 @@ const char RADIO_SETTINGS_PATH[] = RADIO_PATH "/radio.bin";
|
||||
#define BMP_EXT ".bmp"
|
||||
#define PNG_EXT ".png"
|
||||
#define JPG_EXT ".jpg"
|
||||
#define SCRIPTS_EXT ".lua"
|
||||
#define SCRIPT_EXT ".lua"
|
||||
#define SCRIPT_BIN_EXT ".luac"
|
||||
#define TEXT_EXT ".txt"
|
||||
#define FIRMWARE_EXT ".bin"
|
||||
#define EEPROM_EXT ".bin"
|
||||
#define SPORT_FIRMWARE_EXT ".frk"
|
||||
|
||||
#define LEN_FILE_EXTENSION_MAX 5 // longest used, including the dot, excluding null term.
|
||||
|
||||
#if defined(PCBHORUS)
|
||||
#define BITMAPS_EXT BMP_EXT JPG_EXT PNG_EXT
|
||||
#else
|
||||
#define BITMAPS_EXT BMP_EXT
|
||||
#endif
|
||||
|
||||
#ifdef LUA_COMPILER
|
||||
#define SCRIPTS_EXT SCRIPT_BIN_EXT SCRIPT_EXT
|
||||
#else
|
||||
#define SCRIPTS_EXT SCRIPT_EXT
|
||||
#endif
|
||||
|
||||
#define GET_FILENAME(filename, path, var, ext) \
|
||||
char filename[sizeof(path) + sizeof(var) + sizeof(ext)]; \
|
||||
memcpy(filename, path, sizeof(path) - 1); \
|
||||
@@ -99,19 +110,31 @@ inline const pm_char * SDCARD_ERROR(FRESULT result)
|
||||
}
|
||||
#endif
|
||||
|
||||
#define LEN_FILE_EXTENSION 4
|
||||
// NOTE: 'size' must = 0 or be a valid character position within 'filename' array -- it is NOT validated
|
||||
template<class T>
|
||||
T * getFileExtension(T * filename, int size=0)
|
||||
T * getFileExtension(T * filename, uint8_t size=0, uint8_t extMaxLen=0, uint8_t *fnlen=NULL, uint8_t *extlen=NULL)
|
||||
{
|
||||
int len = strlen(filename);
|
||||
if (size != 0 && size < len) {
|
||||
len = size;
|
||||
int len = size;
|
||||
if (!size) {
|
||||
len = strlen(filename);
|
||||
}
|
||||
for (int i=len; i>=len-LEN_FILE_EXTENSION; --i) {
|
||||
if (!extMaxLen) {
|
||||
extMaxLen = LEN_FILE_EXTENSION_MAX;
|
||||
}
|
||||
if (fnlen != NULL) {
|
||||
*fnlen = (uint8_t)len;
|
||||
}
|
||||
for (int i=len-1; i >= 0 && len-i <= extMaxLen; --i) {
|
||||
if (filename[i] == '.') {
|
||||
if (extlen) {
|
||||
*extlen = len-i;
|
||||
}
|
||||
return &filename[i];
|
||||
}
|
||||
}
|
||||
if (extlen != NULL) {
|
||||
*extlen = 0;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -123,8 +146,9 @@ T * getFileExtension(T * filename, int size=0)
|
||||
#define O9X_FOURCC 0x3178396F // o9x for gruvin9x/MEGA2560
|
||||
#endif
|
||||
|
||||
bool isFileAvailable(const char * filename);
|
||||
bool isFileAvailable(const char * filename, bool exclDir = false);
|
||||
int findNextFileIndex(char * filename, uint8_t size, const char * directory);
|
||||
bool isExtensionMatching(const char * extension, const char * pattern, char * match = NULL);
|
||||
|
||||
const char * sdCopyFile(const char * src, const char * dest);
|
||||
const char * sdCopyFile(const char * srcFilename, const char * srcDir, const char * destFilename, const char * destDir);
|
||||
|
||||
Reference in New Issue
Block a user