getdents.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (C) 1993, 1995-2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include <alloca.h>
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <dirent.h>
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/param.h>
  24. #include <sys/types.h>
  25. #include <sys/syscall.h>
  26. #ifndef offsetof
  27. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  28. #endif
  29. struct kernel_dirent
  30. {
  31. long d_ino;
  32. __kernel_off_t d_off;
  33. unsigned short d_reclen;
  34. char d_name[256];
  35. };
  36. #define __NR___syscall_getdents __NR_getdents
  37. static inline _syscall3(int, __syscall_getdents, int, fd, unsigned char *, kdirp, size_t, count);
  38. ssize_t attribute_hidden __getdents (int fd, char *buf, size_t nbytes)
  39. {
  40. struct dirent *dp;
  41. off_t last_offset = -1;
  42. ssize_t retval;
  43. size_t red_nbytes;
  44. struct kernel_dirent *skdp, *kdp;
  45. const size_t size_diff = (offsetof (struct dirent, d_name)
  46. - offsetof (struct kernel_dirent, d_name));
  47. red_nbytes = MIN (nbytes - ((nbytes /
  48. (offsetof (struct dirent, d_name) + 14)) * size_diff),
  49. nbytes - size_diff);
  50. dp = (struct dirent *) buf;
  51. skdp = kdp = alloca (red_nbytes);
  52. retval = __syscall_getdents(fd, (char *)kdp, red_nbytes);
  53. if (retval == -1)
  54. return -1;
  55. while ((char *) kdp < (char *) skdp + retval) {
  56. const size_t alignment = __alignof__ (struct dirent);
  57. /* Since kdp->d_reclen is already aligned for the kernel structure
  58. this may compute a value that is bigger than necessary. */
  59. size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
  60. & ~(alignment - 1));
  61. if ((char *) dp + new_reclen > buf + nbytes) {
  62. /* Our heuristic failed. We read too many entries. Reset
  63. the stream. */
  64. assert (last_offset != -1);
  65. lseek(fd, last_offset, SEEK_SET);
  66. if ((char *) dp == buf) {
  67. /* The buffer the user passed in is too small to hold even
  68. one entry. */
  69. __set_errno (EINVAL);
  70. return -1;
  71. }
  72. break;
  73. }
  74. last_offset = kdp->d_off;
  75. dp->d_ino = kdp->d_ino;
  76. dp->d_off = kdp->d_off;
  77. dp->d_reclen = new_reclen;
  78. dp->d_type = DT_UNKNOWN;
  79. __memcpy (dp->d_name, kdp->d_name,
  80. kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
  81. dp = (struct dirent *) ((char *) dp + new_reclen);
  82. kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
  83. }
  84. return (char *) dp - buf;
  85. }