getdents.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <sys/syscall.h>
  7. #include <bits/wordsize.h>
  8. #if !(defined __UCLIBC_HAS_LFS__ && defined __NR_getdents64 && __WORDSIZE == 64)
  9. #include <dirent.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <bits/kernel_types.h>
  13. #include <bits/kernel-features.h>
  14. /* If the condition above is met, __getdents is defined as an alias
  15. * for __getdents64 (see getdents64.c). Otherwise...
  16. */
  17. /* With newer versions of linux, the getdents syscall returns d_type
  18. * information after the name field.
  19. *
  20. * See __ASSUME_GETDENTS32_D_TYPE in glibc's kernel-features.h for specific
  21. * version / arch details.
  22. */
  23. # ifdef __ARCH_HAS_DEPRECATED_SYSCALLS__
  24. struct kernel_dirent
  25. {
  26. long int d_ino;
  27. __kernel_off_t d_off;
  28. unsigned short int d_reclen;
  29. char d_name[256];
  30. };
  31. # else
  32. # define kernel_dirent dirent
  33. # endif
  34. # if defined __NR_getdents
  35. # define __NR___syscall_getdents __NR_getdents
  36. static __always_inline _syscall3(int, __syscall_getdents, int, fd, unsigned char *, kdirp, size_t, count)
  37. # endif
  38. # if defined __ASSUME_GETDENTS32_D_TYPE && defined __NR_getdents
  39. ssize_t __getdents (int fd, char *buf, size_t nbytes)
  40. {
  41. ssize_t retval;
  42. retval = __syscall_getdents(fd, (unsigned char *)buf, nbytes);
  43. /* The kernel added the d_type value after the name. Change
  44. this now. */
  45. if (retval != -1) {
  46. union {
  47. struct kernel_dirent k;
  48. struct dirent u;
  49. } *kbuf = (void *) buf;
  50. while ((char *) kbuf < buf + retval) {
  51. char d_type = *((char *) kbuf + kbuf->k.d_reclen - 1);
  52. memmove (kbuf->u.d_name, kbuf->k.d_name,
  53. strlen (kbuf->k.d_name) + 1);
  54. kbuf->u.d_type = d_type;
  55. kbuf = (void *) ((char *) kbuf + kbuf->k.d_reclen);
  56. }
  57. }
  58. return retval;
  59. }
  60. # elif ! defined __UCLIBC_HAS_LFS__ || !defined __NR_getdents64
  61. # include <assert.h>
  62. # include <stddef.h>
  63. # include <errno.h>
  64. # include <unistd.h>
  65. # include <sys/param.h>
  66. # include <bits/uClibc_alloc.h>
  67. ssize_t __getdents (int fd, char *buf, size_t nbytes)
  68. {
  69. struct dirent *dp;
  70. off_t last_offset = -1;
  71. ssize_t retval;
  72. size_t red_nbytes;
  73. struct kernel_dirent *skdp, *kdp;
  74. const size_t size_diff = (offsetof (struct dirent, d_name)
  75. - offsetof (struct kernel_dirent, d_name));
  76. # ifndef __ARCH_HAS_DEPRECATED_SYSCALLS__
  77. red_nbytes = MIN (nbytes - ((nbytes /
  78. (offsetof (struct dirent, d_name) + 14)) * size_diff),
  79. nbytes - size_diff);
  80. dp = (struct dirent *) buf;
  81. skdp = kdp = stack_heap_alloc(red_nbytes);
  82. retval = __syscall_getdents(fd, (unsigned char *)kdp, red_nbytes);
  83. # else
  84. dp = (struct dirent *) buf;
  85. skdp = kdp = stack_heap_alloc(nbytes);
  86. retval = INLINE_SYSCALL(getdents64, 3, fd, (unsigned char *)kdp, nbytes);
  87. if (retval > 0) {
  88. /* Did we overflow? */
  89. if (kdp->__pad1 || kdp->__pad2) {
  90. __set_errno(EINVAL);
  91. return -1;
  92. }
  93. }
  94. # endif
  95. if (retval == -1) {
  96. stack_heap_free(skdp);
  97. return -1;
  98. }
  99. while ((char *) kdp < (char *) skdp + retval) {
  100. const size_t alignment = __alignof__ (struct dirent);
  101. /* Since kdp->d_reclen is already aligned for the kernel structure
  102. this may compute a value that is bigger than necessary. */
  103. size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
  104. & ~(alignment - 1));
  105. if ((char *) dp + new_reclen > buf + nbytes) {
  106. /* Our heuristic failed. We read too many entries. Reset
  107. the stream. */
  108. assert (last_offset != -1);
  109. lseek(fd, last_offset, SEEK_SET);
  110. if ((char *) dp == buf) {
  111. /* The buffer the user passed in is too small to hold even
  112. one entry. */
  113. stack_heap_free(skdp);
  114. __set_errno (EINVAL);
  115. return -1;
  116. }
  117. break;
  118. }
  119. last_offset = kdp->d_off;
  120. dp->d_ino = kdp->d_ino;
  121. dp->d_off = kdp->d_off;
  122. dp->d_reclen = new_reclen;
  123. dp->d_type = DT_UNKNOWN;
  124. memcpy (dp->d_name, kdp->d_name,
  125. kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
  126. dp = (struct dirent *) ((char *) dp + new_reclen);
  127. kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
  128. }
  129. stack_heap_free(skdp);
  130. return (char *) dp - buf;
  131. }
  132. # elif __WORDSIZE == 32 && !defined __NR_getdents64
  133. # include <stddef.h>
  134. ssize_t __getdents (int fd, char *buf, size_t nbytes)
  135. {
  136. struct dirent *dp;
  137. struct dirent64 *dp64;
  138. ssize_t ret = __getdents64 (fd, buf, nbytes);
  139. if (ret <= 0)
  140. return ret;
  141. dp64 = (struct dirent64 *) buf;
  142. buf += ret;
  143. while ((void *) dp64 < (void *) buf) {
  144. dp = (struct dirent *) dp64;
  145. dp->d_ino = dp64->d_ino;
  146. dp->d_off = dp64->d_off;
  147. dp->d_reclen = dp64->d_reclen;
  148. dp->d_type = dp64->d_type;
  149. memmove (dp->d_name, dp64->d_name, dp->d_reclen - offsetof (struct dirent64, d_name));
  150. memmove (dp64, dp, dp->d_reclen);
  151. dp64 = ((void *) dp64) + dp->d_reclen;
  152. }
  153. return ret;
  154. }
  155. # endif
  156. # if defined __UCLIBC_HAS_LFS__ && ! defined __NR_getdents64
  157. strong_alias(__getdents,__getdents64)
  158. # endif
  159. #endif