1/* Read a directory. Linux LFS version.
2 Copyright (C) 1997-2024 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/* 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 __no_readdir_decl
24#define __readdir __no___readdir_decl
25#include <dirent.h>
26#undef __readdir
27#undef readdir
28
29/* Read a directory entry from DIRP. */
30struct dirent64 *
31__readdir64 (DIR *dirp)
32{
33 struct dirent64 *dp;
34 int saved_errno = errno;
35
36#if IS_IN (libc)
37 __libc_lock_lock (dirp->lock);
38#endif
39
40 if (dirp->offset >= dirp->size)
41 {
42 /* We've emptied out our buffer. Refill it. */
43
44 size_t maxread = dirp->allocation;
45 ssize_t bytes;
46
47 bytes = __getdents64 (dirp->fd, dirp->data, maxread);
48 if (bytes <= 0)
49 {
50 /* Linux may fail with ENOENT on some file systems if the
51 directory inode is marked as dead (deleted). POSIX
52 treats this as a regular end-of-directory condition, so
53 do not set errno in that case, to indicate success. */
54 if (bytes == 0 || errno == ENOENT)
55 __set_errno (saved_errno);
56#if IS_IN (libc)
57 __libc_lock_unlock (dirp->lock);
58#endif
59 return NULL;
60 }
61 dirp->size = (size_t) bytes;
62
63 /* Reset the offset into the buffer. */
64 dirp->offset = 0;
65 }
66
67 dp = (struct dirent64 *) &dirp->data[dirp->offset];
68 dirp->offset += dp->d_reclen;
69 dirp->filepos = dp->d_off;
70
71#if IS_IN (libc)
72 __libc_lock_unlock (dirp->lock);
73#endif
74
75 return dp;
76}
77libc_hidden_def (__readdir64)
78
79#if _DIRENT_MATCHES_DIRENT64
80strong_alias (__readdir64, __readdir)
81weak_alias (__readdir64, readdir64)
82weak_alias (__readdir64, readdir)
83#else
84/* The compat code expects the 'struct direct' with d_ino being a __ino_t
85 instead of __ino64_t. */
86# include <shlib-compat.h>
87# if IS_IN(rtld)
88weak_alias (__readdir64, readdir64)
89# else
90versioned_symbol (libc, __readdir64, readdir64, GLIBC_2_2);
91# endif
92# if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2)
93# include <olddirent.h>
94
95attribute_compat_text_section
96struct __old_dirent64 *
97__old_readdir64 (DIR *dirp)
98{
99 struct __old_dirent64 *dp;
100 int saved_errno = errno;
101
102#if IS_IN (libc)
103 __libc_lock_lock (dirp->lock);
104#endif
105
106 if (dirp->offset >= dirp->size)
107 {
108 /* We've emptied out our buffer. Refill it. */
109
110 size_t maxread = dirp->allocation;
111 ssize_t bytes;
112
113 bytes = __old_getdents64 (dirp->fd, dirp->data, maxread);
114 if (bytes <= 0)
115 {
116 /* Linux may fail with ENOENT on some file systems if the
117 directory inode is marked as dead (deleted). POSIX
118 treats this as a regular end-of-directory condition, so
119 do not set errno in that case, to indicate success. */
120 if (bytes == 0 || errno == ENOENT)
121 __set_errno (saved_errno);
122#if IS_IN (libc)
123 __libc_lock_unlock (dirp->lock);
124#endif
125 return NULL;
126 }
127 dirp->size = (size_t) bytes;
128
129 /* Reset the offset into the buffer. */
130 dirp->offset = 0;
131 }
132
133 dp = (struct __old_dirent64 *) &dirp->data[dirp->offset];
134 dirp->offset += dp->d_reclen;
135 dirp->filepos = dp->d_off;
136
137#if IS_IN (libc)
138 __libc_lock_unlock (dirp->lock);
139#endif
140
141 return dp;
142}
143libc_hidden_def (__old_readdir64)
144compat_symbol (libc, __old_readdir64, readdir64, GLIBC_2_1);
145# endif /* SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) */
146#endif /* _DIRENT_MATCHES_DIRENT64 */
147

source code of glibc/sysdeps/unix/sysv/linux/readdir64.c