filemanager/bkupengine/src/CMMCScBkupDataStrategies.cpp
branchRCL_3
changeset 38 491b3ed49290
parent 36 95243422089a
child 39 65326cf895ed
equal deleted inserted replaced
36:95243422089a 38:491b3ed49290
     1 /*
       
     2 * Copyright (c) 2005 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: CMMCScBkupDataStrategy implementation
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 #include "CMMCScBkupDataStrategies.h"
       
    20 
       
    21 // User includes
       
    22 #include "MMCScBkupConfig.h"
       
    23 #include "MMCScBkupLogger.h"
       
    24 
       
    25 
       
    26 
       
    27 
       
    28 // ========================= MEMBER FUNCTIONS ================================
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // CMMCScBkupDataStrategy::CMMCScBkupDataStrategy()
       
    32 // 
       
    33 // C++ constructor.
       
    34 // ---------------------------------------------------------------------------
       
    35 CMMCScBkupDataStrategy::CMMCScBkupDataStrategy()
       
    36     {
       
    37     }
       
    38 
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // CMMCScBkupDataStrategy::ReadStrategyLC()
       
    42 // 
       
    43 // Factory function
       
    44 // ---------------------------------------------------------------------------
       
    45 CMMCScBkupDataStrategy* CMMCScBkupDataStrategy::ReadStrategyLC(const TDesC& aName, RFs& aFsSession)
       
    46     {
       
    47     // Fetch the size
       
    48     TEntry entry;
       
    49     const TInt error = aFsSession.Entry(aName, entry);
       
    50     User::LeaveIfError(error);
       
    51 
       
    52     // Construct
       
    53     return CMMCScBkupDataStrategy::ReadStrategyLC(aName, aFsSession, 0, entry.iSize);
       
    54     }
       
    55 
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // CMMCScBkupDataStrategy::ReadStrategyLC()
       
    59 // 
       
    60 // Factory function
       
    61 // ---------------------------------------------------------------------------
       
    62 CMMCScBkupDataStrategy* CMMCScBkupDataStrategy::ReadStrategyLC(const TDesC& aName, RFs& aFsSession, TInt aOffset, TInt aLength)
       
    63     {
       
    64     CMMCScBkupDataStrategy* ret = NULL;
       
    65 
       
    66     // First, try a normal read strategy
       
    67     TRAPD(err, ret = CMMCScBkupDataFileStrategy::NewForReadingL(aName, aFsSession));
       
    68     if  (err != KErrNone)
       
    69         {
       
    70         // Try the file section read strategy - don't trap this - if it fails
       
    71         // then this file cannot be read...
       
    72         ret = CMMCScBkupDataFileSectionReadStrategy::NewL(aName, aFsSession);
       
    73         }
       
    74     //
       
    75     if  (ret)
       
    76         {
       
    77         ret->SetOffsetAndLength(aOffset, aLength);
       
    78         }
       
    79     //
       
    80     CleanupStack::PushL( ret );
       
    81     return ret;
       
    82     }
       
    83 
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // CMMCScBkupDataStrategy::WriteStrategyLC()
       
    87 // 
       
    88 // Factory function
       
    89 // ---------------------------------------------------------------------------
       
    90 CMMCScBkupDataStrategy* CMMCScBkupDataStrategy::WriteStrategyLC( const TDesC& aName, RFs& aFsSession, TBool aUseTempFile )
       
    91     {
       
    92     CMMCScBkupDataStrategy* ret = CMMCScBkupDataFileStrategy::NewForWritingL( aName, aFsSession, aUseTempFile );
       
    93     CleanupStack::PushL(ret);
       
    94     return ret;
       
    95     }
       
    96 
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // CMMCScBkupDataStrategy::Write()
       
   100 // 
       
   101 // 
       
   102 // ---------------------------------------------------------------------------
       
   103 TInt CMMCScBkupDataStrategy::Write(const TDesC8& /*aSource*/)
       
   104     {
       
   105     return KErrNotSupported;
       
   106     }
       
   107 
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // CMMCScBkupDataStrategy::Finalize()
       
   111 // 
       
   112 // 
       
   113 // ---------------------------------------------------------------------------
       
   114 TInt CMMCScBkupDataStrategy::Finalize()
       
   115     {
       
   116     return KErrNone;
       
   117     }
       
   118     
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // CMMCScBkupDataStrategy::SetOffsetAndLength()
       
   122 // 
       
   123 // 
       
   124 // ---------------------------------------------------------------------------
       
   125 void CMMCScBkupDataStrategy::SetOffsetAndLength(TInt aStartOffset, TInt aLengthToRead)
       
   126     {
       
   127     iOffset = aStartOffset; 
       
   128     iLengthToRead = aLengthToRead;
       
   129     }
       
   130 
       
   131 
       
   132 
       
   133 
       
   134 
       
   135 
       
   136 
       
   137 
       
   138 
       
   139 
       
   140 
       
   141 
       
   142 
       
   143 
       
   144 
       
   145 
       
   146 
       
   147 
       
   148 
       
   149 
       
   150 
       
   151 
       
   152 
       
   153 
       
   154 
       
   155 
       
   156 
       
   157 
       
   158 
       
   159 
       
   160 
       
   161 
       
   162 
       
   163 
       
   164 
       
   165 
       
   166 // ========================= MEMBER FUNCTIONS ================================
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // CMMCScBkupDataFileStrategy::CMMCScBkupDataFileStrategy()
       
   170 // 
       
   171 // C++ constructor.
       
   172 // ---------------------------------------------------------------------------
       
   173 CMMCScBkupDataFileStrategy::CMMCScBkupDataFileStrategy( RFs& aFsSession, TBool aUseTempFile )
       
   174 :   iFsSession( aFsSession ), iUseTempFile( aUseTempFile )
       
   175     {
       
   176     }
       
   177 
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // CMMCScBkupDataFileStrategy::~CMMCScBkupDataFileStrategy()
       
   181 // 
       
   182 // C++ destructor.
       
   183 // ---------------------------------------------------------------------------
       
   184 CMMCScBkupDataFileStrategy::~CMMCScBkupDataFileStrategy()
       
   185     {
       
   186     iFile.Close();
       
   187     delete iFileName;
       
   188     }
       
   189 
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // CMMCScBkupDataFileStrategy::ConstructL()
       
   193 // 
       
   194 // 
       
   195 // ---------------------------------------------------------------------------
       
   196 void CMMCScBkupDataFileStrategy::ConstructL( const TDesC& aName, TUint aMode )
       
   197     {
       
   198     iFileName = aName.AllocL();
       
   199     
       
   200     // The previous version of this engine would attempt to use the old
       
   201     // backup & restore API to close this file. 
       
   202     //
       
   203     // With the new Symbian Secure Backup Architecture, data owners are expected
       
   204     // to release file locks on public files as soon as they receive
       
   205     // notification that a backup is beginning.
       
   206     TInt error = KErrNone;
       
   207     //
       
   208     if  ( iUseTempFile )
       
   209         {
       
   210         // Ensure path for temporary files exists on same drive where file will eventually
       
   211         // be stored. This is important, because we estimate available size on target drive
       
   212         // and so also temporary file size affects whether restore can be carried out.
       
   213         HBufC* tempPath = HBufC::NewLC( KMaxFileName );
       
   214         TPtr pTempPath( tempPath->Des() );
       
   215         pTempPath.Zero();
       
   216         if(aName.Length())
       
   217             {
       
   218             pTempPath.Append( aName[0] );
       
   219             }
       
   220         else
       
   221             {
       
   222             const TDriveUnit driveUnit( KMMCScBkupTempFileDrive );
       
   223             const TDriveName drive( driveUnit.Name() );
       
   224             pTempPath.Append( drive );
       
   225             }
       
   226         pTempPath.Append( KMMCScBkupTempDir );
       
   227 
       
   228         error = iFsSession.MkDirAll( pTempPath );
       
   229         if  ( ! (error == KErrNone || error == KErrAlreadyExists) )
       
   230             {
       
   231             User::LeaveIfError( error );
       
   232             }
       
   233             
       
   234         // Make a temporary file
       
   235         error = iFile.Temp( iFsSession, pTempPath, iTempFileName, aMode );
       
   236         __LOG2("CMMCScBkupDataFileStrategy::ConstructL() - Created temporary file: %S, error code: %d", &iTempFileName, error);
       
   237         
       
   238         // Clean up
       
   239         CleanupStack::PopAndDestroy( tempPath );
       
   240         }
       
   241     else
       
   242         {
       
   243         if( aMode & EFileWrite )
       
   244             {
       
   245             error = PrepareToOverwriteFile( iFsSession, *iFileName );
       
   246 
       
   247             if  ( error == KErrNone )
       
   248                 {
       
   249                 error = iFile.Create( iFsSession, *iFileName, aMode );
       
   250                 __LOG2("CMMCScBkupDataFileStrategy::ConstructL() - Created file %S, error code: %d", iFileName, error);
       
   251                 }
       
   252             }
       
   253         else
       
   254             {
       
   255             error = iFile.Open( iFsSession, aName, aMode );
       
   256             }
       
   257         }
       
   258     //
       
   259     User::LeaveIfError(error);
       
   260     }
       
   261 
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // CMMCScBkupDataFileStrategy::NewForReadingL()
       
   265 // 
       
   266 // Factory function
       
   267 // ---------------------------------------------------------------------------
       
   268 CMMCScBkupDataFileStrategy* CMMCScBkupDataFileStrategy::NewForReadingL( const TDesC& aName, RFs& aFsSession )
       
   269     {
       
   270     const TUint mode = EFileRead | EFileShareReadersOnly | EFileStream;
       
   271     //
       
   272     CMMCScBkupDataFileStrategy* self = new(ELeave) CMMCScBkupDataFileStrategy( aFsSession );
       
   273     CleanupStack::PushL(self);
       
   274     self->ConstructL( aName, mode );
       
   275     CleanupStack::Pop( self );
       
   276     return self;
       
   277     }
       
   278 
       
   279 
       
   280 // ---------------------------------------------------------------------------
       
   281 // CMMCScBkupDataFileStrategy::NewForWritingL()
       
   282 // 
       
   283 // Factory function
       
   284 // ---------------------------------------------------------------------------
       
   285 CMMCScBkupDataFileStrategy* CMMCScBkupDataFileStrategy::NewForWritingL( const TDesC& aName, RFs& aFsSession, TBool aUseTempFile )
       
   286     {
       
   287     const TUint mode = EFileWrite | EFileStream;
       
   288     //
       
   289     CMMCScBkupDataFileStrategy* self = new(ELeave) CMMCScBkupDataFileStrategy( aFsSession, aUseTempFile );
       
   290     CleanupStack::PushL(self);
       
   291     self->ConstructL( aName, mode );
       
   292     CleanupStack::Pop( self );
       
   293     return self;
       
   294     }
       
   295 
       
   296 
       
   297 // ---------------------------------------------------------------------------
       
   298 // CMMCScBkupDataFileStrategy::Read()
       
   299 // 
       
   300 //
       
   301 // ---------------------------------------------------------------------------
       
   302 TInt CMMCScBkupDataFileStrategy::Read(TDes8& aSink)
       
   303     {
       
   304     TInt error = KErrNone;
       
   305     //
       
   306     aSink.Zero();
       
   307     TInt amountToRead = (LengthToRead() - Offset());
       
   308     if  (amountToRead > aSink.MaxLength())
       
   309         {
       
   310         amountToRead = aSink.MaxLength();
       
   311         }
       
   312     //
       
   313     if  (amountToRead > 0)
       
   314         {
       
   315         // Do the read
       
   316         error = iFile.Read( static_cast<TInt64>(Offset()), aSink, amountToRead );
       
   317         if  (error == KErrNone)
       
   318             {
       
   319             SetOffset( Offset() + aSink.Length() );
       
   320             }
       
   321         }
       
   322     //
       
   323     return error;
       
   324     }
       
   325 
       
   326 
       
   327 // ---------------------------------------------------------------------------
       
   328 // CMMCScBkupDataFileStrategy::Write()
       
   329 // 
       
   330 //
       
   331 // ---------------------------------------------------------------------------
       
   332 TInt CMMCScBkupDataFileStrategy::Write(const TDesC8& aSource)
       
   333     {
       
   334     // Offset writing not supported (no requirement)
       
   335     const TInt error = iFile.Write( aSource );
       
   336     return error;
       
   337     }
       
   338 
       
   339 
       
   340 // ---------------------------------------------------------------------------
       
   341 // CMMCScBkupDataFileStrategy::Finalize()
       
   342 // 
       
   343 // 
       
   344 // ---------------------------------------------------------------------------
       
   345 TInt CMMCScBkupDataFileStrategy::Finalize()
       
   346     {
       
   347     TInt error = KErrNone;
       
   348     //
       
   349     if  ( iUseTempFile )
       
   350         {
       
   351         // Prepare for over-write
       
   352         error = PrepareToOverwriteFile( iFsSession, *iFileName );
       
   353         //
       
   354         if  ( error == KErrNone )
       
   355             {
       
   356             // Finally, do the rename
       
   357             error = iFile.Rename( *iFileName );
       
   358             __LOG2("CMMCScBkupDataFileStrategy::Finalize() - Renamed temporary file as: %S, error code: %d", iFileName, error);
       
   359             }
       
   360         }
       
   361 
       
   362     // Whatever the situation, we close the file now
       
   363     iFile.Close();
       
   364     return error;
       
   365     }
       
   366 
       
   367 
       
   368 // ---------------------------------------------------------------------------
       
   369 // CMMCScBkupDataFileStrategy::PrepareToOverwriteFile()
       
   370 // 
       
   371 // 
       
   372 // ---------------------------------------------------------------------------
       
   373 TInt CMMCScBkupDataFileStrategy::PrepareToOverwriteFile( RFs& aFsSession, const TDesC& aFileName )
       
   374     {
       
   375     TInt err = KErrNone;
       
   376 
       
   377     // Create the full path, if not exists
       
   378     err = aFsSession.MkDirAll( aFileName );
       
   379 
       
   380     if( err == KErrAlreadyExists || !err )
       
   381         {
       
   382         // Reset file flags
       
   383         err = aFsSession.SetAtt( aFileName, KEntryAttNormal, !KEntryAttNormal );
       
   384         }
       
   385 
       
   386     if( !err )
       
   387         {
       
   388         // Delete the file
       
   389         err = aFsSession.Delete( aFileName );
       
   390         }
       
   391 
       
   392     if( err == KErrNotFound )
       
   393         {
       
   394         // These errors are ignored
       
   395         err = KErrNone;
       
   396         }
       
   397 
       
   398     return err;
       
   399     }
       
   400 
       
   401 
       
   402 
       
   403 
       
   404 
       
   405 
       
   406 
       
   407 
       
   408 
       
   409 
       
   410 
       
   411 
       
   412 
       
   413 
       
   414 
       
   415 
       
   416 
       
   417 
       
   418 
       
   419 
       
   420 
       
   421 
       
   422 
       
   423 
       
   424 
       
   425 
       
   426 
       
   427 
       
   428 
       
   429 
       
   430 
       
   431 
       
   432 
       
   433 
       
   434 
       
   435 
       
   436 
       
   437 
       
   438 
       
   439 
       
   440 
       
   441 
       
   442 
       
   443 
       
   444 
       
   445 
       
   446 
       
   447 
       
   448 
       
   449 // ========================= MEMBER FUNCTIONS ================================
       
   450 
       
   451 // ---------------------------------------------------------------------------
       
   452 // CMMCScBkupDataFileSectionReadStrategy::CMMCScBkupDataFileSectionReadStrategy()
       
   453 // 
       
   454 // C++ constructor.
       
   455 // ---------------------------------------------------------------------------
       
   456 CMMCScBkupDataFileSectionReadStrategy::CMMCScBkupDataFileSectionReadStrategy(RFs& aFsSession)
       
   457 :   iFsSession(aFsSession)
       
   458     {
       
   459     }
       
   460 
       
   461 
       
   462 // ---------------------------------------------------------------------------
       
   463 // CMMCScBkupDataFileSectionReadStrategy::~CMMCScBkupDataFileSectionReadStrategy()
       
   464 // 
       
   465 // 
       
   466 // ---------------------------------------------------------------------------
       
   467 CMMCScBkupDataFileSectionReadStrategy::~CMMCScBkupDataFileSectionReadStrategy()
       
   468     {
       
   469     delete iFileName;
       
   470     }
       
   471 
       
   472 
       
   473 // ---------------------------------------------------------------------------
       
   474 // CMMCScBkupDataFileSectionReadStrategy::ConstructL()
       
   475 // 
       
   476 // 
       
   477 // ---------------------------------------------------------------------------
       
   478 void CMMCScBkupDataFileSectionReadStrategy::ConstructL(const TDesC& aName)
       
   479     {
       
   480     iFileName = aName.AllocL();
       
   481     }
       
   482 
       
   483 
       
   484 // ---------------------------------------------------------------------------
       
   485 // CMMCScBkupDataFileSectionReadStrategy::NewL()
       
   486 // 
       
   487 // 
       
   488 // ---------------------------------------------------------------------------
       
   489 CMMCScBkupDataFileSectionReadStrategy* CMMCScBkupDataFileSectionReadStrategy::NewL(const TDesC& aName, RFs& aFsSession)
       
   490     {
       
   491     CMMCScBkupDataFileSectionReadStrategy* self = new(ELeave) CMMCScBkupDataFileSectionReadStrategy(aFsSession);
       
   492     CleanupStack::PushL(self);
       
   493     self->ConstructL(aName);
       
   494     CleanupStack::Pop(self);
       
   495     return self;
       
   496     }
       
   497 
       
   498 
       
   499 
       
   500 // ---------------------------------------------------------------------------
       
   501 // CMMCScBkupDataFileSectionReadStrategy::Read()
       
   502 // 
       
   503 //
       
   504 // ---------------------------------------------------------------------------
       
   505 TInt CMMCScBkupDataFileSectionReadStrategy::Read(TDes8& aSink)
       
   506     {
       
   507     TInt error = KErrNone;
       
   508     //
       
   509     aSink.Zero();
       
   510     TInt amountToRead = (LengthToRead() - Offset());
       
   511     if  (amountToRead > aSink.MaxLength())
       
   512         {
       
   513         amountToRead = aSink.MaxLength();
       
   514         }
       
   515     //
       
   516     if  (amountToRead > 0)
       
   517         {
       
   518         // Do the read
       
   519         error = iFsSession.ReadFileSection( *iFileName, Offset(), aSink, amountToRead );
       
   520         if  (error == KErrNone)
       
   521             {
       
   522             SetOffset( Offset() + aSink.Length() );
       
   523             }
       
   524         }
       
   525     return error;
       
   526     }
       
   527 
       
   528 
       
   529 
       
   530 
       
   531 
       
   532 
       
   533 
       
   534 
       
   535 
       
   536 
       
   537 
       
   538 
       
   539 
       
   540 
       
   541 
       
   542