Filename.st
changeset 174 9e273c60e785
parent 159 514c749165c3
child 192 3b0eb8864842
--- a/Filename.st	Fri Oct 28 02:22:55 1994 +0100
+++ b/Filename.st	Fri Oct 28 02:23:10 1994 +0100
@@ -20,7 +20,7 @@
 COPYRIGHT (c) 1992 by Claus Gittinger
 	     All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.13 1994-10-10 00:26:07 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.14 1994-10-28 01:23:10 claus Exp $
 '!
 
 !Filename class methodsFor:'documentation'!
@@ -41,7 +41,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.13 1994-10-10 00:26:07 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.14 1994-10-28 01:23:10 claus Exp $
 "
 !
 
@@ -99,6 +99,13 @@
      "
       Filename separator  
      "
+!
+
+isCaseSensitive
+    "return true, if filenames are case sensitive.
+     We ask the OS about this, to be independent here."
+
+    ^ OperatingSystem caseSensisitveFilenames
 ! !
 
 !Filename methodsFor:'instance creation'!
@@ -121,6 +128,26 @@
     "
 ! !
 
+!Filename methodsFor:'comparing'!
+
+hash
+    "return an integer useful as a hash-key"
+
+    ^ nameString hash
+!
+
+= aFilename
+    "return true, if the arguments value are equal by value"
+
+    self species == aFilename species ifTrue:[
+	self class isCaseSensitive ifTrue:[
+	    ^ nameString = aFilename asString
+	].
+	^ nameString sameAs:aFilename asString
+    ].
+    ^ false
+! !
+
 !Filename methodsFor:'converting'!
 
 asString
@@ -152,21 +179,29 @@
     ^ self class separator
 !
 
-directoryName
-    "return the directoryName of the file/directory.
+directory
+    "return the directory name part of the file/directory.
      - thats the name of the directory where the file/dir represented by
-       the receiver is contained in."
+       the receiver is contained in.
+     This method used to be called 'directoryName' but has been renamed
+     for ST-80 compatibility."
 
     ^ OperatingSystem directoryNameOf:(self pathName) "nameString"
 
     "
-     '/foo/bar' asFilename directoryName
-     '.' asFilename directoryName        
-     '..' asFilename directoryName       
-     '../..' asFilename directoryName     
+     '/foo/bar' asFilename directory
+     '.' asFilename directory        
+     '..' asFilename directory       
+     '../..' asFilename directory     
     "
 !
 
+tail
+    "the files name without directory prefix. For ST-80 compatiblity."
+
+    ^ self baseName
+!
+
 baseName
     "return my baseName
      - thats the file/directory name without leading parent-dirs."
@@ -316,7 +351,7 @@
 
     |dir name matching matchLen try allMatching|
 
