getdents.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /* With newer versions of linux, the getdents syscall returns d_type
  18. * information after the name field. Someday, we should add support for
  19. * that instead of always calling getdents64 ...
  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. #if ! defined __UCLIBC_HAS_LFS__ || ! defined __NR_getdents64
  36. libc_hidden_proto(memcpy)
  37. libc_hidden_proto(lseek)
  38. #define __NR___syscall_getdents __NR_getdents
  39. static inline _syscall3(int, __syscall_getdents, int, fd, unsigned char *, kdirp, size_t, count);
  40. ssize_t __getdents (int fd, char *buf, size_t nbytes)
  41. {
  42. struct dirent *dp;
  43. off_t last_offset = -1;
  44. ssize_t retval;
  45. size_t red_nbytes;
  46. struct kernel_dirent *skdp, *kdp;
  47. const size_t size_diff = (offsetof (struct dirent, d_name)
  48. - offsetof (struct kernel_dirent, d_name));
  49. red_nbytes = MIN (nbytes - ((nbytes /
  50. (offsetof (struct dirent, d_name) + 14)) * size_diff),
  51. nbytes - size_diff);
  52. dp = (struct dirent *) buf;
  53. skdp = kdp = alloca (red_nbytes);
  54. retval = __syscall_getdents(fd, (unsigned char *)kdp, red_nbytes);
  55. if (retval == -1)
  56. return -1;
  57. while ((char *) kdp < (char *) skdp + retval) {
  58. const size_t alignment = __alignof__ (struct dirent);
  59. /* Since kdp->d_reclen is already aligned for the kernel structure
  60. this may compute a value that is bigger than necessary. */
  61. size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
  62. & ~(alignment - 1));
  63. if ((char *) dp + new_reclen > buf + nbytes) {
  64. /* Our heuristic failed. We read too many entries. Reset
  65. the stream. */
  66. assert (last_offset != -1);
  67. lseek(fd, last_offset, SEEK_SET);
  68. if ((char *) dp == buf) {
  69. /* The buffer the user passed in is too small to hold even
  70. one entry. */
  71. __set_errno (EINVAL);
  72. return -1;
  73. }
  74. break;
  75. }
  76. last_offset = kdp->d_off;
  77. dp->d_ino = kdp->d_ino;
  78. dp->d_off = kdp->d_off;
  79. dp->d_reclen = new_reclen;
  80. dp->d_type = DT_UNKNOWN;
  81. memcpy (dp->d_name, kdp->d_name,
  82. kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
  83. dp = (struct dirent *) ((char *) dp + new_reclen);
  84. kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
  85. }
  86. return (char *) dp - buf;
  87. }
  88. #elif __WORDSIZE == 32
  89. libc_hidden_proto(memmove)
  90. extern __typeof(__getdents) __getdents64 attribute_hidden;
  91. ssize_t __getdents (int fd, char *buf, size_t nbytes)
  92. {
  93. struct dirent *dp;
  94. struct dirent64 *dp64;
  95. ssize_t ret = __getdents64 (fd, buf, nbytes);
  96. if (ret <= 0)
  97. return ret;
  98. dp64 = (struct dirent64 *) buf;
  99. buf += ret;
  100. while ((void *) dp64 < (void *) buf) {
  101. dp = (struct dirent *) dp64;
  102. dp->d_ino = dp64->d_ino;
  103. dp->d_off = dp64->d_off;
  104. dp->d_reclen = dp64->d_reclen;
  105. dp->d_type = dp64->d_type;
  106. memmove (dp->d_name, dp64->d_name, dp->d_reclen - offsetof (struct dirent64, d_name));
  107. memmove (dp64, dp, dp->d_reclen);
  108. dp64 = ((void *) dp64) + dp->d_reclen;
  109. }
  110. return ret;
  111. }
  112. #endif