|
Packit |
6c4009 |
/* Get directory entries. Linux LFS version.
|
|
Packit |
6c4009 |
Copyright (C) 1997-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library. If not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <dirent.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The kernel struct linux_dirent64 matches the 'struct getdents64' type. */
|
|
Packit |
6c4009 |
ssize_t
|
|
Packit |
6c4009 |
__getdents64 (int fd, char *buf, size_t nbytes)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#if _DIRENT_MATCHES_DIRENT64
|
|
Packit |
6c4009 |
strong_alias (__getdents64, __getdents)
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
# include <shlib-compat.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2)
|
|
Packit Service |
36dffd |
# include <olddirent.h>
|
|
Packit Service |
36dffd |
# include <unistd.h>
|
|
Packit |
6c4009 |
|
|
Packit Service |
36dffd |
static ssize_t
|
|
Packit Service |
36dffd |
handle_overflow (int fd, __off64_t offset, ssize_t count)
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
36dffd |
/* If this is the first entry in the buffer, we can report the
|
|
Packit Service |
36dffd |
error. */
|
|
Packit Service |
36dffd |
if (count == 0)
|
|
Packit Service |
36dffd |
{
|
|
Packit Service |
36dffd |
__set_errno (EOVERFLOW);
|
|
Packit Service |
36dffd |
return -1;
|
|
Packit Service |
36dffd |
}
|
|
Packit Service |
36dffd |
|
|
Packit Service |
36dffd |
/* Otherwise, seek to the overflowing entry, so that the next call
|
|
Packit Service |
36dffd |
will report the error, and return the data read so far.. */
|
|
Packit Service |
36dffd |
if (__lseek64 (fd, offset, SEEK_SET) != 0)
|
|
Packit Service |
36dffd |
return -1;
|
|
Packit Service |
36dffd |
return count;
|
|
Packit Service |
36dffd |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ssize_t
|
|
Packit |
6c4009 |
__old_getdents64 (int fd, char *buf, size_t nbytes)
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
36dffd |
/* We do not move the individual directory entries. This is only
|
|
Packit Service |
36dffd |
possible if the target type (struct __old_dirent64) is smaller
|
|
Packit Service |
36dffd |
than the source type. */
|
|
Packit Service |
36dffd |
_Static_assert (offsetof (struct __old_dirent64, d_name)
|
|
Packit Service |
36dffd |
<= offsetof (struct dirent64, d_name),
|
|
Packit Service |
36dffd |
"__old_dirent64 is larger than dirent64");
|
|
Packit Service |
36dffd |
_Static_assert (__alignof__ (struct __old_dirent64)
|
|
Packit Service |
36dffd |
<= __alignof__ (struct dirent64),
|
|
Packit Service |
36dffd |
"alignment of __old_dirent64 is larger than dirent64");
|
|
Packit |
6c4009 |
|
|
Packit Service |
36dffd |
ssize_t retval = INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes);
|
|
Packit Service |
36dffd |
if (retval > 0)
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
36dffd |
char *p = buf;
|
|
Packit Service |
36dffd |
char *end = buf + retval;
|
|
Packit Service |
36dffd |
while (p < end)
|
|
Packit Service |
8bbac7 |
{
|
|
Packit Service |
36dffd |
struct dirent64 *source = (struct dirent64 *) p;
|
|
Packit Service |
36dffd |
|
|
Packit Service |
36dffd |
/* Copy out the fixed-size data. */
|
|
Packit Service |
36dffd |
__ino_t ino = source->d_ino;
|
|
Packit Service |
36dffd |
__off64_t offset = source->d_off;
|
|
Packit Service |
36dffd |
unsigned int reclen = source->d_reclen;
|
|
Packit Service |
36dffd |
unsigned char type = source->d_type;
|
|
Packit Service |
36dffd |
|
|
Packit Service |
36dffd |
/* Check for ino_t overflow. */
|
|
Packit Service |
36dffd |
if (__glibc_unlikely (ino != source->d_ino))
|
|
Packit Service |
36dffd |
return handle_overflow (fd, offset, p - buf);
|
|
Packit Service |
36dffd |
|
|
Packit Service |
36dffd |
/* Convert to the target layout. Use a separate struct and
|
|
Packit Service |
36dffd |
memcpy to side-step aliasing issues. */
|
|
Packit Service |
36dffd |
struct __old_dirent64 result;
|
|
Packit Service |
36dffd |
result.d_ino = ino;
|
|
Packit Service |
36dffd |
result.d_off = offset;
|
|
Packit Service |
36dffd |
result.d_reclen = reclen;
|
|
Packit Service |
36dffd |
result.d_type = type;
|
|
Packit Service |
36dffd |
|
|
Packit Service |
36dffd |
/* Write the fixed-sized part of the result to the
|
|
Packit Service |
36dffd |
buffer. */
|
|
Packit Service |
36dffd |
size_t result_name_offset = offsetof (struct __old_dirent64, d_name);
|
|
Packit Service |
36dffd |
memcpy (p, &result, result_name_offset);
|
|
Packit Service |
36dffd |
|
|
Packit Service |
36dffd |
/* Adjust the position of the name if necessary. Copy
|
|
Packit Service |
36dffd |
everything until the end of the record, including the
|
|
Packit Service |
36dffd |
terminating NUL byte. */
|
|
Packit Service |
36dffd |
if (result_name_offset != offsetof (struct dirent64, d_name))
|
|
Packit Service |
36dffd |
memmove (p + result_name_offset, source->d_name,
|
|
Packit Service |
36dffd |
reclen - offsetof (struct dirent64, d_name));
|
|
Packit |
6c4009 |
|
|
Packit Service |
36dffd |
p += reclen;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return retval;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
# endif /* SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) */
|
|
Packit |
6c4009 |
#endif /* _DIRENT_MATCHES_DIRENT64 */
|