vfs: fix unregister removing top level VFS instead of nested

Credits @neoniousTR.

Fixes https://github.com/espressif/esp-idf/pull/2770
This commit is contained in:
Ivan Grokhotkov 2018-12-06 19:30:05 +08:00
parent a36d714d1a
commit 61beb16225
2 changed files with 43 additions and 1 deletions

View file

@ -206,6 +206,46 @@ TEST_CASE("vfs parses paths correctly", "[vfs]")
TEST_ESP_OK( esp_vfs_unregister("") );
}
TEST_CASE("vfs unregisters correct nested mount point", "[vfs]")
{
dummy_vfs_t inst_foobar = {
.match_path = "/file",
.called = false
};
esp_vfs_t desc_foobar = DUMMY_VFS();
TEST_ESP_OK( esp_vfs_register("/foo/bar", &desc_foobar, &inst_foobar) );
dummy_vfs_t inst_foo = {
.match_path = "/bar/file",
.called = false
};
esp_vfs_t desc_foo = DUMMY_VFS();
TEST_ESP_OK( esp_vfs_register("/foo", &desc_foo, &inst_foo) );
/* basic operation */
test_opened(&inst_foobar, "/foo/bar/file");
test_not_called(&inst_foo, "/foo/bar/file");
/* this should not match anything */
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, esp_vfs_unregister("/foo/b"));
/* unregister "/foo" and check that we haven't unregistered "/foo/bar" */
TEST_ESP_OK( esp_vfs_unregister("/foo") );
test_not_called(&inst_foo, "/foo/bar/file");
test_opened(&inst_foobar, "/foo/bar/file");
/* repeat the above, with the reverse order of registration */
TEST_ESP_OK( esp_vfs_unregister("/foo/bar") );
TEST_ESP_OK( esp_vfs_register("/foo", &desc_foo, &inst_foo) );
TEST_ESP_OK( esp_vfs_register("/foo/bar", &desc_foobar, &inst_foobar) );
test_opened(&inst_foobar, "/foo/bar/file");
test_not_called(&inst_foo, "/foo/bar/file");
TEST_ESP_OK( esp_vfs_unregister("/foo") );
test_not_called(&inst_foo, "/foo/bar/file");
test_opened(&inst_foobar, "/foo/bar/file");
TEST_ESP_OK( esp_vfs_unregister("/foo/bar") );
}
void test_vfs_register(const char* prefix, bool expect_success, int line)
{

View file

@ -171,12 +171,14 @@ esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx, esp_vfs_id_t
esp_err_t esp_vfs_unregister(const char* base_path)
{
const size_t base_path_len = strlen(base_path);
for (size_t i = 0; i < s_vfs_count; ++i) {
vfs_entry_t* vfs = s_vfs[i];
if (vfs == NULL) {
continue;
}
if (memcmp(base_path, vfs->path_prefix, vfs->path_prefix_len) == 0) {
if (base_path_len == vfs->path_prefix_len &&
memcmp(base_path, vfs->path_prefix, vfs->path_prefix_len) == 0) {
free(vfs);
s_vfs[i] = NULL;