(root)/
glibc-2.38/
sysdeps/
unix/
sysv/
linux/
tst-readdir64-compat.c
       1  /* Test readdir64 compatibility symbol.
       2     Copyright (C) 2018-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <dirent.h>
      20  #include <dlfcn.h>
      21  #include <errno.h>
      22  #include <shlib-compat.h>
      23  #include <stdbool.h>
      24  #include <stdio.h>
      25  #include <string.h>
      26  #include <support/check.h>
      27  
      28  /* Copied from <olddirent.h>.  */
      29  struct __old_dirent64
      30    {
      31      __ino_t d_ino;
      32      __off64_t d_off;
      33      unsigned short int d_reclen;
      34      unsigned char d_type;
      35      char d_name[256];
      36    };
      37  
      38  typedef struct __old_dirent64 *(*compat_readdir64_type) (DIR *);
      39  
      40  #if TEST_COMPAT (libc, GLIBC_2_1, GLIBC_2_2)
      41  struct __old_dirent64 *compat_readdir64 (DIR *);
      42  compat_symbol_reference (libc, compat_readdir64, readdir64, GLIBC_2_1);
      43  #endif
      44  
      45  static int
      46  do_test (void)
      47  {
      48  #if TEST_COMPAT (libc, GLIBC_2_1, GLIBC_2_2)
      49    /* Directory stream using the non-compat readdir64 symbol.  The test
      50       checks against this.  */
      51    DIR *dir_reference = opendir (".");
      52    TEST_VERIFY_EXIT (dir_reference != NULL);
      53    DIR *dir_test = opendir (".");
      54    TEST_VERIFY_EXIT (dir_test != NULL);
      55  
      56    /* This loop assumes that the enumeration order is consistent for
      57       two different handles.  Nothing should write to the current
      58       directory (in the source tree) while this test runs, so there
      59       should not be any difference due to races.  */
      60    size_t count = 0;
      61    while (true)
      62      {
      63        errno = 0;
      64        struct dirent64 *entry_reference = readdir64 (dir_reference);
      65        if (entry_reference == NULL && errno != 0)
      66          FAIL_EXIT1 ("readdir64 entry %zu: %m\n", count);
      67        struct __old_dirent64 *entry_test = compat_readdir64 (dir_test);
      68        if (entry_reference == NULL)
      69          {
      70            if (errno == EOVERFLOW)
      71              {
      72                TEST_VERIFY (entry_reference->d_ino
      73                             != (__ino_t) entry_reference->d_ino);
      74                printf ("info: inode number overflow at entry %zu\n", count);
      75                break;
      76              }
      77            if (errno != 0)
      78              FAIL_EXIT1 ("compat readdir64 entry %zu: %m\n", count);
      79          }
      80  
      81        /* Check that both streams end at the same time.  */
      82        if (entry_reference == NULL)
      83          {
      84            TEST_VERIFY (entry_test == NULL);
      85            break;
      86          }
      87        else
      88          TEST_VERIFY_EXIT (entry_test != NULL);
      89  
      90        /* d_off is never zero because it is the offset of the next
      91           entry (not the current entry).  */
      92        TEST_VERIFY (entry_reference->d_off > 0);
      93  
      94        /* Check that the entries are the same.  */
      95        TEST_COMPARE_BLOB (entry_reference->d_name,
      96                           strlen (entry_reference->d_name),
      97                           entry_test->d_name, strlen (entry_test->d_name));
      98        TEST_COMPARE (entry_reference->d_ino, entry_test->d_ino);
      99        TEST_COMPARE (entry_reference->d_off, entry_test->d_off);
     100        TEST_COMPARE (entry_reference->d_type, entry_test->d_type);
     101        TEST_COMPARE (entry_reference->d_reclen, entry_test->d_reclen);
     102  
     103        ++count;
     104      }
     105    printf ("info: %zu directory entries found\n", count);
     106    TEST_VERIFY (count >= 3);     /* ".", "..", and some source files.  */
     107  
     108    TEST_COMPARE (closedir (dir_test), 0);
     109    TEST_COMPARE (closedir (dir_reference), 0);
     110  #endif
     111    return 0;
     112  }
     113  
     114  #include <support/test-driver.c>