FileDir.st
changeset 159 514c749165c3
parent 92 0c73b48551ac
child 172 52750f9c44de
equal deleted inserted replaced
158:be947d4e7fb2 159:514c749165c3
    19 
    19 
    20 FileDirectory comment:'
    20 FileDirectory comment:'
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    22              All Rights Reserved
    22              All Rights Reserved
    23 
    23 
    24 $Header: /cvs/stx/stx/libbasic/Attic/FileDir.st,v 1.11 1994-08-05 00:54:45 claus Exp $
    24 $Header: /cvs/stx/stx/libbasic/Attic/FileDir.st,v 1.12 1994-10-10 00:26:00 claus Exp $
    25 '!
    25 '!
    26 
    26 
    27 !FileDirectory class methodsFor:'documentation'!
    27 !FileDirectory class methodsFor:'documentation'!
    28 
    28 
    29 copyright
    29 copyright
    40 "
    40 "
    41 !
    41 !
    42 
    42 
    43 version
    43 version
    44 "
    44 "
    45 $Header: /cvs/stx/stx/libbasic/Attic/FileDir.st,v 1.11 1994-08-05 00:54:45 claus Exp $
    45 $Header: /cvs/stx/stx/libbasic/Attic/FileDir.st,v 1.12 1994-10-10 00:26:00 claus Exp $
    46 "
    46 "
    47 !
    47 !
    48 
    48 
    49 documentation
    49 documentation
    50 "
    50 "
    70 !FileDirectory class methodsFor:'instance creation'!
    70 !FileDirectory class methodsFor:'instance creation'!
    71 
    71 
    72 rootDirectory
    72 rootDirectory
    73     "create and return a new FileDirectory for the root directory"
    73     "create and return a new FileDirectory for the root directory"
    74 
    74 
    75     ^ (self basicNew) pathName:'/'
    75     ^ (self basicNew) pathName:(OperatingSystem fileSeparator asString)
    76 
    76 
    77     "
    77     "
    78      FileDirectory rootDirectory contents
    78      FileDirectory rootDirectory contents
    79      FileDirectory rootDirectory files
    79      FileDirectory rootDirectory files
    80      FileDirectory rootDirectory isReadable
    80      FileDirectory rootDirectory isReadable
   108 
   108 
   109 directoryNamed:name in:aFileDirectory
   109 directoryNamed:name in:aFileDirectory
   110     "create and return a new FileDirectory for the directory with given name
   110     "create and return a new FileDirectory for the directory with given name
   111      in another FileDirectory"
   111      in another FileDirectory"
   112 
   112 
   113     |baseName|
   113     |baseName sep|
   114 
   114 
   115     ((name at:1) == $/) ifTrue:[
   115     sep := OperatingSystem fileSeparator.
       
   116     ((name at:1) == sep) ifTrue:[
   116         ^ self directoryNamed:name
   117         ^ self directoryNamed:name
   117     ].
   118     ].
   118     (aFileDirectory isKindOf:FileDirectory) ifTrue:[
   119     (aFileDirectory isKindOf:FileDirectory) ifTrue:[
   119         baseName := aFileDirectory pathName
   120         baseName := aFileDirectory pathName
   120     ] ifFalse:[
   121     ] ifFalse:[
   125         ^ (self basicNew) pathName:(OperatingSystem directoryNameOf:baseName)
   126         ^ (self basicNew) pathName:(OperatingSystem directoryNameOf:baseName)
   126     ].
   127     ].
   127 "
   128 "
   128     (name = '.') ifTrue:[^ aFileDirectory].
   129     (name = '.') ifTrue:[^ aFileDirectory].
   129 
   130 
   130     (baseName = '/') ifFalse:[
   131     (baseName = sep asString) ifFalse:[
   131         (baseName endsWith:'/') ifFalse:[
   132         (baseName endsWith:sep) ifFalse:[
   132             baseName := baseName , '/'
   133             baseName := baseName copyWith:sep
   133         ]
   134         ]
   134     ].
   135     ].
   135     ^ (self basicNew) pathName:(baseName , name)
   136     ^ (self basicNew) pathName:(baseName , name)
   136 ! !
   137 ! !
   137 
   138 
   162 
   163 
   163 pathName:dirName
   164 pathName:dirName
   164     "set my pathname; return nil if not a valid path; self otherwise"
   165     "set my pathname; return nil if not a valid path; self otherwise"
   165 
   166 
   166     pathName := dirName.
   167     pathName := dirName.
   167     (dirName startsWith:'/') ifFalse:[
   168     (dirName startsWith:OperatingSystem fileSeparator) ifFalse:[
   168         lazy := true
   169         lazy := true
   169     ] ifTrue:[
   170     ] ifTrue:[
   170         (dirName includes:$.) ifTrue:[
   171         (dirName includes:$.) ifTrue:[
   171             lazy := true
   172             lazy := true
   172         ]
   173         ]
   224 getFullPathName
   225 getFullPathName
   225     "make my pathname be a full pathname - i.e. starting at root"
   226     "make my pathname be a full pathname - i.e. starting at root"
   226 
   227 
   227     |aStream command shortPathName fullPathName|
   228     |aStream command shortPathName fullPathName|
   228 
   229 
   229     (pathName = '/') ifTrue:[
   230     (pathName = OperatingSystem fileSeparator asString) ifTrue:[
   230         lazy := false.
   231         lazy := false.
   231         ^ self
   232         ^ self
   232     ].
   233     ].
   233 
   234 
   234     "since currentDirectory is used very often, cache its path here"
   235     "since currentDirectory is used very often, cache its path here"
   297 
   298 
   298     |realName|
   299     |realName|
   299 
   300 
   300     (newName = '.') ifFalse:[
   301     (newName = '.') ifFalse:[
   301         (newName = '..') ifFalse:[
   302         (newName = '..') ifFalse:[
   302             ((newName at:1) == $/) ifTrue:[
   303             ((newName at:1) == OperatingSystem fileSeparator) ifTrue:[
   303                 realName := newName copyFrom:2
   304                 realName := newName copyFrom:2
   304             ] ifFalse:[
   305             ] ifFalse:[
   305                 realName := newName
   306                 realName := newName
   306             ].
   307             ].
   307             (realName startsWith:'/') ifTrue:[
   308 	    ^ OperatingSystem createDirectory:(self class fullPathNameOf:realName in:pathName)
   308                 ^ OperatingSystem createDirectory:realName
       
   309             ] ifFalse:[
       
   310                 ^ OperatingSystem createDirectory:(pathName , '/' , realName)
       
   311             ]
       
   312         ]
   309         ]
   313     ].
   310     ].
   314     ^ false
   311     ^ false
   315 !
   312 !
   316 
   313 
   317 removeFile:fileName
   314 removeFile:fileName
   318     "remove the file 'fileName' from myself; return true if successful"
   315     "remove the file 'fileName' from myself; return true if successful"
   319 
   316 
   320     (fileName startsWith:'/') ifTrue:[
   317     ^ OperatingSystem removeFile:(self class fullPathNameOf:fileName in:pathName).
   321         ^ OperatingSystem removeFile:fileName
       
   322     ].
       
   323     ^ OperatingSystem removeFile:(pathName , '/' , fileName)
       
   324 !
   318 !
   325 
   319 
   326 removeDirectory:dirName
   320 removeDirectory:dirName
   327     "remove the directory 'dirName' from myself; return true if successful.
   321     "remove the directory 'dirName' from myself; return true if successful.
   328      If the directory is not empty, the containing files/directories are also
   322      If the directory is not empty, the containing files/directories are also
   329      removed."
   323      removed."
   330 
   324 
   331     |absPath|
   325     |path|
   332 
   326 
   333     (dirName startsWith:'/') ifTrue:[
   327     path := self class fullPathNameOf:dirName in:pathName.
   334         absPath := dirName
   328     (OperatingSystem removeDirectory:path) ifTrue:[^ true].
   335     ] ifFalse:[
   329     ^ OperatingSystem recursiveRemoveDirectory:path
   336         absPath := pathName , '/' , dirName
       
   337     ].
       
   338     (OperatingSystem removeDirectory:absPath) ifTrue:[^ true].
       
   339     ^ OperatingSystem recursiveRemoveDirectory:absPath
       
   340 !
   330 !
   341     
   331     
   342 remove:aFileOrDirectoryName
   332 remove:aFileOrDirectoryName
   343     "remove the file or directory from myself; return true if successful"
   333     "remove the file or directory from myself; return true if successful"
   344 
   334 
   345     |path|
   335     |path|
   346 
   336 
   347     (aFileOrDirectoryName startsWith:'/') ifTrue:[
   337     path := self class fullPathNameOf:aFileOrDirectoryName in:pathName.
   348         path := aFileOrDirectoryName
       
   349     ] ifFalse:[
       
   350         path := (pathName , '/' , aFileOrDirectoryName)
       
   351     ].
       
   352     (OperatingSystem isDirectory:path) ifTrue:[
   338     (OperatingSystem isDirectory:path) ifTrue:[
   353         ^ OperatingSystem removeDirectory:path
   339         ^ OperatingSystem removeDirectory:path
   354     ].
   340     ].
   355     ^ OperatingSystem removeFile:path
   341     ^ OperatingSystem removeFile:path
   356 !
   342 !
   358 link:oldFileName to:newFileName
   344 link:oldFileName to:newFileName
   359     "link oldFileName to newFileName in myself, return true if successful"
   345     "link oldFileName to newFileName in myself, return true if successful"
   360 
   346 
   361     |path1 path2|
   347     |path1 path2|
   362 
   348 
   363     (oldFileName startsWith:'/') ifTrue:[
   349     path1 := self class fullPathNameOf:oldFileName in:pathName.
   364         path1 := oldFileName
   350     path2 := self class fullPathNameOf:newFileName in:pathName.
   365     ] ifFalse:[
       
   366         path1 := (pathName , '/' , oldFileName)
       
   367     ].
       
   368     (newFileName startsWith:'/') ifTrue:[
       
   369         path2 := newFileName
       
   370     ] ifFalse:[
       
   371         path2 := (pathName , '/' , newFileName)
       
   372     ].
       
   373     ^ OperatingSystem linkFile:path1 to:path2
   351     ^ OperatingSystem linkFile:path1 to:path2
   374 !
   352 !
   375 
   353 
   376 renameFile:oldFileName newName:newFileName
   354 renameFile:oldFileName newName:newFileName
   377     "rename the file; return true if successful"
   355     "rename the file; return true if successful"
   378 
   356 
   379     |path1 path2|
   357     |path1 path2|
   380 
   358 
   381     (oldFileName startsWith:'/') ifTrue:[
   359     path1 := self class fullPathNameOf:oldFileName in:pathName.
   382         path1 := oldFileName
   360     path2 := self class fullPathNameOf:newFileName in:pathName.
   383     ] ifFalse:[
       
   384         path1 := (pathName , '/' , oldFileName)
       
   385     ].
       
   386     (newFileName startsWith:'/') ifTrue:[
       
   387         path2 := newFileName
       
   388     ] ifFalse:[
       
   389         path2 := (pathName , '/' , newFileName)
       
   390     ].
       
   391     ^ OperatingSystem renameFile:path1 to:path2
   361     ^ OperatingSystem renameFile:path1 to:path2
       
   362 ! !
       
   363 
       
   364 !FileDirectory class methodsFor:'private'!
       
   365 
       
   366 fullPathNameOf:name in:path
       
   367     (name startsWith:OperatingSystem fileSeparator) ifTrue:[
       
   368         ^ name
       
   369     ].
       
   370     ^ path , OperatingSystem fileSeparator asString , name
   392 ! !
   371 ! !
   393 
   372 
   394 !FileDirectory methodsFor:'queries'!
   373 !FileDirectory methodsFor:'queries'!
   395 
   374 
   396 id
   375 id
   401 
   380 
   402 exists
   381 exists
   403     "return true if this directory exists"
   382     "return true if this directory exists"
   404 
   383 
   405     ^ OperatingSystem isDirectory:pathName
   384     ^ OperatingSystem isDirectory:pathName
   406     "(FileDirectory directoryNamed:'fooBar') exists"
   385 
       
   386     "
       
   387      (FileDirectory directoryNamed:'fooBar') exists
       
   388      (FileDirectory directoryNamed:'/tmp') exists
       
   389     "
   407 !
   390 !
   408 
   391 
   409 isEmpty
   392 isEmpty
   410     "return true, if the directory is empty;
   393     "return true, if the directory is empty;
   411      redefined since '.' and '..' do not count as entries here."
   394      redefined since '.' and '..' do not count as entries here."
   418 
   401 
   419 infoOf:name
   402 infoOf:name
   420     "return an array filled with file info for the file 'aFileName';
   403     "return an array filled with file info for the file 'aFileName';
   421      return nil if such a file does not exist"
   404      return nil if such a file does not exist"
   422 
   405 
   423     (name startsWith:'/') ifTrue:[
   406     ^ OperatingSystem infoOf:(self class fullPathNameOf:name in:pathName)
   424         ^ OperatingSystem infoOf:name
       
   425     ].
       
   426     ^ OperatingSystem infoOf:(pathName , '/' , name)
       
   427 !
   407 !
   428 
   408 
   429 timeOfLastChange:name
   409 timeOfLastChange:name
   430     "return the timeStamp of a file in myself"
   410     "return the timeStamp of a file in myself"
   431 
   411 
   432     (name startsWith:'/') ifTrue:[
   412     ^ OperatingSystem timeOfLastChange:(self class fullPathNameOf:name in:pathName)
   433         ^ OperatingSystem timeOfLastChange:name
       
   434     ].
       
   435     ^ OperatingSystem timeOfLastChange:(pathName , '/' , name)
       
   436 !
   413 !
   437 
   414 
   438 timeOfLastChange
   415 timeOfLastChange
   439     "return the timeStamp of myself"
   416     "return the timeStamp of myself"
   440 
   417 
   441     ^ OperatingSystem timeOfLastChange:pathName
   418     ^ OperatingSystem timeOfLastChange:pathName
   442 !
   419 !
   443 
   420 
   444 accessModeOf:aFileName
   421 accessModeOf:name
   445     "return the access-mode bits (rwxrwxrwx) of a file in myself"
   422     "return the access-mode bits (rwxrwxrwx) of a file in myself"
   446 
   423 
   447     (aFileName startsWith:'/') ifTrue:[
   424     ^ OperatingSystem accessModeOf:(self class fullPathNameOf:name in:pathName)
   448         ^ OperatingSystem accessModeOf:aFileName
   425 !
   449     ].
   426 
   450     ^ OperatingSystem accessModeOf:(pathName , '/' , aFileName)
   427 changeAccessModeOf:name to:modeBits
   451 !
       
   452 
       
   453 changeAccessModeOf:aFileName to:modeBits
       
   454     "set the access-mode bits (rwxrwxrwx) of a file in myself"
   428     "set the access-mode bits (rwxrwxrwx) of a file in myself"
   455 
   429 
   456     (aFileName startsWith:'/') ifTrue:[
   430     ^ OperatingSystem changeAccessModeOf:(self class fullPathNameOf:name in:pathName)
   457         ^ OperatingSystem changeAccessModeOf:aFileName
       
   458                                           to:modeBits
       
   459     ].
       
   460     ^ OperatingSystem changeAccessModeOf:(pathName , '/' , aFileName)
       
   461                                       to:modeBits
   431                                       to:modeBits
   462 !
   432 !
   463 
   433 
   464 typeOf:aFileName
   434 typeOf:name
   465     "return the symbolic type of a file in myself"
   435     "return the symbolic type of a file in myself"
   466 
   436 
   467     (aFileName startsWith:'/') ifTrue:[
   437     ^ OperatingSystem typeOf:(self class fullPathNameOf:name in:pathName)
   468         ^ OperatingSystem typeOf:aFileName
       
   469     ].
       
   470     ^ OperatingSystem typeOf:(pathName , '/' , aFileName)
       
   471 !
   438 !
   472 
   439 
   473 exists:name
   440 exists:name
   474     "return true, if the given name exists in myself"
   441     "return true, if the given name exists in myself"
   475 
   442 
   476     (name startsWith:'/') ifTrue:[
   443     ^ OperatingSystem isValidPath:(self class fullPathNameOf:name in:pathName)
   477         ^ OperatingSystem isValidPath:name
       
   478     ].
       
   479     ^ OperatingSystem isValidPath:(pathName , '/' , name)
       
   480 !
   444 !
   481 
   445 
   482 isDirectory:name
   446 isDirectory:name
   483     "return true, if the given name is that of a directory in myself"
   447     "return true, if the given name is that of a directory in myself"
   484 
   448 
   485     (name startsWith:'/') ifTrue:[
   449     ^ OperatingSystem isDirectory:(self class fullPathNameOf:name in:pathName)
   486         ^ OperatingSystem isDirectory:name
       
   487     ].
       
   488     ^ OperatingSystem isDirectory:(pathName , '/' , name)
       
   489 !
   450 !
   490 
   451 
   491 isReadable:name
   452 isReadable:name
   492     "return true, if the given file is readable"
   453     "return true, if the given file is readable"
   493 
   454 
   494     (name startsWith:'/') ifTrue:[
   455     ^ OperatingSystem isReadable:(self class fullPathNameOf:name in:pathName)
   495         ^ OperatingSystem isReadable:name
       
   496     ].
       
   497     ^ OperatingSystem isReadable:(pathName , '/' , name)
       
   498 !
   456 !
   499 
   457 
   500 isWritable:name
   458 isWritable:name
   501     "return true, if the given file is readable"
   459     "return true, if the given file is readable"
   502 
   460 
   503     (name startsWith:'/') ifTrue:[
   461     ^ OperatingSystem isWritable:(self class fullPathNameOf:name in:pathName)
   504         ^ OperatingSystem isWritable:name
       
   505     ].
       
   506     ^ OperatingSystem isWritable:(pathName , '/' , name)
       
   507 !
   462 !
   508 
   463 
   509 isExecutable:name
   464 isExecutable:name
   510     "return true, if the given file is executable"
   465     "return true, if the given file is executable"
   511 
   466 
   512     (name startsWith:'/') ifTrue:[
   467     ^ OperatingSystem isExecutable:(self class fullPathNameOf:name in:pathName)
   513         ^ OperatingSystem isExecutable:name
       
   514     ].
       
   515     ^ OperatingSystem isExecutable:(pathName , '/' , name)
       
   516 ! !
   468 ! !
   517 
   469 
   518 !FileDirectory methodsFor:'printing & storing'!
   470 !FileDirectory methodsFor:'printing & storing'!
   519 
   471 
   520 printString
   472 printString