getdents64.c 2.7 KB

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