ObjFW  Check-in [a07e980793]

Overview
Comment:Use readdir_r or fall back to readdir + lock
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a07e98079301dda80c1d01ec2aa982803eb70c3f15cc5779a06e0ba34b3c4fe0
User & Date: js on 2014-08-06 00:04:10
Other Links: manifest | tags
Context
2014-08-07
20:20
Fix generators/Makefile on OS X check-in: 21d8c47508 user: js tags: trunk
2014-08-06
00:04
Use readdir_r or fall back to readdir + lock check-in: a07e980793 user: js tags: trunk
00:04
Don't export (v)asprintf implementation check-in: ddbbd50e56 user: js tags: trunk
Changes

Modified configure.ac from [1a97424463] to [1aff663ede].

493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
		])
	])
	AC_MSG_RESULT($ac_cv_snprintf_useful_ret)
])
test x"$have_asprintf" != x"yes" -a x"$ac_cv_snprintf_useful_ret" != x"yes" && \
	AC_MSG_ERROR(No asprintf and no snprintf returning required space!)

AC_CHECK_FUNC(arc4random, [
	AC_DEFINE(HAVE_ARC4RANDOM, 1, [Whether we have arc4random()])
], [
	AC_CHECK_FUNC(random, [
		AC_DEFINE(HAVE_RANDOM, 1, [Whether we have random()])
	])
])

AC_CHECK_LIB(dl, dlopen, LIBS="$LIBS -ldl")
AC_CHECK_HEADERS(dlfcn.h)
case "$host_os" in
	netbsd*)
		dnl dladdr exists on NetBSD, but it is completely broken.
		dnl When using it with code that uses __thread, it freezes the







<
<
<
|
<
<
<







493
494
495
496
497
498
499



500



501
502
503
504
505
506
507
		])
	])
	AC_MSG_RESULT($ac_cv_snprintf_useful_ret)
])
test x"$have_asprintf" != x"yes" -a x"$ac_cv_snprintf_useful_ret" != x"yes" && \
	AC_MSG_ERROR(No asprintf and no snprintf returning required space!)




AC_CHECK_FUNCS([arc4random random], break)




AC_CHECK_LIB(dl, dlopen, LIBS="$LIBS -ldl")
AC_CHECK_HEADERS(dlfcn.h)
case "$host_os" in
	netbsd*)
		dnl dladdr exists on NetBSD, but it is completely broken.
		dnl When using it with code that uses __thread, it freezes the
691
692
693
694
695
696
697


698
699
700
701
702
703
704

AC_ARG_ENABLE(files,
	AS_HELP_STRING([--disable-files], [disable file support]))
AS_IF([test x"$enable_files" != x"no"], [
	AC_DEFINE(OF_HAVE_FILES, 1, [Whether we have files])
	AC_SUBST(USE_SRCS_FILES, '${SRCS_FILES}')
	AC_SUBST(OFZIP, "ofzip$ac_cv_exeext")


])

AC_CHECK_FUNCS([sysconf gmtime_r localtime_r nanosleep lstat])

