author | tm |
Thu, 24 Jul 2003 11:29:43 +0200 | |
changeset 1284 | 57550436b55b |
parent 1271 | ca2e206e7c7f |
child 1309 | c752d54f4e09 |
permissions | -rw-r--r-- |
1005 | 1 |
"{ Package: 'stx:libbasic2' }" |
2 |
||
3 |
HierarchicalURI subclass:#FileURI |
|
4 |
instanceVariableNames:'' |
|
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
7 |
category:'Resources' |
|
8 |
! |
|
9 |
||
10 |
||
11 |
!FileURI class methodsFor:'instance creation'! |
|
12 |
||
13 |
fromFilename:aFilename |
|
14 |
"create an URI based on an a filename" |
|
15 |
||
16 |
^ self new fromFilename:aFilename |
|
17 |
! ! |
|
18 |
||
19 |
!FileURI class methodsFor:'accessing'! |
|
20 |
||
21 |
schemes |
|
22 |
"answer the list of supported schemes" |
|
23 |
||
24 |
^ #(file) |
|
25 |
! ! |
|
26 |
||
27 |
!FileURI methodsFor:'converting'! |
|
28 |
||
29 |
asFilename |
|
30 |
"answer the receiver represented as filename" |
|
31 |
||
32 |
^ authority notNil ifTrue:[ |
|
33 |
Filename remoteHost:authority rootComponents:pathSegments. |
|
34 |
] ifFalse:[ |
|
1268 | 35 |
"kludge" |
36 |
(pathSegments first startsWith:$~) ifTrue:[ |
|
37 |
pathSegments first asFilename construct:(Filename rootComponents:(pathSegments copyFrom:2)). |
|
38 |
] ifFalse:[ |
|
39 |
Filename rootComponents:pathSegments. |
|
40 |
]. |
|
1005 | 41 |
]. |
1268 | 42 |
|
43 |
" |
|
44 |
(URI fromString:'file:~/bla') asFilename |
|
45 |
(URI fromString:'file:~root/bla') asFilename |
|
46 |
" |
|
1005 | 47 |
! ! |
48 |
||
49 |
!FileURI methodsFor:'initialize'! |
|
50 |
||
51 |
fromFilename:aFilename |
|
52 |
"create an URI based on an a filename" |
|
53 |
||
54 |
|components| |
|
55 |
||
56 |
components := aFilename components. |
|
57 |
aFilename isAbsolute ifTrue:[ |
|
58 |
(components size > 3 and:[(components at:2) size == 0]) ifTrue:[ |
|
59 |
"this is a MS-Windows network path: \\host\path" |
|
60 |
authority := components at:3. |
|
61 |
pathSegments := components copyFrom:4. |
|
62 |
] ifFalse:[ |
|
63 |
"this is an absolute path" |
|
64 |
isAbsolute := true. |
|
65 |
pathSegments := components copyFrom:2. |
|
66 |
]. |
|
67 |
] ifFalse:[ |
|
68 |
"this is a relative path" |
|
69 |
isAbsolute := false. |
|
70 |
pathSegments := components. |
|
71 |
] |
|
72 |
||
73 |
" |
|
1258 | 74 |
self fromFilename:'/a/b/c' asFilename |
75 |
self fromFilename:'//a/b/c' asFilename |
|
76 |
self fromFilename:'a/b/c' asFilename |
|
1005 | 77 |
" |
78 |
! ! |
|
79 |
||
1254 | 80 |
!FileURI methodsFor:'queries'! |
81 |
||
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
82 |
exists |
1254 | 83 |
|
84 |
^ self asFilename exists |
|
85 |
! ! |
|
86 |
||
1005 | 87 |
!FileURI methodsFor:'stream access'! |
88 |
||
89 |
readStream |
|
90 |
||
91 |
^ self asFilename readStream |
|
92 |
||
93 |
" |
|
94 |
'file:/etc/group' asURI readStream contents |
|
1268 | 95 |
'file:/~/.profile' asURI readStream contents |
96 |
(URI fromString:'file:~/.profile') asFilename |
|
97 |
(URI fromString:'file:~/.profile') readStream upToEnd |
|
1005 | 98 |
" |
99 |
! |
|
100 |
||
101 |
readStreamDo:aBlock |
|
102 |
"evaluate a block with the read stream as first argument |
|
103 |
and a dictionary containing attributes as second argument. |
|
104 |
The stream is closed after aBlock has been evaluated." |
|
105 |
||
1254 | 106 |
|attributes file stream| |
1005 | 107 |
|
1254 | 108 |
attributes := self class attributes. |
109 |
file := self asFilename. |
|
110 |
attributes at:#fileSize put:(file fileSize). |
|
111 |
attributes at:#baseName put:file baseName. |
|
112 |
attributes at:#uriInfo put:self printString. |
|
1005 | 113 |
|
1254 | 114 |
^ [ |
115 |
stream := file readStream. |
|
1005 | 116 |
aBlock value:stream value:attributes |
117 |
] ensure:[ |
|
118 |
stream notNil ifTrue:[stream close] |
|
119 |
]. |
|
120 |
||
121 |
" |
|
122 |
'file:/etc/group' asURI readStreamDo:[:stream :attributes| |
|
123 |
stream contents |
|
124 |
addFirst:attributes printString; |
|
125 |
yourself |
|
126 |
]. |
|
127 |
" |
|
128 |
! |
|
129 |
||
1254 | 130 |
readStreamsDo:aBlock |
131 |
"evaluate the block with a Collection of streams as first argument |
|
132 |
and a dictionary containing attributes as second argument, |
|
133 |
- a collection with a stream on a single file, |
|
134 |
- or a collection with streams on a directorie's files, but not recursive" |
|
135 |
||
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
136 |
self readStreamsDo:aBlock renameBlock:nil. |
1258 | 137 |
! |
138 |
||
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
139 |
readStreamsDo:aBlock renameBlock:renameBlock |
1258 | 140 |
"evaluate the block with a Collection of streams as first argument |
141 |
and a dictionary containing attributes as second argument, |
|
142 |
- a collection with a stream on a single file, |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
143 |
- or a collection with streams on a directories files, but not recursive" |
1258 | 144 |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
145 |
|attributes fn files list baseName| |
1254 | 146 |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
147 |
fn := self asFilename. |
1254 | 148 |
files := OrderedCollection new. |
1264 | 149 |
list := OrderedCollection new. |
1254 | 150 |
attributes := self class attributes. |
1264 | 151 |
|
1254 | 152 |
fn isDirectory ifTrue:[ |
153 |
attributes at:#requestDirectory put:true. |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
154 |
fn directoryContentsAsFilenamesDo:[:eachFilename| |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
155 |
eachFilename isDirectory ifFalse:[ |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
156 |
files add:eachFilename |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
157 |
]. |
1254 | 158 |
]. |
159 |
] ifFalse:[ |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
160 |
baseName := fn baseName. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
161 |
(baseName includesAny:'*?[]') ifTrue:[ |directoryName| |
1264 | 162 |
attributes at:#requestDirectory put:true. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
163 |
directoryName := fn directory. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
164 |
directoryName directoryContentsDo:[:eachFilenameString| |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
165 |
(baseName match:eachFilenameString) ifTrue:[ |filename| |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
166 |
filename := directoryName construct:eachFilenameString. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
167 |
filename isDirectory ifFalse:[ |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
168 |
files add:(directoryName construct:eachFilenameString). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
169 |
]. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
170 |
]. |
1264 | 171 |
]. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
172 |
] ifFalse:[ |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
173 |
attributes at:#requestDirectory put:false. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
174 |
files add:fn. |
1264 | 175 |
]. |
176 |
]. |
|
177 |
||
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
178 |
files do:[:eachFilename| |baseName stream| |
1254 | 179 |
[ |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
180 |
baseName := eachFilename baseName. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
181 |
attributes at:#fileSize put:eachFilename fileSize. |
1254 | 182 |
attributes at:#baseName put:baseName. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
183 |
(self pathSegments includes:baseName) ifTrue:[ |
1258 | 184 |
attributes at:#uriInfo put:self. |
1266 | 185 |
] ifFalse:[ |uri col| |
186 |
uri := self copy. |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
187 |
col := self pathSegments copy. |
1266 | 188 |
col removeLast. |
189 |
col add:baseName. |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
190 |
uri pathSegments:col. |
1266 | 191 |
attributes at:#uriInfo put:uri. |
1254 | 192 |
]. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
193 |
stream := eachFilename readStream. |
1258 | 194 |
aBlock value:stream value:attributes. |
1254 | 195 |
] ensure:[ |
196 |
stream notNil ifTrue:[stream close] |
|
197 |
]. |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
198 |
|
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
199 |
renameBlock notNil ifTrue:[ |renameFilenameString| |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
200 |
renameFilenameString := renameBlock value:eachFilename pathName. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
201 |
renameFilenameString asFilename exists ifTrue:[ |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
202 |
renameFilenameString := renameFilenameString, '.', |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
203 |
(AbsoluteTime now printStringFormat:'%(year)%(mon)%(day)%h%m%s'). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
204 |
]. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
205 |
eachFilename moveTo:renameFilenameString. |
1258 | 206 |
]. |
1254 | 207 |
]. |
208 |
||
209 |
" |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
210 |
(URI fromString:'file:~/test/out') |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
211 |
readStreamsDo:[:stream :attributes | |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
212 |
Transcript showCR:(attributes at:#baseName). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
213 |
Transcript showCR:(attributes at:#fileSize). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
214 |
Transcript showCR:(attributes at:#requestDirectory). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
215 |
Transcript showCR:(attributes at:#uriInfo). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
216 |
]. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
217 |
(URI fromString:'file:~/test/out/*1') |
1254 | 218 |
readStreamsDo:[:stream :attributes | |
219 |
Transcript showCR:(attributes at:#baseName). |
|
220 |
Transcript showCR:(attributes at:#fileSize). |
|
221 |
Transcript showCR:(attributes at:#requestDirectory). |
|
222 |
Transcript showCR:(attributes at:#uriInfo). |
|
223 |
]. |
|
224 |
" |
|
225 |
! |
|
226 |
||
227 |
writeStreamDo:aBlock |
|
228 |
"evaluate a block with the write stream as first argument |
|
229 |
and a dictionary containing attributes as second argument. |
|
230 |
The stream is closed after aBlock has been evaluated. |
|
231 |
Attributes may be the mime type (key #MIME)" |
|
232 |
||
233 |
|stream| |
|
234 |
||
235 |
^ [ |
|
236 |
stream := self asFilename writeStream. |
|
237 |
aBlock value:stream value:self class attributes |
|
238 |
] ensure:[ |
|
239 |
stream notNil ifTrue:[stream close] |
|
240 |
]. |
|
241 |
||
242 |
" |
|
243 |
(URI fromString:'file:/home/tm/tmp') |
|
244 |
readStreamsDo:[:stream :attributes| |
|
245 |
Transcript showCR:(attributes at:#MIME). |
|
246 |
Transcript showCR:(stream isWritable). |
|
247 |
]. |
|
248 |
" |
|
1258 | 249 |
! |
250 |
||
251 |
writeStreamDo:aBlock create:doCreate |
|
1268 | 252 |
|
253 |
^ self writeStreamDo:aBlock create:doCreate atomic:false. |
|
254 |
! |
|
255 |
||
256 |
writeStreamDo:aBlock create:doCreate atomic:doAtomic |
|
1258 | 257 |
"evaluate a block with the write stream as first argument |
258 |
and a dictionary containing attributes as second argument. |
|
259 |
The stream is closed after aBlock has been evaluated. |
|
1268 | 260 |
Attributes may be the mime type (key #MIME) |
261 |
||
262 |
If doCreate is true, a nonExistent directory will be created. |
|
263 |
If doAtomic is true, files will appear atomically, by using |
|
264 |
an intermediate file theat will be renamed" |
|
1258 | 265 |
|
1268 | 266 |
|stream fileName toFileName| |
1258 | 267 |
|
1268 | 268 |
fileName := self asFilename. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
269 |
toFileName := fileName. |
1268 | 270 |
doAtomic ifTrue:[ |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
271 |
fileName isDirectory ifFalse:[ |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
272 |
toFileName := fileName directory. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
273 |
]. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
274 |
toFileName := toFileName construct:'.transferFile'. |
1268 | 275 |
]. |
1258 | 276 |
[ |
277 |
Stream streamErrorSignal handle:[:ex| |
|
278 |
doCreate ifFalse:[ |
|
279 |
ex reject |
|
280 |
]. |
|
1268 | 281 |
fileName directory recursiveMakeDirectory. |
1284 | 282 |
self exists ifTrue:[ |infoStream| |
283 |
infoStream := '' writeStream. |
|
284 |
self publicPrintOn:infoStream. |
|
285 |
self error:('Local write: Datei %1 already exists!!' bindWith:infoStream contents). |
|
286 |
]. |
|
1268 | 287 |
stream := toFileName writeStream. |
1258 | 288 |
] do:[ |
1284 | 289 |
self exists ifTrue:[ |infoStream| |
290 |
infoStream := '' writeStream. |
|
291 |
self publicPrintOn:infoStream. |
|
292 |
self error:('Local write: Datei %1 already exists!!' bindWith:infoStream contents). |
|
293 |
]. |
|
1268 | 294 |
stream := toFileName writeStream. |
1258 | 295 |
]. |
1268 | 296 |
aBlock value:stream value:self class attributes. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
297 |
stream close. |
1268 | 298 |
doAtomic ifTrue:[ |
299 |
toFileName moveTo:fileName. |
|
300 |
] |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
301 |
] ifCurtailed:[ |
1258 | 302 |
stream notNil ifTrue:[stream close] |
303 |
]. |
|
304 |
||
305 |
" |
|
306 |
(URI fromString:'file:/home/tm/tmp') |
|
307 |
readStreamsDo:[:stream :attributes| |
|
308 |
Transcript showCR:(attributes at:#MIME). |
|
309 |
Transcript showCR:(stream isWritable). |
|
310 |
]. |
|
311 |
" |
|
1005 | 312 |
! ! |
313 |
||
314 |
!FileURI class methodsFor:'documentation'! |
|
315 |
||
316 |
version |
|
1284 | 317 |
^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.9 2003-07-24 09:29:43 tm Exp $' |
1005 | 318 |
! ! |