author | Claus Gittinger <cg@exept.de> |
Sat, 08 Mar 2008 11:53:23 +0100 | |
changeset 1932 | 7bafb4c076d3 |
parent 1519 | 60bb9cc8727e |
child 2474 | 38de996b9ee0 |
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 |
||
1374 | 76 |
!FileURI methodsFor:'initialization'! |
1005 | 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. |
|
1519 | 137 |
attributes at:#fileSize put:file fileSize. |
1254 | 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 |
" |
|
1519 | 149 |
'/etc/group' asURI readStreamDo:[:stream :attributes| |
150 |
stream contents |
|
151 |
addFirst:attributes printString; |
|
152 |
yourself |
|
153 |
]. |
|
154 |
||
1005 | 155 |
'file:/etc/group' asURI readStreamDo:[:stream :attributes| |
156 |
stream contents |
|
157 |
addFirst:attributes printString; |
|
158 |
yourself |
|
159 |
]. |
|
160 |
" |
|
161 |
! |
|
162 |
||
1254 | 163 |
readStreamsDo:aBlock |
164 |
"evaluate the block with a Collection of streams as first argument |
|
165 |
and a dictionary containing attributes as second argument, |
|
166 |
- a collection with a stream on a single file, |
|
167 |
- or a collection with streams on a directorie's files, but not recursive" |
|
168 |
||
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
169 |
self readStreamsDo:aBlock renameBlock:nil. |
1258 | 170 |
! |
171 |
||
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
172 |
readStreamsDo:aBlock renameBlock:renameBlock |
1258 | 173 |
"evaluate the block with a Collection of streams as first argument |
174 |
and a dictionary containing attributes as second argument, |
|
175 |
- 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
|
176 |
- or a collection with streams on a directories files, but not recursive" |
1258 | 177 |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
178 |
|attributes fn files list baseName| |
1254 | 179 |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
180 |
fn := self asFilename. |
1254 | 181 |
files := OrderedCollection new. |
1264 | 182 |
list := OrderedCollection new. |
1254 | 183 |
attributes := self class attributes. |
1264 | 184 |
|
1254 | 185 |
fn isDirectory ifTrue:[ |
186 |
attributes at:#requestDirectory put:true. |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
187 |
fn directoryContentsAsFilenamesDo:[:eachFilename| |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
188 |
eachFilename isDirectory ifFalse:[ |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
189 |
files add:eachFilename |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
190 |
]. |
1254 | 191 |
]. |
192 |
] ifFalse:[ |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
193 |
baseName := fn baseName. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
194 |
(baseName includesAny:'*?[]') ifTrue:[ |directoryName| |
1264 | 195 |
attributes at:#requestDirectory put:true. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
196 |
directoryName := fn directory. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
197 |
directoryName directoryContentsDo:[:eachFilenameString| |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
198 |
(baseName match:eachFilenameString) ifTrue:[ |filename| |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
199 |
filename := directoryName construct:eachFilenameString. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
200 |
filename isDirectory ifFalse:[ |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
201 |
files add:(directoryName construct:eachFilenameString). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
202 |
]. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
203 |
]. |
1264 | 204 |
]. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
205 |
] ifFalse:[ |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
206 |
attributes at:#requestDirectory put:false. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
207 |
files add:fn. |
1264 | 208 |
]. |
209 |
]. |
|
210 |
||
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
211 |
files do:[:eachFilename| |baseName stream| |
1254 | 212 |
[ |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
213 |
baseName := eachFilename baseName. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
214 |
attributes at:#fileSize put:eachFilename fileSize. |
1254 | 215 |
attributes at:#baseName put:baseName. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
216 |
(self pathSegments includes:baseName) ifTrue:[ |
1258 | 217 |
attributes at:#uriInfo put:self. |
1266 | 218 |
] ifFalse:[ |uri col| |
219 |
uri := self copy. |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
220 |
col := self pathSegments copy. |
1266 | 221 |
col removeLast. |
222 |
col add:baseName. |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
223 |
uri pathSegments:col. |
1266 | 224 |
attributes at:#uriInfo put:uri. |
1254 | 225 |
]. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
226 |
stream := eachFilename readStream. |
1258 | 227 |
aBlock value:stream value:attributes. |
1254 | 228 |
] ensure:[ |
229 |
stream notNil ifTrue:[stream close] |
|
230 |
]. |
|
1271
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 |
renameBlock notNil ifTrue:[ |renameFilenameString| |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
233 |
renameFilenameString := renameBlock value:eachFilename pathName. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
234 |
renameFilenameString asFilename exists ifTrue:[ |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
235 |
renameFilenameString := renameFilenameString, '.', |
1436
e680f637d998
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
1374
diff
changeset
|
236 |
(Timestamp now printStringFormat:'%(year)%(mon)%(day)%h%m%s'). |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
237 |
]. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
238 |
eachFilename moveTo:renameFilenameString. |
1258 | 239 |
]. |
1254 | 240 |
]. |
241 |
||
242 |
" |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
243 |
(URI fromString:'file:~/test/out') |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
244 |
readStreamsDo:[:stream :attributes | |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
245 |
Transcript showCR:(attributes at:#baseName). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
246 |
Transcript showCR:(attributes at:#fileSize). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
247 |
Transcript showCR:(attributes at:#requestDirectory). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
248 |
Transcript showCR:(attributes at:#uriInfo). |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
249 |
]. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
250 |
(URI fromString:'file:~/test/out/*1') |
1254 | 251 |
readStreamsDo:[:stream :attributes | |
252 |
Transcript showCR:(attributes at:#baseName). |
|
253 |
Transcript showCR:(attributes at:#fileSize). |
|
254 |
Transcript showCR:(attributes at:#requestDirectory). |
|
255 |
Transcript showCR:(attributes at:#uriInfo). |
|
256 |
]. |
|
257 |
" |
|
258 |
! |
|
259 |
||
260 |
writeStreamDo:aBlock |
|
261 |
"evaluate a block with the write stream as first argument |
|
262 |
and a dictionary containing attributes as second argument. |
|
263 |
The stream is closed after aBlock has been evaluated. |
|
264 |
Attributes may be the mime type (key #MIME)" |
|
265 |
||
266 |
|stream| |
|
267 |
||
268 |
^ [ |
|
269 |
stream := self asFilename writeStream. |
|
270 |
aBlock value:stream value:self class attributes |
|
271 |
] ensure:[ |
|
272 |
stream notNil ifTrue:[stream close] |
|
273 |
]. |
|
274 |
||
275 |
" |
|
276 |
(URI fromString:'file:/home/tm/tmp') |
|
277 |
readStreamsDo:[:stream :attributes| |
|
278 |
Transcript showCR:(attributes at:#MIME). |
|
279 |
Transcript showCR:(stream isWritable). |
|
280 |
]. |
|
281 |
" |
|
1258 | 282 |
! |
283 |
||
284 |
writeStreamDo:aBlock create:doCreate |
|
1268 | 285 |
|
286 |
^ self writeStreamDo:aBlock create:doCreate atomic:false. |
|
287 |
! |
|
288 |
||
289 |
writeStreamDo:aBlock create:doCreate atomic:doAtomic |
|
1258 | 290 |
"evaluate a block with the write stream as first argument |
291 |
and a dictionary containing attributes as second argument. |
|
292 |
The stream is closed after aBlock has been evaluated. |
|
1268 | 293 |
Attributes may be the mime type (key #MIME) |
294 |
||
295 |
If doCreate is true, a nonExistent directory will be created. |
|
296 |
If doAtomic is true, files will appear atomically, by using |
|
297 |
an intermediate file theat will be renamed" |
|
1258 | 298 |
|
1268 | 299 |
|stream fileName toFileName| |
1258 | 300 |
|
1268 | 301 |
fileName := self asFilename. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
302 |
toFileName := fileName. |
1268 | 303 |
doAtomic ifTrue:[ |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
304 |
fileName isDirectory ifFalse:[ |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
305 |
toFileName := fileName directory. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
306 |
]. |
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
307 |
toFileName := toFileName construct:'.transferFile'. |
1268 | 308 |
]. |
1258 | 309 |
[ |
310 |
Stream streamErrorSignal handle:[:ex| |
|
311 |
doCreate ifFalse:[ |
|
312 |
ex reject |
|
313 |
]. |
|
1268 | 314 |
fileName directory recursiveMakeDirectory. |
1284 | 315 |
self exists ifTrue:[ |infoStream| |
316 |
infoStream := '' writeStream. |
|
317 |
self publicPrintOn:infoStream. |
|
318 |
self error:('Local write: Datei %1 already exists!!' bindWith:infoStream contents). |
|
319 |
]. |
|
1268 | 320 |
stream := toFileName writeStream. |
1258 | 321 |
] do:[ |
1284 | 322 |
self exists ifTrue:[ |infoStream| |
323 |
infoStream := '' writeStream. |
|
324 |
self publicPrintOn:infoStream. |
|
325 |
self error:('Local write: Datei %1 already exists!!' bindWith:infoStream contents). |
|
326 |
]. |
|
1268 | 327 |
stream := toFileName writeStream. |
1258 | 328 |
]. |
1268 | 329 |
aBlock value:stream value:self class attributes. |
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
330 |
stream close. |
1268 | 331 |
doAtomic ifTrue:[ |
332 |
toFileName moveTo:fileName. |
|
333 |
] |
|
1271
ca2e206e7c7f
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
334 |
] ifCurtailed:[ |
1258 | 335 |
stream notNil ifTrue:[stream close] |
336 |
]. |
|
337 |
||
338 |
" |
|
339 |
(URI fromString:'file:/home/tm/tmp') |
|
340 |
readStreamsDo:[:stream :attributes| |
|
341 |
Transcript showCR:(attributes at:#MIME). |
|
342 |
Transcript showCR:(stream isWritable). |
|
343 |
]. |
|
344 |
" |
|
1005 | 345 |
! ! |
346 |
||
347 |
!FileURI class methodsFor:'documentation'! |
|
348 |
||
349 |
version |
|
1519 | 350 |
^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.14 2005-02-01 12:42:02 stefan Exp $' |
1005 | 351 |
! ! |