author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 11:09:04 +0200 | |
changeset 5048 | 63f42cfbaf59 |
parent 4882 | cdc1e27ab112 |
child 5211 | 945a2ac21f15 |
permissions | -rw-r--r-- |
4878 | 1 |
"{ Encoding: utf8 }" |
2 |
||
3 |
" |
|
4 |
COPYRIGHT (c) 2009 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
3639 | 14 |
"{ Package: 'stx:libbasic2' }" |
2473 | 15 |
|
3639 | 16 |
"{ NameSpace: Smalltalk }" |
2473 | 17 |
|
18 |
HierarchicalURI subclass:#SftpURI |
|
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Net-Resources' |
|
23 |
! |
|
24 |
||
25 |
Object subclass:#WriteStreamSimulator |
|
26 |
instanceVariableNames:'connection remotePath' |
|
27 |
classVariableNames:'' |
|
28 |
poolDictionaries:'' |
|
29 |
privateIn:SftpURI |
|
30 |
! |
|
31 |
||
32 |
!SftpURI class methodsFor:'documentation'! |
|
33 |
||
34 |
copyright |
|
35 |
" |
|
36 |
COPYRIGHT (c) 2009 by eXept Software AG |
|
37 |
All Rights Reserved |
|
38 |
||
39 |
This software is furnished under a license and may be used |
|
40 |
only in accordance with the terms of that license and with the |
|
41 |
inclusion of the above copyright notice. This software may not |
|
42 |
be provided or otherwise made available to, or used by, any |
|
43 |
other person. No title to or ownership of the software is |
|
44 |
hereby transferred. |
|
45 |
" |
|
46 |
! ! |
|
47 |
||
48 |
!SftpURI class methodsFor:'accessing'! |
|
49 |
||
50 |
schemes |
|
51 |
||
52 |
^ #(sftp) |
|
53 |
! ! |
|
54 |
||
55 |
!SftpURI class methodsFor:'ftp requests'! |
|
56 |
||
57 |
pathExists:aPathname ftpClient:aFtpClient |
|
58 |
|list| |
|
59 |
||
60 |
(OpenError, FTPClient fileNotFoundErrorSignal) handle:[:ex|] do:[ |
|
61 |
list := aFtpClient directoryContentsOf:aPathname. |
|
62 |
^ list size = 1 and:[list first = aPathname]. |
|
63 |
]. |
|
64 |
^ false. |
|
65 |
! ! |
|
66 |
||
67 |
!SftpURI methodsFor:'defaults'! |
|
68 |
||
69 |
defaultPort |
|
70 |
"answer the default ssh port here" |
|
71 |
||
72 |
^ 22 |
|
73 |
! |
|
74 |
||
75 |
defaultUser |
|
76 |
^ OperatingSystem getLoginName |
|
77 |
! ! |
|
78 |
||
79 |
!SftpURI methodsFor:'ftp requests'! |
|
80 |
||
81 |
connectThenDo:aOneArgBlock |
|
4878 | 82 |
"setup a ftp connection and call aOneArgBlock with it. |
83 |
Ensures that the sftp connection is closed afterwards." |
|
2473 | 84 |
|
85 |
|sftpClient| |
|
86 |
||
3639 | 87 |
SftpClient isNil ifTrue:[ |
88 |
Smalltalk loadPackage:'stx:goodies/communication'. |
|
4878 | 89 |
]. |
90 |
^ [ |
|
91 |
sftpClient := SftpClient new |
|
92 |
hostname:self host; |
|
93 |
username:self user; |
|
94 |
port:self port. |
|
2473 | 95 |
|
4878 | 96 |
aOneArgBlock value:sftpClient. |
97 |
] ensure:[ |
|
98 |
sftpClient notNil ifTrue:[ |
|
99 |
sftpClient close |
|
100 |
] |
|
101 |
]. |
|
102 |
||
103 |
"Modified: / 18-03-2019 / 10:54:22 / Claus Gittinger" |
|
2473 | 104 |
! ! |
105 |
||
106 |
!SftpURI methodsFor:'stream access'! |
|
107 |
||
108 |
readStreamDo:aBlock |
|
4878 | 109 |
"evaluate a block with the read stream as first argument |
110 |
and a dictionary containing attributes as second argument. |
|
111 |
The stream is closed after aBlock has been evaluated." |
|
112 |
||
4882 | 113 |
^ self connectThenDo:[:ftp| |
4878 | 114 |
|stream| |
2473 | 115 |
[ |
4878 | 116 |
|path attributes| |
117 |
||
2473 | 118 |
path := self path. |
119 |
attributes := self class attributes. |
|
120 |
attributes at:#fileSize put:(ftp sizeOf:path). |
|
121 |
attributes at:#baseName put:self pathSegments last. |
|
122 |
attributes at:#uriInfo put:self printString. |
|
123 |
||
124 |
stream := ftp getStreamFor:path. |
|
3451 | 125 |
aBlock value:stream optionalArgument:attributes. |
2473 | 126 |
] ensure:[ |
127 |
stream notNil ifTrue:[ |
|
128 |
stream close. |
|
129 |
]. |
|
130 |
]. |
|
131 |
]. |
|
132 |
||
133 |
" |
|
134 |
'ftp://stefan:password@ftp.exept.de/etc/group' asURI readStreamDo:[:stream :attributes | |
|
135 |
self halt |
|
136 |
]. |
|
137 |
" |
|
4878 | 138 |
|
4882 | 139 |
"Modified: / 18-03-2019 / 11:02:01 / Claus Gittinger" |
2473 | 140 |
! |
141 |
||
142 |
readStreamsDo:aBlock |
|
143 |
"evaluate the block with a Collection of streams as first argument |
|
144 |
and a dictionary containing attributes as second argument, |
|
145 |
- a collection with a stream on a single file, |
|
146 |
- or a collection with streams on a directory's files, but not recursive |
|
147 |
||
148 |
The streams are closed after aBlock has been evaluated. |
|
149 |
Attributes may be the mime type (key #MIME)" |
|
150 |
||
151 |
self readStreamsDo:aBlock skipFilenamesWithSuffix:nil renameBlock:nil |
|
152 |
! |
|
153 |
||
154 |
readStreamsDo:aBlock filterBlock:oneArgFilterBlock renameBlock:renameBlock |
|
155 |
"evaluate the block with a Collection of streams as first argument |
|
156 |
and a dictionary containing attributes as second argument, |
|
157 |
- a collection with a stream on a single file, |
|
158 |
- or a collection with streams on a directory's files, but not recursive |
|
159 |
||
160 |
The streams are closed after aBlock has been evaluated. |
|
161 |
Attributes may be the mime type (key #MIME)" |
|
162 |
||
163 |
|attributes list requestDirectory path dirUri dirPath| |
|
164 |
||
165 |
requestDirectory := false. |
|
166 |
path := self path. |
|
167 |
"kludge for pathnames starting with a users home dirctory" |
|
2483
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
168 |
(path startsWith:'/~/') ifTrue:[ |
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
169 |
path := path copyFrom:4. |
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
170 |
]. |
2473 | 171 |
(path startsWith:'/~') ifTrue:[ |
172 |
path := path copyFrom:2. |
|
173 |
]. |
|
174 |
||
175 |
attributes := self class attributes. |
|
176 |
list := OrderedCollection new. |
|
177 |
||
178 |
self connectThenDo:[:ftp| |
|
179 |
|baseName| |
|
180 |
||
181 |
"try to change directory to path. |
|
182 |
If we get a file error, we know that the directory does not exist" |
|
183 |
baseName := self baseName. |
|
184 |
(baseName includesAny:'*?[]') ifTrue:[ |
|
185 |
requestDirectory := true. |
|
186 |
dirUri := self directory. |
|
187 |
dirPath := dirUri path. |
|
188 |
"kludge for pathnames starting with a users home dirctory" |
|
2483
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
189 |
(dirPath startsWith:'/~/') ifTrue:[ |
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
190 |
dirPath := dirPath copyFrom:4. |
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
191 |
]. |
2473 | 192 |
(dirPath startsWith:'/~') ifTrue:[ |
193 |
dirPath := dirPath copyFrom:2. |
|
194 |
]. |
|
195 |
FTPClient fileNotFoundErrorSignal handle:[:ex|] |
|
196 |
"skip error about an empty directory" |
|
197 |
do:[ |
|
198 |
list addAll: |
|
199 |
((ftp directoryContentsOf:dirPath) |
|
2732
ce767436e959
Pcsmd 2.7 0011-pcsmd-isReachable.chg
Stefan Vogel <sv@exept.de>
parents:
2483
diff
changeset
|
200 |
collect:[:filenameString| filenameString withoutSeparators asFilename baseName] |
2473 | 201 |
thenSelect:[:filenameString| filenameString matches:baseName]). |
202 |
]. |
|
203 |
] ifFalse:[ |
|
204 |
[ |
|
205 |
dirUri := self. |
|
206 |
dirPath := path. |
|
207 |
requestDirectory := true. |
|
208 |
FTPClient fileNotFoundErrorSignal handle:[:ex|] |
|
209 |
"skip error about an empty directory" |
|
210 |
do:[ |
|
211 |
list addAll: |
|
212 |
((ftp directoryContentsOf:dirPath) |
|
213 |
collect:[:filenameString| filenameString asFilename baseName] |
|
214 |
"thenSelect:[:filenameString| filenameString matches:baseName]"). |
|
215 |
]. |
|
216 |
] on:FTPClient fileErrorSignal do:[:ex| |
|
217 |
"no directory, fetch path istSelf" |
|
218 |
]. |
|
219 |
]. |
|
220 |
requestDirectory ifFalse:[ |
|
221 |
dirUri := self directory. |
|
222 |
dirPath := dirUri path. |
|
223 |
"kludge for pathnames starting with a users home directory" |
|
2483
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
224 |
(dirPath startsWith:'/~/') ifTrue:[ |
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
225 |
dirPath := dirPath copyFrom:4. |
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
226 |
]. |
2473 | 227 |
(dirPath startsWith:'/~') ifTrue:[ |
228 |
dirPath := dirPath copyFrom:2. |
|
229 |
]. |
|
230 |
list add:self baseName. |
|
231 |
]. |
|
232 |
||
233 |
attributes at:#requestDirectory put:requestDirectory. |
|
234 |
||
235 |
"skip all files which are not selected by filterBlock" |
|
236 |
oneArgFilterBlock notNil ifTrue:[ |
|
237 |
list := list select:oneArgFilterBlock. |
|
238 |
]. |
|
239 |
||
240 |
list do:[:eachBaseName| |stream| |
|
241 |
"get a stream for the contents of the file" |
|
242 |
(OpenError, FTPClient fileErrorSignal) handle:[:ex| |
|
243 |
"ignore errors -- skip subdirectories" |
|
244 |
] do:[ |
|
245 |
stream := ftp getStreamFor:(dirPath, '/', eachBaseName). |
|
246 |
"/ attributes at:#fileSize put:(ftp sizeOf:eachBaseName). |
|
247 |
attributes at:#fileSize put:stream fileSize. |
|
248 |
attributes at:#baseName put:eachBaseName. |
|
249 |
]. |
|
250 |
||
251 |
stream notNil ifTrue:[ |
|
252 |
|srcUri srcPath| |
|
253 |
||
254 |
requestDirectory ifTrue:[ |
|
255 |
"accessing the contents of a directory" |
|
256 |
srcUri := dirUri construct:eachBaseName. |
|
257 |
] ifFalse:[ |pathSegments| |
|
258 |
"accessing a single file" |
|
259 |
srcUri := self. |
|
260 |
]. |
|
261 |
attributes at:#uriInfo put:srcUri. |
|
262 |
||
263 |
[ |
|
3451 | 264 |
aBlock value:stream optionalArgument:attributes |
2473 | 265 |
] ensure:[stream close]. |
266 |
||
267 |
renameBlock notNil ifTrue:[ |
|
268 |
|renameFilenameString| |
|
269 |
||
270 |
renameFilenameString := renameBlock value:eachBaseName. |
|
271 |
"/ [ |
|
272 |
ftp rename:(dirPath, '/', eachBaseName) to:(dirPath, '/', renameFilenameString). |
|
273 |
"/ ] on:FTPClient fileErrorSignal do:[:ex| |
|
274 |
"/ "rename failed, maybe file already exists" |
|
275 |
"/ renameFilenameString := renameFilenameString, '.', |
|
276 |
"/ (Timestamp now printStringFormat:'%(year)%(mon)%(day)%h%m%s'). |
|
277 |
"/ ftp rename:eachBaseName to:renameFilenameString. |
|
278 |
"/ ]. |
|
279 |
]. |
|
280 |
]. |
|
281 |
]. |
|
282 |
]. |
|
283 |
||
284 |
||
285 |
" |
|
286 |
|pwd| |
|
287 |
||
288 |
pwd := Dialog requestPassword:''. |
|
289 |
(URI fromString:('ftp://tm:%1@exept/~/tmp' bindWith:pwd) ) |
|
290 |
readStreamsDo:[:stream :attributes | |
|
291 |
Transcript showCR:(attributes at:#baseName). |
|
292 |
Transcript showCR:(attributes at:#fileSize). |
|
293 |
Transcript showCR:(attributes at:#requestDirectory). |
|
294 |
Transcript showCR:(attributes at:#uriInfo). |
|
295 |
]. |
|
296 |
" |
|
297 |
! |
|
298 |
||
299 |
readStreamsDo:aBlock renameBlock:renameBlock |
|
300 |
"evaluate the block with a Collection of streams as first argument |
|
301 |
and a dictionary containing attributes as second argument, |
|
302 |
- a collection with a stream on a single file, |
|
303 |
- or a collection with streams on a directorie's files, but not recursive |
|
304 |
||
305 |
The streams are closed after aBlock has been evaluated. |
|
306 |
Attributes may be the mime type (key #MIME)" |
|
307 |
||
308 |
self readStreamsDo:aBlock skipFilenamesWithSuffix:nil renameBlock:renameBlock |
|
309 |
! |
|
310 |
||
311 |
writeStreamDo:aBlock |
|
312 |
"use FTPClient for now" |
|
313 |
||
314 |
self connectThenDo:[:ftp| |stream| |
|
315 |
[ |
|
316 |
stream := ftp putStreamFor:self path. |
|
3451 | 317 |
aBlock value:stream optionalArgument:self class attributes. |
2473 | 318 |
] ensure:[ |
319 |
stream notNil ifTrue:[ |
|
320 |
stream close. |
|
321 |
]. |
|
322 |
]. |
|
323 |
] |
|
324 |
||
325 |
" |
|
326 |
'ftp://stefan:password@hippo/etc/group' asURI writeStreamDo:[:stream :attributes | |
|
327 |
self halt |
|
328 |
]. |
|
329 |
" |
|
330 |
! |
|
331 |
||
332 |
writeStreamDo:aBlock create:doCreate |
|
333 |
||
334 |
^ self writeStreamDo:aBlock create:doCreate atomic:false. |
|
335 |
! |
|
336 |
||
337 |
writeStreamDo:aBlock create:doCreate atomic:doAtomic |
|
338 |
"use FTPClient for now. |
|
339 |
||
340 |
If doCreate is true, a nonExistent directory will be created. |
|
341 |
If doAtomic is true, files will appear atomically, by using |
|
342 |
an intermediate file theat will be renamed" |
|
343 |
||
344 |
|path toPath directory| |
|
345 |
||
346 |
path := self path. |
|
347 |
"kludge" |
|
2483
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
348 |
(path startsWith:'/~/') ifTrue:[ |
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
349 |
path := path copyFrom:4. |
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
350 |
]. |
2473 | 351 |
(path startsWith:'/~') ifTrue:[ |
352 |
path := path copyFrom:2. |
|
353 |
]. |
|
354 |
||
355 |
doAtomic ifTrue:[ |
|
356 |
toPath := self directoryPath, '/.transferFile'. |
|
357 |
"kludge" |
|
2483
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
358 |
(toPath startsWith:'/~/') ifTrue:[ |
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
359 |
toPath := toPath copyFrom:4. |
da3897369cd3
Fixes for SFTP/Solaris and '~' paths
Stefan Vogel <sv@exept.de>
parents:
2473
diff
changeset
|
360 |
]. |
2473 | 361 |
(toPath startsWith:'/~') ifTrue:[ |
362 |
toPath := toPath copyFrom:2. |
|
363 |
]. |
|
364 |
] ifFalse:[ |
|
365 |
toPath := path. |
|
366 |
]. |
|
367 |
||
368 |
self connectThenDo:[:ftp| |stream| |
|
369 |
[ |
|
370 |
[ |
|
371 |
(self class pathExists:path ftpClient:ftp) ifTrue:[ |infoStream| |
|
372 |
infoStream := '' writeStream. |
|
373 |
self publicPrintOn:infoStream. |
|
374 |
self error:('FTP write: Datei %1 already exists!!' bindWith:infoStream contents). |
|
375 |
]. |
|
376 |
"/ stream := ftp putStreamFor:toPath. |
|
377 |
stream := WriteStreamSimulator new |
|
378 |
remotePath:toPath; |
|
379 |
connection:ftp. |
|
3451 | 380 |
aBlock value:stream optionalArgument:self class attributes. |
2473 | 381 |
] on:OpenError, FTPClient filePutErrorSignal do:[:ex| |
382 |
doCreate ifFalse:[ |
|
383 |
ex reject |
|
384 |
]. |
|
385 |
"create the missing directory on the fly" |
|
386 |
directory := self directoryPath. |
|
387 |
FTPClient fileNotFoundErrorSignal handle:[:ex| ] do:[ |
|
388 |
ftp mkdir:directory. |
|
389 |
]. |
|
390 |
"/ ftp cd:directory. |
|
391 |
(self class pathExists:path ftpClient:ftp) ifTrue:[ |infoStream| |
|
392 |
infoStream := '' writeStream. |
|
393 |
self publicPrintOn:infoStream. |
|
394 |
self error:('FTP write: Datei %1 already exists!!' bindWith:infoStream contents). |
|
395 |
]. |
|
396 |
"/ stream := ftp putStreamFor:toPath. |
|
3451 | 397 |
aBlock value:stream optionalArgument:self class attributes. |
2473 | 398 |
]. |
399 |
stream close. |
|
400 |
doAtomic ifTrue:[ |
|
401 |
ftp rename:toPath to:path |
|
402 |
]. |
|
403 |
] ifCurtailed:[ |
|
404 |
stream notNil ifTrue:[ |
|
405 |
stream close. |
|
406 |
]. |
|
407 |
]. |
|
408 |
] |
|
409 |
! ! |
|
410 |
||
411 |
!SftpURI methodsFor:'testing'! |
|
412 |
||
413 |
exists |
|
414 |
"does the file represented by this uri exist? |
|
415 |
establish a connection for try to get a readStream" |
|
416 |
||
417 |
|exists| |
|
418 |
||
419 |
self connectThenDo:[:aFtpClient| |
|
420 |
exists := self class pathExists:self path ftpClient:aFtpClient |
|
421 |
]. |
|
422 |
||
423 |
^ exists ? false |
|
424 |
||
425 |
" |
|
426 |
|pwd uri| |
|
427 |
||
428 |
pwd := Dialog requestPassword:'Password:'. |
|
429 |
uri := (URI fromString:('ftp://tm:%1@exept/home/tm/tmp/test.txt' bindWith:pwd) ). |
|
430 |
uri pathExists |
|
431 |
" |
|
432 |
! |
|
433 |
||
434 |
isAbsolute |
|
435 |
"there is nothing like a relative ftp URI" |
|
436 |
||
437 |
^ true |
|
438 |
! |
|
439 |
||
440 |
isRemote |
|
441 |
"return true, if this is a remote URI" |
|
442 |
||
443 |
^ true |
|
444 |
! ! |
|
445 |
||
446 |
!SftpURI::WriteStreamSimulator methodsFor:'accessing'! |
|
447 |
||
448 |
connection |
|
449 |
^ connection |
|
450 |
! |
|
451 |
||
452 |
connection:something |
|
453 |
connection := something. |
|
454 |
! |
|
455 |
||
456 |
remotePath |
|
457 |
^ remotePath |
|
458 |
! |
|
459 |
||
460 |
remotePath:something |
|
461 |
remotePath := something. |
|
462 |
! ! |
|
463 |
||
464 |
!SftpURI::WriteStreamSimulator methodsFor:'copying'! |
|
465 |
||
466 |
copyToEndFrom:readStream |
|
467 |
|localPathName| |
|
468 |
||
469 |
readStream isFileStream ifFalse:[ |
|
470 |
self error:'WriteStreamSimulator - source is not a FileStream'. |
|
471 |
]. |
|
472 |
||
473 |
localPathName := readStream pathName. |
|
474 |
connection copyLocal:localPathName to:remotePath. |
|
475 |
! ! |
|
476 |
||
477 |
!SftpURI::WriteStreamSimulator methodsFor:'queries'! |
|
478 |
||
479 |
isWritable |
|
480 |
^ remotePath notNil |
|
481 |
! ! |
|
482 |
||
483 |
!SftpURI::WriteStreamSimulator methodsFor:'stream simulation'! |
|
484 |
||
485 |
close |
|
486 |
^ self |
|
487 |
! ! |
|
488 |
||
489 |
!SftpURI class methodsFor:'documentation'! |
|
490 |
||
491 |
version_CVS |
|
3639 | 492 |
^ '$Header$' |
2473 | 493 |
! ! |
2873 | 494 |