filemanager/src/fmbkupengine/src/CMMCScBkupStateValidateDiskSpace.cpp
branchRCL_3
changeset 20 491b3ed49290
equal deleted inserted replaced
19:95243422089a 20:491b3ed49290
       
     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: CMMCScBkupStateValidateDiskSpace implementation
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 #include "CMMCScBkupStateValidateDiskSpace.h"
       
    20 
       
    21 // System includes
       
    22 #include <e32std.h>
       
    23 #include <s32strm.h>
       
    24 #include <s32mem.h>
       
    25 
       
    26 // User includes
       
    27 #include "MMCScBkupLogger.h"
       
    28 #include "CMMCScBkupFileInfo.h"
       
    29 #include "CMMCScBkupArchive.h"
       
    30 #include "CMMCScBkupArchiveFooter.h"
       
    31 #include "CMMCScBkupArchiveHeader.h"
       
    32 #include "CMMCScBkupDataOwnerInfo.h"
       
    33 #include "RMMCScBkupProgressSizer.h"
       
    34 #include "MMMCScBkupProgressObserver.h"
       
    35 #include "CMMCScBkupFileListCollection.h"
       
    36 #include "CMMCScBkupDataOwnerCollection.h"
       
    37 #include "CMMCScBkupIndexPublicDataFiles.h"
       
    38 #include "CMMCScBkupDataStrategies.h"
       
    39 #include "MMCScBkupSBEUtils.h"
       
    40 
       
    41 // ========================= MEMBER FUNCTIONS ================================
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CMMCScBkupStateValidateDiskSpace::~CMMCScBkupStateValidateDiskSpace()
       
    45 // 
       
    46 // Destructor.
       
    47 // ---------------------------------------------------------------------------
       
    48 CMMCScBkupStateValidateDiskSpace::~CMMCScBkupStateValidateDiskSpace()
       
    49     {
       
    50     iDriveSizes.Close();
       
    51     iDriveMaxFileSizes.Close();
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CMMCScBkupStateValidateDiskSpace::CMMCScBkupStateValidateDiskSpace()
       
    56 // 
       
    57 // C++ constructor.
       
    58 // ---------------------------------------------------------------------------
       
    59 CMMCScBkupStateValidateDiskSpace::CMMCScBkupStateValidateDiskSpace( MMMCScBkupDriver& aDriver )
       
    60 :   CMMCScBkupState( aDriver ), iCumulatedSize(0)
       
    61     {
       
    62     __LOG1("CMMCScBkupStateValidateDiskSpace::CMMCScBkupStateValidateDiskSpace() - 0x%08x", StateId().iUid );
       
    63     }
       
    64 
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CMMCScBkupStateValidateDiskSpace::NewL()
       
    68 // 
       
    69 // 
       
    70 // ---------------------------------------------------------------------------
       
    71 CMMCScBkupStateValidateDiskSpace* CMMCScBkupStateValidateDiskSpace::NewL( MMMCScBkupDriver& aDriver )
       
    72     {
       
    73     CMMCScBkupStateValidateDiskSpace* self = new(ELeave) CMMCScBkupStateValidateDiskSpace( aDriver );
       
    74     CleanupStack::PushL(self);
       
    75     self->ConstructL();
       
    76     CleanupStack::Pop();
       
    77     return self;
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // CMMCScBkupStateValidateDiskSpace::ConstructL()
       
    82 // 
       
    83 // 
       
    84 // ---------------------------------------------------------------------------
       
    85 void CMMCScBkupStateValidateDiskSpace::ConstructL( )
       
    86     {
       
    87     for( TInt i = EDriveA; i<=EDriveZ; i++ )
       
    88         {
       
    89         const TDriveNumber drive = static_cast< TDriveNumber >( i );
       
    90         
       
    91         // Zero-initialize max. file sizing info arrays
       
    92         TMMCScBkupDriveAndSize newEntry( drive, 0 );
       
    93         iDriveSizes.AppendL( newEntry );
       
    94         TMMCScBkupDriveAndSize maxEntry( drive, 0 );
       
    95         iDriveMaxFileSizes.AppendL( maxEntry );
       
    96         }
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // CMMCScBkupStateValidateDiskSpace::StateId()
       
   101 // 
       
   102 // 
       
   103 // ---------------------------------------------------------------------------
       
   104 TMMCScBkupStateId CMMCScBkupStateValidateDiskSpace::StateId() const
       
   105     {
       
   106     return KMMCScBkupStateIdValidateDiskSpace;
       
   107     }
       
   108 
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // CMMCScBkupStateValidateDiskSpace::NextStateId()
       
   112 // 
       
   113 // 
       
   114 // ---------------------------------------------------------------------------
       
   115 TMMCScBkupStateId CMMCScBkupStateValidateDiskSpace::NextStateId() const
       
   116     {
       
   117     TMMCScBkupStateId nextState = KMMCScBkupStateIdOperationComplete;
       
   118     //
       
   119     const TMMCScBkupOperationType type = Driver().DrvOperation();
       
   120     switch(type)
       
   121         {
       
   122     case EMMCScBkupOperationTypeFullBackup:
       
   123         nextState = KMMCScBkupStateIdArchiveOpPublicDataFiles;
       
   124         break;
       
   125     case EMMCScBkupOperationTypeFullRestore:
       
   126         nextState = KMMCScBkupStateIdSetPhoneMode;
       
   127         break;
       
   128     default:
       
   129         ASSERT( EFalse );
       
   130         break;
       
   131         }
       
   132     //
       
   133     return nextState;
       
   134     }
       
   135 
       
   136 
       
   137 // ---------------------------------------------------------------------------
       
   138 // CMMCScBkupStateValidateDiskSpace::PerformStateInitL()
       
   139 // 
       
   140 // 
       
   141 // ---------------------------------------------------------------------------
       
   142 void CMMCScBkupStateValidateDiskSpace::PerformStateInitL()
       
   143     {
       
   144     CompleteSelf();
       
   145     }
       
   146 
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // CMMCScBkupStateValidateDiskSpace::PerformAsynchronousStateStepL()
       
   150 // 
       
   151 // 
       
   152 // ---------------------------------------------------------------------------
       
   153 void CMMCScBkupStateValidateDiskSpace::PerformAsynchronousStateStepL()
       
   154     {
       
   155     const TMMCScBkupOperationType type = Driver().DrvOperation();
       
   156 
       
   157     switch(type)
       
   158         {
       
   159     case EMMCScBkupOperationTypeFullBackup:
       
   160         {
       
   161         ValidateFreeSpaceBeforeBackupL();
       
   162         break;
       
   163         }
       
   164     case EMMCScBkupOperationTypeFullRestore:
       
   165         {
       
   166         ValidateFreeSpaceBeforeRestoreL();
       
   167         break;
       
   168         }
       
   169     default:
       
   170         User::Leave(KErrNotSupported);
       
   171         break;
       
   172         }
       
   173     }
       
   174 
       
   175 
       
   176 // ---------------------------------------------------------------------------
       
   177 // CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeBackupL()
       
   178 // 
       
   179 // 
       
   180 // ---------------------------------------------------------------------------
       
   181 void CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeBackupL()
       
   182     {
       
   183     CMMCScBkupDataOwnerCollection& dataOwners = Driver().DrvDataOwners();
       
   184     TInt64 uncompressedSize;
       
   185 
       
   186     __LOG("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeBackupL() - START");
       
   187     
       
   188     // Calculate the total progress required for the entire backup operation
       
   189     uncompressedSize = dataOwners.TotalOperationalSizeL();
       
   190     __LOG1("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeBackupL() - Total estimated uncompressed size for archive %Ld", uncompressedSize);
       
   191 	//const TInt ownerCount = dataOwners.Count();
       
   192     
       
   193     //for( TInt i=0; i<ownerCount; i++ )
       
   194     //    {
       
   195     //    CMMCScBkupDataOwnerInfo& owner = dataOwners.Owner( i );
       
   196         // Reset the size of public data to 0. We update this value
       
   197         // with the compressed size during the compression stage. This
       
   198         // is needed in order to create accurate restore information (since
       
   199         // all of the data owner sizing info is written to disk).
       
   200         // owner.ResetOperationalSize( EMMCScBkupOwnerDataTypePublicData );
       
   201     //    }
       
   202               
       
   203     // Let's check do public files fit in target drive
       
   204     TInt driveNumber;
       
   205     TDriveInfo driveInfo;
       
   206     TVolumeInfo volInfo;
       
   207     TInt8 percentFree(100);
       
   208     
       
   209     // First get the drive number, where archive resides and then get drive's free space.
       
   210     TInt err = ADI().ADIRawArchiveFile().Drive( driveNumber, driveInfo );
       
   211     if( err == KErrNone)
       
   212         {
       
   213         err = ADI().ADIFsSession().Volume( volInfo, driveNumber );
       
   214         if( err == KErrNone)
       
   215             {
       
   216             iCumulatedSize += uncompressedSize;
       
   217             
       
   218             if(Driver().DrvLastCategory())
       
   219                 {
       
   220                 __LOG3("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeBackupL() - Final cumulated size %Ld, free size on drive %c is %Ld", 
       
   221                     iCumulatedSize, driveNumber + 'A', volInfo.iFree);
       
   222                 
       
   223                 if( iCumulatedSize > volInfo.iFree)
       
   224                     {
       
   225                     percentFree = TInt( (volInfo.iFree  * 100 ) / iCumulatedSize ); // accurate enough, no rounding
       
   226                     }
       
   227                     
       
   228                 err = Driver().DrvProgressHandler().MMCScBkupHandleFreeSpace( percentFree );
       
   229                 if( err != KErrNone )
       
   230                     {
       
   231                     // Error indicates backup creation no more allowed
       
   232                     Driver().DrvProgressHandler().MMCScBkupStartBackuping( EFalse );
       
   233                     User::Leave( err );
       
   234                     }
       
   235                 else
       
   236                     {
       
   237                     Driver().DrvProgressHandler().MMCScBkupStartBackuping( ETrue );
       
   238                     }
       
   239                 }
       
   240             else
       
   241                 {
       
   242                 __LOG3("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeBackupL() - Cumulated size now %Ld, free size on drive %c is %Ld", 
       
   243                     iCumulatedSize, driveNumber + 'A', volInfo.iFree);
       
   244                 }
       
   245             }
       
   246         else
       
   247             {
       
   248             __LOG1("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeBackupL() - Volume info error %d", err);
       
   249             }
       
   250         }
       
   251     
       
   252     __LOG("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeBackupL() - END");
       
   253     }
       
   254 
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL()
       
   258 // 
       
   259 // 
       
   260 // ---------------------------------------------------------------------------
       
   261 void CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL()
       
   262     {
       
   263     CMMCScBkupArchiveFooter& footer = Driver().DrvArchive().Footer();
       
   264     CMMCScBkupIndexPublicDataFiles& index = static_cast<CMMCScBkupIndexPublicDataFiles&>( footer.IndexByType( EMMCScBkupOwnerDataTypePublicData ) );
       
   265     CMMCScBkupDataOwnerCollection& dataOwners = Driver().DrvDataOwners();
       
   266     CMMCScBkupFileListCollection& fileList = Driver().DrvFileList();
       
   267     TInt indexValueCurrent(0);
       
   268     
       
   269     __LOG("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - START");
       
   270     
       
   271     const TInt ownerCount = dataOwners.Count();
       
   272     
       
   273     // We can only make a direct comparison for public data
       
   274     for( TInt i=0; i<ownerCount; i++ )
       
   275         {
       
   276         CMMCScBkupDataOwnerInfo& owner = dataOwners.Owner( i );
       
   277         const CDataOwnerInfo& sbeDataOwner = owner.Owner();
       
   278 
       
   279         if  ( MMCScBkupSBEUtils::HasPublicDataL( sbeDataOwner ) )
       
   280             {
       
   281             for( TInt i = EDriveA; i<=EDriveZ; i++ )
       
   282                 {
       
   283                 const TDriveNumber drive = static_cast< TDriveNumber >( i );
       
   284                 
       
   285                 const TInt64 size = owner.OperationalSize( EMMCScBkupOwnerDataTypePublicData, drive );
       
   286                 TMMCScBkupDriveAndSize& entry = iDriveSizes[ drive ];
       
   287                 entry.SetSize(entry.Size() + size);
       
   288                 __LOG4("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - DO: 0x%08x has %Ld bytes for drive %c (total %Ld)", 
       
   289                     owner.SecureId().iId, size, entry.Drive() + 'A', entry.Size());
       
   290                 }
       
   291             }
       
   292         }
       
   293     
       
   294 #if defined(__MMCSCBKUPLOGGING_ENABLED__)
       
   295     for( TInt i = EDriveA; i<=EDriveZ; i++ )
       
   296         {
       
   297         TMMCScBkupDriveAndSize& entry = iDriveSizes[ i ];
       
   298         __LOG2("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - Initially %Ld bytes required on drive %c", 
       
   299             entry.Size(), entry.Drive() + 'A');
       
   300         }
       
   301 #endif
       
   302     
       
   303     const TInt count = index.Count();
       
   304 
       
   305     __LOG1("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - Public files to compare %d", count);
       
   306     
       
   307     // Check needed size by comparing archived public files to files in disk - decrease size if matching file found
       
   308     while( ++indexValueCurrent < count )
       
   309         {
       
   310         // Get the entry to restore
       
   311         TMMCScBkupArchiveVector entryInfo;
       
   312         //const CMMCScBkupFileInfo& fileInfo = index.At( indexValueCurrent, entryInfo );
       
   313         const CMMCScBkupFileInfo& fileInfo = fileList.Entry(indexValueCurrent);
       
   314         const TDriveNumber drive = fileInfo.Drive();
       
   315 
       
   316         __LOG2("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - Archived file: %S, of size %d", &fileInfo.FileName(), fileInfo.Size());
       
   317 
       
   318         // Check file (if any) size in disk
       
   319         RFile64 restoreFile;
       
   320         TInt64 fileSize = 0;
       
   321         TInt error = restoreFile.Open(ADI().ADIFsSession(), fileInfo.FileName(), EFileShareReadersOnly | EFileRead);
       
   322         if(error == KErrNone)
       
   323             {
       
   324             error = restoreFile.Size(fileSize);
       
   325             if(error == KErrNone)
       
   326                 {
       
   327                 __LOG1("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - Found matching file of size %d", fileInfo.Size());
       
   328                 
       
   329                 // Take into account biggest file already on disk, because reserves that much space temporarily
       
   330                 if(iDriveMaxFileSizes[ drive ].Size() < fileSize && KMMCScBkupUseTempFile)
       
   331                     {
       
   332                     TMMCScBkupDriveAndSize& bigFile = iDriveMaxFileSizes[ drive ];
       
   333                     bigFile.SetSize(fileSize);
       
   334                     }
       
   335                 }
       
   336             else
       
   337                 {
       
   338                 fileSize = 0;
       
   339                 __LOG1("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - Error % requesting file size", error);
       
   340                 }
       
   341             
       
   342             restoreFile.Close();
       
   343             }
       
   344             
       
   345         TMMCScBkupDriveAndSize& entry = iDriveSizes[ drive ];
       
   346         entry.SetSize(entry.Size() - fileSize);
       
   347         
       
   348         __LOG2("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - %Ld bytes required now on drive %c", entry.Size(), entry.Drive() + 'A');
       
   349         }
       
   350     
       
   351     __LOG("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - No estimation for private and system files");
       
   352     
       
   353     if(Driver().DrvLastCategory())
       
   354         {
       
   355         TVolumeInfo volInfo;
       
   356         TInt spaceCheck(KErrNone);
       
   357 
       
   358         // Let user interrupt restore if necessary in future. Currently if any of drives cannot be restored, we interrupt restore.
       
   359         for( TInt i = EDriveA; i<=EDriveZ; i++ )
       
   360             {
       
   361             // Check whether data fits into this drive.
       
   362             const TDriveNumber drive = static_cast< TDriveNumber >( i );
       
   363                     
       
   364             TInt err = ADI().ADIFsSession().Volume( volInfo, drive );
       
   365             if( err == KErrNone)
       
   366                 {
       
   367                 // Total compressed size. If this exceeds total size of drive, then restore does not succeed for sure.
       
   368                 const TInt64 size = dataOwners.DiskSpaceRequiredForRestore( drive ); 
       
   369                 __LOG3("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - drive %c requires %Ld + %Ld for temporary file", 
       
   370                     iDriveSizes[ drive ].Drive() + 'A', iDriveSizes[ drive ].Size(), iDriveMaxFileSizes[ drive ].Size());
       
   371                 
       
   372                 if  ( (( iDriveSizes[ drive ].Size() + iDriveMaxFileSizes[ drive ].Size() ) > volInfo.iFree ) ||
       
   373                        ( size > volInfo.iSize ) )
       
   374                     {
       
   375                     __LOG2("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - Not enough space (%Ld) on drive %c", 
       
   376                         TInt(volInfo.iFree), iDriveSizes[ drive ].Drive() + 'A');
       
   377                     spaceCheck = KErrDiskFull;
       
   378                     break;
       
   379                     }
       
   380                 }
       
   381             }
       
   382         
       
   383         User::LeaveIfError(spaceCheck);
       
   384         }
       
   385     else
       
   386         {
       
   387         __LOG("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - Checking next category...");
       
   388         }
       
   389     
       
   390     __LOG("CMMCScBkupStateValidateDiskSpace::ValidateFreeSpaceBeforeRestoreL() - END");
       
   391     }
       
   392 
       
   393