1 | /* Get directory entries. Linux LFS version. |
2 | Copyright (C) 1997-2021 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 <string.h> |
20 | #include <dirent.h> |
21 | #include <errno.h> |
22 | #include <limits.h> |
23 | |
24 | /* The kernel struct linux_dirent64 matches the 'struct dirent64' type. */ |
25 | ssize_t |
26 | __getdents64 (int fd, void *buf, size_t nbytes) |
27 | { |
28 | /* The system call takes an unsigned int argument, and some length |
29 | checks in the kernel use an int type. */ |
30 | if (nbytes > INT_MAX) |
31 | nbytes = INT_MAX; |
32 | return INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes); |
33 | } |
34 | libc_hidden_def (__getdents64) |
35 | weak_alias (__getdents64, getdents64) |
36 | |
37 | #if _DIRENT_MATCHES_DIRENT64 |
38 | strong_alias (__getdents64, __getdents) |
39 | #else |
40 | # include <shlib-compat.h> |
41 | |
42 | # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) |
43 | # include <olddirent.h> |
44 | # include <unistd.h> |
45 | |
46 | static ssize_t |
47 | handle_overflow (int fd, __off64_t offset, ssize_t count) |
48 | { |
49 | /* If this is the first entry in the buffer, we can report the |
50 | error. */ |
51 | if (offset == 0) |
52 | { |
53 | __set_errno (EOVERFLOW); |
54 | return -1; |
55 | } |
56 | |
57 | /* Otherwise, seek to the overflowing entry, so that the next call |
58 | will report the error, and return the data read so far. */ |
59 | if (__lseek64 (fd, offset, SEEK_SET) != 0) |
60 | return -1; |
61 | return count; |
62 | } |
63 | |
64 | ssize_t |
65 | __old_getdents64 (int fd, char *buf, size_t nbytes) |
66 | { |
67 | /* We do not move the individual directory entries. This is only |
68 | possible if the target type (struct __old_dirent64) is smaller |
69 | than the source type. */ |
70 | _Static_assert (offsetof (struct __old_dirent64, d_name) |
71 | <= offsetof (struct dirent64, d_name), |
72 | "__old_dirent64 is larger than dirent64" ); |
73 | _Static_assert (__alignof__ (struct __old_dirent64) |
74 | <= __alignof__ (struct dirent64), |
75 | "alignment of __old_dirent64 is larger than dirent64" ); |
76 | |
77 | ssize_t retval = INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes); |
78 | if (retval > 0) |
79 | { |
80 | /* This is the marker for the first entry. Offset 0 is reserved |
81 | for the first entry (see rewinddir). Here, we use it as a |
82 | marker for the first entry in the buffer. We never actually |
83 | seek to offset 0 because handle_overflow reports the error |
84 | directly, so it does not matter that the offset is incorrect |
85 | if entries have been read from the descriptor before (so that |
86 | the descriptor is not actually at offset 0). */ |
87 | __off64_t previous_offset = 0; |
88 | |
89 | char *p = buf; |
90 | char *end = buf + retval; |
91 | while (p < end) |
92 | { |
93 | struct dirent64 *source = (struct dirent64 *) p; |
94 | |
95 | /* Copy out the fixed-size data. */ |
96 | __ino_t ino = source->d_ino; |
97 | __off64_t offset = source->d_off; |
98 | unsigned int reclen = source->d_reclen; |
99 | unsigned char type = source->d_type; |
100 | |
101 | /* Check for ino_t overflow. */ |
102 | if (__glibc_unlikely (ino != source->d_ino)) |
103 | return handle_overflow (fd, previous_offset, p - buf); |
104 | |
105 | /* Convert to the target layout. Use a separate struct and |
106 | memcpy to side-step aliasing issues. */ |
107 | struct __old_dirent64 result; |
108 | result.d_ino = ino; |
109 | result.d_off = offset; |
110 | result.d_reclen = reclen; |
111 | result.d_type = type; |
112 | |
113 | /* Write the fixed-sized part of the result to the |
114 | buffer. */ |
115 | size_t result_name_offset = offsetof (struct __old_dirent64, d_name); |
116 | memcpy (p, &result, result_name_offset); |
117 | |
118 | /* Adjust the position of the name if necessary. Copy |
119 | everything until the end of the record, including the |
120 | terminating NUL byte. */ |
121 | if (result_name_offset != offsetof (struct dirent64, d_name)) |
122 | memmove (p + result_name_offset, source->d_name, |
123 | reclen - offsetof (struct dirent64, d_name)); |
124 | |
125 | p += reclen; |
126 | previous_offset = offset; |
127 | } |
128 | } |
129 | return retval; |
130 | } |
131 | # endif /* SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) */ |
132 | #endif /* _DIRENT_MATCHES_DIRENT64 */ |
133 | |