# HG changeset patch # User Claus Gittinger # Date 817089397 -3600 # Node ID 8b17f96bf05aae97096dc23fa40124bd91fabedd # Parent 0e1ec470923dd54d8890bc0f3f175492884f5d15 checkin from browser diff -r 0e1ec470923d -r 8b17f96bf05a FileDir.st --- a/FileDir.st Thu Nov 23 02:14:07 1995 +0100 +++ b/FileDir.st Thu Nov 23 02:16:37 1995 +0100 @@ -11,10 +11,10 @@ " Collection subclass:#FileDirectory - instanceVariableNames:'pathName lazy' - classVariableNames:'PathOfCurrentDirectory' - poolDictionaries:'' - category:'Collections-Files' + instanceVariableNames:'pathName lazy' + classVariableNames:'PathOfCurrentDirectory' + poolDictionaries:'' + category:'Collections-Files' ! !FileDirectory class methodsFor:'documentation'! @@ -33,10 +33,6 @@ " ! -version - ^ '$Header: /cvs/stx/stx/libbasic/Attic/FileDir.st,v 1.24 1995-11-11 15:23:00 cg Exp $' -! - documentation " FileDirectories represent directories in the underlying host system. @@ -53,6 +49,10 @@ Use instances of Filename if possible. " +! + +version + ^ '$Header: /cvs/stx/stx/libbasic/Attic/FileDir.st,v 1.25 1995-11-23 01:16:37 cg Exp $' ! ! !FileDirectory class methodsFor:'initialization'! @@ -75,30 +75,6 @@ !FileDirectory class methodsFor:'instance creation'! -directoryNamed:name - "create and return a new FileDirectory for the directory - with given pathname" - - ^ (self basicNew) pathName:name - - " - (FileDirectory directoryNamed:'..') pathName - (FileDirectory directoryNamed:'../..') files - " -! - -rootDirectory - "create and return a new FileDirectory for the root directory" - - ^ self directoryNamed:(OperatingSystem fileSeparator asString) - - " - FileDirectory rootDirectory contents - FileDirectory rootDirectory files - FileDirectory rootDirectory isReadable - " -! - currentDirectory "create and return a new FileDirectory for the current directory" @@ -112,6 +88,18 @@ " ! +directoryNamed:name + "create and return a new FileDirectory for the directory + with given pathname" + + ^ (self basicNew) pathName:name + + " + (FileDirectory directoryNamed:'..') pathName + (FileDirectory directoryNamed:'../..') files + " +! + directoryNamed:name in:aFileDirectory "create and return a new FileDirectory for the directory with given name in another FileDirectory" @@ -140,6 +128,18 @@ ] ]. ^ self directoryNamed:(baseName , name) +! + +rootDirectory + "create and return a new FileDirectory for the root directory" + + ^ self directoryNamed:(OperatingSystem fileSeparator asString) + + " + FileDirectory rootDirectory contents + FileDirectory rootDirectory files + FileDirectory rootDirectory isReadable + " ! ! !FileDirectory class methodsFor:'private'! @@ -164,39 +164,6 @@ ^ OperatingSystem baseNameOf:pathName ! -directoryName - "return my directoryName - - thats the directory name where I'm in" - - lazy ifTrue:[self getFullPathName]. - ^ OperatingSystem directoryNameOf:pathName -! - -pathName - "return my full pathname" - - lazy ifTrue:[self getFullPathName]. - ^ pathName -! - -pathName:dirName - "set my pathname; return nil if not a valid path; self otherwise" - - pathName := dirName. - (dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ - lazy := true - ] ifTrue:[ - (dirName startsWith:'./') ifFalse:[ -"/ (dirName includes:$.) ifTrue:[ - lazy := true - ] - ]. - ^ self -" - (OperatingSystem isDirectory:pathName) ifFalse:[^ nil] -" -! - contents "return a collection with all files and subdirectories in the receiver" @@ -223,6 +190,14 @@ ^ coll ! +directoryName + "return my directoryName + - thats the directory name where I'm in" + + lazy ifTrue:[self getFullPathName]. + ^ OperatingSystem directoryNameOf:pathName +! + files "return a collection with all plain files in the receiver directory" @@ -234,6 +209,102 @@ ]. coll sort. ^ coll +! + +pathName + "return my full pathname" + + lazy ifTrue:[self getFullPathName]. + ^ pathName +! + +pathName:dirName + "set my pathname; return nil if not a valid path; self otherwise" + + pathName := dirName. + (dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ + lazy := true + ] ifTrue:[ + (dirName startsWith:'./') ifFalse:[ +"/ (dirName includes:$.) ifTrue:[ + lazy := true + ] + ]. + ^ self +" + (OperatingSystem isDirectory:pathName) ifFalse:[^ nil] +" +! ! + +!FileDirectory methodsFor:'basic'! + +createDirectory:newName + "create a new filedirectory as a subdirectory of myself; + return true if successful" + + |realName| + + (newName notNil and:[newName notEmpty]) ifTrue:[ + (newName ~= '.' and:[newName ~= '..']) ifTrue:[ + ((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ + realName := newName copyFrom:2 + ] ifFalse:[ + realName := newName + ]. + ^ OperatingSystem createDirectory:(self pathNameOf:realName) + ] + ]. + ^ false +! + +link:oldFileName to:newFileName + "link oldFileName to newFileName in myself, return true if successful" + + |path1 path2| + + path1 := self pathNameOf:oldFileName. + path2 := self pathNameOf:newFileName. + ^ OperatingSystem linkFile:path1 to:path2 +! + +remove:aFileOrDirectoryName + "remove the file or directory from myself; return true if successful" + + |path| + + path := self pathNameOf:aFileOrDirectoryName. + (OperatingSystem isDirectory:path) ifTrue:[ + ^ OperatingSystem removeDirectory:path + ]. + ^ OperatingSystem removeFile:path +! + +removeDirectory:dirName + "remove the directory 'dirName' from myself; return true if successful. + If the directory is not empty, the containing files/directories are also + removed." + + |path| + + path := self pathNameOf:dirName. + (OperatingSystem removeDirectory:path) ifTrue:[^ true]. + ^ OperatingSystem recursiveRemoveDirectory:path +! + +removeFile:fileName + "remove the file 'fileName' from myself; return true if successful" + + ^ OperatingSystem removeFile:(self pathNameOf:fileName). +! + +renameFile:oldFileName newName:newFileName + "rename the file; return true if successful" + + |path1 path2| + + path1 := self pathNameOf:oldFileName. + path2 := self pathNameOf:newFileName. + ^ OperatingSystem renameFile:path1 to:path2 ! ! !FileDirectory methodsFor:'converting'! @@ -250,6 +321,130 @@ ^ self asFilename construct:someFile ! ! +!FileDirectory methodsFor:'enumerating'! + +allDirectoriesDo:aBlock + "evaluate the argument, aBlock for every directory name + in the directory and in all subdirectories" + + |aStream command line| + + lazy ifTrue:[self getFullPathName]. + command := 'cd ' , pathName , '; find . -type d -print'. + aStream := PipeStream readingFrom:command. + aStream isNil ifTrue:[^ nil]. + [aStream atEnd] whileFalse:[ + line := aStream nextLine. + line notNil ifTrue:[ + (line = '.') ifFalse:[ + "cut off initial ./" + line := line copyFrom:3 + ]. + aBlock value:line + ] + ]. + aStream close +! + +allFilesDo:aBlock + "evaluate the argument, aBlock for every file name in the directory and in all + subdirectories" + + |aStream command line| + + lazy ifTrue:[self getFullPathName]. + command := 'cd ' , pathName , '; find . -print'. + aStream := PipeStream readingFrom:command. + aStream isNil ifTrue:[^ nil]. + [aStream atEnd] whileFalse:[ + line := aStream nextLine. + line notNil ifTrue:[ + (line = '.') ifFalse:[ + "cut off initial ./" + line := line copyFrom:3 + ]. + aBlock value:line + ] + ]. + aStream close +! + +directoriesDo:aBlock + "evaluate the argument, aBlock for every subdirectory name in the directory" + + self where:[:name | (self isDirectory:name) ifTrue:[ + ((name ~= '.') and:[name ~= '..']) + ] ifFalse:[ + false + ] + ] do:aBlock +! + +do:aBlock + "evaluate the argument, aBlock for every name in the directory" + + self where:[:name | true] do:aBlock +! + +filesDo:aBlock + "evaluate the argument, aBlock for every plain file name in the directory" + + self where:[:name | (self isDirectory:name) not] do:aBlock +! + +namesDo:aBlock + "evaluate the argument, aBlock for every name in the directory. + for ST-80 compatibility" + + self do:aBlock +! + +where:testBlock do:aBlock + "evaluate the argument, aBlock for every object in the directory + for which testBlock evaluates to true." + + |aStream name| + + aStream := DirectoryStream directoryNamed:pathName. + aStream isNil ifTrue:[^ nil]. + [aStream atEnd] whileFalse:[ + name := aStream nextLine. + name notNil ifTrue:[ + (testBlock value:name) ifTrue:[ + aBlock value:name + ] + ] + ]. + aStream close +! ! + +!FileDirectory methodsFor:'more instance creation'! + +directoryNamed:aName + ^ self class directoryNamed:aName in:self pathName +! ! + +!FileDirectory methodsFor:'printing & storing'! + +printOn:aStream + "append a printed representation of the receiver to aStream." + + lazy ifTrue:[self getFullPathName]. + aStream nextPutAll:'(a FileDirectory pathName:'; + nextPutAll:pathName; + nextPutAll:')' +! + +storeOn:aStream + "append a printed representation of the receiver to aStream, + which allows reconstructing it via readFrom:" + + lazy ifTrue:[self getFullPathName]. + aStream nextPutAll:'(FileDirectory directoryNamed:'. + aStream nextPutAll:pathName. + aStream nextPut:$) +! ! + !FileDirectory methodsFor:'private'! getFullPathName @@ -289,87 +484,18 @@ ^ self class fullPathNameOf:fileName in:pathName ! ! -!FileDirectory methodsFor:'basic'! - -createDirectory:newName - "create a new filedirectory as a subdirectory of myself; - return true if successful" - - |realName| +!FileDirectory methodsFor:'queries'! - (newName notNil and:[newName notEmpty]) ifTrue:[ - (newName ~= '.' and:[newName ~= '..']) ifTrue:[ - ((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ - realName := newName copyFrom:2 - ] ifFalse:[ - realName := newName - ]. - ^ OperatingSystem createDirectory:(self pathNameOf:realName) - ] - ]. - ^ false -! +accessModeOf:name + "return the access-mode bits (rwxrwxrwx) of a file in myself" -removeFile:fileName - "remove the file 'fileName' from myself; return true if successful" - - ^ OperatingSystem removeFile:(self pathNameOf:fileName). + ^ OperatingSystem accessModeOf:(self pathNameOf:name) ! -removeDirectory:dirName - "remove the directory 'dirName' from myself; return true if successful. - If the directory is not empty, the containing files/directories are also - removed." - - |path| - - path := self pathNameOf:dirName. - (OperatingSystem removeDirectory:path) ifTrue:[^ true]. - ^ OperatingSystem recursiveRemoveDirectory:path -! - -remove:aFileOrDirectoryName - "remove the file or directory from myself; return true if successful" - - |path| - - path := self pathNameOf:aFileOrDirectoryName. - (OperatingSystem isDirectory:path) ifTrue:[ - ^ OperatingSystem removeDirectory:path - ]. - ^ OperatingSystem removeFile:path -! - -link:oldFileName to:newFileName - "link oldFileName to newFileName in myself, return true if successful" +changeAccessModeOf:name to:modeBits + "set the access-mode bits (rwxrwxrwx) of a file in myself" - |path1 path2| - - path1 := self pathNameOf:oldFileName. - path2 := self pathNameOf:newFileName. - ^ OperatingSystem linkFile:path1 to:path2 -! - -renameFile:oldFileName newName:newFileName - "rename the file; return true if successful" - - |path1 path2| - - path1 := self pathNameOf:oldFileName. - path2 := self pathNameOf:newFileName. - ^ OperatingSystem renameFile:path1 to:path2 -! ! - -!FileDirectory methodsFor:'queries'! - -species - ^ OrderedCollection -! - -id - "return the directories file-id (inode number)" - - ^ OperatingSystem idOf:pathName + ^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits ! exists @@ -383,6 +509,31 @@ " ! +exists:name + "return true, if the given name exists in myself" + + ^ OperatingSystem isValidPath:(self pathNameOf:name) +! + +id + "return the directories file-id (inode number)" + + ^ OperatingSystem idOf:pathName +! + +infoOf:name + "return an array filled with file info for the file 'aFileName'; + return nil if such a file does not exist" + + ^ OperatingSystem infoOf:(self pathNameOf:name) +! + +isDirectory:name + "return true, if the given name is that of a directory in myself" + + ^ OperatingSystem isDirectory:(self pathNameOf:name) +! + isEmpty "return true, if the directory is empty; redefined since '.' and '..' do not count as entries here." @@ -393,53 +544,10 @@ ^ true ! -infoOf:name - "return an array filled with file info for the file 'aFileName'; - return nil if such a file does not exist" - - ^ OperatingSystem infoOf:(self pathNameOf:name) -! - -timeOfLastChange:name - "return the timeStamp of a file in myself" - - ^ OperatingSystem timeOfLastChange:(self pathNameOf:name) -! - -timeOfLastChange - "return the timeStamp of myself" - - ^ OperatingSystem timeOfLastChange:pathName -! - -accessModeOf:name - "return the access-mode bits (rwxrwxrwx) of a file in myself" +isExecutable:name + "return true, if the given file is executable" - ^ OperatingSystem accessModeOf:(self pathNameOf:name) -! - -changeAccessModeOf:name to:modeBits - "set the access-mode bits (rwxrwxrwx) of a file in myself" - - ^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits -! - -typeOf:name - "return the symbolic type of a file in myself" - - ^ OperatingSystem typeOf:(self pathNameOf:name) -! - -exists:name - "return true, if the given name exists in myself" - - ^ OperatingSystem isValidPath:(self pathNameOf:name) -! - -isDirectory:name - "return true, if the given name is that of a directory in myself" - - ^ OperatingSystem isDirectory:(self pathNameOf:name) + ^ OperatingSystem isExecutable:(self pathNameOf:name) ! isReadable:name @@ -454,132 +562,26 @@ ^ OperatingSystem isWritable:(self pathNameOf:name) ! -isExecutable:name - "return true, if the given file is executable" - - ^ OperatingSystem isExecutable:(self pathNameOf:name) -! ! - -!FileDirectory methodsFor:'printing & storing'! - -printOn:aStream - "append a printed representation of the receiver to aStream." - - lazy ifTrue:[self getFullPathName]. - aStream nextPutAll:'(a FileDirectory pathName:'; - nextPutAll:pathName; - nextPutAll:')' +species + ^ OrderedCollection ! -storeOn:aStream - "append a printed representation of the receiver to aStream, - which allows reconstructing it via readFrom:" - - lazy ifTrue:[self getFullPathName]. - aStream nextPutAll:'(FileDirectory directoryNamed:'. - aStream nextPutAll:pathName. - aStream nextPut:$) -! ! - -!FileDirectory methodsFor:'more instance creation'! - -directoryNamed:aName - ^ self class directoryNamed:aName in:self pathName -! ! - -!FileDirectory methodsFor:'enumerating'! +timeOfLastChange + "return the timeStamp of myself" -where:testBlock do:aBlock - "evaluate the argument, aBlock for every object in the directory - for which testBlock evaluates to true." - - |aStream name| - - aStream := DirectoryStream directoryNamed:pathName. - aStream isNil ifTrue:[^ nil]. - [aStream atEnd] whileFalse:[ - name := aStream nextLine. - name notNil ifTrue:[ - (testBlock value:name) ifTrue:[ - aBlock value:name - ] - ] - ]. - aStream close -! - -do:aBlock - "evaluate the argument, aBlock for every name in the directory" - - self where:[:name | true] do:aBlock + ^ OperatingSystem timeOfLastChange:pathName ! -namesDo:aBlock - "evaluate the argument, aBlock for every name in the directory. - for ST-80 compatibility" - - self do:aBlock -! - -filesDo:aBlock - "evaluate the argument, aBlock for every plain file name in the directory" +timeOfLastChange:name + "return the timeStamp of a file in myself" - self where:[:name | (self isDirectory:name) not] do:aBlock -! - -directoriesDo:aBlock - "evaluate the argument, aBlock for every subdirectory name in the directory" - - self where:[:name | (self isDirectory:name) ifTrue:[ - ((name ~= '.') and:[name ~= '..']) - ] ifFalse:[ - false - ] - ] do:aBlock + ^ OperatingSystem timeOfLastChange:(self pathNameOf:name) ! -allFilesDo:aBlock - "evaluate the argument, aBlock for every file name in the directory and in all - subdirectories" - - |aStream command line| - - lazy ifTrue:[self getFullPathName]. - command := 'cd ' , pathName , '; find . -print'. - aStream := PipeStream readingFrom:command. - aStream isNil ifTrue:[^ nil]. - [aStream atEnd] whileFalse:[ - line := aStream nextLine. - line notNil ifTrue:[ - (line = '.') ifFalse:[ - "cut off initial ./" - line := line copyFrom:3 - ]. - aBlock value:line - ] - ]. - aStream close -! +typeOf:name + "return the symbolic type of a file in myself" -allDirectoriesDo:aBlock - "evaluate the argument, aBlock for every directory name - in the directory and in all subdirectories" - - |aStream command line| + ^ OperatingSystem typeOf:(self pathNameOf:name) +! ! - lazy ifTrue:[self getFullPathName]. - command := 'cd ' , pathName , '; find . -type d -print'. - aStream := PipeStream readingFrom:command. - aStream isNil ifTrue:[^ nil]. - [aStream atEnd] whileFalse:[ - line := aStream nextLine. - line notNil ifTrue:[ - (line = '.') ifFalse:[ - "cut off initial ./" - line := line copyFrom:3 - ]. - aBlock value:line - ] - ]. - aStream close -! ! +FileDirectory initialize! diff -r 0e1ec470923d -r 8b17f96bf05a FileDirectory.st --- a/FileDirectory.st Thu Nov 23 02:14:07 1995 +0100 +++ b/FileDirectory.st Thu Nov 23 02:16:37 1995 +0100 @@ -11,10 +11,10 @@ " Collection subclass:#FileDirectory - instanceVariableNames:'pathName lazy' - classVariableNames:'PathOfCurrentDirectory' - poolDictionaries:'' - category:'Collections-Files' + instanceVariableNames:'pathName lazy' + classVariableNames:'PathOfCurrentDirectory' + poolDictionaries:'' + category:'Collections-Files' ! !FileDirectory class methodsFor:'documentation'! @@ -33,10 +33,6 @@ " ! -version - ^ '$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.24 1995-11-11 15:23:00 cg Exp $' -! - documentation " FileDirectories represent directories in the underlying host system. @@ -53,6 +49,10 @@ Use instances of Filename if possible. " +! + +version + ^ '$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.25 1995-11-23 01:16:37 cg Exp $' ! ! !FileDirectory class methodsFor:'initialization'! @@ -75,30 +75,6 @@ !FileDirectory class methodsFor:'instance creation'! -directoryNamed:name - "create and return a new FileDirectory for the directory - with given pathname" - - ^ (self basicNew) pathName:name - - " - (FileDirectory directoryNamed:'..') pathName - (FileDirectory directoryNamed:'../..') files - " -! - -rootDirectory - "create and return a new FileDirectory for the root directory" - - ^ self directoryNamed:(OperatingSystem fileSeparator asString) - - " - FileDirectory rootDirectory contents - FileDirectory rootDirectory files - FileDirectory rootDirectory isReadable - " -! - currentDirectory "create and return a new FileDirectory for the current directory" @@ -112,6 +88,18 @@ " ! +directoryNamed:name + "create and return a new FileDirectory for the directory + with given pathname" + + ^ (self basicNew) pathName:name + + " + (FileDirectory directoryNamed:'..') pathName + (FileDirectory directoryNamed:'../..') files + " +! + directoryNamed:name in:aFileDirectory "create and return a new FileDirectory for the directory with given name in another FileDirectory" @@ -140,6 +128,18 @@ ] ]. ^ self directoryNamed:(baseName , name) +! + +rootDirectory + "create and return a new FileDirectory for the root directory" + + ^ self directoryNamed:(OperatingSystem fileSeparator asString) + + " + FileDirectory rootDirectory contents + FileDirectory rootDirectory files + FileDirectory rootDirectory isReadable + " ! ! !FileDirectory class methodsFor:'private'! @@ -164,39 +164,6 @@ ^ OperatingSystem baseNameOf:pathName ! -directoryName - "return my directoryName - - thats the directory name where I'm in" - - lazy ifTrue:[self getFullPathName]. - ^ OperatingSystem directoryNameOf:pathName -! - -pathName - "return my full pathname" - - lazy ifTrue:[self getFullPathName]. - ^ pathName -! - -pathName:dirName - "set my pathname; return nil if not a valid path; self otherwise" - - pathName := dirName. - (dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ - lazy := true - ] ifTrue:[ - (dirName startsWith:'./') ifFalse:[ -"/ (dirName includes:$.) ifTrue:[ - lazy := true - ] - ]. - ^ self -" - (OperatingSystem isDirectory:pathName) ifFalse:[^ nil] -" -! - contents "return a collection with all files and subdirectories in the receiver" @@ -223,6 +190,14 @@ ^ coll ! +directoryName + "return my directoryName + - thats the directory name where I'm in" + + lazy ifTrue:[self getFullPathName]. + ^ OperatingSystem directoryNameOf:pathName +! + files "return a collection with all plain files in the receiver directory" @@ -234,6 +209,102 @@ ]. coll sort. ^ coll +! + +pathName + "return my full pathname" + + lazy ifTrue:[self getFullPathName]. + ^ pathName +! + +pathName:dirName + "set my pathname; return nil if not a valid path; self otherwise" + + pathName := dirName. + (dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ + lazy := true + ] ifTrue:[ + (dirName startsWith:'./') ifFalse:[ +"/ (dirName includes:$.) ifTrue:[ + lazy := true + ] + ]. + ^ self +" + (OperatingSystem isDirectory:pathName) ifFalse:[^ nil] +" +! ! + +!FileDirectory methodsFor:'basic'! + +createDirectory:newName + "create a new filedirectory as a subdirectory of myself; + return true if successful" + + |realName| + + (newName notNil and:[newName notEmpty]) ifTrue:[ + (newName ~= '.' and:[newName ~= '..']) ifTrue:[ + ((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ + realName := newName copyFrom:2 + ] ifFalse:[ + realName := newName + ]. + ^ OperatingSystem createDirectory:(self pathNameOf:realName) + ] + ]. + ^ false +! + +link:oldFileName to:newFileName + "link oldFileName to newFileName in myself, return true if successful" + + |path1 path2| + + path1 := self pathNameOf:oldFileName. + path2 := self pathNameOf:newFileName. + ^ OperatingSystem linkFile:path1 to:path2 +! + +remove:aFileOrDirectoryName + "remove the file or directory from myself; return true if successful" + + |path| + + path := self pathNameOf:aFileOrDirectoryName. + (OperatingSystem isDirectory:path) ifTrue:[ + ^ OperatingSystem removeDirectory:path + ]. + ^ OperatingSystem removeFile:path +! + +removeDirectory:dirName + "remove the directory 'dirName' from myself; return true if successful. + If the directory is not empty, the containing files/directories are also + removed." + + |path| + + path := self pathNameOf:dirName. + (OperatingSystem removeDirectory:path) ifTrue:[^ true]. + ^ OperatingSystem recursiveRemoveDirectory:path +! + +removeFile:fileName + "remove the file 'fileName' from myself; return true if successful" + + ^ OperatingSystem removeFile:(self pathNameOf:fileName). +! + +renameFile:oldFileName newName:newFileName + "rename the file; return true if successful" + + |path1 path2| + + path1 := self pathNameOf:oldFileName. + path2 := self pathNameOf:newFileName. + ^ OperatingSystem renameFile:path1 to:path2 ! ! !FileDirectory methodsFor:'converting'! @@ -250,6 +321,130 @@ ^ self asFilename construct:someFile ! ! +!FileDirectory methodsFor:'enumerating'! + +allDirectoriesDo:aBlock + "evaluate the argument, aBlock for every directory name + in the directory and in all subdirectories" + + |aStream command line| + + lazy ifTrue:[self getFullPathName]. + command := 'cd ' , pathName , '; find . -type d -print'. + aStream := PipeStream readingFrom:command. + aStream isNil ifTrue:[^ nil]. + [aStream atEnd] whileFalse:[ + line := aStream nextLine. + line notNil ifTrue:[ + (line = '.') ifFalse:[ + "cut off initial ./" + line := line copyFrom:3 + ]. + aBlock value:line + ] + ]. + aStream close +! + +allFilesDo:aBlock + "evaluate the argument, aBlock for every file name in the directory and in all + subdirectories" + + |aStream command line| + + lazy ifTrue:[self getFullPathName]. + command := 'cd ' , pathName , '; find . -print'. + aStream := PipeStream readingFrom:command. + aStream isNil ifTrue:[^ nil]. + [aStream atEnd] whileFalse:[ + line := aStream nextLine. + line notNil ifTrue:[ + (line = '.') ifFalse:[ + "cut off initial ./" + line := line copyFrom:3 + ]. + aBlock value:line + ] + ]. + aStream close +! + +directoriesDo:aBlock + "evaluate the argument, aBlock for every subdirectory name in the directory" + + self where:[:name | (self isDirectory:name) ifTrue:[ + ((name ~= '.') and:[name ~= '..']) + ] ifFalse:[ + false + ] + ] do:aBlock +! + +do:aBlock + "evaluate the argument, aBlock for every name in the directory" + + self where:[:name | true] do:aBlock +! + +filesDo:aBlock + "evaluate the argument, aBlock for every plain file name in the directory" + + self where:[:name | (self isDirectory:name) not] do:aBlock +! + +namesDo:aBlock + "evaluate the argument, aBlock for every name in the directory. + for ST-80 compatibility" + + self do:aBlock +! + +where:testBlock do:aBlock + "evaluate the argument, aBlock for every object in the directory + for which testBlock evaluates to true." + + |aStream name| + + aStream := DirectoryStream directoryNamed:pathName. + aStream isNil ifTrue:[^ nil]. + [aStream atEnd] whileFalse:[ + name := aStream nextLine. + name notNil ifTrue:[ + (testBlock value:name) ifTrue:[ + aBlock value:name + ] + ] + ]. + aStream close +! ! + +!FileDirectory methodsFor:'more instance creation'! + +directoryNamed:aName + ^ self class directoryNamed:aName in:self pathName +! ! + +!FileDirectory methodsFor:'printing & storing'! + +printOn:aStream + "append a printed representation of the receiver to aStream." + + lazy ifTrue:[self getFullPathName]. + aStream nextPutAll:'(a FileDirectory pathName:'; + nextPutAll:pathName; + nextPutAll:')' +! + +storeOn:aStream + "append a printed representation of the receiver to aStream, + which allows reconstructing it via readFrom:" + + lazy ifTrue:[self getFullPathName]. + aStream nextPutAll:'(FileDirectory directoryNamed:'. + aStream nextPutAll:pathName. + aStream nextPut:$) +! ! + !FileDirectory methodsFor:'private'! getFullPathName @@ -289,87 +484,18 @@ ^ self class fullPathNameOf:fileName in:pathName ! ! -!FileDirectory methodsFor:'basic'! - -createDirectory:newName - "create a new filedirectory as a subdirectory of myself; - return true if successful" - - |realName| +!FileDirectory methodsFor:'queries'! - (newName notNil and:[newName notEmpty]) ifTrue:[ - (newName ~= '.' and:[newName ~= '..']) ifTrue:[ - ((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ - realName := newName copyFrom:2 - ] ifFalse:[ - realName := newName - ]. - ^ OperatingSystem createDirectory:(self pathNameOf:realName) - ] - ]. - ^ false -! +accessModeOf:name + "return the access-mode bits (rwxrwxrwx) of a file in myself" -removeFile:fileName - "remove the file 'fileName' from myself; return true if successful" - - ^ OperatingSystem removeFile:(self pathNameOf:fileName). + ^ OperatingSystem accessModeOf:(self pathNameOf:name) ! -removeDirectory:dirName - "remove the directory 'dirName' from myself; return true if successful. - If the directory is not empty, the containing files/directories are also - removed." - - |path| - - path := self pathNameOf:dirName. - (OperatingSystem removeDirectory:path) ifTrue:[^ true]. - ^ OperatingSystem recursiveRemoveDirectory:path -! - -remove:aFileOrDirectoryName - "remove the file or directory from myself; return true if successful" - - |path| - - path := self pathNameOf:aFileOrDirectoryName. - (OperatingSystem isDirectory:path) ifTrue:[ - ^ OperatingSystem removeDirectory:path - ]. - ^ OperatingSystem removeFile:path -! - -link:oldFileName to:newFileName - "link oldFileName to newFileName in myself, return true if successful" +changeAccessModeOf:name to:modeBits + "set the access-mode bits (rwxrwxrwx) of a file in myself" - |path1 path2| - - path1 := self pathNameOf:oldFileName. - path2 := self pathNameOf:newFileName. - ^ OperatingSystem linkFile:path1 to:path2 -! - -renameFile:oldFileName newName:newFileName - "rename the file; return true if successful" - - |path1 path2| - - path1 := self pathNameOf:oldFileName. - path2 := self pathNameOf:newFileName. - ^ OperatingSystem renameFile:path1 to:path2 -! ! - -!FileDirectory methodsFor:'queries'! - -species - ^ OrderedCollection -! - -id - "return the directories file-id (inode number)" - - ^ OperatingSystem idOf:pathName + ^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits ! exists @@ -383,6 +509,31 @@ " ! +exists:name + "return true, if the given name exists in myself" + + ^ OperatingSystem isValidPath:(self pathNameOf:name) +! + +id + "return the directories file-id (inode number)" + + ^ OperatingSystem idOf:pathName +! + +infoOf:name + "return an array filled with file info for the file 'aFileName'; + return nil if such a file does not exist" + + ^ OperatingSystem infoOf:(self pathNameOf:name) +! + +isDirectory:name + "return true, if the given name is that of a directory in myself" + + ^ OperatingSystem isDirectory:(self pathNameOf:name) +! + isEmpty "return true, if the directory is empty; redefined since '.' and '..' do not count as entries here." @@ -393,53 +544,10 @@ ^ true ! -infoOf:name - "return an array filled with file info for the file 'aFileName'; - return nil if such a file does not exist" - - ^ OperatingSystem infoOf:(self pathNameOf:name) -! - -timeOfLastChange:name - "return the timeStamp of a file in myself" - - ^ OperatingSystem timeOfLastChange:(self pathNameOf:name) -! - -timeOfLastChange - "return the timeStamp of myself" - - ^ OperatingSystem timeOfLastChange:pathName -! - -accessModeOf:name - "return the access-mode bits (rwxrwxrwx) of a file in myself" +isExecutable:name + "return true, if the given file is executable" - ^ OperatingSystem accessModeOf:(self pathNameOf:name) -! - -changeAccessModeOf:name to:modeBits - "set the access-mode bits (rwxrwxrwx) of a file in myself" - - ^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits -! - -typeOf:name - "return the symbolic type of a file in myself" - - ^ OperatingSystem typeOf:(self pathNameOf:name) -! - -exists:name - "return true, if the given name exists in myself" - - ^ OperatingSystem isValidPath:(self pathNameOf:name) -! - -isDirectory:name - "return true, if the given name is that of a directory in myself" - - ^ OperatingSystem isDirectory:(self pathNameOf:name) + ^ OperatingSystem isExecutable:(self pathNameOf:name) ! isReadable:name @@ -454,132 +562,26 @@ ^ OperatingSystem isWritable:(self pathNameOf:name) ! -isExecutable:name - "return true, if the given file is executable" - - ^ OperatingSystem isExecutable:(self pathNameOf:name) -! ! - -!FileDirectory methodsFor:'printing & storing'! - -printOn:aStream - "append a printed representation of the receiver to aStream." - - lazy ifTrue:[self getFullPathName]. - aStream nextPutAll:'(a FileDirectory pathName:'; - nextPutAll:pathName; - nextPutAll:')' +species + ^ OrderedCollection ! -storeOn:aStream - "append a printed representation of the receiver to aStream, - which allows reconstructing it via readFrom:" - - lazy ifTrue:[self getFullPathName]. - aStream nextPutAll:'(FileDirectory directoryNamed:'. - aStream nextPutAll:pathName. - aStream nextPut:$) -! ! - -!FileDirectory methodsFor:'more instance creation'! - -directoryNamed:aName - ^ self class directoryNamed:aName in:self pathName -! ! - -!FileDirectory methodsFor:'enumerating'! +timeOfLastChange + "return the timeStamp of myself" -where:testBlock do:aBlock - "evaluate the argument, aBlock for every object in the directory - for which testBlock evaluates to true." - - |aStream name| - - aStream := DirectoryStream directoryNamed:pathName. - aStream isNil ifTrue:[^ nil]. - [aStream atEnd] whileFalse:[ - name := aStream nextLine. - name notNil ifTrue:[ - (testBlock value:name) ifTrue:[ - aBlock value:name - ] - ] - ]. - aStream close -! - -do:aBlock - "evaluate the argument, aBlock for every name in the directory" - - self where:[:name | true] do:aBlock + ^ OperatingSystem timeOfLastChange:pathName ! -namesDo:aBlock - "evaluate the argument, aBlock for every name in the directory. - for ST-80 compatibility" - - self do:aBlock -! - -filesDo:aBlock - "evaluate the argument, aBlock for every plain file name in the directory" +timeOfLastChange:name + "return the timeStamp of a file in myself" - self where:[:name | (self isDirectory:name) not] do:aBlock -! - -directoriesDo:aBlock - "evaluate the argument, aBlock for every subdirectory name in the directory" - - self where:[:name | (self isDirectory:name) ifTrue:[ - ((name ~= '.') and:[name ~= '..']) - ] ifFalse:[ - false - ] - ] do:aBlock + ^ OperatingSystem timeOfLastChange:(self pathNameOf:name) ! -allFilesDo:aBlock - "evaluate the argument, aBlock for every file name in the directory and in all - subdirectories" - - |aStream command line| - - lazy ifTrue:[self getFullPathName]. - command := 'cd ' , pathName , '; find . -print'. - aStream := PipeStream readingFrom:command. - aStream isNil ifTrue:[^ nil]. - [aStream atEnd] whileFalse:[ - line := aStream nextLine. - line notNil ifTrue:[ - (line = '.') ifFalse:[ - "cut off initial ./" - line := line copyFrom:3 - ]. - aBlock value:line - ] - ]. - aStream close -! +typeOf:name + "return the symbolic type of a file in myself" -allDirectoriesDo:aBlock - "evaluate the argument, aBlock for every directory name - in the directory and in all subdirectories" - - |aStream command line| + ^ OperatingSystem typeOf:(self pathNameOf:name) +! ! - lazy ifTrue:[self getFullPathName]. - command := 'cd ' , pathName , '; find . -type d -print'. - aStream := PipeStream readingFrom:command. - aStream isNil ifTrue:[^ nil]. - [aStream atEnd] whileFalse:[ - line := aStream nextLine. - line notNil ifTrue:[ - (line = '.') ifFalse:[ - "cut off initial ./" - line := line copyFrom:3 - ]. - aBlock value:line - ] - ]. - aStream close -! ! +FileDirectory initialize!