getdents64.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <features.h>
  7. #include <alloca.h>
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <dirent.h>
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <sys/param.h>
  16. #include <sys/types.h>
  17. #include <sys/syscall.h>
  18. #if defined __UCLIBC_HAS_LFS__ && defined __NR_getdents64
  19. libc_hidden_proto(memcpy)
  20. libc_hidden_proto(lseek64)
  21. # ifndef offsetof
  22. # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  23. # endif
  24. struct kernel_dirent64
  25. {
  26. uint64_t d_ino;
  27. int64_t d_off;
  28. unsigned short d_reclen;
  29. unsigned char d_type;
  30. char d_name[256];
  31. };
  32. # define __NR___syscall_getdents64 __NR_getdents64
  33. static inline _syscall3(int, __syscall_getdents64, int, fd, unsigned char *, dirp, size_t, count);
  34. ssize_t __getdents64 (int fd, char *buf, size_t nbytes) attribute_hidden;
  35. ssize_t __getdents64 (int fd, char *buf, size_t nbytes)
  36. {
  37. struct dirent64 *dp;
  38. off64_t last_offset = -1;
  39. ssize_t retval;
  40. size_t red_nbytes;
  41. struct kernel_dirent64 *skdp, *kdp;
  42. const size_t size_diff = (offsetof (struct dirent64, d_name)
  43. - offsetof (struct kernel_dirent64, d_name));
  44. red_nbytes = MIN (nbytes - ((nbytes /
  45. (offsetof (struct dirent64, d_name) + 14)) * size_diff),
  46. nbytes - size_diff);
  47. dp = (struct dirent64 *) buf;
  48. skdp = kdp = alloca (red_nbytes);
  49. retval = __syscall_getdents64(fd, (unsigned char *)kdp, red_nbytes);
  50. if (retval == -1)
  51. return -1;
  52. while ((char *) kdp < (char *) skdp + retval) {
  53. const size_t alignment = __alignof__ (struct dirent64);
  54. /* Since kdp->d_reclen is already aligned for the kernel structure
  55. this may compute a value that is bigger than necessary. */
  56. size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
  57. & ~(alignment - 1));
  58. if ((char *) dp + new_reclen > buf + nbytes) {
  59. /* Our heuristic failed. We read too many entries. Reset
  60. the stream. */
  61. assert (last_offset != -1);
  62. lseek64(fd, last_offset, SEEK_SET);
  63. if ((char *) dp == buf) {
  64. /* The buffer the user passed in is too small to hold even
  65. one entry. */
  66. __set_errno (EINVAL);
  67. return -1;
  68. }
  69. break;
  70. }
  71. last_offset = kdp->d_off;
  72. dp->d_ino = kdp->d_ino;
  73. dp->d_off = kdp->d_off;
  74. dp->d_reclen = new_reclen;
  75. dp->d_type = kdp->d_type;
  76. memcpy (dp->d_name, kdp->d_name,
  77. kdp->d_reclen - offsetof (struct kernel_dirent64, d_name));
  78. dp = (struct dirent64 *) ((char *) dp + new_reclen);
  79. kdp = (struct kernel_dirent64 *) (((char *) kdp) + kdp->d_reclen);
  80. }
  81. return (char *) dp - buf;
  82. }
  83. #if __WORDSIZE == 64
  84. /* since getdents doesnt give us d_type but getdents64 does, try and
  85. * use getdents64 as much as possible */
  86. attribute_hidden strong_alias(__getdents64,__getdents)
  87. #endif
  88. #endif /* __UCLIBC_HAS_LFS__ */