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