Filename.st
changeset 17143 5b1217eaafa7
parent 17113 a521179b677c
child 17227 c75e790d86f1
equal deleted inserted replaced
17142:4cc07796219b 17143:5b1217eaafa7
  2484      Raises an error if the file does not exist."
  2484      Raises an error if the file does not exist."
  2485 
  2485 
  2486     ^ FileStream readonlyFileNamed:(self osNameForAccess)
  2486     ^ FileStream readonlyFileNamed:(self osNameForAccess)
  2487 
  2487 
  2488     "
  2488     "
  2489      '/tmp/foo' asFilename readStream 
  2489       '/tmp/foo' asFilename readStream.
  2490     "
  2490     "
  2491 !
  2491 !
  2492 
  2492 
  2493 readWriteStream
  2493 readWriteStream
  2494     "return a stream for read/write the file represented by the receiver.
  2494     "return a stream for read/write the file represented by the receiver.
  2879 
  2879 
  2880     "Contents is not copied if newName represent same file as me."
  2880     "Contents is not copied if newName represent same file as me."
  2881     newName asAbsoluteFilename = self asAbsoluteFilename ifTrue: [ ^ self ].
  2881     newName asAbsoluteFilename = self asAbsoluteFilename ifTrue: [ ^ self ].
  2882 
  2882 
  2883     inStream := self readStream.
  2883     inStream := self readStream.
       
  2884     inStream isNil ifTrue:[
       
  2885         "open failed, but somenone did a proceed for the OpenError.
       
  2886          Ignore this file but continue in order to copy the rest when
       
  2887          doing a recursive copy"
       
  2888         ^ self.
       
  2889     ].
       
  2890 
  2884     [
  2891     [
  2885         newNameAlreadyExists := newName exists.
  2892         newNameAlreadyExists := newName exists.
  2886         outStream := newName writeStream.
  2893         outStream := newName writeStream.
  2887         newNameAlreadyExists ifFalse:[
  2894         newNameAlreadyExists ifFalse:[
  2888             [
  2895             [
  2895         inStream binary; buffered:false.
  2902         inStream binary; buffered:false.
  2896         outStream binary; buffered:false.
  2903         outStream binary; buffered:false.
  2897         inStream copyToEndInto:outStream.
  2904         inStream copyToEndInto:outStream.
  2898     ] ensure:[
  2905     ] ensure:[
  2899         inStream close.
  2906         inStream close.
  2900         outStream notNil ifTrue:[outStream syncData; close].
  2907         outStream notNil ifTrue:[outStream close].
  2901     ].
  2908     ].
  2902 
  2909 
  2903     "
  2910     "
  2904      'Make.proto' asFilename copyTo:'/tmp/Makefile.foo'
  2911      'Make.proto' asFilename copyTo:'/tmp/Makefile.foo'
  2905      'Make.proto' asFilename copyTo:'/'
  2912      'Make.proto' asFilename copyTo:'/'
  2967         writeStream := self newReadWriteStream.
  2974         writeStream := self newReadWriteStream.
  2968     ].
  2975     ].
  2969     writeStream close.
  2976     writeStream close.
  2970 !
  2977 !
  2971 
  2978 
       
  2979 createAsSymbolicLinkTo:linkFilenameString
       
  2980     "create a directory with the receivers name.
       
  2981      Raises an exception if not successful"
       
  2982 
       
  2983     OperatingSystem createSymbolicLinkFrom:linkFilenameString to:self pathName.
       
  2984 
       
  2985     "
       
  2986         '/tmp/link' asFilename makeSymbolicLinkTo:'bla'
       
  2987     "
       
  2988 !
       
  2989 
  2972 delete
  2990 delete
  2973     "remove the file - same as remove, for ST-80 compatibility"
  2991     "remove the file - same as remove, for ST-80 compatibility"
  2974 
  2992 
  2975     self remove
  2993     self remove
  2976 !
  2994 !
  3074 
  3092 
  3075 recursiveCopyTo:destination
  3093 recursiveCopyTo:destination
  3076     "if I represent a regular file, copy it.
  3094     "if I represent a regular file, copy it.
  3077      Otherwise, copy the directory and recursively
  3095      Otherwise, copy the directory and recursively
  3078      all of its subfiles/subdirectories.
  3096      all of its subfiles/subdirectories.
  3079      Raises an exception if not successful."
  3097 
  3080 
  3098      Raises an exception if not successful.
  3081     |ok d|
  3099      Do not resolve symbolic links.
  3082 
  3100      If a whole directory is to be copied and the destination directory 
       
  3101      does not exist, it will be created."
       
  3102 
       
  3103     |ok destinationFilename|
       
  3104 
       
  3105     destinationFilename := destination asFilename.
  3083     self isDirectory ifFalse:[
  3106     self isDirectory ifFalse:[
  3084         d := destination asFilename.
  3107         destinationFilename isDirectory ifTrue:[
  3085         d isDirectory ifTrue:[
  3108             destinationFilename := destinationFilename construct:self baseName.
  3086             d := d construct:self baseName.
       
  3087         ].
  3109         ].
  3088         self copyTo:d.
  3110         self copyTo:destinationFilename.
  3089         ^ self.
  3111         ^ self.
  3090     ].
  3112     ].
  3091 
  3113 
  3092     "/ typically, an 'cp -r' is faster;
  3114     "/ typically, an 'cp -r' is faster;
  3093     "/ however, if the command fails (or the OS does not support it),
  3115     "/ however, if the command fails (or the OS does not support it),
  3094     "/ fallBack doing a manual directory walk.
  3116     "/ fallBack doing a manual directory walk.
  3095 
  3117 
  3096     ok := OperatingSystem 
  3118     ok := OperatingSystem 
  3097             recursiveCopyDirectory:(self osNameForDirectory)
  3119             recursiveCopyDirectory:(self osNameForDirectory)
  3098             to:(destination asFilename osNameForDirectory).
  3120             to:(destinationFilename osNameForDirectory).
  3099 
  3121 
  3100     ok ifFalse:[
  3122     ok ifFalse:[
  3101         self recursiveCopyWithoutOSCommandTo:destination
  3123         self recursiveCopyWithoutOSCommandTo:destinationFilename
  3102     ].
  3124     ].
       
  3125 
       
  3126     "
       
  3127         '.' asFilename recursiveCopyTo:'/tmp/xxx'.
       
  3128     "
  3103 
  3129 
  3104     "Created: / 05-05-1999 / 13:35:01 / cg"
  3130     "Created: / 05-05-1999 / 13:35:01 / cg"
  3105     "Modified: / 31-05-1999 / 13:11:34 / cg"
  3131     "Modified: / 31-05-1999 / 13:11:34 / cg"
  3106     "Modified: / 29-07-2010 / 12:41:06 / sr"
  3132     "Modified: / 29-07-2010 / 12:41:06 / sr"
  3107 !
  3133 !
  3108 
  3134 
  3109 recursiveCopyWithoutOSCommandTo:destination
  3135 recursiveCopyWithoutOSCommandTo:destination
  3110     "if I represent a regular file, copy it.
  3136     "if I represent a regular file, copy it.
  3111      Otherwise, copy the directory and all of its subfiles/subdirectories.
  3137      Otherwise, copy the directory and all of its subfiles/subdirectories.
  3112      This one walks down the directory hierarchy, not using any OS command to do the copy.
  3138      This one walks down the directory hierarchy, not using any OS command to do the copy.
  3113      Raises an exception if not successful."
  3139      Raises an exception if not successful.
       
  3140 
       
  3141      Do not resolve symbolic links.
       
  3142      If a whole directory is to be copied and the destination directory 
       
  3143      does not exist, it will be created."
  3114 
  3144 
  3115     |destinationFilename|
  3145     |destinationFilename|
  3116 
  3146 
  3117     destinationFilename := destination asFilename.
  3147     destinationFilename := destination asFilename.
  3118 
  3148 
  3119     self isDirectory ifTrue:[
  3149     self isDirectory ifTrue:[
  3120         destinationFilename exists ifTrue:[
  3150         destinationFilename exists ifFalse:[
  3121             destinationFilename := destinationFilename construct:self baseName.
       
  3122             destinationFilename makeDirectory.
       
  3123         ] ifFalse:[
       
  3124             destinationFilename makeDirectory.
  3151             destinationFilename makeDirectory.
  3125             destinationFilename accessRights:self accessRights.
  3152             destinationFilename accessRights:self accessRights.
  3126         ].
  3153         ].
  3127 
  3154 
  3128         self directoryContents do:[:aFilenameString |
  3155         self directoryContents do:[:aFilenameString |
  3129             |src dst|
  3156             |src dst|
  3130 
  3157 
  3131             src := self construct:aFilenameString.
  3158             src := self construct:aFilenameString.
       
  3159             dst := destinationFilename construct:aFilenameString.
       
  3160 
  3132             src isDirectory ifTrue:[
  3161             src isDirectory ifTrue:[
  3133                 src recursiveCopyWithoutOSCommandTo:destinationFilename
  3162                 src recursiveCopyWithoutOSCommandTo:dst
       
  3163             ] ifFalse:[src isSymbolicLink ifTrue:[                    
       
  3164                 dst
       
  3165                     remove;
       
  3166                     createAsSymbolicLinkTo:src linkInfo path.
  3134             ] ifFalse:[
  3167             ] ifFalse:[
  3135                 src copyTo:(destinationFilename construct:aFilenameString)
  3168                 src copyTo:dst.
  3136             ].
  3169             ]].
  3137         ]
  3170         ].
  3138     ] ifFalse:[
  3171     ] ifFalse:[
  3139         self copyTo:destinationFilename
  3172         destinationFilename isDirectory ifTrue:[
       
  3173             destinationFilename := destinationFilename construct:self baseName.
       
  3174         ].
       
  3175         self copyTo:destinationFilename.
  3140     ]
  3176     ]
  3141 
  3177 
  3142     "
  3178     "
  3143      '.' asFilename recursiveCopyWithoutOSCommandTo:'/tmp/xxx'.
  3179      '.' asFilename recursiveCopyWithoutOSCommandTo:'/tmp/xxx'.
  3144      'smalltalk.rc' asFilename recursiveCopyWithoutOSCommandTo:'/tmp/xxx'.
  3180      'smalltalk.rc' asFilename recursiveCopyWithoutOSCommandTo:'/tmp/xxx'.
  6058 ! !
  6094 ! !
  6059 
  6095 
  6060 !Filename class methodsFor:'documentation'!
  6096 !Filename class methodsFor:'documentation'!
  6061 
  6097 
  6062 version
  6098 version
  6063     ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.431 2014-11-23 16:51:36 cg Exp $'
  6099     ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.432 2014-11-27 16:48:48 stefan Exp $'
  6064 !
  6100 !
  6065 
  6101 
  6066 version_CVS
  6102 version_CVS
  6067     ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.431 2014-11-23 16:51:36 cg Exp $'
  6103     ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.432 2014-11-27 16:48:48 stefan Exp $'
  6068 ! !
  6104 ! !
  6069 
  6105 
  6070 
  6106 
  6071 Filename initialize!
  6107 Filename initialize!