|
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // EPOC32 FTP Engine header file |
|
15 // Author: Philippe Gabriel |
|
16 // Implements set of APIs simplyfying access to the FTP protocol |
|
17 // |
|
18 // |
|
19 |
|
20 /** |
|
21 @file FTPSESS.CPP |
|
22 @internalComponent |
|
23 */ |
|
24 |
|
25 #include <e32test.h> |
|
26 #include "SESSION.H" |
|
27 #include "DEBUG.H" |
|
28 |
|
29 // |
|
30 // Minterface Implementation |
|
31 // |
|
32 |
|
33 void CFTPSessionDerived::SetErrorNotifier(const TInt aError) |
|
34 /** |
|
35 Define the CFTPSetError callback notifier |
|
36 */ |
|
37 { |
|
38 switch(aError) |
|
39 { |
|
40 case MFtpSessionNotifier::ESocketError: |
|
41 iFtpSessionNotifier->ConnectionError((MFtpSessionNotifier::TOpComp)aError); |
|
42 ErrorTransition(MFtpProtocolNotifier::EOpConnectionFailed); |
|
43 break; |
|
44 case MFtpSessionNotifier::EAlreadyConnected: |
|
45 case MFtpSessionNotifier::ENotConnected: |
|
46 iFtpSessionNotifier->ConnectionError((MFtpSessionNotifier::TOpComp)aError); |
|
47 //No state transition in this case |
|
48 break; |
|
49 case MFtpSessionNotifier::EFileSystemError: |
|
50 case MFtpSessionNotifier::EFileOpenFailure: |
|
51 case MFtpSessionNotifier::EFileReadError: |
|
52 case MFtpSessionNotifier::EFileWriteError: |
|
53 case MFtpSessionNotifier::EFileAlreadyExist: |
|
54 case MFtpSessionNotifier::EFileNotExist: |
|
55 case MFtpSessionNotifier::EDirAlreadyExist: |
|
56 case MFtpSessionNotifier::EDirNotExist: |
|
57 iFtpSessionNotifier->LocalFileSystemError((MFtpSessionNotifier::TOpComp)aError); |
|
58 ErrorTransition(MFtpProtocolNotifier::EOpFailed); |
|
59 break; |
|
60 case MFtpSessionNotifier::EOpCanceled: |
|
61 iFtpSessionNotifier->Cancel(); |
|
62 ErrorTransition(MFtpProtocolNotifier::EOpCanceled); |
|
63 break; |
|
64 default: |
|
65 // I forgot something |
|
66 __ASSERT_DEBUG(FALSE, User::Panic(_L("CFTPSessionDerived::SetError"), 0)); |
|
67 break; |
|
68 } |
|
69 } |
|
70 |
|
71 void CFTPSessionDerived::ErrorNotification(const TOpComp aStatus) |
|
72 { |
|
73 switch(aStatus) |
|
74 { |
|
75 case MFtpProtocolNotifier::EOpConnectionReset: |
|
76 // Total mayhem, irrecoverable |
|
77 // We got a TCP RST |
|
78 // Notify upper layer |
|
79 iFtpSessionNotifier->ConnReset(); |
|
80 break; |
|
81 case MFtpProtocolNotifier::EOpConnectionFailed: |
|
82 __ASSERT_DEBUG(iState == EConnecting, User::Panic(_L("CFTPSessionDerived::ErrorNotification"), 0)); |
|
83 iFtpSessionNotifier->ConnectionError(MFtpSessionNotifier::EConnectionFailed); |
|
84 break; |
|
85 case MFtpProtocolNotifier::EXferReset: |
|
86 __ASSERT_DEBUG((iState == EPerformingList) |
|
87 ||(iState == ERetrieveFile) |
|
88 ||(iState == EStoreFile) |
|
89 ||(iState == EStoreFileSendEOF), |
|
90 User::Panic(_L("CFTPSessionDerived::ErrorNotification"), 0)); |
|
91 iFtpSessionNotifier->RemoteFileSystemError(MFtpSessionNotifier::ERemoteFileSystemError); |
|
92 break; |
|
93 case MFtpProtocolNotifier::EXferNotInitialised: |
|
94 __ASSERT_DEBUG((iState == EPerformingList) |
|
95 ||(iState == ERetrieveFile) |
|
96 ||(iState == EStoreFile) |
|
97 ||(iState == EStoreFileSendEOF), |
|
98 User::Panic(_L("CFTPSessionDerived::ServerNegativeAnswerNotification"), 0)); |
|
99 iFtpSessionNotifier->RemoteFileSystemError(MFtpSessionNotifier::ERemoteFileOpenFailure); |
|
100 break; |
|
101 case MFtpProtocolNotifier::EHostNotFound: |
|
102 iFtpSessionNotifier->ConnectionError(MFtpSessionNotifier::EHostNotExist); |
|
103 break; |
|
104 case MFtpProtocolNotifier::EOpCanceled: |
|
105 iFtpSessionNotifier->Cancel(); |
|
106 break; |
|
107 default: |
|
108 // I forgot something |
|
109 __ASSERT_DEBUG(FALSE, User::Panic(_L("ErrorNotification::SetError"), 0)); |
|
110 iFtpSessionNotifier->EUnknownError(); |
|
111 break; |
|
112 } |
|
113 ErrorTransition(aStatus); |
|
114 } |
|
115 |
|
116 void CFTPSessionDerived::ServerMessage(const TDesC8& aMessage) |
|
117 { |
|
118 iFtpSessionNotifier->ServerMessage(aMessage); |
|
119 } |
|
120 |
|
121 void CFTPSessionDerived::ServerNegativeAnswerNotification(const TOpComp /*aStatus*/) |
|
122 { |
|
123 // Fetch the answer |
|
124 iCFtpProtocol->FTPServerAnswer(iServerAnswer); |
|
125 // Have to discriminate on the answer to send back a meaningfull error |
|
126 // fetch a Lex object |
|
127 TLex input(iServerAnswer); |
|
128 TUint uintServerAnswer; |
|
129 // Convert |
|
130 input.Val(uintServerAnswer,EDecimal); |
|
131 FTPPROTDEBUG1(_DBGFtpsess,_L("CFTPSessionDerived::ServerNegativeAnswerNotification Server converted answer:<%u>\n"),uintServerAnswer); |
|
132 // Check the answer is within bounds |
|
133 if ((uintServerAnswer<(TUint)100) || (uintServerAnswer>(TUint)599)) |
|
134 { |
|
135 FTPPROTDEBUG2(0xffff,_L("!!!!!!!!!!!!!CFTPSessionDerived::ServerNegativeAnswerNotification Server answer:<%d> is out of bound\n"),iServerAnswer); |
|
136 __ASSERT_DEBUG(FALSE, User::Panic(_L("CFTPSessionDerived::ServerNegativeAnswerNotification"), 0)); |
|
137 // Set it to a generic failure code |
|
138 uintServerAnswer = 599; |
|
139 } |
|
140 // Check this is really an error |
|
141 __ASSERT_DEBUG(uintServerAnswer>199, User::Panic(_L("CFTPSessionDerived::ServerNegativeAnswerNotification"), 0)); |
|
142 // Unfortunately servers do not seem to follow the RFC recommendation very well |
|
143 // And the list of messages in RFC959 p40-42 is not reliable. |
|
144 // All we can do is best guess, anything more appropriate need the user |
|
145 // to read the text meesage sent back by the server |
|
146 switch(iState) |
|
147 { |
|
148 // Intermediate State |
|
149 case EConnecting: |
|
150 // Assume server unreachable |
|
151 iFtpSessionNotifier->ConnectionError(MFtpSessionNotifier::EConnectionFailed); |
|
152 break; |
|
153 case ELoginUser: |
|
154 case ELoginPass: |
|
155 // Assume login,pass invalid |
|
156 iFtpSessionNotifier->ConnectionError(MFtpSessionNotifier::ELoginFailed); |
|
157 break; |
|
158 case ERenameFileFrom: |
|
159 case ERenameFileTo: |
|
160 // file not found or no access |
|
161 iFtpSessionNotifier->RemoteFileSystemError(MFtpSessionNotifier::EPermissionDenied); |
|
162 break; |
|
163 case EPerformingPortForList: |
|
164 case EPerformingPasvForList: |
|
165 case EPerformingPortForRetrieve: |
|
166 case EPerformingPasvForRetrieve: |
|
167 case EPerformingPasvForStore: |
|
168 case EPerformingPortForStore: |
|
169 // What kind of error could we have following a port? |
|
170 iFtpSessionNotifier->EUnknownError(); |
|
171 break; |
|
172 case EPerformingRestForRetrieve: |
|
173 // file not found or no access |
|
174 iFtpSessionNotifier->RemoteFileSystemError(MFtpSessionNotifier::ERestartNotSupported); |
|
175 break; |
|
176 // Semi-Intermediate states |
|
177 case EPerformingList: |
|
178 case ERetrieveFile: |
|
179 case EStoreFile: |
|
180 iFtpSessionNotifier->RemoteFileSystemError(MFtpSessionNotifier::ERemoteFileSystemError); |
|
181 break; |
|
182 // Final States, call back notifier |
|
183 case EConnected: |
|
184 // Assume timeout |
|
185 iFtpSessionNotifier->ConnectionError(MFtpSessionNotifier::ETimedOut); |
|
186 break; |
|
187 case EStoreFileSendEOF: |
|
188 case EGetCurrentDirectory: |
|
189 case EChangeDirectory: |
|
190 case ECreateDirectory: |
|
191 case EDeleteDirectory: |
|
192 case EDeleteFile: |
|
193 iFtpSessionNotifier->RemoteFileSystemError(MFtpSessionNotifier::EPermissionDenied); |
|
194 break; |
|
195 case EClosing: |
|
196 // What kind of error could we have here? |
|
197 iFtpSessionNotifier->EUnknownError(); |
|
198 break; |
|
199 default: |
|
200 // Invalid state |
|
201 __ASSERT_DEBUG(FALSE, User::Panic(_L("CFTPSessionDerived::ServerNegativeAnswerNotification"), 0)); |
|
202 break; |
|
203 } |
|
204 ErrorTransition(MFtpProtocolNotifier::EOpFailed); |
|
205 } |
|
206 |
|
207 void CFTPSessionDerived::ServerXFerNotification(const TOpComp /*aStatus*/) |
|
208 { |
|
209 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::ServerXFerNotification called - State:")); |
|
210 TDes8& currentBuf = iFileXferBuffer[iCurrentXferBuffer]; |
|
211 iCurrentXferBuffer = !iCurrentXferBuffer; //swap buffers; |
|
212 |
|
213 switch(iState) |
|
214 { |
|
215 // Semi-Intermediate states |
|
216 case EPerformingList: |
|
217 FTPPROTDEBUG(_DBGFtpsess,_L("EPerformingList \n")); |
|
218 iFtpSessionNotifier->MoreData(); |
|
219 break; |
|
220 case ERetrieveFile: |
|
221 FTPPROTDEBUG(_DBGFtpsess,_L("ERetrieveFile \n")); |
|
222 // Reissue the read request |
|
223 iCFtpProtocol->RecvBuffer(&iFileXferBuffer[iCurrentXferBuffer]); |
|
224 // Save packet in the open file |
|
225 if(iFileOpErrorOccured || (KErrNone != iFile.Write(currentBuf))) |
|
226 { |
|
227 // Notify error |
|
228 if(!iFileOpErrorOccured) |
|
229 //1st time, notify |
|
230 iFtpSessionNotifier->LocalFileSystemError(MFtpSessionNotifier::EFileWriteError); |
|
231 iFileOpErrorOccured = TRUE; |
|
232 // __ASSERT_DEBUG(fileOpReturn==KErrNone, User::Panic(_L("CFTPSessionDerived::ServerXFerNotification"), 0)); |
|
233 } |
|
234 // Just to be sure |
|
235 if(iFileOpErrorOccured || (KErrNone != iFile.Flush())) |
|
236 { |
|
237 // Notify error |
|
238 if(!iFileOpErrorOccured) |
|
239 //1st time, notify |
|
240 iFtpSessionNotifier->LocalFileSystemError(MFtpSessionNotifier::EFileWriteError); |
|
241 iFileOpErrorOccured = TRUE; |
|
242 // __ASSERT_DEBUG(fileOpReturn==KErrNone, User::Panic(_L("CFTPSessionDerived::ServerXFerNotification"), 0)); |
|
243 } |
|
244 // Call the Progress notifier |
|
245 iFtpSessionNotifier->TransferProgress(currentBuf.Length()); |
|
246 break; |
|
247 case EStoreFile: |
|
248 FTPPROTDEBUG(_DBGFtpsess,_L("EStoreFile \n")); |
|
249 // Call the Progress notifier |
|
250 if((iFileOpErrorOccured || (currentBuf.Length() == 0)) && !iFirstRun) |
|
251 { |
|
252 // Send an EOF |
|
253 iState = EStoreFileSendEOF; |
|
254 iCFtpProtocol->SendEOF(); |
|
255 } |
|
256 else { |
|
257 // Reissue the Send request |
|
258 iCFtpProtocol->SendBuffer(¤tBuf); |
|
259 iFirstRun=EFalse; |
|
260 } |
|
261 |
|
262 iFtpSessionNotifier->TransferProgress(currentBuf.Length()); |
|
263 |
|
264 // Construct a packet from the open file |
|
265 if(iFileOpErrorOccured || (KErrNone != iFile.Read(iFileXferBuffer[iCurrentXferBuffer]))) |
|
266 { |
|
267 // Notify error |
|
268 if(!iFileOpErrorOccured) |
|
269 //1st time, notify |
|
270 iFtpSessionNotifier->LocalFileSystemError(MFtpSessionNotifier::EFileWriteError); |
|
271 iFileOpErrorOccured = ETrue; |
|
272 } |
|
273 break; |
|
274 case EAbortingStore: |
|
275 FTPPROTDEBUG(_DBGFtpsess,_L("EAbortingStore \n")); |
|
276 // Shutdown the DTP channel |
|
277 iCFtpProtocol->SendEOF(); |
|
278 break; |
|
279 case EAbortingRetrieve: |
|
280 case EAbortingList: |
|
281 FTPPROTDEBUG(_DBGFtpsess,_L("EAbortingRetrieve - EAbortingList\n")); |
|
282 // Reissue the read request to flush the recv buffer |
|
283 iCFtpProtocol->RecvBuffer(¤tBuf); |
|
284 break; |
|
285 default: |
|
286 FTPPROTDEBUG(_DBGFtpsess,_L("INVALID \n")); |
|
287 // Invalid state |
|
288 __ASSERT_DEBUG(FALSE, User::Panic(_L("CFtpProtocol::ServerXFerNotification"), 0)); |
|
289 break; |
|
290 } |
|
291 } |
|
292 |
|
293 void CFTPSessionDerived::ServerPositiveAnswerNotification(const TOpComp /*aStatus*/) |
|
294 /** |
|
295 Define the CFtpProtocol callback notifiers |
|
296 */ |
|
297 { |
|
298 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::ServerPositiveAnswerNotification called\n")); |
|
299 // Fetch the answer |
|
300 // PG 200898 Don't need the answer just now, maybe in the future |
|
301 // iCFtpProtocol->FTPServerAnswer(iServerAnswer); |
|
302 // Just do a normal transition |
|
303 NormalTransition(); |
|
304 } |
|
305 // |
|
306 // State Transition functions |
|
307 // |
|
308 |
|
309 void CFTPSessionDerived::NormalTransition(void) |
|
310 /** |
|
311 NormalTransition |
|
312 Called when an operation has been positively acknowledged |
|
313 and we progress to the next operation |
|
314 No input parameter, only operates on global member data |
|
315 */ |
|
316 { |
|
317 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::NormalTransition called\n")); |
|
318 TBool loopAgain; |
|
319 do |
|
320 { |
|
321 // Do Pre State update action |
|
322 switch(iState) |
|
323 { |
|
324 case ERetrieveFile: |
|
325 case EStoreFileSendEOF: |
|
326 //Finished, replace the old local file with the temp one |
|
327 //and delete the temp file |
|
328 if(iTempStore) |
|
329 { |
|
330 iTempStore = FALSE; |
|
331 if(iFs.Delete(iRenamedLocalFileName)!=KErrNone) |
|
332 { |
|
333 iCFTPSetError->SetError(MFtpSessionNotifier::EFileSystemError); |
|
334 return; |
|
335 } |
|
336 } |
|
337 iFile.Close(); |
|
338 break; |
|
339 case EPerformingTypeForRetrieve: |
|
340 case EPerformingTypeForStore: |
|
341 iCurrentRepresentationType = iNewRepresentationType; |
|
342 break; |
|
343 case EAbortingStore: |
|
344 case EAbortingRetrieve: |
|
345 case EAbortingList: |
|
346 iFtpSessionNotifier->Cancel(); |
|
347 break; |
|
348 case EConnected: |
|
349 // We should never get here |
|
350 // BUT WE DO, reason is: |
|
351 // during xfers, some ftp servers don't ack the abort |
|
352 // where we (the client) enter an abort state and wait for the server to |
|
353 // send us either |
|
354 // 4 (xfer canceled) - 2 (abort ack) |
|
355 // or just 2 (abort ack) |
|
356 // but some lame servers send us: |
|
357 // 2 (xfer finished) - 2 (abort ack) |
|
358 // Don't report a completion to the client just bail out |
|
359 return; |
|
360 default: |
|
361 break; |
|
362 } |
|
363 // Update the state |
|
364 iState = UpdateState(iState,MFtpProtocolNotifier::EOpComplete); |
|
365 // Do some action according to the new state |
|
366 loopAgain = FALSE; |
|
367 switch(iState) |
|
368 { |
|
369 // Intermediate State |
|
370 // Don't callback notifier |
|
371 case EInitiateListOperation: |
|
372 case EInitiateRetrieveOperation: |
|
373 case EInitiateStoreOperation: |
|
374 // These are just a bunch of dummy states |
|
375 // allowing to streamline the code a bit |
|
376 loopAgain = TRUE; |
|
377 break; |
|
378 case ELoginUser: |
|
379 iCFtpProtocol->User(iBuffer1); |
|
380 break; |
|
381 case ELoginPass: |
|
382 iCFtpProtocol->Pass(iBuffer2); |
|
383 break; |
|
384 case ERenameFileTo: |
|
385 iCFtpProtocol->Rnto(iBuffer1); |
|
386 break; |
|
387 case EPerformingTypeForRetrieve: |
|
388 case EPerformingTypeForStore: |
|
389 if(iNewRepresentationType != iCurrentRepresentationType) |
|
390 { |
|
391 // New type differs from the old, tell the server to change it |
|
392 if(iNewRepresentationType == EASCII) |
|
393 iCFtpProtocol->Type(_L8("A")); |
|
394 else |
|
395 iCFtpProtocol->Type(_L8("I")); |
|
396 break; |
|
397 } |
|
398 else |
|
399 // New type same as the old, do nothing |
|
400 loopAgain = TRUE; |
|
401 break; |
|
402 case EPerformingPasvForList: |
|
403 case EPerformingPasvForRetrieve: |
|
404 case EPerformingPasvForStore: |
|
405 iCFtpProtocol->Pasv(); |
|
406 break; |
|
407 case EPerformingPortForList: |
|
408 case EPerformingPortForRetrieve: |
|
409 case EPerformingPortForStore: |
|
410 iCFtpProtocol->Port(); |
|
411 break; |
|
412 case EPerformingList: |
|
413 // Set buffer |
|
414 iCFtpProtocol->RecvBuffer(iFileList); |
|
415 iCFtpProtocol->List(iBuffer1); |
|
416 break; |
|
417 case EPerformingRestForStore: |
|
418 case EPerformingRestForRetrieve: |
|
419 // Do I really need to send a restart? |
|
420 if(iRESTOffset !=0 ) |
|
421 { |
|
422 //Send a REST command |
|
423 iBuffer2.Num(iRESTOffset,EDecimal); |
|
424 // Reset immediately the RESTOffset |
|
425 iRESTOffset = 0; |
|
426 iCFtpProtocol->Rest(iBuffer2); |
|
427 } |
|
428 else |
|
429 loopAgain = TRUE; |
|
430 break; |
|
431 case ERetrieveFile: |
|
432 // Set buffer |
|
433 iCFtpProtocol->RecvBuffer(&iFileXferBuffer[iCurrentXferBuffer]); |
|
434 iCFtpProtocol->Retr(iBuffer1); |
|
435 break; |
|
436 case EStoreFile: |
|
437 // Set buffer |
|
438 iCFtpProtocol->Stor(iBuffer1); |
|
439 // Construct a packet from the open file |
|
440 iFileXferBuffer[iCurrentXferBuffer].SetLength(0); |
|
441 iCurrentXferBuffer = !iCurrentXferBuffer; |
|
442 iFile.Read(iFileXferBuffer[iCurrentXferBuffer]); |
|
443 //Don't send yet, just bump the notifier |
|
444 iFirstRun=ETrue; |
|
445 ServerXFerNotification(EOpComplete); |
|
446 break; |
|
447 // Final States, call back notifier |
|
448 case EConnected: |
|
449 case EIdle: |
|
450 iFtpSessionNotifier->Complete(); |
|
451 break; |
|
452 default: |
|
453 // Invalid state |
|
454 __ASSERT_DEBUG(FALSE, User::Panic(_L("CFtpProtocol"), 0)); |
|
455 break; |
|
456 } |
|
457 }while(loopAgain); |
|
458 } |
|
459 |
|
460 void CFTPSessionDerived::ErrorTransition(const TOpComp aStatus) |
|
461 /** |
|
462 ErrorTransition |
|
463 Called when an operation has failed |
|
464 need to do some clearup |
|
465 and determine what the next state is gonna be |
|
466 */ |
|
467 { |
|
468 // Close local file |
|
469 iFile.Close(); |
|
470 |
|
471 if ( iTempStore ) |
|
472 {//rename orig file back iRenamedLocalFileName contains original file name as well |
|
473 iTempStore = FALSE; |
|
474 TBuf<256> fileName; |
|
475 fileName.Copy( iRenamedLocalFileName.Ptr(), iRenamedLocalFileName.Length() - 1 ); |
|
476 iFs.Delete( fileName ); //just in case |
|
477 iFs.Rename( iRenamedLocalFileName, fileName ); |
|
478 //should i report a possible error to anyone? |
|
479 } |
|
480 // Trust the lower layer to cleanup the connection mess |
|
481 // just find out what the next state is |
|
482 iState = UpdateState(iState,aStatus); |
|
483 } |
|
484 |
|
485 CFTPSessionDerived::TState CFTPSessionDerived::UpdateState(CFTPSessionDerived::TState aState,MFtpProtocolNotifier::TOpComp aStatus) |
|
486 { |
|
487 CFTPSessionDerived::TState returnValue; |
|
488 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::UpdateState called\n")); |
|
489 switch(aStatus) |
|
490 { |
|
491 case MFtpProtocolNotifier::EOpComplete: |
|
492 switch(aState) |
|
493 { |
|
494 case EConnecting: |
|
495 returnValue = ELoginUser; |
|
496 break; |
|
497 case ELoginUser: |
|
498 returnValue = ELoginPass; |
|
499 break; |
|
500 case ERenameFileFrom: |
|
501 returnValue = ERenameFileTo; |
|
502 break; |
|
503 case EInitiateListOperation: |
|
504 if(iPASVMode) |
|
505 returnValue = EPerformingPasvForList; |
|
506 else |
|
507 returnValue = EPerformingPortForList; |
|
508 break; |
|
509 case EInitiateRetrieveOperation: |
|
510 returnValue = EPerformingTypeForRetrieve; |
|
511 break; |
|
512 case EInitiateStoreOperation: |
|
513 returnValue = EPerformingTypeForStore; |
|
514 break; |
|
515 case EPerformingTypeForRetrieve: |
|
516 if(iPASVMode) |
|
517 returnValue = EPerformingPasvForRetrieve; |
|
518 else |
|
519 returnValue = EPerformingPortForRetrieve; |
|
520 break; |
|
521 case EPerformingTypeForStore: |
|
522 if(iPASVMode) |
|
523 returnValue = EPerformingPasvForStore; |
|
524 else |
|
525 returnValue = EPerformingPortForStore; |
|
526 break; |
|
527 |
|
528 case EPerformingPortForList: |
|
529 case EPerformingPasvForList: |
|
530 returnValue = EPerformingList; |
|
531 break; |
|
532 case EPerformingPortForRetrieve: |
|
533 case EPerformingPasvForRetrieve: |
|
534 returnValue = EPerformingRestForRetrieve; |
|
535 break; |
|
536 case EPerformingPortForStore: |
|
537 case EPerformingPasvForStore: |
|
538 returnValue = EPerformingRestForStore; |
|
539 break; |
|
540 case EPerformingRestForRetrieve: |
|
541 returnValue = ERetrieveFile; |
|
542 break; |
|
543 case EPerformingRestForStore: |
|
544 returnValue = EStoreFile; |
|
545 break; |
|
546 case ERetrieveFile: |
|
547 case EStoreFileSendEOF: |
|
548 case EPerformingList: |
|
549 case ELoginPass: |
|
550 case EGetCurrentDirectory: |
|
551 case EChangeDirectory: |
|
552 case ECreateDirectory: |
|
553 case EDeleteDirectory: |
|
554 case EDeleteFile: |
|
555 case ERenameFileTo: |
|
556 case EAbortingStore: |
|
557 case EAbortingRetrieve: |
|
558 case EAbortingList: |
|
559 returnValue = EConnected; |
|
560 break; |
|
561 case EClosing: |
|
562 returnValue = EIdle; |
|
563 break; |
|
564 default: |
|
565 // Invalid state |
|
566 __ASSERT_DEBUG(FALSE, User::Panic(_L("CFTPSessionDerived::UpdateState Bad State"), 0)); |
|
567 returnValue = EIdle; |
|
568 break; |
|
569 } // switch (aState) |
|
570 break; |
|
571 //Cancel |
|
572 case MFtpProtocolNotifier::EOpCanceled: |
|
573 switch(aState) |
|
574 { |
|
575 case EAbortingStore: |
|
576 case EAbortingRetrieve: |
|
577 case EAbortingList: |
|
578 returnValue = EConnected; |
|
579 break; |
|
580 case EConnecting: |
|
581 returnValue = EIdle; |
|
582 break; |
|
583 default: |
|
584 //Otherwise don't change the state |
|
585 returnValue = aState; |
|
586 break; |
|
587 } |
|
588 break; |
|
589 // Error statuses |
|
590 case MFtpProtocolNotifier::EOpFailed: |
|
591 // This means I got a Negative answer from the PI Channel |
|
592 // hence PI Channel still there ==> still connected |
|
593 returnValue = EConnected; |
|
594 break; |
|
595 case MFtpProtocolNotifier::EOpConnectionReset: |
|
596 case MFtpProtocolNotifier::EOpConnectionFailed: |
|
597 case MFtpProtocolNotifier::EHostNotFound: |
|
598 //Simple massive errors |
|
599 // No need to look at the state |
|
600 returnValue = EIdle; |
|
601 break; |
|
602 case MFtpProtocolNotifier::EXferReset: |
|
603 case MFtpProtocolNotifier::EXferNotInitialised: |
|
604 returnValue = EConnected; |
|
605 break; |
|
606 case MFtpProtocolNotifier::ESocketError: |
|
607 switch(aState) |
|
608 { |
|
609 // Socket error while connecting PI Channel |
|
610 case EConnecting: |
|
611 returnValue = EIdle; |
|
612 break; |
|
613 // else socket error while openning DTP Channel |
|
614 default: |
|
615 // Check the state is in the acceptable range |
|
616 __ASSERT_DEBUG( |
|
617 (aState == EPerformingPortForList)|| |
|
618 (aState == EPerformingList)|| |
|
619 (aState == EPerformingPortForRetrieve)|| |
|
620 (aState == ERetrieveFile)|| |
|
621 (aState == EPerformingPortForStore)|| |
|
622 (aState == EStoreFile) |
|
623 , User::Panic(_L("CFTPSessionDerived::UpdateState Invalid State"), 0)); |
|
624 returnValue = EConnected; |
|
625 break; |
|
626 } |
|
627 break; |
|
628 default: |
|
629 // Invalid status |
|
630 __ASSERT_DEBUG(FALSE, User::Panic(_L("CFTPSessionDerived::UpdateState Bad Status"), 0)); |
|
631 returnValue = EIdle; |
|
632 break; |
|
633 } // switch (aStatus) |
|
634 return returnValue; |
|
635 } |
|
636 // |
|
637 // Creation, Destruction |
|
638 // |
|
639 |
|
640 CFTPSessionDerived::CFTPSessionDerived( |
|
641 ):iState(EIdle),FileServerInitialised(FALSE) |
|
642 ,iCurrentRepresentationType(EUninitialised),iRESTOffset(0) |
|
643 /** |
|
644 C++ constructor |
|
645 */ |
|
646 { |
|
647 } |
|
648 |
|
649 EXPORT_C CFTPSession* CFTPSession::NewL(MFtpSessionNotifier* aNotifier) |
|
650 /** Allocates and constructs a new FTP session object. |
|
651 * |
|
652 * @param aNotifier Callback interface to notify the client of the completion |
|
653 * of operations or to report errors. For each FTP session, the FTP client should |
|
654 * instantiate an object of this type. |
|
655 * @return New FTP session object */ |
|
656 { |
|
657 return CFTPSessionDerived::NewL(aNotifier); |
|
658 } |
|
659 |
|
660 CFTPSessionDerived* CFTPSessionDerived::NewL(MFtpSessionNotifier* aNotifier) |
|
661 { |
|
662 CFTPSessionDerived* self=new (ELeave) CFTPSessionDerived(); |
|
663 CleanupStack::PushL(self); |
|
664 self->ConstructL(aNotifier); |
|
665 CleanupStack::Pop(); |
|
666 return self; |
|
667 } |
|
668 |
|
669 void CFTPSessionDerived::ConstructL(MFtpSessionNotifier* aNotifier) |
|
670 /** |
|
671 2nd phase construction |
|
672 */ |
|
673 { |
|
674 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::ConstructL called\n")); |
|
675 iCFtpProtocol = CFtpProtocol::NewL(this); |
|
676 iCFTPSetError = CFTPSetError::NewL(this); |
|
677 User::LeaveIfError(iFs.Connect()); |
|
678 iFtpSessionNotifier = aNotifier; |
|
679 } |
|
680 |
|
681 CFTPSession::~CFTPSession() |
|
682 /** Destructor. */ |
|
683 {} |
|
684 |
|
685 CFTPSessionDerived::~CFTPSessionDerived() |
|
686 /** |
|
687 Destruction |
|
688 */ |
|
689 { |
|
690 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::~CFTPSessionDerived() called\n")); |
|
691 iFs.Close(); |
|
692 delete iCFtpProtocol; |
|
693 delete iCFTPSetError; |
|
694 } |
|
695 |
|
696 // |
|
697 // Interface |
|
698 // |
|
699 |
|
700 void CFTPSessionDerived::Connect( const TSockAddr& aNetAddr, //IP address |
|
701 const TDesC8& aUserName, |
|
702 const TDesC8& aPassword, |
|
703 const TConnectionMode aConnectionMode) |
|
704 /** |
|
705 Establish a connection with a server: |
|
706 */ |
|
707 { |
|
708 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::Connect called\n")); |
|
709 if(iState != EIdle) |
|
710 { |
|
711 // Already connected, BailOut |
|
712 iCFTPSetError->SetError(MFtpSessionNotifier::EAlreadyConnected); |
|
713 return; |
|
714 } |
|
715 iPASVMode = (aConnectionMode == Epassive); |
|
716 iBuffer1 = aUserName; |
|
717 iBuffer2 = aPassword; |
|
718 iTInt1 = aConnectionMode; |
|
719 iState = EConnecting; |
|
720 iCFtpProtocol->Connect((TSockAddr&)aNetAddr); |
|
721 } |
|
722 |
|
723 void CFTPSessionDerived::Connect( |
|
724 const THostName& aServerName, |
|
725 const TDesC8& aUserName, |
|
726 const TDesC8& aPassword, |
|
727 const TConnectionMode aConnectionMode, |
|
728 const TUint aPort) |
|
729 { |
|
730 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::Connect called\n")); |
|
731 if(iState != EIdle) |
|
732 { |
|
733 // Already connected, BailOut |
|
734 iCFTPSetError->SetError(MFtpSessionNotifier::EAlreadyConnected); |
|
735 return; |
|
736 } |
|
737 iPASVMode = (aConnectionMode == Epassive); |
|
738 iBuffer1 = aUserName; |
|
739 iBuffer2 = aPassword; |
|
740 iTInt1 = aConnectionMode; |
|
741 iState = EConnecting; |
|
742 iCFtpProtocol->Connect(aServerName,aPort); |
|
743 } |
|
744 |
|
745 void CFTPSessionDerived::Close() |
|
746 /** |
|
747 Close connection with a server |
|
748 */ |
|
749 { |
|
750 if(iState == EIdle) |
|
751 { |
|
752 // Not connected, BailOut |
|
753 iCFTPSetError->SetError(MFtpSessionNotifier::ENotConnected); |
|
754 return; |
|
755 } |
|
756 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::Close called\n")); |
|
757 iState = EClosing; |
|
758 iCFtpProtocol->Quit(); |
|
759 } |
|
760 |
|
761 void CFTPSessionDerived::Cancel() |
|
762 /** |
|
763 Cancel latest FTP operation |
|
764 */ |
|
765 { |
|
766 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::Cancel called\n")); |
|
767 // Switch on current state |
|
768 switch(iState) |
|
769 { |
|
770 // If I'm storing or recving files I need to negociate |
|
771 // an abort with the server |
|
772 case EStoreFile: |
|
773 iState = EAbortingStore; |
|
774 iCFtpProtocol->Abor(); |
|
775 iFile.Close(); |
|
776 break; |
|
777 case ERetrieveFile: |
|
778 iState = EAbortingRetrieve; |
|
779 iCFtpProtocol->Abor(); |
|
780 iFile.Close(); |
|
781 break; |
|
782 case EPerformingList: |
|
783 iState = EAbortingList; |
|
784 iCFtpProtocol->Abor(); |
|
785 break; |
|
786 // Simpler states, no negociation with server |
|
787 case EConnecting: |
|
788 // Note: UserCancel is synchronous, hence need to trigger a |
|
789 // notification myself |
|
790 iCFtpProtocol->UserCancel(); |
|
791 iCFTPSetError->SetError(MFtpSessionNotifier::EOpCanceled); |
|
792 break; |
|
793 default: |
|
794 // Cancel currently only implemented for store and receive |
|
795 iCFTPSetError->SetError(MFtpSessionNotifier::EOpCanceled); |
|
796 return; |
|
797 } |
|
798 } |
|
799 |
|
800 // Passes a marker object to store a position for an aborted transfer operation |
|
801 //void CFTPSessionDerived::SetMarker(TFTPRestartMarker& /*aTFTPRestartMarker*/) |
|
802 // { |
|
803 // FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::SetMarker called\n")); |
|
804 // FTPPROTDEBUG(_DBGFtpsess,_L("Not Implemented Yet -- Sorry\n")); |
|
805 // } |
|
806 |
|
807 void CFTPSessionDerived::Restart(const TUint aTFTPRestartOffset) |
|
808 /** |
|
809 Restart an aborted transfer operation |
|
810 */ |
|
811 { |
|
812 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::Restart called\n")); |
|
813 iRESTOffset = aTFTPRestartOffset; |
|
814 } |
|
815 |
|
816 void CFTPSessionDerived::Store(const TDesC& aLocalFileName, |
|
817 const TDesC8& aNewRemoteFileName, |
|
818 const TBool /*aOverwrite*/, |
|
819 const RepresentationType aRepresentationType, |
|
820 const TransferMode /*aTransferMode*/) |
|
821 /** |
|
822 Transfer APIs |
|
823 Store a file on the server |
|
824 */ |
|
825 { |
|
826 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::Store called\n")); |
|
827 if(iState == EIdle) |
|
828 { |
|
829 // Not connected, BailOut |
|
830 iCFTPSetError->SetError(MFtpSessionNotifier::ENotConnected); |
|
831 return; |
|
832 } |
|
833 // Try to open the local file |
|
834 if (iFile.Open(iFs, aLocalFileName, EFileRead|EFileShareReadersOnly) != KErrNone) |
|
835 { |
|
836 iCFTPSetError->SetError(MFtpSessionNotifier::EFileNotExist); |
|
837 return; |
|
838 } |
|
839 debugInt =0; |
|
840 iFileOpErrorOccured = FALSE; |
|
841 iBuffer1 = aNewRemoteFileName; |
|
842 iNewRepresentationType = aRepresentationType; |
|
843 iState = EInitiateStoreOperation; |
|
844 NormalTransition(); |
|
845 } |
|
846 |
|
847 void CFTPSessionDerived::Retrieve(const TDesC8& aRemoteFileName, |
|
848 const TDesC& aNewLocalFileName, |
|
849 const TOpenMode aOpenMode, |
|
850 const RepresentationType aRepresentationType, |
|
851 const TransferMode /*aTransferMode*/) |
|
852 /** |
|
853 Get a file from the server |
|
854 */ |
|
855 { |
|
856 TInt filepos; |
|
857 |
|
858 //initialise the flag for indicating if the exisiting local file has been renamed |
|
859 __ASSERT_DEBUG( iTempStore == FALSE, User::Panic(_L("CFTPSessionDerived::Retrieve"), 0) ); |
|
860 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::Retrieve called\n")); |
|
861 TBuf<256> fileName =aNewLocalFileName; |
|
862 TInt counter; |
|
863 // Set the path to C:\ as pert of the defetc Fix DEF058464, to avoid getting |
|
864 // files in C:\private\{uid} folder which is the expected behavior as part |
|
865 // of the platform security. |
|
866 iFs.SetSessionPath(_L("C:\\")); |
|
867 |
|
868 if(iState == EIdle) |
|
869 { |
|
870 // Not connected, BailOut |
|
871 iCFTPSetError->SetError(MFtpSessionNotifier::ENotConnected); |
|
872 return; |
|
873 } |
|
874 // Added to test |
|
875 iFs.SetSessionPath(_L("C:\\")); |
|
876 // Try to open the local file |
|
877 switch(aOpenMode) |
|
878 { |
|
879 case EOverwrite: |
|
880 // In the local folder see if the remote file already exists |
|
881 if(!iFile.Open(iFs, aNewLocalFileName, EFileWrite|EFileShareExclusive)) |
|
882 { |
|
883 for(counter=1;counter<5;counter++) |
|
884 { |
|
885 //Create a different name for the existing local file |
|
886 fileName.AppendNum(counter); |
|
887 |
|
888 // change the existing local file name to the one created above (if possible) |
|
889 // If renaming was succesful, then do the settings and start retrieving, else |
|
890 // try a new name. |
|
891 if(!iFile.Rename(fileName)) |
|
892 { |
|
893 iRenamedLocalFileName = fileName; |
|
894 break; |
|
895 } |
|
896 else |
|
897 { |
|
898 if(counter==4) |
|
899 { |
|
900 // all the 4 possible names are used.Use the last name |
|
901 iFs.Delete(fileName); |
|
902 if(iFile.Rename(fileName)!=KErrNone) |
|
903 { |
|
904 iCFTPSetError->SetError(MFtpSessionNotifier::EFileSystemError); |
|
905 return; |
|
906 } |
|
907 |
|
908 iRenamedLocalFileName = fileName; |
|
909 break; |
|
910 } |
|
911 fileName=aNewLocalFileName; |
|
912 } |
|
913 } |
|
914 iFile.Close(); |
|
915 iTempStore= TRUE; |
|
916 if(iFile.Replace(iFs,aNewLocalFileName,EFileWrite|EFileShareExclusive)!=KErrNone) |
|
917 { |
|
918 iCFTPSetError->SetError(MFtpSessionNotifier::EFileOpenFailure); |
|
919 return; |
|
920 } |
|
921 |
|
922 |
|
923 } |
|
924 else |
|
925 { |
|
926 if(iFile.Replace(iFs,aNewLocalFileName,EFileWrite|EFileShareExclusive)!=KErrNone) |
|
927 { |
|
928 iCFTPSetError->SetError(MFtpSessionNotifier::EFileOpenFailure); |
|
929 return; |
|
930 } |
|
931 } |
|
932 break; |
|
933 case ENoOverwrite: |
|
934 if (iFile.Create(iFs, aNewLocalFileName, EFileWrite|EFileShareExclusive) != KErrNone) |
|
935 { |
|
936 iCFTPSetError->SetError(MFtpSessionNotifier::EFileAlreadyExist); |
|
937 return; |
|
938 } |
|
939 break; |
|
940 case EExpand: |
|
941 // Open file |
|
942 if (iFile.Open(iFs, aNewLocalFileName, EFileWrite|EFileShareExclusive) != KErrNone) |
|
943 { |
|
944 iCFTPSetError->SetError(MFtpSessionNotifier::EFileNotExist); |
|
945 return; |
|
946 } |
|
947 // Goto EOF |
|
948 if (iFile.Seek(ESeekEnd,filepos) != KErrNone) |
|
949 { |
|
950 iFile.Close(); |
|
951 iCFTPSetError->SetError(MFtpSessionNotifier::EFileOpenFailure); |
|
952 return; |
|
953 } |
|
954 break; |
|
955 } |
|
956 // Set up parameters |
|
957 debugInt =0; |
|
958 iFileOpErrorOccured = FALSE; |
|
959 iBuffer1 = aRemoteFileName; |
|
960 iNewRepresentationType = aRepresentationType; |
|
961 iState = EInitiateRetrieveOperation; |
|
962 NormalTransition(); |
|
963 } |
|
964 |
|
965 void CFTPSessionDerived::ChangeDirectory(const TDesC8& aDirectoryName) |
|
966 { |
|
967 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::ChangeDirectory called\n")); |
|
968 if(iState == EIdle) |
|
969 { |
|
970 // Not connected, BailOut |
|
971 iCFTPSetError->SetError(MFtpSessionNotifier::ENotConnected); |
|
972 return; |
|
973 } |
|
974 iState = EGetCurrentDirectory; |
|
975 iCFtpProtocol->Cwd(aDirectoryName); |
|
976 } |
|
977 |
|
978 void CFTPSessionDerived::CreateDirectory(const TDesC8& aDirectoryName) |
|
979 { |
|
980 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::CreateDirectory called\n")); |
|
981 if(iState == EIdle) |
|
982 { |
|
983 // Not connected, BailOut |
|
984 iCFTPSetError->SetError(MFtpSessionNotifier::ENotConnected); |
|
985 return; |
|
986 } |
|
987 iState = ECreateDirectory; |
|
988 iCFtpProtocol->Mkd(aDirectoryName); |
|
989 } |
|
990 |
|
991 void CFTPSessionDerived::DeleteDirectory(const TDesC8& aDirectoryName) |
|
992 { |
|
993 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::DeleteDirectory called\n")); |
|
994 if(iState == EIdle) |
|
995 { |
|
996 // Not connected, BailOut |
|
997 iCFTPSetError->SetError(MFtpSessionNotifier::ENotConnected); |
|
998 return; |
|
999 } |
|
1000 iState = EDeleteDirectory; |
|
1001 iCFtpProtocol->Rmd(aDirectoryName); |
|
1002 } |
|
1003 |
|
1004 void CFTPSessionDerived::GetCurrentDirectory() |
|
1005 { |
|
1006 // Result on the PI connection between quotes |
|
1007 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::GetCurrentDirectory called\n")); |
|
1008 if(iState == EIdle) |
|
1009 { |
|
1010 // Not connected, BailOut |
|
1011 iCFTPSetError->SetError(MFtpSessionNotifier::ENotConnected); |
|
1012 return; |
|
1013 } |
|
1014 iState = EGetCurrentDirectory; |
|
1015 iCFtpProtocol->Pwd(); |
|
1016 } |
|
1017 |
|
1018 void CFTPSessionDerived::ListDirectory(const TDesC8& aDirectoryName,TDes8& aFileList) |
|
1019 { |
|
1020 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::ListDirectory called\n")); |
|
1021 if(iState == EIdle) |
|
1022 { |
|
1023 // Not connected, BailOut |
|
1024 iCFTPSetError->SetError(MFtpSessionNotifier::ENotConnected); |
|
1025 return; |
|
1026 } |
|
1027 if (iState != EPerformingList) |
|
1028 { |
|
1029 iBuffer1 = aDirectoryName; |
|
1030 iFileList = &aFileList; |
|
1031 iState = EInitiateListOperation; |
|
1032 NormalTransition(); |
|
1033 } |
|
1034 else |
|
1035 { |
|
1036 // Set buffer |
|
1037 iCFtpProtocol->RecvBuffer(&aFileList); |
|
1038 } |
|
1039 } |
|
1040 |
|
1041 void CFTPSessionDerived::DeleteFile(const TDesC8& aFileName) |
|
1042 { |
|
1043 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::DeleteFile called\n")); |
|
1044 if(iState == EIdle) |
|
1045 { |
|
1046 // Not connected, BailOut |
|
1047 iCFTPSetError->SetError(MFtpSessionNotifier::ENotConnected); |
|
1048 return; |
|
1049 } |
|
1050 iState = EDeleteFile; |
|
1051 iCFtpProtocol->Dele(aFileName); |
|
1052 } |
|
1053 |
|
1054 void CFTPSessionDerived::RenameFile( const TDesC8& aRemoteFileName, |
|
1055 const TDesC8& aNewRemoteFileName) |
|
1056 { |
|
1057 FTPPROTDEBUG(_DBGFtpsess,_L("CFTPSessionDerived::RenameFile called\n")); |
|
1058 if(iState == EIdle) |
|
1059 { |
|
1060 // Not connected, BailOut |
|
1061 iCFTPSetError->SetError(MFtpSessionNotifier::ENotConnected); |
|
1062 return; |
|
1063 } |
|
1064 iState = ERenameFileFrom; |
|
1065 iBuffer1 = aNewRemoteFileName; |
|
1066 iCFtpProtocol->Rnfr(aRemoteFileName); |
|
1067 } |
|
1068 |
|
1069 EXPORT_C TUint32 CFTPSession::GetVersion(void) |
|
1070 /** Gets API version information. |
|
1071 * |
|
1072 * The vesion information format is: |
|
1073 * |
|
1074 * @li byte 3: ftpsess dll major version |
|
1075 * @li byte 2: ftpsess dll minor version |
|
1076 * @li byte 1: ftpprot dll major version |
|
1077 * @li byte 0: ftpprot dll minor version |
|
1078 * |
|
1079 * @return Version information */ |
|
1080 { |
|
1081 TUint32 result; |
|
1082 result = FTPSESS_VERSION_NUMBER; |
|
1083 result = result<<16; |
|
1084 result = result | CFtpProtocol::GetVersion(); |
|
1085 return result; |
|
1086 } |
|
1087 |
|
1088 // |
|
1089 // DLL Entry point |
|
1090 // |
|
1091 |