]>
Commit | Line | Data |
---|---|---|
942de61a | 1 | /* Read a directory. Linux LFS version. |
2b778ceb | 2 | Copyright (C) 1997-2021 Free Software Foundation, Inc. |
942de61a AZ |
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 | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
942de61a AZ |
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> | |
905ae44c AZ |
26 | #undef __readdir |
27 | #undef readdir | |
942de61a | 28 | |
905ae44c AZ |
29 | /* Read a directory entry from DIRP. */ |
30 | struct dirent64 * | |
31 | __readdir64 (DIR *dirp) | |
32 | { | |
33 | struct dirent64 *dp; | |
34 | int saved_errno = errno; | |
9756dfe1 | 35 | |
905ae44c AZ |
36 | #if IS_IN (libc) |
37 | __libc_lock_lock (dirp->lock); | |
38 | #endif | |
9756dfe1 | 39 | |
905ae44c AZ |
40 | do |
41 | { | |
42 | size_t reclen; | |
43 | ||
44 | if (dirp->offset >= dirp->size) | |
45 | { | |
46 | /* We've emptied out our buffer. Refill it. */ | |
47 | ||
48 | size_t maxread = dirp->allocation; | |
49 | ssize_t bytes; | |
50 | ||
51 | bytes = __getdents64 (dirp->fd, dirp->data, maxread); | |
52 | if (bytes <= 0) | |
53 | { | |
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) | |
58 | bytes = 0; | |
59 | ||
60 | /* Don't modifiy errno when reaching EOF. */ | |
61 | if (bytes == 0) | |
62 | __set_errno (saved_errno); | |
63 | dp = NULL; | |
64 | break; | |
65 | } | |
66 | dirp->size = (size_t) bytes; | |
942de61a | 67 | |
905ae44c AZ |
68 | /* Reset the offset into the buffer. */ |
69 | dirp->offset = 0; | |
70 | } | |
71 | ||
72 | dp = (struct dirent64 *) &dirp->data[dirp->offset]; | |
73 | ||
74 | reclen = dp->d_reclen; | |
75 | ||
76 | dirp->offset += reclen; | |
77 | ||
78 | dirp->filepos = dp->d_off; | |
79 | ||
80 | /* Skip deleted files. */ | |
81 | } while (dp->d_ino == 0); | |
82 | ||
83 | #if IS_IN (libc) | |
84 | __libc_lock_unlock (dirp->lock); | |
85 | #endif | |
86 | ||
87 | return dp; | |
88 | } | |
4587a421 | 89 | libc_hidden_def (__readdir64) |
905ae44c | 90 | |
942de61a AZ |
91 | #if _DIRENT_MATCHES_DIRENT64 |
92 | strong_alias (__readdir64, __readdir) | |
1ab18a5b | 93 | weak_alias (__readdir64, readdir64) |
942de61a AZ |
94 | weak_alias (__readdir64, readdir) |
95 | #else | |
96 | /* The compat code expects the 'struct direct' with d_ino being a __ino_t | |
97 | instead of __ino64_t. */ | |
98 | # include <shlib-compat.h> | |
80420092 AZ |
99 | # if IS_IN(rtld) |
100 | weak_alias (__readdir64, readdir64) | |
101 | # else | |
942de61a | 102 | versioned_symbol (libc, __readdir64, readdir64, GLIBC_2_2); |
80420092 | 103 | # endif |
942de61a AZ |
104 | # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) |
105 | # include <olddirent.h> | |
905ae44c AZ |
106 | |
107 | attribute_compat_text_section | |
108 | struct __old_dirent64 * | |
109 | __old_readdir64 (DIR *dirp) | |
110 | { | |
111 | struct __old_dirent64 *dp; | |
112 | int saved_errno = errno; | |
113 | ||
114 | #if IS_IN (libc) | |
115 | __libc_lock_lock (dirp->lock); | |
116 | #endif | |
117 | ||
118 | do | |
119 | { | |
120 | size_t reclen; | |
121 | ||
122 | if (dirp->offset >= dirp->size) | |
123 | { | |
124 | /* We've emptied out our buffer. Refill it. */ | |
125 | ||
126 | size_t maxread = dirp->allocation; | |
127 | ssize_t bytes; | |
128 | ||
129 | bytes = __old_getdents64 (dirp->fd, dirp->data, maxread); | |
130 | if (bytes <= 0) | |
131 | { | |
132 | /* On some systems getdents fails with ENOENT when the | |
133 | open directory has been rmdir'd already. POSIX.1 | |
134 | requires that we treat this condition like normal EOF. */ | |
135 | if (bytes < 0 && errno == ENOENT) | |
136 | bytes = 0; | |
137 | ||
138 | /* Don't modifiy errno when reaching EOF. */ | |
139 | if (bytes == 0) | |
140 | __set_errno (saved_errno); | |
141 | dp = NULL; | |
142 | break; | |
143 | } | |
144 | dirp->size = (size_t) bytes; | |
145 | ||
146 | /* Reset the offset into the buffer. */ | |
147 | dirp->offset = 0; | |
148 | } | |
149 | ||
150 | dp = (struct __old_dirent64 *) &dirp->data[dirp->offset]; | |
151 | ||
152 | reclen = dp->d_reclen; | |
153 | ||
154 | dirp->offset += reclen; | |
155 | ||
156 | dirp->filepos = dp->d_off; | |
157 | ||
158 | /* Skip deleted files. */ | |
159 | } while (dp->d_ino == 0); | |
160 | ||
161 | #if IS_IN (libc) | |
162 | __libc_lock_unlock (dirp->lock); | |
163 | #endif | |
164 | ||
165 | return dp; | |
166 | } | |
942de61a AZ |
167 | libc_hidden_def (__old_readdir64) |
168 | compat_symbol (libc, __old_readdir64, readdir64, GLIBC_2_1); | |
169 | # endif /* SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) */ | |
170 | #endif /* _DIRENT_MATCHES_DIRENT64 */ |