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