1 /* Test readdir64 compatibility symbol.
2    Copyright (C) 2018-2021 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 struct __old_dirent64 *compat_readdir64 (DIR *);
41 compat_symbol_reference (libc, compat_readdir64, readdir64, GLIBC_2_1);
42 
43 static int
do_test(void)44 do_test (void)
45 {
46   /* Directory stream using the non-compat readdir64 symbol.  The test
47      checks against this.  */
48   DIR *dir_reference = opendir (".");
49   TEST_VERIFY_EXIT (dir_reference != NULL);
50   DIR *dir_test = opendir (".");
51   TEST_VERIFY_EXIT (dir_test != NULL);
52 
53   /* This loop assumes that the enumeration order is consistent for
54      two different handles.  Nothing should write to the current
55      directory (in the source tree) while this test runs, so there
56      should not be any difference due to races.  */
57   size_t count = 0;
58   while (true)
59     {
60       errno = 0;
61       struct dirent64 *entry_reference = readdir64 (dir_reference);
62       if (entry_reference == NULL && errno != 0)
63         FAIL_EXIT1 ("readdir64 entry %zu: %m\n", count);
64       struct __old_dirent64 *entry_test = compat_readdir64 (dir_test);
65       if (entry_reference == NULL)
66         {
67           if (errno == EOVERFLOW)
68             {
69               TEST_VERIFY (entry_reference->d_ino
70                            != (__ino_t) entry_reference->d_ino);
71               printf ("info: inode number overflow at entry %zu\n", count);
72               break;
73             }
74           if (errno != 0)
75             FAIL_EXIT1 ("compat readdir64 entry %zu: %m\n", count);
76         }
77 
78       /* Check that both streams end at the same time.  */
79       if (entry_reference == NULL)
80         {
81           TEST_VERIFY (entry_test == NULL);
82           break;
83         }
84       else
85         TEST_VERIFY_EXIT (entry_test != NULL);
86 
87       /* d_off is never zero because it is the offset of the next
88          entry (not the current entry).  */
89       TEST_VERIFY (entry_reference->d_off > 0);
90 
91       /* Check that the entries are the same.  */
92       TEST_COMPARE_BLOB (entry_reference->d_name,
93                          strlen (entry_reference->d_name),
94                          entry_test->d_name, strlen (entry_test->d_name));
95       TEST_COMPARE (entry_reference->d_ino, entry_test->d_ino);
96       TEST_COMPARE (entry_reference->d_off, entry_test->d_off);
97       TEST_COMPARE (entry_reference->d_type, entry_test->d_type);
98       TEST_COMPARE (entry_reference->d_reclen, entry_test->d_reclen);
99 
100       ++count;
101     }
102   printf ("info: %zu directory entries found\n", count);
103   TEST_VERIFY (count >= 3);     /* ".", "..", and some source files.  */
104 
105   TEST_COMPARE (closedir (dir_test), 0);
106   TEST_COMPARE (closedir (dir_reference), 0);
107   return 0;
108 }
109 
110 #include <support/test-driver.c>
111