Manual pages: mc • mcdiff • mcedit • mcview

root/tests/src/vfs/local/local_opendir.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mock_opendir
  2. mock_readdir
  3. setup
  4. teardown
  5. count_entries
  6. START_TEST
  7. START_TEST
  8. START_TEST
  9. START_TEST
  10. main

   1 /*
   2    src/vfs/local - tests for local_opendir() and local_readdir() functions
   3 
   4    Copyright (C) 2026
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Yury V. Zaytsev <yury@shurup.com>, 2026
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "/src/vfs/local"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include <dirent.h>
  31 #include <unistd.h>  // rmdir()
  32 
  33 #include "lib/strutil.h"
  34 
  35 /* --------------------------------------------------------------------------------------------- */
  36 /* mocked functions */
  37 /* --------------------------------------------------------------------------------------------- */
  38 
  39 static DIR *mock_opendir (const char *name);
  40 static struct dirent *mock_readdir (DIR *dirp);
  41 
  42 // route the opendir() and readdir() calls of the local VFS through the mocks
  43 #define opendir(name) mock_opendir (name)
  44 #define readdir(dirp) mock_readdir (dirp)
  45 #include "src/vfs/local/local.c"
  46 #undef opendir
  47 #undef readdir
  48 
  49 // calls to fail with EINTR before passing through
  50 static int mock_opendir__eintr_count = 0;
  51 
  52 /* @Mock */
  53 static DIR *
  54 mock_opendir (const char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
  55 {
  56     if (mock_opendir__eintr_count > 0)
  57     {
  58         mock_opendir__eintr_count--;
  59         errno = EINTR;
  60         return NULL;
  61     }
  62 
  63     return opendir (name);
  64 }
  65 
  66 // calls to fail with EINTR before passing through
  67 static int mock_readdir__eintr_count = 0;
  68 
  69 // whether to report the end of the directory right away
  70 static gboolean mock_readdir__eof = FALSE;
  71 
  72 // entries to consume before failing with EIO, or -1 to pass through
  73 static int mock_readdir__fail_after = -1;
  74 
  75 /* @Mock */
  76 static struct dirent *
  77 mock_readdir (DIR *dirp)
     /* [previous][next][first][last][top][bottom][index][help]  */
  78 {
  79     if (mock_readdir__eintr_count > 0)
  80     {
  81         mock_readdir__eintr_count--;
  82         errno = EINTR;
  83         return NULL;
  84     }
  85 
  86     if (mock_readdir__eof)
  87         return NULL;  // like readdir(), leaving errno untouched
  88 
  89     if (mock_readdir__fail_after < 0)
  90         return readdir (dirp);
  91 
  92     // FreeBSD and macOS keep the advanced offset if reading fails after partial progress
  93     for (; mock_readdir__fail_after > 0; mock_readdir__fail_after--)
  94         (void) readdir (dirp);
  95     mock_readdir__fail_after = -1;
  96 
  97     errno = EIO;
  98     return NULL;
  99 }
 100 
 101 /* --------------------------------------------------------------------------------------------- */
 102 
 103 /* @Before */
 104 static void
 105 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 106 {
 107     str_init_strings (NULL);
 108 
 109     vfs_init ();
 110     vfs_init_localfs ();
 111 }
 112 
 113 /* --------------------------------------------------------------------------------------------- */
 114 
 115 /* @After */
 116 static void
 117 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 118 {
 119     vfs_shut ();
 120     str_uninit_strings ();
 121 }
 122 
 123 /* --------------------------------------------------------------------------------------------- */
 124 
 125 static int
 126 count_entries (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 127 {
 128     void *info;
 129     struct vfs_dirent *entry;
 130     int count = 0;
 131 
 132     info = local_opendir (vpath);
 133     mctest_assert_not_null (info);
 134 
 135     while ((entry = local_readdir (info)) != NULL)
 136     {
 137         vfs_dirent_free (entry);
 138         count++;
 139     }
 140 
 141     local_closedir (info);
 142 
 143     return count;
 144 }
 145 
 146 /* --------------------------------------------------------------------------------------------- */
 147 
 148 /* @Test */
 149 START_TEST (test_local_opendir_stale_eintr)
     /* [previous][next][first][last][top][bottom][index][help]  */
 150 {
 151     char *dir;
 152     vfs_path_t *vpath;
 153     void *info;
 154 
 155     // given: a directory without any entries, not even "." and ".." (as on some FUSE filesystems),
 156     // and errno left at EINTR by an earlier interrupted syscall (#5156)
 157     dir = g_dir_make_tmp ("mctest-XXXXXX", NULL);
 158     mctest_assert_not_null (dir);
 159     vpath = vfs_path_from_str (dir);
 160     mock_readdir__eof = TRUE;
 161     errno = EINTR;
 162 
 163     // when
 164     info = local_opendir (vpath);
 165 
 166     // then: the directory is opened rather than reopened forever
 167     mctest_assert_not_null (info);
 168 
 169     // cleanup
 170     local_closedir (info);
 171     mock_readdir__eof = FALSE;
 172     vfs_path_free (vpath, TRUE);
 173     rmdir (dir);
 174     g_free (dir);
 175 }
 176 END_TEST
 177 
 178 /* --------------------------------------------------------------------------------------------- */
 179 
 180 /* @Test */
 181 START_TEST (test_local_opendir_readdir_error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 182 {
 183     char *dir;
 184     vfs_path_t *vpath;
 185     int expected;
 186     int actual;
 187 
 188     // given: a directory with entries ("." and ".."), the first readdir() of which fails with an
 189     // error other than EINTR after consuming one of them
 190     dir = g_dir_make_tmp ("mctest-XXXXXX", NULL);
 191     mctest_assert_not_null (dir);
 192     vpath = vfs_path_from_str (dir);
 193     expected = count_entries (vpath);
 194     ck_assert_int_gt (expected, 0);
 195     mock_readdir__fail_after = 1;
 196 
 197     // when
 198     actual = count_entries (vpath);
 199 
 200     // then: the stream is rewound, so that no entry is lost
 201     ck_assert_int_eq (actual, expected);
 202 
 203     // cleanup
 204     vfs_path_free (vpath, TRUE);
 205     rmdir (dir);
 206     g_free (dir);
 207 }
 208 END_TEST
 209 
 210 /* --------------------------------------------------------------------------------------------- */
 211 
 212 /* @Test */
 213 START_TEST (test_local_opendir_opendir_eintr)
     /* [previous][next][first][last][top][bottom][index][help]  */
 214 {
 215     char *dir;
 216     vfs_path_t *vpath;
 217     void *info;
 218 
 219     // given: a directory, the opening of which is interrupted once
 220     dir = g_dir_make_tmp ("mctest-XXXXXX", NULL);
 221     mctest_assert_not_null (dir);
 222     vpath = vfs_path_from_str (dir);
 223     mock_opendir__eintr_count = 1;
 224 
 225     // when
 226     info = local_opendir (vpath);
 227 
 228     // then: opening is retried
 229     mctest_assert_not_null (info);
 230 
 231     // cleanup
 232     local_closedir (info);
 233     vfs_path_free (vpath, TRUE);
 234     rmdir (dir);
 235     g_free (dir);
 236 }
 237 END_TEST
 238 
 239 /* --------------------------------------------------------------------------------------------- */
 240 
 241 /* @Test */
 242 START_TEST (test_local_readdir_eintr)
     /* [previous][next][first][last][top][bottom][index][help]  */
 243 {
 244     char *dir;
 245     vfs_path_t *vpath;
 246     void *info;
 247     struct vfs_dirent *entry;
 248     int expected;
 249     int actual;
 250 
 251     // given: a directory with entries ("." and ".."), the listing of which is interrupted once
 252     // after the first of them
 253     dir = g_dir_make_tmp ("mctest-XXXXXX", NULL);
 254     mctest_assert_not_null (dir);
 255     vpath = vfs_path_from_str (dir);
 256     expected = count_entries (vpath);
 257     ck_assert_int_gt (expected, 1);
 258     info = local_opendir (vpath);
 259     mctest_assert_not_null (info);
 260     entry = local_readdir (info);
 261     mctest_assert_not_null (entry);
 262     vfs_dirent_free (entry);
 263     mock_readdir__eintr_count = 1;
 264 
 265     // when
 266     for (actual = 1; (entry = local_readdir (info)) != NULL; actual++)
 267         vfs_dirent_free (entry);
 268 
 269     // then: reading is retried, so that the listing is complete
 270     ck_assert_int_eq (actual, expected);
 271 
 272     // cleanup
 273     local_closedir (info);
 274     vfs_path_free (vpath, TRUE);
 275     rmdir (dir);
 276     g_free (dir);
 277 }
 278 END_TEST
 279 
 280 /* --------------------------------------------------------------------------------------------- */
 281 
 282 int
 283 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 284 {
 285     TCase *tc_core;
 286 
 287     tc_core = tcase_create ("Core");
 288 
 289     tcase_add_checked_fixture (tc_core, setup, teardown);
 290 
 291     // Add new tests here: ***************
 292     tcase_add_test (tc_core, test_local_opendir_stale_eintr);
 293     tcase_add_test (tc_core, test_local_opendir_readdir_error);
 294     tcase_add_test (tc_core, test_local_opendir_opendir_eintr);
 295     tcase_add_test (tc_core, test_local_readdir_eintr);
 296     // ***********************************
 297 
 298     return mctest_run_all (tc_core);
 299 }
 300 
 301 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */