Skip to content
Snippets Groups Projects
Commit fb1a385c authored by Hauke Mehrtens's avatar Hauke Mehrtens
Browse files

BLE: FileTrans: Create a directory if needed


When a file should be created in a non existing folder, it will now be
automatically created.

Signed-off-by: default avatarHauke Mehrtens <hauke@hauke-m.de>
parent 8e8d8614
No related branches found
No related tags found
No related merge requests found
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "crc32.h" #include "crc32.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -227,6 +228,38 @@ static void sendCrcResponse( ...@@ -227,6 +228,38 @@ static void sendCrcResponse(
AttsHandleValueNtf(connId, FILE_TRANS_CENTRAL_RX_VAL_HDL, len, answer); AttsHandleValueNtf(connId, FILE_TRANS_CENTRAL_RX_VAL_HDL, len, answer);
} }
/*
* This function splits the path into the folders and the file name and
* creates all the missing folders.
* This function modifies the given parameter!
*/
static int bleFileCreateOrOpen(char *filepath)
{
char *path;
char *fileName = NULL;
int ret;
while (true) {
path = strtok(filepath, "/");
filepath = NULL;
if (path == NULL)
break;
/* ignore relative paths */
if (strcmp(path, "..") == 0 || strcmp(path, ".") == 0)
return -EINVAL;
if (fileName) {
ret = epic_file_mkdir(fileName);
if (ret)
return ret;
}
fileName = path;
}
return epic_file_open(filepath, "w");
}
static uint8_t bleFileOpen(dmConnId_t connId, uint8_t *pValue, uint16_t len) static uint8_t bleFileOpen(dmConnId_t connId, uint8_t *pValue, uint16_t len)
{ {
char filepath[100]; char filepath[100];
...@@ -242,7 +275,7 @@ static uint8_t bleFileOpen(dmConnId_t connId, uint8_t *pValue, uint16_t len) ...@@ -242,7 +275,7 @@ static uint8_t bleFileOpen(dmConnId_t connId, uint8_t *pValue, uint16_t len)
if (file_fd != -1) if (file_fd != -1)
epic_file_close(file_fd); epic_file_close(file_fd);
file_fd = epic_file_open(filepath, "w"); file_fd = bleFileCreateOrOpen(filepath);
if (file_fd < 0) { if (file_fd < 0) {
sendCrcResponse(connId, 'e', 0, NULL, "open failed"); sendCrcResponse(connId, 'e', 0, NULL, "open failed");
return ATT_ERR_RESOURCES; return ATT_ERR_RESOURCES;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment