ObjFW  Check-in [36434923ad]

Overview
Comment:Don't depend on OFMutex in OFFile.

+[load] creates 3 instances of OFFile and thus +[initialize] is also
called. This causes problems if OFMutex has not been registered with
the runtime yet. It had been fixed by changing the order of .o files,
but this is too fragile and thus it has been changed to use of_mutex_t.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 36434923ad0d0b4550d8c34138469c2ea9cca0a1b89bab174ef192c08b73a729
User & Date: js on 2011-10-07 11:19:58
Other Links: manifest | tags
Context
2011-10-09
13:05
Adjust objfw-config and objfw-compile to buildsys changes. check-in: 36464567d2 user: js tags: trunk
2011-10-07
11:19
Don't depend on OFMutex in OFFile. check-in: 36434923ad user: js tags: trunk
2011-10-06
22:39
Fix a strange bug that was introduced by newer ld versions.
The order of .o files is suddenly very important for +[load].
check-in: bb06d0f275 user: js tags: trunk
Changes

Modified src/Makefile from [e90d73cf15] to [995c3eb96d].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
include ../extra.mk

SUBDIRS = exceptions

SHARED_LIB = ${OBJFW_SHARED_LIB}
STATIC_LIB = ${OBJFW_STATIC_LIB}
LIB_MAJOR = ${OBJFW_LIB_MAJOR}
LIB_MINOR = ${OBJFW_LIB_MINOR}

# OFThread needs to be before OFFile, as OFFile requires OFMutex in +[load].
# Some new ld versions are very picky about this, while it always worked with
# older versions.
SRCS = OFApplication.m			\
       OFArray.m			\
       OFAutoreleasePool.m		\
       OFBlock.m			\
       OFConstantString.m		\
       OFCountedSet.m			\
       OFDataArray.m			\
       OFDataArray+Hashing.m		\
       OFDate.m				\
       OFDictionary.m			\
       OFDoubleMatrix.m			\
       OFDoubleVector.m			\
       OFEnumerator.m			\
       ${OFTHREAD_M}			\
       OFFile.m				\
       OFFloatMatrix.m			\
       OFFloatVector.m			\
       OFHash.m				\
       OFHTTPRequest.m			\
       OFIntrospection.m		\
       OFList.m				\









<
<
<













<







1
2
3
4
5
6
7
8
9



10
11
12
13
14
15
16
17
18
19
20
21
22

23
24
25
26
27
28
29
include ../extra.mk

SUBDIRS = exceptions

SHARED_LIB = ${OBJFW_SHARED_LIB}
STATIC_LIB = ${OBJFW_STATIC_LIB}
LIB_MAJOR = ${OBJFW_LIB_MAJOR}
LIB_MINOR = ${OBJFW_LIB_MINOR}




SRCS = OFApplication.m			\
       OFArray.m			\
       OFAutoreleasePool.m		\
       OFBlock.m			\
       OFConstantString.m		\
       OFCountedSet.m			\
       OFDataArray.m			\
       OFDataArray+Hashing.m		\
       OFDate.m				\
       OFDictionary.m			\
       OFDoubleMatrix.m			\
       OFDoubleVector.m			\
       OFEnumerator.m			\

       OFFile.m				\
       OFFloatMatrix.m			\
       OFFloatVector.m			\
       OFHash.m				\
       OFHTTPRequest.m			\
       OFIntrospection.m		\
       OFList.m				\
51
52
53
54
55
56
57

58
59
60
61
62
63
64
       OFString.m			\
       OFString+Hashing.m		\
       OFString+Serialization.m		\
       OFString+URLEncoding.m		\
       OFString+XMLEscaping.m		\
       OFString+XMLUnescaping.m		\
       OFTCPSocket.m			\

       OFURL.m				\
       OFXMLAttribute.m			\
       OFXMLCDATA.m			\
       OFXMLCharacters.m		\
       OFXMLComment.m			\
       OFXMLElement.m			\
       OFXMLElement+Serialization.m	\







>







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
       OFString.m			\
       OFString+Hashing.m		\
       OFString+Serialization.m		\
       OFString+URLEncoding.m		\
       OFString+XMLEscaping.m		\
       OFString+XMLUnescaping.m		\
       OFTCPSocket.m			\
       ${OFTHREAD_M}			\
       OFURL.m				\
       OFXMLAttribute.m			\
       OFXMLCDATA.m			\
       OFXMLCharacters.m		\
       OFXMLComment.m			\
       OFXMLElement.m			\
       OFXMLElement+Serialization.m	\

Modified src/OFFile.m from [d95eac9ee4] to [ec14d28310].

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

54
55


56
57
58
59
60
61
62
# include <grp.h>
#endif

#import "OFFile.h"
#import "OFString.h"
#import "OFArray.h"
#ifdef OF_THREADS
# import "OFThread.h"
#endif
#import "OFDate.h"
#import "OFApplication.h"
#import "OFAutoreleasePool.h"

