skins/AknSkins/srvsrc/aknssrvactivebackupdataclient.cpp
changeset 0 05e9090e2422
child 32 d9c996538b26
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:  Skin server's active data owner in backup/restore.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <f32file.h>
       
    20 
       
    21 #include "aknssrvactivebackupdataclient.h"
       
    22 #include "AknsSrvUtils.h"
       
    23 
       
    24 #include "AknsDebug.h"
       
    25 
       
    26 // Maximum size of header info.
       
    27 const TUint KMaxHeaderSize = 256;
       
    28 
       
    29 // number of bytes to read from file per block
       
    30 const TUint KCheckSumBlockSize = 128;
       
    31 
       
    32 // Default size of restore/backup stream size in bytes.
       
    33 const TUint KArbitraryNumber=1024;
       
    34 
       
    35 // 8 bytes
       
    36 const TUint K8Bytes = 8;
       
    37 // 16 bytes
       
    38 const TUint K16Bytes = 16;
       
    39 // Length of extension (including preceding dot).
       
    40 const TInt KExtensionLength = 4;
       
    41 
       
    42 _LIT( KSkinRootDir,":\\resource\\skins\\" );
       
    43 
       
    44 // ======== MEMBER FUNCTIONS ========
       
    45 
       
    46 // Skin files are backed up in the following format.
       
    47 // filename contains drive and path in addition to filename.
       
    48 // <checksum><filesize><filenamelen><filename>
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // Symbian constructor.
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CAknsSrvActiveBackupDataClient* CAknsSrvActiveBackupDataClient::NewL( RFs& aFsSession )
       
    55     {
       
    56     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::NewL" );
       
    57     CAknsSrvActiveBackupDataClient* self =
       
    58         new( ELeave ) CAknsSrvActiveBackupDataClient( aFsSession );
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL();
       
    61     CleanupStack::Pop( self );
       
    62 
       
    63     return self;
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // Destructor
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CAknsSrvActiveBackupDataClient::~CAknsSrvActiveBackupDataClient()
       
    71     {
       
    72     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::destructor" );
       
    73     iFileArray.Reset();
       
    74     iFileArray.Close();
       
    75     iFile.Close();
       
    76     delete iBuffer;
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Prepare for backup and restore ( no actions).
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 void CAknsSrvActiveBackupDataClient::PrepareForBURL( TInt /*aBackupStateValue*/ )
       
    84     {
       
    85     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::PrepareForBURL" );
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // Inform that all data has been backed up or restored.
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 void CAknsSrvActiveBackupDataClient::AllSnapshotsSuppliedL()
       
    93     {
       
    94     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::AllSnapshotsSuppliedL" );
       
    95     // Finalize and cleanup.
       
    96     return;
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // Not supported.
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 void CAknsSrvActiveBackupDataClient::ReceiveSnapshotDataL(
       
   104     TDriveNumber /*aDrive*/, TDesC8& /*aBuffer*/, TBool /*aLastSection*/)
       
   105     {
       
   106     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::ReceiveSnapshotDataL" );
       
   107     User::Leave( KErrNotSupported );
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // Make a guess about data size.
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 TUint CAknsSrvActiveBackupDataClient::GetExpectedDataSize(
       
   115     TDriveNumber /*aDrive*/)
       
   116     {
       
   117     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::GetExpectedDataSize" );
       
   118     // we have no idea at this point - we even don't know what is to be
       
   119     // backed up yet
       
   120     return KArbitraryNumber;
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // Not supported.
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 void CAknsSrvActiveBackupDataClient::GetSnapshotDataL(
       
   128     TDriveNumber /*aDrive*/, TPtr8& /*aBuffer*/, TBool& /*aFinished*/)
       
   129     {
       
   130     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::GetSnapshotDataL" );
       
   131     User::Leave( KErrNotSupported );
       
   132     }
       
   133 
       
   134 // ---------------------------------------------------------------------------
       
   135 // Initialize for backup - collect to-be-backed-up-files.
       
   136 // ---------------------------------------------------------------------------
       
   137 //
       
   138 void CAknsSrvActiveBackupDataClient::InitialiseGetBackupDataL(
       
   139     TDriveNumber aDrive)
       
   140     {
       
   141     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::InitialiseGetBackupDataL" );
       
   142     DoInitialiseGetBackupDataL( aDrive );
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // Run state machine for backup. Each file is opened and streamed to the
       
   147 // BUR engine.
       
   148 // ---------------------------------------------------------------------------
       
   149 //
       
   150 void CAknsSrvActiveBackupDataClient::GetBackupDataSectionL(
       
   151     TPtr8& aBuffer, TBool& aFinished)
       
   152     {
       
   153     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::GetBackupDataSectionL" );
       
   154     // Make sure that the buffer is empty and starts from the beginning
       
   155     aBuffer.SetLength(0);
       
   156 
       
   157     // don't assume they set it to false
       
   158     aFinished = EFalse;
       
   159     // any files to backup
       
   160     if( iFileArray.Count() == 0 )
       
   161         {
       
   162         // nothing to backup - just return the finished flag
       
   163         aFinished = ETrue;
       
   164         // clear the list and stop.
       
   165         iFileArray.Reset();
       
   166         AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::GetBackupDataSectionL no files" );
       
   167         return;
       
   168         }
       
   169 
       
   170     // run the state machine
       
   171     while( ETrue )
       
   172         {
       
   173         switch( iBackupState )
       
   174             {
       
   175             // open a file for processing
       
   176             case EBackupNoFileOpen:
       
   177                 {
       
   178                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::GetBackupDataSectionL state %d", iBackupState );
       
   179                 if( iFileIndex >= iFileArray.Count() )
       
   180                     {
       
   181                     // all files have been processed - send the finished flag
       
   182                     aFinished = ETrue;
       
   183                     // clear the list and stop.
       
   184                     iFileArray.Reset();
       
   185                     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::GetBackupDataSectionL all processed" );
       
   186                     return;
       
   187                     }
       
   188 
       
   189                 // open file to send
       
   190                 TInt rc=iFile.Open( iFsSession,
       
   191                                     iFileArray[iFileIndex].FullName(),
       
   192                                     ( EFileRead | EFileShareExclusive | EFileStream ) );
       
   193                 if( rc != KErrNone )
       
   194                     {
       
   195                     // there's nothing we can do if we can't open the file
       
   196                     // so we just skip it
       
   197                     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::GetBackupDataSectionL skip file" );
       
   198                     ++iFileIndex;
       
   199                     break;
       
   200                     }
       
   201                 iBackupState = EBackupOpenNothingSent;
       
   202                 break;
       
   203                 }
       
   204             // nothing sent (so far) for this file - send the header info
       
   205             case EBackupOpenNothingSent:
       
   206                 {
       
   207                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::GetBackupDataSectionL state %d", iBackupState );
       
   208                 TInt fileSize;
       
   209                 TInt retValue = iFile.Size( fileSize );
       
   210                 if( retValue != KErrNone || fileSize == 0 )
       
   211                     {
       
   212                     // empty or unreadable - skip this file
       
   213                     iBackupState = EBackupEndOfFile;
       
   214                     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::GetBackupDataSectionL skip file2" );
       
   215                     break;
       
   216                     }
       
   217 
       
   218                 // build the header - this is an instance member because it
       
   219                 // has to persist over multiple calls to this method
       
   220                 TPtr headerPtr = iBuffer->Des();
       
   221 
       
   222                 // get the checksum - only grab last 4 bytes - enough to be satisfied that
       
   223                 // the backup and restore worked ok
       
   224                 TUint64 checksum = CheckSumL( iFile ) & 0xffffffff;
       
   225 
       
   226                 // build the header - note NOT AppendFormat(); wipes out previous content.
       
   227                 // <no of files><checksum><filesize><filenamelen><filename>
       
   228                 headerPtr.Format(_L("%8x%8lx%8x%8x"),
       
   229                     iFileArray.Count(),
       
   230                     checksum,
       
   231                     fileSize,
       
   232                     iFileArray[iFileIndex].FullName().Length());
       
   233                 headerPtr.Append( iFileArray[iFileIndex].FullName() );
       
   234 
       
   235                 // we need it to look like an 8bit buffer
       
   236                 TPtr8 headerPtr8(
       
   237                     (TUint8*)headerPtr.Ptr(),
       
   238                     headerPtr.Size(),
       
   239                     headerPtr.Size() );
       
   240 
       
   241                 // Check how much room is left in the buffer.
       
   242                 // it starts out empty when we get it from the BUE
       
   243                 TInt available = aBuffer.MaxSize() - aBuffer.Size();
       
   244 
       
   245                 // Check is there enough room for the whole header.
       
   246                 TBool enoughRoom = headerPtr8.Size() < available;
       
   247 
       
   248                 // append the header to the buffer (only till it's full)
       
   249                 aBuffer.Append(
       
   250                     headerPtr8.Ptr(),
       
   251                     enoughRoom ? headerPtr8.Size() : available) ;
       
   252 
       
   253                 // decide what needs to happen next
       
   254                 // if complete then we need data, otherwise we need to put
       
   255                 // the rest of the header in the next chunk
       
   256                 if( enoughRoom )
       
   257                     {
       
   258                     iBackupState = EBackupOpenAllHeaderSent;
       
   259                     }
       
   260                 else
       
   261                     {
       
   262                     // we need to keep track of how much of the header has
       
   263                     // been sent so that we only send the reminder on the next
       
   264                     // iteration
       
   265                     iHeaderSent = available;
       
   266                     iBackupState = EBackupOpenPartHeaderSent;
       
   267                     }
       
   268 
       
   269                 // if the buffer's full we need to return control to the backup engine
       
   270                 // Because the finishedFlag is not set, the BUE will process this
       
   271                 // chunk and then ask for another
       
   272                 if( aBuffer.Size() == aBuffer.MaxSize() )
       
   273                     {
       
   274                     return;
       
   275                     }
       
   276                 break;
       
   277                 }
       
   278             // need to send the rest of the header
       
   279             case EBackupOpenPartHeaderSent:
       
   280                 {
       
   281                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::GetBackupDataSectionL state %d", iBackupState );
       
   282                 // get back the header - this is already loaded with the necessary info
       
   283                 // from the previous state we were in
       
   284                 TPtr headerPtr = iBuffer->Des();
       
   285 
       
   286                 // we need it to look like an 8bit buffer
       
   287                 TPtr8 headerPtr8(
       
   288                     (TUint8*)headerPtr.Ptr(),
       
   289                     headerPtr.Size(),
       
   290                     headerPtr.Size() );
       
   291 
       
   292                 // Check how many bytes have we yet to send.
       
   293                 TInt bytesRemaining = headerPtr.Size() - iHeaderSent;
       
   294 
       
   295                 // Check how much room in the buffer.
       
   296                 TInt available = aBuffer.MaxSize() - aBuffer.Size();
       
   297 
       
   298                 // enough, if not send as much as we can
       
   299                 TBool enoughRoom = bytesRemaining < available;
       
   300                 aBuffer.Append(
       
   301                     headerPtr8.Ptr() + iHeaderSent,
       
   302                     enoughRoom ? bytesRemaining : available );
       
   303 
       
   304                 if( enoughRoom )
       
   305                     {
       
   306                     iHeaderSent = 0; // ready for next header
       
   307                     iBackupState = EBackupOpenAllHeaderSent;
       
   308                     }
       
   309                 else
       
   310                     {
       
   311                     iHeaderSent += available; // ready to do round again
       
   312                     // state remains as EBackupOpenPartHeaderSent
       
   313                     }
       
   314 
       
   315                 // if the buffer's full we need to return control to the backup engine
       
   316                 // Because the finishedFlag is not set, the BUE will process this
       
   317                 // chunk and then ask for another
       
   318                 if( aBuffer.Size() == aBuffer.MaxSize() )
       
   319                     {
       
   320                     return;
       
   321                     }
       
   322                 break;
       
   323                 }
       
   324             // need to send some data
       
   325             case EBackupOpenAllHeaderSent:
       
   326                 {
       
   327                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::GetBackupDataSectionL state %d", iBackupState );
       
   328                 // how many bytes can we send
       
   329                 TInt available = aBuffer.MaxSize() - aBuffer.Size();
       
   330 
       
   331                 // create a buffer for this data (plus one for PtrZ)
       
   332                 HBufC8* transferBuffer = HBufC8::NewLC( available + 1 );
       
   333                 TPtr8 bufferToSend = transferBuffer->Des();
       
   334 
       
   335                 // get the data
       
   336                 User::LeaveIfError( iFile.Read( bufferToSend, available ) );
       
   337 
       
   338                 // Check how much did we actually read.
       
   339                 TInt bytesRead = bufferToSend.Size();
       
   340 
       
   341                 // EOF
       
   342                 if( bytesRead == 0 )
       
   343                     {
       
   344                     CleanupStack::PopAndDestroy( transferBuffer );
       
   345                     iBackupState = EBackupEndOfFile;
       
   346                     break;
       
   347                     }
       
   348 
       
   349                 // add it to the aBuffer
       
   350                 aBuffer.Append( bufferToSend.PtrZ(), bytesRead );
       
   351 
       
   352                 // tidy up
       
   353                 CleanupStack::PopAndDestroy( transferBuffer );
       
   354 
       
   355                 // if the buffer's full we need to return control to the backup engine
       
   356                 if( aBuffer.Size() == aBuffer.MaxSize() )
       
   357                     {
       
   358                     return;
       
   359                     }
       
   360                 break;
       
   361                 }
       
   362             // At the end of the current file.
       
   363             case EBackupEndOfFile:
       
   364                 {
       
   365                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::GetBackupDataSectionL state %d", iBackupState );
       
   366 
       
   367                 // how many bytes can we send
       
   368                 if ( aBuffer.Size() != 0 )
       
   369                     {
       
   370                     TInt available = aBuffer.MaxSize() - aBuffer.Size();
       
   371                     // pad the end of the buffer with NULL.
       
   372                     HBufC8* transferBuffer = HBufC8::NewLC( available + 1 );
       
   373                     TPtr8 bufferToSend = transferBuffer->Des();
       
   374                     bufferToSend.FillZ();
       
   375                     aBuffer.Append( bufferToSend.PtrZ(), available );
       
   376                     CleanupStack::PopAndDestroy( transferBuffer );
       
   377                     if( aBuffer.Size() != aBuffer.MaxSize() )
       
   378                         {
       
   379                         // Sanity check
       
   380                         User::Leave( KErrGeneral );
       
   381                         }
       
   382                     }
       
   383                 // Close file and move on to next file.
       
   384                 iFile.Close();
       
   385                 ++iFileIndex;
       
   386                 // Start all over again.
       
   387                 iBackupState = EBackupNoFileOpen;
       
   388                 break;
       
   389                 }
       
   390             default:
       
   391                 {
       
   392                 // not reachable
       
   393                 return;
       
   394                 }
       
   395             }
       
   396         }
       
   397     }
       
   398 
       
   399 // ---------------------------------------------------------------------------
       
   400 // Initialize restore.
       
   401 // ---------------------------------------------------------------------------
       
   402 //
       
   403 void CAknsSrvActiveBackupDataClient::InitialiseRestoreBaseDataL(
       
   404     TDriveNumber /*aDrive*/ )
       
   405     {
       
   406     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::InitialiseRestoreBaseDataL" );
       
   407     // this is the first state of the restore state machine
       
   408     iRestoreState = ERestoreNumberOfFiles;
       
   409     // to keep track in the state machine whether any data was actually sent
       
   410     iDataRestored = EFalse;
       
   411     iFilesRestored = 0;
       
   412     }
       
   413 
       
   414 // ---------------------------------------------------------------------------
       
   415 // Run state machine for restore. Receive stream from BUR engine and turn it
       
   416 // to file(s).
       
   417 // ---------------------------------------------------------------------------
       
   418 //
       
   419 void CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL(
       
   420     TDesC8& aBuffer, TBool aFinished )
       
   421     {
       
   422     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL" );
       
   423     // convert the buffer - this is KMaxHeaderSize=256
       
   424     TInt bufferIndex;
       
   425     TPtr bufPtr = iBuffer->Des();
       
   426 
       
   427     // used to walk the buffer
       
   428     // got a new buffer - because each time this method is called, we have a
       
   429     // fresh chunk of data
       
   430     bufferIndex = 0;
       
   431 
       
   432     // to mark when the state machine is through
       
   433     TBool done = EFalse;
       
   434 
       
   435     // check whether this is an empty restore
       
   436     if( aFinished && !iDataRestored )
       
   437         {
       
   438         // we have to do this and not rely on aFinishedFlag alone, because
       
   439         // if aFinished is used, we'll process the last state of the machine
       
   440         // which does tidyup, except that if there was no data, no tidyup should
       
   441         // be done
       
   442         AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL empty restore" );
       
   443         return;
       
   444         }
       
   445 
       
   446     // run the machine
       
   447     do
       
   448         {
       
   449         // Check how many bytes are there available in the buffer for processing.
       
   450         TInt bytesAvailable = aBuffer.Length() - bufferIndex;
       
   451         // the reason why we are testing finishedFlag is because we must
       
   452         // make sure we re-enter the machine to do the tidyup
       
   453         if( bytesAvailable <= 0 && !aFinished )
       
   454             {
       
   455             // ran out of data in the chunk
       
   456             // so we return and wait for more data to arrive
       
   457             return;
       
   458             }
       
   459 
       
   460         if( aFinished &&
       
   461             iRestoreState != ERestoreComplete &&
       
   462             iRestoreState != ERestoreExpectMoreData )
       
   463             {
       
   464             // ran out of data early
       
   465             // will be ERestoreComplete if data not aligned on 128
       
   466             // will be ERestoreExpectMoreData if data aligned on 128
       
   467             User::Leave( KErrCorrupt );
       
   468             }
       
   469         // yep there was some data in the chunk if we got here
       
   470         if( bytesAvailable > 0 )
       
   471             {
       
   472             iDataRestored = ETrue;
       
   473             }
       
   474         switch( iRestoreState )
       
   475             {
       
   476             case ERestoreNumberOfFiles: // 16 bytes
       
   477                 {
       
   478                 TInt bytesToGet = bytesAvailable >= K16Bytes ? K16Bytes : bytesAvailable;
       
   479                 TPtrC8 ptr8 = aBuffer.Mid( bufferIndex, bytesToGet );
       
   480                 bufferIndex += bytesToGet;
       
   481 
       
   482                 // convert to 16 bit
       
   483                 TPtr ptr16(
       
   484                     (TUint16*) ptr8.Ptr(),
       
   485                     bytesToGet >> 1,
       
   486                     bytesToGet >> 1 );
       
   487 
       
   488                 // stick it in the heap buffer
       
   489                 bufPtr.Zero();
       
   490                 bufPtr.Append( ptr16 );
       
   491 
       
   492                 if( bufPtr.Length() == K8Bytes )
       
   493                     {
       
   494                     // If file size is now read, read file name size next.
       
   495                     iRestoreState = ERestoreExpectChecksum;
       
   496                     }
       
   497 
       
   498                 break;
       
   499                 }
       
   500             case ERestoreExpectChecksum:
       
   501                 {
       
   502                 // first of all, process the heap buffer to extract the checksum
       
   503                 TLex lex( bufPtr );
       
   504                 lex.SkipSpace();
       
   505                 lex.Val( iNumberofFiles, EHex );
       
   506 
       
   507                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   508                 // if we have 16 bytes, get them;
       
   509                 // otherwise get what we have available.
       
   510                 // if not complete then we need to go around again to get the rest
       
   511                 TInt bytesToGet = bytesAvailable >= K16Bytes ? K16Bytes : bytesAvailable;
       
   512 
       
   513                 // extract the bytes
       
   514                 TPtrC8 ptr8 = aBuffer.Mid( bufferIndex, bytesToGet );
       
   515                 bufferIndex += bytesToGet;
       
   516 
       
   517                 // convert to 16 bit
       
   518                 TPtr ptr16(
       
   519                     (TUint16*) ptr8.Ptr(),
       
   520                     bytesToGet >> 1,
       
   521                     bytesToGet >> 1 );
       
   522 
       
   523                 // stick it in the heap buffer
       
   524                 bufPtr.Zero();
       
   525                 bufPtr.Append( ptr16 );
       
   526 
       
   527                 if( bufPtr.Length() == 8 )
       
   528                     {
       
   529                     // If complete checksum is available, move on to filesize.
       
   530                     iRestoreState = ERestoreExpectFileSize;
       
   531                     }
       
   532                 else
       
   533                     {
       
   534                     // Otherwise continue reading the checksum.
       
   535                     iRestoreState = ERestoreExpectMoreChecksum;
       
   536                     }
       
   537                 break;
       
   538                 }
       
   539             // checksum overran buffer on first/subsequent read.
       
   540             case ERestoreExpectMoreChecksum:
       
   541                 {
       
   542                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   543                 // Check how many more bytes do we want.
       
   544                 TInt bytesNeeded=( K8Bytes - bufPtr.Length() ) << 1;
       
   545                 TInt bytesToGet=
       
   546                     bytesAvailable >= bytesNeeded ? bytesNeeded : bytesAvailable;
       
   547 
       
   548                 // extract the bytes
       
   549                 TPtrC8 ptr8 = aBuffer.Mid( bufferIndex, bytesToGet );
       
   550                 bufferIndex += bytesToGet;
       
   551 
       
   552                 // convert to 16 bit
       
   553                 TPtr ptr16(
       
   554                     (TUint16*) ptr8.Ptr(),
       
   555                     bytesToGet >> 1,
       
   556                     bytesToGet >> 1 );
       
   557 
       
   558                 // append it to the heap buffer
       
   559                 bufPtr.Append( ptr16 );
       
   560 
       
   561                 if( bufPtr.Length() == K8Bytes )
       
   562                     {
       
   563                     // If complete checksum is now available, move on to filesize.
       
   564                     iRestoreState = ERestoreExpectFileSize;
       
   565                     }
       
   566                 else
       
   567                     {
       
   568                     // same state : ERestoreExpectMoreChecksum;
       
   569                     }
       
   570                 break;
       
   571                 }
       
   572             case ERestoreExpectFileSize: // 16 bytes
       
   573                 {
       
   574                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   575                 // first of all, process the heap buffer to extract the checksum
       
   576                 TLex lex( bufPtr );
       
   577                 lex.SkipSpace();
       
   578                 lex.Val( iChecksum, EHex );
       
   579 
       
   580                 // now start getting the file size
       
   581                 TInt bytesToGet = bytesAvailable >= K16Bytes ? K16Bytes : bytesAvailable;
       
   582                 TPtrC8 ptr8 = aBuffer.Mid( bufferIndex, bytesToGet );
       
   583                 bufferIndex += bytesToGet;
       
   584 
       
   585                 // convert to 16 bit
       
   586                 TPtr ptr16(
       
   587                     (TUint16*) ptr8.Ptr(),
       
   588                     bytesToGet >> 1,
       
   589                     bytesToGet >> 1 );
       
   590 
       
   591                 // stick it in the heap buffer
       
   592                 bufPtr.Zero();
       
   593                 bufPtr.Append( ptr16 );
       
   594 
       
   595                 if( bufPtr.Length() == K8Bytes )
       
   596                     {
       
   597                     // If file size is now read, read file name size next.
       
   598                     iRestoreState = ERestoreExpectFileNameSize;
       
   599                     }
       
   600                 else
       
   601                     {
       
   602                     // Otherwise continue reading the file size.
       
   603                     iRestoreState = ERestoreExpectMoreFileSize;
       
   604                     }
       
   605                 break;
       
   606                 }
       
   607             // file size didn't fit into one buffer
       
   608             case ERestoreExpectMoreFileSize:
       
   609                 {
       
   610                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   611                 // Check how many more bytes do we want.
       
   612                 TInt bytesNeeded = ( K8Bytes - bufPtr.Length() ) << 1;
       
   613                 TInt bytesToGet = bytesAvailable >= bytesNeeded ? bytesNeeded : bytesAvailable;
       
   614                 TPtrC8 ptr8 = aBuffer.Mid( bufferIndex, bytesToGet );
       
   615                 bufferIndex += bytesToGet;
       
   616 
       
   617                 // convert to 16 bit
       
   618                 TPtr ptr16(
       
   619                     (TUint16*) ptr8.Ptr(),
       
   620                     bytesToGet >> 1,
       
   621                     bytesToGet >> 1 );
       
   622 
       
   623                 // append it to the heap buffer
       
   624                 bufPtr.Append( ptr16 );
       
   625 
       
   626                 if( bufPtr.Length() == K8Bytes )
       
   627                     {
       
   628                     // If file size is now read, read file name size next.
       
   629                     iRestoreState = ERestoreExpectFileNameSize;
       
   630                     }
       
   631                 else
       
   632                     {
       
   633                     // same state ERestoreExpectMoreFileSize;
       
   634                     }
       
   635                 break;
       
   636                 }
       
   637             // the size of the file name to restore
       
   638             case ERestoreExpectFileNameSize:
       
   639                 {
       
   640                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   641                 // first of all, process the heap buffer to extract the file size
       
   642                 TLex lex( bufPtr );
       
   643                 lex.SkipSpace();
       
   644                 lex.Val( iFileSize, EHex );
       
   645 
       
   646                 // now start getting the file name size
       
   647                 TInt bytesToGet = bytesAvailable >= K16Bytes ? K16Bytes : bytesAvailable;
       
   648                 TPtrC8 ptr8 = aBuffer.Mid( bufferIndex, bytesToGet );
       
   649                 bufferIndex += bytesToGet;
       
   650 
       
   651                 // convert to 16 bit
       
   652                 TPtr ptr16(
       
   653                     (TUint16*) ptr8.Ptr(),
       
   654                     bytesToGet >> 1,
       
   655                     bytesToGet >> 1 );
       
   656 
       
   657                 // stick it in the heap buffer
       
   658                 bufPtr.Zero();
       
   659                 bufPtr.Append( ptr16 );
       
   660 
       
   661                 if( bufPtr.Length() == K8Bytes )
       
   662                     {
       
   663                     // If filename size is now read, read filename next.
       
   664                     iRestoreState = ERestoreExpectFileName;
       
   665                     }
       
   666                 else
       
   667                     {
       
   668                     // Otherwise, continue reading the filename size.
       
   669                     iRestoreState = ERestoreExpectMoreFileNameSize;
       
   670                     }
       
   671                 break;
       
   672                 }
       
   673             // the file name size overran the buffer
       
   674             case ERestoreExpectMoreFileNameSize:
       
   675                 {
       
   676                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   677                 // Check how many more bytes do we want.
       
   678                 TInt bytesNeeded = ( K8Bytes - bufPtr.Length() ) << 1;
       
   679                 TInt bytesToGet = bytesAvailable >= bytesNeeded ? bytesNeeded : bytesAvailable;
       
   680                 TPtrC8 ptr8 = aBuffer.Mid( bufferIndex, bytesToGet );
       
   681                 bufferIndex += bytesToGet;
       
   682 
       
   683                 // convert to 16 bit
       
   684                 TPtr ptr16(
       
   685                     (TUint16*) ptr8.Ptr(),
       
   686                     bytesToGet >> 1,
       
   687                     bytesToGet >> 1 );
       
   688 
       
   689                 // append it to the heap buffer
       
   690                 bufPtr.Append( ptr16 );
       
   691 
       
   692                 if( bufPtr.Length() == K8Bytes )
       
   693                     {
       
   694                     // If filename size is now read, read filename next.
       
   695                     iRestoreState = ERestoreExpectFileName;
       
   696                     }
       
   697                 else
       
   698                     {
       
   699                     // same state ERestoreExpectMoreFileNameSize;
       
   700                     }
       
   701                 break;
       
   702                 }
       
   703             case ERestoreExpectFileName:  // the name of the file to restore
       
   704                 {
       
   705                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   706                 // first of all, process the heap buffer to extract the file name size
       
   707                 TLex lex( bufPtr );
       
   708                 lex.SkipSpace();
       
   709                 lex.Val( iFileNameSize, EHex );
       
   710 
       
   711                 // now start getting the file name
       
   712                 TInt bytesToGet = bytesAvailable >= iFileNameSize << 1 ? iFileNameSize << 1:bytesAvailable;
       
   713                 TPtrC8 ptr8 = aBuffer.Mid( bufferIndex, bytesToGet );
       
   714                 bufferIndex += bytesToGet;
       
   715 
       
   716                 // convert to 16 bit
       
   717                 TPtr ptr16(
       
   718                     (TUint16*) ptr8.Ptr(),
       
   719                     bytesToGet >> 1,
       
   720                     bytesToGet >> 1 );
       
   721 
       
   722                 // stick it in the heap buffer
       
   723                 bufPtr.Zero();
       
   724                 bufPtr.Append( ptr16 );
       
   725 
       
   726                 if( bufPtr.Length() == iFileNameSize )
       
   727                     {
       
   728                     // If filename is now read, start reading the file content.
       
   729                     iRestoreState = ERestoreExpectData;
       
   730                     }
       
   731                 else
       
   732                     {
       
   733                     // Otherwise, continue reading the file.
       
   734                     iRestoreState = ERestoreExpectMoreFileName;
       
   735                     }
       
   736                 break;
       
   737                 }
       
   738             // the file name overran the buffer
       
   739             case ERestoreExpectMoreFileName:
       
   740                 {
       
   741                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   742                 // Check how many more bytes do we want.
       
   743                 TInt bytesNeeded = ( iFileNameSize  -bufPtr.Length() ) << 1;
       
   744                 TInt bytesToGet = bytesAvailable >= bytesNeeded ? bytesNeeded : bytesAvailable;
       
   745                 TPtrC8 ptr8 = aBuffer.Mid( bufferIndex, bytesToGet );
       
   746                 bufferIndex += bytesToGet;
       
   747 
       
   748                 // convert to 16 bit
       
   749                 TPtr ptr16(
       
   750                     (TUint16*) ptr8.Ptr(),
       
   751                     bytesToGet >> 1,
       
   752                     bytesToGet >> 1 );
       
   753 
       
   754                 // append it to the heap buffer
       
   755                 bufPtr.Append( ptr16 );
       
   756 
       
   757                 if( bufPtr.Length() == iFileNameSize )
       
   758                     {
       
   759                     // If filename is now read, start reading the file content.
       
   760                     iRestoreState = ERestoreExpectData;
       
   761                     }
       
   762                 else
       
   763                     {
       
   764                     // same state ERestoreExpectMoreFileName;
       
   765                     }
       
   766                 break;
       
   767                 }
       
   768             // now for the data
       
   769             case ERestoreExpectData:
       
   770                 {
       
   771                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   772                 iFileOpenError = EFalse;
       
   773                 // iFileSize counts down
       
   774                 TInt bytesToGet = bytesAvailable >= iFileSize ? iFileSize : bytesAvailable;
       
   775 
       
   776                 if ( iFsSession.IsValidName( bufPtr ) &&
       
   777                      !AknsSrvUtils::IsFile( iFsSession, bufPtr ) &&
       
   778                      RestoreFileInPrivateDirectoryL( bufPtr ) == KErrNone )
       
   779                     {
       
   780                     // write it
       
   781                     TInt fileErr = iFile.Write( aBuffer.Mid( bufferIndex, bytesToGet ) );
       
   782                     AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL file write %d", fileErr );
       
   783                     }
       
   784                 else
       
   785                     {
       
   786                     iFileOpenError = ETrue;
       
   787                     }
       
   788 
       
   789                 // update our counters
       
   790                 bufferIndex += bytesToGet;
       
   791                 iFileSize -= bytesToGet;
       
   792 
       
   793                 // Check if finished now.
       
   794                 if( iFileSize == 0 )
       
   795                     {
       
   796                     // If file is now written, restore is complete (for this file).
       
   797                     iRestoreState = ERestoreComplete;
       
   798                     }
       
   799                 else
       
   800                     {
       
   801                     // Otherwise, continue reading and writing the file.
       
   802                     iRestoreState = ERestoreExpectMoreData;
       
   803                     }
       
   804                 if ( iFileOpenError )
       
   805                     {
       
   806                     return;
       
   807                     }
       
   808 
       
   809                 break;
       
   810                 }
       
   811             // will almost certainly exceed a single buffer
       
   812             case ERestoreExpectMoreData:
       
   813                 {
       
   814                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   815                 // Check how much more.
       
   816                 TInt bytesToGet = bytesAvailable >= iFileSize ? iFileSize : bytesAvailable;
       
   817 
       
   818                 // write it
       
   819                 if (!iFileOpenError)
       
   820                     {
       
   821                     TInt fileErr = iFile.Write( aBuffer.Mid( bufferIndex, bytesToGet ) );
       
   822                     AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL file write %d", fileErr );
       
   823                     }
       
   824 
       
   825                 // update the counters
       
   826                 bufferIndex += bytesToGet;
       
   827                 iFileSize -= bytesToGet;
       
   828 
       
   829                 // Check if finished now.
       
   830                 if( iFileSize == 0 )
       
   831                     {
       
   832                     // If file is now written, restore is complete (for this file).
       
   833                     iRestoreState = ERestoreComplete;
       
   834                     }
       
   835                 else
       
   836                     {
       
   837                     // same state ERestoreExpectMoreData;
       
   838                     }
       
   839                 break;
       
   840                 }
       
   841             // file completely restored
       
   842             case ERestoreComplete:
       
   843                 {
       
   844                 AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL state %d", iRestoreState );
       
   845                 // calculate the checksum
       
   846                 if(!iFileOpenError)
       
   847                     {
       
   848                     TUint64 cksum = CheckSumL( iFile ) & 0xffffffff;
       
   849 
       
   850                     // validate that the checksum matches
       
   851                     if( cksum != iChecksum )
       
   852                         {
       
   853                         AKNS_TRACE_DEBUG2("CAknsSrvActiveBackupDataClient::RestoreBaseDataSectionL checksum fail %d %d", cksum, iChecksum );
       
   854                         User::Leave( KErrCorrupt );
       
   855                         }
       
   856 
       
   857                     // Done with the file, so close it.
       
   858                     iFile.Close();
       
   859                     }
       
   860                 iFilesRestored++;
       
   861 
       
   862                 // end of data - Check if another file is to be restored.
       
   863                 iRestoreState = ERestoreNumberOfFiles;
       
   864                 iFileOpenError = EFalse;
       
   865 
       
   866                 // Each file should end at the end of buffer.
       
   867                 aFinished = ETrue;
       
   868                 return;
       
   869                 }
       
   870             default:
       
   871                 break;
       
   872             }
       
   873         } while(!done);
       
   874     }
       
   875 
       
   876 // ---------------------------------------------------------------------------
       
   877 // Incremental restoration is not supported.
       
   878 // ---------------------------------------------------------------------------
       
   879 //
       
   880 void CAknsSrvActiveBackupDataClient::InitialiseRestoreIncrementDataL(
       
   881     TDriveNumber /*aDrive*/)
       
   882     {
       
   883     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::InitialiseRestoreIncrementDataL" );
       
   884     User::Leave( KErrNotSupported );
       
   885     }
       
   886 
       
   887 // ---------------------------------------------------------------------------
       
   888 // Incremental restoration is not supported.
       
   889 // ---------------------------------------------------------------------------
       
   890 //
       
   891 void CAknsSrvActiveBackupDataClient::RestoreIncrementDataSectionL(
       
   892     TDesC8& /*aBuffer*/, TBool /*aFinished*/)
       
   893     {
       
   894     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::RestoreIncrementDataSectionL" );
       
   895     User::Leave( KErrNotSupported );
       
   896     }
       
   897 
       
   898 // ---------------------------------------------------------------------------
       
   899 // Called when restore is complete - sets data back to initial state.
       
   900 // ---------------------------------------------------------------------------
       
   901 //
       
   902 void CAknsSrvActiveBackupDataClient::RestoreComplete(TDriveNumber /*aDrive*/)
       
   903     {
       
   904     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::RestoreComplete" );
       
   905     // Possibly delete some extra data.
       
   906     iDataRestored = EFalse;
       
   907     iRestoreState = ERestoreNumberOfFiles;
       
   908     }
       
   909 
       
   910 // ---------------------------------------------------------------------------
       
   911 // Tidy up when operation is over.
       
   912 // ---------------------------------------------------------------------------
       
   913 //
       
   914 void CAknsSrvActiveBackupDataClient::TerminateMultiStageOperation()
       
   915     {
       
   916     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::TerminateMultiStageOperation" );
       
   917     iFileArray.Reset();
       
   918     }
       
   919 
       
   920 // ---------------------------------------------------------------------------
       
   921 //
       
   922 // ---------------------------------------------------------------------------
       
   923 //
       
   924 TUint CAknsSrvActiveBackupDataClient::GetDataChecksum(TDriveNumber /*aDrive*/)
       
   925     {
       
   926     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::GetDataChecksum" );
       
   927     // not required - not implemented
       
   928     return KArbitraryNumber;
       
   929     }
       
   930 
       
   931 // ---------------------------------------------------------------------------
       
   932 // Initialize backup.
       
   933 // ---------------------------------------------------------------------------
       
   934 //
       
   935 void CAknsSrvActiveBackupDataClient::InitialiseGetProxyBackupDataL(
       
   936     TSecureId /*aSID*/, TDriveNumber aDrive )
       
   937     {
       
   938     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::InitialiseGetProxyBackupDataL" );
       
   939     DoInitialiseGetBackupDataL( aDrive );
       
   940     }
       
   941 
       
   942 // ---------------------------------------------------------------------------
       
   943 // Initializes restore state machine.
       
   944 // ---------------------------------------------------------------------------
       
   945 //
       
   946 void CAknsSrvActiveBackupDataClient::InitialiseRestoreProxyBaseDataL(
       
   947     TSecureId /*aSID*/, TDriveNumber /*aDrive*/ )
       
   948     {
       
   949     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::InitialiseRestoreProxyBaseDataL" );
       
   950     // this is the first state of the restore state machine
       
   951     iRestoreState = ERestoreNumberOfFiles;
       
   952     // to keep track in the state machine whether any data was actually sent
       
   953     iDataRestored = EFalse;
       
   954     }
       
   955 
       
   956 // ---------------------------------------------------------------------------
       
   957 // Creates a checksum for the files.
       
   958 // ---------------------------------------------------------------------------
       
   959 //
       
   960 TUint64 CAknsSrvActiveBackupDataClient::CheckSumL(const RFile& aOpenFile) const
       
   961     {
       
   962     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::CheckSumL" );
       
   963     // scoot through the database file building the checksum
       
   964     TInt seekPos = 0; // rewind first
       
   965     User::LeaveIfError( aOpenFile.Seek( ESeekStart, seekPos ) );
       
   966     TUint64 total = 0;
       
   967     HBufC8* block = HBufC8::NewL( KCheckSumBlockSize );
       
   968     CleanupStack::PushL( block );
       
   969     TPtr8 ptr=block->Des();
       
   970     while( ETrue )
       
   971         {
       
   972         User::LeaveIfError( aOpenFile.Read( ptr ) );
       
   973         TInt len = ptr.Length();
       
   974         if( len == 0 )
       
   975             {
       
   976             break;
       
   977             }
       
   978         // calculate the checksum
       
   979         for( TInt i = 0; i < len; ++i )
       
   980             {
       
   981             TUint64 carry = total&(0x8000000000000000ULL);
       
   982             total<<=1;
       
   983             if( carry )
       
   984                 {
       
   985                 total|=1;
       
   986                 }
       
   987             TUint in = ptr[i];
       
   988             total += in;
       
   989             }
       
   990         };
       
   991     CleanupStack::PopAndDestroy( block );
       
   992     // restore file position
       
   993     seekPos = 0;
       
   994     User::LeaveIfError( aOpenFile.Seek( ESeekStart, seekPos ) );
       
   995     return total;
       
   996     }
       
   997 
       
   998 // ---------------------------------------------------------------------------
       
   999 // Scan directory for files.
       
  1000 // ---------------------------------------------------------------------------
       
  1001 //
       
  1002 void CAknsSrvActiveBackupDataClient::ScanDirectoryForSkinFilesL(
       
  1003     const TDesC& aRootDir )
       
  1004     {
       
  1005     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::ScanDirectoryForSkinFilesL" );
       
  1006     CDirScan *dirScan = CDirScan::NewLC( iFsSession );
       
  1007     dirScan->SetScanDataL(
       
  1008         aRootDir,
       
  1009         KEntryAttNormal | KEntryAttHidden | KEntryAttSystem |
       
  1010         KEntryAttDir,
       
  1011         ESortNone );
       
  1012 
       
  1013     // Fetch all directories and files from root.
       
  1014     CDir* entryList = NULL;
       
  1015     TParse parse;
       
  1016     for(;;)
       
  1017         {
       
  1018         TRAPD( err, dirScan->NextL( entryList ) );
       
  1019 
       
  1020         // Stop in error case, or if no more data.
       
  1021         if (!entryList  || ( err != KErrNone) )
       
  1022             {
       
  1023             break;
       
  1024             }
       
  1025 
       
  1026         for (TInt i=0; i < entryList->Count(); i++)
       
  1027             {
       
  1028             TEntry entry = (*entryList)[i];
       
  1029             const TDesC& dir = dirScan->FullPath();
       
  1030             parse.Set( entry.iName, &dir, NULL );
       
  1031             if ( !entry.IsDir() )
       
  1032                 {
       
  1033                 iFileArray.Append( parse );
       
  1034                 }
       
  1035             }
       
  1036         delete entryList;
       
  1037         }
       
  1038     AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::ScanDirectoryForSkinFilesL noFiles=%d", iFileArray.Count() );
       
  1039 
       
  1040     // Destroy the list.
       
  1041     CleanupStack::PopAndDestroy( dirScan );
       
  1042     }
       
  1043 
       
  1044 // ---------------------------------------------------------------------------
       
  1045 // Re-creates backed-up file in private directory.
       
  1046 // ---------------------------------------------------------------------------
       
  1047 //
       
  1048 TInt CAknsSrvActiveBackupDataClient::RestoreFileInPrivateDirectoryL(
       
  1049     const TDesC& aFileName )
       
  1050     {
       
  1051     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::RestoreFileInPrivateDirectoryL" );
       
  1052     _LIT( KAknsStaticPath, "\\private\\10207114\\import" );
       
  1053     _LIT( KAknsSknExtension, ".SKN" );
       
  1054     TBool parseError = EFalse;
       
  1055     TInt fileErr = KErrNone;
       
  1056 
       
  1057     HBufC* fileNameBuffer = HBufC::NewL( KMaxFileName );
       
  1058     TParsePtrC fileName( aFileName );
       
  1059     TPtr bufferPtr = fileNameBuffer->Des();
       
  1060 
       
  1061     // First append drive and static path.
       
  1062     if ( fileName.DrivePresent() )
       
  1063         {
       
  1064         bufferPtr.Append( fileName.Drive() );
       
  1065         }
       
  1066     else
       
  1067         {
       
  1068         AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::RestoreFileInPrivateDirectoryL parse error1" );
       
  1069         parseError = ETrue;
       
  1070         }
       
  1071     if ( !parseError )
       
  1072         {
       
  1073         bufferPtr.Append( KAknsStaticPath );
       
  1074         }
       
  1075 
       
  1076     if ( fileName.PathPresent() && !parseError )
       
  1077         {
       
  1078         // Take path without the trailing backslash.
       
  1079         TPtrC path = fileName.Path().Left( fileName.Path().Length() - 1 );
       
  1080 
       
  1081         // Locate last backslash.
       
  1082         TChar backslash('\\');
       
  1083         TInt bsLoc = path.LocateReverse( backslash );
       
  1084 
       
  1085         // Append skin PID to the directory.
       
  1086         bufferPtr.Append( fileName.Path().Mid( bsLoc ) );
       
  1087 
       
  1088         if ( fileName.ExtPresent() )
       
  1089             {
       
  1090             // Last, append filename. Now string is complete.
       
  1091             bufferPtr.Append( fileName.NameAndExt() );
       
  1092 
       
  1093             // switch the extension and check for file existance.
       
  1094             bufferPtr.Replace(
       
  1095                 bufferPtr.Length() - KExtensionLength,
       
  1096                 KExtensionLength,
       
  1097                 KAknsSknExtension );
       
  1098             if ( !AknsSrvUtils::IsFile( iFsSession, bufferPtr ) )
       
  1099                 {
       
  1100                 // There is no matching .skn-file, do not restore the graphics file.
       
  1101                 fileErr = KErrNotFound;
       
  1102                 }
       
  1103 
       
  1104             // switch back the graphics file extension.
       
  1105             bufferPtr.Replace(
       
  1106                 bufferPtr.Length() - KExtensionLength,
       
  1107                 KExtensionLength,
       
  1108                 fileName.Ext() );
       
  1109             }
       
  1110         else
       
  1111             {
       
  1112             AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::RestoreFileInPrivateDirectoryL parse error2" );
       
  1113             parseError = ETrue;
       
  1114             }
       
  1115         }
       
  1116     else
       
  1117         {
       
  1118         AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::RestoreFileInPrivateDirectoryL parse error3" );
       
  1119         parseError = ETrue;
       
  1120         }
       
  1121 
       
  1122     if( !parseError && fileErr == KErrNone )
       
  1123         {
       
  1124         if ( AknsSrvUtils::IsFile( iFsSession, bufferPtr ) )
       
  1125             {
       
  1126             // Do not restore, if the file already exists.
       
  1127             fileErr = KErrAlreadyExists;
       
  1128             }
       
  1129         else
       
  1130             {
       
  1131             // Create the file.
       
  1132             fileErr = iFile.Replace(
       
  1133                 iFsSession,
       
  1134                 bufferPtr,
       
  1135                 ( EFileWrite | EFileShareExclusive | EFileStream ) );
       
  1136             }
       
  1137         }
       
  1138 
       
  1139     AKNS_TRACE_DEBUG1("CAknsSrvActiveBackupDataClient::RestoreFileInPrivateDirectoryL parse ret=%d", fileErr );
       
  1140     if ( fileErr != KErrNone )
       
  1141         {
       
  1142         // If error creating or parsing the file, skip to next one.
       
  1143         return fileErr;
       
  1144         }
       
  1145     if ( parseError )
       
  1146         {
       
  1147         return KErrArgument;
       
  1148         }
       
  1149     return KErrNone;
       
  1150     }
       
  1151 
       
  1152 // ---------------------------------------------------------------------------
       
  1153 // C++ constructor.
       
  1154 // ---------------------------------------------------------------------------
       
  1155 //
       
  1156 CAknsSrvActiveBackupDataClient::CAknsSrvActiveBackupDataClient(
       
  1157     RFs& aFsSession ) : iFsSession( aFsSession )
       
  1158     {
       
  1159     }
       
  1160 
       
  1161 // ---------------------------------------------------------------------------
       
  1162 // 2nd phase constructor.
       
  1163 // ---------------------------------------------------------------------------
       
  1164 //
       
  1165 void CAknsSrvActiveBackupDataClient::ConstructL()
       
  1166     {
       
  1167     AKNS_TRACE_DEBUG("CAknsSrvActiveBackupDataClient::ConstructL" );
       
  1168     iBuffer=HBufC::NewL( KMaxHeaderSize );
       
  1169     }
       
  1170 
       
  1171 // ---------------------------------------------------------------------------
       
  1172 // Do initialise get backup data.
       
  1173 // ---------------------------------------------------------------------------
       
  1174 //
       
  1175 void CAknsSrvActiveBackupDataClient::DoInitialiseGetBackupDataL(
       
  1176     TDriveNumber aDrive )
       
  1177     {
       
  1178     // this is the index of the file being processed - point to the beginning
       
  1179     iFileIndex=0;
       
  1180     // the first state of the backup state machine
       
  1181     iBackupState=EBackupNoFileOpen;
       
  1182 
       
  1183     TFileName path;
       
  1184     TChar driveLetter;
       
  1185     RFs::DriveToChar( aDrive, driveLetter );
       
  1186     path.Append( driveLetter );
       
  1187     path.Append( KSkinRootDir );
       
  1188     // Store the directories and filenames.
       
  1189     ScanDirectoryForSkinFilesL( path );
       
  1190     }
       
  1191 
       
  1192 // End of file