]>
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-2019 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 #ifdef __NR_getdents64
38 static int getdents64_supported
= true;
39 if (atomic_load_relaxed (&getdents64_supported
))
41 ssize_t ret
= INLINE_SYSCALL_CALL (getdents64
, fd
, buf
, nbytes
);
42 if (ret
>= 0 || errno
!= ENOSYS
)
45 atomic_store_relaxed (&getdents64_supported
, false);
49 /* Unfortunately getdents64 was only wire-up for MIPS n64 on Linux 3.10.
50 If the syscall is not available it need to fallback to the non-LFS one.
51 Also to avoid an unbounded allocation through VLA/alloca or malloc (which
52 would make the syscall non async-signal-safe) it uses a limited buffer.
53 This is sub-optimal for large NBYTES, however this is a fallback
54 mechanism to emulate a syscall that kernel should provide. */
58 #if _MIPS_SIM == _ABI64
65 unsigned short int d_reclen
;
69 /* The largest possible practical length of the d_name member are 255
70 Unicode characters in UTF-8 encoding, so d_name is 766 bytes long, plus
71 18 (mips64) / 10 (mips64n32) bytes from header, for total of 784 (mips64)
72 / 776 (mips64n32) bytes total. Ensure that the minimum size holds at
74 enum { KBUF_SIZE
= 1024 };
76 size_t kbuf_size
= nbytes
< KBUF_SIZE
? nbytes
: KBUF_SIZE
;
78 const size_t size_diff
= (offsetof (struct dirent64
, d_name
)
79 - offsetof (struct kernel_dirent
, d_name
));
81 struct dirent64
*dp
= (struct dirent64
*) buf
;
84 off64_t last_offset
= -1;
86 ssize_t r
= INLINE_SYSCALL_CALL (getdents
, fd
, kbuf
, kbuf_size
);
90 struct kernel_dirent
*skdp
, *kdp
;
91 skdp
= kdp
= (struct kernel_dirent
*) kbuf
;
93 while ((char *) kdp
< (char *) skdp
+ r
)
95 /* This macro is used to avoid aliasing violation. */
96 #define KDP_MEMBER(src, member) \
97 (__typeof__((struct kernel_dirent){0}.member) *) \
98 memcpy (&((__typeof__((struct kernel_dirent){0}.member)){0}), \
99 ((char *)(src) + offsetof (struct kernel_dirent, member)),\
100 sizeof ((struct kernel_dirent){0}.member))
102 /* This is a conservative approximation, since some of size_diff might
103 fit into the existing padding for alignment. */
104 unsigned short int k_reclen
= *KDP_MEMBER (kdp
, d_reclen
);
105 unsigned short int new_reclen
= ALIGN_UP (k_reclen
+ size_diff
,
106 _Alignof (struct dirent64
));
107 if (nb
+ new_reclen
> nbytes
)
109 /* Entry is too large for the fixed-size buffer. */
110 if (last_offset
== -1)
112 __set_errno (EINVAL
);
116 /* The new entry will overflow the input buffer, rewind to last
117 obtained entry and return. */
118 __lseek64 (fd
, last_offset
, SEEK_SET
);
119 return (char *) dp
- (char *) buf
;
123 memcpy (((char *) dp
+ offsetof (struct dirent64
, d_ino
)),
124 KDP_MEMBER (kdp
, d_ino
), sizeof ((struct dirent64
){0}.d_ino
));
125 memcpy (((char *) dp
+ offsetof (struct dirent64
, d_off
)),
126 KDP_MEMBER (kdp
, d_off
), sizeof ((struct dirent64
){0}.d_off
));
127 last_offset
= *KDP_MEMBER (kdp
, d_off
);
128 memcpy (((char *) dp
+ offsetof (struct dirent64
, d_reclen
)),
129 &new_reclen
, sizeof (new_reclen
));
130 dp
->d_type
= *((char *) kdp
+ k_reclen
- 1);
131 memcpy (dp
->d_name
, kdp
->d_name
,
132 k_reclen
- offsetof (struct kernel_dirent
, d_name
));
134 dp
= (struct dirent64
*) ((char *) dp
+ new_reclen
);
135 kdp
= (struct kernel_dirent
*) (((char *) kdp
) + k_reclen
);
138 return (char *) dp
- (char *) buf
;
140 libc_hidden_def (__getdents64
)
141 weak_alias (__getdents64
, getdents64
)
143 #if _DIRENT_MATCHES_DIRENT64
144 strong_alias (__getdents64
, __getdents
)