#import "OFChangeDirectoryFailedException.h"
#import "OFChangeFileModeFailedException.h"
#import "OFChangeFileOwnerFailedException.h"
#import "OFCreateDirectoryFailedException.h"
#import "OFDeleteDirectoryFailedException.h"
#import "OFDeleteFileFailedException.h"

#import "OFInvalidArgumentException.h"
#import "OFLinkFailedException.h"


#import "OFNotImplementedException.h"
#import "OFOpenFileFailedException.h"
#import "OFOutOfMemoryException.h"
#import "OFReadFailedException.h"
#import "OFRenameFileFailedException.h"
#import "OFSeekFailedException.h"
#import "OFSymlinkFailedException.h"







|











>


>
>







35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# include <grp.h>
#endif

#import "OFFile.h"
#import "OFString.h"
#import "OFArray.h"
#ifdef OF_THREADS
# import "threading.h"
#endif
#import "OFDate.h"
#import "OFApplication.h"
#import "OFAutoreleasePool.h"

#import "OFChangeDirectoryFailedException.h"
#import "OFChangeFileModeFailedException.h"
#import "OFChangeFileOwnerFailedException.h"
#import "OFCreateDirectoryFailedException.h"
#import "OFDeleteDirectoryFailedException.h"
#import "OFDeleteFileFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFLinkFailedException.h"
#import "OFMutexLockFailedException.h"
#import "OFMutexUnlockFailedException.h"
#import "OFNotImplementedException.h"
#import "OFOpenFileFailedException.h"
#import "OFOutOfMemoryException.h"
#import "OFReadFailedException.h"
#import "OFRenameFileFailedException.h"
#import "OFSeekFailedException.h"
#import "OFSymlinkFailedException.h"
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#define DIR_MODE DEFAULT_MODE | S_IXUSR | S_IXGRP | S_IXOTH

OFStream *of_stdin = nil;
OFStream *of_stdout = nil;
OFStream *of_stderr = nil;

#if defined(OF_THREADS) && !defined(_WIN32)
static OFMutex *mutex;
#endif

static int parse_mode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;
	if (!strcmp(mode, "rb"))







|







92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#define DIR_MODE DEFAULT_MODE | S_IXUSR | S_IXGRP | S_IXOTH

OFStream *of_stdin = nil;
OFStream *of_stdout = nil;
OFStream *of_stderr = nil;

#if defined(OF_THREADS) && !defined(_WIN32)
static of_mutex_t mutex;
#endif

static int parse_mode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;
	if (!strcmp(mode, "rb"))
159
160
161
162
163
164
165
166

167



168
169
170
171
172
173
174
	of_stdout = [[OFFileSingleton alloc] initWithFileDescriptor: 1];
	of_stderr = [[OFFileSingleton alloc] initWithFileDescriptor: 2];
}

#if defined(OF_THREADS) && !defined(_WIN32)
+ (void)initialize
{
	if (self == [OFFile class])

		mutex = [[OFMutex alloc] init];



}
#endif

+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode
{
	return [[[self alloc] initWithPath: path







|
>
|
>
>
>







162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
	of_stdout = [[OFFileSingleton alloc] initWithFileDescriptor: 1];
	of_stderr = [[OFFileSingleton alloc] initWithFileDescriptor: 2];
}

#if defined(OF_THREADS) && !defined(_WIN32)
+ (void)initialize
{
	if (self != [OFFile class])
		return;

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

+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode
{
	return [[[self alloc] initWithPath: path
385
386
387
388
389
390
391


392
393
394
395
396
397
398
399
	gid_t gid = -1;

	if (owner == nil && group == nil)
		@throw [OFInvalidArgumentException exceptionWithClass: self
							     selector: _cmd];

# ifdef OF_THREADS


	[mutex lock];

	@try {
# endif
		if (owner != nil) {
			struct passwd *passwd;

			if ((passwd = getpwnam([owner cStringWithEncoding:







>
>
|







392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
	gid_t gid = -1;

	if (owner == nil && group == nil)
		@throw [OFInvalidArgumentException exceptionWithClass: self
							     selector: _cmd];

# ifdef OF_THREADS
	if (!of_mutex_lock(&mutex))
		@throw [OFMutexLockFailedException exceptionWithClass: self
								mutex: nil];

	@try {
# endif
		if (owner != nil) {
			struct passwd *passwd;

			if ((passwd = getpwnam([owner cStringWithEncoding:
418
419
420
421
422
423
424



425
426
427
428
429
430
431
432
						 owner: owner
						 group: group];

			gid = group_->gr_gid;
		}
# ifdef OF_THREADS
	} @finally {



		[mutex unlock];
	}
# endif

	if (chown([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    uid, gid))
		@throw [OFChangeFileOwnerFailedException
		    exceptionWithClass: self







>
>
>
|







427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
						 owner: owner
						 group: group];

			gid = group_->gr_gid;
		}
# ifdef OF_THREADS
	} @finally {
		if (!of_mutex_unlock(&mutex))
			@throw [OFMutexUnlockFailedException
			    exceptionWithClass: self
					 mutex: nil];
	}
# endif

	if (chown([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    uid, gid))
		@throw [OFChangeFileOwnerFailedException
		    exceptionWithClass: self