aboutsummaryrefslogtreecommitdiff
path: root/support/support_readdir.c
blob: c4ccc3d28578e79edacf4409ac34e7207eafc92b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* Type-generic wrapper for readdir functions.
   Copyright (C) 2024-2025 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <support/readdir.h>

#include <dlfcn.h>
#include <stddef.h>
#include <stdlib.h>
#include <support/check.h>
#include <support/support.h>
#include <support/xdirent.h>

/* Copied from <olddirent.h>.  */
struct __old_dirent64
  {
    __ino_t d_ino;
    __off64_t d_off;
    unsigned short int d_reclen;
    unsigned char d_type;
    char d_name[256];
  };

static struct __old_dirent64 *(*readdir64_compat) (DIR *);
static int (*readdir64_r_compat) (DIR *, struct __old_dirent64 *,
                                  struct __old_dirent64 **);

static void __attribute__ ((constructor))
init (void)
{
  /* These compat symbols exists on alpha, i386, m67k , powerpc, s390,
     sparc. at the same GLIBC_2.1 version. */
  readdir64_compat = dlvsym (RTLD_DEFAULT, "readdir64", "GLIBC_2.1");
  readdir64_r_compat = dlvsym (RTLD_DEFAULT, "readdir64_r", "GLIBC_2.1");
}

enum support_readdir_op
support_readdir_op_last (void)
{
  if (readdir64_r_compat != NULL)
    {
      TEST_VERIFY (readdir64_compat != NULL);
      return SUPPORT_READDIR64_R_COMPAT;
    }
  else
    return SUPPORT_READDIR64_R;
}

const char *
support_readdir_function (enum support_readdir_op op)
{
  switch (op)
    {
      case SUPPORT_READDIR:
        return "readdir";
      case SUPPORT_READDIR64:
        return "readdir64";
      case SUPPORT_READDIR_R:
        return "readdir_r";
      case SUPPORT_READDIR64_R:
        return "readdir64_r";
      case SUPPORT_READDIR64_COMPAT:
        return "readdir64@GBLIC_2.1";
      case SUPPORT_READDIR64_R_COMPAT:
        return "readdir64_r@GBLIC_2.1";
    }
  FAIL_EXIT1 ("invalid support_readdir_op constant: %d", op);
}

unsigned int
support_readdir_inode_width (enum support_readdir_op op)
{
  switch (op)
    {
      case SUPPORT_READDIR:
      case SUPPORT_READDIR_R:
        return sizeof ((struct dirent) { 0, }.d_ino) * 8;
      case SUPPORT_READDIR64:
      case SUPPORT_READDIR64_R:
        return sizeof ((struct dirent64) { 0, }.d_ino) * 8;
      case SUPPORT_READDIR64_COMPAT:
      case SUPPORT_READDIR64_R_COMPAT:
        return sizeof ((struct __old_dirent64) { 0, }.d_ino) * 8;
    }
  FAIL_EXIT1 ("invalid support_readdir_op constant: %d", op);
}

unsigned int
support_readdir_offset_width (enum support_readdir_op op)
{
#ifdef _DIRENT_HAVE_D_OFF
  switch (op)
    {
    case SUPPORT_READDIR:
    case SUPPORT_READDIR_R:
      return sizeof ((struct dirent) { 0, }.d_off) * 8;
    case SUPPORT_READDIR64:
    case SUPPORT_READDIR64_R:
      return sizeof ((struct dirent64) { 0, }.d_off) * 8;
    case SUPPORT_READDIR64_COMPAT:
    case SUPPORT_READDIR64_R_COMPAT:
      return sizeof ((struct __old_dirent64) { 0, }.d_off) * 8;
    }
#else
  switch (op)
    {
    case SUPPORT_READDIR:
    case SUPPORT_READDIR_R:
    case SUPPORT_READDIR64:
    case SUPPORT_READDIR64_R:
    case SUPPORT_READDIR64_COMPAT:
    case SUPPORT_READDIR64_R_COMPAT:
      return 0;
    }
#endif
  FAIL_EXIT1 ("invalid support_readdir_op constant: %d", op);
}

bool
support_readdir_r_variant (enum support_readdir_op op)
{
  switch (op)
    {
      case SUPPORT_READDIR:
      case SUPPORT_READDIR64:
      case SUPPORT_READDIR64_COMPAT:
        return false;
      case SUPPORT_READDIR_R:
      case SUPPORT_READDIR64_R:
      case SUPPORT_READDIR64_R_COMPAT:
        return true;
    }
  FAIL_EXIT1 ("invalid support_readdir_op constant: %d", op);
}

static bool
copy_dirent (struct support_dirent *dst, struct dirent *src)
{
  if (src == NULL)
    return false;
  dst->d_ino = src->d_ino;
#ifdef _DIRENT_HAVE_D_OFF
  dst->d_off = src->d_off;
#else
  dst->d_off = 0;
#endif
  dst->d_type = src->d_type;
  dst->d_name = xstrdup (src->d_name);
  return true;
}

