]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/mips/mips64/getdents64.c
1 /* Get directory entries. Linux/MIPSn64 LFS version.
2 Copyright (C) 2018-2023 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 <https://www.gnu.org/licenses/>. */
23 #include <sys/param.h>
27 #include <include/libc-pointer-arith.h>
30 __getdents64 (int fd
, void *buf
, size_t nbytes
)
32 /* The system call takes an unsigned int argument, and some length
33 checks in the kernel use an int type. */
37 static int getdents64_supported
= true;
38 if (atomic_load_relaxed (&getdents64_supported
))
40 ssize_t ret
= INLINE_SYSCALL_CALL (getdents64
, fd
, buf
, nbytes
);
41 if (ret
>= 0 || errno
!= ENOSYS
)
44 atomic_store_relaxed (&getdents64_supported
, false);
47 /* Unfortunately getdents64 was only wire-up for MIPS n64 on Linux 3.10.
48 If the syscall is not available it need to fallback to the non-LFS one.
49 Also to avoid an unbounded allocation through VLA/alloca or malloc (which
50 would make the syscall non async-signal-safe) it uses a limited buffer.
51 This is sub-optimal for large NBYTES, however this is a fallback
52 mechanism to emulate a syscall that kernel should provide. */
56 #if _MIPS_SIM == _ABI64
63 unsigned short int d_reclen
;
67 /* The largest possible practical length of the d_name member are 255
68 Unicode characters in UTF-8 encoding, so d_name is 766 bytes long, plus
69 18 (mips64) / 10 (mips64n32) bytes from header, for total of 784 (mips64)
70 / 776 (mips64n32) bytes total. Ensure that the minimum size holds at
72 enum { KBUF_SIZE
= 1024 };
74 size_t kbuf_size
= nbytes
< KBUF_SIZE
? nbytes
: KBUF_SIZE
;
76 const size_t size_diff
= (offsetof (struct dirent64
, d_name
)
77 - offsetof (struct kernel_dirent
, d_name
));
79 struct dirent64
*dp
= (struct dirent64
*) buf
;
82 off64_t last_offset
= -1;
84 ssize_t r
= INLINE_SYSCALL_CALL (getdents
, fd
, kbuf
, kbuf_size
);
88 struct kernel_dirent
*skdp
, *kdp
;
89 skdp
= kdp
= (struct kernel_dirent
*) kbuf
;
91 while ((char *) kdp
< (char *) skdp
+ r
)
93 /* This is a conservative approximation, since some of size_diff might
94 fit into the existing padding for alignment. */
96 /* Obtain the d_ino, d_off, and d_reclen from kernel filled buffer. */
97 struct kernel_dirent kdirent
;
98 memcpy (&kdirent
, kdp
, offsetof (struct kernel_dirent
, d_name
));
100 unsigned short int new_reclen
= ALIGN_UP (kdirent
.d_reclen
+ size_diff
,
101 _Alignof (struct dirent64
));
102 if (nb
+ new_reclen
> nbytes
)
104 /* Entry is too large for the fixed-size buffer. */
105 if (last_offset
== -1)
107 __set_errno (EINVAL
);
111 /* The new entry will overflow the input buffer, rewind to last
112 obtained entry and return. */
113 __lseek64 (fd
, last_offset
, SEEK_SET
);
114 return (char *) dp
- (char *) buf
;
119 d64
.d_ino
= kdirent
.d_ino
;
120 d64
.d_off
= kdirent
.d_off
;
121 d64
.d_reclen
= new_reclen
;
122 d64
.d_type
= *((char *) kdp
+ kdirent
.d_reclen
- 1);
123 /* First copy only the header. */
124 memcpy (dp
, &d64
, offsetof (struct dirent64
, d_name
));
125 /* And then the d_name. */
126 memcpy (dp
->d_name
, kdp
->d_name
,
127 kdirent
.d_reclen
- offsetof (struct kernel_dirent
, d_name
));
129 last_offset
= kdirent
.d_off
;
131 dp
= (struct dirent64
*) ((char *) dp
+ new_reclen
);
132 kdp
= (struct kernel_dirent
*) (((char *) kdp
) + kdirent
.d_reclen
);
135 return (char *) dp
- (char *) buf
;
137 libc_hidden_def (__getdents64
)
138 weak_alias (__getdents64
, getdents64
)
140 #if _DIRENT_MATCHES_DIRENT64
141 strong_alias (__getdents64
, __getdents
)