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

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 8d57f6
#  include <olddirent.h>
Packit Service 8d57f6
#  include <unistd.h>
Packit 6c4009
Packit Service 8d57f6
static ssize_t
Packit Service 8d57f6
handle_overflow (int fd, __off64_t offset, ssize_t count)
Packit 6c4009
{
Packit Service 8d57f6
  /* If this is the first entry in the buffer, we can report the
Packit Service 8d57f6
     error.  */
Packit Service 8d57f6
  if (count == 0)
Packit Service 8d57f6
    {
Packit Service 8d57f6
      __set_errno (EOVERFLOW);
Packit Service 8d57f6
      return -1;
Packit Service 8d57f6
    }
Packit Service 8d57f6
Packit Service 8d57f6
  /* Otherwise, seek to the overflowing entry, so that the next call
Packit Service 8d57f6
     will report the error, and return the data read so far..  */
Packit Service 8d57f6
  if (__lseek64 (fd, offset, SEEK_SET) != 0)
Packit Service 8d57f6
    return -1;
Packit Service 8d57f6
  return count;
Packit Service 8d57f6
}
Packit 6c4009
Packit 6c4009
ssize_t
Packit 6c4009
__old_getdents64 (int fd, char *buf, size_t nbytes)
Packit 6c4009
{
Packit Service 8d57f6
  /* We do not move the individual directory entries.  This is only
Packit Service 8d57f6
     possible if the target type (struct __old_dirent64) is smaller
Packit Service 8d57f6
     than the source type.  */
Packit Service 8d57f6
  _Static_assert (offsetof (struct __old_dirent64, d_name)
Packit Service 8d57f6
		  <= offsetof (struct dirent64, d_name),
Packit Service 8d57f6
		  "__old_dirent64 is larger than dirent64");
Packit Service 8d57f6
  _Static_assert (__alignof__ (struct __old_dirent64)
Packit Service 8d57f6
		  <= __alignof__ (struct dirent64),
Packit Service 8d57f6
		  "alignment of __old_dirent64 is larger than dirent64");
Packit 6c4009
Packit Service 8d57f6
  ssize_t retval = INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes);
Packit Service 8d57f6
  if (retval > 0)
Packit 6c4009
    {
Packit Service 8d57f6
      char *p = buf;
Packit Service 8d57f6
      char *end = buf + retval;
Packit Service 8d57f6
      while (p < end)
Packit Service 3e830d
	{
Packit Service 8d57f6
	  struct dirent64 *source = (struct dirent64 *) p;
Packit Service 8d57f6
Packit Service 8d57f6
	  /* Copy out the fixed-size data.  */
Packit Service 8d57f6
	  __ino_t ino = source->d_ino;
Packit Service 8d57f6
	  __off64_t offset = source->d_off;
Packit Service 8d57f6
	  unsigned int reclen = source->d_reclen;
Packit Service 8d57f6
	  unsigned char type = source->d_type;
Packit Service 8d57f6
Packit Service 8d57f6
	  /* Check for ino_t overflow.  */
Packit Service 8d57f6
	  if (__glibc_unlikely (ino != source->d_ino))
Packit Service 8d57f6
	    return handle_overflow (fd, offset, p - buf);
Packit Service 8d57f6
Packit Service 8d57f6
	  /* Convert to the target layout.  Use a separate struct and
Packit Service 8d57f6
	     memcpy to side-step aliasing issues.  */
Packit Service 8d57f6
	  struct __old_dirent64 result;
Packit Service 8d57f6
	  result.d_ino = ino;
Packit Service 8d57f6
	  result.d_off = offset;
Packit Service 8d57f6
	  result.d_reclen = reclen;
Packit Service 8d57f6
	  result.d_type = type;
Packit Service 8d57f6
Packit Service 8d57f6
	  /* Write the fixed-sized part of the result to the
Packit Service 8d57f6
	     buffer.  */
Packit Service 8d57f6
	  size_t result_name_offset = offsetof (struct __old_dirent64, d_name);
Packit Service 8d57f6
	  memcpy (p, &result, result_name_offset);
Packit Service 8d57f6
Packit Service 8d57f6
	  /* Adjust the position of the name if necessary.  Copy
Packit Service 8d57f6
	     everything until the end of the record, including the
Packit Service 8d57f6
	     terminating NUL byte.  */
Packit Service 8d57f6
	  if (result_name_offset != offsetof (struct dirent64, d_name))
Packit Service 8d57f6
	    memmove (p + result_name_offset, source->d_name,
Packit Service 8d57f6
		     reclen - offsetof (struct dirent64, d_name));
Packit 6c4009
Packit Service 8d57f6
	  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  */