getdents.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 1993, 95, 96, 97, 98, 99 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 Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. 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. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. #include <stdlib.h>
  16. #include <errno.h>
  17. #include <dirent.h>
  18. #include <stddef.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/param.h>
  22. #include <sys/types.h>
  23. #include <sysdep.h>
  24. typedef long __kernel_off_t;
  25. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  26. extern int _getdents __P ((int fd, char *buf, size_t nbytes));
  27. /* For Linux we need a special version of this file since the
  28. definition of `struct dirent' is not the same for the kernel and
  29. the libc. There is one additional field which might be introduced
  30. in the kernel structure in the future. Here is a copy of the kernel
  31. struct dirent...
  32. */
  33. struct kernel_dirent {
  34. long d_ino;
  35. __kernel_off_t d_off;
  36. unsigned short d_reclen;
  37. char d_name[256]; /* We must not include limits.h! */
  38. };
  39. #ifdef GETDENTS64
  40. # define __getdents __getdents64
  41. # define dirent dirent64
  42. #endif
  43. /* The problem here is that we cannot simply read the next NBYTES
  44. bytes. We need to take the additional field into account. We use
  45. some heuristic. Assuming the directory contains names with 14
  46. characters on average we can compute an estimated number of entries
  47. which fit in the buffer. Taking this number allows us to specify a
  48. reasonable number of bytes to read. If we should be wrong, we can
  49. reset the file descriptor. In practice the kernel is limiting the
  50. amount of data returned much more then the reduced buffer size. */
  51. ssize_t getdents (int fd, char *buf, size_t nbytes)
  52. {
  53. off_t last_offset = 0;
  54. size_t red_nbytes;
  55. struct kernel_dirent *skdp, *kdp;
  56. struct dirent *dp;
  57. int retval;
  58. const size_t size_diff = (offsetof (struct dirent, d_name)
  59. - offsetof (struct kernel_dirent, d_name));
  60. red_nbytes = nbytes - ((nbytes / (offsetof (struct dirent, d_name) + 14))
  61. * size_diff);
  62. dp = (struct dirent *) buf;
  63. skdp = kdp = malloc (red_nbytes);
  64. retval = _getdents(fd, (char *) kdp, red_nbytes);
  65. if (retval == -1)
  66. return -1;
  67. while ((char *) kdp < (char *) skdp + retval)
  68. {
  69. const size_t alignment = __alignof__ (struct dirent);
  70. /* Since kdp->d_reclen is already aligned for the kernel structure
  71. this may compute a value that is bigger than necessary. */
  72. size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
  73. & ~(alignment - 1));
  74. if ((char *) dp + new_reclen > buf + nbytes)
  75. {
  76. /* Our heuristic failed. We read too many entries. Reset
  77. the stream. `last_offset' contains the last known
  78. position. If it is zero this is the first record we are
  79. reading. In this case do a relative search. */
  80. if (last_offset == 0)
  81. lseek (fd, -retval, SEEK_CUR);
  82. else
  83. lseek (fd, last_offset, SEEK_SET);
  84. break;
  85. }
  86. last_offset = kdp->d_off;
  87. dp->d_ino = kdp->d_ino;
  88. dp->d_off = kdp->d_off;
  89. dp->d_reclen = new_reclen;
  90. dp->d_type = DT_UNKNOWN;
  91. memcpy (dp->d_name, kdp->d_name,
  92. kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
  93. dp = (struct dirent *) ((char *) dp + new_reclen);
  94. kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
  95. }
  96. return (char *) dp - buf;
  97. }