getdents64.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (C) 1993, 1995-2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include <features.h>
  16. #include <alloca.h>
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include <dirent.h>
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include "sysdep.h"
  25. #include <sys/param.h>
  26. #include <sys/types.h>
  27. #include <sys/syscall.h>
  28. #if defined __UCLIBC_HAS_LFS__ && defined __NR_getdents64
  29. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  30. struct kernel_dirent64
  31. {
  32. uint64_t d_ino;
  33. int64_t d_off;
  34. unsigned short d_reclen;
  35. unsigned char d_type;
  36. char d_name[256];
  37. };
  38. #define __NR___syscall_getdents64 __NR_getdents64
  39. static inline _syscall3(int, __syscall_getdents64, int, fd, unsigned char *, dirp, size_t, count);
  40. ssize_t __getdents64 (int fd, char *buf, size_t nbytes)
  41. {
  42. struct dirent64 *dp;
  43. off64_t last_offset = -1;
  44. ssize_t retval;
  45. size_t red_nbytes;
  46. struct kernel_dirent64 *skdp, *kdp;
  47. const size_t size_diff = (offsetof (struct dirent64, d_name)
  48. - offsetof (struct kernel_dirent64, d_name));
  49. red_nbytes = MIN (nbytes - ((nbytes /
  50. (offsetof (struct dirent64, d_name) + 14)) * size_diff),
  51. nbytes - size_diff);
  52. dp = (struct dirent64 *) buf;
  53. skdp = kdp = alloca (red_nbytes);
  54. retval = __syscall_getdents64(fd, (char *)kdp, red_nbytes);
  55. if (retval == -1)
  56. return -1;
  57. while ((char *) kdp < (char *) skdp + retval) {
  58. const size_t alignment = __alignof__ (struct dirent64);
  59. /* Since kdp->d_reclen is already aligned for the kernel structure
  60. this may compute a value that is bigger than necessary. */
  61. size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
  62. & ~(alignment - 1));
  63. if ((char *) dp + new_reclen > buf + nbytes) {
  64. /* Our heuristic failed. We read too many entries. Reset
  65. the stream. */
  66. assert (last_offset != -1);
  67. lseek64(fd, last_offset, SEEK_SET);
  68. if ((char *) dp == buf) {
  69. /* The buffer the user passed in is too small to hold even
  70. one entry. */
  71. __set_errno (EINVAL);
  72. return -1;
  73. }
  74. break;
  75. }
  76. last_offset = kdp->d_off;
  77. dp->d_ino = kdp->d_ino;
  78. dp->d_off = kdp->d_off;
  79. dp->d_reclen = new_reclen;
  80. dp->d_type = DT_UNKNOWN;
  81. memcpy (dp->d_name, kdp->d_name,
  82. kdp->d_reclen - offsetof (struct kernel_dirent64, d_name));
  83. dp = (struct dirent64 *) ((char *) dp + new_reclen);
  84. kdp = (struct kernel_dirent64 *) (((char *) kdp) + kdp->d_reclen);
  85. }
  86. return (char *) dp - buf;
  87. }
  88. #else
  89. ssize_t __getdents (int fd, char *buf, size_t nbytes);
  90. ssize_t __getdents64 (int fd, char *buf, size_t nbytes)
  91. {
  92. return(__getdents(fd, buf, nbytes));
  93. }
  94. #endif /* __UCLIBC_HAS_LFS__ */