wmdrm/wmdrmengine/wmdrmserver/server/src/wmdrmdatastore.cpp
changeset 23 493788a4a8a4
parent 0 95b198f216e5
equal deleted inserted replaced
5:79d62d1d7957 23:493788a4a8a4
    18 
    18 
    19 #include <ezlib.h>
    19 #include <ezlib.h>
    20 #include <e32math.h>
    20 #include <e32math.h>
    21 #include <symmetric.h>
    21 #include <symmetric.h>
    22 #include <bacntf.h>
    22 #include <bacntf.h>
    23 #include <centralrepository.h> 
    23 #include <centralrepository.h>
    24 
    24 
    25 #include "wmdrmdatastore.h"
    25 #include "wmdrmdatastore.h"
    26 #include "wmdrmprivatecrkeys.h"
    26 #include "WMDRMPrivateCRKeys.h"
    27 #include "wmdrmkeystorage.h"
    27 #include "wmdrmkeystorage.h"
    28 #include "slotdatacache.h"
    28 #include "slotdatacache.h"
    29 #include "wmdrmdb.h"
    29 #include "wmdrmdb.h"
    30 #include "drmrightsstoringlocation.h"
    30 #include "drmrightsstoringlocation.h"
    31 #include "drmutilityinternaltypes.h"
    31 #include "drmutilityinternaltypes.h"
    54     self->ConstructL();
    54     self->ConstructL();
    55     CleanupStack::Pop( self );
    55     CleanupStack::Pop( self );
    56     return self;
    56     return self;
    57     }
    57     }
    58 
    58 
    59 CWmDrmDataStore::CWmDrmDataStore( CWmDrmServer* aServer ) 
    59 CWmDrmDataStore::CWmDrmDataStore( CWmDrmServer* aServer )
    60     : iServer( aServer ), iMinFreeSpace2( 0 )
    60     : iServer( aServer ), iMinFreeSpace2( 0 )
    61     {
    61     {
    62     LOGFN( "CWmDrmDataStore::CWmDrmDataStore" );
    62     LOGFN( "CWmDrmDataStore::CWmDrmDataStore" );
    63     }
    63     }
    64 
    64 
    65 void CWmDrmDataStore::ConstructL()
    65 void CWmDrmDataStore::ConstructL()
    66     {
    66     {
    67     TDrmScheme drmScheme( EDrmSchemeWmDrm );
    67     TDrmScheme drmScheme( EDrmSchemeWmDrm );
    68     TFileName tempFile, tempFile2;
    68     TFileName tempFile, tempFile2;
    69     TChar driveLetter;
    69     TChar driveLetter;
    70     
    70 
    71     LOGFN( "CWmDrmDataStore::ConstructL" );
    71     LOGFN( "CWmDrmDataStore::ConstructL" );
    72     
    72 
    73     // Check which drive is configured in the Central Repository Key
    73     // Check which drive is configured in the Central Repository Key
    74     // for the desired storing location of WM DRM rights (license store).
    74     // for the desired storing location of WM DRM rights (license store).
    75 	iWmDrmRightsConfigFound = DrmRightsStoringLocation::CheckDrmRightsStorageDriveL(
    75     iWmDrmRightsConfigFound = DrmRightsStoringLocation::CheckDrmRightsStorageDriveL(
    76 	    iServer->Fs(), drmScheme, driveLetter );
    76         iServer->Fs(), drmScheme, driveLetter );
    77     
    77 
    78     // If the storing location is configured to other drive than the default 
    78     // If the storing location is configured to other drive than the default
    79 	// system drive, prepare utility files for that drive, too.
    79     // system drive, prepare utility files for that drive, too.
    80     if ( iWmDrmRightsConfigFound )
    80     if ( iWmDrmRightsConfigFound )
    81         {
    81         {
    82         PrepareInfoFilesL( ETrue, driveLetter, iDummyDb2 );
    82         PrepareInfoFilesL( ETrue, driveLetter, iDummyDb2 );
    83         }
    83         }
    84     
    84 
    85     // Prepare the utility files for the default system drive
    85     // Prepare the utility files for the default system drive
    86     PrepareInfoFilesL( EFalse, (TUint)iServer->Fs().GetSystemDriveChar(), 
    86     PrepareInfoFilesL( EFalse, (TUint)iServer->Fs().GetSystemDriveChar(),
    87         iDummyDb );
    87         iDummyDb );
    88     }
    88     }
    89 
    89 
    90 CWmDrmDataStore::~CWmDrmDataStore()
    90 CWmDrmDataStore::~CWmDrmDataStore()
    91     {
    91     {
   109     CBufferedDecryptor* decryptor = NULL;
   109     CBufferedDecryptor* decryptor = NULL;
   110     CModeCBCDecryptor* cbcDecryptor = NULL;
   110     CModeCBCDecryptor* cbcDecryptor = NULL;
   111     CAESDecryptor* aesDecryptor = NULL;
   111     CAESDecryptor* aesDecryptor = NULL;
   112     CPaddingPKCS7* padding = NULL;
   112     CPaddingPKCS7* padding = NULL;
   113     TInt size = 0;
   113     TInt size = 0;
   114     
   114 
   115     LOGFN( "CWmDrmDataStore::ReadInitialFreeSpaceL" );
   115     LOGFN( "CWmDrmDataStore::ReadInitialFreeSpaceL" );
   116     User::LeaveIfError( file.Open( iServer->Fs(), aFileName, EFileRead ) );
   116     User::LeaveIfError( file.Open( iServer->Fs(), aFileName, EFileRead ) );
   117     CleanupClosePushL( file );
   117     CleanupClosePushL( file );
   118     
   118 
   119     User::LeaveIfError( file.Size( size ) );
   119     User::LeaveIfError( file.Size( size ) );
   120 	    
   120 
   121 	if( size != ( 2 * KAESKeyLength ) )
   121     if( size != ( 2 * KAESKeyLength ) )
   122 	    {
   122         {
   123 	    User::Leave(KErrCorrupt);
   123         User::Leave(KErrCorrupt);
   124 	    }
   124         }
   125 
   125 
   126     User::LeaveIfError( file.Read( iv ) );
   126     User::LeaveIfError( file.Read( iv ) );
   127 	User::LeaveIfError( file.Read( encryptedData ) );
   127     User::LeaveIfError( file.Read( encryptedData ) );
   128 #if defined(FF_PLATFORM_SIMULATOR) || defined(__WINSCW__)
   128 #if defined(FF_PLATFORM_SIMULATOR) || defined(__WINSCW__)
   129 	key.Copy( KDummyKey );
   129     key.Copy( KDummyKey );
   130 #else
   130 #else
   131 	iServer->Cache()->iKeyStorage->GetDeviceSpecificKeyL( key );
   131     iServer->Cache()->iKeyStorage->GetDeviceSpecificKeyL( key );
   132 #endif
   132 #endif
   133 	aesDecryptor = CAESDecryptor::NewL( key );
   133     aesDecryptor = CAESDecryptor::NewL( key );
   134     CleanupStack::PushL( aesDecryptor );
   134     CleanupStack::PushL( aesDecryptor );
   135     
   135 
   136     cbcDecryptor = CModeCBCDecryptor::NewL( aesDecryptor, iv );
   136     cbcDecryptor = CModeCBCDecryptor::NewL( aesDecryptor, iv );
   137     CleanupStack::Pop( aesDecryptor );
   137     CleanupStack::Pop( aesDecryptor );
   138     CleanupStack::PushL( cbcDecryptor );
   138     CleanupStack::PushL( cbcDecryptor );
   139     
   139 
   140     padding = CPaddingPKCS7::NewL( KAESKeyLength );
   140     padding = CPaddingPKCS7::NewL( KAESKeyLength );
   141     CleanupStack::PushL( padding );
   141     CleanupStack::PushL( padding );
   142     
   142 
   143     decryptor = CBufferedDecryptor::NewL( cbcDecryptor, padding );
   143     decryptor = CBufferedDecryptor::NewL( cbcDecryptor, padding );
   144     CleanupStack::Pop( 2, cbcDecryptor ); //padding, cbcDecryptor
   144     CleanupStack::Pop( 2, cbcDecryptor ); //padding, cbcDecryptor
   145     CleanupStack::PushL( decryptor );
   145     CleanupStack::PushL( decryptor );
   146 	
   146 
   147 	decryptor->ProcessFinalL( encryptedData, decryptedData );
   147     decryptor->ProcessFinalL( encryptedData, decryptedData );
   148 	CleanupStack::PopAndDestroy( 2, &file ); //decryptor, file
   148     CleanupStack::PopAndDestroy( 2, &file ); //decryptor, file
   149 	TLex8 lex( decryptedData );
   149     TLex8 lex( decryptedData );
   150 	
   150 
   151 	if ( aConfiguredDrive )
   151     if ( aConfiguredDrive )
   152 	    {
   152         {
   153 	    User::LeaveIfError( lex.Val( iInitialFreeSpace2 ) );
   153         User::LeaveIfError( lex.Val( iInitialFreeSpace2 ) );
   154 	    }
   154         }
   155 	else 
   155     else
   156 	    {
   156         {
   157 	    User::LeaveIfError( lex.Val( iInitialFreeSpace ) );
   157         User::LeaveIfError( lex.Val( iInitialFreeSpace ) );
   158 	    }    
   158         }
   159     }
   159     }
   160         
   160 
   161 void CWmDrmDataStore::WriteInitialFreeSpaceL( const TDesC& aFileName,
   161 void CWmDrmDataStore::WriteInitialFreeSpaceL( const TDesC& aFileName,
   162     TBool& aConfiguredDrive )
   162     TBool& aConfiguredDrive )
   163     {
   163     {
   164     RFile file;
   164     RFile file;
   165     TBuf8<KMaxTInt64BufLength + 2 * KAESKeyLength> encryptedData;
   165     TBuf8<KMaxTInt64BufLength + 2 * KAESKeyLength> encryptedData;
   168     TBuf8<KAESKeyLength> iv;
   168     TBuf8<KAESKeyLength> iv;
   169     CBufferedEncryptor* encryptor = NULL;
   169     CBufferedEncryptor* encryptor = NULL;
   170     CModeCBCEncryptor* cbcEncryptor = NULL;
   170     CModeCBCEncryptor* cbcEncryptor = NULL;
   171     CAESEncryptor* aesEncryptor = NULL;
   171     CAESEncryptor* aesEncryptor = NULL;
   172     CPaddingPKCS7* padding = NULL;
   172     CPaddingPKCS7* padding = NULL;
   173     
   173 
   174     LOGFN( "CWmDrmDataStore::WriteInitialFreeSpaceL" );
   174     LOGFN( "CWmDrmDataStore::WriteInitialFreeSpaceL" );
   175     User::LeaveIfError( file.Create( iServer->Fs(), aFileName, EFileWrite ) );
   175     User::LeaveIfError( file.Create( iServer->Fs(), aFileName, EFileWrite ) );
   176     CleanupClosePushL( file );
   176     CleanupClosePushL( file );
   177     if ( aConfiguredDrive )
   177     if ( aConfiguredDrive )
   178         {
   178         {
   179         iInitialFreeSpace2 = iServer->FreeSpaceL( aConfiguredDrive );
   179         iInitialFreeSpace2 = iServer->FreeSpaceL( aConfiguredDrive );
   180         }
   180         }
   181     else
   181     else
   182         {
   182         {
   183         iInitialFreeSpace = iServer->FreeSpaceL( aConfiguredDrive );
   183         iInitialFreeSpace = iServer->FreeSpaceL( aConfiguredDrive );
   184         }    
   184         }
   185     iv.SetLength( KAESKeyLength );
   185     iv.SetLength( KAESKeyLength );
   186     TRandom::RandomL( iv );
   186     TRandom::RandomL( iv );
   187 #if defined(FF_PLATFORM_SIMULATOR) || defined(__WINSCW__)
   187 #if defined(FF_PLATFORM_SIMULATOR) || defined(__WINSCW__)
   188     key.Copy( KDummyKey );
   188     key.Copy( KDummyKey );
   189 #else
   189 #else
   190     iServer->Cache()->iKeyStorage->GetDeviceSpecificKeyL( key );
   190     iServer->Cache()->iKeyStorage->GetDeviceSpecificKeyL( key );
   191 #endif
   191 #endif
   192     aesEncryptor = CAESEncryptor::NewL( key );
   192     aesEncryptor = CAESEncryptor::NewL( key );
   193     CleanupStack::PushL( aesEncryptor );
   193     CleanupStack::PushL( aesEncryptor );
   194     
   194 
   195     cbcEncryptor = CModeCBCEncryptor::NewL( aesEncryptor, iv );
   195     cbcEncryptor = CModeCBCEncryptor::NewL( aesEncryptor, iv );
   196     CleanupStack::Pop( aesEncryptor );
   196     CleanupStack::Pop( aesEncryptor );
   197     CleanupStack::PushL( cbcEncryptor );
   197     CleanupStack::PushL( cbcEncryptor );
   198     
   198 
   199     padding = CPaddingPKCS7::NewL( KAESKeyLength );
   199     padding = CPaddingPKCS7::NewL( KAESKeyLength );
   200     CleanupStack::PushL( padding );
   200     CleanupStack::PushL( padding );
   201     
   201 
   202     encryptor = CBufferedEncryptor::NewL( cbcEncryptor, padding );
   202     encryptor = CBufferedEncryptor::NewL( cbcEncryptor, padding );
   203     CleanupStack::Pop( 2, cbcEncryptor ); //padding, cbcEncryptor
   203     CleanupStack::Pop( 2, cbcEncryptor ); //padding, cbcEncryptor
   204     CleanupStack::PushL( encryptor );
   204     CleanupStack::PushL( encryptor );
   205     
   205 
   206     if ( aConfiguredDrive )
   206     if ( aConfiguredDrive )
   207         {
   207         {
   208         decryptedData.AppendNum( iInitialFreeSpace2 );
   208         decryptedData.AppendNum( iInitialFreeSpace2 );
   209         }
   209         }
   210     else
   210     else
   211         {
   211         {
   212         decryptedData.AppendNum( iInitialFreeSpace );
   212         decryptedData.AppendNum( iInitialFreeSpace );
   213         }    
   213         }
   214             
   214 
   215     encryptor->ProcessFinalL( decryptedData, encryptedData );
   215     encryptor->ProcessFinalL( decryptedData, encryptedData );
   216     User::LeaveIfError( file.Write( iv ) );
   216     User::LeaveIfError( file.Write( iv ) );
   217     User::LeaveIfError( file.Write( encryptedData ) );
   217     User::LeaveIfError( file.Write( encryptedData ) );
   218     CleanupStack::PopAndDestroy( 2, &file ); //encryptor, file
   218     CleanupStack::PopAndDestroy( 2, &file ); //encryptor, file
   219     }
   219     }
   226     TInt dataStoreSize( 0 );
   226     TInt dataStoreSize( 0 );
   227     TInt dummyDbSize( 0 );
   227     TInt dummyDbSize( 0 );
   228     TInt ratio( 0 );
   228     TInt ratio( 0 );
   229     TInt ratio2( 0 );
   229     TInt ratio2( 0 );
   230     TBool internalMassDriveNotFull( ETrue );
   230     TBool internalMassDriveNotFull( ETrue );
   231     
   231 
   232     LOGFN( "CWmDrmDataStore::DataStoreStateL" );
   232     LOGFN( "CWmDrmDataStore::DataStoreStateL" );
   233     freeSpace = iServer->FreeSpaceL( EFalse );
   233     freeSpace = iServer->FreeSpaceL( EFalse );
   234     
   234 
   235     if ( iWmDrmRightsConfigFound )
   235     if ( iWmDrmRightsConfigFound )
   236         {
   236         {
   237         // Check free space from the configured drive, too.
   237         // Check free space from the configured drive, too.
   238         freeSpace2 = iServer->FreeSpaceL( ETrue );
   238         freeSpace2 = iServer->FreeSpaceL( ETrue );
   239         
   239 
   240         if ( freeSpace2 < iMinFreeSpace2 ) 
   240         if ( freeSpace2 < iMinFreeSpace2 )
   241             {
   241             {
   242             internalMassDriveNotFull = EFalse;
   242             internalMassDriveNotFull = EFalse;
   243             }
   243             }
   244             
   244 
   245         dummyDbSize = DummyDBSizeL( ETrue );
   245         dummyDbSize = DummyDBSizeL( ETrue );
   246         dataStoreSize = DataStoreSizeL( ETrue );
   246         dataStoreSize = DataStoreSizeL( ETrue );
   247         ratio2 = dataStoreSize * 100 / iInitialFreeSpace2;
   247         ratio2 = dataStoreSize * 100 / iInitialFreeSpace2;
   248         freeSpace2 += dummyDbSize;
   248         freeSpace2 += dummyDbSize;
   249 #ifdef _LOGGING        
   249 #ifdef _LOGGING
   250         TBuf<KMaxTInt64BufLength> free2;
   250         TBuf<KMaxTInt64BufLength> free2;
   251         LOG1( "CWmDrmDataStore::DataStoreStateL: Free space (2): ");
   251         LOG1( "CWmDrmDataStore::DataStoreStateL: Free space (2): ");
   252         free2.AppendNumUC( freeSpace2, EDecimal );
   252         free2.AppendNumUC( freeSpace2, EDecimal );
   253         LOG( free2 );
   253         LOG( free2 );
   254         TBuf<KMaxTInt64BufLength> free2Min;
   254         TBuf<KMaxTInt64BufLength> free2Min;
   255         LOG1( "CWmDrmDataStore::DataStoreStateL: Minimum free space (2): ");
   255         LOG1( "CWmDrmDataStore::DataStoreStateL: Minimum free space (2): ");
   256         free2Min.AppendNumUC( iMinFreeSpace2, EDecimal );
   256         free2Min.AppendNumUC( iMinFreeSpace2, EDecimal );
   257         LOG( free2Min );
   257         LOG( free2Min );
   258 #endif             
   258 #endif
   259         }
   259         }
   260     
   260 
   261     // Check the system drive storage space next.   
   261     // Check the system drive storage space next.
   262     dummyDbSize = DummyDBSizeL( EFalse );
   262     dummyDbSize = DummyDBSizeL( EFalse );
   263     dataStoreSize = DataStoreSizeL( EFalse );
   263     dataStoreSize = DataStoreSizeL( EFalse );
   264     ratio = dataStoreSize * 100 / iInitialFreeSpace;
   264     ratio = dataStoreSize * 100 / iInitialFreeSpace;
   265     freeSpace += dummyDbSize;
   265     freeSpace += dummyDbSize;
   266 #ifdef _LOGGING        
   266 #ifdef _LOGGING
   267     TBuf<KMaxTInt64BufLength> free;
   267     TBuf<KMaxTInt64BufLength> free;
   268     LOG1( "CWmDrmDataStore::DataStoreStateL: Free space: ");
   268     LOG1( "CWmDrmDataStore::DataStoreStateL: Free space: ");
   269     free.AppendNumUC( freeSpace, EDecimal );
   269     free.AppendNumUC( freeSpace, EDecimal );
   270     LOG( free );
   270     LOG( free );
   271     TBuf<KMaxTInt64BufLength> freeMin;
   271     TBuf<KMaxTInt64BufLength> freeMin;
   272     LOG1( "CWmDrmDataStore::DataStoreStateL: Minimum free space: ");
   272     LOG1( "CWmDrmDataStore::DataStoreStateL: Minimum free space: ");
   273     freeMin.AppendNumUC( iMinFreeSpace, EDecimal );
   273     freeMin.AppendNumUC( iMinFreeSpace, EDecimal );
   274     LOG( freeMin );
   274     LOG( freeMin );
   275 #endif      
   275 #endif
   276     
   276 
   277     // Select the state of the storage space.      
   277     // Select the state of the storage space.
   278     if ( ( freeSpace > iMinFreeSpace ) && internalMassDriveNotFull ) 
   278     if ( ( freeSpace > iMinFreeSpace ) && internalMassDriveNotFull )
   279         {
   279         {
   280         LOG1( "CWmDrmDataStore::DataStoreStateL: Store space Ok" );
   280         LOG1( "CWmDrmDataStore::DataStoreStateL: Store space Ok" );
   281         state = EStoreSpaceOK;
   281         state = EStoreSpaceOK;
   282         }
   282         }
   283     else
   283     else
   284         {
   284         {
   285         // The configured drive is running out of space. The system drive 
   285         // The configured drive is running out of space. The system drive
   286         // may also be running out of storage space, but calculate
   286         // may also be running out of storage space, but calculate
   287         // the ratio of database size to initial free drive space and the
   287         // the ratio of database size to initial free drive space and the
   288         // state of the drive storage space from the configured drive because
   288         // state of the drive storage space from the configured drive because
   289         // it is likely to fill up faster since the media files may be synced to it. 
   289         // it is likely to fill up faster since the media files may be synced to it.
   290         if ( !internalMassDriveNotFull )
   290         if ( !internalMassDriveNotFull )
   291             {
   291             {
   292             LOG2( "Ratio (2): %d", ratio2 );
   292             LOG2( "Ratio (2): %d", ratio2 );
   293             if ( ratio2 <= iMaxSpaceRatio2 )
   293             if ( ratio2 <= iMaxSpaceRatio2 )
   294                 {
   294                 {
   300                 LOG1( "CWmDrmDataStore::DataStoreStateL: Store space full (2)" );
   300                 LOG1( "CWmDrmDataStore::DataStoreStateL: Store space full (2)" );
   301                 state = EStoreSpaceFull;
   301                 state = EStoreSpaceFull;
   302                 }
   302                 }
   303             }
   303             }
   304         else
   304         else
   305             // Only the system drive is running out of storage space. 
   305             // Only the system drive is running out of storage space.
   306             {
   306             {
   307             LOG2( "Ratio: %d", ratio );
   307             LOG2( "Ratio: %d", ratio );
   308             if ( ratio <= iMaxSpaceRatio )
   308             if ( ratio <= iMaxSpaceRatio )
   309                 {
   309                 {
   310                 LOG1( "CWmDrmDataStore::DataStoreStateL Store space low" );
   310                 LOG1( "CWmDrmDataStore::DataStoreStateL Store space low" );
   313             else
   313             else
   314                 {
   314                 {
   315                 LOG1( "CWmDrmDataStore::DataStoreStateL Store space full" );
   315                 LOG1( "CWmDrmDataStore::DataStoreStateL Store space full" );
   316                 state = EStoreSpaceFull;
   316                 state = EStoreSpaceFull;
   317                 }
   317                 }
   318             }    
   318             }
   319         }
   319         }
   320         
   320 
   321     LOG2( "DataStoreState: %d", state );
   321     LOG2( "DataStoreState: %d", state );
   322     return state;
   322     return state;
   323     }
   323     }
   324 
   324 
   325 void CWmDrmDataStore::InitializeDummyDbFileL( const TDesC& aFileName,
   325 void CWmDrmDataStore::InitializeDummyDbFileL( const TDesC& aFileName,
   326     RFile& aDummyDb, TBool& aConfiguredDrive )
   326     RFile& aDummyDb, TBool& aConfiguredDrive )
   327     {
   327     {
   328     TInt r( KErrNone );
   328     TInt r( KErrNone );
   329     
   329 
   330     LOGFN( "CWmDrmDataStore::InitializeDummyDbFileL" );
   330     LOGFN( "CWmDrmDataStore::InitializeDummyDbFileL" );
   331     
   331 
   332     r = aDummyDb.Create( iServer->Fs(), aFileName, EFileWrite );
   332     r = aDummyDb.Create( iServer->Fs(), aFileName, EFileWrite );
   333     if ( r == KErrAlreadyExists )
   333     if ( r == KErrAlreadyExists )
   334         {
   334         {
   335         User::LeaveIfError( 
   335         User::LeaveIfError(
   336             aDummyDb.Open( iServer->Fs(), aFileName, EFileWrite ) );
   336             aDummyDb.Open( iServer->Fs(), aFileName, EFileWrite ) );
   337         }
   337         }
   338     else if( !r )
   338     else if( !r )
   339         {
   339         {
   340         TInt dataStoreSize( DataStoreSizeL( aConfiguredDrive ) );
   340         TInt dataStoreSize( DataStoreSizeL( aConfiguredDrive ) );
   341         if ( aConfiguredDrive )
   341         if ( aConfiguredDrive )
   342             {
   342             {
   343             if ( dataStoreSize <= iDummyDbInitialSize2 )
   343             if ( dataStoreSize <= iDummyDbInitialSize2 )
   344                 {
   344                 {
   345                 User::LeaveIfError( 
   345                 User::LeaveIfError(
   346                     aDummyDb.SetSize( iDummyDbInitialSize2 - dataStoreSize ) );
   346                     aDummyDb.SetSize( iDummyDbInitialSize2 - dataStoreSize ) );
   347                 }
   347                 }
   348             else
   348             else
   349                 {
   349                 {
   350                 User::LeaveIfError( aDummyDb.SetSize( 0 ) );
   350                 User::LeaveIfError( aDummyDb.SetSize( 0 ) );
   351                 }
   351                 }
   352             }
   352             }
   353         else 
   353         else
   354             {
   354             {
   355             if ( dataStoreSize <= iDummyDbInitialSize )
   355             if ( dataStoreSize <= iDummyDbInitialSize )
   356                 {
   356                 {
   357                 User::LeaveIfError( 
   357                 User::LeaveIfError(
   358                     aDummyDb.SetSize( iDummyDbInitialSize - dataStoreSize ) );
   358                     aDummyDb.SetSize( iDummyDbInitialSize - dataStoreSize ) );
   359                 }
   359                 }
   360             else
   360             else
   361                 {
   361                 {
   362                 User::LeaveIfError( aDummyDb.SetSize( 0 ) );
   362                 User::LeaveIfError( aDummyDb.SetSize( 0 ) );
   363                 }
   363                 }
   364             } 
   364             }
   365         }
   365         }
   366     else
   366     else
   367         {
   367         {
   368         User::Leave( r );
   368         User::Leave( r );
   369         }
   369         }
   377         {
   377         {
   378         TInt dummyDbSize( DummyDBSizeL( aConfiguredDrive ) );
   378         TInt dummyDbSize( DummyDBSizeL( aConfiguredDrive ) );
   379         LOG2( "dummyDbSize: %d", dummyDbSize );
   379         LOG2( "dummyDbSize: %d", dummyDbSize );
   380         if ( aSize <= dummyDbSize )
   380         if ( aSize <= dummyDbSize )
   381             {
   381             {
   382             if ( aConfiguredDrive ) 
   382             if ( aConfiguredDrive )
   383                 {
   383                 {
   384             	User::LeaveIfError( iDummyDb2.SetSize( dummyDbSize - aSize ) );
   384                 User::LeaveIfError( iDummyDb2.SetSize( dummyDbSize - aSize ) );
   385             	}
   385                 }
   386             else
   386             else
   387             	{	
   387                 {
   388             	User::LeaveIfError( iDummyDb.SetSize( dummyDbSize - aSize ) );
   388                 User::LeaveIfError( iDummyDb.SetSize( dummyDbSize - aSize ) );
   389             	}
   389                 }
   390            	} 		
   390             }
   391         else
   391         else
   392             {
   392             {
   393             if ( aConfiguredDrive )
   393             if ( aConfiguredDrive )
   394             	{
   394                 {
   395             	User::LeaveIfError( iDummyDb2.SetSize( 0 ) );
   395                 User::LeaveIfError( iDummyDb2.SetSize( 0 ) );
   396             	}
   396                 }
   397             else 
   397             else
   398             	{
   398                 {
   399             	User::LeaveIfError( iDummyDb.SetSize( 0 ) );	
   399                 User::LeaveIfError( iDummyDb.SetSize( 0 ) );
   400           		}
   400                 }
   401           	}		
   401             }
   402         }
   402         }
   403     else
   403     else
   404         {
   404         {
   405         TInt dataStoreSize( DataStoreSizeL( aConfiguredDrive ) );
   405         TInt dataStoreSize( DataStoreSizeL( aConfiguredDrive ) );
   406         LOG2( "dataStoreSize: %d", dataStoreSize );
   406         LOG2( "dataStoreSize: %d", dataStoreSize );
   407         if ( aConfiguredDrive )
   407         if ( aConfiguredDrive )
   408             {
   408             {
   409             if ( dataStoreSize <= iDummyDbInitialSize2 )
   409             if ( dataStoreSize <= iDummyDbInitialSize2 )
   410                 {
   410                 {
   411                 User::LeaveIfError( 
   411                 User::LeaveIfError(
   412                     iDummyDb2.SetSize( iDummyDbInitialSize2 - dataStoreSize ) );
   412                     iDummyDb2.SetSize( iDummyDbInitialSize2 - dataStoreSize ) );
   413             	}
   413                 }
   414             else 
   414             else
   415             	{
   415                 {
   416             	User::LeaveIfError( iDummyDb2.SetSize( 0 ) );
   416                 User::LeaveIfError( iDummyDb2.SetSize( 0 ) );
   417             	}
   417                 }
   418             }
   418             }
   419         else 
   419         else
   420             {
   420             {
   421             if ( dataStoreSize <= iDummyDbInitialSize )
   421             if ( dataStoreSize <= iDummyDbInitialSize )
   422                 {
   422                 {
   423                 User::LeaveIfError( 
   423                 User::LeaveIfError(
   424                     iDummyDb.SetSize( iDummyDbInitialSize - dataStoreSize ) );
   424                     iDummyDb.SetSize( iDummyDbInitialSize - dataStoreSize ) );
   425             	}
   425                 }
   426             else 
   426             else
   427             	{
   427                 {
   428             	User::LeaveIfError( iDummyDb.SetSize( 0 ) );
   428                 User::LeaveIfError( iDummyDb.SetSize( 0 ) );
   429             	}
   429                 }
   430             }
   430             }
   431         } 
   431         }
   432     }
   432     }
   433 
   433 
   434 TInt CWmDrmDataStore::DataStoreSizeL( TBool aConfiguredDrive )
   434 TInt CWmDrmDataStore::DataStoreSizeL( TBool aConfiguredDrive )
   435     {
   435     {
   436     TInt dataStoreSize( iServer->Db()->DataBaseSize( aConfiguredDrive ) );
   436     TInt dataStoreSize( iServer->Db()->DataBaseSize( aConfiguredDrive ) );
   446         User::LeaveIfError( iDummyDb2.Size( dummyDbSize ) );
   446         User::LeaveIfError( iDummyDb2.Size( dummyDbSize ) );
   447         }
   447         }
   448     else
   448     else
   449         {
   449         {
   450         User::LeaveIfError( iDummyDb.Size( dummyDbSize ) );
   450         User::LeaveIfError( iDummyDb.Size( dummyDbSize ) );
   451         }    
   451         }
   452     return dummyDbSize;
   452     return dummyDbSize;
   453     }
   453     }
   454 
   454 
   455 void CWmDrmDataStore::PrepareInfoFilesL( TBool aConfiguredDrive, 
   455 void CWmDrmDataStore::PrepareInfoFilesL( TBool aConfiguredDrive,
   456     TChar aDriveLetter, RFile& aDummyDb )
   456     TChar aDriveLetter, RFile& aDummyDb )
   457     {
   457     {
   458     LOGFN( "CWmDrmDataStore::PrepareInfoFilesL" );
   458     LOGFN( "CWmDrmDataStore::PrepareInfoFilesL" );
   459     CRepository* repository( NULL );
   459     CRepository* repository( NULL );
   460     TInt r( KErrNone );
   460     TInt r( KErrNone );
   461     TFileName dummyDbFile; 
   461     TFileName dummyDbFile;
   462     TFileName initialFreeSpaceFile;
   462     TFileName initialFreeSpaceFile;
   463     
   463 
   464     initialFreeSpaceFile.Format( KPrivateDir, (TUint)aDriveLetter );
   464     initialFreeSpaceFile.Format( KPrivateDir, (TUint)aDriveLetter );
   465     iServer->Fs().MkDirAll( initialFreeSpaceFile );
   465     iServer->Fs().MkDirAll( initialFreeSpaceFile );
   466     initialFreeSpaceFile.Format( KInitialFreeSpaceFile, (TUint)aDriveLetter );
   466     initialFreeSpaceFile.Format( KInitialFreeSpaceFile, (TUint)aDriveLetter );
   467     dummyDbFile.Format( KDummyDbFile, (TUint)aDriveLetter );
   467     dummyDbFile.Format( KDummyDbFile, (TUint)aDriveLetter );
   468     	
   468 
   469     TRAP( r, WriteInitialFreeSpaceL( initialFreeSpaceFile, 
   469     TRAP( r, WriteInitialFreeSpaceL( initialFreeSpaceFile,
   470         aConfiguredDrive ) );
   470         aConfiguredDrive ) );
   471     if ( r )
   471     if ( r )
   472         {
   472         {
   473         r = KErrNone;
   473         r = KErrNone;
   474         
   474 
   475         // catch the read error
   475         // catch the read error
   476         TRAP( r, ReadInitialFreeSpaceL( initialFreeSpaceFile, 
   476         TRAP( r, ReadInitialFreeSpaceL( initialFreeSpaceFile,
   477             aConfiguredDrive ) );
   477             aConfiguredDrive ) );
   478         
   478 
   479         // if an error occurs, this means that we are unable to read the info, 
   479         // if an error occurs, this means that we are unable to read the info,
   480         // thus we need to delete the file and run write again.
   480         // thus we need to delete the file and run write again.
   481         // hopefully this being a temporary error, but if we fail again we fail
   481         // hopefully this being a temporary error, but if we fail again we fail
   482         // until next ConstructL
   482         // until next ConstructL
   483         if( r != KErrNone )
   483         if( r != KErrNone )
   484             {
   484             {
   485             // delete the file:
   485             // delete the file:
   486             iServer->Fs().Delete( initialFreeSpaceFile );
   486             iServer->Fs().Delete( initialFreeSpaceFile );
   487             
   487 
   488             // Calc & Write the new info
   488             // Calc & Write the new info
   489             WriteInitialFreeSpaceL( initialFreeSpaceFile, aConfiguredDrive );
   489             WriteInitialFreeSpaceL( initialFreeSpaceFile, aConfiguredDrive );
   490             }
   490             }
   491         }
   491         }
   492     
   492 
   493     if ( aConfiguredDrive )
   493     if ( aConfiguredDrive )
   494         {
   494         {
   495 #ifdef _LOGGING        
   495 #ifdef _LOGGING
   496         TBuf<KMaxTInt64BufLength> freeSpace2;
   496         TBuf<KMaxTInt64BufLength> freeSpace2;
   497         LOG1( "CWmDrmDataStore::DataStoreStateL: Initial free space (2): ");
   497         LOG1( "CWmDrmDataStore::DataStoreStateL: Initial free space (2): ");
   498         freeSpace2.AppendNumUC( iInitialFreeSpace2, EDecimal );
   498         freeSpace2.AppendNumUC( iInitialFreeSpace2, EDecimal );
   499         LOG( freeSpace2 );
   499         LOG( freeSpace2 );
   500 #endif          
   500 #endif
   501         if ( iInitialFreeSpace2 <= 0 )
   501         if ( iInitialFreeSpace2 <= 0 )
   502             {
   502             {
   503             User::Leave( KErrNotReady );
   503             User::Leave( KErrNotReady );
   504             }
   504             }
   505         }
   505         }
   506     else 
   506     else
   507         {
   507         {
   508 #ifdef _LOGGING        
   508 #ifdef _LOGGING
   509         TBuf<KMaxTInt64BufLength> freeSpace;
   509         TBuf<KMaxTInt64BufLength> freeSpace;
   510         LOG1( "CWmDrmDataStore::DataStoreStateL: Initial free space: ");
   510         LOG1( "CWmDrmDataStore::DataStoreStateL: Initial free space: ");
   511         freeSpace.AppendNumUC( iInitialFreeSpace, EDecimal );
   511         freeSpace.AppendNumUC( iInitialFreeSpace, EDecimal );
   512         LOG( freeSpace );
   512         LOG( freeSpace );
   513 #endif          
   513 #endif
   514         if ( iInitialFreeSpace <= 0 )
   514         if ( iInitialFreeSpace <= 0 )
   515             {
   515             {
   516             User::Leave( KErrNotReady );
   516             User::Leave( KErrNotReady );
   517             }
   517             }
   518         }
   518         }
   519     
   519 
   520     //LOG1( "CWmDrmDataStore::PrepareInfoFilesL Check Cenrep" );
   520     //LOG1( "CWmDrmDataStore::PrepareInfoFilesL Check Cenrep" );
   521     
   521 
   522     TRAP( r, repository = CRepository::NewL( KCRUidWMDRM ) );
   522     TRAP( r, repository = CRepository::NewL( KCRUidWMDRM ) );
   523     if ( repository )
   523     if ( repository )
   524         {
   524         {
   525         TInt rate( 0 );
   525         TInt rate( 0 );
   526         
   526 
   527         // Check the cenrep key parameters either for the default system drive
   527         // Check the cenrep key parameters either for the default system drive
   528         // or for the internal mass drive depending whether the WMDRM rights
   528         // or for the internal mass drive depending whether the WMDRM rights
   529         // are configured to be partially stored to the internal mass drive or
   529         // are configured to be partially stored to the internal mass drive or
   530         // not. 
   530         // not.
   531         if ( !aConfiguredDrive )
   531         if ( !aConfiguredDrive )
   532             { 
   532             {
   533             r = repository->Get( KWMDRMLicStoreLowMem, rate );
   533             r = repository->Get( KWMDRMLicStoreLowMem, rate );
   534             if ( r )
   534             if ( r )
   535                 {
   535                 {
   536                 iMinFreeSpace = KMegaByte;      
   536                 iMinFreeSpace = KMegaByte;
   537                 }
   537                 }
   538             else
   538             else
   539                 {
   539                 {
   540                 iMinFreeSpace = rate * KMegaByte;   
   540                 iMinFreeSpace = rate * KMegaByte;
   541                 }
   541                 }
   542             r = repository->Get( KWMDRMLicStoreSizeRatio, iMaxSpaceRatio );
   542             r = repository->Get( KWMDRMLicStoreSizeRatio, iMaxSpaceRatio );
   543             if ( r )
   543             if ( r )
   544                 {
   544                 {
   545                 iMaxSpaceRatio = KMaxSpaceRatio;
   545                 iMaxSpaceRatio = KMaxSpaceRatio;
   546                 }
   546                 }
   547             r = repository->Get( KWMDRMLicStoreReservedSpace, rate );
   547             r = repository->Get( KWMDRMLicStoreReservedSpace, rate );
   548             if ( r )
   548             if ( r )
   549                 {
   549                 {
   550                 iDummyDbInitialSize = KDummyDbInitialSize;   
   550                 iDummyDbInitialSize = KDummyDbInitialSize;
   551                 }
   551                 }
   552             else
   552             else
   553                 {
   553                 {
   554                 iDummyDbInitialSize = rate * KMegaByte;     
   554                 iDummyDbInitialSize = rate * KMegaByte;
   555                 }
   555                 }
   556             }
   556             }
   557         else 
   557         else
   558             {
   558             {
   559             r = repository->Get( KWMDRM2LicStoreLowMem, rate );
   559             r = repository->Get( KWMDRM2LicStoreLowMem, rate );
   560             if ( r )
   560             if ( r )
   561                 {
   561                 {
   562                 iMinFreeSpace2 = KTwoHundredMegaBytes;      
   562                 iMinFreeSpace2 = KTwoHundredMegaBytes;
   563                 }
   563                 }
   564             else
   564             else
   565                 {
   565                 {
   566                 iMinFreeSpace2 = rate * KMegaByte;
   566                 iMinFreeSpace2 = rate * KMegaByte;
   567                 }
   567                 }
   571                 iMaxSpaceRatio2 = KMaxSpaceRatio2;
   571                 iMaxSpaceRatio2 = KMaxSpaceRatio2;
   572                 }
   572                 }
   573             r = repository->Get( KWMDRM2LicStoreReservedSpace, rate );
   573             r = repository->Get( KWMDRM2LicStoreReservedSpace, rate );
   574             if ( r )
   574             if ( r )
   575                 {
   575                 {
   576                 iDummyDbInitialSize2 = KDummyDbInitialSize;   
   576                 iDummyDbInitialSize2 = KDummyDbInitialSize;
   577                 }
   577                 }
   578             else
   578             else
   579                 {
   579                 {
   580                 iDummyDbInitialSize2 = rate * KMegaByte;     
   580                 iDummyDbInitialSize2 = rate * KMegaByte;
   581                 }
   581                 }
   582             }          
   582             }
   583         delete repository;
   583         delete repository;
   584         }
   584         }
   585     else
   585     else
   586         {
   586         {
   587         if ( !aConfiguredDrive )
   587         if ( !aConfiguredDrive )
   588             {
   588             {
   589             iMinFreeSpace = KMegaByte; 
   589             iMinFreeSpace = KMegaByte;
   590             iMaxSpaceRatio = KMaxSpaceRatio;
   590             iMaxSpaceRatio = KMaxSpaceRatio;
   591             iDummyDbInitialSize = KDummyDbInitialSize;
   591             iDummyDbInitialSize = KDummyDbInitialSize;
   592             }
   592             }
   593         else 
   593         else
   594             {
   594             {
   595             iMinFreeSpace2 = KTwoHundredMegaBytes; 
   595             iMinFreeSpace2 = KTwoHundredMegaBytes;
   596             iMaxSpaceRatio2 = KMaxSpaceRatio2;
   596             iMaxSpaceRatio2 = KMaxSpaceRatio2;
   597             iDummyDbInitialSize2 = KDummyDbInitialSize;            
   597             iDummyDbInitialSize2 = KDummyDbInitialSize;
   598             }
   598             }
   599         }
   599         }
   600      
   600 
   601     InitializeDummyDbFileL( dummyDbFile, aDummyDb, aConfiguredDrive );
   601     InitializeDummyDbFileL( dummyDbFile, aDummyDb, aConfiguredDrive );
   602     
   602 
   603     }
   603     }