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