getdents.c 3.7 KB

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