Skip to content

Commit

Permalink
fix(radio): remove invalid characters from log filenames (EdgeTX#5129)
Browse files Browse the repository at this point in the history
  • Loading branch information
philmoz authored Jun 6, 2024
1 parent 8087283 commit 8e64c3b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
39 changes: 11 additions & 28 deletions radio/src/logs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,58 +115,41 @@ void logsInit()

const char * logsOpen()
{
if (!sdMounted())
return STR_NO_SDCARD;

// Determine and set log file filename
FRESULT result;

// /LOGS/modelnamexxxxxx_YYYY-MM-DD-HHMMSS.log
char filename[sizeof(LOGS_PATH) + LEN_MODEL_NAME + 18 + 4 + 1];

if (!sdMounted())
return STR_NO_SDCARD;

// check and create folder here
strcpy(filename, STR_LOGS_PATH);
char* tmp = strAppend(filename, STR_LOGS_PATH);
const char * error = sdCheckAndCreateDirectory(filename);
if (error) {
return error;
}

filename[sizeof(LOGS_PATH) - 1] = '/';
memcpy(&filename[sizeof(LOGS_PATH)], g_model.header.name, sizeof(g_model.header.name));
filename[sizeof(LOGS_PATH) + LEN_MODEL_NAME] = '\0';

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) {
tmp = strAppend(tmp, "/");
if (g_model.header.name[0]) {
tmp = strAppend(tmp, sanitizeForFilename(g_model.header.name, LEN_MODEL_NAME));
} else {
#if defined(EEPROM)
uint8_t num = g_eeGeneral.currModel + 1;
#else
// TODO
uint8_t num = 1;
#endif
strcpy(&filename[sizeof(LOGS_PATH)], STR_MODEL);
filename[sizeof(LOGS_PATH) + PSIZE(TR_MODEL)] = (char)((num / 10) + '0');
filename[sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 1] = (char)((num % 10) + '0');
len = sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 2;
tmp = strAppend(tmp, STR_MODEL);
tmp = strAppendUnsigned(tmp, num, 2);
}

char * tmp = &filename[len];

#if defined(RTCLOCK)
tmp = strAppendDate(tmp, true);
#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);
if (result != FR_OK) {
Expand Down
15 changes: 15 additions & 0 deletions radio/src/strhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@
static char _static_str_buffer[32];
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 hex2char(uint8_t hex) { return (hex >= 10 ? hex - 10 + 'A' : hex + '0'); }
Expand Down
2 changes: 2 additions & 0 deletions radio/src/strhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ typedef union {
TimerDisplayOptions displayOptions;
} TimerOptions;

const char* sanitizeForFilename(const char* name, int len);

char hex2zchar(uint8_t hex);
char hex2char(uint8_t hex);
char zchar2char(int8_t idx);
Expand Down

0 comments on commit 8e64c3b

Please sign in to comment.