class: ExternalStream
authorStefan Vogel <sv@exept.de>
Thu, 03 Apr 2014 17:31:38 +0200
changeset 16300 277c0cb1ecbc
parent 16299 1ca7a21475c9
child 16301 9eb74aad5c2b
class: ExternalStream changed: #close #closeFile
ExternalStream.st
--- a/ExternalStream.st	Thu Apr 03 17:30:38 2014 +0200
+++ b/ExternalStream.st	Thu Apr 03 17:31:38 2014 +0200
@@ -9,7 +9,6 @@
  other person.  No title to or ownership of the software is
  hereby transferred.
 "
-
 "{ Package: 'stx:libbasic' }"
 
 ReadWriteStream subclass:#ExternalStream
@@ -2230,16 +2229,13 @@
 !ExternalStream methodsFor:'closing'!
 
 close
-    "close the stream - tell operating system"
+    "close the stream. 
+     No error of the stream is not open."
 
     self isOpen ifTrue:[
-	Lobby unregister:self.
-	PrimitiveFailure handle:[:ex |
-	    ('ExternalStream [info] error in close cought (%1).' bindWith:self printString) errorPrintCR.
-	] do:[
-	    self closeFile.
-	].
-    ]
+        Lobby unregister:self.
+        self closeFile.
+    ].
 !
 
 shutDown
@@ -4407,7 +4403,9 @@
 
 clearEOF
     hitEOF := false
-!
+! !
+
+!ExternalStream protectedMethodsFor:'private'!
 
 closeFile
     "low level close - may be redefined in subclasses
@@ -4416,60 +4414,73 @@
     |fp error|
 
 %{
-    if ((__INST(handleType) == nil)
+    if ((__INST(handleType) == nil) 
      || (__INST(handleType) == @symbol(filePointer))
      || (__INST(handleType) == @symbol(socketFilePointer))
      || (__INST(handleType) == @symbol(pipeFilePointer))) {
-	if ((fp = __INST(handle)) != nil) {
-	    FILEPOINTER f;
-	    int rslt;
-
-	    f = __FILEVal(fp);
-	    if (@global(FileOpenTrace) == true) {
-		fprintf(stderr, "fclose [ExternalStream] %"_lx_"\n", (INT)f);
-	    }
+        if ((fp = __INST(handle)) == nil) {
+            error = @symbol(errorNotOpen);
+        } else {
+            FILEPOINTER f = __FILEVal(fp);
+            int rslt;
+
+            // whether the fclose() will be successful or not - the handle is invalid now!
+            __INST(handle) = nil;
+
+            if (@global(FileOpenTrace) == true) {
+                fprintf(stderr, "fclose [ExternalStream] %"_lx_"\n", (INT)f);
+            }
 #ifdef WIN32
-	    if (__INST(mode) != @symbol(readonly)) {
-		// do a fflush() first, so that fclose() doesn't block
-		// we suspect, that EINTR causes problems in fclose()
-		do {
-		    __threadErrno = 0;
-		    rslt = __STX_C_CALL1("fflush", fflush, f);
-		} while((rslt < 0) && (__threadErrno == EINTR));
-	    }
-	    do {
-		__threadErrno = 0;
-		rslt = __STX_C_NOINT_CALL1("fclose", fclose, f);
-	    } while((rslt < 0) && (__threadErrno == EINTR));
+            if (__INST(mode) != @symbol(readonly)) {
+                // do a fflush() first, so that fclose() doesn't block
+                // we suspect, that EINTR causes problems in fclose()
+                do {
+                    __threadErrno = 0;
+                    rslt = __STX_C_CALL1("fflush", fflush, f);
+                } while((rslt < 0) && (__threadErrno == EINTR));
+            }
+            do {
+                __threadErrno = 0;
+                rslt = __STX_C_NOINT_CALL1("fclose", fclose, f);
+            } while((rslt < 0) && (__threadErrno == EINTR));
 #else
-	    __BEGIN_INTERRUPTABLE__
-	    rslt = fclose(f);
-	    __END_INTERRUPTABLE__
+            __BEGIN_INTERRUPTABLE__
+            rslt = fclose(f);
+            __END_INTERRUPTABLE__
 #endif
-	    if (rslt < 0) {
-		error = __mkSmallInteger(__threadErrno);
-		goto out;
-	    }
-	    __INST(handle) = nil;
-	}
-	RETURN (self);
+            if (rslt < 0) {
+                error = __mkSmallInteger(__threadErrno);
+                goto out;
+            }
+        }
+        RETURN (self);
     }
 out:;
 %}.
 
     error notNil ifTrue:[
-	self primitiveFailed.
-	^ self.
+        error == #errorNotOpen ifTrue:[
+            self errorNotOpen.
+        ].
+        error isInteger ifTrue:[
+            lastErrorNumber := error.
+            self writeError:error.
+            ^ self.
+        ].
+        self primitiveFailed:error.
+        ^ self.
     ].
 
     "/ fallback for rel5
 
     fp := handle.
     fp notNil ifTrue:[
-	handle := nil.
-	self closeFile:fp
+        handle := nil.
+        self closeFile:fp
     ]
-!
+! !
+
+!ExternalStream methodsFor:'private'!
 
 closeFile:handle
     "for rel5 only"
@@ -5947,11 +5958,11 @@
 !ExternalStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.381 2014-04-03 12:58:08 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.382 2014-04-03 15:31:38 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.381 2014-04-03 12:58:08 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.382 2014-04-03 15:31:38 stefan Exp $'
 ! !