class: Filename
authorStefan Vogel <sv@exept.de>
Wed, 21 Jan 2015 20:03:50 +0100
changeset 17310 ebac8ce814f7
parent 17309 cbe100c27478
child 17311 bafd05547c4e
class: Filename comment/format in: #directoriesDo: #directoryContentsAsFilenamesDo: Raise OpenError for non-existent of non-accessible directories
Filename.st
--- a/Filename.st	Wed Jan 21 19:44:21 2015 +0100
+++ b/Filename.st	Wed Jan 21 20:03:50 2015 +0100
@@ -11,6 +11,8 @@
 "
 "{ Package: 'stx:libbasic' }"
 
+"{ NameSpace: Smalltalk }"
+
 Object subclass:#Filename
 	instanceVariableNames:'nameString'
 	classVariableNames:'TempDirectory DefaultTempDirectory ConcreteClass'
@@ -2122,8 +2124,16 @@
 directories
     "return a collection of directories contained in the directory represented by the receiver."
 
-    ^ self directoryContentsAsFilenames 
-        select:[:eachFileOrDirectory | eachFileOrDirectory isDirectory]
+    |collection|
+
+    collection := OrderedCollection new.
+    self directoryContentsAsFilenamesDo:[:eachFileOrDirectory | 
+        eachFileOrDirectory isDirectory ifTrue:[
+            collection add:eachFileOrDirectory.
+        ].
+    ].
+
+    ^ collection
 
     "
      '.' asFilename directories.
@@ -2138,8 +2148,7 @@
      The enumerations order is undefined - i.e. usually NOT sorted by
      filenames (but by creation time - on some systems).
      This excludes entries for '.' or '..'.
