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