getdents.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #if ! defined __UCLIBC_HAS_LFS__ || ! defined __NR_getdents64
  25. libc_hidden_proto(memcpy)
  26. libc_hidden_proto(lseek)
  27. #ifndef offsetof
  28. # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  29. #endif
  30. struct kernel_dirent
  31. {
  32. long int d_ino;
  33. __kernel_off_t d_off;
  34. unsigned short int d_reclen;
  35. char d_name[256];
  36. };
  37. #define __NR___syscall_getdents __NR_getdents
  38. static inline _syscall3(int, __syscall_getdents, int, fd, unsigned char *, kdirp, size_t, count);
  39. ssize_t attribute_hidden __getdents (int fd, char *buf, size_t nbytes);
  40. ssize_t attribute_hidden __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, (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. attribute_hidden strong_alias(__getdents,__getdents64)
  89. #endif