static bool
copy_dirent64 (struct support_dirent *dst, struct dirent64 *src)
{
  if (src == NULL)
    return false;
  dst->d_ino = src->d_ino;
#ifdef _DIRENT_HAVE_D_OFF
  dst->d_off = src->d_off;
#else
  dst->d_off = 0;
#endif
  dst->d_type = src->d_type;
  dst->d_name = xstrdup (src->d_name);
  return true;
}

static bool
copy_old_dirent64 (struct support_dirent *dst, struct __old_dirent64 *src)
{
  if (src == NULL)
    return false;
  dst->d_ino = src->d_ino;
#ifdef _DIRENT_HAVE_D_OFF
  dst->d_off = src->d_off;
#else
  dst->d_off = 0;
#endif
  dst->d_type = src->d_type;
  dst->d_name = xstrdup (src->d_name);
  return true;
}

bool
support_readdir (DIR *stream, enum support_readdir_op op,
                 struct support_dirent *e)
{
  free (e->d_name);
  e->d_name = NULL;
  switch (op)
    {
    case SUPPORT_READDIR:
      return copy_dirent (e, xreaddir (stream));
    case SUPPORT_READDIR64:
      return copy_dirent64 (e, xreaddir64 (stream));

      /* The functions readdir_r, readdir64_r were deprecated in glibc 2.24.  */
      DIAG_PUSH_NEEDS_COMMENT;
      DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");

    case SUPPORT_READDIR_R:
      {
        struct dirent buf;
        if (!xreaddir_r (stream, &buf))
          return false;
        return copy_dirent (e, &buf);
      }
    case SUPPORT_READDIR64_R:
      {
        struct dirent64 buf;
        if (!xreaddir64_r (stream, &buf))
          return false;
        return copy_dirent64 (e, &buf);
      }

      DIAG_POP_NEEDS_COMMENT;

    case SUPPORT_READDIR64_COMPAT:
      if (readdir64_compat == NULL)
        FAIL_EXIT1 ("readdir64 compat function not implemented");
      return copy_old_dirent64 (e, readdir64_compat (stream));

    case SUPPORT_READDIR64_R_COMPAT:
      {
        if (readdir64_r_compat == NULL)
          FAIL_EXIT1 ("readdir64_r compat function not implemented");
        struct __old_dirent64 buf;
        struct __old_dirent64 *e1;
        int ret = readdir64_r_compat (stream, &buf, &e1);
        if (ret != 0)
          {
            errno = ret;
            FAIL ("readdir64_r@GLIBC_2.1: %m");
            return false;
          }
        if (e1 == NULL)
          return false;
        return copy_old_dirent64 (e, e1);
      }
    }
  FAIL_EXIT1 ("support_readdir: invalid op argument %d", (int) op);
}

void
support_readdir_expect_error (DIR *stream, enum support_readdir_op op,
                              int expected)
{
  switch (op)
    {
    case SUPPORT_READDIR:
      errno = 0;
      TEST_VERIFY (readdir (stream) == NULL);
      TEST_COMPARE (errno, expected);
      return;
    case SUPPORT_READDIR64:
      errno = 0;
      TEST_VERIFY (readdir64 (stream) == NULL);
      TEST_COMPARE (errno, expected);
      return;

      /* The functions readdir_r, readdir64_r were deprecated in glibc 2.24.  */
      DIAG_PUSH_NEEDS_COMMENT;
      DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");

    case SUPPORT_READDIR_R:
      {
        struct dirent buf;
        struct dirent *e;
        errno = readdir_r (stream, &buf, &e);
        TEST_COMPARE (errno, expected);;
      }
      return;
    case SUPPORT_READDIR64_R:
      {
        struct dirent64 buf;
        struct dirent64 *e;
        errno = readdir64_r (stream, &buf, &e);
        TEST_COMPARE (errno, expected);;
      }
      return;

      DIAG_POP_NEEDS_COMMENT;

    case SUPPORT_READDIR64_COMPAT:
      if (readdir64_compat == NULL)
        FAIL_EXIT1 ("readdir64_r compat function not implemented");
      errno = 0;
      TEST_VERIFY (readdir64_compat (stream) == NULL);
      TEST_COMPARE (errno, expected);
      return;
    case SUPPORT_READDIR64_R_COMPAT:
      {
        if (readdir64_r_compat == NULL)
          FAIL_EXIT1 ("readdir64_r compat function not implemented");
        struct __old_dirent64 buf;
        struct __old_dirent64 *e;
        errno = readdir64_r_compat (stream, &buf, &e);
        TEST_COMPARE (errno, expected);
      }
      return;
    }
  FAIL_EXIT1 ("support_readdir_expect_error: invalid op argument %d",
              (int) op);
}