getdents.c 4.5 KB

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