|
Packit Service |
82fcde |
/* Get directory entries. Linux non-LFS version.
|
|
Packit Service |
82fcde |
Copyright (C) 1993-2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
This file is part of the GNU C Library.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License as published by the Free Software Foundation; either
|
|
Packit Service |
82fcde |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library. If not, see
|
|
Packit Service |
82fcde |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <dirent.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#if !_DIRENT_MATCHES_DIRENT64
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# include <unistd.h>
|
|
Packit Service |
82fcde |
# include <string.h>
|
|
Packit Service |
82fcde |
# include <errno.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# ifndef DIRENT_SET_DP_INO
|
|
Packit Service |
82fcde |
# define DIRENT_SET_DP_INO(dp, value) (dp)->d_ino = (value)
|
|
Packit Service |
82fcde |
# endif
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Pack the dirent64 struct down into 32-bit offset/inode fields, and
|
|
Packit Service |
82fcde |
ensure that no overflow occurs. */
|
|
Packit Service |
82fcde |
ssize_t
|
|
Packit Service |
82fcde |
__getdents (int fd, char *buf, size_t nbytes)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
union
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
/* For !_DIRENT_MATCHES_DIRENT64 kernel 'linux_dirent64' has the same
|
|
Packit Service |
82fcde |
layout of 'struct dirent64'. */
|
|
Packit Service |
82fcde |
struct dirent64 k;
|
|
Packit Service |
82fcde |
struct dirent u;
|
|
Packit Service |
82fcde |
char b[1];
|
|
Packit Service |
82fcde |
} *kbuf = (void *) buf, *outp, *inp;
|
|
Packit Service |
82fcde |
size_t kbytes = nbytes;
|
|
Packit Service |
82fcde |
off64_t last_offset = -1;
|
|
Packit Service |
82fcde |
ssize_t retval;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define size_diff (offsetof (struct dirent64, d_name) \
|
|
Packit Service |
82fcde |
- offsetof (struct dirent, d_name))
|
|
Packit Service |
82fcde |
char kbuftmp[sizeof (struct dirent) + size_diff];
|
|
Packit Service |
82fcde |
if (nbytes <= sizeof (struct dirent))
|
|
Packit Service |
82fcde |
kbuf = (void*) kbuftmp;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
retval = INLINE_SYSCALL_CALL (getdents64, fd, kbuf, kbytes);
|
|
Packit Service |
82fcde |
if (retval == -1)
|
|
Packit Service |
82fcde |
return -1;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* These two pointers might alias the same memory buffer.
|
|
Packit Service |
82fcde |
Standard C requires that we always use the same type for them,
|
|
Packit Service |
82fcde |
so we must use the union type. */
|
|
Packit Service |
82fcde |
inp = kbuf;
|
|
Packit Service |
82fcde |
outp = (void *) buf;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
while (&inp->b < &kbuf->b + retval)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
const size_t alignment = _Alignof (struct dirent);
|
|
Packit Service |
82fcde |
/* Since inp->k.d_reclen is already aligned for the kernel
|
|
Packit Service |
82fcde |
structure this may compute a value that is bigger
|
|
Packit Service |
82fcde |
than necessary. */
|
|
Packit Service |
82fcde |
size_t old_reclen = inp->k.d_reclen;
|
|
Packit Service |
82fcde |
size_t new_reclen = ((old_reclen - size_diff + alignment - 1)
|
|
Packit Service |
82fcde |
& ~(alignment - 1));
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Copy the data out of the old structure into temporary space.
|
|
Packit Service |
82fcde |
Then copy the name, which may overlap if BUF == KBUF. */
|
|
Packit Service |
82fcde |
const uint64_t d_ino = inp->k.d_ino;
|
|
Packit Service |
82fcde |
const int64_t d_off = inp->k.d_off;
|
|
Packit Service |
82fcde |
const uint8_t d_type = inp->k.d_type;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
memmove (outp->u.d_name, inp->k.d_name,
|
|
Packit Service |
82fcde |
old_reclen - offsetof (struct dirent64, d_name));
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Now we have copied the data from INP and access only OUTP. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
DIRENT_SET_DP_INO (&outp->u, d_ino);
|
|
Packit Service |
82fcde |
outp->u.d_off = d_off;
|
|
Packit Service |
82fcde |
if ((sizeof (outp->u.d_ino) != sizeof (inp->k.d_ino)
|
|
Packit Service |
82fcde |
&& outp->u.d_ino != d_ino)
|
|
Packit Service |
82fcde |
|| (sizeof (outp->u.d_off) != sizeof (inp->k.d_off)
|
|
Packit Service |
82fcde |
&& outp->u.d_off != d_off))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
/* Overflow. If there was at least one entry before this one,
|
|
Packit Service |
82fcde |
return them without error, otherwise signal overflow. */
|
|
Packit Service |
82fcde |
if (last_offset != -1)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
__lseek64 (fd, last_offset, SEEK_SET);
|
|
Packit Service |
82fcde |
return outp->b - buf;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
last_offset = d_off;
|
|
Packit Service |
82fcde |
outp->u.d_reclen = new_reclen;
|
|
Packit Service |
82fcde |
outp->u.d_type = d_type;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
inp = (void *) inp + old_reclen;
|
|
Packit Service |
82fcde |
outp = (void *) outp + new_reclen;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
return outp->b - buf;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# undef DIRENT_SET_DP_INO
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#endif /* _DIRENT_MATCHES_DIRENT64 */
|