Blame sysdeps/unix/sysv/linux/tst-readdir64-compat.c

Packit Service 516631
/* Test readdir64 compatibility symbol.
Packit Service 516631
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Service 516631
   This file is part of the GNU C Library.
Packit Service 516631
Packit Service 516631
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 516631
   modify it under the terms of the GNU Lesser General Public
Packit Service 516631
   License as published by the Free Software Foundation; either
Packit Service 516631
   version 2.1 of the License, or (at your option) any later version.
Packit Service 516631
Packit Service 516631
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 516631
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 516631
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 516631
   Lesser General Public License for more details.
Packit Service 516631
Packit Service 516631
   You should have received a copy of the GNU Lesser General Public
Packit Service 516631
   License along with the GNU C Library; if not, see
Packit Service 516631
   <http://www.gnu.org/licenses/>.  */
Packit Service 516631
Packit Service 516631
#include <dirent.h>
Packit Service 516631
#include <dlfcn.h>
Packit Service 516631
#include <errno.h>
Packit Service 516631
#include <shlib-compat.h>
Packit Service 516631
#include <stdbool.h>
Packit Service 516631
#include <stdio.h>
Packit Service 516631
#include <string.h>
Packit Service 516631
#include <support/check.h>
Packit Service 516631
Packit Service 516631
/* Copied from <olddirent.h>.  */
Packit Service 516631
struct __old_dirent64
Packit Service 516631
  {
Packit Service 516631
    __ino_t d_ino;
Packit Service 516631
    __off64_t d_off;
Packit Service 516631
    unsigned short int d_reclen;
Packit Service 516631
    unsigned char d_type;
Packit Service 516631
    char d_name[256];
Packit Service 516631
  };
Packit Service 516631
Packit Service 516631
typedef struct __old_dirent64 *(*compat_readdir64_type) (DIR *);
Packit Service 516631
Packit Service 516631
#if TEST_COMPAT (libc, GLIBC_2_1, GLIBC_2_2)
Packit Service 516631
struct __old_dirent64 *compat_readdir64 (DIR *);
Packit Service 516631
compat_symbol_reference (libc, compat_readdir64, readdir64, GLIBC_2_1);
Packit Service 516631
#endif
Packit Service 516631
Packit Service 516631
static int
Packit Service 516631
do_test (void)
Packit Service 516631
{
Packit Service 516631
#if TEST_COMPAT (libc, GLIBC_2_1, GLIBC_2_2)
Packit Service 516631
Packit Service 516631
  /* Directory stream using the non-compat readdir64 symbol.  The test
Packit Service 516631
     checks against this.  */
Packit Service 516631
  DIR *dir_reference = opendir (".");
Packit Service 516631
  TEST_VERIFY_EXIT (dir_reference != NULL);
Packit Service 516631
  DIR *dir_test = opendir (".");
Packit Service 516631
  TEST_VERIFY_EXIT (dir_test != NULL);
Packit Service 516631
Packit Service 516631
  /* This loop assumes that the enumeration order is consistent for
Packit Service 516631
     two different handles.  Nothing should write to the current
Packit Service 516631
     directory (in the source tree) while this test runs, so there
Packit Service 516631
     should not be any difference due to races.  */
Packit Service 516631
  size_t count = 0;
Packit Service 516631
  while (true)
Packit Service 516631
    {
Packit Service 516631
      errno = 0;
Packit Service 516631
      struct dirent64 *entry_reference = readdir64 (dir_reference);
Packit Service 516631
      if (entry_reference == NULL && errno != 0)
Packit Service 516631
        FAIL_EXIT1 ("readdir64 entry %zu: %m\n", count);
Packit Service 516631
      struct __old_dirent64 *entry_test = compat_readdir64 (dir_test);
Packit Service 516631
      if (entry_reference == NULL)
Packit Service 516631
        {
Packit Service 516631
          if (errno == EOVERFLOW)
Packit Service 516631
            {
Packit Service 516631
              TEST_VERIFY (entry_reference->d_ino
Packit Service 516631
                           != (__ino_t) entry_reference->d_ino);
Packit Service 516631
              printf ("info: inode number overflow at entry %zu\n", count);
Packit Service 516631
              break;
Packit Service 516631
            }
Packit Service 516631
          if (errno != 0)
Packit Service 516631
            FAIL_EXIT1 ("compat readdir64 entry %zu: %m\n", count);
Packit Service 516631
        }
Packit Service 516631
Packit Service 516631
      /* Check that both streams end at the same time.  */
Packit Service 516631
      if (entry_reference == NULL)
Packit Service 516631
        {
Packit Service 516631
          TEST_VERIFY (entry_test == NULL);
Packit Service 516631
          break;
Packit Service 516631
        }
Packit Service 516631
      else
Packit Service 516631
        TEST_VERIFY_EXIT (entry_test != NULL);
Packit Service 516631
Packit Service 516631
      /* Check that the entries are the same.  */
Packit Service 516631
      TEST_COMPARE_BLOB (entry_reference->d_name,
Packit Service 516631
                         strlen (entry_reference->d_name),
Packit Service 516631
                         entry_test->d_name, strlen (entry_test->d_name));
Packit Service 516631
      TEST_COMPARE (entry_reference->d_ino, entry_test->d_ino);
Packit Service 516631
      TEST_COMPARE (entry_reference->d_off, entry_test->d_off);
Packit Service 516631
      TEST_COMPARE (entry_reference->d_type, entry_test->d_type);
Packit Service 516631
      TEST_COMPARE (entry_reference->d_reclen, entry_test->d_reclen);
Packit Service 516631
Packit Service 516631
      ++count;
Packit Service 516631
    }
Packit Service 516631
  printf ("info: %zu directory entries found\n", count);
Packit Service 516631
  TEST_VERIFY (count >= 3);     /* ".", "..", and some source files.  */
Packit Service 516631
Packit Service 516631
  TEST_COMPARE (closedir (dir_test), 0);
Packit Service 516631
  TEST_COMPARE (closedir (dir_reference), 0);
Packit Service 516631
#endif
Packit Service 516631
  return 0;
Packit Service 516631
}
Packit Service 516631
Packit Service 516631
#include <support/test-driver.c>