From 71fe41d0e080d73790e67d4657e24bebb0ed46dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=C3=B6rbert?= <55799864+winzkigermany@users.noreply.github.com> Date: Wed, 8 Dec 2021 11:38:12 +0100 Subject: [PATCH 1/2] vfs: don't overwrite errno by a hard coded ENOENT Calling "open" in CHECK_AND_CALL sets a perfectly correct errno. There is no need to overwrite that with a value of ENOENT, since doing so hides lower level errors like EIO. Closes https://github.com/espressif/esp-idf/pull/8036 --- components/vfs/vfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index 9adafe2208..905c636af5 100644 --- a/components/vfs/vfs.c +++ b/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; } From bce69ab7c113c534b625ee9f94e4b086d114f463 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 13 Jan 2022 17:10:00 +0100 Subject: [PATCH 2/2] vfs: add test for errno value after 'open' --- components/vfs/test/test_vfs_open.c | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 components/vfs/test/test_vfs_open.c diff --git a/components/vfs/test/test_vfs_open.c b/components/vfs/test/test_vfs_open.c new file mode 100644 index 0000000000..4db1a386ba --- /dev/null +++ b/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 +#include +#include +#include +#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")); +}