]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 1991-2019 Free Software Foundation, Inc. |
47707456 | 2 | This file is part of the GNU C Library. |
a68b0d31 | 3 | |
47707456 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
a68b0d31 | 8 | |
47707456 UD |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
a68b0d31 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
a68b0d31 UD |
17 | |
18 | #include <errno.h> | |
19 | #include <limits.h> | |
20 | #include <stddef.h> | |
21 | #include <string.h> | |
22 | #include <dirent.h> | |
23 | #include <unistd.h> | |
24 | #include <sys/types.h> | |
25 | #include <assert.h> | |
26 | ||
27 | #include <dirstream.h> | |
28 | ||
56ddf355 UD |
29 | #ifndef __READDIR_R |
30 | # define __READDIR_R __readdir_r | |
31 | # define __GETDENTS __getdents | |
32 | # define DIRENT_TYPE struct dirent | |
33 | # define __READDIR_R_ALIAS | |
34 | #endif | |
a68b0d31 UD |
35 | |
36 | /* Read a directory entry from DIRP. */ | |
37 | int | |
56ddf355 | 38 | __READDIR_R (DIR *dirp, DIRENT_TYPE *entry, DIRENT_TYPE **result) |
a68b0d31 | 39 | { |
56ddf355 | 40 | DIRENT_TYPE *dp; |
0d8733c4 | 41 | size_t reclen; |
90341544 | 42 | const int saved_errno = errno; |
91ce4085 | 43 | int ret; |
a68b0d31 UD |
44 | |
45 | __libc_lock_lock (dirp->lock); | |
46 | ||
47 | do | |
48 | { | |
a68b0d31 UD |
49 | if (dirp->offset >= dirp->size) |
50 | { | |
51 | /* We've emptied out our buffer. Refill it. */ | |
52 | ||
53 | size_t maxread; | |
a68b0d31 UD |
54 | ssize_t bytes; |
55 | ||
56 | #ifndef _DIRENT_HAVE_D_RECLEN | |
57 | /* Fixed-size struct; must read one at a time (see below). */ | |
58 | maxread = sizeof *dp; | |
59 | #else | |
60 | maxread = dirp->allocation; | |
61 | #endif | |
62 | ||
56ddf355 | 63 | bytes = __GETDENTS (dirp->fd, dirp->data, maxread); |
a68b0d31 UD |
64 | if (bytes <= 0) |
65 | { | |
90341544 RM |
66 | /* On some systems getdents fails with ENOENT when the |
67 | open directory has been rmdir'd already. POSIX.1 | |
68 | requires that we treat this condition like normal EOF. */ | |
69 | if (bytes < 0 && errno == ENOENT) | |
70 | { | |
71 | bytes = 0; | |
72 | __set_errno (saved_errno); | |
73 | } | |
91ce4085 FW |
74 | if (bytes < 0) |
75 | dirp->errcode = errno; | |
90341544 | 76 | |
a68b0d31 UD |
77 | dp = NULL; |
78 | break; | |
79 | } | |
80 | dirp->size = (size_t) bytes; | |
81 | ||
82 | /* Reset the offset into the buffer. */ | |
83 | dirp->offset = 0; | |
84 | } | |
85 | ||
56ddf355 | 86 | dp = (DIRENT_TYPE *) &dirp->data[dirp->offset]; |
a68b0d31 UD |
87 | |
88 | #ifdef _DIRENT_HAVE_D_RECLEN | |
89 | reclen = dp->d_reclen; | |
90 | #else | |
56ddf355 | 91 | /* The only version of `struct dirent*' that lacks `d_reclen' |
a68b0d31 UD |
92 | is fixed-size. */ |
93 | assert (sizeof dp->d_name > 1); | |
94 | reclen = sizeof *dp; | |
95 | /* The name is not terminated if it is the largest possible size. | |
96 | Clobber the following byte to ensure proper null termination. We | |
97 | read just one entry at a time above so we know that byte will not | |
98 | be used later. */ | |
99 | dp->d_name[sizeof dp->d_name] = '\0'; | |
100 | #endif | |
101 | ||
102 | dirp->offset += reclen; | |
103 | ||
104 | #ifdef _DIRENT_HAVE_D_OFF | |
105 | dirp->filepos = dp->d_off; | |
106 | #else | |
107 | dirp->filepos += reclen; | |
108 | #endif | |
109 | ||
91ce4085 FW |
110 | #ifdef NAME_MAX |
111 | if (reclen > offsetof (DIRENT_TYPE, d_name) + NAME_MAX + 1) | |
112 | { | |
113 | /* The record is very long. It could still fit into the | |
114 | caller-supplied buffer if we can skip padding at the | |
115 | end. */ | |
116 | size_t namelen = _D_EXACT_NAMLEN (dp); | |
117 | if (namelen <= NAME_MAX) | |
118 | reclen = offsetof (DIRENT_TYPE, d_name) + namelen + 1; | |
119 | else | |
120 | { | |
121 | /* The name is too long. Ignore this file. */ | |
122 | dirp->errcode = ENAMETOOLONG; | |
123 | dp->d_ino = 0; | |
124 | continue; | |
125 | } | |
126 | } | |
127 | #endif | |
128 | ||
129 | /* Skip deleted and ignored files. */ | |
c063ba61 UD |
130 | } |
131 | while (dp->d_ino == 0); | |
a68b0d31 UD |
132 | |
133 | if (dp != NULL) | |
1a811397 | 134 | { |
1a811397 | 135 | *result = memcpy (entry, dp, reclen); |
91ce4085 | 136 | #ifdef _DIRENT_HAVE_D_RECLEN |
1a811397 UD |
137 | entry->d_reclen = reclen; |
138 | #endif | |
91ce4085 | 139 | ret = 0; |
1a811397 | 140 | } |
c063ba61 | 141 | else |
91ce4085 FW |
142 | { |
143 | *result = NULL; | |
144 | ret = dirp->errcode; | |
145 | } | |
a68b0d31 UD |
146 | |
147 | __libc_lock_unlock (dirp->lock); | |
148 | ||
91ce4085 | 149 | return ret; |
a68b0d31 | 150 | } |
56ddf355 UD |
151 | |
152 | #ifdef __READDIR_R_ALIAS | |
a68b0d31 | 153 | weak_alias (__readdir_r, readdir_r) |
56ddf355 | 154 | #endif |
942de61a AZ |
155 | |
156 | #undef __READDIR_R | |
157 | #undef __GETDENTS | |
158 | #undef DIRENT_TYPE | |
159 | #undef __READDIR_R_ALIAS |