1 /* Get directory entries. Linux/MIPSn64 LFS version.
2 Copyright (C) 2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <http://www.gnu.org/licenses/>. */
23 #include <sys/param.h>
25 #include <scratch_buffer.h>
28 __getdents64 (int fd
, char *buf
, size_t nbytes
)
30 #ifdef __NR_getdents64
31 ssize_t ret
= INLINE_SYSCALL_CALL (getdents64
, fd
, buf
, nbytes
);
36 /* Unfortunately getdents64 was only wire-up for MIPS n64 on Linux 3.10.
37 If syscall is not available it need to fallback to non-LFS one. */
43 unsigned short int d_reclen
;
47 const size_t size_diff
= (offsetof (struct dirent64
, d_name
)
48 - offsetof (struct kernel_dirent
, d_name
));
50 size_t red_nbytes
= MIN (nbytes
51 - ((nbytes
/ (offsetof (struct dirent64
, d_name
)
55 struct scratch_buffer tmpbuf
;
56 scratch_buffer_init (&tmpbuf
);
57 if (!scratch_buffer_set_array_size (&tmpbuf
, red_nbytes
, sizeof (uint8_t)))
58 INLINE_SYSCALL_ERROR_RETURN_VALUE (ENOMEM
);
60 struct kernel_dirent
*skdp
, *kdp
;
61 skdp
= kdp
= tmpbuf
.data
;
63 ssize_t retval
= INLINE_SYSCALL_CALL (getdents
, fd
, kdp
, red_nbytes
);
66 scratch_buffer_free (&tmpbuf
);
70 off64_t last_offset
= -1;
71 struct dirent64
*dp
= (struct dirent64
*) buf
;
72 while ((char *) kdp
< (char *) skdp
+ retval
)
74 const size_t alignment
= _Alignof (struct dirent64
);
75 /* Since kdp->d_reclen is already aligned for the kernel structure
76 this may compute a value that is bigger than necessary. */
77 size_t new_reclen
= ((kdp
->d_reclen
+ size_diff
+ alignment
- 1)
79 if ((char *) dp
+ new_reclen
> buf
+ nbytes
)
81 /* Our heuristic failed. We read too many entries. Reset
83 assert (last_offset
!= -1);
84 __lseek64 (fd
, last_offset
, SEEK_SET
);
86 if ((char *) dp
== buf
)
88 scratch_buffer_free (&tmpbuf
);
89 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL
);
95 last_offset
= kdp
->d_off
;
96 dp
->d_ino
= kdp
->d_ino
;
97 dp
->d_off
= kdp
->d_off
;
98 dp
->d_reclen
= new_reclen
;
99 dp
->d_type
= *((char *) kdp
+ kdp
->d_reclen
- 1);
100 memcpy (dp
->d_name
, kdp
->d_name
,
101 kdp
->d_reclen
- offsetof (struct kernel_dirent
, d_name
));
103 dp
= (struct dirent64
*) ((char *) dp
+ new_reclen
);
104 kdp
= (struct kernel_dirent
*) (((char *) kdp
) + kdp
->d_reclen
);
107 scratch_buffer_free (&tmpbuf
);
108 return (char *) dp
- buf
;
110 #if _DIRENT_MATCHES_DIRENT64
111 strong_alias (__getdents64
, __getdents
)