FileURI.st
changeset 1271 ca2e206e7c7f
parent 1268 48b43aebf125
child 1284 57550436b55b
--- a/FileURI.st	Mon Jul 14 00:40:50 2003 +0200
+++ b/FileURI.st	Mon Jul 14 00:50:08 2003 +0200
@@ -79,7 +79,7 @@
 
 !FileURI methodsFor:'queries'!
 
-pathExists
+exists
 
     ^ self asFilename exists
 ! !
@@ -133,80 +133,88 @@
      - a collection with a stream on a single file,
      - or a collection with streams on a directorie's files, but not recursive"
 
-    self readStreamsDo:aBlock thenRemove:false.
+    self readStreamsDo:aBlock renameBlock:nil.
 !
 
-readStreamsDo:aBlock thenRemove:doRemoveSource
+readStreamsDo:aBlock renameBlock:renameBlock
     "evaluate the block with a Collection of streams as first argument
      and a dictionary containing attributes as second argument,
      - a collection with a stream on a single file,
-     - or a collection with streams on a directorie's files, but not recursive"
+     - or a collection with streams on a directories files, but not recursive"
 
-    |attributes fn files list dirPath|
+    |attributes fn files list baseName|
 
-    fn := self path asFilename.
+    fn := self asFilename.
     files := OrderedCollection new.
     list := OrderedCollection new.
     attributes := self class attributes.
 
     fn isDirectory ifTrue:[
-        dirPath := fn pathName.
         attributes at:#requestDirectory put:true.
-        (DirectoryContents directoryNamed:dirPath) filesDo:[:aFile|
-            files add:aFile
+        fn directoryContentsAsFilenamesDo:[:eachFilename|
+            eachFilename isDirectory ifFalse:[
+                files add:eachFilename
+            ].
         ].
     ] ifFalse:[
-        attributes at:#requestDirectory put:false.
-        files add:fn.
-    ].
-
-    fn isDirectory ifFalse:[ |bName|
-        bName := fn baseName.
-        (bName startsWith:'*') ifTrue:[
-            files removeAll.
-            fn := fn directory.
-            dirPath := fn pathName.
+        baseName := fn baseName.
+        (baseName includesAny:'*?[]') ifTrue:[ |directoryName|
             attributes at:#requestDirectory put:true.
-            (DirectoryContents directoryNamed:dirPath) filesDo:[:aFile|
-                files add:aFile
+            directoryName := fn directory.
+            directoryName directoryContentsDo:[:eachFilenameString|
+                (baseName match:eachFilenameString) ifTrue:[ |filename|
+                    filename := directoryName construct:eachFilenameString.
+                    filename isDirectory ifFalse:[
+                        files add:(directoryName construct:eachFilenameString).
+                    ].
+                ].
             ].
-        ].
-        (bName startsWith:'*.') ifTrue:[ |rest|
-            rest := bName restAfter:$*.
-            (rest includesString:'*') ifTrue:[
-                self error:'can''t resolve path:', self printString
-            ].
-            files := files select:[:aFile| aFile pathName endsWith:rest ]
+        ] ifFalse:[
+            attributes at:#requestDirectory put:false.
+            files add:fn.
         ].
     ].
 
-    files do:[:aFile| |baseName stream|
+    files do:[:eachFilename| |baseName stream|
         [
-            baseName := aFile baseName.
-            attributes at:#fileSize put:(aFile fileSize).
+            baseName := eachFilename baseName.
+            attributes at:#fileSize put:eachFilename fileSize.
             attributes at:#baseName put:baseName.  
-            (self pathSegements includes:baseName) ifTrue:[
+            (self pathSegments includes:baseName) ifTrue:[
                 attributes at:#uriInfo put:self.  
             ] ifFalse:[ |uri col|
                 uri := self copy.
-                col := self pathSegements copy.
+                col := self pathSegments copy.
                 col removeLast.
                 col add:baseName.
-                uri pathSegements:col.
+                uri pathSegments:col.
                 attributes at:#uriInfo put:uri.  
             ].
-            stream := aFile readStream.
+            stream := eachFilename readStream.
             aBlock value:stream value:attributes.
         ] ensure:[
             stream notNil ifTrue:[stream close]
         ].
-        doRemoveSource == true ifTrue:[
-            aFile remove
+
+        renameBlock notNil ifTrue:[ |renameFilenameString|
+            renameFilenameString := renameBlock value:eachFilename pathName.
+            renameFilenameString asFilename exists ifTrue:[
+                renameFilenameString := renameFilenameString, '.', 
+                        (AbsoluteTime now printStringFormat:'%(year)%(mon)%(day)%h%m%s').
+            ].
+            eachFilename moveTo:renameFilenameString.
         ].
     ].
 
     "
-        (URI fromString:'file:/home/tm/tmp') 
+        (URI fromString:'file:~/test/out') 
+            readStreamsDo:[:stream :attributes | 
+                Transcript showCR:(attributes at:#baseName).
+                Transcript showCR:(attributes at:#fileSize).
+                Transcript showCR:(attributes at:#requestDirectory).
+                Transcript showCR:(attributes at:#uriInfo).
+            ].
+        (URI fromString:'file:~/test/out/*1') 
             readStreamsDo:[:stream :attributes | 
                 Transcript showCR:(attributes at:#baseName).
                 Transcript showCR:(attributes at:#fileSize).
@@ -258,10 +266,12 @@
     |stream fileName toFileName|
 
     fileName := self asFilename.
+    toFileName := fileName.
     doAtomic ifTrue:[
-        toFileName := fileName directory construct:'.transferFile'.
-    ] ifFalse:[
-        toFileName := fileName.
+        fileName isDirectory ifFalse:[
+            toFileName := fileName directory.
+        ].
+        toFileName := toFileName construct:'.transferFile'.
     ].
     [
         Stream streamErrorSignal handle:[:ex|
@@ -274,10 +284,11 @@
             stream := toFileName writeStream.
         ].
         aBlock value:stream value:self class attributes.
+        stream close.
         doAtomic ifTrue:[
             toFileName moveTo:fileName.
         ]
-    ] ensure:[
+    ] ifCurtailed:[
         stream notNil ifTrue:[stream close]
     ].
 
@@ -293,5 +304,5 @@
 !FileURI class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.7 2003-07-11 12:46:51 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.8 2003-07-13 22:50:00 stefan Exp $'
 ! !