getdents64.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <sys/param.h>
  25. #include <sys/types.h>
  26. #include <sys/syscall.h>
  27. #if defined __UCLIBC_HAS_LFS__ && defined __NR_getdents64
  28. #ifndef offsetof
  29. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  30. #endif
  31. struct kernel_dirent64
  32. {
  33. uint64_t d_ino;
  34. int64_t d_off;
  35. unsigned short d_reclen;
  36. unsigned char d_type;
  37. char d_name[256];
  38. };
  39. #define __NR___syscall_getdents64 __NR_getdents64
  40. static inline _syscall3(int, __syscall_getdents64, int, fd, unsigned char *, dirp, size_t, count);
  41. ssize_t attribute_hidden __getdents64 (int fd, char *buf, size_t nbytes)
  42. {
  43. struct dirent64 *dp;
  44. off64_t last_offset = -1;
  45. ssize_t retval;
  46. size_t red_nbytes;
  47. struct kernel_dirent64 *skdp, *kdp;
  48. const size_t size_diff = (offsetof (struct dirent64, d_name)
  49. - offsetof (struct kernel_dirent64, d_name));
  50. red_nbytes = MIN (nbytes - ((nbytes /
  51. (offsetof (struct dirent64, d_name) + 14)) * size_diff),
  52. nbytes - size_diff);
  53. dp = (struct dirent64 *) buf;
  54. skdp = kdp = alloca (red_nbytes);
  55. retval = __syscall_getdents64(fd, (char *)kdp, red_nbytes);
  56. if (retval == -1)
  57. return -1;
  58. while ((char *) kdp < (char *) skdp + retval) {
  59. const size_t alignment = __alignof__ (struct dirent64);
  60. /* Since kdp->d_reclen is already aligned for the kernel structure
  61. this may compute a value that is bigger than necessary. */
  62. size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
  63. & ~(alignment - 1));
  64. if ((char *) dp + new_reclen > buf + nbytes) {
  65. /* Our heuristic failed. We read too many entries. Reset
  66. the stream. */
  67. assert (last_offset != -1);
  68. lseek64(fd, last_offset, SEEK_SET);
  69. if ((char *) dp == buf) {
  70. /* The buffer the user passed in is too small to hold even
  71. one entry. */
  72. __set_errno (EINVAL);
  73. return -1;
  74. }
  75. break;
  76. }
  77. last_offset = kdp->d_off;
  78. dp->d_ino = kdp->d_ino;
  79. dp->d_off = kdp->d_off;
  80. dp->d_reclen = new_reclen;
  81. dp->d_type = DT_UNKNOWN;
  82. __memcpy (dp->d_name, kdp->d_name,
  83. kdp->d_reclen - offsetof (struct kernel_dirent64, d_name));
  84. dp = (struct dirent64 *) ((char *) dp + new_reclen);
  85. kdp = (struct kernel_dirent64 *) (((char *) kdp) + kdp->d_reclen);
  86. }
  87. return (char *) dp - buf;
  88. }
  89. #else
  90. extern ssize_t __getdents (int fd, char *buf, size_t nbytes) attribute_hidden;
  91. ssize_t attribute_hidden __getdents64 (int fd, char *buf, size_t nbytes)
  92. {
  93. return(__getdents(fd, buf, nbytes));
  94. }
  95. #endif /* __UCLIBC_HAS_LFS__ */