getdents64.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Get directory entries. Linux LFS version.
  2. Copyright (C) 1997-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 <string.h>
  16. #include <dirent.h>
  17. #include <errno.h>
  18. /* The kernel struct linux_dirent64 matches the 'struct getdents64' type. */
  19. ssize_t
  20. __getdents64 (int fd, char *buf, size_t nbytes)
  21. {
  22. return INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes);
  23. }
  24. #if _DIRENT_MATCHES_DIRENT64
  25. strong_alias (__getdents64, __getdents)
  26. #else
  27. # include <shlib-compat.h>
  28. # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2)
  29. # include <olddirent.h>
  30. # include <unistd.h>
  31. static ssize_t
  32. handle_overflow (int fd, __off64_t offset, ssize_t count)
  33. {
  34. /* If this is the first entry in the buffer, we can report the
  35. error. */
  36. if (offset == 0)
  37. {
  38. __set_errno (EOVERFLOW);
  39. return -1;
  40. }
  41. /* Otherwise, seek to the overflowing entry, so that the next call
  42. will report the error, and return the data read so far. */
  43. if (__lseek64 (fd, offset, SEEK_SET) != 0)
  44. return -1;
  45. return count;
  46. }
  47. ssize_t
  48. __old_getdents64 (int fd, char *buf, size_t nbytes)
  49. {
  50. /* We do not move the individual directory entries. This is only
  51. possible if the target type (struct __old_dirent64) is smaller
  52. than the source type. */
  53. _Static_assert (offsetof (struct __old_dirent64, d_name)
  54. <= offsetof (struct dirent64, d_name),
  55. "__old_dirent64 is larger than dirent64");
  56. _Static_assert (__alignof__ (struct __old_dirent64)
  57. <= __alignof__ (struct dirent64),
  58. "alignment of __old_dirent64 is larger than dirent64");
  59. ssize_t retval = INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes);
  60. if (retval > 0)
  61. {
  62. /* This is the marker for the first entry. Offset 0 is reserved
  63. for the first entry (see rewinddir). Here, we use it as a
  64. marker for the first entry in the buffer. We never actually
  65. seek to offset 0 because handle_overflow reports the error
  66. directly, so it does not matter that the offset is incorrect
  67. if entries have been read from the descriptor before (so that
  68. the descriptor is not actually at offset 0). */
  69. __off64_t previous_offset = 0;
  70. char *p = buf;
  71. char *end = buf + retval;
  72. while (p < end)
  73. {
  74. struct dirent64 *source = (struct dirent64 *) p;
  75. /* Copy out the fixed-size data. */
  76. __ino_t ino = source->d_ino;
  77. __off64_t offset = source->d_off;
  78. unsigned int reclen = source->d_reclen;
  79. unsigned char type = source->d_type;
  80. /* Check for ino_t overflow. */
  81. if (__glibc_unlikely (ino != source->d_ino))
  82. return handle_overflow (fd, previous_offset, p - buf);
  83. /* Convert to the target layout. Use a separate struct and
  84. memcpy to side-step aliasing issues. */
  85. struct __old_dirent64 result;
  86. result.d_ino = ino;
  87. result.d_off = offset;
  88. result.d_reclen = reclen;
  89. result.d_type = type;
  90. /* Write the fixed-sized part of the result to the
  91. buffer. */
  92. size_t result_name_offset = offsetof (struct __old_dirent64, d_name);
  93. memcpy (p, &result, result_name_offset);
  94. /* Adjust the position of the name if necessary. Copy
  95. everything until the end of the record, including the
  96. terminating NUL byte. */
  97. if (result_name_offset != offsetof (struct dirent64, d_name))
  98. memmove (p + result_name_offset, source->d_name,
  99. reclen - offsetof (struct dirent64, d_name));
  100. p += reclen;
  101. previous_offset = offset;
  102. }
  103. }
  104. return retval;
  105. }
  106. # endif /* SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) */
  107. #endif /* _DIRENT_MATCHES_DIRENT64 */