getdents.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 <alloca.h>
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <dirent.h>
  10. #include <stddef.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/param.h>
  15. #include <sys/types.h>
  16. #include <sys/syscall.h>
  17. #include <bits/kernel_types.h>
  18. #include <bits/kernel-features.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 = alloca (red_nbytes);
  79. retval = __syscall_getdents(fd, (unsigned char *)kdp, red_nbytes);
  80. if (retval == -1)
  81. return -1;
  82. while ((char *) kdp < (char *) skdp + retval) {
  83. const size_t alignment = __alignof__ (struct dirent);
  84. /* Since kdp->d_reclen is already aligned for the kernel structure
  85. this may compute a value that is bigger than necessary. */
  86. size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
  87. & ~(alignment - 1));
  88. if ((char *) dp + new_reclen > buf + nbytes) {
  89. /* Our heuristic failed. We read too many entries. Reset
  90. the stream. */
  91. assert (last_offset != -1);
  92. lseek(fd, last_offset, SEEK_SET);
  93. if ((char *) dp == buf) {
  94. /* The buffer the user passed in is too small to hold even
  95. one entry. */
  96. __set_errno (EINVAL);
  97. return -1;
  98. }
  99. break;
  100. }
  101. last_offset = kdp->d_off;
  102. dp->d_ino = kdp->d_ino;
  103. dp->d_off = kdp->d_off;
  104. dp->d_reclen = new_reclen;
  105. dp->d_type = DT_UNKNOWN;
  106. memcpy (dp->d_name, kdp->d_name,
  107. kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
  108. dp = (struct dirent *) ((char *) dp + new_reclen);
  109. kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
  110. }
  111. return (char *) dp - buf;
  112. }
  113. #elif __WORDSIZE == 32
  114. extern __typeof(__getdents) __getdents64 attribute_hidden;
  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. attribute_hidden strong_alias(__getdents,__getdents64)
  139. #endif
  140. #endif