Repository navigation
fix(local_opendir): clear stale errno before the EINTR check #5156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zyv
merged 6 commits into
MidnightCommander:master
from
KuzinAndrey:fix-local-opendir-stale-errno
Oct 8, 2026
+348
−5
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
869db18
Ticket #5156: tests: add tests for local_opendir()
zyv 7c7146b
(local_opendir): clear stale errno before checking for EINTR
KuzinAndrey 33606c3
tests: add test for local_opendir() with an interrupted opendir()
zyv 8656b03
(local_opendir): retry opendir() on EINTR
zyv d4153a1
tests: add test for local_readdir() with an interrupted readdir()
zyv 8fe5f9f
(local_readdir): retry readdir() on EINTR
zyv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
|
|
||
| SUBDIRS = | ||
| SUBDIRS = local | ||
|
|
||
| if ENABLE_VFS_EXTFS | ||
| SUBDIRS += extfs | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| PACKAGE_STRING = "/src/vfs/local" | ||
|
|
||
| AM_CPPFLAGS = \ | ||
| $(GLIB_CFLAGS) \ | ||
| -I$(top_srcdir) \ | ||
| @CHECK_CFLAGS@ | ||
|
|
||
| LIBS = @CHECK_LIBS@ \ | ||
| $(top_builddir)/src/libinternal.la \ | ||
| $(top_builddir)/lib/libmc.la | ||
|
|
||
| if ENABLE_MCLIB | ||
| LIBS += $(GLIB_LIBS) | ||
| endif | ||
|
|
||
| TESTS = \ | ||
| local_opendir | ||
|
|
||
| check_PROGRAMS = $(TESTS) | ||
|
|
||
| local_opendir_SOURCES = \ | ||
| local_opendir.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,301 @@ | ||
| /* | ||
| src/vfs/local - tests for local_opendir() and local_readdir() functions | ||
|
|
||
| Copyright (C) 2026 | ||
| Free Software Foundation, Inc. | ||
|
|
||
| Written by: | ||
| Yury V. Zaytsev <yury@shurup.com>, 2026 | ||
|
|
||
| This file is part of the Midnight Commander. | ||
|
|
||
| The Midnight Commander is free software: you can redistribute it | ||
| and/or modify it under the terms of the GNU General Public License as | ||
| published by the Free Software Foundation, either version 3 of the License, | ||
| or (at your option) any later version. | ||
|
|
||
| The Midnight Commander is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #define TEST_SUITE_NAME "/src/vfs/local" | ||
|
|
||
| #include "tests/mctest.h" | ||
|
|
||
| #include <dirent.h> | ||
| #include <unistd.h> // rmdir() | ||
|
|
||
| #include "lib/strutil.h" | ||
|
|
||
| /* --------------------------------------------------------------------------------------------- */ | ||
| /* mocked functions */ | ||
| /* --------------------------------------------------------------------------------------------- */ | ||
|
|
||
| static DIR *mock_opendir (const char *name); | ||
| static struct dirent *mock_readdir (DIR *dirp); | ||
|
|
||
| // route the opendir() and readdir() calls of the local VFS through the mocks | ||
| #define opendir(name) mock_opendir (name) | ||
| #define readdir(dirp) mock_readdir (dirp) | ||
| #include "src/vfs/local/local.c" | ||
| #undef opendir | ||
| #undef readdir | ||
|
|
||
| // calls to fail with EINTR before passing through | ||
| static int mock_opendir__eintr_count = 0; | ||
|
|
||
| /* @Mock */ | ||
| static DIR * | ||
| mock_opendir (const char *name) | ||
| { | ||
| if (mock_opendir__eintr_count > 0) | ||
| { | ||
| mock_opendir__eintr_count--; | ||
| errno = EINTR; | ||
| return NULL; | ||
| } | ||
|
|
||
| return opendir (name); | ||
| } | ||
|
|
||
| // calls to fail with EINTR before passing through | ||
| static int mock_readdir__eintr_count = 0; | ||
|
|
||
| // whether to report the end of the directory right away | ||
| static gboolean mock_readdir__eof = FALSE; | ||
|
|
||
| // entries to consume before failing with EIO, or -1 to pass through | ||
| static int mock_readdir__fail_after = -1; | ||
|
|
||
| /* @Mock */ | ||
| static struct dirent * | ||
| mock_readdir (DIR *dirp) | ||
| { | ||
| if (mock_readdir__eintr_count > 0) | ||
| { | ||
| mock_readdir__eintr_count--; | ||
| errno = EINTR; | ||
| return NULL; | ||
| } | ||
|
|
||
| if (mock_readdir__eof) | ||
| return NULL; // like readdir(), leaving errno untouched | ||
|
|
||
| if (mock_readdir__fail_after < 0) | ||
| return readdir (dirp); | ||
|
|
||
| // FreeBSD and macOS keep the advanced offset if reading fails after partial progress | ||
|
zyv marked this conversation as resolved.
|
||
| for (; mock_readdir__fail_after > 0; mock_readdir__fail_after--) | ||
| (void) readdir (dirp); | ||
| mock_readdir__fail_after = -1; | ||
|
|
||
| errno = EIO; | ||
| return NULL; | ||
| } | ||
|
|
||
| /* --------------------------------------------------------------------------------------------- */ | ||
|
|
||
| /* @Before */ | ||
| static void | ||
| setup (void) | ||
| { | ||
| str_init_strings (NULL); | ||
|
|
||
| vfs_init (); | ||
| vfs_init_localfs (); | ||
| } | ||
|
|
||
| /* --------------------------------------------------------------------------------------------- */ | ||
|
|
||
| /* @After */ | ||
| static void | ||
| teardown (void) | ||
| { | ||
| vfs_shut (); | ||
| str_uninit_strings (); | ||
| } | ||
|
|
||
| /* --------------------------------------------------------------------------------------------- */ | ||
|
|
||
| static int | ||
| count_entries (const vfs_path_t *vpath) | ||
| { | ||
| void *info; | ||
| struct vfs_dirent *entry; | ||
| int count = 0; | ||
|
|
||
| info = local_opendir (vpath); | ||
| mctest_assert_not_null (info); | ||
|
|
||
| while ((entry = local_readdir (info)) != NULL) | ||
| { | ||
| vfs_dirent_free (entry); | ||
| count++; | ||
| } | ||
|
|
||
| local_closedir (info); | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| /* --------------------------------------------------------------------------------------------- */ | ||
|
|
||
| /* @Test */ | ||
| START_TEST (test_local_opendir_stale_eintr) | ||
| { | ||
| char *dir; | ||
| vfs_path_t *vpath; | ||
| void *info; | ||
|
|
||
| // given: a directory without any entries, not even "." and ".." (as on some FUSE filesystems), | ||
| // and errno left at EINTR by an earlier interrupted syscall (#5156) | ||
| dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); | ||
| mctest_assert_not_null (dir); | ||
| vpath = vfs_path_from_str (dir); | ||
| mock_readdir__eof = TRUE; | ||
| errno = EINTR; | ||
|
|
||
| // when | ||
| info = local_opendir (vpath); | ||
|
|
||
| // then: the directory is opened rather than reopened forever | ||
| mctest_assert_not_null (info); | ||
|
|
||
| // cleanup | ||
| local_closedir (info); | ||
| mock_readdir__eof = FALSE; | ||
| vfs_path_free (vpath, TRUE); | ||
| rmdir (dir); | ||
| g_free (dir); | ||
| } | ||
| END_TEST | ||
|
|
||
| /* --------------------------------------------------------------------------------------------- */ | ||
|
|
||
| /* @Test */ | ||
| START_TEST (test_local_opendir_readdir_error) | ||
| { | ||
| char *dir; | ||
| vfs_path_t *vpath; | ||
| int expected; | ||
| int actual; | ||
|
|
||
| // given: a directory with entries ("." and ".."), the first readdir() of which fails with an | ||
| // error other than EINTR after consuming one of them | ||
| dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); | ||
| mctest_assert_not_null (dir); | ||
| vpath = vfs_path_from_str (dir); | ||
| expected = count_entries (vpath); | ||
| ck_assert_int_gt (expected, 0); | ||
| mock_readdir__fail_after = 1; | ||
|
|
||
| // when | ||
| actual = count_entries (vpath); | ||
|
|
||
| // then: the stream is rewound, so that no entry is lost | ||
| ck_assert_int_eq (actual, expected); | ||
|
|
||
| // cleanup | ||
| vfs_path_free (vpath, TRUE); | ||
| rmdir (dir); | ||
| g_free (dir); | ||
| } | ||
| END_TEST | ||
|
|
||
| /* --------------------------------------------------------------------------------------------- */ | ||
|
|
||
| /* @Test */ | ||
| START_TEST (test_local_opendir_opendir_eintr) | ||
| { | ||
| char *dir; | ||
| vfs_path_t *vpath; | ||
| void *info; | ||
|
|
||
| // given: a directory, the opening of which is interrupted once | ||
| dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); | ||
| mctest_assert_not_null (dir); | ||
| vpath = vfs_path_from_str (dir); | ||
| mock_opendir__eintr_count = 1; | ||
|
|
||
| // when | ||
| info = local_opendir (vpath); | ||
|
|
||
| // then: opening is retried | ||
| mctest_assert_not_null (info); | ||
|
|
||
| // cleanup | ||
| local_closedir (info); | ||
| vfs_path_free (vpath, TRUE); | ||
| rmdir (dir); | ||
| g_free (dir); | ||
| } | ||
| END_TEST | ||
|
|
||
| /* --------------------------------------------------------------------------------------------- */ | ||
|
|
||
| /* @Test */ | ||
| START_TEST (test_local_readdir_eintr) | ||
| { | ||
| char *dir; | ||
| vfs_path_t *vpath; | ||
| void *info; | ||
| struct vfs_dirent *entry; | ||
| int expected; | ||
| int actual; | ||
|
|
||
| // given: a directory with entries ("." and ".."), the listing of which is interrupted once | ||
| // after the first of them | ||
| dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); | ||
| mctest_assert_not_null (dir); | ||
| vpath = vfs_path_from_str (dir); | ||
| expected = count_entries (vpath); | ||
| ck_assert_int_gt (expected, 1); | ||
| info = local_opendir (vpath); | ||
| mctest_assert_not_null (info); | ||
| entry = local_readdir (info); | ||
| mctest_assert_not_null (entry); | ||
| vfs_dirent_free (entry); | ||
| mock_readdir__eintr_count = 1; | ||
|
|
||
| // when | ||
| for (actual = 1; (entry = local_readdir (info)) != NULL; actual++) | ||
| vfs_dirent_free (entry); | ||
|
|
||
| // then: reading is retried, so that the listing is complete | ||
| ck_assert_int_eq (actual, expected); | ||
|
|
||
| // cleanup | ||
| local_closedir (info); | ||
| vfs_path_free (vpath, TRUE); | ||
| rmdir (dir); | ||
| g_free (dir); | ||
| } | ||
| END_TEST | ||
|
|
||
| /* --------------------------------------------------------------------------------------------- */ | ||
|
|
||
| int | ||
| main (void) | ||
| { | ||
| TCase *tc_core; | ||
|
|
||
| tc_core = tcase_create ("Core"); | ||
|
|
||
| tcase_add_checked_fixture (tc_core, setup, teardown); | ||
|
|
||
| // Add new tests here: *************** | ||
| tcase_add_test (tc_core, test_local_opendir_stale_eintr); | ||
| tcase_add_test (tc_core, test_local_opendir_readdir_error); | ||
| tcase_add_test (tc_core, test_local_opendir_opendir_eintr); | ||
| tcase_add_test (tc_core, test_local_readdir_eintr); | ||
| // *********************************** | ||
|
|
||
| return mctest_run_all (tc_core); | ||
| } | ||
|
|
||
| /* --------------------------------------------------------------------------------------------- */ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.