fix(radio): remove invalid characters from log filenames (#5129)
This commit is contained in:
+11
-28
@@ -115,58 +115,41 @@ void logsInit()
|
|||||||
|
|
||||||
const char * logsOpen()
|
const char * logsOpen()
|
||||||
{
|
{
|
||||||
|
if (!sdMounted())
|
||||||
|
return STR_NO_SDCARD;
|
||||||
|
|
||||||
// Determine and set log file filename
|
// Determine and set log file filename
|
||||||
FRESULT result;
|
FRESULT result;
|
||||||
|
|
||||||
// /LOGS/modelnamexxxxxx_YYYY-MM-DD-HHMMSS.log
|
// /LOGS/modelnamexxxxxx_YYYY-MM-DD-HHMMSS.log
|
||||||
char filename[sizeof(LOGS_PATH) + LEN_MODEL_NAME + 18 + 4 + 1];
|
char filename[sizeof(LOGS_PATH) + LEN_MODEL_NAME + 18 + 4 + 1];
|
||||||
|
|
||||||
if (!sdMounted())
|
|
||||||
return STR_NO_SDCARD;
|
|
||||||
|
|
||||||
// check and create folder here
|
// check and create folder here
|
||||||
strcpy(filename, STR_LOGS_PATH);
|
char* tmp = strAppend(filename, STR_LOGS_PATH);
|
||||||
const char * error = sdCheckAndCreateDirectory(filename);
|
const char * error = sdCheckAndCreateDirectory(filename);
|
||||||
if (error) {
|
if (error) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
filename[sizeof(LOGS_PATH) - 1] = '/';
|
tmp = strAppend(tmp, "/");
|
||||||
memcpy(&filename[sizeof(LOGS_PATH)], g_model.header.name, sizeof(g_model.header.name));
|
if (g_model.header.name[0]) {
|
||||||
filename[sizeof(LOGS_PATH) + LEN_MODEL_NAME] = '\0';
|
tmp = strAppend(tmp, sanitizeForFilename(g_model.header.name, LEN_MODEL_NAME));
|
||||||
|
} else {
|
||||||
uint8_t i = sizeof(LOGS_PATH) + LEN_MODEL_NAME - 1;
|
|
||||||
uint8_t len = 0;
|
|
||||||
while (i > sizeof(LOGS_PATH) - 1) {
|
|
||||||
if (!len && filename[i])
|
|
||||||
len = i+1;
|
|
||||||
if (len) {
|
|
||||||
if (!filename[i])
|
|
||||||
filename[i] = '_';
|
|
||||||
}
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (len == 0) {
|
|
||||||
#if defined(EEPROM)
|
#if defined(EEPROM)
|
||||||
uint8_t num = g_eeGeneral.currModel + 1;
|
uint8_t num = g_eeGeneral.currModel + 1;
|
||||||
#else
|
#else
|
||||||
// TODO
|
// TODO
|
||||||
uint8_t num = 1;
|
uint8_t num = 1;
|
||||||
#endif
|
#endif
|
||||||
strcpy(&filename[sizeof(LOGS_PATH)], STR_MODEL);
|
tmp = strAppend(tmp, STR_MODEL);
|
||||||
filename[sizeof(LOGS_PATH) + PSIZE(TR_MODEL)] = (char)((num / 10) + '0');
|
tmp = strAppendUnsigned(tmp, num, 2);
|
||||||
filename[sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 1] = (char)((num % 10) + '0');
|
|
||||||
len = sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char * tmp = &filename[len];
|
|
||||||
|
|
||||||
#if defined(RTCLOCK)
|
#if defined(RTCLOCK)
|
||||||
tmp = strAppendDate(tmp, true);
|
tmp = strAppendDate(tmp, true);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
strcpy(tmp, STR_LOGS_EXT);
|
strAppend(tmp, STR_LOGS_EXT);
|
||||||
|
|
||||||
result = f_open(&g_oLogFile, filename, FA_OPEN_ALWAYS | FA_WRITE | FA_OPEN_APPEND);
|
result = f_open(&g_oLogFile, filename, FA_OPEN_ALWAYS | FA_WRITE | FA_OPEN_APPEND);
|
||||||
if (result != FR_OK) {
|
if (result != FR_OK) {
|
||||||
|
|||||||
@@ -33,6 +33,21 @@
|
|||||||
static char _static_str_buffer[32];
|
static char _static_str_buffer[32];
|
||||||
static const char s_charTab[] = "_-.,";
|
static const char s_charTab[] = "_-.,";
|
||||||
|
|
||||||
|
const char* sanitizeForFilename(const char* name, int len)
|
||||||
|
{
|
||||||
|
strAppend(_static_str_buffer, name, len);
|
||||||
|
|
||||||
|
char *s = _static_str_buffer;
|
||||||
|
// Remove invalid characters in filename
|
||||||
|
for (int i = 0; s[i]; i += 1)
|
||||||
|
if (s[i] == '"' || s[i] == ':' || s[i] == '\\' ||
|
||||||
|
s[i] == '/' || s[i] == '<' || s[i] == '>' ||
|
||||||
|
s[i] == '?' || s[i] == '*')
|
||||||
|
s[i] = '_';
|
||||||
|
|
||||||
|
return _static_str_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
char hex2zchar(uint8_t hex) { return (hex >= 10 ? hex - 9 : 27 + hex); }
|
char hex2zchar(uint8_t hex) { return (hex >= 10 ? hex - 9 : 27 + hex); }
|
||||||
|
|
||||||
char hex2char(uint8_t hex) { return (hex >= 10 ? hex - 10 + 'A' : hex + '0'); }
|
char hex2char(uint8_t hex) { return (hex >= 10 ? hex - 10 + 'A' : hex + '0'); }
|
||||||
|
|||||||
@@ -56,6 +56,8 @@ typedef union {
|
|||||||
TimerDisplayOptions displayOptions;
|
TimerDisplayOptions displayOptions;
|
||||||
} TimerOptions;
|
} TimerOptions;
|
||||||
|
|
||||||
|
const char* sanitizeForFilename(const char* name, int len);
|
||||||
|
|
||||||
char hex2zchar(uint8_t hex);
|
char hex2zchar(uint8_t hex);
|
||||||
char hex2char(uint8_t hex);
|
char hex2char(uint8_t hex);
|
||||||
char zchar2char(int8_t idx);
|
char zchar2char(int8_t idx);
|
||||||
|
|||||||
Reference in New Issue
Block a user