AC_CHECK_HEADERS([pwd.h grp.h])
AC_CHECK_FUNC(chmod, [
	AC_DEFINE(OF_HAVE_CHMOD, 1, [Whether we have chmod()])







>
>







685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700

AC_ARG_ENABLE(files,
	AS_HELP_STRING([--disable-files], [disable file support]))
AS_IF([test x"$enable_files" != x"no"], [
	AC_DEFINE(OF_HAVE_FILES, 1, [Whether we have files])
	AC_SUBST(USE_SRCS_FILES, '${SRCS_FILES}')
	AC_SUBST(OFZIP, "ofzip$ac_cv_exeext")

	AC_CHECK_FUNCS(readdir_r)
])

AC_CHECK_FUNCS([sysconf gmtime_r localtime_r nanosleep lstat])

AC_CHECK_HEADERS([pwd.h grp.h])
AC_CHECK_FUNC(chmod, [
	AC_DEFINE(OF_HAVE_CHMOD, 1, [Whether we have chmod()])

Modified src/OFFile.m from [282630ecb7] to [10bc91c172].

112
113
114
115
116
117
118



119
120
121
122
123
124
125

#define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
#define DIR_MODE DEFAULT_MODE | S_IXUSR | S_IXGRP | S_IXOTH

#if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS)
static of_mutex_t mutex;
#endif




int
of_stat(OFString *path, of_stat_t *buffer)
{
#ifdef _WIN32
	return _wstat([path UTF16String], buffer);
#else







>
>
>







112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

#define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
#define DIR_MODE DEFAULT_MODE | S_IXUSR | S_IXGRP | S_IXOTH

#if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS)
static of_mutex_t mutex;
#endif
#if !defined(HAVE_READDIR_R) && !defined(_WIN32) && defined(OF_HAVE_THREADS)
static of_mutex_t mutex;
#endif

int
of_stat(OFString *path, of_stat_t *buffer)
{
#ifdef _WIN32
	return _wstat([path UTF16String], buffer);
#else
183
184
185
186
187
188
189






190
191
192
193
194
195
196
+ (void)initialize
{
	if (self != [OFFile class])
		return;

#if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS)
	if (!of_mutex_new(&mutex))






		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
#endif

#ifdef __wii__
	if (!fatInitDefault())
		@throw [OFInitializationFailedException







>
>
>
>
>
>







186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
+ (void)initialize
{
	if (self != [OFFile class])
		return;

#if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS)
	if (!of_mutex_new(&mutex))
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
#endif

#if !defined(HAVE_READDIR_R) && !defined(_WIN32) && defined(OF_HAVE_THREADS)
	if (!of_mutex_new(&mutex))
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
#endif

#ifdef __wii__
	if (!fatInitDefault())
		@throw [OFInitializationFailedException
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372





373

374



375
376












377
378
379
380


381
382
383
384
385
386
387
388
389




390
391
392
393
394
395
396
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

	files = [OFMutableArray array];

#ifndef _WIN32
	DIR *dir;
	struct dirent *dirent;

	encoding = [OFString nativeOSEncoding];

	if ((dir = opendir([path cStringWithEncoding: encoding])) == NULL)
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];






	@try {

		while ((dirent = readdir(dir)) != NULL) {



			void *pool = objc_autoreleasePoolPush();
			OFString *file;













			if (strcmp(dirent->d_name, ".") == 0 ||
			    strcmp(dirent->d_name, "..") == 0)
				continue;



			file = [OFString stringWithCString: dirent->d_name
						  encoding: encoding];
			[files addObject: file];

			objc_autoreleasePoolPop(pool);
		}
	} @finally {
		closedir(dir);




	}
#else
	void *pool = objc_autoreleasePoolPush();
	HANDLE handle;
	WIN32_FIND_DATAW fd;

	path = [path stringByAppendingString: @"\\*"];







<







>
>
>
>
>

>
|
>
>
>
|

>
>
>
>
>
>
>
>
>
>
>
>




>
>









>
>
>
>







367
368
369
370
371
372
373

374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

	files = [OFMutableArray array];

#ifndef _WIN32
	DIR *dir;


	encoding = [OFString nativeOSEncoding];

	if ((dir = opendir([path cStringWithEncoding: encoding])) == NULL)
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];

# if !defined(HAVE_READDIR_R) && defined(OF_HAVE_THREADS)
	if (!of_mutex_lock(&mutex))
		@throw [OFLockFailedException exception];
# endif

	@try {
		for (;;) {
			struct dirent *dirent;
# ifdef HAVE_READDIR_R
			struct dirent buffer;
# endif
			void *pool;
			OFString *file;

# ifdef HAVE_READDIR_R
			if (readdir_r(dir, &buffer, &dirent) != 0)
				@throw [OFReadFailedException
				    exceptionWithObject: self
					requestedLength: 0];
# else
			dirent = readdir(dir);
# endif

			if (dirent == NULL)
				break;

			if (strcmp(dirent->d_name, ".") == 0 ||
			    strcmp(dirent->d_name, "..") == 0)
				continue;

			pool = objc_autoreleasePoolPush();

			file = [OFString stringWithCString: dirent->d_name
						  encoding: encoding];
			[files addObject: file];

			objc_autoreleasePoolPop(pool);
		}
	} @finally {
		closedir(dir);
# if !defined(HAVE_READDIR_R) && defined(OF_HAVE_THREADS)
		if (!of_mutex_unlock(&mutex))
			@throw [OFUnlockFailedException exception];
# endif
	}
#else
	void *pool = objc_autoreleasePoolPush();
	HANDLE handle;
	WIN32_FIND_DATAW fd;

	path = [path stringByAppendingString: @"\\*"];