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