vfs: implement vfs_unregister

This commit is contained in:
Ivan Grokhotkov 2016-12-29 22:21:29 +08:00 committed by Ivan Grokhotkov
parent edd924f273
commit 3f889de5ab
3 changed files with 38 additions and 6 deletions

View file

@ -131,6 +131,15 @@ typedef struct
esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx); 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 * 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. * newlib when it needs to use any of the syscalls.

View file

@ -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) 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); size_t len = strlen(base_path);
if (len < 2 || len > ESP_VFS_PATH_MAX) { if (len < 2 || len > ESP_VFS_PATH_MAX) {
return ESP_ERR_INVALID_ARG; 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) { if (entry == NULL) {
return ESP_ERR_NO_MEM; 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 strcpy(entry->path_prefix, base_path); // we have already verified argument length
memcpy(&entry->vfs, vfs, sizeof(esp_vfs_t)); memcpy(&entry->vfs, vfs, sizeof(esp_vfs_t));
entry->path_prefix_len = len; entry->path_prefix_len = len;
entry->ctx = ctx; entry->ctx = ctx;
entry->offset = s_vfs_count; entry->offset = index;
s_vfs[s_vfs_count] = entry;
++s_vfs_count;
return ESP_OK; 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) static const vfs_entry_t* get_vfs_for_fd(int fd)
{ {
int index = ((fd & VFS_INDEX_MASK) >> VFS_INDEX_S); int index = ((fd & VFS_INDEX_MASK) >> VFS_INDEX_S);

View file

@ -31,6 +31,7 @@ Functions
^^^^^^^^^ ^^^^^^^^^
.. doxygenfunction:: esp_vfs_register .. doxygenfunction:: esp_vfs_register
.. doxygenfunction:: esp_vfs_unregister
.. doxygenfunction:: esp_vfs_write .. doxygenfunction:: esp_vfs_write
.. doxygenfunction:: esp_vfs_lseek .. doxygenfunction:: esp_vfs_lseek
.. doxygenfunction:: esp_vfs_read .. doxygenfunction:: esp_vfs_read