浏览代码

Merge branch 'bugfix/vfs_open_errno' into 'master'

vfs: don't overwrite errno by a hard coded ENOENT (Github PR)

Closes IDFGH-6378

See merge request espressif/esp-idf!16790
Martin Vychodil 4 年之前
父节点
当前提交
f01b9a583e
共有 2 个文件被更改,包括 39 次插入1 次删除
  1. 38 0
      components/vfs/test/test_vfs_open.c
  2. 1 1
      components/vfs/vfs.c

+ 38 - 0
components/vfs/test/test_vfs_open.c

@@ -0,0 +1,38 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include "esp_vfs.h"
+#include "unity.h"
+
+static int open_errno_test_open(const char * path, int flags, int mode)
+{
+    errno = EIO;
+    return -1;
+}
+
+TEST_CASE("esp_vfs_open sets correct errno", "[vfs]")
+{
+    esp_vfs_t desc = {
+        .open = open_errno_test_open
+    };
+    TEST_ESP_OK(esp_vfs_register("/test", &desc, NULL));
+
+    int fd = open("/test/path", 0, 0);
+    int e = errno;
+    TEST_ASSERT_EQUAL(-1, fd);
+    TEST_ASSERT_EQUAL(EIO, e);
+
+    fd = open("/nonexistent/path", 0, 0);
+    e = errno;
+    TEST_ASSERT_EQUAL(-1, fd);
+    TEST_ASSERT_EQUAL(ENOENT, e);
+
+    TEST_ESP_OK(esp_vfs_unregister("/test"));
+}

+ 1 - 1
components/vfs/vfs.c

@@ -415,7 +415,7 @@ int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode)
         __errno_r(r) = ENOMEM;
         return -1;
     }
-    __errno_r(r) = ENOENT;
+    __errno_r(r) = errno;
     return -1;
 }