Blame sysdeps/unix/sysv/linux/getdents.c

Packit 6c4009
/* Get directory entries.  Linux non-LFS version.
Packit 6c4009
   Copyright (C) 1993-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 <dirent.h>
Packit 6c4009
Packit 6c4009
#if !_DIRENT_MATCHES_DIRENT64
Packit 6c4009
Packit 6c4009
# include <unistd.h>
Packit 6c4009
# include <string.h>
Packit 6c4009
# include <errno.h>
Packit 6c4009
Packit 6c4009
# ifndef DIRENT_SET_DP_INO
Packit 6c4009
#  define DIRENT_SET_DP_INO(dp, value) (dp)->d_ino = (value)
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
/* Pack the dirent64 struct down into 32-bit offset/inode fields, and
Packit 6c4009
   ensure that no overflow occurs.  */
Packit 6c4009
ssize_t
Packit 6c4009
__getdents (int fd, char *buf, size_t nbytes)
Packit 6c4009
{
Packit 6c4009
  union
Packit 6c4009
  {
Packit 6c4009
    /* For !_DIRENT_MATCHES_DIRENT64 kernel 'linux_dirent64' has the same
Packit 6c4009
       layout of 'struct dirent64'.  */
Packit 6c4009
    struct dirent64 k;
Packit 6c4009
    struct dirent u;
Packit 6c4009
    char b[1];
Packit 6c4009
  } *kbuf = (void *) buf, *outp, *inp;
Packit 6c4009
  size_t kbytes = nbytes;
Packit 6c4009
  off64_t last_offset = -1;
Packit 6c4009
  ssize_t retval;
Packit 6c4009
Packit 6c4009
# define size_diff (offsetof (struct dirent64, d_name) \
Packit 6c4009
		    - offsetof (struct dirent, d_name))
Packit 6c4009
  char kbuftmp[sizeof (struct dirent) + size_diff];
Packit 6c4009
  if (nbytes <= sizeof (struct dirent))
Packit 6c4009
    kbuf = (void*) kbuftmp;
Packit 6c4009
Packit 6c4009
  retval = INLINE_SYSCALL_CALL (getdents64, fd, kbuf, kbytes);
Packit 6c4009
  if (retval == -1)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* These two pointers might alias the same memory buffer.
Packit 6c4009
     Standard C requires that we always use the same type for them,
Packit 6c4009
     so we must use the union type.  */
Packit 6c4009
  inp = kbuf;
Packit 6c4009
  outp = (void *) buf;
Packit 6c4009
Packit 6c4009
  while (&inp->b < &kbuf->b + retval)
Packit 6c4009
    {
Packit 6c4009
      const size_t alignment = _Alignof (struct dirent);
Packit 6c4009
      /* Since inp->k.d_reclen is already aligned for the kernel
Packit 6c4009
         structure this may compute a value that is bigger
Packit 6c4009
         than necessary.  */
Packit 6c4009
      size_t old_reclen = inp->k.d_reclen;
Packit 6c4009
      size_t new_reclen = ((old_reclen - size_diff + alignment - 1)
Packit 6c4009
                           & ~(alignment - 1));
Packit 6c4009
Packit 6c4009
      /* Copy the data out of the old structure into temporary space.
Packit 6c4009
         Then copy the name, which may overlap if BUF == KBUF.  */
Packit 6c4009
      const uint64_t d_ino = inp->k.d_ino;
Packit 6c4009
      const int64_t d_off = inp->k.d_off;
Packit 6c4009
      const uint8_t d_type = inp->k.d_type;
Packit 6c4009
Packit 6c4009
      memmove (outp->u.d_name, inp->k.d_name,
Packit 6c4009
               old_reclen - offsetof (struct dirent64, d_name));
Packit 6c4009
Packit 6c4009
      /* Now we have copied the data from INP and access only OUTP.  */
Packit 6c4009
Packit 6c4009
      DIRENT_SET_DP_INO (&outp->u, d_ino);
Packit 6c4009
      outp->u.d_off = d_off;
Packit 6c4009
      if ((sizeof (outp->u.d_ino) != sizeof (inp->k.d_ino)
Packit 6c4009
           && outp->u.d_ino != d_ino)
Packit 6c4009
          || (sizeof (outp->u.d_off) != sizeof (inp->k.d_off)
Packit 6c4009
              && outp->u.d_off != d_off))
Packit 6c4009
        {
Packit 6c4009
          /* Overflow.  If there was at least one entry before this one,
Packit 6c4009
             return them without error, otherwise signal overflow.  */
Packit 6c4009
          if (last_offset != -1)
Packit 6c4009
            {
Packit 6c4009
              __lseek64 (fd, last_offset, SEEK_SET);
Packit 6c4009
              return outp->b - buf;
Packit 6c4009
            }
Packit 6c4009
	  return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
Packit 6c4009
        }
Packit 6c4009
Packit 6c4009
      last_offset = d_off;
Packit 6c4009
      outp->u.d_reclen = new_reclen;
Packit 6c4009
      outp->u.d_type = d_type;
Packit 6c4009
Packit 6c4009
      inp = (void *) inp + old_reclen;
Packit 6c4009
      outp = (void *) outp + new_reclen;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return outp->b - buf;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# undef DIRENT_SET_DP_INO
Packit 6c4009
Packit 6c4009
#endif /* _DIRENT_MATCHES_DIRENT64  */