1 | /* Read a directory. Linux no-LFS version. |
2 | Copyright (C) 2018-2023 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 | #include <dirent.h> |
20 | |
21 | #if !_DIRENT_MATCHES_DIRENT64 |
22 | #include <dirstream.h> |
23 | |
24 | /* Read a directory entry from DIRP. */ |
25 | struct dirent * |
26 | __readdir_unlocked (DIR *dirp) |
27 | { |
28 | struct dirent *dp; |
29 | int saved_errno = errno; |
30 | |
31 | if (dirp->offset >= dirp->size) |
32 | { |
33 | /* We've emptied out our buffer. Refill it. */ |
34 | |
35 | size_t maxread = dirp->allocation; |
36 | ssize_t bytes; |
37 | |
38 | bytes = __getdents (dirp->fd, dirp->data, maxread); |
39 | if (bytes <= 0) |
40 | { |
41 | /* Linux may fail with ENOENT on some file systems if the |
42 | directory inode is marked as dead (deleted). POSIX |
43 | treats this as a regular end-of-directory condition, so |
44 | do not set errno in that case, to indicate success. */ |
45 | if (bytes == 0 || errno == ENOENT) |
46 | __set_errno (saved_errno); |
47 | return NULL; |
48 | } |
49 | dirp->size = (size_t) bytes; |
50 | |
51 | /* Reset the offset into the buffer. */ |
52 | dirp->offset = 0; |
53 | } |
54 | |
55 | dp = (struct dirent *) &dirp->data[dirp->offset]; |
56 | dirp->offset += dp->d_reclen; |
57 | dirp->filepos = dp->d_off; |
58 | |
59 | return dp; |
60 | } |
61 | |
62 | struct dirent * |
63 | __readdir (DIR *dirp) |
64 | { |
65 | struct dirent *dp; |
66 | |
67 | #if IS_IN (libc) |
68 | __libc_lock_lock (dirp->lock); |
69 | #endif |
70 | dp = __readdir_unlocked (dirp); |
71 | #if IS_IN (libc) |
72 | __libc_lock_unlock (dirp->lock); |
73 | #endif |
74 | |
75 | return dp; |
76 | } |
77 | weak_alias (__readdir, readdir) |
78 | |
79 | #endif |
80 | |