xref: /xnu-10063.101.15/tests/extended_getdirentries64.c (revision 94d3b452840153a99b38a3a9659680b2a006908e)
1*94d3b452SApple OSS Distributions #ifdef T_NAMESPACE
2*94d3b452SApple OSS Distributions #undef T_NAMESPACE
3*94d3b452SApple OSS Distributions #endif
4*94d3b452SApple OSS Distributions 
5*94d3b452SApple OSS Distributions #include <darwintest.h>
6*94d3b452SApple OSS Distributions #include <darwintest_multiprocess.h>
7*94d3b452SApple OSS Distributions 
8*94d3b452SApple OSS Distributions #define PRIVATE 1
9*94d3b452SApple OSS Distributions #include "../bsd/sys/dirent.h"
10*94d3b452SApple OSS Distributions 
11*94d3b452SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
12*94d3b452SApple OSS Distributions 
13*94d3b452SApple OSS Distributions ssize_t __getdirentries64(int fd, void *buf, size_t bufsize, off_t *basep);
14*94d3b452SApple OSS Distributions 
15*94d3b452SApple OSS Distributions T_DECL(getdirentries64_extended, "check for GETDIRENTRIES64_EOF")
16*94d3b452SApple OSS Distributions {
17*94d3b452SApple OSS Distributions 	char buf[GETDIRENTRIES64_EXTENDED_BUFSIZE];
18*94d3b452SApple OSS Distributions 	getdirentries64_flags_t *flags;
19*94d3b452SApple OSS Distributions 	ssize_t result;
20*94d3b452SApple OSS Distributions 	off_t offset;
21*94d3b452SApple OSS Distributions 	int fd;
22*94d3b452SApple OSS Distributions 	bool eof = false;
23*94d3b452SApple OSS Distributions 
24*94d3b452SApple OSS Distributions 	flags = (getdirentries64_flags_t *)(uintptr_t)(buf + sizeof(buf) -
25*94d3b452SApple OSS Distributions 	    sizeof(getdirentries64_flags_t));
26*94d3b452SApple OSS Distributions 	fd = open("/", O_DIRECTORY | O_RDONLY);
27*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd, "open(/)");
28*94d3b452SApple OSS Distributions 
29*94d3b452SApple OSS Distributions 	for (;;) {
30*94d3b452SApple OSS Distributions 		*flags = (getdirentries64_flags_t)~0;
31*94d3b452SApple OSS Distributions 		result = __getdirentries64(fd, buf, sizeof(buf), &offset);
32*94d3b452SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(result, "__getdirentries64()");
33*94d3b452SApple OSS Distributions 		T_ASSERT_LE((size_t)result, sizeof(buf) - sizeof(getdirentries64_flags_t),
34*94d3b452SApple OSS Distributions 		    "The kernel should have left space for the flags");
35*94d3b452SApple OSS Distributions 		T_ASSERT_NE(*flags, (getdirentries64_flags_t)~0,
36*94d3b452SApple OSS Distributions 		    "The kernel should have returned status");
37*94d3b452SApple OSS Distributions 		if (eof) {
38*94d3b452SApple OSS Distributions 			T_ASSERT_EQ(result, 0l, "At EOF, we really should be done");
39*94d3b452SApple OSS Distributions 			T_ASSERT_TRUE(*flags & GETDIRENTRIES64_EOF, "And EOF should still be set");
40*94d3b452SApple OSS Distributions 			T_END;
41*94d3b452SApple OSS Distributions 		}
42*94d3b452SApple OSS Distributions 		T_ASSERT_NE(result, 0l, "We're not at EOF, we should have an entry");
43*94d3b452SApple OSS Distributions 		eof = (*flags & GETDIRENTRIES64_EOF);
44*94d3b452SApple OSS Distributions 	}
45*94d3b452SApple OSS Distributions }
46