hti/HtiServicePlugins/HtiFtpServicePlugin/src/HtiFtpServicePlugin.cpp
changeset 0 a03f92240627
child 4 73ff0d268e1d
equal deleted inserted replaced
-1:000000000000 0:a03f92240627
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  HtiFtpServicePlugin implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "HtiFtpServicePlugin.h"
       
    21 #include <HtiFtpBackupFakeBase.h>
       
    22 #include <HtiDispatcherInterface.h>
       
    23 #include <HTILogging.h>
       
    24 
       
    25 #include <e32property.h>
       
    26 #include <hash.h>
       
    27 
       
    28 // CONSTANTS
       
    29 static const TUid KFtpServiceUid = { 0x1020DEC5 };
       
    30 _LIT( KBackslash, "\\" );
       
    31 _LIT( KRootPathFormat, "%c:\\" );
       
    32 _LIT( KHtiFtpBackupFakeDllName, "HtiFtpBackupFake.dll" );
       
    33 const static TInt KMinBufferSize = 1024;
       
    34 const static TUint8 KUnicodeMask = 0x1;
       
    35 const static TInt KFileSizeMsgSize = 5;
       
    36 
       
    37 //error description
       
    38 _LIT8( KErrDescrUnknownCmd, "unknown command" );
       
    39 _LIT8( KErrDescrNoSpace, "no disk space" );
       
    40 _LIT8( KErrDescrFailedRead, "failed read file" );
       
    41 _LIT8( KErrDescrFailedWrite, "failed write file" );
       
    42 _LIT8( KErrDescrFailedGetDir, "failed to read directory" );
       
    43 _LIT8( KErrDescrEmptyDirname, "directory name empty" );
       
    44 _LIT8( KErrDescrInvalidDirnameLength, "invalid directory name length" );
       
    45 _LIT8( KErrDescrEmptyFilename, "file name empty" );
       
    46 _LIT8( KErrDescrInvalidFilenameLength, "invalid file name length" );
       
    47 _LIT8( KErrDescrFailedMkDir, "failed create directory" );
       
    48 _LIT8( KErrDescrFailedRmDir, "failed remove directory" );
       
    49 _LIT8( KErrDescrFailedDeleFile, "failed delete file" );
       
    50 _LIT8( KErrDescrFailedRenameFile, "failed rename file" );
       
    51 _LIT8( KErrDescrFailedCopyFile, "failed copy file" );
       
    52 _LIT8( KErrDescrFailedMoveFile, "failed move file" );
       
    53 _LIT8( KErrDescrInvalidStorArgument, "invalid arguments" );
       
    54 _LIT8( KErrDescrFailedCreateFile, "invalid create file" );
       
    55 _LIT8( KErrDescrFailedOpenFile, "failed open file" );
       
    56 _LIT8( KErrDescrFailedCloseFile, "failed close file" );
       
    57 _LIT8( KErrDescrInvalidDataMessage, "invalid data message" );
       
    58 _LIT8( KErrDescrNoMemory, "no memory to send file" );
       
    59 _LIT8( KErrDescrNoCancel, "nothing to cancel" );
       
    60 _LIT8( KErrDescrBusy, "Busy" );
       
    61 _LIT8( KErrDescrFailedCopyTcb, "Failed to copy to Tcb directories" );
       
    62 _LIT8( KErrDescrInvalidForceArgs, "invalid arguments for setforce" );
       
    63 _LIT8( KErrDescrInvalidChecksumArgs, "invalid arguments for checksum" );
       
    64 _LIT8( KErrDescrInvalidFormatArgs, "invalid arguments for format" );
       
    65 _LIT8( KErrDescrInvalidDriveListArgs, "invalid arguments for drive list" );
       
    66 _LIT8( KErrDescrFailedFormat, "failed to format" );
       
    67 _LIT8( KErrDescrNotSupported, "command not supported" );
       
    68 
       
    69 // MACROS
       
    70 
       
    71 // LOCAL CONSTANTS AND MACROS
       
    72 
       
    73 // MODULE DATA STRUCTURES
       
    74 
       
    75 // LOCAL FUNCTION PROTOTYPES
       
    76 
       
    77 // FORWARD DECLARATIONS
       
    78 
       
    79 // ============================ MEMBER FUNCTIONS ===============================
       
    80 
       
    81 /**
       
    82 * CFtpHandlerAO implementation
       
    83 */
       
    84 CFtpHandlerAO::CFtpHandlerAO( MFtpObserverAO* anObserver ):
       
    85     CActive( EPriorityStandard ),
       
    86     iObserver( anObserver ),
       
    87     iCancelFileMan( EFalse )
       
    88     {
       
    89     CActiveScheduler::Add( this );
       
    90     }
       
    91 
       
    92 CFtpHandlerAO::~CFtpHandlerAO()
       
    93     {
       
    94     }
       
    95 
       
    96 void CFtpHandlerAO::Start()
       
    97     {
       
    98     SetActive();
       
    99     }
       
   100 
       
   101 MFileManObserver::TControl CFtpHandlerAO::NotifyFileManStarted()
       
   102     {
       
   103     return iCancelFileMan?
       
   104                 MFileManObserver::EAbort :
       
   105                 MFileManObserver::EContinue;
       
   106     }
       
   107 
       
   108 MFileManObserver::TControl CFtpHandlerAO::NotifyFileManOperation()
       
   109     {
       
   110     return iCancelFileMan?
       
   111                 MFileManObserver::ECancel :
       
   112                 MFileManObserver::EContinue;
       
   113     }
       
   114 
       
   115 MFileManObserver::TControl CFtpHandlerAO::NotifyFileManEnded()
       
   116     {
       
   117     return iCancelFileMan?
       
   118                 MFileManObserver::EAbort :
       
   119                 MFileManObserver::EContinue;
       
   120     }
       
   121 
       
   122 void CFtpHandlerAO::RunL()
       
   123     {
       
   124     iObserver->FtpComplete( iStatus.Int() );
       
   125     }
       
   126 
       
   127 void CFtpHandlerAO::DoCancel()
       
   128     {
       
   129     iCancelFileMan = ETrue;
       
   130     }
       
   131 
       
   132 CProcessLogonAO::CProcessLogonAO( MFtpObserverAO* anObserver ):
       
   133     CActive( EPriorityStandard ),
       
   134     iObserver( anObserver )
       
   135     {
       
   136     CActiveScheduler::Add( this );
       
   137     }
       
   138 
       
   139 CProcessLogonAO::~CProcessLogonAO()
       
   140     {
       
   141     Cancel();
       
   142     iProcess.Close();
       
   143     }
       
   144 
       
   145 void CProcessLogonAO::Start( const TDesC& aCmdLine )
       
   146     {
       
   147     TInt err = iProcess.Create( KHtiFileHlp, aCmdLine );
       
   148 
       
   149     if ( err == KErrNone )
       
   150         {
       
   151         iProcess.Logon( iStatus );
       
   152         //start HtiFileHlp.exe
       
   153         iProcess.Resume();
       
   154         }
       
   155     else
       
   156         {
       
   157         TRequestStatus* pS = &iStatus;
       
   158         User::RequestComplete( pS, err );
       
   159         }
       
   160     SetActive();
       
   161     }
       
   162 
       
   163 void CProcessLogonAO::RunL()
       
   164     {
       
   165     iObserver->FtpComplete( iStatus.Int() );
       
   166     }
       
   167 
       
   168 void CProcessLogonAO::DoCancel()
       
   169     {
       
   170     iProcess.LogonCancel( iStatus );
       
   171     }
       
   172 
       
   173 // Create instance of concrete ECOM interface implementation
       
   174 CHtiFtpServicePlugin* CHtiFtpServicePlugin::NewL()
       
   175     {
       
   176     CHtiFtpServicePlugin* self = new (ELeave) CHtiFtpServicePlugin;
       
   177     CleanupStack::PushL (self);
       
   178     self->ConstructL();
       
   179     CleanupStack::Pop();
       
   180     return self;
       
   181     }
       
   182 
       
   183 // Constructor
       
   184 CHtiFtpServicePlugin::CHtiFtpServicePlugin():
       
   185     iState( EIdle ),
       
   186     iSendBuffer( NULL ),
       
   187     iSendBufferDes( NULL, 0 ),
       
   188     iHandlerAO( NULL ),
       
   189     iProcessLogonAO( NULL )
       
   190     {
       
   191     }
       
   192 
       
   193 CHtiFtpServicePlugin::~CHtiFtpServicePlugin()
       
   194     {
       
   195     HTI_LOG_FUNC_IN("~CHtiFtpServicePlugin");
       
   196     delete iSendBuffer;
       
   197     delete iHandlerAO;
       
   198     delete iProcessLogonAO;
       
   199     delete iFileMan;
       
   200     delete iBackupFake;
       
   201     iBackupFakeLib.Close();
       
   202     iFs.Close();
       
   203     HTI_LOG_FUNC_OUT("~CHtiFtpServicePlugin");
       
   204     }
       
   205 
       
   206 // Second phase construction.
       
   207 void CHtiFtpServicePlugin::ConstructL()
       
   208     {
       
   209     HTI_LOG_FUNC_IN("CHtiFtpServicePlugin::ConstructL");
       
   210 
       
   211     User::LeaveIfError( iFs.Connect() );
       
   212     iFileMan = CFileMan::NewL( iFs );
       
   213 
       
   214     HTI_LOG_FUNC_OUT("CHtiFtpServicePlugin::ConstructL");
       
   215     }
       
   216 
       
   217 void CHtiFtpServicePlugin::InitL()
       
   218     {
       
   219     if ( iDispatcher->GetFreeMemory() < KMinBufferSize )
       
   220         {
       
   221         User::Leave( KErrNoMemory );
       
   222         }
       
   223     // just guess
       
   224 
       
   225     iBufferSize = iDispatcher->GetFreeMemory()/10;
       
   226     iBufferSize = iBufferSize < KMinBufferSize ?
       
   227                     KMinBufferSize :
       
   228                     iBufferSize;
       
   229     //test
       
   230     iBufferSize = 3*KMinBufferSize;
       
   231     }
       
   232 
       
   233 TInt CHtiFtpServicePlugin::SendControlMsg( TFtpCommand aCmd,
       
   234                                            const TDesC8& aMsg  )
       
   235     {
       
   236     TInt err = KErrNone;
       
   237     HBufC8* temp = NULL;
       
   238     TRAP( err, temp = HBufC8::NewL( 1 + aMsg.Size() ) );
       
   239     if ( err == KErrNone )
       
   240         {
       
   241         temp->Des().Append( aCmd );
       
   242         temp->Des().Append( aMsg );
       
   243         err = iDispatcher->DispatchOutgoingMessage( temp,
       
   244                         KFtpServiceUid,
       
   245                         EFalse,
       
   246                         EHtiPriorityControl );
       
   247         if ( err != KErrNone )
       
   248             {
       
   249             delete temp;
       
   250             }
       
   251         }
       
   252     return err;
       
   253     }
       
   254 
       
   255 inline TInt CHtiFtpServicePlugin::SendErrorMsg( TInt anError,
       
   256                                                 const TDesC8& aMsg )
       
   257     {
       
   258     SetBURFakeState( EFalse ); // errors ignored
       
   259     return iDispatcher->DispatchOutgoingErrorMessage( anError,
       
   260                                                aMsg,
       
   261                                                KFtpServiceUid);
       
   262     }
       
   263 
       
   264 TBool CHtiFtpServicePlugin::IsBusy()
       
   265     {
       
   266     return !( iState == EIdle || iState == EStorWait || iState == ERetrWait );
       
   267     }
       
   268 
       
   269 void CHtiFtpServicePlugin::ProcessMessageL(const TDesC8& aMessage,
       
   270                 THtiMessagePriority aPriority)
       
   271     {
       
   272     HTI_LOG_FUNC_IN("CHtiFtpServicePlugin::ProcessMessage");
       
   273 
       
   274     if ( IsBusy() )
       
   275         {
       
   276         //should not happend - service busy,deny request
       
   277         User::Leave( KErrInUse );
       
   278         }
       
   279 
       
   280     switch ( aPriority )
       
   281         {
       
   282         case EHtiPriorityData:
       
   283             {
       
   284             HandleDataMessageL( aMessage );
       
   285             }
       
   286             break;
       
   287         case EHtiPriorityControl:
       
   288             {
       
   289             if ( iState == EStorWait || iState == ERetrWait )
       
   290                 {
       
   291                 HandleCancelL( aMessage );
       
   292                 }
       
   293             else
       
   294                 {
       
   295                 HandleControlMessageL( aMessage );
       
   296                 }
       
   297             }
       
   298             break;
       
   299         default:
       
   300             HTI_LOG_TEXT("Unknown priority");
       
   301         }
       
   302 
       
   303     HTI_LOG_FUNC_OUT("CHtiFtpServicePlugin::ProcessMessage");
       
   304     }
       
   305 
       
   306 void CHtiFtpServicePlugin::HandleControlMessageL(const TDesC8& aMessage)
       
   307     {
       
   308     TInt err = KErrNone;
       
   309 
       
   310     if ( aMessage.Length() > 0 )
       
   311         {
       
   312         TBool unicode = aMessage[0]&KUnicodeMask;
       
   313         HTI_LOG_FORMAT("cmd %d", aMessage[0] );
       
   314 
       
   315         switch ( aMessage[0] )
       
   316             {
       
   317             case EFtpSTOR:
       
   318             case EFtpSTOR_u:
       
   319                 {
       
   320                 //receive file
       
   321                 //get filesize
       
   322                 if ( aMessage.Length() > 5 )
       
   323                     {
       
   324                     iFileSize = aMessage[1] +
       
   325                                 ( aMessage[2] << 8  ) +
       
   326                                 ( aMessage[3] << 16 ) +
       
   327                                 ( aMessage[4] << 24 );
       
   328 
       
   329                     HTI_LOG_FORMAT( "Filesize %d", iFileSize );
       
   330                     //get fileName
       
   331                     if ( GetFileNameL( aMessage.Mid( 5 ),
       
   332                                       unicode ) )
       
   333                         {
       
   334                         HandleReceiveFileL();
       
   335                         }
       
   336                     }
       
   337                 else
       
   338                     {
       
   339                     HTI_LOG_TEXT("no file size or file name");
       
   340                     User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   341                                             KErrDescrInvalidStorArgument) );
       
   342 
       
   343                     }
       
   344                 }
       
   345                 break;
       
   346             case EFtpRETR:
       
   347             case EFtpRETR_u:
       
   348                 {
       
   349                 //send file
       
   350                 //get fileName and size
       
   351                 if ( GetFileNameL( aMessage.Mid(1),
       
   352                                    unicode ) )
       
   353                     {
       
   354                     HandleSendFileL();
       
   355                     }
       
   356                 }
       
   357                 break;
       
   358             case EFtpLIST:
       
   359             case EFtpLIST_u:
       
   360             case EFtpLISTSIZES:
       
   361             case EFtpLISTSIZES_u:
       
   362                 {
       
   363                 if ( GetDirectoryL( aMessage.Mid( 1 ),
       
   364                                    unicode ) )
       
   365                     {
       
   366                     HandleListL( unicode,
       
   367                         ( KEntryAttHidden| KEntryAttSystem|KEntryAttNormal ),
       
   368                         ( aMessage[0] == EFtpLISTSIZES ||
       
   369                           aMessage[0] == EFtpLISTSIZES_u ) );
       
   370                     }
       
   371                 }
       
   372                 break;
       
   373             case EFtpLISTDIR:
       
   374             case EFtpLISTDIR_u:
       
   375                 {
       
   376                 if ( GetDirectoryL( aMessage.Mid( 1 ),
       
   377                                    unicode ) )
       
   378                     {
       
   379                     HandleListL( unicode,
       
   380                                 (KEntryAttMatchExclusive|
       
   381                                 KEntryAttHidden|KEntryAttSystem|
       
   382                                 KEntryAttDir), EFalse );
       
   383                     }
       
   384                 }
       
   385                 break;
       
   386             case EFtpMKD:
       
   387             case EFtpMKD_u:
       
   388                 {
       
   389                 if ( GetDirectoryL( aMessage.Mid( 1 ),
       
   390                                    unicode ) )
       
   391                     {
       
   392                     if ( IsFileTcb( iFileName ) )
       
   393                         {
       
   394                         HandleTcbMkdL( iFileName );
       
   395                         }
       
   396                     else
       
   397                         {
       
   398                         err = iFs.MkDirAll( iFileName );
       
   399                         if ( err == KErrNone || err == KErrAlreadyExists )
       
   400                             {
       
   401                             User::LeaveIfError( SendControlMsg( EFtpOK,
       
   402                                                                 KNullDesC8) );
       
   403                             }
       
   404                         else
       
   405                             {
       
   406                             User::LeaveIfError( SendErrorMsg( err,
       
   407                                                         KErrDescrFailedMkDir ) );
       
   408 
       
   409                             }
       
   410                         }
       
   411                     }
       
   412                 }
       
   413                 break;
       
   414             case EFtpRMD:
       
   415             case EFtpRMD_u:
       
   416                 {
       
   417                 if ( GetDirectoryL( aMessage.Mid( 1 ),
       
   418                                    unicode ) )
       
   419                     {
       
   420                     if ( IsFileTcb( iFileName ) )
       
   421                         {
       
   422                         HandleTcbRmdL( iFileName );
       
   423                         }
       
   424                     else
       
   425                         {
       
   426                         delete iHandlerAO;
       
   427                         iHandlerAO = NULL;
       
   428 
       
   429                         iHandlerAO = new(ELeave) CFtpHandlerAO( this );
       
   430                         err = iFileMan->RmDir( iFileName, iHandlerAO->iStatus );
       
   431                         if ( err == KErrNone)
       
   432                             {
       
   433                             iState = ERmdBusy;
       
   434                             iHandlerAO->Start();
       
   435                             }
       
   436                         else
       
   437                             {
       
   438                             delete iHandlerAO;
       
   439                             iHandlerAO = NULL;
       
   440                             User::LeaveIfError( SendErrorMsg( err,
       
   441                                                         KErrDescrFailedRmDir ) );
       
   442                             }
       
   443                         }
       
   444                     }
       
   445                 }
       
   446                 break;
       
   447             case EFtpDELE:
       
   448             case EFtpDELE_u:
       
   449                 {
       
   450                 if ( GetFileNameL( aMessage.Mid( 1 ),
       
   451                                    unicode ) )
       
   452                     {
       
   453                     if ( IsFileTcb( iFileName ) )
       
   454                         {
       
   455                         HandleTcbDeleteL( iFileName );
       
   456                         }
       
   457                     else
       
   458                         {
       
   459                         HandleDeleteL( iFileName );
       
   460                         }
       
   461                     }
       
   462                 }
       
   463                 break;
       
   464             case EFtpCANCEL:
       
   465                 {
       
   466                 User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   467                                                 KErrDescrNoCancel ) );
       
   468                 }
       
   469                 break;
       
   470             case EFtpSETFORCE:
       
   471                 {
       
   472                 if ( aMessage.Length() != 2 )
       
   473                     {
       
   474                     User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   475                                                 KErrDescrInvalidForceArgs ) );
       
   476                     }
       
   477                 else
       
   478                     {
       
   479                     if ( aMessage[1] )
       
   480                         {
       
   481                         // Setting forced operations on
       
   482                         err = iBackupFakeLib.Load( KHtiFtpBackupFakeDllName );
       
   483                         HTI_LOG_FORMAT( "BackupFake DLL load returned %d", err );
       
   484                         if ( err == KErrNone && iBackupFakeLib.Type()[1] ==
       
   485                                 KHtiFtpBackupFakeInterfaceUid )
       
   486                             {
       
   487                             HTI_LOG_TEXT( "BackupFake DLL found" );
       
   488                             TLibraryFunction entry = iBackupFakeLib.Lookup( 1 );
       
   489                             if ( entry != NULL )
       
   490                                 {
       
   491                                 iBackupFake = ( CHtiFtpBackupFakeBase* ) entry();
       
   492                                 TRAP( err, iBackupFake->ConstructL( &iFs ) );
       
   493                                 }
       
   494                             }
       
   495                         if ( err == KErrNone )
       
   496                             {
       
   497                             SendControlMsg( EFtpOK, KNullDesC8 );
       
   498                             }
       
   499                         else
       
   500                             {
       
   501                             User::LeaveIfError( SendErrorMsg( KErrNotSupported,
       
   502                                 KErrDescrNotSupported ) );
       
   503                             }
       
   504                         }
       
   505                     else
       
   506                         {
       
   507                         // Setting forced operations off
       
   508                         delete iBackupFake;
       
   509                         iBackupFake = NULL;
       
   510                         iBackupFakeLib.Close();
       
   511                         SendControlMsg( EFtpOK, KNullDesC8 );
       
   512                         }
       
   513                     }
       
   514                 }
       
   515                 break;
       
   516             case EFtpCHECKSUM:
       
   517             case EFtpCHECKSUM_u:
       
   518                 {
       
   519                 if ( aMessage.Length() < 3 )
       
   520                     {
       
   521                     User::LeaveIfError(
       
   522                         SendErrorMsg( KErrArgument,
       
   523                                       KErrDescrInvalidChecksumArgs ) );
       
   524                     }
       
   525                 else if ( GetFileNameL( aMessage.Mid( 2 ), unicode ) )
       
   526                     {
       
   527                     HandleCheckSumCalcL( (TAlgorithm)aMessage[1], iFileName );
       
   528                     }
       
   529                 }
       
   530                 break;
       
   531             case EFtpFORMAT:
       
   532                 {
       
   533                 if ( aMessage.Length() != 3 )
       
   534                     {
       
   535                     User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   536                             KErrDescrInvalidFormatArgs ) );
       
   537                     }
       
   538                 else
       
   539                     {
       
   540                     HandleFormat( aMessage[1], aMessage[2] );
       
   541                     }
       
   542                 }
       
   543                 break;
       
   544             case EFtpLISTDRIVES:
       
   545             case EFtpLISTDRIVES_u:
       
   546                 {
       
   547                 if ( aMessage.Length() != 1 )
       
   548                     {
       
   549                     User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   550                         KErrDescrInvalidDriveListArgs ) );
       
   551                     }
       
   552                 else
       
   553                     {
       
   554                     HandleListDrivesL( unicode );
       
   555                     }
       
   556                 }
       
   557                 break;
       
   558             case EFtpRENAME:
       
   559             case EFtpRENAME_u:
       
   560                 {
       
   561                 HandleRenameL( aMessage, unicode );
       
   562                 }
       
   563                 break;
       
   564             case EFtpCOPY:
       
   565             case EFtpCOPY_u:
       
   566                 {
       
   567                 HandleCopyL( aMessage, unicode );
       
   568                 }
       
   569                 break;
       
   570             case EFtpMOVE:
       
   571             case EFtpMOVE_u:
       
   572                 {
       
   573                 HandleMoveL( aMessage, unicode );
       
   574                 }
       
   575                 break;
       
   576             default:
       
   577                 {
       
   578                 //Error: unknown command
       
   579                 User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   580                                                 KErrDescrUnknownCmd ) );
       
   581                 }
       
   582             }
       
   583         }
       
   584     else
       
   585         {
       
   586         //error: empty request
       
   587         User::LeaveIfError( SendErrorMsg( KErrArgument, KErrDescrUnknownCmd ) );
       
   588         }
       
   589     }
       
   590 
       
   591 void CHtiFtpServicePlugin::HandleDeleteL( const TDesC& aFilename )
       
   592     {
       
   593     HTI_LOG_FUNC_IN( "CHtiFtpServicePlugin::HandleDeleteL" );
       
   594     delete iHandlerAO;
       
   595     iHandlerAO = NULL;
       
   596 
       
   597     iHandlerAO = new(ELeave) CFtpHandlerAO( this );
       
   598     TInt err = iFileMan->Delete( aFilename, 0, iHandlerAO->iStatus );
       
   599     if ( err == KErrNone)
       
   600         {
       
   601         iState = EDeleBusy;
       
   602         iHandlerAO->Start();
       
   603         }
       
   604     else
       
   605         {
       
   606         HTI_LOG_FORMAT( "Delete failed %d", err );
       
   607         delete iHandlerAO;
       
   608         iHandlerAO = NULL;
       
   609         User::LeaveIfError( SendErrorMsg( err,
       
   610                                     KErrDescrFailedDeleFile ) );
       
   611         }
       
   612     HTI_LOG_FUNC_OUT( "CHtiFtpServicePlugin::HandleDeleteL" );
       
   613     }
       
   614 
       
   615 void CHtiFtpServicePlugin::HandleRenameL( const TDesC8& aMessage, TBool aUnicode )
       
   616     {
       
   617     delete iHandlerAO;
       
   618     iHandlerAO = NULL;
       
   619 
       
   620     TInt originalLenngth = aMessage[1];
       
   621 
       
   622     GetFileNameL( aMessage.Mid( 1, originalLenngth + 1 ), aUnicode );
       
   623 
       
   624     //If last character is back slash remove it
       
   625     RemoveEndBackslash(iFileName);
       
   626 
       
   627     TFileName origName = iFileName;
       
   628 
       
   629     TInt newNamePos = originalLenngth + 2 ;
       
   630     TInt newNameLength = aMessage[newNamePos];
       
   631     GetFileNameL( aMessage.Mid( newNamePos, newNameLength + 1 ), aUnicode );
       
   632 
       
   633     RemoveEndBackslash( iFileName );
       
   634 
       
   635     if ( IsFileTcb( origName ) || IsFileTcb( iFileName ) )
       
   636         {
       
   637         HandleTcbRenameL( origName, iFileName );
       
   638         }
       
   639     else
       
   640         {
       
   641         iHandlerAO = new ( ELeave ) CFtpHandlerAO( this );
       
   642         TInt err = iFileMan->Rename( origName, iFileName,
       
   643                     CFileMan::EOverWrite, iHandlerAO->iStatus );
       
   644 
       
   645         if ( err == KErrNone )
       
   646             {
       
   647             iState = ERenameBusy;
       
   648             iHandlerAO->Start();
       
   649             }
       
   650         else
       
   651             {
       
   652             HTI_LOG_FORMAT( "Rename failed %d", err );
       
   653             delete iHandlerAO;
       
   654             iHandlerAO = NULL;
       
   655             User::LeaveIfError( SendErrorMsg(
       
   656                     err, KErrDescrFailedRenameFile ) );
       
   657             }
       
   658         }
       
   659 
       
   660     }
       
   661 
       
   662 void CHtiFtpServicePlugin::RemoveEndBackslash( TFileName& aFileName  )
       
   663     {
       
   664     //If last character is back slash remove it
       
   665     if ( aFileName.Right( 1 ) == KBackslash )
       
   666         {
       
   667         aFileName.Delete( aFileName.Length() - 1, 1 );
       
   668         }
       
   669     }
       
   670 
       
   671 void CHtiFtpServicePlugin::HandleCopyL( const TDesC8& aMessage, TBool aUnicode )
       
   672     {
       
   673     delete iHandlerAO;
       
   674     iHandlerAO = NULL;
       
   675 
       
   676     TInt originalLenngth = aMessage[1];
       
   677 
       
   678     GetFileNameL( aMessage.Mid( 1, originalLenngth + 1 ), aUnicode );
       
   679     TFileName origName = iFileName;
       
   680 
       
   681     TInt newNamePos = originalLenngth + 2 ;
       
   682     TInt newNameLength = aMessage[newNamePos];
       
   683     GetFileNameL( aMessage.Mid( newNamePos, newNameLength + 1 ), aUnicode );
       
   684 
       
   685     if ( IsFileTcb( origName ) || IsFileTcb( iFileName ) )
       
   686         {
       
   687         HandleTcbCopyL( origName, iFileName );
       
   688         }
       
   689     else{
       
   690         iHandlerAO = new ( ELeave ) CFtpHandlerAO( this );
       
   691         TInt err = iFileMan->Copy( origName, iFileName,
       
   692                 ( CFileMan::EOverWrite | CFileMan::ERecurse ),
       
   693                 iHandlerAO->iStatus );
       
   694 
       
   695         if ( err == KErrNone)
       
   696             {
       
   697             iState = ECopyBusy;
       
   698             iHandlerAO->Start();
       
   699             }
       
   700         else
       
   701             {
       
   702             HTI_LOG_FORMAT( "Rename failed %d", err );
       
   703             delete iHandlerAO;
       
   704             iHandlerAO = NULL;
       
   705             User::LeaveIfError( SendErrorMsg( err,
       
   706                                         KErrDescrFailedCopyFile ) );
       
   707             }
       
   708         }
       
   709     }
       
   710 
       
   711 void CHtiFtpServicePlugin::HandleMoveL( const TDesC8& aMessage, TBool aUnicode )
       
   712     {
       
   713     delete iHandlerAO;
       
   714     iHandlerAO = NULL;
       
   715 
       
   716     TInt originalLenngth = aMessage[1];
       
   717 
       
   718     GetFileNameL( aMessage.Mid( 1, originalLenngth + 1 ), aUnicode );
       
   719 
       
   720     RemoveEndBackslash( iFileName );
       
   721 
       
   722     TFileName origName = iFileName;
       
   723 
       
   724     TInt newNamePos = originalLenngth + 2 ;
       
   725     TInt newNameLength = aMessage[newNamePos];
       
   726     GetFileNameL( aMessage.Mid( newNamePos, newNameLength + 1 ), aUnicode );
       
   727     // make sure destination ends with backslash - destination always directory
       
   728     if ( iFileName.Right( 1 ) != KBackslash )
       
   729         {
       
   730         iFileName.Append( KBackslash );
       
   731         }
       
   732 
       
   733     if ( IsFileTcb( origName ) || IsFileTcb( iFileName ) )
       
   734         {
       
   735         HandleTcbMoveL( origName, iFileName );
       
   736         }
       
   737     else
       
   738         {
       
   739         iHandlerAO = new ( ELeave ) CFtpHandlerAO( this );
       
   740         TInt err = iFileMan->Move( origName, iFileName,
       
   741                 ( CFileMan::EOverWrite | CFileMan::ERecurse ),
       
   742                 iHandlerAO->iStatus );
       
   743 
       
   744         if ( err == KErrNone )
       
   745             {
       
   746             iState = EMoveBusy;
       
   747             iHandlerAO->Start();
       
   748             }
       
   749         else
       
   750             {
       
   751             HTI_LOG_FORMAT( "Move failed %d", err );
       
   752             delete iHandlerAO;
       
   753             iHandlerAO = NULL;
       
   754             User::LeaveIfError( SendErrorMsg( err,
       
   755                                         KErrDescrFailedMoveFile ) );
       
   756             }
       
   757         }
       
   758     }
       
   759 
       
   760 void CHtiFtpServicePlugin::HandleCancelL(const TDesC8& aMessage)
       
   761     {
       
   762 
       
   763     if ( aMessage.Length()>0 )
       
   764         {
       
   765         HTI_LOG_FORMAT("cmd %d", aMessage[0]);
       
   766 
       
   767         if ( aMessage[0] == EFtpCANCEL )
       
   768             {
       
   769             //handle cancel
       
   770             iFile.Close();
       
   771 
       
   772             if ( iState == EStorWait )
       
   773                 {
       
   774                 iFs.Delete(iFileName);
       
   775                 }
       
   776             else if ( iState == ERetrWait )
       
   777                 {
       
   778                 iDispatcher->RemoveMemoryObserver(this);
       
   779 
       
   780                 delete iSendBuffer;
       
   781                 iSendBuffer = NULL;
       
   782                 }
       
   783             //other states filtered out before
       
   784 
       
   785             delete iHandlerAO;
       
   786             iHandlerAO = NULL;
       
   787 
       
   788             iState = EIdle;
       
   789 
       
   790             User::LeaveIfError( SendControlMsg( EFtpOK, KNullDesC8) );
       
   791             }
       
   792         else
       
   793             {
       
   794             User::LeaveIfError( SendErrorMsg( KErrServerBusy,
       
   795                             KErrDescrBusy) );
       
   796             }
       
   797         }
       
   798     else
       
   799         {
       
   800         //send err
       
   801         User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   802                             KErrDescrUnknownCmd ) );
       
   803         }
       
   804     }
       
   805 
       
   806 void CHtiFtpServicePlugin::CopyUnicode( TDes& aTo, const TDesC8& aFrom )
       
   807 {
       
   808     HTI_LOG_FUNC_IN("CHtiFtpServicePlugin::CopyUnicode");
       
   809     TInt len = aFrom.Length()>>1;
       
   810     aTo.SetLength( len );
       
   811     for ( TInt i = 0; i < len; ++i )
       
   812     {
       
   813         aTo[i] = (TUint16)aFrom[i<<1] + (((TUint16)aFrom[(i<<1)+1])<<8);
       
   814     }
       
   815     HTI_LOG_FUNC_OUT("CHtiFtpServicePlugin::CopyUnicode");
       
   816 }
       
   817 
       
   818 TBool CHtiFtpServicePlugin::GetFileNameL( const TDesC8& aFilename,
       
   819                                           TBool aToUnicode )
       
   820     {
       
   821     HTI_LOG_FUNC_IN("CHtiFtpServicePlugin::GetFileNameL");
       
   822     if ( aFilename.Length() > 1 )
       
   823         {
       
   824         TInt length = aFilename[0];
       
   825         TInt size = aToUnicode ? ( length * 2 ) : length;
       
   826 
       
   827         if ( ( size + 1 ) == aFilename.Size() )
       
   828             {
       
   829             if ( aToUnicode )
       
   830                 {
       
   831                 //const TPtrC8 ptr = aFilename.Mid(1).Ptr();
       
   832                 //iFileName.Copy( (TUint16*)ptr, len );
       
   833                 CopyUnicode( iFileName, aFilename.Mid( 1 ) );
       
   834                 }
       
   835             else
       
   836                 {
       
   837                 iFileName.Copy( aFilename.Mid( 1, length ) );
       
   838                 }
       
   839 
       
   840             HTI_LOG_TEXT( "filename:" );
       
   841             HTI_LOG_DES( iFileName );
       
   842             return ETrue;
       
   843             }
       
   844         else
       
   845             {
       
   846             User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   847                                               KErrDescrInvalidFilenameLength ) );
       
   848             }
       
   849         }
       
   850     else
       
   851         {
       
   852         User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   853                                           KErrDescrEmptyFilename ) );
       
   854         }
       
   855 
       
   856     HTI_LOG_FUNC_OUT("CHtiFtpServicePlugin::GetFileNameL");
       
   857     return EFalse;
       
   858     }
       
   859 
       
   860 TBool CHtiFtpServicePlugin::GetDirectoryL( const TDesC8& aDirname,
       
   861                                            TBool aToUnicode )
       
   862     {
       
   863     HTI_LOG_FUNC_IN("CHtiFtpServicePlugin::GetDirectoryL");
       
   864     if ( aDirname.Length() > 1 )
       
   865         {
       
   866         TInt len = aDirname[0];
       
   867         TInt size = aToUnicode ? ( len * 2 ) : len;
       
   868         if ( ( size + 1 ) == aDirname.Size() )
       
   869             {
       
   870             if ( aToUnicode )
       
   871                 {
       
   872                 //const TUint8* ptr = aDirname.Mid(1).Ptr();
       
   873                 //iFileName.Copy( (TUint16*)ptr, len );
       
   874                 CopyUnicode( iFileName, aDirname.Mid(1) );
       
   875                 }
       
   876             else
       
   877                 {
       
   878                 iFileName.Copy( aDirname.Mid( 1, len ) );
       
   879                 }
       
   880 
       
   881             HTI_LOG_TEXT( "dir:" );
       
   882             HTI_LOG_DES( iFileName );
       
   883             if ( iFileName.Right( 1 ) != KBackslash )
       
   884                 {
       
   885                 iFileName.Append( KBackslash );
       
   886                 }
       
   887             HTI_LOG_FUNC_OUT("CHtiFtpServicePlugin::GetDirectoryL");
       
   888             return ETrue;
       
   889             }
       
   890         else
       
   891             {
       
   892             User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   893                                               KErrDescrInvalidDirnameLength ) );
       
   894             }
       
   895         }
       
   896     else
       
   897         {
       
   898         User::LeaveIfError( SendErrorMsg( KErrArgument,
       
   899                                           KErrDescrEmptyDirname ) );
       
   900         }
       
   901 
       
   902     HTI_LOG_FUNC_OUT("CHtiFtpServicePlugin::GetDirectoryL");
       
   903     return EFalse;
       
   904     }
       
   905 
       
   906 void CHtiFtpServicePlugin::HandleListL( TBool aUnicodText,
       
   907                                         TUint aReadingAtt, TBool aSizes )
       
   908     {
       
   909     HTI_LOG_FUNC_IN("HandleListL");
       
   910     CDir* dir;
       
   911     TInt err = iFs.GetDir( iFileName, aReadingAtt, ESortNone, dir );
       
   912     if ( err != KErrNone )
       
   913         {
       
   914         User::LeaveIfError( SendErrorMsg( err, KErrDescrFailedGetDir ) );
       
   915         return;
       
   916         }
       
   917 
       
   918     CleanupStack::PushL( dir );
       
   919     //build list
       
   920     delete iSendBuffer;
       
   921     iSendBuffer = NULL;
       
   922     TInt bufferLen = dir->Count()*KMaxFileName;
       
   923     if ( aUnicodText )
       
   924         {
       
   925         bufferLen *= 2;
       
   926         }
       
   927     bufferLen += dir->Count();
       
   928     if ( aSizes )
       
   929         {
       
   930         bufferLen += 4 * dir->Count();
       
   931         }
       
   932 
       
   933     iSendBuffer = HBufC8::NewL( bufferLen );
       
   934     TInt dirNameLen = 0;
       
   935     for ( TInt i = 0; i < dir->Count(); ++i)
       
   936         {
       
   937         dirNameLen = (*dir)[i].iName.Length();
       
   938         iSendBuffer->Des().Append( dirNameLen );
       
   939         if ( aUnicodText )
       
   940             {
       
   941             iSendBuffer->Des().Append( (TUint8*)((*dir)[i].iName.Ptr()),
       
   942                                        dirNameLen*2 );
       
   943             }
       
   944         else
       
   945             {
       
   946             iSendBuffer->Des().Append( (*dir)[i].iName );
       
   947             }
       
   948         if ( aSizes )
       
   949             {
       
   950             TInt size = (*dir)[i].iSize;
       
   951             iSendBuffer->Des().Append( (TUint8*)(&size), 4 );
       
   952             }
       
   953         }
       
   954 
       
   955     err = iDispatcher->DispatchOutgoingMessage(iSendBuffer,
       
   956                         KFtpServiceUid,
       
   957                         EFalse,
       
   958                         EHtiPriorityControl);
       
   959 
       
   960     if (  err != KErrNone )
       
   961         {
       
   962         //wait for a memory
       
   963         iState = EListBusy;
       
   964         iDispatcher->AddMemoryObserver( this );
       
   965         }
       
   966     else
       
   967         {
       
   968         iSendBuffer = NULL;
       
   969         }
       
   970 
       
   971     CleanupStack::PopAndDestroy();//dir
       
   972     HTI_LOG_FUNC_OUT("HandleListL");
       
   973     }
       
   974 
       
   975 void CHtiFtpServicePlugin::HandleDataMessageL( const TDesC8& aMessage )
       
   976     {
       
   977     switch ( iState )
       
   978         {
       
   979         case EStorWait:
       
   980             {
       
   981             iState = EStorBusy;
       
   982             iCurrentOffset += aMessage.Size();
       
   983 
       
   984             TInt anError = iFile.Write( aMessage );
       
   985             if ( anError == KErrNone  )
       
   986                 {
       
   987                 HTI_LOG_FORMAT("received %d", iCurrentOffset);
       
   988                 if ( iCurrentOffset == iFileSize )
       
   989                     {
       
   990                     HTI_LOG_TEXT("receiving is over");
       
   991                     //receiving is over
       
   992                     TInt err = iFile.Flush();
       
   993                     iFile.Close();
       
   994                     iState = EIdle;
       
   995                     if ( err != KErrNone )
       
   996                         {
       
   997                         //err
       
   998                         HTI_LOG_TEXT("failed to close file");
       
   999                         iFs.Delete(iFileName);
       
  1000                         SendErrorMsg( err, KErrDescrFailedCloseFile );
       
  1001                         }
       
  1002                     else
       
  1003                         {
       
  1004                         //if file should be copied to TCB
       
  1005                         //copy it to to TCB from temp location
       
  1006                         if ( IsFileTcb( iFileName ) )
       
  1007                             {
       
  1008                             HandleTcbCopyL( KTmpFileName, iFileName );
       
  1009                             }
       
  1010                         else
       
  1011                             {
       
  1012                             SetBURFakeState( EFalse );
       
  1013                             SendControlMsg( EFtpOK, KNullDesC8 );
       
  1014                             }
       
  1015                         }
       
  1016                     }
       
  1017                 else
       
  1018                     {
       
  1019                     iState = EStorWait;
       
  1020                     }
       
  1021                 }
       
  1022             else
       
  1023                 {
       
  1024                 HTI_LOG_FORMAT("error writing file %d", anError);
       
  1025                 //abort operation and send err msg
       
  1026 
       
  1027                 iFile.Close();
       
  1028                 iFs.Delete(iFileName);
       
  1029                 iState = EIdle;
       
  1030                 SendErrorMsg( anError, KErrDescrFailedWrite );
       
  1031                 }
       
  1032             }
       
  1033             break;
       
  1034         default:
       
  1035             //do nothing
       
  1036             User::LeaveIfError( SendErrorMsg( KErrArgument,
       
  1037                                               KErrDescrInvalidDataMessage ) );
       
  1038             break;
       
  1039         }
       
  1040     }
       
  1041 
       
  1042 void CHtiFtpServicePlugin::HandleReceiveFileL()
       
  1043     {
       
  1044     HTI_LOG_FUNC_IN("HandleReceiveFileL");
       
  1045 
       
  1046     // create file
       
  1047     TInt err = KErrNone;
       
  1048 
       
  1049     //if file should be copied to TCB
       
  1050     //first copy it to temp location
       
  1051     if ( IsFileTcb( iFileName ) )
       
  1052         {
       
  1053         err = iFile.Replace( iFs, KTmpFileName, EFileWrite );
       
  1054         }
       
  1055         else
       
  1056         {
       
  1057         err = iFile.Replace(
       
  1058             iFs, iFileName, EFileWrite | EFileShareExclusive );
       
  1059         }
       
  1060 
       
  1061     if ( err != KErrNone )
       
  1062         {
       
  1063         err = iFile.Replace(
       
  1064             iFs, iFileName, EFileWrite | EFileShareAny );
       
  1065         }
       
  1066 
       
  1067     if ( err != KErrNone )
       
  1068         {
       
  1069         err = iFile.Replace(
       
  1070             iFs, iFileName, EFileWrite | EFileShareReadersOrWriters );
       
  1071         }
       
  1072 
       
  1073     if ( err != KErrNone )
       
  1074         {
       
  1075         if ( SetBURFakeState( ETrue ) == KErrNone )
       
  1076             {
       
  1077             err = iFile.Replace(
       
  1078                 iFs, iFileName, EFileWrite | EFileShareExclusive );
       
  1079             }
       
  1080         }
       
  1081 
       
  1082     if ( err != KErrNone )
       
  1083         {
       
  1084         HTI_LOG_TEXT("failed create file");
       
  1085         SendErrorMsg( err, KErrDescrFailedCreateFile );
       
  1086         return;
       
  1087         }
       
  1088 
       
  1089     //check that there is enough disk space
       
  1090     err = iFile.SetSize( iFileSize );
       
  1091     if ( err != KErrNone )
       
  1092         {
       
  1093         HTI_LOG_TEXT("not enough space");
       
  1094         SendErrorMsg( err, KErrDescrNoSpace );
       
  1095         iFile.Close();
       
  1096         iFs.Delete( iFileName );
       
  1097         return;
       
  1098         }
       
  1099 
       
  1100     // init receiving byte counter
       
  1101     iCurrentOffset = 0;
       
  1102     iState = EStorWait;
       
  1103 
       
  1104     //send ok and
       
  1105     User::LeaveIfError( SendControlMsg( EFtpOK, KNullDesC8 ) );
       
  1106     //...wait for data messages
       
  1107     HTI_LOG_FUNC_OUT("HandleReceiveFileL");
       
  1108     }
       
  1109 
       
  1110 void CHtiFtpServicePlugin::HandleSendFileL()
       
  1111     {
       
  1112     //open it
       
  1113     TInt err = iFile.Open( iFs, iFileName, EFileRead | EFileShareAny );
       
  1114 
       
  1115     if ( err != KErrNone )
       
  1116         {
       
  1117         err = iFile.Open( iFs, iFileName, EFileRead | EFileShareReadersOnly );
       
  1118         }
       
  1119 
       
  1120     if ( err != KErrNone )
       
  1121         {
       
  1122         err = iFile.Open( iFs, iFileName, EFileRead | EFileShareReadersOrWriters );
       
  1123         }
       
  1124 
       
  1125     if ( err != KErrNone )
       
  1126         {
       
  1127         if ( SetBURFakeState( ETrue ) == KErrNone )
       
  1128             {
       
  1129             err = iFile.Open(
       
  1130                 iFs, iFileName, EFileRead | EFileShareReadersOnly );
       
  1131             }
       
  1132         }
       
  1133 
       
  1134     if ( err != KErrNone )
       
  1135         {
       
  1136         HTI_LOG_FORMAT("failed open file %d", err);
       
  1137         SendErrorMsg( err, KErrDescrFailedOpenFile );
       
  1138         return;
       
  1139         }
       
  1140 
       
  1141     //send file size
       
  1142     err = iFile.Size( iFileSize );
       
  1143     if ( err != KErrNone )
       
  1144         {
       
  1145         HTI_LOG_TEXT("failed get filesize");
       
  1146         User::LeaveIfError( SendErrorMsg( err, KErrDescrFailedOpenFile ) );
       
  1147         }
       
  1148 
       
  1149     TBuf8<KFileSizeMsgSize> fileSizeMsg;
       
  1150     fileSizeMsg.Append( (TUint8*)(&iFileSize), 4 );
       
  1151     User::LeaveIfError( SendControlMsg( EFtpFILESIZE, fileSizeMsg ) );
       
  1152 
       
  1153     iCurrentOffset = 0;
       
  1154     //start sending
       
  1155     //always observe when sending
       
  1156     iDispatcher->AddMemoryObserver( this );
       
  1157 
       
  1158     //create handler for following RFile::Write
       
  1159     delete iHandlerAO;
       
  1160     iHandlerAO = NULL;
       
  1161     iHandlerAO = new(ELeave) CFtpHandlerAO( this );
       
  1162 
       
  1163     //start operation
       
  1164     ReadToBuffer();
       
  1165     }
       
  1166 
       
  1167 void CHtiFtpServicePlugin::ReadToBuffer()
       
  1168     {
       
  1169     HTI_LOG_FUNC_IN("ReadToBuffer");
       
  1170     //check that iHandlerAO valid
       
  1171     if ( iHandlerAO )
       
  1172         {
       
  1173         if ( !iHandlerAO->IsActive() )
       
  1174             {
       
  1175             //dispatch messages in the outgoing queue until run out of memory
       
  1176             TInt err;
       
  1177             //allocate memory and read from file
       
  1178             delete iSendBuffer;
       
  1179             iSendBuffer = NULL;
       
  1180 
       
  1181             TRAP( err, iSendBuffer = HBufC8::NewL( iBufferSize ) );
       
  1182             if ( err == KErrNone )
       
  1183                 {
       
  1184                 HTI_LOG_TEXT("read file");
       
  1185                 iState = ERetrBusy;
       
  1186                 iSendBufferDes.Set( iSendBuffer->Des() );
       
  1187                 iFile.Read( iSendBufferDes, iHandlerAO->iStatus  );
       
  1188                 iHandlerAO->Start();
       
  1189                 }
       
  1190             else
       
  1191                 {
       
  1192                 //go to idle state
       
  1193                 HTI_LOG_TEXT("impossible to alloc mem");
       
  1194                 iDispatcher->RemoveMemoryObserver( this );
       
  1195 
       
  1196                 delete iHandlerAO;
       
  1197                 iHandlerAO = NULL;
       
  1198 
       
  1199                 iFile.Close();
       
  1200                 iState = EIdle;
       
  1201                 //try to send err message
       
  1202                 SendErrorMsg( KErrNoMemory, KErrDescrNoMemory );
       
  1203                 }
       
  1204             }
       
  1205         else
       
  1206             {
       
  1207             //error, ignore
       
  1208             HTI_LOG_TEXT("ERROR: Call ReadBuffer with active handler");
       
  1209             }
       
  1210         }
       
  1211     else
       
  1212         {
       
  1213         //error
       
  1214         HTI_LOG_TEXT("ERROR: Call ReadBuffer with no handler");
       
  1215         //go to idle
       
  1216         iState = EIdle;
       
  1217         }
       
  1218     HTI_LOG_FUNC_OUT("ReadToBuffer");
       
  1219     }
       
  1220 
       
  1221 void CHtiFtpServicePlugin::SendBuffer()
       
  1222     {
       
  1223     HTI_LOG_FUNC_IN("SendBuffer");
       
  1224     if ( iSendBuffer )
       
  1225         {
       
  1226         HTI_LOG_FORMAT("iCurrentOffset %d bytes", iCurrentOffset);
       
  1227         //send buffer
       
  1228         TInt err = iDispatcher->DispatchOutgoingMessage( iSendBuffer,
       
  1229                         KFtpServiceUid,
       
  1230                         EFalse,
       
  1231                         EHtiPriorityData);
       
  1232 
       
  1233         if ( err == KErrNone )
       
  1234             {
       
  1235             HTI_LOG_TEXT("message was dispatched");
       
  1236             //message was dispatched
       
  1237             iSendBuffer = NULL;
       
  1238             //check do we have to continue
       
  1239             if ( iCurrentOffset >= iFileSize )
       
  1240                 {
       
  1241                 HTI_LOG_TEXT( "over sending" );
       
  1242 
       
  1243                 delete iHandlerAO;
       
  1244                 iHandlerAO = NULL;
       
  1245 
       
  1246                 iFile.Close();
       
  1247                 iState = EIdle;
       
  1248 
       
  1249                 iDispatcher->RemoveMemoryObserver( this );
       
  1250                 SetBURFakeState( EFalse );
       
  1251                 }
       
  1252             else
       
  1253                 {
       
  1254                 //read and send next package
       
  1255                 ReadToBuffer();
       
  1256                 }
       
  1257             }
       
  1258         else if ( err == KErrNoMemory )
       
  1259             {
       
  1260             //wait for memory
       
  1261             //observer should be active
       
  1262             iState = ERetrWait;
       
  1263             }
       
  1264         else if ( err != KErrNone )
       
  1265             {
       
  1266             HTI_LOG_FORMAT("error dispatching outgoing message %d", err );
       
  1267             //some err, abort operation
       
  1268             delete iSendBuffer;
       
  1269             iSendBuffer = NULL;
       
  1270 
       
  1271             delete iHandlerAO;
       
  1272             iHandlerAO = NULL;
       
  1273 
       
  1274             iFile.Close();
       
  1275 
       
  1276             iState = EIdle;
       
  1277 
       
  1278             iDispatcher->RemoveMemoryObserver( this );
       
  1279             SetBURFakeState( EFalse );
       
  1280             }
       
  1281         }
       
  1282     else
       
  1283         {
       
  1284         //really weird error, go to idle
       
  1285         HTI_LOG_TEXT("ERROR: SendBuffer with empty iSendBuffer");
       
  1286         iState = EIdle;
       
  1287         SetBURFakeState( EFalse );
       
  1288         }
       
  1289     HTI_LOG_FUNC_OUT("SendBuffer");
       
  1290     }
       
  1291 
       
  1292 void CHtiFtpServicePlugin::FtpComplete( TInt anError)
       
  1293     {
       
  1294     HTI_LOG_FUNC_IN("FtpComplete");
       
  1295     HTI_LOG_FORMAT("error %d", anError);
       
  1296 
       
  1297     //NOTE: can't leave from here
       
  1298     switch ( iState )
       
  1299         {
       
  1300         case ERmdBusy:
       
  1301             {
       
  1302             //send OK message
       
  1303             if ( anError == KErrNone )
       
  1304                 {
       
  1305                 SendControlMsg( EFtpOK, KNullDesC8 );
       
  1306                 }
       
  1307             else
       
  1308                 {
       
  1309                 SendErrorMsg( anError, KErrDescrFailedRmDir );
       
  1310                 }
       
  1311             delete iHandlerAO;
       
  1312             iHandlerAO = NULL;
       
  1313 
       
  1314             iState = EIdle;
       
  1315             }
       
  1316             break;
       
  1317         case EDeleBusy:
       
  1318             {
       
  1319             //send OK message
       
  1320             if ( anError == KErrNone )
       
  1321                 {
       
  1322                 SetBURFakeState( EFalse );
       
  1323                 SendControlMsg( EFtpOK, KNullDesC8 );
       
  1324                 }
       
  1325             else
       
  1326                 {
       
  1327                 if ( SetBURFakeState( ETrue ) != KErrNone )
       
  1328                     {
       
  1329                     // Force was not requested, not supported OR already tried.
       
  1330                     SendErrorMsg( anError, KErrDescrFailedDeleFile );
       
  1331                     }
       
  1332                 else
       
  1333                     {
       
  1334                     // try delete again
       
  1335                     TRAPD( err, HandleDeleteL( iFileName ) );
       
  1336                     if ( err == KErrNone )
       
  1337                         {
       
  1338                         break;
       
  1339                         }
       
  1340                     else
       
  1341                         {
       
  1342                         SendErrorMsg( err, KErrDescrFailedDeleFile );
       
  1343                         }
       
  1344                     }
       
  1345                 }
       
  1346             delete iHandlerAO;
       
  1347             iHandlerAO = NULL;
       
  1348 
       
  1349             iState = EIdle;
       
  1350             }
       
  1351             break;
       
  1352         case ERenameBusy:
       
  1353             {
       
  1354             //send OK message
       
  1355             if ( anError == KErrNone )
       
  1356                 {
       
  1357                 SetBURFakeState( EFalse );
       
  1358                 SendControlMsg( EFtpOK, KNullDesC8 );
       
  1359                 }
       
  1360             else
       
  1361                 {
       
  1362                 SendErrorMsg( anError, KErrDescrFailedRenameFile );
       
  1363                 }
       
  1364 
       
  1365             delete iHandlerAO;
       
  1366             iHandlerAO = NULL;
       
  1367 
       
  1368             iState = EIdle;
       
  1369             }
       
  1370             break;
       
  1371         case ECopyBusy:
       
  1372             {
       
  1373             //send OK message
       
  1374             if ( anError == KErrNone )
       
  1375                 {
       
  1376                 SetBURFakeState( EFalse );
       
  1377                 SendControlMsg( EFtpOK, KNullDesC8 );
       
  1378                 }
       
  1379             else
       
  1380                 {
       
  1381                 SendErrorMsg( anError, KErrDescrFailedCopyFile );
       
  1382                 }
       
  1383 
       
  1384             delete iHandlerAO;
       
  1385             iHandlerAO = NULL;
       
  1386 
       
  1387             iState = EIdle;
       
  1388             }
       
  1389             break;
       
  1390         case EMoveBusy:
       
  1391             {
       
  1392             //send OK message
       
  1393             if ( anError == KErrNone )
       
  1394                 {
       
  1395                 SetBURFakeState( EFalse );
       
  1396                 SendControlMsg( EFtpOK, KNullDesC8 );
       
  1397                 }
       
  1398             else
       
  1399                 {
       
  1400                 SendErrorMsg( anError, KErrDescrFailedMoveFile );
       
  1401                 }
       
  1402 
       
  1403             delete iHandlerAO;
       
  1404             iHandlerAO = NULL;
       
  1405 
       
  1406             iState = EIdle;
       
  1407             }
       
  1408             break;
       
  1409         case EMoveTcbBusy:
       
  1410             {
       
  1411             //send OK message
       
  1412             if ( anError == KErrNone )
       
  1413                 {
       
  1414                 SetBURFakeState( EFalse );
       
  1415                 SendControlMsg( EFtpOK, KNullDesC8 );
       
  1416                 }
       
  1417             else
       
  1418                 {
       
  1419                 SendErrorMsg( anError, KErrDescrFailedMoveFile );
       
  1420                 }
       
  1421 
       
  1422             delete iProcessLogonAO;
       
  1423             iProcessLogonAO = NULL;
       
  1424 
       
  1425             iState = EIdle;
       
  1426             }
       
  1427             break;
       
  1428         case ERenameTcbBusy:
       
  1429             {
       
  1430             //send OK message
       
  1431             if ( anError == KErrNone )
       
  1432                 {
       
  1433                 SetBURFakeState( EFalse );
       
  1434                 SendControlMsg( EFtpOK, KNullDesC8 );
       
  1435                 }
       
  1436             else
       
  1437                 {
       
  1438                 SendErrorMsg( anError, KErrDescrFailedRenameFile );
       
  1439                 }
       
  1440 
       
  1441             delete iProcessLogonAO;
       
  1442             iProcessLogonAO = NULL;
       
  1443 
       
  1444             iState = EIdle;
       
  1445             }
       
  1446             break;
       
  1447         case EDeleTcbBusy:
       
  1448             {
       
  1449             //send OK message
       
  1450             if ( anError == KErrNone )
       
  1451                 {
       
  1452                 SetBURFakeState( EFalse );
       
  1453                 SendControlMsg( EFtpOK, KNullDesC8 );
       
  1454                 }
       
  1455             else
       
  1456                 {
       
  1457                 if ( SetBURFakeState( ETrue ) != KErrNone )
       
  1458                     {
       
  1459                     // Force was not requested, not supported OR already tried.
       
  1460                     SendErrorMsg( anError, KErrDescrFailedDeleFile );
       
  1461                     }
       
  1462                 else
       
  1463                     {
       
  1464                      // try delete again
       
  1465                     TRAPD( err, HandleTcbDeleteL( iFileName ) );
       
  1466                     if ( err == KErrNone )
       
  1467                         {
       
  1468                         break;
       
  1469                         }
       
  1470                     else
       
  1471                         {
       
  1472                         SendErrorMsg( err, KErrDescrFailedDeleFile );
       
  1473                         }
       
  1474                     }
       
  1475                 }
       
  1476             delete iProcessLogonAO;
       
  1477             iProcessLogonAO = NULL;
       
  1478 
       
  1479             iState = EIdle;
       
  1480             }
       
  1481             break;
       
  1482         case EMkdTcbBusy:
       
  1483             {
       
  1484             if ( anError == KErrNone || anError == KErrAlreadyExists )
       
  1485                 {
       
  1486                 SendControlMsg( EFtpOK, KNullDesC8);
       
  1487                 }
       
  1488             else
       
  1489                 {
       
  1490                 SendErrorMsg( anError, KErrDescrFailedMkDir );
       
  1491                 }
       
  1492             delete iProcessLogonAO;
       
  1493             iProcessLogonAO = NULL;
       
  1494 
       
  1495             iState = EIdle;
       
  1496 
       
  1497             }
       
  1498             break;
       
  1499         case ERmdTcbBusy:
       
  1500             {
       
  1501             if ( anError == KErrNone || anError == KErrAlreadyExists )
       
  1502                 {
       
  1503                 SendControlMsg( EFtpOK, KNullDesC8);
       
  1504                 }
       
  1505             else
       
  1506                 {
       
  1507                 SendErrorMsg( anError, KErrDescrFailedRmDir );
       
  1508                 }
       
  1509             delete iProcessLogonAO;
       
  1510             iProcessLogonAO = NULL;
       
  1511 
       
  1512             iState = EIdle;
       
  1513 
       
  1514             }
       
  1515             break;
       
  1516 
       
  1517         case EStorBusy:
       
  1518             {
       
  1519             if ( anError == KErrNone  )
       
  1520                 {
       
  1521                 HTI_LOG_FORMAT("received %d", iCurrentOffset);
       
  1522                 if ( iCurrentOffset == iFileSize )
       
  1523                     {
       
  1524                     HTI_LOG_TEXT("receiveing is over");
       
  1525                     //receiveing is over
       
  1526                     delete iHandlerAO;
       
  1527                     iHandlerAO = NULL;
       
  1528 
       
  1529                     iState = EIdle;
       
  1530 
       
  1531                     TInt err = iFile.Flush();
       
  1532                     iFile.Close();
       
  1533                     if ( err != KErrNone )
       
  1534                         {
       
  1535                         //err
       
  1536                         HTI_LOG_TEXT("failed to close file");
       
  1537                         iFs.Delete(iFileName);
       
  1538                         SendErrorMsg( err, KErrDescrFailedCloseFile );
       
  1539                         }
       
  1540                     else
       
  1541                         {
       
  1542                         //if file should be copied to TCB
       
  1543                         //copy it to to TCB from temp location
       
  1544                         if ( IsFileTcb( iFileName ) )
       
  1545                             {
       
  1546                             TRAP( err, HandleTcbCopyL(
       
  1547                                 KTmpFileName, iFileName ) );
       
  1548                             if ( err != KErrNone )
       
  1549                                 {
       
  1550                                 SendErrorMsg( anError, KErrDescrFailedCopyTcb );
       
  1551                                 iFs.Delete( KTmpFileName );
       
  1552                                 delete iProcessLogonAO;
       
  1553                                 iProcessLogonAO = NULL;
       
  1554                                 iState = EIdle;
       
  1555                                 }
       
  1556                             }
       
  1557                         else
       
  1558                             {
       
  1559                             SetBURFakeState( EFalse );
       
  1560                             SendControlMsg( EFtpOK, KNullDesC8 );
       
  1561                             }
       
  1562                         }
       
  1563                     }
       
  1564                 else
       
  1565                     {
       
  1566                     iState = EStorWait;
       
  1567                     //busy state is over
       
  1568                     iDispatcher->Notify(KErrNone);
       
  1569                     }
       
  1570                 }
       
  1571             else
       
  1572                 {
       
  1573                 HTI_LOG_FORMAT("error writing file %d", anError);
       
  1574                 //abort operation and send err msg
       
  1575                 delete iHandlerAO;
       
  1576                 iHandlerAO = NULL;
       
  1577 
       
  1578                 iFile.Close();
       
  1579                 iFs.Delete(iFileName);
       
  1580                 iState = EIdle;
       
  1581                 SendErrorMsg( anError, KErrDescrFailedWrite );
       
  1582                 }
       
  1583             }
       
  1584             break;
       
  1585 
       
  1586         case EStorTcbBusy:
       
  1587             {
       
  1588             if ( anError == KErrNone )
       
  1589                 {
       
  1590                 SetBURFakeState( EFalse );
       
  1591                 SendControlMsg( EFtpOK, KNullDesC8 );
       
  1592                 }
       
  1593             else
       
  1594                 {
       
  1595                 if ( SetBURFakeState( ETrue ) != KErrNone )
       
  1596                     {
       
  1597                     // Force was not requested, not supported OR already tried.
       
  1598                     HTI_LOG_FORMAT("error copy to tcb %d", anError);
       
  1599                     //abort operation and send err msg
       
  1600                     SendErrorMsg( anError, KErrDescrFailedCopyTcb );
       
  1601                     }
       
  1602                 else
       
  1603                     {
       
  1604                     // try copy again
       
  1605                     TRAPD( err, HandleTcbCopyL( KTmpFileName, iFileName ) );
       
  1606                     if ( err == KErrNone )
       
  1607                         {
       
  1608                         break;
       
  1609                         // don't continue, this method will be called again
       
  1610                         // by the AO after copying is tried
       
  1611                         }
       
  1612                     else
       
  1613                         {
       
  1614                         SendErrorMsg( err, KErrDescrFailedCopyTcb );
       
  1615                         }
       
  1616                     }
       
  1617                 }
       
  1618 
       
  1619             //delete temp file
       
  1620             HTI_LOG_TEXT("delete tmp file");
       
  1621             TInt err = iFs.Delete(KTmpFileName);
       
  1622             if ( err != KErrNone )
       
  1623                 {
       
  1624                 HTI_LOG_FORMAT("error delete tmp file %d", err);
       
  1625                 }
       
  1626 
       
  1627             delete iProcessLogonAO;
       
  1628             iProcessLogonAO = NULL;
       
  1629             iState = EIdle;
       
  1630             }
       
  1631             break;
       
  1632 
       
  1633         case ERetrBusy:
       
  1634             {
       
  1635             if ( anError == KErrNone )
       
  1636                 {
       
  1637                 HTI_LOG_FORMAT("read %d bytes", iSendBuffer->Size());
       
  1638                 iCurrentOffset += iSendBuffer->Size();
       
  1639 
       
  1640                 SendBuffer();
       
  1641                 }
       
  1642             else
       
  1643                 {
       
  1644                 HTI_LOG_FORMAT("failed read file %d", anError);
       
  1645                 //error reading file
       
  1646                 //abort operation and send err msg
       
  1647                 delete iSendBuffer;
       
  1648                 iSendBuffer = NULL;
       
  1649 
       
  1650                 delete iHandlerAO;
       
  1651                 iHandlerAO = NULL;
       
  1652 
       
  1653                 iFile.Close();
       
  1654 
       
  1655                 iState = EIdle;
       
  1656                 iDispatcher->RemoveMemoryObserver( this );
       
  1657 
       
  1658                 SendErrorMsg( anError, KErrDescrFailedRead );
       
  1659                 }
       
  1660             }
       
  1661             break;
       
  1662         default:
       
  1663             {
       
  1664             //some error, should not be called
       
  1665             HTI_LOG_TEXT("invalid state for FtpComplete");
       
  1666             }
       
  1667         }
       
  1668     HTI_LOG_FUNC_OUT("FtpComplete");
       
  1669     }
       
  1670 
       
  1671 void CHtiFtpServicePlugin::NotifyMemoryChange( TInt aAvailableMemory )
       
  1672     {
       
  1673     HTI_LOG_FUNC_IN("NotifyMemoryChange");
       
  1674     switch ( iState )
       
  1675         {
       
  1676         case ERetrWait:
       
  1677             {
       
  1678             if ( iSendBuffer )
       
  1679                 {
       
  1680                 if ( aAvailableMemory >= iSendBuffer->Size() )
       
  1681                     {
       
  1682                     //continue sending
       
  1683                     SendBuffer();
       
  1684                     }
       
  1685                 }
       
  1686             else
       
  1687                 {//impossible
       
  1688                 //nothing to send
       
  1689                 //just reset
       
  1690                 HTI_LOG_TEXT("invalid state for mem");
       
  1691 
       
  1692                 delete iHandlerAO;
       
  1693                 iHandlerAO = NULL;
       
  1694                 iFile.Close();
       
  1695                 iDispatcher->RemoveMemoryObserver(this);
       
  1696                 iState = EIdle;
       
  1697                 }
       
  1698             }
       
  1699             break;
       
  1700         case EListBusy:
       
  1701             {
       
  1702             if ( iSendBuffer )
       
  1703                 {
       
  1704                 if ( aAvailableMemory >= iSendBuffer->Size() )
       
  1705                     {
       
  1706                     //send buffer
       
  1707                     TInt err = iDispatcher->DispatchOutgoingMessage(
       
  1708                                     iSendBuffer,
       
  1709                                     KFtpServiceUid,
       
  1710                                     EFalse,
       
  1711                                     EHtiPriorityData);
       
  1712                     if ( err != KErrNone )
       
  1713                         {
       
  1714                         //error, reset
       
  1715                         HTI_LOG_TEXT("failed to send LIST");
       
  1716                         delete iSendBuffer;
       
  1717                         }
       
  1718                     iSendBuffer = NULL;
       
  1719                     iDispatcher->RemoveMemoryObserver(this);
       
  1720                     iState = EIdle;
       
  1721                     }
       
  1722                 }
       
  1723             else
       
  1724                 {//impossible
       
  1725                 //nothing to send
       
  1726                 //just reset
       
  1727                 HTI_LOG_TEXT("invalid state for mem");
       
  1728                 iDispatcher->RemoveMemoryObserver(this);
       
  1729                 iState = EIdle;
       
  1730                 }
       
  1731             }
       
  1732             break;
       
  1733         default:
       
  1734             //some error, should not be called
       
  1735             HTI_LOG_TEXT("invalid state for mem");
       
  1736             //iDispatcher->RemoveMemoryObserver(this);
       
  1737             //iState = EIdle;
       
  1738         }
       
  1739     HTI_LOG_FUNC_OUT("NotifyMemoryChange");
       
  1740     }
       
  1741 
       
  1742 TBool CHtiFtpServicePlugin::IsFileTcb( const TDesC& aFilename )
       
  1743     {
       
  1744     return aFilename.FindF( KHtiTcbSys ) == KPathOffset ||
       
  1745            aFilename.FindF( KHtiTcbResource ) == KPathOffset;
       
  1746     }
       
  1747 
       
  1748 void CHtiFtpServicePlugin::HandleTcbDeleteL( const TDesC& aFilename )
       
  1749     {
       
  1750     HTI_LOG_FUNC_IN("HandleTcbDeleteL");
       
  1751     HBufC* cmdLine = HBufC::NewLC( aFilename.Length() + 2 );
       
  1752 
       
  1753     //make command line "d|filename"
       
  1754     cmdLine->Des().Copy( KHtiFileHlpDeleteCmd );
       
  1755     cmdLine->Des().Append( KHtiFileHlpDelim );
       
  1756     cmdLine->Des().Append( aFilename );
       
  1757 
       
  1758     HTI_LOG_DES( *cmdLine );
       
  1759 
       
  1760     delete iProcessLogonAO;
       
  1761     iProcessLogonAO = NULL;
       
  1762 
       
  1763     iProcessLogonAO = new(ELeave) CProcessLogonAO( this );
       
  1764     iProcessLogonAO->Start( *cmdLine );
       
  1765 
       
  1766     iState = EDeleTcbBusy;
       
  1767 
       
  1768     CleanupStack::PopAndDestroy( cmdLine );
       
  1769     HTI_LOG_FUNC_OUT("HandleTcbDeleteL");
       
  1770     }
       
  1771 
       
  1772 void CHtiFtpServicePlugin::HandleTcbCopyL( const TDesC& aFromFilename,
       
  1773                                            const TDesC& aToFilename )
       
  1774     {
       
  1775     HTI_LOG_FUNC_IN("HandleTcbCopyL");
       
  1776     HBufC* cmdLine = HBufC::NewLC( aFromFilename.Length() + aToFilename.Length() + 3 );
       
  1777 
       
  1778     //make command line "c|fromfilename|tofilename"
       
  1779     cmdLine->Des().Copy( KHtiFileHlpCopyCmd );
       
  1780     cmdLine->Des().Append( KHtiFileHlpDelim );
       
  1781     cmdLine->Des().Append( aFromFilename );
       
  1782     cmdLine->Des().Append( KHtiFileHlpDelim );
       
  1783     cmdLine->Des().Append( aToFilename );
       
  1784 
       
  1785     HTI_LOG_DES( *cmdLine );
       
  1786 
       
  1787     delete iProcessLogonAO;
       
  1788     iProcessLogonAO = NULL;
       
  1789 
       
  1790     iProcessLogonAO = new(ELeave) CProcessLogonAO( this );
       
  1791     iProcessLogonAO->Start( *cmdLine );
       
  1792 
       
  1793     iState = EStorTcbBusy;
       
  1794 
       
  1795     CleanupStack::PopAndDestroy( cmdLine );
       
  1796     HTI_LOG_FUNC_OUT("HandleTcbCopyL");
       
  1797     }
       
  1798 
       
  1799 void CHtiFtpServicePlugin::HandleTcbMkdL( const TDesC& aDirname )
       
  1800     {
       
  1801     HTI_LOG_FUNC_IN("HandleTcbMkdL");
       
  1802     HBufC* cmdLine = HBufC::NewLC( aDirname.Length() + 2 );
       
  1803 
       
  1804     //make command line "m|dirname"
       
  1805     cmdLine->Des().Copy( KHtiFileHlpMkdCmd );
       
  1806     cmdLine->Des().Append( KHtiFileHlpDelim );
       
  1807     cmdLine->Des().Append( aDirname );
       
  1808 
       
  1809     HTI_LOG_DES( *cmdLine );
       
  1810 
       
  1811     delete iProcessLogonAO;
       
  1812     iProcessLogonAO = NULL;
       
  1813 
       
  1814     iProcessLogonAO = new(ELeave) CProcessLogonAO( this );
       
  1815     iProcessLogonAO->Start(*cmdLine);
       
  1816 
       
  1817     iState = EMkdTcbBusy;
       
  1818 
       
  1819     CleanupStack::PopAndDestroy(cmdLine);
       
  1820     HTI_LOG_FUNC_OUT("HandleTcbMkdL");
       
  1821     }
       
  1822 
       
  1823 void CHtiFtpServicePlugin::HandleTcbRmdL( const TDesC& aDirname )
       
  1824     {
       
  1825     HTI_LOG_FUNC_IN("HandleTcbRmdL");
       
  1826     HBufC* cmdLine = HBufC::NewLC( aDirname.Length() + 2 );
       
  1827 
       
  1828     //make command line "r|dirname"
       
  1829     cmdLine->Des().Copy( KHtiFileHlpRmdCmd );
       
  1830     cmdLine->Des().Append( KHtiFileHlpDelim );
       
  1831     cmdLine->Des().Append( aDirname );
       
  1832 
       
  1833     HTI_LOG_DES( *cmdLine );
       
  1834 
       
  1835     delete iProcessLogonAO;
       
  1836     iProcessLogonAO = NULL;
       
  1837 
       
  1838     iProcessLogonAO = new ( ELeave ) CProcessLogonAO( this );
       
  1839     iProcessLogonAO->Start( *cmdLine );
       
  1840 
       
  1841     iState = ERmdTcbBusy;
       
  1842 
       
  1843     CleanupStack::PopAndDestroy( cmdLine );
       
  1844     HTI_LOG_FUNC_OUT("HandleTcbRmdL");
       
  1845     }
       
  1846 
       
  1847 void CHtiFtpServicePlugin::HandleTcbRenameL(
       
  1848         const TDesC& aTargetName, const TDesC& aDestName )
       
  1849     {
       
  1850     HTI_LOG_FUNC_IN("HandleTcbRenameL");
       
  1851     HBufC* cmdLine = HBufC::NewLC( aTargetName.Length() + 4 + aDestName.Length());
       
  1852 
       
  1853     //make command line "r|dirname"
       
  1854     cmdLine->Des().Copy( KHtiFileHlpRenameCmd );
       
  1855     cmdLine->Des().Append( KHtiFileHlpDelim );
       
  1856     cmdLine->Des().Append( aTargetName );
       
  1857     cmdLine->Des().Append( KHtiFileHlpDelim );
       
  1858     cmdLine->Des().Append( aDestName );
       
  1859 
       
  1860     HTI_LOG_DES( *cmdLine );
       
  1861 
       
  1862     delete iProcessLogonAO;
       
  1863     iProcessLogonAO = NULL;
       
  1864 
       
  1865     iProcessLogonAO = new ( ELeave ) CProcessLogonAO( this );
       
  1866     iProcessLogonAO->Start( *cmdLine );
       
  1867 
       
  1868     iState = ERenameTcbBusy;
       
  1869 
       
  1870     CleanupStack::PopAndDestroy( cmdLine );
       
  1871     HTI_LOG_FUNC_OUT("HandleTcbRenameL");
       
  1872     }
       
  1873 
       
  1874 void CHtiFtpServicePlugin::HandleTcbMoveL(
       
  1875         const TDesC& aTargetName, const TDesC& aDestName )
       
  1876     {
       
  1877     HTI_LOG_FUNC_IN("HandleTcbRenameL");
       
  1878     HBufC* cmdLine = HBufC::NewLC( aTargetName.Length() + 4 + aDestName.Length());
       
  1879 
       
  1880     //make command line "r|dirname"
       
  1881     cmdLine->Des().Copy( KHtiFileHlpMoveCmd );
       
  1882     cmdLine->Des().Append( KHtiFileHlpDelim );
       
  1883     cmdLine->Des().Append( aTargetName );
       
  1884     cmdLine->Des().Append( KHtiFileHlpDelim );
       
  1885     cmdLine->Des().Append( aDestName );
       
  1886 
       
  1887     HTI_LOG_DES( *cmdLine );
       
  1888 
       
  1889     delete iProcessLogonAO;
       
  1890     iProcessLogonAO = NULL;
       
  1891 
       
  1892     iProcessLogonAO = new ( ELeave ) CProcessLogonAO( this );
       
  1893     iProcessLogonAO->Start( *cmdLine );
       
  1894 
       
  1895     iState = EMoveTcbBusy;
       
  1896 
       
  1897     CleanupStack::PopAndDestroy( cmdLine );
       
  1898     HTI_LOG_FUNC_OUT("HandleTcbRenameL");
       
  1899     }
       
  1900 
       
  1901 TInt CHtiFtpServicePlugin::SetBURFakeState( TBool aOn )
       
  1902     {
       
  1903     HTI_LOG_FUNC_IN( "CHtiFtpServicePlugin::SetBURFakeStateL" );
       
  1904     TInt err = KErrNone;
       
  1905 
       
  1906     if ( iBackupFake == NULL )
       
  1907         {
       
  1908         // Foreced operations not requested or not supported
       
  1909         err = KErrNotSupported;
       
  1910         }
       
  1911 
       
  1912     else if ( aOn )
       
  1913         {
       
  1914         HTI_LOG_TEXT( "Calling ActivateBackup()" );
       
  1915         err = iBackupFake->ActivateBackup();
       
  1916         }
       
  1917 
       
  1918     else
       
  1919         {
       
  1920         HTI_LOG_TEXT( "Calling DeactivateBackup()" );
       
  1921         err = iBackupFake->DeactivateBackup();
       
  1922         }
       
  1923 
       
  1924     HTI_LOG_FUNC_OUT( "CHtiFtpServicePlugin::SetBURFakeStateL" );
       
  1925     return err;
       
  1926     }
       
  1927 
       
  1928 void CHtiFtpServicePlugin::HandleCheckSumCalcL( TAlgorithm aAlgorithm,
       
  1929                                                 const TDesC& aFilename )
       
  1930     {
       
  1931     HTI_LOG_FUNC_IN( "CHtiFtpServicePlugin::HandleCheckSumCalcL" );
       
  1932 
       
  1933     RFile file;
       
  1934     TInt err = file.Open( iFs, aFilename, EFileRead | EFileShareReadersOnly );
       
  1935     if ( err )
       
  1936         {
       
  1937         HTI_LOG_FORMAT( "Error opening file, err: %d", err );
       
  1938         SendErrorMsg( err, KErrDescrFailedOpenFile );
       
  1939         return;
       
  1940         }
       
  1941 
       
  1942     CleanupClosePushL( file );
       
  1943     switch ( aAlgorithm )
       
  1944         {
       
  1945         case EMD5:
       
  1946             {
       
  1947             HTI_LOG_TEXT( "Using MD5 algorithm" );
       
  1948             const TInt KBufSize( 1024 );
       
  1949             TInt size = 0;
       
  1950             TInt offset = 0;
       
  1951 
       
  1952             file.Size( size );
       
  1953 
       
  1954             HBufC8* buf = HBufC8::NewMaxLC( KBufSize );
       
  1955             TPtr8 filePtr( buf->Des() );
       
  1956 
       
  1957             CMD5* md5 = CMD5::NewL();
       
  1958             CleanupStack::PushL( md5 );
       
  1959 
       
  1960             while ( offset < size - KBufSize )
       
  1961                 {
       
  1962                 User::LeaveIfError( file.Read( filePtr, KBufSize ) );
       
  1963                 md5->Hash( filePtr );
       
  1964                 offset += filePtr.Length();
       
  1965                 }
       
  1966 
       
  1967             file.Read( filePtr, size - offset );
       
  1968             filePtr.SetLength( size - offset );
       
  1969             md5->Hash( filePtr );
       
  1970 
       
  1971             filePtr.Set( NULL, 0, 0 );
       
  1972             HBufC8* result = md5->Hash( filePtr ).AllocL();
       
  1973             CleanupStack::PushL( result );
       
  1974 
       
  1975             HTI_LOG_TEXT( "Got following MD5:" );
       
  1976             HTI_LOG_DES( *result );
       
  1977 
       
  1978             User::LeaveIfError(
       
  1979                 iDispatcher->DispatchOutgoingMessage( result,
       
  1980                                                       KFtpServiceUid,
       
  1981                                                       EFalse,
       
  1982                                                       EHtiPriorityControl ) );
       
  1983             CleanupStack::Pop( result ); // do not delete, ownership transfered
       
  1984             CleanupStack::PopAndDestroy( 2 ); // md5, buf
       
  1985             break;
       
  1986             }
       
  1987 
       
  1988         default:
       
  1989             {
       
  1990             HTI_LOG_FORMAT( "Invalid algorithm: %d", aAlgorithm );
       
  1991             SendErrorMsg( KErrNotFound, KErrDescrInvalidChecksumArgs );
       
  1992             break;
       
  1993             }
       
  1994         }
       
  1995     CleanupStack::PopAndDestroy(); // file
       
  1996 
       
  1997     HTI_LOG_FUNC_OUT( "CHtiFtpServicePlugin::HandleCheckSumCalcL" );
       
  1998     }
       
  1999 
       
  2000 void CHtiFtpServicePlugin::HandleFormat( const TUint8 aDrive, const TUint8 aMode )
       
  2001     {
       
  2002     HTI_LOG_FUNC_IN( "CHtiFtpServicePlugin::HandleFormat" );
       
  2003 
       
  2004     // Convert the format mode: in HTI protocol 0 means full format and 1
       
  2005     // (or anything else currently) means quick format
       
  2006     TUint formatMode = 0;
       
  2007     if ( aMode )
       
  2008         formatMode = ESpecialFormat | EQuickFormat;
       
  2009     else
       
  2010         formatMode = ESpecialFormat | EFullFormat;
       
  2011 
       
  2012     // Create the drive name (drive letter and colon)
       
  2013     TDriveName drive;
       
  2014     drive.Append( aDrive );
       
  2015     drive.Append( _L( ":" ) );
       
  2016 
       
  2017     // Check that HTI is not running from the drive that is to be formatted
       
  2018     RProcess thisProcess;
       
  2019     if ( thisProcess.FileName().FindF( drive ) == 0 )
       
  2020         {
       
  2021         HTI_LOG_FORMAT( "HTI running from drive %S cannot format", &drive );
       
  2022         SendErrorMsg( KErrInUse, KErrDescrFailedFormat );
       
  2023         HTI_LOG_FUNC_OUT( "CHtiFtpServicePlugin::HandleFormat" );
       
  2024         return;
       
  2025         }
       
  2026 
       
  2027     // Activate backup/restore to close apps that might have files open.
       
  2028     // Ignore errors - try to format even if backup fake is not supported.
       
  2029     SetBURFakeState( ETrue );
       
  2030 
       
  2031     // Start the format
       
  2032     HTI_LOG_FORMAT( "Starting to format drive %S", &drive );
       
  2033     HTI_LOG_FORMAT( "Format mode = %d", formatMode );
       
  2034     TInt remainingTracks = 0;
       
  2035     RFormat format;
       
  2036     TInt err = format.Open( iFs, drive, formatMode, remainingTracks );
       
  2037     if ( err == KErrNone )
       
  2038         {
       
  2039         HTI_LOG_FORMAT( "Formatting %d tracks", remainingTracks );
       
  2040         while ( remainingTracks && err == KErrNone )
       
  2041             {
       
  2042             err = format.Next( remainingTracks );
       
  2043             HTI_LOG_FORMAT( "Tracks remaining: %d", remainingTracks );
       
  2044             }
       
  2045         }
       
  2046     format.Close();
       
  2047 
       
  2048     // Deactivate backup/restore - errors ignored.
       
  2049     SetBURFakeState( EFalse );
       
  2050 
       
  2051     if ( err == KErrNone )
       
  2052         {
       
  2053         SendControlMsg( EFtpOK, KNullDesC8 );
       
  2054         }
       
  2055 
       
  2056     else
       
  2057         {
       
  2058         SendErrorMsg( err, KErrDescrFailedFormat );
       
  2059         }
       
  2060 
       
  2061     HTI_LOG_FUNC_OUT( "CHtiFtpServicePlugin::HandleFormat" );
       
  2062     }
       
  2063 
       
  2064 void CHtiFtpServicePlugin::HandleListDrivesL( TBool aUnicode )
       
  2065     {
       
  2066     HTI_LOG_FUNC_IN( "CHtiFtpServicePlugin::HandleListDrivesL" );
       
  2067     TInt driveCount = 0;
       
  2068     TDriveList driveList;
       
  2069 
       
  2070     User::LeaveIfError( iFs.DriveList( driveList, KDriveAttAll ) );
       
  2071 
       
  2072     // Buffer for the drive list that is returned
       
  2073     CBufFlat* driveListBuf = CBufFlat::NewL( 256 );
       
  2074     CleanupStack::PushL( driveListBuf );
       
  2075     TInt bufPos = 0;
       
  2076 
       
  2077     for ( TInt i = 0; i < KMaxDrives; i++ )
       
  2078         {
       
  2079         if ( driveList[i] )
       
  2080             {
       
  2081             HTI_LOG_FORMAT( "Found drive number %d", i );
       
  2082             TVolumeInfo volInfo;
       
  2083             if ( iFs.Volume( volInfo, i ) == KErrNone )
       
  2084                 {
       
  2085                 driveCount++;
       
  2086                 // root path with length byte
       
  2087                 TChar driveLetter;
       
  2088                 iFs.DriveToChar( i, driveLetter );
       
  2089                 HTI_LOG_FORMAT( "Got volume info for drive %c",
       
  2090                         TUint( driveLetter ) );
       
  2091                 TBuf<3> rootPathBuf;
       
  2092                 rootPathBuf.AppendFormat( KRootPathFormat,
       
  2093                         TUint( driveLetter ) );
       
  2094                 TBuf8<1> lengthBuf;
       
  2095                 lengthBuf.Append( rootPathBuf.Length() );
       
  2096                 TBuf8<6> rootPathBuf8;
       
  2097                 if ( aUnicode )
       
  2098                     {
       
  2099                     rootPathBuf8.Append( ( TUint8* ) rootPathBuf.Ptr(),
       
  2100                             rootPathBuf.Length() * 2 );
       
  2101                     }
       
  2102                 else
       
  2103                     {
       
  2104                     rootPathBuf8.Copy( rootPathBuf );
       
  2105                     }
       
  2106                 driveListBuf->ExpandL( bufPos, rootPathBuf8.Length() + 1 );
       
  2107                 driveListBuf->Write( bufPos, lengthBuf, 1 );
       
  2108                 bufPos++;
       
  2109                 driveListBuf->Write( bufPos, rootPathBuf8 );
       
  2110                 bufPos += rootPathBuf8.Length();
       
  2111 
       
  2112                 // media type 1 byte
       
  2113                 TBuf8<8> tmpBuf;
       
  2114                 tmpBuf.Append( volInfo.iDrive.iType );
       
  2115                 driveListBuf->ExpandL( bufPos, tmpBuf.Length() );
       
  2116                 driveListBuf->Write( bufPos, tmpBuf );
       
  2117                 bufPos += tmpBuf.Length();
       
  2118 
       
  2119                 // UID 4 bytes
       
  2120                 tmpBuf.Copy( ( TUint8* ) ( &volInfo.iUniqueID ), 4 );
       
  2121                 driveListBuf->ExpandL( bufPos, tmpBuf.Length() );
       
  2122                 driveListBuf->Write( bufPos, tmpBuf );
       
  2123                 bufPos += tmpBuf.Length();
       
  2124 
       
  2125                 // size 8 bytes
       
  2126                 tmpBuf.Copy( ( TUint8* ) ( &volInfo.iSize ), 8 );
       
  2127                 driveListBuf->ExpandL( bufPos, tmpBuf.Length() );
       
  2128                 driveListBuf->Write( bufPos, tmpBuf );
       
  2129                 bufPos += tmpBuf.Length();
       
  2130 
       
  2131                 // free space 8 bytes
       
  2132                 tmpBuf.Copy( ( TUint8* ) ( &volInfo.iFree ), 8 );
       
  2133                 driveListBuf->ExpandL( bufPos, tmpBuf.Length() );
       
  2134                 driveListBuf->Write( bufPos, tmpBuf );
       
  2135                 bufPos += tmpBuf.Length();
       
  2136 
       
  2137                 // name with length byte
       
  2138                 HBufC8* driveNameBuf8 = NULL;
       
  2139                 TInt driveNameLength = volInfo.iName.Length();
       
  2140                 lengthBuf.Zero();
       
  2141                 lengthBuf.Append( driveNameLength );
       
  2142                 driveListBuf->ExpandL( bufPos, 1 );
       
  2143                 driveListBuf->Write( bufPos, lengthBuf, 1 );
       
  2144                 bufPos++;
       
  2145                 if ( driveNameLength > 0 )
       
  2146                     {
       
  2147                     if ( aUnicode )
       
  2148                         {
       
  2149                         driveNameBuf8 = HBufC8::NewL( driveNameLength * 2 );
       
  2150                         driveNameBuf8->Des().Append(
       
  2151                                 ( TUint8* ) volInfo.iName.Ptr(),
       
  2152                                 driveNameLength * 2 );
       
  2153                         }
       
  2154                     else
       
  2155                         {
       
  2156                         driveNameBuf8 = HBufC8::NewL( driveNameLength );
       
  2157                         driveNameBuf8->Des().Append( volInfo.iName );
       
  2158                         }
       
  2159                     HTI_LOG_FORMAT( "Drive name: %S", &volInfo.iName );
       
  2160                     driveListBuf->ExpandL( bufPos, driveNameBuf8->Length() );
       
  2161                     driveListBuf->Write( bufPos, driveNameBuf8->Ptr(),
       
  2162                         driveNameBuf8->Length() );
       
  2163                     bufPos += driveNameBuf8->Length();
       
  2164                     delete driveNameBuf8;
       
  2165                     driveNameBuf8 = NULL;
       
  2166                     }
       
  2167                 }
       
  2168             }
       
  2169         }
       
  2170 
       
  2171     // All drives added - write number of drives to the beginning of buffer
       
  2172     TBuf8<1> countBuf;
       
  2173     countBuf.Append( ( TUint8* ) ( &driveCount ), 1 );
       
  2174     driveListBuf->InsertL( 0, countBuf, 1 );
       
  2175 
       
  2176     // Make a copy of the buffer to iSendBuffer
       
  2177     delete iSendBuffer;
       
  2178     iSendBuffer = NULL;
       
  2179     iSendBuffer = driveListBuf->Ptr( 0 ).AllocL();
       
  2180     CleanupStack::PopAndDestroy(); // driveListBuf
       
  2181 
       
  2182     // ...and send it away
       
  2183     TInt err = iDispatcher->DispatchOutgoingMessage(
       
  2184         iSendBuffer, KFtpServiceUid, EFalse, EHtiPriorityControl );
       
  2185 
       
  2186     if (  err != KErrNone )
       
  2187         {
       
  2188         //wait for memory
       
  2189         iState = EListBusy;
       
  2190         iDispatcher->AddMemoryObserver( this );
       
  2191         }
       
  2192     else
       
  2193         {
       
  2194         iSendBuffer = NULL; // ownership transferred
       
  2195         }
       
  2196 
       
  2197     HTI_LOG_FUNC_OUT( "CHtiFtpServicePlugin::HandleListDrivesL" );
       
  2198     }
       
  2199 
       
  2200 
       
  2201 // End of File