-    dir := self directoryName.
+    dir := self directory.
     name := self baseName.
     matching := OrderedCollection new.
     (FileDirectory directoryNamed:dir) do:[:fileName |
@@ -390,7 +425,60 @@
     "
 ! !
 
-!Filename methodsFor:'file access'!
+!Filename methodsFor:'file queries'!
+
+info
+    "return the files info; that is a collection of file attributes,
+     (actually a dictionary) where the keys are #type, #uid, #gid, #size etc.
+    The actual amount and detail returned may depend on the OS used."
+
+    ^ OperatingSystem infoOf:nameString
+!
+
+fileSize
+    "return the size of the file in bytes"
+
+    |info|
+
+    info := self info.
+    info isNil ifTrue:[^ nil].
+    ^ info at:#size
+!
+
+fileType
+    "this returns a string describing the type of contents of
+     the file. This is done using the unix 'file' command,
+     (which usually is configurable by /etc/magic).
+     On non-unix systems, this may return an empty string, not knowning
+     about the contents."
+
+    |stream typeString|
+
+    typeString := ''.
+    stream := PipeStream readingFrom:('file ' , self pathName).
+    stream notNil ifTrue:[
+	typeString := stream contents asString.
+	stream close.
+	typeString := typeString copyFrom:(typeString indexOf:$:) + 1.
+	typeString := typeString withoutSeparators
+    ] ifFalse:[
+	"
+	 could add some fallback code here, for systems, where no
+	 file command is avaliable ...
+	 ... or at least analyze directory info.
+	"
+    ].
+    ^ typeString
+
+    "
+     'Makefile' asFilename fileType 
+     '.' asFilename fileType    
+     '/dev/null' asFilename fileType        
+     'smalltalk.rc' asFilename fileType    
+    "
+! !
+
+!Filename methodsFor:'file operations'!
 
 delete
     "remove the file - same as remove, for ST-80 compatibility"
@@ -444,12 +532,120 @@
     "
 !
 
+moveTo:newName
+    "copy the file represented by the receiver, then delete it.
+     This is different to renaming in case of symbolic links"
+
+    self copyTo:newName.
+    self remove
+!
+
+makeDirectory
+    "create a directory with the receivers name"
+
+    OperatingSystem createDirectory:nameString
+!
+
+addAccessRights:aCollection
+    "add the access rights as specified in aCollection for the file represented
+     by the receiver. The argument must be a collection of symbols,
+     such as #readUser, #writeGroup etc."
+
+    |access|
+
+    access := OperatingSystem accessModeOf:nameString.
+    aCollection do:[:accessSymbol |
+	access := access bitOr:(OperatingSystem accessMaskFor:accessSymbol).
+    ].
+    OperatingSystem changeAccessModeOf:nameString to:access
+
+    "
+     'foo' asFilename writeStream close.
+     'foo' asFilename addAccessRights:#(readUser readGroup readOthers).
+     'foo' asFilename addAccessRights:#(writeUser writeGroup writeOthers).
+     'foo' asFilename addAccessRights:#(executeUser executeGroup executeOthers).
+    "
+!
+
+removeAccessRights:aCollection
+    "remove the access rights as specified in aCollection for the file represented
+     by the receiver. The argument must be a collection of symbols,
+     such as #readUser, #writeGroup etc."
+
+    |access|
+
+    access := OperatingSystem accessModeOf:nameString.
+    aCollection do:[:accessSymbol |
+	access := access bitAnd:(OperatingSystem accessMaskFor:accessSymbol) bitInvert.
+    ].
+    OperatingSystem changeAccessModeOf:nameString to:access
+
+    "
+     'foo' asFilename writeStream close.
+     'foo' asFilename removeAccessRights:#(readUser readGroup readOthers).
+     'foo' asFilename removeAccessRights:#(writeUser writeGroup writeOthers).
+     'foo' asFilename removeAccessRights:#(executeUser executeGroup executeOthers).
+    "
+!
+
+makeReadableForAll
+    "make the file readable for all - you must have permission to do so."
+
+    self addAccessRights:#(readUser readGroup readOthers)
+!
+
+makeReadable
+    "make the file readable for  the owner - you must have permission to do so."
+
+    self addAccessRights:#(readUser)
+!
+
+makeWritable
+    "make the file writableable for all - you must have permission to do so."
+
+    self addAccessRights:#(writeUser)
+!
+
+makeWritableForAll
+    "make the file writable for all - you must have permission to do so."
+
+    self addAccessRights:#(writeUser writeGroup writeOthers)
+!
+
+makeUnwritable
+    "make the file unwritable for all - you must have permission to do so."
+
+    self removeAccessRights:#(writeUser writeGroup writeOthers)
+! !
+
+!Filename methodsFor:'file utilities'!
+
 fileIn
     "load smalltalk code from the file"
 
     ^ self readStream fileIn
 !
 
+edit
+    "start an editView on the file represented by the receiver"
+
+    EditTextView openOn:self asString
+!
+
+contentsOfEntireFile
+    "return the contents of the file as a string"
+
+    |s contents|
+
+    s := self readStream.
+    [
+	contents := s contents
+    ] valueNowOrOnUnwindDo:[s close].
+    ^ contents
+! !
+
+!Filename methodsFor:'file access'!
+
 newReadWriteStream
     "return a stream for read/write"
 
@@ -483,12 +679,17 @@
 !Filename methodsFor:'printing & storing'!
 
 storeOn:aStream
+    "append a printed representation of the receiver to aStream,
+     which allows reconstructing it via readFrom:"
+
     aStream nextPut:$(.
     nameString storeOn:aStream.
     aStream nextPutAll:' asFilename)'
 !
 
 printOn:aStream
+    "append a printed representation of the receiver to aStream."
+
     aStream nextPutAll:'FileName('''.
     nameString printOn:aStream.
     aStream nextPutAll:''')'