diff --git a/components/vfs/include/esp_vfs.h b/components/vfs/include/esp_vfs.h index 7dd273fb0..5d9236b54 100644 --- a/components/vfs/include/esp_vfs.h +++ b/components/vfs/include/esp_vfs.h @@ -131,6 +131,15 @@ typedef struct esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx); +/** + * Unregister a virtual filesystem for given path prefix + * + * @param base_path file prefix previously used in esp_vfs_register call + * @return ESP_OK if successful, ESP_ERR_INVALID_STATE if VFS for given prefix + * hasn't been registered + */ +esp_err_t esp_vfs_unregister(const char* base_path); + /** * These functions are to be used in newlib syscall table. They will be called by * newlib when it needs to use any of the syscalls. diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index b60c60a81..b166a427b 100644 --- a/components/vfs/vfs.c +++ b/components/vfs/vfs.c @@ -54,9 +54,6 @@ static size_t s_vfs_count = 0; esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx) { - if (s_vfs_count >= VFS_MAX_COUNT) { - return ESP_ERR_NO_MEM; - } size_t len = strlen(base_path); if (len < 2 || len > ESP_VFS_PATH_MAX) { return ESP_ERR_INVALID_ARG; @@ -68,16 +65,41 @@ esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ct if (entry == NULL) { return ESP_ERR_NO_MEM; } + size_t index; + for (index = 0; index < s_vfs_count; ++index) { + if (s_vfs[index] == NULL) { + break; + } + } + if (index == s_vfs_count) { + if (s_vfs_count >= VFS_MAX_COUNT) { + free(entry); + return ESP_ERR_NO_MEM; + } + ++s_vfs_count; + } + s_vfs[index] = entry; strcpy(entry->path_prefix, base_path); // we have already verified argument length memcpy(&entry->vfs, vfs, sizeof(esp_vfs_t)); entry->path_prefix_len = len; entry->ctx = ctx; - entry->offset = s_vfs_count; - s_vfs[s_vfs_count] = entry; - ++s_vfs_count; + entry->offset = index; return ESP_OK; } +esp_err_t esp_vfs_unregister(const char* base_path) +{ + for (size_t i = 0; i < s_vfs_count; ++i) { + vfs_entry_t* vfs = s_vfs[i]; + if (memcmp(base_path, vfs->path_prefix, vfs->path_prefix_len) == 0) { + free(vfs); + s_vfs[i] = NULL; + return ESP_OK; + } + } + return ESP_ERR_INVALID_STATE; +} + static const vfs_entry_t* get_vfs_for_fd(int fd) { int index = ((fd & VFS_INDEX_MASK) >> VFS_INDEX_S); diff --git a/docs/api/vfs.rst b/docs/api/vfs.rst index 798aac549..71550f111 100644 --- a/docs/api/vfs.rst +++ b/docs/api/vfs.rst @@ -31,6 +31,7 @@ Functions ^^^^^^^^^ .. doxygenfunction:: esp_vfs_register +.. doxygenfunction:: esp_vfs_unregister .. doxygenfunction:: esp_vfs_write .. doxygenfunction:: esp_vfs_lseek .. doxygenfunction:: esp_vfs_read