1 /* Read a directory in reentrant mode. Linux LFS version.
2 Copyright (C) 1997-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 /* When _DIRENT_MATCHES_DIRENT64 is defined we can alias 'readdir64' to
20 'readdir'. However the function signatures are not equal due
21 different return types, so we need to suppress {__}readdir so weak
22 and strong alias do not throw conflicting types errors. */
23 #define readdir_r __no_readdir_r_decl
24 #define __readdir_r __no___readdir_r_decl
29 /* Read a directory entry from DIRP. */
31 __readdir64_r (DIR *dirp
, struct dirent64
*entry
, struct dirent64
**result
)
35 const int saved_errno
= errno
;
38 __libc_lock_lock (dirp
->lock
);
42 if (dirp
->offset
>= dirp
->size
)
44 /* We've emptied out our buffer. Refill it. */
46 size_t maxread
= dirp
->allocation
;
49 maxread
= dirp
->allocation
;
51 bytes
= __getdents64 (dirp
->fd
, dirp
->data
, maxread
);
54 /* On some systems getdents fails with ENOENT when the
55 open directory has been rmdir'd already. POSIX.1
56 requires that we treat this condition like normal EOF. */
57 if (bytes
< 0 && errno
== ENOENT
)
60 __set_errno (saved_errno
);
63 dirp
->errcode
= errno
;
68 dirp
->size
= (size_t) bytes
;
70 /* Reset the offset into the buffer. */
74 dp
= (struct dirent64
*) &dirp
->data
[dirp
->offset
];
76 reclen
= dp
->d_reclen
;
78 dirp
->offset
+= reclen
;
80 dirp
->filepos
= dp
->d_off
;
82 if (reclen
> offsetof (struct dirent64
, d_name
) + NAME_MAX
+ 1)
84 /* The record is very long. It could still fit into the
85 caller-supplied buffer if we can skip padding at the
87 size_t namelen
= _D_EXACT_NAMLEN (dp
);
88 if (namelen
<= NAME_MAX
)
89 reclen
= offsetof (struct dirent64
, d_name
) + namelen
+ 1;
92 /* The name is too long. Ignore this file. */
93 dirp
->errcode
= ENAMETOOLONG
;
99 /* Skip deleted and ignored files. */
101 while (dp
->d_ino
== 0);
105 *result
= memcpy (entry
, dp
, reclen
);
106 entry
->d_reclen
= reclen
;
115 __libc_lock_unlock (dirp
->lock
);
121 #if _DIRENT_MATCHES_DIRENT64
122 strong_alias (__readdir64_r
, __readdir_r
)
123 weak_alias (__readdir64_r
, readdir_r
)
124 weak_alias (__readdir64_r
, readdir64_r
)
126 /* The compat code expects the 'struct direct' with d_ino being a __ino_t
127 instead of __ino64_t. */
128 # include <shlib-compat.h>
129 versioned_symbol (libc
, __readdir64_r
, readdir64_r
, GLIBC_2_2
);
130 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2)
131 # include <olddirent.h>
134 attribute_compat_text_section
135 __old_readdir64_r (DIR *dirp
, struct __old_dirent64
*entry
,
136 struct __old_dirent64
**result
)
138 struct __old_dirent64
*dp
;
140 const int saved_errno
= errno
;
143 __libc_lock_lock (dirp
->lock
);
147 if (dirp
->offset
>= dirp
->size
)
149 /* We've emptied out our buffer. Refill it. */
151 size_t maxread
= dirp
->allocation
;
154 maxread
= dirp
->allocation
;
156 bytes
= __old_getdents64 (dirp
->fd
, dirp
->data
, maxread
);
159 /* On some systems getdents fails with ENOENT when the
160 open directory has been rmdir'd already. POSIX.1
161 requires that we treat this condition like normal EOF. */
162 if (bytes
< 0 && errno
== ENOENT
)
165 __set_errno (saved_errno
);
168 dirp
->errcode
= errno
;
173 dirp
->size
= (size_t) bytes
;
175 /* Reset the offset into the buffer. */
179 dp
= (struct __old_dirent64
*) &dirp
->data
[dirp
->offset
];
181 reclen
= dp
->d_reclen
;
183 dirp
->offset
+= reclen
;
185 dirp
->filepos
= dp
->d_off
;
187 if (reclen
> offsetof (struct __old_dirent64
, d_name
) + NAME_MAX
+ 1)
189 /* The record is very long. It could still fit into the
190 caller-supplied buffer if we can skip padding at the
192 size_t namelen
= _D_EXACT_NAMLEN (dp
);
193 if (namelen
<= NAME_MAX
)
194 reclen
= offsetof (struct __old_dirent64
, d_name
) + namelen
+ 1;
197 /* The name is too long. Ignore this file. */
198 dirp
->errcode
= ENAMETOOLONG
;
204 /* Skip deleted and ignored files. */
206 while (dp
->d_ino
== 0);
210 *result
= memcpy (entry
, dp
, reclen
);
211 entry
->d_reclen
= reclen
;
220 __libc_lock_unlock (dirp
->lock
);
225 compat_symbol (libc
, __old_readdir64_r
, readdir64_r
, GLIBC_2_1
);
226 # endif /* SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) */
227 #endif /* _DIRENT_MATCHES_DIRENT64 */