]> granicus.if.org Git - esp-idf/commitdiff
vfs: fix unregister removing top level VFS instead of nested
authorIvan Grokhotkov <ivan@espressif.com>
Thu, 6 Dec 2018 11:30:05 +0000 (19:30 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Thu, 6 Dec 2018 11:43:49 +0000 (19:43 +0800)
Credits @neoniousTR.

Fixes https://github.com/espressif/esp-idf/pull/2770

components/vfs/test/test_vfs_paths.c
components/vfs/vfs.c

index e9ff6a0a0a30830f6a98064facf56033a1de0c5a..f7071d26240d4f365e74e17393792ad4466e0f3e 100644 (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)
 {
index 65718ca90faae5c2cdf0a61e30df104dd99f82d4..3032077effe3e73475286a03817ea3503593b648 100644 (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;