Skip to content

Commit

Permalink
Fix getHostAppDir(), broken in SmingHub#1913.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Nov 3, 2019
1 parent 26a6f7c commit f755999
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
8 changes: 5 additions & 3 deletions Sming/Arch/Host/Components/hostlib/hostlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ int msleep(unsigned ms)
return nanosleep(&req, &rem);
}

void getHostAppDir(char* path, size_t bufSize)
size_t getHostAppDir(char* path, size_t bufSize)
{
if(path == NULL ||bufSize == 0) {
return;
return 0;
}

size_t len;
Expand All @@ -46,7 +46,9 @@ void getHostAppDir(char* path, size_t bufSize)
#endif
path[len] = '\0';
char* p = strrchr(path, sep);
if(p) {
if(p != NULL) {
p[1] = '\0';
len = 1 + p - path;
}
return len;
}
9 changes: 7 additions & 2 deletions Sming/Arch/Host/Components/hostlib/hostlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ extern "C" {

int msleep(unsigned ms);

// Include trailing path separator
void getHostAppDir(char* path, size_t bufSize);
/**
* @brief Get directory where application is executing from
* @param path Receives directory path, including trailing path separator
* @param bufSize
* @retval size_t Number of characters written, excluding NUL
*/
size_t getHostAppDir(char* path, size_t bufSize);

#ifdef __cplusplus
}
Expand Down
9 changes: 6 additions & 3 deletions Sming/Arch/Host/Components/spi_flash/flashmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
static int flashFile = -1;
static size_t flashFileSize = 0x400000U;
static char flashFileName[256];
static const char* defaultFlashFileName = "flash.bin";
static const char defaultFlashFileName[] = "flash.bin";

#define SPI_FLASH_SEC_SIZE 4096

Expand All @@ -42,8 +42,11 @@ bool host_flashmem_init(FlashmemConfig& config)
strncpy(flashFileName, config.filename, sizeof(flashFileName));
flashFileName[sizeof(flashFileName) - 1] = '\0';
} else {
getHostAppDir(flashFileName, sizeof(flashFileName));
strcpy(flashFileName, defaultFlashFileName);
size_t len = getHostAppDir(flashFileName, sizeof(flashFileName));
if(len + sizeof(defaultFlashFileName) > sizeof(flashFileName)) {
return false;
}
memcpy(&flashFileName[len], defaultFlashFileName, sizeof(defaultFlashFileName));
config.filename = flashFileName;
}

Expand Down

0 comments on commit f755999

Please sign in to comment.