getdents64.c 3.5 KB

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