Skip to content

Commit 6763898

Browse files
committed
feat(storage/vfs): cleanup path prefix handling
1 parent a45b12a commit 6763898

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

components/vfs/private_include/esp_vfs_private.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ extern "C" {
2121

2222
typedef struct vfs_entry_ {
2323
int flags; /*!< ESP_VFS_FLAG_CONTEXT_PTR and/or ESP_VFS_FLAG_READONLY_FS or ESP_VFS_FLAG_DEFAULT */
24-
const esp_vfs_fs_ops_t *vfs; // contains pointers to VFS functions
25-
void *ctx; // optional pointer which can be passed to VFS
26-
int offset; // index of this structure in s_vfs array
27-
size_t path_prefix_len; // micro-optimization to avoid doing extra strlen
28-
const char path_prefix[] __attribute__ ((counted_by (path_prefix_len))); // path prefix mapped to this VFS
24+
const esp_vfs_fs_ops_t *vfs; /*!< contains pointers to VFS functions */
25+
void *ctx; /*!< optional pointer which can be passed to VFS */
26+
int offset; /*!< index of this structure in s_vfs array */
27+
size_t path_prefix_len; /*!< micro-optimization to avoid doing extra strlen, contains length of the string, not the size of path_prefix array */
28+
const char path_prefix[]; /*!< path prefix mapped to this VFS */
2929
} vfs_entry_t;
3030

3131
/**

components/vfs/vfs.c

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,15 @@ static esp_err_t esp_vfs_make_fs_ops(const esp_vfs_t *vfs, esp_vfs_fs_ops_t **mi
395395
return ESP_ERR_NO_MEM;
396396
}
397397

398+
static bool is_path_prefix_valid(const char *path, size_t length) {
399+
return (length >= 2)
400+
&& (length <= ESP_VFS_PATH_MAX)
401+
&& (path[0] == '/')
402+
&& (path[length - 1] != '/');
403+
}
404+
398405
static esp_err_t esp_vfs_register_fs_common(
399406
const char *base_path,
400-
size_t base_path_len,
401407
const esp_vfs_fs_ops_t *vfs,
402408
int flags,
403409
void *ctx,
@@ -412,13 +418,15 @@ static esp_err_t esp_vfs_register_fs_common(
412418
return ESP_ERR_INVALID_ARG;
413419
}
414420

415-
if (base_path_len != LEN_PATH_PREFIX_IGNORED) {
416-
/* empty prefix is allowed, "/" is not allowed */
417-
if ((base_path_len == 1) || (base_path_len > ESP_VFS_PATH_MAX)) {
418-
return ESP_ERR_INVALID_ARG;
419-
}
420-
/* prefix has to start with "/" and not end with "/" */
421-
if (base_path_len >= 2 && ((base_path[0] != '/') || (base_path[base_path_len - 1] == '/'))) {
421+
size_t base_path_len = 0;
422+
const char *_base_path = "";
423+
424+
if (base_path != NULL) {
425+
base_path_len = strlen(base_path);
426+
_base_path = base_path;
427+
428+
if (base_path_len != 0 && !is_path_prefix_valid(base_path, base_path_len)) {
429+
ESP_LOGE(TAG, "Invalid path prefix");
422430
return ESP_ERR_INVALID_ARG;
423431
}
424432
}
@@ -436,25 +444,20 @@ static esp_err_t esp_vfs_register_fs_common(
436444
s_vfs_count++;
437445
}
438446

439-
size_t alloc_size = sizeof(vfs_entry_t)
440-
+ (base_path_len == LEN_PATH_PREFIX_IGNORED ? 0 : base_path_len + 1);
441-
442-
vfs_entry_t *entry = (vfs_entry_t*) heap_caps_malloc(alloc_size, VFS_MALLOC_FLAGS);
447+
vfs_entry_t *entry = heap_caps_malloc(sizeof(vfs_entry_t) + base_path_len + 1, VFS_MALLOC_FLAGS);
443448
if (entry == NULL) {
444449
return ESP_ERR_NO_MEM;
445450
}
446451

447452
s_vfs[index] = entry;
448453

449-
entry->path_prefix_len = base_path_len;
454+
entry->path_prefix_len = base_path == NULL ? LEN_PATH_PREFIX_IGNORED : base_path_len;
450455
entry->vfs = vfs;
451456
entry->ctx = ctx;
452457
entry->offset = index;
453458
entry->flags = flags;
454459

455-
if (base_path_len != LEN_PATH_PREFIX_IGNORED) {
456-
memcpy((char *)(entry->path_prefix), base_path, base_path_len + 1);
457-
}
460+
memcpy((char *)(entry->path_prefix), _base_path, base_path_len + 1);
458461

459462
if (vfs_index) {
460463
*vfs_index = index;
@@ -470,16 +473,21 @@ esp_err_t esp_vfs_register_fs(const char* base_path, const esp_vfs_fs_ops_t* vfs
470473
return ESP_ERR_INVALID_ARG;
471474
}
472475

476+
if (base_path == NULL) {
477+
ESP_LOGE(TAG, "base_path cannot be null");
478+
return ESP_ERR_INVALID_ARG;
479+
}
480+
473481
if ((flags & ESP_VFS_FLAG_STATIC)) {
474-
return esp_vfs_register_fs_common(base_path, strlen(base_path), vfs, flags, ctx, NULL);
482+
return esp_vfs_register_fs_common(base_path, vfs, flags, ctx, NULL);
475483
}
476484

477485
esp_vfs_fs_ops_t *_vfs = esp_vfs_duplicate_fs_ops(vfs);
478486
if (_vfs == NULL) {
479487
return ESP_ERR_NO_MEM;
480488
}
481489

482-
esp_err_t ret = esp_vfs_register_fs_common(base_path, strlen(base_path), _vfs, flags, ctx, NULL);
490+
esp_err_t ret = esp_vfs_register_fs_common(base_path, _vfs, flags, ctx, NULL);
483491
if (ret != ESP_OK) {
484492
esp_vfs_free_fs_ops(_vfs);
485493
return ret;
@@ -500,13 +508,29 @@ esp_err_t esp_vfs_register_common(const char* base_path, size_t len, const esp_v
500508
return ESP_ERR_INVALID_ARG;
501509
}
502510

511+
if (len != LEN_PATH_PREFIX_IGNORED) {
512+
if (base_path == NULL) {
513+
ESP_LOGE(TAG, "Path prefix cannot be NULL");
514+
return ESP_ERR_INVALID_ARG;
515+
}
516+
517+
// This is for backwards compatibility
518+
if (strlen(base_path) != len) {
519+
ESP_LOGE(TAG, "Mismatch between real and given path prefix lengths.");
520+
return ESP_ERR_INVALID_ARG;
521+
}
522+
} else {
523+
// New API expects NULL in this case
524+
base_path = NULL;
525+
}
526+
503527
esp_vfs_fs_ops_t *_vfs = NULL;
504528
esp_err_t ret = esp_vfs_make_fs_ops(vfs, &_vfs);
505529
if (ret != ESP_OK) {
506530
return ret;
507531
}
508532

509-
ret = esp_vfs_register_fs_common(base_path, len, _vfs, vfs->flags, ctx, vfs_index);
533+
ret = esp_vfs_register_fs_common(base_path, _vfs, vfs->flags, ctx, vfs_index);
510534
if (ret != ESP_OK) {
511535
esp_vfs_free_fs_ops(_vfs);
512536
return ret;
@@ -564,7 +588,7 @@ esp_err_t esp_vfs_register_fs_with_id(const esp_vfs_fs_ops_t *vfs, int flags, vo
564588
}
565589

566590
*vfs_id = -1;
567-
return esp_vfs_register_fs_common("", LEN_PATH_PREFIX_IGNORED, vfs, flags, ctx, vfs_id);
591+
return esp_vfs_register_fs_common(NULL, vfs, flags, ctx, vfs_id);
568592
}
569593

570594
esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx, esp_vfs_id_t *vfs_id)

0 commit comments

Comments
 (0)