getdents.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Get directory entries. Linux non-LFS version.
  2. Copyright (C) 1993-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library. If not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <dirent.h>
  16. #if !_DIRENT_MATCHES_DIRENT64
  17. # include <unistd.h>
  18. # include <string.h>
  19. # include <errno.h>
  20. # ifndef DIRENT_SET_DP_INO
  21. # define DIRENT_SET_DP_INO(dp, value) (dp)->d_ino = (value)
  22. # endif
  23. /* Pack the dirent64 struct down into 32-bit offset/inode fields, and
  24. ensure that no overflow occurs. */
  25. ssize_t
  26. __getdents (int fd, char *buf, size_t nbytes)
  27. {
  28. union
  29. {
  30. /* For !_DIRENT_MATCHES_DIRENT64 kernel 'linux_dirent64' has the same
  31. layout of 'struct dirent64'. */
  32. struct dirent64 k;
  33. struct dirent u;
  34. char b[1];
  35. } *kbuf = (void *) buf, *outp, *inp;
  36. size_t kbytes = nbytes;
  37. off64_t last_offset = -1;
  38. ssize_t retval;
  39. # define size_diff (offsetof (struct dirent64, d_name) \
  40. - offsetof (struct dirent, d_name))
  41. char kbuftmp[sizeof (struct dirent) + size_diff];
  42. if (nbytes <= sizeof (struct dirent))
  43. kbuf = (void*) kbuftmp;
  44. retval = INLINE_SYSCALL_CALL (getdents64, fd, kbuf, kbytes);
  45. if (retval == -1)
  46. return -1;
  47. /* These two pointers might alias the same memory buffer.
  48. Standard C requires that we always use the same type for them,
  49. so we must use the union type. */
  50. inp = kbuf;
  51. outp = (void *) buf;
  52. while (&inp->b < &kbuf->b + retval)
  53. {
  54. const size_t alignment = _Alignof (struct dirent);
  55. /* Since inp->k.d_reclen is already aligned for the kernel
  56. structure this may compute a value that is bigger
  57. than necessary. */
  58. size_t old_reclen = inp->k.d_reclen;
  59. size_t new_reclen = ((old_reclen - size_diff + alignment - 1)
  60. & ~(alignment - 1));
  61. /* Copy the data out of the old structure into temporary space.
  62. Then copy the name, which may overlap if BUF == KBUF. */
  63. const uint64_t d_ino = inp->k.d_ino;
  64. const int64_t d_off = inp->k.d_off;
  65. const uint8_t d_type = inp->k.d_type;
  66. memmove (outp->u.d_name, inp->k.d_name,
  67. old_reclen - offsetof (struct dirent64, d_name));
  68. /* Now we have copied the data from INP and access only OUTP. */
  69. DIRENT_SET_DP_INO (&outp->u, d_ino);
  70. outp->u.d_off = d_off;
  71. if ((sizeof (outp->u.d_ino) != sizeof (inp->k.d_ino)
  72. && outp->u.d_ino != d_ino)
  73. || (sizeof (outp->u.d_off) != sizeof (inp->k.d_off)
  74. && outp->u.d_off != d_off))
  75. {
  76. /* Overflow. If there was at least one entry before this one,
  77. return them without error, otherwise signal overflow. */
  78. if (last_offset != -1)
  79. {
  80. __lseek64 (fd, last_offset, SEEK_SET);
  81. return outp->b - buf;
  82. }
  83. return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
  84. }
  85. last_offset = d_off;
  86. outp->u.d_reclen = new_reclen;
  87. outp->u.d_type = d_type;
  88. inp = (void *) inp + old_reclen;
  89. outp = (void *) outp + new_reclen;
  90. }
  91. return outp->b - buf;
  92. }
  93. # undef DIRENT_SET_DP_INO
  94. #endif /* _DIRENT_MATCHES_DIRENT64 */