Filename.st
branchjv
changeset 18071 009cf668b0ed
parent 18066 89d51443ba6f
parent 15490 5089229237d8
child 18079 7b5afc0ad3d5
--- a/Filename.st	Mon Jul 01 22:14:20 2013 +0100
+++ b/Filename.st	Tue Jul 09 22:51:30 2013 +0100
@@ -13,8 +13,7 @@
 
 Object subclass:#Filename
 	instanceVariableNames:'nameString'
-	classVariableNames:'NextTempFilenameIndex TempDirectory DefaultTempDirectory
-		ConcreteClass'
+	classVariableNames:'TempDirectory DefaultTempDirectory ConcreteClass'
 	poolDictionaries:''
 	category:'System-Support'
 !
@@ -497,7 +496,9 @@
      If any of the environment variables ST_TMPDIR or TMPDIR is set, 
      its value defines the temp directory.
      Notice, that no file is created by this - only a unique name
-     is generated."
+     is generated.
+
+     DO NOT USE THIS FOR PLAIN FILES - IT IS UNSECURE use FileStream>>#newTemporary"
 
     ^ self newTemporaryIn:(self tempDirectory)
 
@@ -548,7 +549,9 @@
      Notice: only a unique filename object is created and returned - no physical
      file is created by this method (i.e. you have to send #writeStream or
      whatever to it in order to really create something).
-     See also: #newTemporary which looks for a good temp directory."
+     See also: #newTemporary which looks for a good temp directory.
+
+     DO NOT USE THIS FOR PLAIN FILES - IT IS UNSECURE use FileStream>>#newTemporaryIn:"
 
     ^ self newTemporaryIn:aDirectoryOrNil nameTemplate:(self tempFileNameTemplate)
 
@@ -586,7 +589,9 @@
      Notice: only a unique filename object is created and returned - no physical
      file is created by this method (i.e. you have to send #writeStream or
      whatever to it in order to really create something).
-     See also: #newTemporary which looks for a good temp directory."
+     See also: #newTemporary which looks for a good temp directory.
+
+     DO NOT USE THIS FOR PLAIN FILES - IT IS UNSECURE use FileStream>>#newTemporaryIn:nameTemplate:"
 
     |nameString newTempFilename|
 
@@ -597,15 +602,12 @@
     "although the above allows things to be redefined in concrete classes,
      the following should work on all systems ..."
 
-    NextTempFilenameIndex isNil ifTrue:[
-        NextTempFilenameIndex := 1.
-    ].
-
     [
-        nameString := template bindWith:(OperatingSystem getProcessId) with:NextTempFilenameIndex.
-        NextTempFilenameIndex := NextTempFilenameIndex + 1.
-
-        (aDirectoryOrNil isNil or:[aDirectoryOrNil asString isEmpty]) ifTrue:[
+        "Use random numbers in order to improve the security 
+         by making the generated names less predictable"
+        nameString := template bindWith:(OperatingSystem getProcessId) with:RandomGenerator new nextInteger.
+
+        aDirectoryOrNil isNil ifTrue:[
             newTempFilename := self named:nameString
         ] ifFalse:[
             newTempFilename := aDirectoryOrNil asFilename construct:nameString
@@ -2266,7 +2268,7 @@
 
     ^ OperatingSystem accessDeniedErrorSignal
         raiseRequestWith:filename?self
-        errorString:(' - cannot create/write file: ' , (filename ? self) asString)
+        errorString:(' - cannot create/write file: "%1"' bindWith:(filename ? self) asString)
 !
 
 fileNotFoundError:filename 
@@ -2331,6 +2333,28 @@
     "
 !
 
+existingReadWriteStream
+    "return a stream for read/write the file represented by the receiver.
+     If the file does not already exist, an exception is raised."
+
+    ^ FileStream oldFileNamed:(self osNameForAccess)
+
+    "
+     '/tmp/blaFaselQuall666666' asFilename remove.
+     '/tmp/blaFaselQuall666666' asFilename existingReadWriteStream.
+    "
+    "
+     |s|
+     s := '/tmp/foo' asFilename readWriteStream.
+     s nextPutAll:'1234567890'; close.
+     s := '/tmp/foo' asFilename existingReadWriteStream.
+     s nextPutAll:'abcdef'; close.
+
+     '/tmp/foo' asFilename contents inspect.     
+     '/tmp/foo' asFilename remove      
+    "
+!
+
 newReadWriteStream
     "return a stream for read/write the file represented by the receiver.
      If the file does not already exist, it is created;
@@ -2353,6 +2377,25 @@
     "
 !
 
+openWithMode:anArrayOrString
+    "return a stream for read/write the file represented by the receiver.
+     If the file does not already exist, it is created;
+     if it does exist, it is truncated."
+
+    ^ FileStream open:self osNameForAccess withMode:anArrayOrString
+
+    "
+     |s|
+
+     s := '/tmp/foo' asFilename openWithMode:'r+'.
+     s nextPutAll:'1234567890'.
+     s close.
+
+
+     '/tmp/foo' asFilename contents   
+    "
+!
+
 readStream
     "return a stream for reading from the file represented by the receiver.
      Raises an error if the file does not exist."
@@ -2795,6 +2838,51 @@
     "Modified: / 29-09-2006 / 16:26:32 / cg"
 !
 
+copyToStream:outStream
+    "Copy the files contents into another file.
+     The argument must be convertable to a filename.
+     Raises an exception, if an error occurs."
+
+    |newName inStream resetBinary|
+
+    "Contents is not copied if newName represent same file as me."
+    outStream isFileStream ifTrue:[
+        outStream fileName asAbsoluteFilename = self asAbsoluteFilename ifTrue: [ ^ self ].
+    ].
+
+    inStream := self readStream.
+    inStream isNil ifTrue:[
+        ^ self fileNotFoundError:self 
+    ].
+
+    [
+        inStream binary.
+        resetBinary := false.
+        outStream isBinary ifFalse:[
+            outStream binary.
+            resetBinary := true.
+        ].
+
+        [
+            inStream copyToEndInto:outStream.
+        ] on:Error do:[:ex|
+            ^ self fileCreationError:newName
+        ]
+    ] ensure:[
+        inStream close.
+        resetBinary ifTrue:[
+            outStream text.
+        ].
+    ].
+
+    "   
+     |out|
+     out := FileStream newTemporary.
+     'Make.proto' asFilename copyToStream:out.
+     out reset; contents
+    "
+!
+
 createAsEmptyFile
     "create an empty file with the receivers name.
      Raises an exception if not successful
@@ -5926,11 +6014,11 @@
 !Filename class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.397 2013-06-04 10:16:54 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.401 2013-07-08 19:23:30 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.397 2013-06-04 10:16:54 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.401 2013-07-08 19:23:30 stefan Exp $'
 ! !