LoongArch: Undef __NR_fstat and __NR_newfstatat.
[glibc.git] / sysdeps / unix / sysv / linux / readdir64.c
blobe6b8867b7a361a62d5564ad4aaecab15b932798b
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.
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 __no_readdir_decl
24 #define __readdir __no___readdir_decl
25 #include <dirent.h>
26 #undef __readdir
27 #undef readdir
29 /* Read a directory entry from DIRP. No locking. */
30 static struct dirent64 *
31 __readdir64_unlocked (DIR *dirp)
33 struct dirent64 *dp;
34 int saved_errno = errno;
36 if (dirp->offset >= dirp->size)
38 /* We've emptied out our buffer. Refill it. */
40 size_t maxread = dirp->allocation;
41 ssize_t bytes;
43 bytes = __getdents64 (dirp->fd, dirp->data, maxread);
44 if (bytes <= 0)
46 /* Linux may fail with ENOENT on some file systems if the
47 directory inode is marked as dead (deleted). POSIX
48 treats this as a regular end-of-directory condition, so
49 do not set errno in that case, to indicate success. */
50 if (bytes == 0 || errno == ENOENT)
51 __set_errno (saved_errno);
52 return NULL;
54 dirp->size = (size_t) bytes;
56 /* Reset the offset into the buffer. */
57 dirp->offset = 0;
60 dp = (struct dirent64 *) &dirp->data[dirp->offset];
61 dirp->offset += dp->d_reclen;
62 dirp->filepos = dp->d_off;
64 return dp;
67 /* Read a directory entry from DIRP. */
68 struct dirent64 *
69 __readdir64 (DIR *dirp)
71 __libc_lock_lock (dirp->lock);
72 struct dirent64 *dp = __readdir64_unlocked (dirp);
73 __libc_lock_unlock (dirp->lock);
74 return dp;
76 libc_hidden_def (__readdir64)
78 #if _DIRENT_MATCHES_DIRENT64
79 strong_alias (__readdir64, __readdir)
80 weak_alias (__readdir64, readdir64)
81 weak_alias (__readdir64, readdir)
82 #else
83 /* The compat code expects the 'struct direct' with d_ino being a __ino_t
84 instead of __ino64_t. */
85 # include <shlib-compat.h>
86 # if IS_IN(rtld)
87 weak_alias (__readdir64, readdir64)
88 # else
89 versioned_symbol (libc, __readdir64, readdir64, GLIBC_2_2);
90 # endif
91 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2)
92 # include <olddirent.h>
94 attribute_compat_text_section
95 struct __old_dirent64 *
96 __old_readdir64 (DIR *dirp)
98 struct __old_dirent64 *dp;
99 int saved_errno = errno;
101 __libc_lock_lock (dirp->lock);
103 while (1)
105 errno = 0;
106 struct dirent64 *newdp = __readdir64_unlocked (dirp);
107 if (newdp == NULL)
109 if (errno == 0 && dirp->errcode != 0)
110 __set_errno (dirp->errcode);
111 else if (errno == 0)
112 __set_errno (saved_errno);
113 dp = NULL;
114 break;
117 /* Convert to the target layout. Use a separate struct and
118 memcpy to side-step aliasing issues. */
119 struct __old_dirent64 result;
120 result.d_ino = newdp->d_ino;
121 result.d_off = newdp->d_off;
122 result.d_reclen = newdp->d_reclen;
123 result.d_type = newdp->d_type;
125 /* Check for ino_t overflow. */
126 if (__glibc_unlikely (result.d_ino != newdp->d_ino))
128 dirp->errcode = ENAMETOOLONG;
129 continue;
132 /* Overwrite the fixed-sized part. */
133 dp = (struct __old_dirent64 *) newdp;
134 memcpy (dp, &result, offsetof (struct __old_dirent64, d_name));
136 /* Move the name. */
137 _Static_assert (offsetof (struct __old_dirent64, d_name)
138 <= offsetof (struct dirent64, d_name),
139 "old struct must be smaller");
140 if (offsetof (struct __old_dirent64, d_name)
141 != offsetof (struct dirent64, d_name))
142 memmove (dp->d_name, newdp->d_name, strlen (newdp->d_name) + 1);
144 __set_errno (saved_errno);
145 break;
148 __libc_lock_unlock (dirp->lock);
149 return dp;
151 libc_hidden_def (__old_readdir64)
152 compat_symbol (libc, __old_readdir64, readdir64, GLIBC_2_1);
153 # endif /* SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) */
154 #endif /* _DIRENT_MATCHES_DIRENT64 */