getdents.c 4.3 KB

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