Filename.st
changeset 542 fec8c38962ba
parent 539 11aca4b770ac
child 545 32adce698074
--- a/Filename.st	Tue Nov 14 12:15:28 1995 +0100
+++ b/Filename.st	Tue Nov 14 12:18:26 1995 +0100
@@ -33,7 +33,7 @@
 !
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.39 1995-11-14 10:31:39 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.40 1995-11-14 11:18:26 cg Exp $'
 !
 
 documentation
@@ -190,7 +190,8 @@
 !Filename class methodsFor:'instance creation'!
 
 named:aString
-    "return a filename for a directory named aString."
+    "return a filename for a directory named aString.
+     This is the same as 'aString asFilename'."
 
     ^ (self basicNew) setName:aString
 
@@ -219,6 +220,16 @@
     "
 !
 
+defaultDirectoryName
+    "ST80 compatibility: return the defaultDirectories name (as a string)"
+
+    ^ self defaultDirectory name
+
+    "
+     Filename defaultDirectoryName 
+    "
+!
+
 newTemporary
     "return a new unique filename - use this for temporary files.
      The filenames returned are '/tmp/stxtmp_xx_nn' where xx is our
@@ -485,6 +496,38 @@
     ^ s
 ! !
 
+!Filename methodsFor:'error handling'!
+
+reportError:string with:filename
+    ^ OperatingSystem errorSignal
+	raiseRequestWith:filename
+	errorString:string
+!
+
+fileNotFoundError:filename 
+    ^ OperatingSystem fileNotFoundErrorSignal
+	raiseRequestWith:filename
+	errorString:('file not found: ' , filename asString)
+!
+
+fileCreationError:filename
+    ^ OperatingSystem accessDeniedErrorSignal
+	raiseRequestWith:filename
+	errorString:('cannot create/write file: ' , filename asString)
+!
+
+accessDeniedError:filename
+    ^ OperatingSystem accessDeniedErrorSignal
+	raiseRequestWith:filename
+	errorString:('access denied: ' , filename asString)
+!
+
+removeError:filename
+    ^ OperatingSystem accessDeniedErrorSignal
+	raiseRequestWith:filename
+	errorString:('cannot remove: ' , filename asString)
+! !
+
 !Filename methodsFor:'instance creation'!
 
 constructString:subname
@@ -1234,19 +1277,24 @@
 delete
     "remove the file - same as remove, for ST-80 compatibility"
 
-    ^ self remove
+    self remove
 !
 
 remove
     "remove the file/directory - the argument must be convertable to a String.
      Return true if sucessfull, false if not.
-     If the receiver represents a directory AND that directory is not empty,
-     false is returned. Use recursiveRemove in order to remove these."
+     Use recursiveRemove in order to (recursively) remove non empty directories."
+
+    |ok|
 
     self isDirectory ifTrue:[
-	^ OperatingSystem removeDirectory:nameString
+	ok := OperatingSystem removeDirectory:nameString
+    ] ifFalse:[
+	ok := OperatingSystem removeFile:nameString
     ].
-    ^ OperatingSystem removeFile:nameString
+    ok ifFalse:[
+	self removeError:self
+    ].
 
     "
      (FileStream newFileNamed:'foo') close.
@@ -1265,10 +1313,14 @@
 !
 
 recursiveRemove
-    "remove the directory and all of its subfiles/subdirectories.
-     Return true if sucessfull, false if not."
+    "remove the directory and all of its subfiles/subdirectories."
+
+    |ok|
 
-    ^ OperatingSystem recursiveRemoveDirectory:nameString
+    ok := OperatingSystem recursiveRemoveDirectory:nameString.
+    ok ifFalse:[
+	self removeError:self
+    ].
 
     "
      'foo' asFilename makeDirectory.
@@ -1289,10 +1341,17 @@
 !
 
 renameTo:newName
-    "rename the file - the argument must be convertable to a String.
-     Return true if sucessfull, false if not."
+    "rename the file - the argument must be convertable to a String."
+
+    |ok|
 
-    ^ OperatingSystem renameFile:nameString to:(newName asString)
+    ok := OperatingSystem renameFile:nameString to:(newName asString).
+    ok ifFalse:[
+	self exists ifFalse:[
+	    ^ self fileNotFoundError:self
+	].
+	^ self accessDeniedError:newName asFilename.
+    ].
 
     "
      '/tmp/foo' asFilename renameTo:'/tmp/bar'
@@ -1303,28 +1362,28 @@
     "copy the file - the argument must be convertable to a filename.
      Return true if successfull, false if not."
 
-    |inStream outStream buffer bufferSize count|
+    |inStream outStream buffer bufferSize count newFile|
 
     bufferSize := 8 * 1024.
     buffer := ByteArray new:bufferSize.
     inStream := self readStream.
+    inStream isNil ifTrue:[
+	^ self fileNotFoundError:self 
+    ].
 
-    outStream := newName asFilename writeStream.
-    (inStream isNil or:[outStream isNil]) ifTrue:[
-	'FILENAME: file copy failed' errorPrintNL.
-	^ false.
+    outStream := (newFile := newName asFilename) writeStream.
+    outStream isNil ifTrue:[
+	^ self fileCreationError:newFile
     ].
 
     [inStream atEnd] whileFalse:[
 	count := inStream nextBytes:bufferSize into:buffer.
 	(outStream nextPutBytes:count from:buffer) ~= count ifTrue:[
-	    'FILENAME: file copy failed' errorPrintNL.
-	    ^ false
+	    ^ self fileCreationError:newFile
 	]
     ].
     outStream close.
     inStream close.
-    ^ true
 
     "
      'Makefile' asFilename copyTo:'Makefile.foo'
@@ -1337,15 +1396,17 @@
      This is different to renaming in case of cross device moves.
      Return true if successfull, false if not."
 
-    (self copyTo:newName) ifFalse:[^ false].
-    ^ self remove
+    self copyTo:newName.
+    self remove
 !
 
 makeDirectory
     "create a directory with the receivers name.
      Return true if successfull, false if not."
 
-    ^ OperatingSystem createDirectory:nameString
+    (OperatingSystem createDirectory:nameString) ifFalse:[
+	^ self fileCreationError:self
+    ]
 !
 
 addAccessRights:aCollection
@@ -1359,7 +1420,9 @@
     aCollection do:[:accessSymbol |
 	access := access bitOr:(OperatingSystem accessMaskFor:accessSymbol).
     ].
-    OperatingSystem changeAccessModeOf:nameString to:access
+    (OperatingSystem changeAccessModeOf:nameString to:access) ifFalse:[
+	^ self accessDeniedError:self
+    ]
 
     "
      'foo' asFilename writeStream close.
@@ -1380,7 +1443,9 @@
     aCollection do:[:accessSymbol |
 	access := access bitAnd:(OperatingSystem accessMaskFor:accessSymbol) bitInvert.
     ].
-    ^ OperatingSystem changeAccessModeOf:nameString to:access
+    (OperatingSystem changeAccessModeOf:nameString to:access) ifFalse:[
+	^ self accessDeniedError:self
+    ].
 
     "
      'foo' asFilename writeStream close.