getdents.c 4.3 KB

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