spiffs: Prefix filenames with '/' as other implmenetations do

This commit is contained in:
YAMAMOTO Takashi 2020-12-21 16:15:06 +09:00 committed by Xiang Xiao
parent 9b7e7efeb8
commit c9b783e5e6
4 changed files with 54 additions and 5 deletions

View File

@ -183,6 +183,27 @@ config SPIFFS_COMPAT_OLD_NUTTX
Enable the compatiblilty with older NuttX versions.
(Older than NuttX 11.0.0, where this option was introduced.)
Note: For better compatibility, you may want to disable
CONFIG_SPIFFS_LEADING_SLASH because those versions of NuttX
didn't have the option.
Note: This config affects the on-flash structure.
config SPIFFS_LEADING_SLASH
bool "Prefix every filename with a slash"
default y
---help---
Assume and ensure that on-flash filenames are always prefixed
with a slash.
This enables the compatibily with images generated by
the following tools:
* mkspiffs
* ESP-IDF spiffsgen.py
Note: SPIFFS_NAME_MAX doesn't include the leading slash appended
by this option.
Note: This config affects the on-flash structure.
endif # FS_SPIFFS

View File

@ -318,7 +318,12 @@ static int spiffs_find_objhdr_pgndx_callback(FAR struct spiffs_s *fs,
SPIFFS_PH_FLAG_NDXDELE)) ==
(SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_NDXDELE))
{
if (strcmp((FAR const char *)user_const, (FAR char *)objhdr.name) == 0)
if (
#ifdef CONFIG_SPIFFS_LEADING_SLASH
((FAR char *)objhdr.name)[0] == '/' &&
#endif
strcmp((FAR const char *)user_const,
(FAR char *)objhdr.name + SPIFFS_LEADING_SLASH_SIZE) == 0)
{
return OK;
}
@ -1384,7 +1389,11 @@ int spiffs_fobj_create(FAR struct spiffs_s *fs,
objndx_hdr.type = type;
objndx_hdr.size = SPIFFS_UNDEFINED_LEN;
strncpy((char *)objndx_hdr.name, (const char *)name,
#ifdef CONFIG_SPIFFS_LEADING_SLASH
objndx_hdr.name[0] = '/';
#endif
strncpy((char *)objndx_hdr.name + SPIFFS_LEADING_SLASH_SIZE,
(const char *)name,
CONFIG_SPIFFS_NAME_MAX);
/* Update page */
@ -1472,7 +1481,8 @@ int spiffs_fobj_update_ndxhdr(FAR struct spiffs_s *fs,
if (name != NULL)
{
strncpy((FAR char *)objhdr->name, (FAR const char *)name,
strncpy((FAR char *)objhdr->name + SPIFFS_LEADING_SLASH_SIZE,
(FAR const char *)name,
CONFIG_SPIFFS_NAME_MAX);
}

View File

@ -397,6 +397,12 @@ begin_packed_struct struct spiffs_page_header_s
/* Object index header page header */
#ifdef CONFIG_SPIFFS_LEADING_SLASH
#define SPIFFS_LEADING_SLASH_SIZE 1
#else
#define SPIFFS_LEADING_SLASH_SIZE 0
#endif
begin_packed_struct struct spiffs_pgobj_ndxheader_s
{
struct spiffs_page_header_s phdr; /* common page header */
@ -406,7 +412,8 @@ begin_packed_struct struct spiffs_pgobj_ndxheader_s
#endif
uint32_t size; /* size of object */
uint8_t type; /* type of object */
uint8_t name[CONFIG_SPIFFS_NAME_MAX]; /* name of object */
uint8_t name[SPIFFS_LEADING_SLASH_SIZE +
CONFIG_SPIFFS_NAME_MAX]; /* name of object */
} end_packed_struct;
/* Object index page header */

View File

@ -330,7 +330,18 @@ static int spiffs_readdir_callback(FAR struct spiffs_s *fs,
DEBUGASSERT(dir != NULL);
entryp = &dir->fd_dir;
strncpy(entryp->d_name, (FAR char *)objhdr.name, NAME_MAX + 1);
#ifdef CONFIG_SPIFFS_LEADING_SLASH
/* Skip the leading '/'. */
if (objhdr.name[0] != '/')
{
return -EINVAL; /* The filesystem is corrupted */
}
#endif
strncpy(entryp->d_name,
(FAR char *)objhdr.name + SPIFFS_LEADING_SLASH_SIZE,
NAME_MAX + 1);
entryp->d_type = objhdr.type;
return OK;
}