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