-     NoOp for non-existing directories; however, this behavior
-     may be changed in the near future, to raise an exception instead.
+     OpenError is raised if I represent a non-existant or non-readable directories.
      So users of this method better test for existing directory before."
 
     self directoryContentsAsFilenamesDo:[:eachFileOrDirectory |
@@ -2159,8 +2168,7 @@
      The enumerations order is undefined - i.e. usually NOT sorted by
      filenames (but by creation time - on some systems).
      This excludes entries for '.' or '..'.
-     NoOp for non-existing directories; however, this behavior
-     may be changed in the near future, to raise an exception instead.
+     An OpenError exception is raised it the directory does not exist or is not readable.
      So users of this method better test for existing directory before.
      Notice: this enumerates fileName objects; see also
      #directoryContentsDo:, which enumerates strings."
@@ -2183,34 +2191,36 @@
      The enumerations order is undefined - i.e. usually NOT sorted by
      filenames (but by creation time - on some systems).
      This excludes entries for '.' or '..'.
-     An exception is raised it the directory does not exist..
+     An OpenError exception is raised it the directory does not exist or is not readable.
      So users of this method better test for existing directory before.
      Notice: this enumerates strings; see also
      #directoryContentsAsFilenamesDo:, which enumerates fileName objects."
 
-    |s fn|
-
-    FileStream openErrorSignal 
-        handle:[:ex |] 
-        do:[
-            s := DirectoryStream directoryNamed:(self osNameForDirectoryContents).
+    |s|
+
+    s := DirectoryStream directoryNamed:self osNameForDirectoryContents.
+    "check for nil, in order to allow to proceed from an OpenError"
+    s notNil ifTrue:[
+        [
+            [s atEnd] whileFalse:[
+                |fn|
+
+                fn := s nextLine.
+                (fn ~= '.' and:[fn ~= '..']) ifTrue:[        
+                    aBlock value:fn
+                ].
+            ].
+        ] ensure:[
+            s close.
         ].
-    s isNil ifTrue:[^ self].
-
-    [
-        [s atEnd] whileFalse:[
-            fn := s nextLine.
-            ((fn ~= '.') and:[fn ~= '..']) ifTrue:[        
-                aBlock value:fn
-            ]
-        ]
-    ] ensure:[
-        s close
     ].
 
     "
      '.' asFilename directoryContentsDo:[:fn | Transcript showCR:fn].
      'doeSnotExIST' asFilename directoryContentsDo:[:fn | Transcript showCR:fn].
+     [
+        'doeSnotExIST' asFilename directoryContentsDo:[:fn | Transcript showCR:fn].
+     ] on:OpenError do:[:ex| ex proceed]
     "
 
     "Modified: / 18.9.1997 / 18:42:23 / stefan"
@@ -2220,8 +2230,15 @@
 files
     "return a collection of files contained in the directory represented by the receiver."
 
-    ^ self directoryContentsAsFilenames 
-        reject:[:eachFileOrDirectory | eachFileOrDirectory isDirectory ]
+    |collection|
+
+    collection := OrderedCollection new.
+    self directoryContentsAsFilenamesDo:[:eachFileName | 
+        eachFileName isRegularFile ifTrue:[
+            collection add:eachFileName
+        ].
+    ].
+    ^ collection.
 
     "
      '.' asFilename files.
@@ -2233,9 +2250,9 @@
 filesDo:aBlock
     "evaluate aBlock for all files contained in the directory represented by the receiver."
 
-    ^ self directoryContentsAsFilenames 
-        do:[:eachFileOrDirectory | 
-            eachFileOrDirectory isDirectory ifFalse:[ aBlock value: eachFileOrDirectory]].
+    ^ self directoryContentsAsFilenamesDo:[:eachFileOrDirectory | 
+        eachFileOrDirectory isRegularFile ifTrue:[ aBlock value: eachFileOrDirectory].
+    ].
 
     "
      '.' asFilename filesDo:[:f | Transcript showCR:f].
@@ -2330,17 +2347,18 @@
      The enumerations order within a directory is undefined - i.e. usually NOT sorted by
      filenames (but by creation time - on some systems).
      This excludes entries for '.' or '..'.
-     NoOp for non-existing directories; however, this behavior
-     may be changed in the near future, to raise an exception instead.
+     OpenError is raised if the name I represent does not exist or is not readable.
      So users of this method better test for existing directory before."
 
     self isDirectory ifTrue:[
         aBlock value:self.
-        self allDirectoriesDo:aBlock
     ].
+    self allDirectoriesDo:aBlock.
 
     "
      '.' asFilename withAllDirectoriesDo:[:fn | Transcript showCR:fn name].
+     'IdoNOTexist' asFilename withAllDirectoriesDo:[:fn | Transcript showCR:fn name].
+     '/etc/hosts' asFilename withAllDirectoriesDo:[:fn | Transcript showCR:fn name].
     "
 ! !
 
@@ -4773,19 +4791,21 @@
             |first longest|
 
             first := nil.
-            self directoryContentsDo:[:fileName |
-                ((fileName ~= '.') and:[fileName ~= parentString]) ifTrue:[
-                    matching add:fileName.    
-                    first isNil ifTrue:[
-                        first := longest := fileName.
-                    ] ifFalse:[
-                        "/ more than one file
-                        longest := longest commonPrefixWith:fileName ignoreCase:caseless.
-                        longest isEmpty ifTrue:[ 
-                            ^ #() 
-                        ].
+            OpenError catch:[
+                self directoryContentsDo:[:fileName |
+                    ((fileName ~= '.') and:[fileName ~= parentString]) ifTrue:[
+                        matching add:fileName.    
+                        first isNil ifTrue:[
+                            first := longest := fileName.
+                        ] ifFalse:[
+                            "/ more than one file
+                            longest := longest commonPrefixWith:fileName ignoreCase:caseless.
+                            longest isEmpty ifTrue:[ 
+                                ^ #() 
+                            ].
+                        ]
                     ]
-                ]
+                ].
             ].
             longest notNil ifTrue:[
                 nameString := (self constructString:longest).
@@ -6120,11 +6140,11 @@
 !Filename class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.433 2014-12-18 14:49:57 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.434 2015-01-21 19:03:50 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.433 2014-12-18 14:49:57 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.434 2015-01-21 19:03:50 stefan Exp $'
 ! !