filemanager/bkupengine/src/CMMCScBkupArchive.cpp
changeset 0 6a9f87576119
equal deleted inserted replaced
-1:000000000000 0:6a9f87576119
       
     1 /*
       
     2 * Copyright (c) 2005-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: CMMCScBkupArchive Implementation
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 #include "CMMCScBkupArchive.h"
       
    20 
       
    21 // System includes
       
    22 #include <bautils.h>
       
    23 
       
    24 // User includes
       
    25 #include "MMCScBkupLogger.h"
       
    26 #include "MMCScBkupArchiveUtils.h"
       
    27 #include "CMMCScBkupArchiveHeader.h"
       
    28 #include "CMMCScBkupArchiveFooter.h"
       
    29 #include "MMCScBkupPhoneModelUtils.h"
       
    30 #include "CMMCScBkupArchiveDataManager.h"
       
    31 
       
    32 
       
    33 
       
    34 // ========================= MEMBER FUNCTIONS ================================
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // CMMCScBkupArchive::CMMCScBkupArchive()
       
    38 // 
       
    39 // C++ constructor.
       
    40 // ---------------------------------------------------------------------------
       
    41 CMMCScBkupArchive::CMMCScBkupArchive( RFs& aFsSession, MMMCScBkupProgressObserver& aProgressManager, 
       
    42     MMMCScBkupDriver& aDriver, TBitFlags aCategory )
       
    43 :   iFsSession( aFsSession ), iProgressManager( aProgressManager ), iDriver( aDriver ), iCategory( aCategory )
       
    44     {
       
    45     }
       
    46 
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // CMMCScBkupArchive::~CMMCScBkupArchive()
       
    50 // 
       
    51 // Destructor.
       
    52 // ---------------------------------------------------------------------------
       
    53 CMMCScBkupArchive::~CMMCScBkupArchive()
       
    54     {
       
    55     Close( KErrNone );
       
    56     DeleteOldArchive();
       
    57     // Must do these last, as we may need to use the file name in order 
       
    58     // to delete any partially created or old archives.
       
    59     delete iArchiveFileName;
       
    60     iArchiveFileName = NULL;
       
    61 
       
    62     delete iOldArchiveFileName;
       
    63     iOldArchiveFileName = NULL;
       
    64     }
       
    65 
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CMMCScBkupArchive::NewL()
       
    69 // 
       
    70 // 
       
    71 // ---------------------------------------------------------------------------
       
    72 CMMCScBkupArchive* CMMCScBkupArchive::NewL( RFs& aFsSession, MMMCScBkupProgressObserver& aProgressManager, 
       
    73     MMMCScBkupDriver& aDriver, TBitFlags aCategory  )
       
    74     {
       
    75     __LOG("CMMCScBkupArchive::NewL() - START");
       
    76     CMMCScBkupArchive* self = new(ELeave) CMMCScBkupArchive( aFsSession, aProgressManager, aDriver, aCategory );
       
    77     __LOG("CMMCScBkupArchive::NewL() - END");
       
    78     return self;
       
    79     }
       
    80 
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // CMMCScBkupArchive::OpenForReadingL()
       
    84 // 
       
    85 // 
       
    86 // ---------------------------------------------------------------------------
       
    87 void CMMCScBkupArchive::OpenForReadingL( const TDesC& aName )
       
    88     {
       
    89     __LOG1("CMMCScBkupArchive::OpenForReadingL() - START - aName: %S", &aName);
       
    90 
       
    91     Close( KErrNone );
       
    92     iArchiveFileName = aName.AllocL();
       
    93     //
       
    94     const TInt error = iArchiveFile.Open(iFsSession, *iArchiveFileName, EFileShareReadersOnly | EFileStream | EFileRead);
       
    95     __LOG1("CMMCScBkupArchive::OpenForReadingL() - open error: %d", error);
       
    96     User::LeaveIfError(error);
       
    97     //
       
    98     PrepareObjectsL();
       
    99     SetMode(EModeReading);
       
   100     __LOG1("CMMCScBkupArchive::OpenForReadingL() - END - file handle: 0x%08x", iArchiveFile.SubSessionHandle());
       
   101     }
       
   102 
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // CMMCScBkupArchive::OpenForWritingL()
       
   106 // 
       
   107 // 
       
   108 // ---------------------------------------------------------------------------
       
   109 void CMMCScBkupArchive::OpenForWritingL( const TDesC& aName )
       
   110     {
       
   111     __LOG1("CMMCScBkupArchive::OpenForWritingL() - START - aName: %S", &aName);
       
   112 
       
   113     Close( KErrNone );
       
   114     iArchiveFileName = aName.AllocL();
       
   115 
       
   116     // Name old archive as .old until disk space validation is carried out.
       
   117     _LIT( KOldSuffix, "old" );
       
   118     const TChar dot('.');
       
   119     iOldArchiveFileName = HBufC::NewL(aName.Length() + KOldSuffix().Length() + 1);
       
   120     TPtr temp( iOldArchiveFileName->Des() );
       
   121     temp.Copy( aName );
       
   122     const TInt location = temp.LocateReverseF( dot ) + 1;
       
   123     TInt length = temp.Length() - location;
       
   124 
       
   125     if( location != KErrNotFound && length <= KOldSuffix().Length() )
       
   126         {
       
   127         temp.Delete( location, length );
       
   128         temp.Append( KOldSuffix().Ptr(), length );
       
   129         }
       
   130     else
       
   131         {
       
   132         temp.Append( dot );
       
   133         temp.Append( KOldSuffix().Ptr(), length );
       
   134         }
       
   135 
       
   136     // Ensure archive directory and attributes permit writing of file
       
   137     TInt error = PrepareToOverwrite(aName);
       
   138     __LOG1("CMMCScBkupArchive::OpenForWritingL() - prepare to over-write error: %d", error);
       
   139     User::LeaveIfError(error);
       
   140 
       
   141     // Open file for writing
       
   142     error = iArchiveFile.Replace(iFsSession, *iArchiveFileName, EFileShareExclusive | EFileStream | EFileWrite);
       
   143     __LOG1("CMMCScBkupArchive::OpenForWritingL() - replace error: %d", error);
       
   144     User::LeaveIfError(error);
       
   145     //
       
   146     PrepareObjectsL();
       
   147     SetMode(EModeWriting);
       
   148     __LOG1("CMMCScBkupArchive::OpenForWritingL() - END - file handle: 0x%08x", iArchiveFile.SubSessionHandle());
       
   149     }
       
   150 
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // CMMCScBkupArchive::RestoreOldArchive()
       
   154 // 
       
   155 // 
       
   156 // ---------------------------------------------------------------------------
       
   157 void CMMCScBkupArchive::RestoreOldArchive()
       
   158     {
       
   159     Close( KErrCancel );
       
   160     TInt err = iFsSession.Rename( *iOldArchiveFileName, *iArchiveFileName );
       
   161     
       
   162     __LOG2("CMMCScBkupArchive::RestoreOldArchive() - %S restore error: %d", iArchiveFileName, err);
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // CMMCScBkupArchive::DeleteOldArchive()
       
   167 // 
       
   168 // 
       
   169 // ---------------------------------------------------------------------------
       
   170 void CMMCScBkupArchive::DeleteOldArchive()
       
   171     {
       
   172     if( iOldArchiveFileName != NULL )
       
   173         {
       
   174         TInt err = iFsSession.Delete( *iOldArchiveFileName );
       
   175         
       
   176         __LOG2("CMMCScBkupArchive::DeleteOldArchive() - %S delete error: %d", iOldArchiveFileName, err);
       
   177         }
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // CMMCScBkupArchive::Close()
       
   182 // 
       
   183 // 
       
   184 // ---------------------------------------------------------------------------
       
   185 void CMMCScBkupArchive::Close( TInt aError )
       
   186     {
       
   187     __LOG1("CMMCScBkupArchive::Close() - aError: %d", aError);
       
   188 
       
   189     delete iHeader;
       
   190     iHeader = NULL;
       
   191     delete iFooter;
       
   192     iFooter = NULL;
       
   193     delete iDataManager;
       
   194     iDataManager = NULL;
       
   195     //
       
   196     if  ( aError != KErrNone && Mode() == EModeWriting )
       
   197         {
       
   198         // Delete the archive if there was an error with the backup operation
       
   199         __LOG1("CMMCScBkupArchive::Close() - ERROR CREATING BACKUP - aError: %d => archive will be deleted", aError);
       
   200 
       
   201         // Ignore error when attempting delete
       
   202         iArchiveFile.Close();
       
   203         (void) iFsSession.Delete( *iArchiveFileName );
       
   204         }
       
   205 
       
   206     // Close archive. It might have already been closed in an error situation
       
   207     // but that's okay - it doesn't cause any problems to close it twice.
       
   208     iArchiveFile.Close();
       
   209     
       
   210     // Ignore error if setting read-only attribute fails, because it is not fatal
       
   211     iFsSession.SetAtt( *iArchiveFileName, KEntryAttReadOnly, !KEntryAttReadOnly );
       
   212     
       
   213     // Reset mode back to default, ready for next operation
       
   214     iMode = EModeUninitialised;
       
   215 
       
   216     __LOG("CMMCScBkupArchive::Close() - END");
       
   217     }
       
   218 
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // CMMCScBkupArchive::Header()
       
   222 // 
       
   223 // 
       
   224 // ---------------------------------------------------------------------------
       
   225 CMMCScBkupArchiveHeader& CMMCScBkupArchive::Header() const
       
   226     {
       
   227     return *iHeader;
       
   228     }
       
   229 
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 // CMMCScBkupArchive::Footer()
       
   233 // 
       
   234 // 
       
   235 // ---------------------------------------------------------------------------
       
   236 CMMCScBkupArchiveFooter& CMMCScBkupArchive::Footer() const
       
   237     {
       
   238     return *iFooter;
       
   239     }
       
   240 
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 // CMMCScBkupArchive::ADI()
       
   244 // 
       
   245 // 
       
   246 // ---------------------------------------------------------------------------
       
   247 MMMCScBkupArchiveDataInterface& CMMCScBkupArchive::ADI() const
       
   248     {
       
   249     return *iDataManager;
       
   250     }
       
   251 
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 // CMMCScBkupArchive::PrepareToOverwrite()
       
   255 // 
       
   256 // 
       
   257 // ---------------------------------------------------------------------------
       
   258 TInt CMMCScBkupArchive::PrepareToOverwrite( const TDesC& aFile )
       
   259     {
       
   260     __LOG1("CMMCScBkupArchive::PrepareToOverwrite() - START - aFile: %S", &aFile);
       
   261     TInt err = KErrNone;
       
   262 
       
   263     // Try deleting file with temporary name (one we have created earlier,
       
   264     // but which has potentially remained for some reason).
       
   265     err = iFsSession.Delete( *iOldArchiveFileName );
       
   266     __LOG2("CMMCScBkupArchive::PrepareToOverwrite() - delete %S result: %d", iOldArchiveFileName, err);
       
   267     // Rename the file for possible later "restoration".
       
   268     err = iFsSession.Rename( aFile, *iOldArchiveFileName );
       
   269     __LOG1("CMMCScBkupArchive::PrepareToOverwrite() - rename existing file: %d", err);
       
   270     // Reset file flags in order to make sure file can be deleted
       
   271     err = iFsSession.SetAtt( *iOldArchiveFileName, KEntryAttNormal, !KEntryAttNormal );
       
   272     __LOG1("CMMCScBkupArchive::PrepareToOverwrite() - setFileAttNormal: %d", err);
       
   273 
       
   274     // Create the full path, if not exists
       
   275     err = iFsSession.MkDirAll( aFile );
       
   276     __LOG1("CMMCScBkupArchive::PrepareToOverwrite() - mkDirAll: %d", err);
       
   277 
       
   278     if  (err == KErrAlreadyExists || err == KErrNone )
       
   279         {
       
   280         // Set folder hidden, ignore error
       
   281         err = iFsSession.SetAtt( BaflUtils::DriveAndPathFromFullName( aFile ), KEntryAttHidden, !KEntryAttHidden );
       
   282         __LOG1("CMMCScBkupArchive::PrepareToOverwrite() - setFolderHidden: %d", err);
       
   283         
       
   284         // Reset file flags in order to make sure file can be deleted
       
   285         err = iFsSession.SetAtt( aFile, KEntryAttNormal, !KEntryAttNormal );
       
   286         __LOG1("CMMCScBkupArchive::PrepareToOverwrite() - setFileAttNormal: %d", err);
       
   287 
       
   288         }
       
   289     //
       
   290     if  ( err == KErrNotFound )
       
   291         {
       
   292         // These errors are ignored
       
   293         __LOG1("CMMCScBkupArchive::PrepareToOverwrite() - mapped %d to KErrNone -> everything is okay", err);
       
   294         err = KErrNone;
       
   295         }
       
   296     //
       
   297     __LOG1("CMMCScBkupArchive::PrepareToOverwrite() - END - err: %d", err);
       
   298     return err;
       
   299     }
       
   300 
       
   301 
       
   302 // ---------------------------------------------------------------------------
       
   303 // CMMCScBkupArchive::ValidArchiveForRestoreL()
       
   304 // 
       
   305 // 
       
   306 // ---------------------------------------------------------------------------
       
   307 TBool CMMCScBkupArchive::ValidArchiveForRestoreL( RFs& aFsSession, const TDesC& aFileName )
       
   308     {
       
   309     HBufC8* modelInfo = NULL;
       
   310     TBitFlags archiveFlags( 0 );
       
   311 
       
   312     TVersion archiveVersion;
       
   313 
       
   314     // Read the header from the specified archive
       
   315     MMCScBkupArchiveUtils::ReadPhoneValidityInformationL( aFsSession, aFileName, modelInfo, archiveFlags, archiveVersion );
       
   316     CleanupStack::PushL( modelInfo );
       
   317 
       
   318     // Check whether its okay to restore the archive.
       
   319     const TBool validArchiveForRestore = MMCScBkupPhoneModelUtils::ArchiveRestorePermissableL( *modelInfo, archiveFlags, archiveVersion );
       
   320     CleanupStack::PopAndDestroy(modelInfo);
       
   321     //
       
   322     return validArchiveForRestore;
       
   323     }
       
   324 
       
   325 
       
   326 
       
   327 // ---------------------------------------------------------------------------
       
   328 // CMMCScBkupArchive::SetMode()
       
   329 // 
       
   330 // 
       
   331 // ---------------------------------------------------------------------------
       
   332 void CMMCScBkupArchive::SetMode(TMode aMode)
       
   333     {
       
   334     iMode = aMode;
       
   335     }
       
   336 
       
   337 
       
   338 // ---------------------------------------------------------------------------
       
   339 // CMMCScBkupArchive::PrepareObjectsL()
       
   340 // 
       
   341 // 
       
   342 // ---------------------------------------------------------------------------
       
   343 void CMMCScBkupArchive::PrepareObjectsL()
       
   344     {
       
   345     __LOG("CMMCScBkupArchive::PrepareObjectsL() - START");
       
   346 
       
   347     __LOG("CMMCScBkupArchive::PrepareObjectsL() - creating data manager...");
       
   348     iDataManager = CMMCScBkupArchiveDataManager::NewL( iFsSession, iArchiveFile, iProgressManager );
       
   349 
       
   350     __LOG("CMMCScBkupArchive::PrepareObjectsL() - creating header...");
       
   351     iHeader = CMMCScBkupArchiveHeader::NewL( *iDataManager, iDriver );
       
   352 
       
   353     __LOG("CMMCScBkupArchive::PrepareObjectsL() - creating footer...");
       
   354     iFooter = CMMCScBkupArchiveFooter::NewL( *iDataManager, iDriver );
       
   355 
       
   356     __LOG("CMMCScBkupArchive::PrepareObjectsL() - END");
       
   357     }
       
   358 
       
   359 
       
   360 
       
   361 
       
   362 
       
   363 
       
   364