From 61beb16225afde8dbe28577e432c7f92ea66cd4b Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 6 Dec 2018 19:30:05 +0800 Subject: [PATCH] vfs: fix unregister removing top level VFS instead of nested Credits @neoniousTR. Fixes https://github.com/espressif/esp-idf/pull/2770 --- components/vfs/test/test_vfs_paths.c | 40 ++++++++++++++++++++++++++++ components/vfs/vfs.c | 4 ++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/components/vfs/test/test_vfs_paths.c b/components/vfs/test/test_vfs_paths.c index e9ff6a0a0..f7071d262 100644 --- a/components/vfs/test/test_vfs_paths.c +++ b/components/vfs/test/test_vfs_paths.c @@ -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) { diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index 65718ca90..3032077ef 100644 --- a/components/vfs/vfs.c +++ b/components/vfs/vfs.c @@ -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;