1005
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
HierarchicalURI subclass:#FtpURI
|
|
4 |
instanceVariableNames:''
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Resources'
|
|
8 |
!
|
|
9 |
|
|
10 |
|
|
11 |
!FtpURI class methodsFor:'accessing'!
|
|
12 |
|
|
13 |
schemes
|
|
14 |
|
|
15 |
^ #(ftp)
|
|
16 |
! !
|
|
17 |
|
|
18 |
!FtpURI methodsFor:'defaults'!
|
|
19 |
|
|
20 |
defaultPassword
|
|
21 |
|
|
22 |
^ 'secret@secret'
|
|
23 |
!
|
|
24 |
|
|
25 |
defaultPort
|
|
26 |
"answer the default command-port here"
|
|
27 |
|
|
28 |
^ 21
|
|
29 |
!
|
|
30 |
|
|
31 |
defaultUser
|
|
32 |
|
|
33 |
^ 'anonymous'
|
|
34 |
! !
|
|
35 |
|
1258
|
36 |
!FtpURI methodsFor:'ftp requests'!
|
|
37 |
|
|
38 |
connectThenDo:aOneArgBlock
|
|
39 |
|
|
40 |
|ftp|
|
|
41 |
|
|
42 |
ftp := FTPClient new.
|
|
43 |
|
|
44 |
|
|
45 |
[
|
|
46 |
ftp connectTo:self host
|
|
47 |
port:self port
|
|
48 |
user:(self user ? self defaultUser)
|
|
49 |
password:(self password ? self defaultPassword).
|
|
50 |
aOneArgBlock value:ftp
|
|
51 |
] ensure:[
|
|
52 |
ftp notNil ifTrue:[
|
|
53 |
ftp close.
|
|
54 |
].
|
|
55 |
]
|
|
56 |
!
|
1254
|
57 |
|
|
58 |
pathExists
|
1258
|
59 |
"establish a connection for try to get a readSteream"
|
1254
|
60 |
|
|
61 |
|exists|
|
|
62 |
|
1258
|
63 |
self connectThenDo:[:aFtpClient|
|
|
64 |
exists := self pathExistsFtp:aFtpClient.
|
1254
|
65 |
].
|
|
66 |
|
1258
|
67 |
^ exists ? false
|
1254
|
68 |
|
1258
|
69 |
"
|
|
70 |
|pwd uri|
|
|
71 |
|
|
72 |
pwd := Dialog requestPassword:'Password:'.
|
|
73 |
uri := (URI fromString:('ftp://tm:%1@exept/home/tm/tmp/test.txt' bindWith:pwd) ).
|
|
74 |
uri pathExists
|
|
75 |
"
|
|
76 |
!
|
|
77 |
|
|
78 |
pathExists:aPathname ftpClient:aFtpClient
|
|
79 |
|
|
80 |
|list|
|
|
81 |
|
|
82 |
list := aFtpClient list:aPathname.
|
|
83 |
^ list contains:[:aLine| (aLine asCollectionOfWords last) = aPathname ]
|
|
84 |
!
|
|
85 |
|
|
86 |
pathExistsFtp:aFtpClient
|
|
87 |
|
|
88 |
^ self pathExists:self path ftpClient:aFtpClient
|
|
89 |
! !
|
|
90 |
|
1005
|
91 |
!FtpURI methodsFor:'stream access'!
|
|
92 |
|
|
93 |
readStreamDo:aBlock
|
|
94 |
"use FTPClient for now"
|
|
95 |
|
1254
|
96 |
self connectThenDo:[:ftp| |stream path attributes|
|
|
97 |
[
|
|
98 |
path := self path.
|
|
99 |
attributes := self class attributes.
|
|
100 |
attributes at:#fileSize put:(ftp sizeOf:path).
|
|
101 |
attributes at:#baseName put:self pathSegements last.
|
|
102 |
attributes at:#uriInfo put:self printString.
|
|
103 |
|
|
104 |
stream := ftp getStreamFor:path.
|
|
105 |
aBlock value:stream value:attributes.
|
|
106 |
] ensure:[
|
|
107 |
stream notNil ifTrue:[
|
|
108 |
stream close.
|
|
109 |
].
|
|
110 |
].
|
|
111 |
].
|
1005
|
112 |
|
1254
|
113 |
"
|
1268
|
114 |
'ftp://stefan:password@hippo/etc/group' asURI readStreamDo:[:stream :attributes |
|
1254
|
115 |
self halt
|
|
116 |
].
|
|
117 |
"
|
|
118 |
!
|
|
119 |
|
|
120 |
readStreamsDo:aBlock
|
|
121 |
"evaluate the block with a Collection of streams as first argument
|
|
122 |
and a dictionary containing attributes as second argument,
|
|
123 |
- a collection with a stream on a single file,
|
|
124 |
- or a collection with streams on a directorie's files, but not recursive
|
|
125 |
|
|
126 |
The streams are closed after aBlock has been evaluated.
|
|
127 |
Attributes may be the mime type (key #MIME)"
|
|
128 |
|
1264
|
129 |
self readStreamsDo:aBlock skipFilenamesWithSuffix:nil thenRemove:false
|
1258
|
130 |
!
|
|
131 |
|
1264
|
132 |
readStreamsDo:aBlock skipFilenamesWithSuffix:aSuffix thenRemove:doRemoveSource
|
1258
|
133 |
"evaluate the block with a Collection of streams as first argument
|
|
134 |
and a dictionary containing attributes as second argument,
|
|
135 |
- a collection with a stream on a single file,
|
|
136 |
- or a collection with streams on a directorie's files, but not recursive
|
|
137 |
|
|
138 |
The streams are closed after aBlock has been evaluated.
|
|
139 |
Attributes may be the mime type (key #MIME)"
|
|
140 |
|
1266
|
141 |
|attributes list requestDirectory path dirPath|
|
1254
|
142 |
|
|
143 |
requestDirectory := false.
|
|
144 |
path := self path.
|
1268
|
145 |
"kludge"
|
|
146 |
(path startsWith:'/~') ifTrue:[
|
|
147 |
path := path copyFrom:2.
|
|
148 |
].
|
1254
|
149 |
attributes := self class attributes.
|
|
150 |
list := OrderedCollection new.
|
|
151 |
|
|
152 |
self connectThenDo:[:ftp|
|
|
153 |
FTPClient fileErrorSignal handle:[:ex|
|
|
154 |
list add:path.
|
|
155 |
attributes at:#requestDirectory put:false.
|
|
156 |
] do:[
|
1266
|
157 |
dirPath := path.
|
|
158 |
ftp cd:dirPath.
|
1254
|
159 |
requestDirectory := true.
|
|
160 |
attributes at:#requestDirectory put:true.
|
|
161 |
list addAll:((ftp list) collect:[:aLine| aLine asCollectionOfWords last]).
|
1005
|
162 |
].
|
1254
|
163 |
|
1264
|
164 |
requestDirectory ifFalse:[ |bName|
|
|
165 |
bName := self pathSegements last.
|
|
166 |
(bName startsWith:'*') ifTrue:[
|
|
167 |
list removeAll.
|
|
168 |
requestDirectory := true.
|
|
169 |
attributes at:#requestDirectory put:true.
|
1266
|
170 |
dirPath := (path asFilename directory) pathName.
|
|
171 |
ftp cd:dirPath.
|
1264
|
172 |
list addAll:((ftp list) collect:[:aLine| aLine asCollectionOfWords last]).
|
|
173 |
].
|
|
174 |
(bName startsWith:'*.') ifTrue:[ |rest|
|
|
175 |
rest := bName restAfter:$*.
|
|
176 |
(rest includesString:'*') ifTrue:[
|
|
177 |
self error:'can''t resolve path:', self printString
|
|
178 |
].
|
|
179 |
list := list select:[:str| str endsWith:rest ]
|
|
180 |
].
|
|
181 |
].
|
|
182 |
|
|
183 |
aSuffix size ~~ 0 ifTrue:[
|
1265
|
184 |
list := list select:[:str| (str endsWith:aSuffix) not ]
|
1264
|
185 |
].
|
|
186 |
|
1254
|
187 |
list do:[:aPathName| |baseName stream|
|
|
188 |
FTPClient fileErrorSignal handle:[:ex|
|
1258
|
189 |
"/ skip subdirectories and the summary of the list
|
1254
|
190 |
] do:[
|
|
191 |
stream := ftp getStreamFor:aPathName.
|
|
192 |
attributes at:#fileSize put:(ftp sizeOf:aPathName).
|
1258
|
193 |
requestDirectory
|
|
194 |
ifTrue:[ baseName := aPathName ]
|
|
195 |
ifFalse:[ baseName := self pathSegements last ].
|
1254
|
196 |
attributes at:#baseName put:baseName
|
|
197 |
].
|
|
198 |
|
1258
|
199 |
stream notNil ifTrue:[ |src srcPath|
|
|
200 |
(self pathSegements includes:baseName) ifTrue:[
|
1266
|
201 |
srcPath := (dirPath asFilename) pathName.
|
1258
|
202 |
attributes at:#uriInfo put:self.
|
1266
|
203 |
] ifFalse:[ |pathSegements|
|
|
204 |
src := self copy.
|
|
205 |
pathSegements := (dirPath asFilename construct:baseName) components.
|
|
206 |
pathSegements removeFirst.
|
|
207 |
src pathSegements:pathSegements.
|
1258
|
208 |
srcPath := src path.
|
|
209 |
attributes at:#uriInfo put:src.
|
1254
|
210 |
].
|
1258
|
211 |
|
|
212 |
[ aBlock value:stream value:attributes ]
|
|
213 |
ensure:[ stream close ].
|
|
214 |
doRemoveSource == true ifTrue:[
|
1267
|
215 |
(srcPath startsWith:'/') ifFalse:[ srcPath := '/', srcPath ].
|
1258
|
216 |
ftp delete:srcPath.
|
|
217 |
].
|
1254
|
218 |
].
|
|
219 |
].
|
|
220 |
].
|
|
221 |
|
|
222 |
|
|
223 |
"
|
|
224 |
|pwd|
|
|
225 |
|
|
226 |
pwd := Dialog requestPassword:''.
|
|
227 |
(URI fromString:('ftp://tm:%1@exept/home/tm/tmp' bindWith:pwd) )
|
|
228 |
readStreamsDo:[:stream :attributes |
|
|
229 |
Transcript showCR:(attributes at:#baseName).
|
|
230 |
Transcript showCR:(attributes at:#fileSize).
|
|
231 |
Transcript showCR:(attributes at:#requestDirectory).
|
|
232 |
Transcript showCR:(attributes at:#uriInfo).
|
|
233 |
].
|
|
234 |
"
|
|
235 |
!
|
|
236 |
|
1264
|
237 |
readStreamsDo:aBlock thenRemove:doRemoveSource
|
|
238 |
"evaluate the block with a Collection of streams as first argument
|
|
239 |
and a dictionary containing attributes as second argument,
|
|
240 |
- a collection with a stream on a single file,
|
|
241 |
- or a collection with streams on a directorie's files, but not recursive
|
|
242 |
|
|
243 |
The streams are closed after aBlock has been evaluated.
|
|
244 |
Attributes may be the mime type (key #MIME)"
|
|
245 |
|
|
246 |
self readStreamsDo:aBlock skipFilenamesWithSuffix:nil thenRemove:doRemoveSource
|
|
247 |
!
|
|
248 |
|
1254
|
249 |
writeStreamDo:aBlock
|
|
250 |
"use FTPClient for now"
|
|
251 |
|
|
252 |
self connectThenDo:[:ftp| |stream|
|
|
253 |
[
|
|
254 |
ftp connectTo:self host
|
|
255 |
port:self port
|
|
256 |
user:(self user ? self defaultUser)
|
|
257 |
password:(self password ? self defaultPassword).
|
|
258 |
stream := ftp putStreamFor:self path.
|
|
259 |
aBlock value:stream value:self class attributes.
|
|
260 |
] ensure:[
|
|
261 |
stream notNil ifTrue:[
|
|
262 |
stream close.
|
|
263 |
].
|
1005
|
264 |
].
|
|
265 |
]
|
1254
|
266 |
|
1005
|
267 |
"
|
1268
|
268 |
'ftp://stefan:password@hippo/etc/group' asURI writeStreamDo:[:stream :attributes |
|
1005
|
269 |
self halt
|
|
270 |
].
|
|
271 |
"
|
1266
|
272 |
!
|
|
273 |
|
|
274 |
writeStreamDo:aBlock create:doCreate
|
1268
|
275 |
|
|
276 |
^ self writeStreamDo:aBlock create:doCreate atomic:false.
|
|
277 |
!
|
1266
|
278 |
|
1268
|
279 |
writeStreamDo:aBlock create:doCreate atomic:doAtomic
|
|
280 |
"use FTPClient for now.
|
|
281 |
|
|
282 |
If doCreate is true, a nonExistent directory will be created.
|
|
283 |
If doAtomic is true, files will appear atomically, by using
|
|
284 |
an intermediate file theat will be renamed"
|
|
285 |
|
|
286 |
|path toPath directory|
|
1266
|
287 |
|
|
288 |
path := self path.
|
1268
|
289 |
doAtomic ifTrue:[
|
|
290 |
toPath := self directoryPath, '/.transferFile'.
|
|
291 |
] ifFalse:[
|
|
292 |
toPath := path.
|
|
293 |
].
|
|
294 |
|
1266
|
295 |
self connectThenDo:[:ftp| |stream|
|
|
296 |
[
|
|
297 |
ftp connectTo:self host
|
|
298 |
port:self port
|
|
299 |
user:(self user ? self defaultUser)
|
|
300 |
password:(self password ? self defaultPassword).
|
|
301 |
|
1268
|
302 |
[
|
|
303 |
stream := ftp putStreamFor:toPath.
|
|
304 |
] on:FTPClient filePutErrorSignal do:[:ex|
|
1266
|
305 |
doCreate ifFalse:[
|
|
306 |
ex reject
|
1268
|
307 |
].
|
|
308 |
"create the missing directory on the fly"
|
|
309 |
directory := self directoryPath.
|
1267
|
310 |
FTPClient fileNotFoundErrorSignal handle:[:ex| ] do:[
|
1268
|
311 |
ftp mkdir:directory.
|
1267
|
312 |
].
|
1268
|
313 |
ftp cd:directory.
|
|
314 |
stream := ftp putStreamFor:toPath.
|
1266
|
315 |
].
|
|
316 |
aBlock value:stream value:self class attributes.
|
1268
|
317 |
doAtomic ifTrue:[
|
|
318 |
ftp rename:toPath to:path
|
|
319 |
].
|
1266
|
320 |
] ensure:[
|
|
321 |
stream notNil ifTrue:[
|
|
322 |
stream close.
|
|
323 |
].
|
|
324 |
].
|
|
325 |
]
|
1268
|
326 |
! !
|
1266
|
327 |
|
1268
|
328 |
!FtpURI methodsFor:'testing'!
|
|
329 |
|
|
330 |
isAbsolute
|
|
331 |
"there is nothing like a relative ftp URI"
|
|
332 |
|
|
333 |
^ true
|
1005
|
334 |
! !
|
|
335 |
|
|
336 |
!FtpURI class methodsFor:'documentation'!
|
|
337 |
|
|
338 |
version
|
1268
|
339 |
^ '$Header: /cvs/stx/stx/libbasic2/FtpURI.st,v 1.8 2003-07-11 12:48:15 stefan Exp $'
|
1005
|
340 |
! !
|