aboutsummaryrefslogtreecommitdiff
path: root/support/readdir.h
blob: 32e3e4ed1a2e76dd2b4b5fe4e3f37b4d60ca973b (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
/* 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/>.  */

#ifndef SUPPORT_READDIR_H
#define SUPPORT_READDIR_H

#include <dirent.h>
#include <stdbool.h>
#include <stdint.h>

__BEGIN_DECLS

/* Definition independent of _FILE_OFFSET_BITS.  */
struct support_dirent
{
  uint64_t d_ino;
  uint64_t d_off;               /* 0 if d_off is not supported.  */
  uint32_t d_type;
  char *d_name;
};

/* Operation to be performed by support_readdir below.  */
enum support_readdir_op
  {
    SUPPORT_READDIR,
    SUPPORT_READDIR64,
    SUPPORT_READDIR_R,
    SUPPORT_READDIR64_R,
    SUPPORT_READDIR64_COMPAT,
    SUPPORT_READDIR64_R_COMPAT,
  };

/* Returns the last supported function.  May exclude
   SUPPORT_READDIR64_R_COMPAT if not implemented.  */
enum support_readdir_op support_readdir_op_last (void);

/* Returns the name of the function that corresponds to the OP constant.  */
const char *support_readdir_function (enum support_readdir_op op);

/* Returns the d_ino field width for OP, in bits.  */
unsigned int support_readdir_inode_width (enum support_readdir_op op);

/* Returns the d_off field width for OP, in bits.  Zero if not present.  */
unsigned int support_readdir_offset_width (enum support_readdir_op op);

/* Returns true if OP is an _r variant with name length restrictions.  */
bool support_readdir_r_variant (enum support_readdir_op op);

/* First, free E->d_name and set the field to NULL.  Then call the
   readdir variant as specified by OP.  If successfully, copy fields
   to E, make a copy of the entry name using strdup, and write its
   addres sto E->d_name.

   Return true if an entry was read, or false if the end of the
   directory stream was reached.  Terminates the process upon error.
   The caller is expected to free E->d_name if the function is not
   called again for this E.

   Note that this function assumes that E->d_name has been initialized
   to NULL or has been allocated by a previous call to this function.  */
bool support_readdir (DIR *stream, enum support_readdir_op op,
                      struct support_dirent *e) __nonnull ((1, 3));

/* Checks that the readdir operation OP fails with errno value EXPECTED.  */
void support_readdir_expect_error (DIR *stream, enum support_readdir_op op,
                                   int expected) __nonnull ((1));

__END_DECLS

#endif /* SUPPORT_READDIR_H */