Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -39,11 +39,11 @@ /** * \brief A class which provides functions to read, write and manipulate files. */ @interface OFFile: OFSeekableStream { - int fileDescriptor; + int fd; BOOL closable; BOOL atEndOfStream; } /** Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -531,13 +531,12 @@ #if !defined(_WIN32) && !defined(_PSP) if (!override) { struct stat s; - if (fstat(sourceFile->fileDescriptor, &s) == 0) - fchmod(destinationFile->fileDescriptor, - s.st_mode); + if (fstat(sourceFile->fd, &s) == 0) + fchmod(destinationFile->fd, s.st_mode); } #else (void)override; #endif } @finally { @@ -659,11 +658,11 @@ OF_STRING_ENCODING_NATIVE])) == -1) @throw [OFInvalidArgumentException exceptionWithClass: [self class] selector: _cmd]; - if ((fileDescriptor = open([path cStringWithEncoding: + if ((fd = open([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1) @throw [OFOpenFileFailedException exceptionWithClass: [self class] path: path mode: mode]; @@ -675,22 +674,22 @@ } return self; } -- initWithFileDescriptor: (int)fileDescriptor_ +- initWithFileDescriptor: (int)fileDescriptor { self = [super init]; - fileDescriptor = fileDescriptor_; + fd = fileDescriptor; return self; } - (BOOL)_isAtEndOfStream { - if (fileDescriptor == -1) + if (fd == -1) return YES; return atEndOfStream; } @@ -697,12 +696,11 @@ - (size_t)_readIntoBuffer: (void*)buffer length: (size_t)length { ssize_t ret; - if (fileDescriptor == -1 || atEndOfStream || - (ret = read(fileDescriptor, buffer, length)) < 0) + if (fd == -1 || atEndOfStream || (ret = read(fd, buffer, length)) < 0) @throw [OFReadFailedException exceptionWithClass: [self class] stream: self requestedLength: length]; if (ret == 0) @@ -712,20 +710,19 @@ } - (void)_writeBuffer: (const void*)buffer length: (size_t)length { - if (fileDescriptor == -1 || atEndOfStream || - write(fileDescriptor, buffer, length) < length) + if (fd == -1 || atEndOfStream || write(fd, buffer, length) < length) @throw [OFWriteFailedException exceptionWithClass: [self class] stream: self requestedLength: length]; } - (void)_seekToOffset: (off_t)offset { - if (lseek(fileDescriptor, offset, SEEK_SET) == -1) + if (lseek(fd, offset, SEEK_SET) == -1) @throw [OFSeekFailedException exceptionWithClass: [self class] stream: self offset: offset whence: SEEK_SET]; } @@ -732,11 +729,11 @@ - (off_t)_seekForwardWithOffset: (off_t)offset { off_t ret; - if ((ret = lseek(fileDescriptor, offset, SEEK_CUR)) == -1) + if ((ret = lseek(fd, offset, SEEK_CUR)) == -1) @throw [OFSeekFailedException exceptionWithClass: [self class] stream: self offset: offset whence: SEEK_CUR]; @@ -745,36 +742,41 @@ - (off_t)_seekToOffsetRelativeToEnd: (off_t)offset { off_t ret; - if ((ret = lseek(fileDescriptor, offset, SEEK_END)) == -1) + if ((ret = lseek(fd, offset, SEEK_END)) == -1) @throw [OFSeekFailedException exceptionWithClass: [self class] stream: self offset: offset whence: SEEK_END]; return ret; } -- (int)fileDescriptor +- (int)fileDescriptorForReading +{ + return fd; +} + +- (int)fileDescriptorForWriting { - return fileDescriptor; + return fd; } - (void)close { - if (fileDescriptor != -1) - close(fileDescriptor); + if (fd != -1) + close(fd); - fileDescriptor = -1; + fd = -1; } - (void)dealloc { - if (closable && fileDescriptor != -1) - close(fileDescriptor); + if (closable && fd != -1) + close(fd); [super dealloc]; } @end Index: src/OFProcess.m ================================================================== --- src/OFProcess.m +++ src/OFProcess.m @@ -292,15 +292,29 @@ [self close]; [super dealloc]; } -/* - * FIXME: Add -[fileDescriptor]. The problem is that we have two FDs, which is - * not yet supported by OFStreamObserver. This has to be split into one - * FD for reading and one for writing. - */ +- (int)fileDescriptorForReading +{ +#ifndef _WIN32 + return readPipe[0]; +#else + @throw [OFNotImplementedException exceptionWithClass: [self class] + selector: _cmd]; +#endif +} + +- (int)fileDescriptorForWRiting +{ +#ifndef _WIN32 + return writePipe[1]; +#else + @throw [OFNotImplementedException exceptionWithClass: [self class] + selector: _cmd]; +#endif +} - (void)closeForWriting { #ifndef _WIN32 if (writePipe[1] != -1) Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -808,15 +808,22 @@ * \param enable Whether the stream should be blocking */ - (void)setBlocking: (BOOL)enable; /** - * \brief Returns the file descriptor for the stream. + * \brief Returns the file descriptor for the read end of the stream. + * + * \return The file descriptor for the read end of the stream + */ +- (int)fileDescriptorForReading; + +/** + * \brief Returns the file descriptor for the write end of the stream. * - * \return The file descriptor for the stream + * \return The file descriptor for the write end of the stream */ -- (int)fileDescriptor; +- (int)fileDescriptorForWriting; /** * \brief Closes the stream. */ - (void)close; Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -1362,35 +1362,46 @@ } - (void)setBlocking: (BOOL)enable { #ifndef _WIN32 - int flags; + int readFlags, writeFlags; - blocking = enable; + readFlags = fcntl([self fileDescriptorForReading], F_GETFL); + writeFlags = fcntl([self fileDescriptorForWriting], F_GETFL); - if ((flags = fcntl([self fileDescriptor], F_GETFL)) == -1) + if (readFlags == -1 || writeFlags == -1) @throw [OFSetOptionFailedException exceptionWithClass: [self class] stream: self]; - if (enable) - flags &= ~O_NONBLOCK; - else - flags |= O_NONBLOCK; + if (enable) { + readFlags &= ~O_NONBLOCK; + writeFlags &= ~O_NONBLOCK; + } else { + readFlags |= O_NONBLOCK; + writeFlags |= O_NONBLOCK; + } - if (fcntl([self fileDescriptor], F_SETFL, flags) == -1) + if (fcntl([self fileDescriptorForReading], F_SETFL, readFlags) == -1 || + fcntl([self fileDescriptorForWriting], F_SETFL, writeFlags) == -1) @throw [OFSetOptionFailedException exceptionWithClass: [self class] stream: self]; #else @throw [OFNotImplementedException exceptionWithClass: [self class] selector: _cmd]; #endif } -- (int)fileDescriptor +- (int)fileDescriptorForReading +{ + @throw [OFNotImplementedException exceptionWithClass: [self class] + selector: _cmd]; +} + +- (int)fileDescriptorForWriting { @throw [OFNotImplementedException exceptionWithClass: [self class] selector: _cmd]; } Index: src/OFStreamObserver.m ================================================================== --- src/OFStreamObserver.m +++ src/OFStreamObserver.m @@ -187,11 +187,11 @@ - (void)addStreamForReading: (OFStream*)stream { [mutex lock]; @try { int qi = QUEUE_ADD | QUEUE_READ; - int fd = [stream fileDescriptor]; + int fd = [stream fileDescriptorForReading]; [queue addObject: stream]; [queueInfo addItem: &qi]; [queueFDs addItem: &fd]; } @finally { @@ -204,11 +204,11 @@ - (void)addStreamForWriting: (OFStream*)stream { [mutex lock]; @try { int qi = QUEUE_ADD | QUEUE_WRITE; - int fd = [stream fileDescriptor]; + int fd = [stream fileDescriptorForWriting]; [queue addObject: stream]; [queueInfo addItem: &qi]; [queueFDs addItem: &fd]; } @finally { @@ -221,11 +221,11 @@ - (void)removeStreamForReading: (OFStream*)stream { [mutex lock]; @try { int qi = QUEUE_REMOVE | QUEUE_READ; - int fd = [stream fileDescriptor]; + int fd = [stream fileDescriptorForReading]; [queue addObject: stream]; [queueInfo addItem: &qi]; [queueFDs addItem: &fd]; } @finally { @@ -243,11 +243,11 @@ - (void)removeStreamForWriting: (OFStream*)stream { [mutex lock]; @try { int qi = QUEUE_REMOVE | QUEUE_WRITE; - int fd = [stream fileDescriptor]; + int fd = [stream fileDescriptorForWriting]; [queue addObject: stream]; [queueInfo addItem: &qi]; [queueFDs addItem: &fd]; } @finally { Index: src/OFStreamObserver_select.m ================================================================== --- src/OFStreamObserver_select.m +++ src/OFStreamObserver_select.m @@ -116,47 +116,47 @@ objects = [readStreams objects]; count = [readStreams count]; for (i = 0; i < count; i++) { - int fileDescriptor = [objects[i] fileDescriptor]; + int fd = [objects[i] fileDescriptorForReading]; pool = objc_autoreleasePoolPush(); - if (FD_ISSET(fileDescriptor, &readFDs_)) { + if (FD_ISSET(fd, &readFDs_)) { realEvents++; [delegate streamIsReadyForReading: objects[i]]; } - if (FD_ISSET(fileDescriptor, &exceptFDs_)) { + if (FD_ISSET(fd, &exceptFDs_)) { realEvents++; [delegate streamDidReceiveException: objects[i]]; /* * Prevent calling it twice in case the FD is in both * sets. */ - FD_CLR(fileDescriptor, &exceptFDs_); + FD_CLR(fd, &exceptFDs_); } objc_autoreleasePoolPop(pool); } objects = [writeStreams objects]; count = [writeStreams count]; for (i = 0; i < count; i++) { - int fileDescriptor = [objects[i] fileDescriptor]; + int fd = [objects[i] fileDescriptorForWriting]; pool = objc_autoreleasePoolPush(); - if (FD_ISSET(fileDescriptor, &writeFDs_)) { + if (FD_ISSET(fd, &writeFDs_)) { realEvents++; [delegate streamIsReadyForWriting: objects[i]]; } - if (FD_ISSET(fileDescriptor, &exceptFDs_)) { + if (FD_ISSET(fd, &exceptFDs_)) { realEvents++; [delegate streamDidReceiveException: objects[i]]; } objc_autoreleasePoolPop(pool); Index: src/OFStreamSocket.m ================================================================== --- src/OFStreamSocket.m +++ src/OFStreamSocket.m @@ -144,11 +144,16 @@ exceptionWithClass: [self class] stream: self]; } #endif -- (int)fileDescriptor +- (int)fileDescriptorForReading +{ + return sock; +} + +- (int)fileDescriptorForWriting { return sock; } - (void)close