mmappcomponents/mmmtpdataprovider/tsrc/mtpdataprovidertestmodule/src/cteststoragemgr.cpp
changeset 0 a2952bb97e68
child 48 b7b49303d0c0
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2009 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:  Storage manager for test module dummy mtp implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bautils.h>
       
    20 #include <mtp/cmtptypestring.h>
       
    21 #include <mtp/mtpdatatypeconstants.h>
       
    22 #include <mtp/mtpprotocolconstants.h>
       
    23 
       
    24 #include "cteststoragemgr.h"
       
    25 #include "logging.h"
       
    26 
       
    27 #ifdef LOGGER
       
    28 #undef LOGGER
       
    29 #endif
       
    30 #define LOGGER CTestStorageMgr::iLog
       
    31 
       
    32 // StorageID bit manipulation patterns.
       
    33 static const TUint32    KLogicalIdMask(0x0000FFFF);
       
    34 static const TUint32    KPhysicalIdMask(0xFFFF0000);
       
    35 
       
    36 static const TUint      KLogicalNumberMask(0x000000FF);
       
    37 static const TUint      KLogicalOwnerShift(8);
       
    38 static const TUint      KPhysicalNumberShift(16);
       
    39 static const TUint      KPhysicalOwnerShift(24);
       
    40 static const TUint8     KMaxOwnedStorages(0xFF);
       
    41 
       
    42 CStifLogger* CTestStorageMgr::iLog = NULL;
       
    43 
       
    44 /**
       
    45 MTP data provider framework storage manager factory method.
       
    46 @return A pointer to an MTP data provider framework storage manager. Ownership 
       
    47 IS transfered.
       
    48 @leave One of the system wide error codes, if a processing failure occurs.
       
    49 */
       
    50 CTestStorageMgr* CTestStorageMgr::NewL()
       
    51     {
       
    52     TFileName logFileName;
       
    53     logFileName.Copy( KMtpDataProviderTestModuleLogFile );
       
    54     
       
    55     if ( CTestStorageMgr::iLog == NULL )
       
    56         {
       
    57         CTestStorageMgr::iLog = CStifLogger::NewL(
       
    58                 KMtpDataProviderTestModuleLogPath, 
       
    59                 logFileName,
       
    60                 CStifLogger::ETxt,
       
    61                 TRACE_TARGET,
       
    62                 ETrue, ETrue, ETrue, EFalse, ETrue );
       
    63         }
       
    64     PRINTF0( ">CTestStorageMgr::NewL" );
       
    65     CTestStorageMgr* self = new( ELeave ) CTestStorageMgr();
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL();
       
    68     CleanupStack::Pop( self );
       
    69     PRINTF0( "<CTestStorageMgr::NewL" );
       
    70     return self;
       
    71     }
       
    72 
       
    73 /**
       
    74 Destructor.
       
    75 */
       
    76 CTestStorageMgr::~CTestStorageMgr()
       
    77     {
       
    78     PRINTF0( ">CTestStorageMgr::~CTestStorageMgr()" );
       
    79     iFs.Close();
       
    80     iPhysicalStorageNumbers.Reset();
       
    81     iStorages.ResetAndDestroy();
       
    82     PRINTF0( "<CTestStorageMgr::~CTestStorageMgr()" );
       
    83     if ( CTestStorageMgr::iLog != NULL )
       
    84         {
       
    85         delete CTestStorageMgr::iLog;
       
    86         }
       
    87     }
       
    88 
       
    89 /**
       
    90 Extracts the storage number of the logical storage ID encoded in the specified
       
    91 StorageID.
       
    92 @param aStorageId The storage ID.
       
    93 @return The storage number.
       
    94 */
       
    95 TUint CTestStorageMgr::LogicalStorageNumber( TUint32 aStorageId ) 
       
    96     {
       
    97     PRINTF1( ">CTestStorageMgr::LogicalStorageNumber aStorageId = 0x%x", aStorageId );
       
    98     TUint number = aStorageId & KLogicalNumberMask;
       
    99     PRINTF1( "<CTestStorageMgr::LogicalStorageNumber number = 0x%x", number );
       
   100     return number;
       
   101     }
       
   102 
       
   103 /**
       
   104 Extracts the ID of the data provider responsible for the logical storage ID 
       
   105 encoded in the specified StorageID.
       
   106 @param aStorageId The storage ID.
       
   107 @return The data provider owner ID.
       
   108 */    
       
   109 TUint CTestStorageMgr::LogicalStorageOwner( TUint32 aStorageId ) 
       
   110     {
       
   111     PRINTF1( ">CTestStorageMgr::LogicalStorageOwner aStorageId = 0x%x", aStorageId );
       
   112     TUint owner = ( aStorageId & KLogicalIdMask ) >> KLogicalOwnerShift;
       
   113     PRINTF1( "<CTestStorageMgr::LogicalStorageOwner owner = 0x%x", owner );
       
   114     return owner;
       
   115     }
       
   116 
       
   117 /**
       
   118 Extracts the storage number of the physical storage ID encoded in the specified
       
   119 StorageID.
       
   120 @param aStorageId The storage ID.
       
   121 @return The storage number.
       
   122 */
       
   123 TUint CTestStorageMgr::PhysicalStorageNumber( TUint32 aStorageId ) 
       
   124     {
       
   125     PRINTF1( ">CTestStorageMgr::PhysicalStorageNumber aStorageId = 0x%x", aStorageId );
       
   126     TUint number = ( aStorageId & KPhysicalIdMask ) >> KPhysicalNumberShift;
       
   127     PRINTF1( "<CTestStorageMgr::PhysicalStorageNumber number = 0x%x", number );
       
   128     return number;
       
   129     }
       
   130     
       
   131 /**
       
   132 Extracts the ID of the data provider responsible for the physical storage ID 
       
   133 encoded in the specified StorageID.
       
   134 @param aStorageId The storage ID.
       
   135 @return The data provider owner ID.
       
   136 */
       
   137 TUint CTestStorageMgr::PhysicalStorageOwner( TUint32 aStorageId ) 
       
   138     {
       
   139     PRINTF1( ">CTestStorageMgr::PhysicalStorageOwner aStorageId = 0x%x", aStorageId );
       
   140     TUint owner = ( aStorageId & KPhysicalIdMask ) >> KPhysicalOwnerShift;
       
   141     PRINTF1( "<CTestStorageMgr::PhysicalStorageOwner owner = 0x%x", owner );
       
   142     return owner;
       
   143     }
       
   144 
       
   145 /**
       
   146 Sets the default MTP StorageID. This should be set once at start up and not 
       
   147 subsequently changed.
       
   148 @param aStorageId The system default MTP StorageID.
       
   149 @panic USER 0, in debug builds only, if the default StorageID is set more than
       
   150 once.
       
   151 */    
       
   152 void CTestStorageMgr::SetDefaultStorageId( TUint32 aStorageId )
       
   153     {
       
   154     PRINTF1( "<>CTestStorageMgr::SetDefaultStorageId aStorageId = 0x%x", aStorageId );
       
   155     iDefaultStorageId = aStorageId;
       
   156     }
       
   157 
       
   158 /**
       
   159 Creates a mapping between the specified Symbian OS drive number and MTP 
       
   160 StorageID.
       
   161 @param aDriveNumber The Symbian OS drive number.
       
   162 @param aStorageId The MTP StorageID.
       
   163 @leave One of the sysem wide error codes, if a processing failure occurs.
       
   164 */
       
   165 void CTestStorageMgr::SetDriveMappingL( TDriveNumber aDriveNumber, TUint32 aStorageId )
       
   166     {
       
   167     PRINTF2( ">CTestStorageMgr::SetDriveMappingL aDriveNumber = %d aStorageId = 0x%x", aDriveNumber, aStorageId );
       
   168     iMapDriveToStorage[aDriveNumber] = aStorageId;
       
   169     PRINTF0( "<CTestStorageMgr::SetDriveMappingL" );
       
   170     }
       
   171 
       
   172 /**
       
   173 Sets the framework storages owner identifier. This should be set once at start 
       
   174 up and not subsequently changed.
       
   175 @param aDataProviderId The framework storages owner identifier.
       
   176 @panic USER 0, in debug builds only, if the framework storages owner identifier
       
   177 is set more than once.
       
   178 */    
       
   179 void CTestStorageMgr::SetFrameworkId( TUint aDataProviderId )
       
   180     {
       
   181     PRINTF1( ">CTestStorageMgr::SetFrameworkId aDataProviderId = %d", aDataProviderId );
       
   182     __ASSERT_DEBUG( ( iFrameworkId == KErrNotFound ), User::Invariant() );
       
   183     iFrameworkId = aDataProviderId;
       
   184     PRINTF0( "<CTestStorageMgr::SetFrameworkId" );
       
   185     }
       
   186 
       
   187 TUint32 CTestStorageMgr::AllocateLogicalStorageIdL( TUint aDataProviderId, 
       
   188         TDriveNumber aDriveNumber, const CMTPStorageMetaData& aStorage )
       
   189     {
       
   190     PRINTF2( ">CTestStorageMgr::AllocateLogicalStorageIdL aDataProviderId = %u aDriveNumber = %d", aDataProviderId, aDriveNumber );
       
   191     TUint id( AllocateLogicalStorageIdL( aDataProviderId, 
       
   192             PhysicalStorageId( aDriveNumber ), aStorage ) );
       
   193     PRINTF1( "<CTestStorageMgr::AllocateLogicalStorageIdL id = 0x%x", id );
       
   194     return id;
       
   195     }
       
   196 
       
   197 TUint32 CTestStorageMgr::AllocateLogicalStorageIdL( TUint aDataProviderId,
       
   198         TUint32 aPhysicalStorageId, const CMTPStorageMetaData& aStorage )
       
   199     {
       
   200     PRINTF2( ">CTestStorageMgr::AllocateLogicalStorageIdL aDataProviderId = %u aPhysicalStorageId = 0x%x", aDataProviderId, aPhysicalStorageId );
       
   201     // Resolve the physical storage.
       
   202     CMTPStorageMetaData& physical( StorageMetaDataL( aPhysicalStorageId ) );
       
   203     // Validate the SUID and storage type.
       
   204     if ( iStorages.Find( aStorage.DesC( CMTPStorageMetaData::EStorageSuid ),
       
   205             StorageKeyMatchSuid ) != KErrNotFound )
       
   206         {
       
   207         // SUID is not unique.
       
   208         User::Leave( KErrAlreadyExists );
       
   209         }
       
   210     else if ( aStorage.Uint( CMTPStorageMetaData::EStorageSystemType ) !=
       
   211               physical.Uint( CMTPStorageMetaData::EStorageSystemType ) )
       
   212         {
       
   213         // Physical/logical storage type mis-match.
       
   214         User::Leave( KErrArgument );
       
   215         }
       
   216     else if ( aStorage.Uint( CMTPStorageMetaData::EStorageSystemType ) ==
       
   217             CMTPStorageMetaData::ESystemTypeDefaultFileSystem )
       
   218         {
       
   219         // Validate that the SUID path exists.
       
   220         if ( !BaflUtils::PathExists( iFs,
       
   221                 aStorage.DesC( CMTPStorageMetaData::EStorageSuid ) ) )
       
   222             {
       
   223             User::Leave( KErrPathNotFound );
       
   224             }
       
   225         
       
   226         // Validate that the SUID path corresponds to the physical storage drive.
       
   227         TInt storageDrive( DriveNumber( aPhysicalStorageId ) );
       
   228         TParse p;
       
   229         User::LeaveIfError( p.Set( aStorage.DesC(
       
   230                 CMTPStorageMetaData::EStorageSuid ), NULL, NULL ) );
       
   231         TInt suidDrive( 0 );
       
   232         User::LeaveIfError( iFs.CharToDrive( TChar( p.Drive()[0] ), 
       
   233                 suidDrive ) );
       
   234         if ( suidDrive != storageDrive )
       
   235             {
       
   236             // SUID path/physical storage drive mis-match.
       
   237             User::Leave( KErrArgument );
       
   238             }
       
   239         }
       
   240     
       
   241     // Allocate a logical StorageId.
       
   242     TInt32 id( AllocateLogicalStorageId( aDataProviderId,
       
   243             aPhysicalStorageId ) );
       
   244     User::LeaveIfError( id );
       
   245     
       
   246     // Create the logical storage meta-data.
       
   247     CMTPStorageMetaData* logical( CMTPStorageMetaData::NewLC( aStorage ) );
       
   248     logical->SetUint( CMTPStorageMetaData::EStorageId, id );
       
   249     
       
   250     // Store the logical storage meta-data.
       
   251     iStorages.InsertInOrderL( logical, StorageOrder );
       
   252     CleanupStack::Pop( logical );
       
   253     
       
   254     // Associate the logical and physical storages.
       
   255     RArray<TUint> logicals;
       
   256     CleanupClosePushL( logicals );
       
   257     physical.GetUintArrayL( CMTPStorageMetaData::EStorageLogicalIds,
       
   258             logicals );
       
   259     logicals.InsertInOrderL( id );
       
   260     physical.SetUintArrayL( CMTPStorageMetaData::EStorageLogicalIds,
       
   261             logicals );
       
   262     CleanupStack::PopAndDestroy( &logicals );
       
   263     
       
   264     PRINTF1( "<CTestStorageMgr::AllocateLogicalStorageIdL id = 0x%x", id );
       
   265     return id;
       
   266     }
       
   267 
       
   268 TUint32 CTestStorageMgr::AllocatePhysicalStorageIdL( TUint aDataProviderId,
       
   269         const CMTPStorageMetaData& aStorage )
       
   270     {
       
   271     PRINTF1( ">CTestStorageMgr::AllocatePhysicalStorageIdL aDataProviderId = %u", aDataProviderId );
       
   272     // Validate the SUID.
       
   273     if ( iStorages.Find( aStorage.DesC( CMTPStorageMetaData::EStorageSuid ),
       
   274             StorageKeyMatchSuid ) != KErrNotFound )
       
   275         {
       
   276         // SUID is not unique.
       
   277         User::Leave( KErrAlreadyExists );
       
   278         }
       
   279     
       
   280     // Allocate a physical StorageId.
       
   281     TInt32 id( AllocatePhysicalStorageId( aDataProviderId ) );
       
   282     User::LeaveIfError( id );
       
   283     
       
   284     // Create the physical storage meta-data.
       
   285     CMTPStorageMetaData* physical( CMTPStorageMetaData::NewLC( aStorage ) );
       
   286     const RArray<TUint> noStorages;
       
   287     physical->SetUint( CMTPStorageMetaData::EStorageId, id );
       
   288     physical->SetUintArrayL( CMTPStorageMetaData::EStorageLogicalIds, 
       
   289             noStorages );
       
   290     
       
   291     // Store the physical storage meta-data.
       
   292     iStorages.InsertInOrderL( physical, StorageOrder );
       
   293     CleanupStack::Pop( physical );
       
   294     
       
   295     PRINTF1( "<CTestStorageMgr::AllocatePhysicalStorageIdL id = 0x%x", id );
       
   296     return id;
       
   297     }
       
   298 
       
   299 TInt CTestStorageMgr::DeallocateLogicalStorageId( TUint aDataProviderId, 
       
   300         TUint32 aLogicalStorageId )
       
   301     {
       
   302     PRINTF2( ">CTestStorageMgr::DeallocateLogicalStorageId aDataProviderId = %d aLogicalStorageId = 0x%x", aDataProviderId, aLogicalStorageId );
       
   303     TInt result = KErrArgument;
       
   304     
       
   305     // Validate the StorageID.
       
   306     if ( LogicalStorageId( aLogicalStorageId ) )
       
   307         {
       
   308         result = iStorages.FindInOrder( aLogicalStorageId, StorageOrder );
       
   309         if ( result != KErrNotFound )
       
   310             {
       
   311             // Validate the storage owner.
       
   312             if ( LogicalStorageOwner( iStorages[result]->
       
   313                     Uint( CMTPStorageMetaData::EStorageId ) ) !=
       
   314                     aDataProviderId )
       
   315                 {
       
   316                 result = KErrAccessDenied;
       
   317                 }
       
   318             else
       
   319                 {
       
   320                 TRAP( result, RemoveLogicalStorageL( result ) );
       
   321                 }
       
   322             }
       
   323         }
       
   324     PRINTF1( "<CTestStorageMgr::DeallocateLogicalStorageId result = %d", result );
       
   325     return result;
       
   326     }
       
   327 
       
   328 void CTestStorageMgr::DeallocateLogicalStorageIds( TUint aDataProviderId,
       
   329         TUint32 aPhysicalStorageId )
       
   330     {
       
   331     PRINTF2( ">CTestStorageMgr::DeallocateLogicalStorageIds aDataProviderId = %d aPhysicalStorageId = 0x%x", aDataProviderId, aPhysicalStorageId );
       
   332     TInt result = iStorages.FindInOrder( aPhysicalStorageId, StorageOrder );
       
   333     if ( result != KErrNotFound )
       
   334         {
       
   335         const RArray<TUint>& logicals( iStorages[result]->
       
   336                 UintArray( CMTPStorageMetaData::EStorageLogicalIds ) );
       
   337         TUint count = logicals.Count();
       
   338         while ( count )
       
   339             {
       
   340             const TUint KIdx = count - 1;
       
   341             if ( LogicalStorageOwner( logicals[KIdx] ) == aDataProviderId )
       
   342                 {
       
   343                 DeallocateLogicalStorageId( aDataProviderId, logicals[KIdx] );
       
   344                 }
       
   345             count--;
       
   346             }
       
   347         }
       
   348     PRINTF0( "<CTestStorageMgr::DeallocateLogicalStorageIds" );
       
   349     }
       
   350 
       
   351 TInt CTestStorageMgr::DeallocatePhysicalStorageId( TUint aDataProviderId,
       
   352         TUint32 aPhysicalStorageId )
       
   353     {
       
   354     PRINTF2( ">CTestStorageMgr::DeallocatePhysicalStorageId aDataProviderId = %u aPhysicalStorageId = 0x%x", aDataProviderId, aPhysicalStorageId );
       
   355     TInt result = KErrArgument;
       
   356     
       
   357     // Validate the StorageID.
       
   358     if ( !LogicalStorageId( aPhysicalStorageId ) )
       
   359         {
       
   360         result = iStorages.FindInOrder( aPhysicalStorageId, StorageOrder );
       
   361         if ( result != KErrNotFound )
       
   362             {
       
   363             // Validate the storage owner.
       
   364             if ( PhysicalStorageOwner( iStorages[result]->
       
   365                     Uint( CMTPStorageMetaData::EStorageId ) ) !=
       
   366                     aDataProviderId )
       
   367                 {
       
   368                 result = KErrAccessDenied;
       
   369                 }
       
   370             else
       
   371                 {
       
   372                 // Deallocate all associated logical storages.
       
   373                 const RArray<TUint>& logicals( iStorages[result]->
       
   374                         UintArray( CMTPStorageMetaData::EStorageLogicalIds ) );
       
   375                 TUint count = logicals.Count();
       
   376                 while ( count )
       
   377                     {
       
   378                     const TUint KIdx = --count;
       
   379                     DeallocateLogicalStorageId( aDataProviderId,
       
   380                             logicals[KIdx] );
       
   381                     }
       
   382                 
       
   383                 // Delete the storage.
       
   384                 delete iStorages[result];
       
   385                 iStorages.Remove( result );
       
   386                 }
       
   387             }
       
   388         }
       
   389     PRINTF1( "<CTestStorageMgr::DeallocatePhysicalStorageId result = %d", result );
       
   390     return result;
       
   391     }
       
   392 
       
   393 TUint32 CTestStorageMgr::DefaultStorageId() const
       
   394     {
       
   395     PRINTF1( "<>CTestStorageMgr::DefaultStorageId iDefaultStorageId = 0x%x", iDefaultStorageId );
       
   396     return iDefaultStorageId;
       
   397     }
       
   398 
       
   399 TInt CTestStorageMgr::DriveNumber( TUint32 aStorageId ) const
       
   400     {
       
   401     PRINTF1( ">CTestStorageMgr::DriveNumber aStorageId = 0x%x", aStorageId );
       
   402     TInt drive = EDriveC; // return C by default
       
   403     if ( PhysicalStorageOwner( aStorageId ) == iFrameworkId )
       
   404         {
       
   405         const TUint32 KPhysicalId = PhysicalStorageId( aStorageId );
       
   406         const TUint KCount = iMapDriveToStorage.Count();
       
   407         for ( TUint i = 0; ( i < KCount ) && ( drive == KErrNotFound ); i++ )
       
   408             {
       
   409             if ( PhysicalStorageId( iMapDriveToStorage[i] ) == KPhysicalId )
       
   410                 {
       
   411                 drive = i;
       
   412                 }
       
   413             }
       
   414         }
       
   415     PRINTF1( "<CTestStorageMgr::DriveNumber drive = %d", drive );
       
   416     return drive;
       
   417     }
       
   418 
       
   419 TInt32 CTestStorageMgr::FrameworkStorageId( TDriveNumber aDriveNumber ) const
       
   420     {
       
   421     PRINTF1( ">CTestStorageMgr::FrameworkStorageId aDriveNumber = %d", aDriveNumber );
       
   422     TInt32 result = KErrNotFound;
       
   423     TInt32 id = iMapDriveToStorage[aDriveNumber];
       
   424     if ( ( id != KErrNotFound ) && ( LogicalStorageId( id ) ) )
       
   425         {
       
   426         result = id;
       
   427         }
       
   428     PRINTF1( "<CTestStorageMgr::FrameworkStorageId id = 0x%x", result );
       
   429     return result;
       
   430     }
       
   431 
       
   432  void CTestStorageMgr::GetAvailableDrivesL(
       
   433          RArray<TDriveNumber>& aDrives ) const
       
   434     {
       
   435     PRINTF0( ">CTestStorageMgr::GetAvailableDrivesL" );
       
   436     aDrives.Reset();
       
   437     for ( TUint i = 0; ( i < iMapDriveToStorage.Count() ); i++ )
       
   438         {
       
   439         if ( iMapDriveToStorage[i] != KErrNotFound )
       
   440             {
       
   441             PRINTV1( "Appending drive %d", i );
       
   442             aDrives.AppendL( static_cast<TDriveNumber>( i ) );
       
   443             }
       
   444         }
       
   445     PRINTF0( "<CTestStorageMgr::GetAvailableDrivesL" );
       
   446     }
       
   447 
       
   448 void CTestStorageMgr::GetLogicalStoragesL(
       
   449         const TMTPStorageMgrQueryParams& aParams,
       
   450         RPointerArray<const CMTPStorageMetaData>& aStorages ) const
       
   451     {
       
   452     PRINTF2( ">CTestStorageMgr::GetLogicalStoragesL suid = %S system type = %d", &aParams.StorageSuid(), aParams.StorageSystemType() );
       
   453     aStorages.Reset();
       
   454     const TBool KAllStorages = ( aParams.StorageSuid() == KNullDesC );
       
   455     const TBool KAllStorageSystemTypes = ( aParams.StorageSystemType() ==
       
   456             CMTPStorageMetaData::ESystemTypeUndefined );
       
   457     const TUint KCount = iStorages.Count();
       
   458     for ( TUint i = 0; i < KCount; i++ )
       
   459         {
       
   460         const CMTPStorageMetaData& storage = *iStorages[i];
       
   461         if ( ( ( KAllStorages ) ||
       
   462                ( storage.DesC( CMTPStorageMetaData::EStorageSuid ) ==
       
   463                        aParams.StorageSuid() ) ) &&
       
   464              ( ( KAllStorageSystemTypes) ||
       
   465                ( storage.Uint( CMTPStorageMetaData::EStorageSystemType ) ==
       
   466                          aParams.StorageSystemType() ) ) &&
       
   467              ( LogicalStorageId(
       
   468                      storage.Uint( CMTPStorageMetaData::EStorageId ) ) ) )
       
   469             {
       
   470             PRINTV1( "Appending storage 0x%x", storage.Uint( CMTPStorageMetaData::EStorageId ) );
       
   471             aStorages.AppendL( iStorages[i] );
       
   472             }
       
   473         }
       
   474     PRINTF0( "<CTestStorageMgr::GetLogicalStoragesL" );
       
   475     }
       
   476 
       
   477 void CTestStorageMgr::GetPhysicalStoragesL( 
       
   478         const TMTPStorageMgrQueryParams& aParams, 
       
   479         RPointerArray<const CMTPStorageMetaData>& aStorages ) const
       
   480     {
       
   481     PRINTF2( ">CTestStorageMgr::GetPhysicalStoragesL suid = %S system type = %d", &aParams.StorageSuid(), aParams.StorageSystemType() );
       
   482     aStorages.Reset();
       
   483     const TBool KAllStorages = ( aParams.StorageSuid() == KNullDesC );
       
   484     const TBool KAllStorageSystemTypes = ( aParams.StorageSystemType() ==
       
   485             CMTPStorageMetaData::ESystemTypeUndefined );
       
   486     const TUint KCount = iStorages.Count();
       
   487     for ( TUint i = 0; i < KCount; i++ )
       
   488         {
       
   489         const CMTPStorageMetaData& storage = *iStorages[i];
       
   490         if ( ( ( KAllStorages ) ||
       
   491                ( storage.DesC( CMTPStorageMetaData::EStorageSuid ) ==
       
   492                        aParams.StorageSuid() ) ) &&
       
   493              ( ( KAllStorageSystemTypes ) ||
       
   494                ( storage.Uint( CMTPStorageMetaData::EStorageSystemType ) ==
       
   495                        aParams.StorageSystemType() ) ) &&
       
   496              ( !LogicalStorageId( 
       
   497                      storage.Uint( CMTPStorageMetaData::EStorageId ) ) ) )
       
   498             {
       
   499             PRINTV1( "Appending storage 0x%x", storage.Uint( CMTPStorageMetaData::EStorageId ) );
       
   500             aStorages.AppendL( iStorages[i] );
       
   501             }
       
   502         }
       
   503     PRINTF0( "<CTestStorageMgr::GetPhysicalStoragesL" );
       
   504     }
       
   505 
       
   506 TUint32 CTestStorageMgr::LogicalStorageId( TUint32 aStorageId ) const
       
   507     {
       
   508     PRINTF1( ">CTestStorageMgr::LogicalStorageId aStorageId = 0x%x", aStorageId );
       
   509     TUint32 id = 1;
       
   510     PRINTF1( "<CTestStorageMgr::LogicalStorageId id = 0x%x", id );
       
   511     return id;
       
   512     }
       
   513 
       
   514 TInt32 CTestStorageMgr::LogicalStorageId( const TDesC& aStorageSuid ) const
       
   515     {
       
   516     PRINTF1( ">CTestStorageMgr::LogicalStorageId aStorageSuid = %S", &aStorageSuid );
       
   517     TInt32 id = KErrNotFound;
       
   518     TInt idx = iStorages.Find( aStorageSuid, StorageKeyMatchSuid );
       
   519     if ( idx != KErrNotFound )
       
   520         {
       
   521         id = iStorages[ idx ]->Uint( CMTPStorageMetaData::EStorageId );
       
   522         if ( !LogicalStorageId( id ) )
       
   523             {
       
   524             id = KErrNotFound;
       
   525             }
       
   526         }
       
   527     PRINTF1( "<CTestStorageMgr::LogicalStorageId id = 0x%x", id );
       
   528     return id;
       
   529     }
       
   530 
       
   531 TInt32 CTestStorageMgr::PhysicalStorageId( TDriveNumber aDriveNumber ) const
       
   532     {
       
   533     PRINTF1( ">CTestStorageMgr::PhysicalStorageId aDriveNumber = %d", aDriveNumber );
       
   534     TInt32 storageId = iMapDriveToStorage[aDriveNumber];
       
   535     if ( storageId != KErrNotFound )
       
   536         {
       
   537         storageId = PhysicalStorageId( storageId );
       
   538         }
       
   539     PRINTF1( "<CTestStorageMgr::PhysicalStorageId storageId = 0x%x", storageId );
       
   540     return storageId;
       
   541     }
       
   542 
       
   543 TUint32 CTestStorageMgr::PhysicalStorageId( TUint32 aStorageId ) const
       
   544     {
       
   545     PRINTF1( ">CTestStorageMgr::PhysicalStorageId aStorageId = 0x%x", aStorageId );
       
   546     TUint32 id = ( aStorageId & KPhysicalIdMask );
       
   547     PRINTF1( "<CTestStorageMgr::PhysicalStorageId id = 0x%x", id );
       
   548     return id;
       
   549     }
       
   550 
       
   551 const CMTPStorageMetaData& CTestStorageMgr::StorageL( 
       
   552         TUint32 aStorageId ) const
       
   553     {
       
   554     PRINTF1( ">CTestStorageMgr::StorageL aStorageId = 0x%x", aStorageId );
       
   555     TInt idx = iStorages.FindInOrder( aStorageId, StorageOrder );
       
   556     User::LeaveIfError( idx );
       
   557     PRINTF1( "<CTestStorageMgr::StorageL idx = %d", idx );
       
   558     return *iStorages[idx];
       
   559     }
       
   560 
       
   561 TUint32 CTestStorageMgr::StorageId( TUint32 aPhysicalStorageId, 
       
   562         TUint32 aLogicalStorageId ) const
       
   563     {
       
   564     PRINTF2( ">CTestStorageMgr::StorageId aPhysicalStorageId = 0x%x aLogicalStorageId = 0x%x", aPhysicalStorageId, aLogicalStorageId );
       
   565     TUint32 id = ( aPhysicalStorageId | aLogicalStorageId );
       
   566     PRINTF1( "<CTestStorageMgr::StorageId id = 0x%x", id );
       
   567     return id;
       
   568     }
       
   569 
       
   570 TBool CTestStorageMgr::ValidStorageId( TUint32 aStorageId ) const
       
   571     {
       
   572     PRINTF1( ">CTestStorageMgr::ValidStorageId aStorageId = 0x%x", aStorageId );
       
   573     TBool valid = 
       
   574             iStorages.FindInOrder( aStorageId, StorageOrder ) != KErrNotFound;
       
   575     PRINTF1( "<CTestStorageMgr::ValidStorageId valid = %d", valid );
       
   576     return valid;
       
   577     }
       
   578 
       
   579 CMTPTypeString* CTestStorageMgr::VolumeIdL( TUint aDataProviderId,
       
   580         TUint32 aStorageId, const TDesC& aVolumeIdSuffix ) const
       
   581     {
       
   582     PRINTF3( ">CTestStorageMgr::VolumeIdL aDataProviderId = %u aStorageId = 0x%x aVolumeIdSuffix = %S", aDataProviderId, aStorageId, &aVolumeIdSuffix );
       
   583     // Validate the StorageId.
       
   584     TUint owner = LogicalStorageId( aStorageId ) ?
       
   585             LogicalStorageOwner( aStorageId ) :
       
   586             PhysicalStorageOwner( aStorageId );
       
   587     if ( !ValidStorageId( aStorageId ) )
       
   588         {
       
   589         User::Leave( KErrNotFound );
       
   590         }
       
   591     else if ( aDataProviderId != owner )
       
   592         {
       
   593         User::Leave( KErrAccessDenied );
       
   594         }
       
   595     
       
   596     // Generate a unique volume ID.
       
   597     RBuf16 buffer;
       
   598     buffer.CreateL( KMTPMaxStringCharactersLength );
       
   599     CleanupClosePushL( buffer );
       
   600     buffer.Format( _L( "%08X" ), aStorageId );
       
   601     
       
   602     if ( aVolumeIdSuffix.Length() != 0 )
       
   603         {
       
   604         // Append the separator and suffix, truncating if necessary.
       
   605         buffer.Append( _L( "-" ) );
       
   606         buffer.Append( aVolumeIdSuffix.Left( KMTPMaxStringCharactersLength - 
       
   607                 buffer.Length() ) );
       
   608         }
       
   609     
       
   610     CMTPTypeString* volumeId = CMTPTypeString::NewL( buffer );
       
   611     CleanupStack::PopAndDestroy( &buffer );
       
   612     
       
   613     PRINTF1( "<CTestStorageMgr::VolumeIdL volumeId = %S", volumeId );
       
   614     return volumeId;
       
   615     }
       
   616 
       
   617 /**
       
   618 Constructor.
       
   619 */
       
   620 CTestStorageMgr::CTestStorageMgr() :
       
   621         iFrameworkId( KErrNotFound )
       
   622     {
       
   623     // implementation not required
       
   624     }
       
   625     
       
   626 /**
       
   627 Second phase constructor.
       
   628 @leave One of the system wide error code, if a processing failure occurs.
       
   629 */
       
   630 void CTestStorageMgr::ConstructL()
       
   631     {
       
   632     PRINTF0( ">CTestStorageMgr::ConstructL" );
       
   633     
       
   634     iFs.Connect();
       
   635     for ( TUint i = 0; i < KMaxDrives; i++ )
       
   636         {
       
   637         iMapDriveToStorage[i] = KErrNotFound;
       
   638         }
       
   639     PRINTF0( "<CTestStorageMgr::ConstructL" );
       
   640     }
       
   641 
       
   642 /**
       
   643 Allocates a new 32-bit logical StorageId for the storage owner as a partition 
       
   644 of the specified physical MTP StorageID.
       
   645 @param aDataProviderId The storage owner data provider identifier.
       
   646 @param aPhysicalStorageId The physical MTP StorageID.
       
   647 @return The new logical StorageId.
       
   648 @return KErrNotFound, if the specified physical MTP StorageID does not exist
       
   649 @return KErrOverflow, if the maximum number of storages would be exceeded.
       
   650 */
       
   651 TInt32 CTestStorageMgr::AllocateLogicalStorageId( TUint aDataProviderId,
       
   652         TUint32 aPhysicalStorageId )
       
   653     {
       
   654     PRINTF2( ">CTestStorageMgr::AllocateLogicalStorageId aDataProviderId = %d aPhysicalStorageId = 0x%x", aDataProviderId, aPhysicalStorageId );
       
   655     TInt result = iStorages.FindInOrder( aPhysicalStorageId, StorageOrder );
       
   656     if ( result != KErrNotFound )
       
   657         {
       
   658         // Scan for the first available storage number.
       
   659         const RArray<TUint>& logicalIds( iStorages[result]->
       
   660                 UintArray( CMTPStorageMetaData::EStorageLogicalIds ) );
       
   661         TUint num = 1;
       
   662         do
       
   663             {
       
   664             result = EncodeLogicalStorageId( aPhysicalStorageId,
       
   665                     aDataProviderId, num );
       
   666             }
       
   667         while ( ( logicalIds.FindInOrder( result ) != KErrNotFound ) &&
       
   668                 ( ++num <= KMaxOwnedStorages ) );
       
   669         
       
   670         if ( num >= KMaxOwnedStorages )
       
   671             {
       
   672             result = KErrOverflow;
       
   673             }
       
   674         }
       
   675     PRINTF1( "<CTestStorageMgr::AllocateLogicalStorageId result = %d", result );
       
   676     return result;
       
   677     }
       
   678 
       
   679 /**
       
   680 Allocates a new 32-bit physical StorageId for the storage owner.
       
   681 @param aDataProviderId The storage owner data provider identifier.
       
   682 @return The new physical StorageId.
       
   683 @return KErrOverflow, if the maximum number of storages would be exceeded.
       
   684 @return One of the system wide error code, if a processing failure occurs.
       
   685 */
       
   686 TInt32 CTestStorageMgr::AllocatePhysicalStorageId( TUint aDataProviderId )
       
   687     {
       
   688     PRINTF1( ">CTestStorageMgr::AllocatePhysicalStorageId aDataProviderId = %d", aDataProviderId );
       
   689     TInt32 result = KErrNone;
       
   690     while ( ( iPhysicalStorageNumbers.Count() < ( aDataProviderId + 1 ) ) && 
       
   691             ( result == KErrNone ) )
       
   692         {
       
   693         result = iPhysicalStorageNumbers.Append( 0 );
       
   694         }
       
   695     
       
   696     if ( result == KErrNone )
       
   697         {
       
   698         if ( iPhysicalStorageNumbers[aDataProviderId] < KMaxOwnedStorages )
       
   699             {
       
   700             result = EncodePhysicalStorageId( aDataProviderId,
       
   701                     ++iPhysicalStorageNumbers[aDataProviderId] );
       
   702             }
       
   703         else
       
   704             {
       
   705             result = KErrOverflow;
       
   706             }
       
   707         }
       
   708     PRINTF1( "<CTestStorageMgr::AllocatePhysicalStorageId result = %d", result );
       
   709     return result;
       
   710     }
       
   711 
       
   712 /**
       
   713 Encodes the specified physical MTP StorageID, data provider identifier, and 
       
   714 storage number as a fully formed MTP StorageID.
       
   715 @param aPhysicalStorageId The physical MTP StorageID.
       
   716 @param aDataProviderId The data provider identifier.
       
   717 @param aStorageNumber The storage number.
       
   718 @return The fully formed MTP StorageID.
       
   719 */  
       
   720 TUint32 CTestStorageMgr::EncodeLogicalStorageId( TUint32 aPhysicalStorageId,
       
   721         TUint aDataProviderId, TUint aStorageNumber )
       
   722     {
       
   723     PRINTF3( ">CTestStorageMgr::EncodeLogicalStorageId aPhysicalStorageId = 0x%x aDataProviderId = %d aStorageNumber = %d", aPhysicalStorageId, aDataProviderId, aStorageNumber );
       
   724     TUint32 id = StorageId( aPhysicalStorageId,
       
   725             EncodeLogicalStorageOwner( aDataProviderId) | 
       
   726             EncodeLogicalStorageNumber( aStorageNumber ) );
       
   727     PRINTF1( "<CTestStorageMgr::EncodeLogicalStorageId id = 0x%x", id );
       
   728     return id;
       
   729     }
       
   730 
       
   731 /**
       
   732 Encodes the storage identifier as the logical storage number in a fully formed 
       
   733 MTP StorageID.
       
   734 @param aStorageNumber The storage number.
       
   735 @return The encoded logical storage number.
       
   736 */  
       
   737 TUint32 CTestStorageMgr::EncodeLogicalStorageNumber( TUint aStorageNumber )
       
   738     {
       
   739     PRINTF1( "<>CTestStorageMgr::EncodeLogicalStorageNumber aStorageNumber = 0x%x", aStorageNumber );
       
   740     return aStorageNumber;
       
   741     }
       
   742 
       
   743 /**
       
   744 Encodes the specified data provider identifier as the logical storage owner 
       
   745 in a fully formed MTP StorageID.
       
   746 @param aDataProviderId The data provider identifier.
       
   747 @return The encoded logical storage owner.
       
   748 */
       
   749 TUint32 CTestStorageMgr::EncodeLogicalStorageOwner( TUint aDataProviderId )
       
   750     {
       
   751     PRINTF1( ">CTestStorageMgr::EncodeLogicalStorageOwner aDataProviderId = %d", aDataProviderId );
       
   752     TUint32 owner = ( aDataProviderId << KLogicalOwnerShift );
       
   753     PRINTF1( "<CTestStorageMgr::EncodeLogicalStorageOwner with result 0x%x", owner );
       
   754     return owner;
       
   755     }
       
   756     
       
   757 /**
       
   758 Encodes the specified data provider identifier and storage number as an  
       
   759 physical MTP StorageID.
       
   760 @param aDataProviderId The data provider identifier.
       
   761 @param aStorageNumber The storage number.
       
   762 @return The encoded physical MTP StorageID.
       
   763 */  
       
   764 TUint32 CTestStorageMgr::EncodePhysicalStorageId( TUint aDataProviderId,
       
   765         TUint aStorageNumber )
       
   766     {
       
   767     PRINTF2( ">CTestStorageMgr::EncodePhysicalStorageId aDataProviderId = %d, aStorageNumber = %d", aDataProviderId, aStorageNumber );
       
   768     TUint32 id = EncodePhysicalStorageOwner( aDataProviderId ) |
       
   769             EncodePhysicalStorageNumber( aStorageNumber );
       
   770     PRINTF1( "<CTestStorageMgr::EncodePhysicalStorageId id = 0x%x", id );
       
   771     return id;
       
   772     }
       
   773 
       
   774 /**
       
   775 Encodes the storage identifier as the physical storage number in a fully formed 
       
   776 MTP StorageID.
       
   777 @param aStorageNumber The storage number.
       
   778 @return The encoded physical storage number.
       
   779 */  
       
   780 TUint32 CTestStorageMgr::EncodePhysicalStorageNumber( TUint aStorageNumber )
       
   781     {
       
   782     PRINTF1( ">CTestStorageMgr::EncodePhysicalStorageNumber aStorageNumber = 0x%x", aStorageNumber );
       
   783     TUint32 number = ( aStorageNumber << KPhysicalNumberShift );
       
   784     PRINTF1( "<CTestStorageMgr::EncodePhysicalStorageNumber number = 0x%x", number );
       
   785     return number;
       
   786     }
       
   787 
       
   788 /**
       
   789 Encodes the specified data provider identifier as the physical storage owner 
       
   790 in a fully formed MTP StorageID.
       
   791 @param aDataProviderId The data provider identifier.
       
   792 @return The encoded physical storage owner.
       
   793 */
       
   794 TUint32 CTestStorageMgr::EncodePhysicalStorageOwner( TUint aDataProviderId )
       
   795     {
       
   796     PRINTF1( ">CTestStorageMgr::EncodePhysicalStorageOwner aDataProviderId = 0x%x", aDataProviderId );
       
   797     TUint32 owner = ( aDataProviderId << KPhysicalOwnerShift );
       
   798     PRINTF1( "<CTestStorageMgr::EncodePhysicalStorageOwner owner = 0x%x", owner );
       
   799     return owner;
       
   800     }
       
   801 
       
   802 /**
       
   803 Removes the logical storages table entry at the specified index.
       
   804 @param aIdx The storages table index.
       
   805 @leave One of the system wide error codes, if a processing failure occurs.
       
   806 */
       
   807 void CTestStorageMgr::RemoveLogicalStorageL( TUint aIdx )
       
   808     {
       
   809     PRINTF1( ">CTestStorageMgr::RemoveLogicalStorageL aIdx = %u", aIdx );
       
   810     TUint32 id = iStorages[aIdx]->Uint( CMTPStorageMetaData::EStorageId );
       
   811     PRINTV1( "Removing storage with id 0x%x", id );
       
   812     // Disassociate the logical and physical storages.
       
   813     CMTPStorageMetaData& physical( StorageMetaDataL( 
       
   814             PhysicalStorageId( id ) ) );
       
   815     RArray<TUint> logicals;
       
   816     CleanupClosePushL( logicals );
       
   817     physical.GetUintArrayL( CMTPStorageMetaData::EStorageLogicalIds,
       
   818             logicals );
       
   819     logicals.Remove( logicals.FindInOrderL( id ) );
       
   820     physical.SetUintArrayL( CMTPStorageMetaData::EStorageLogicalIds,
       
   821             logicals );
       
   822     CleanupStack::PopAndDestroy( &logicals );
       
   823     
       
   824     // Delete the storage.
       
   825     delete iStorages[aIdx];
       
   826     iStorages.Remove( aIdx );
       
   827     PRINTF0( "<CTestStorageMgr::RemoveLogicalStorageL" );
       
   828     }
       
   829 
       
   830 /**
       
   831 Provides a non-const reference to the storage meta-data for the specified 
       
   832 logical MTP StorageID.
       
   833 @param aStorageId The physical or fully formed logical MTP StorageID.
       
   834 @leave KErrNotFound if the specified StorageID does not exist.
       
   835 @leave One of the system wide error codes, if a processing failure occurs.
       
   836 */
       
   837 CMTPStorageMetaData& CTestStorageMgr::StorageMetaDataL( TUint32 aStorageId )
       
   838     {
       
   839     PRINTF1( ">CTestStorageMgr::StorageMetaDataL aStorageId = 0x%x", aStorageId );
       
   840     TInt idx = iStorages.FindInOrder( aStorageId, StorageOrder );
       
   841     User::LeaveIfError( idx );
       
   842     PRINTF1( "<CTestStorageMgr::StorageMetaDataL idx = %d", idx );
       
   843     return *iStorages[idx];
       
   844     }
       
   845 
       
   846 /**
       
   847 Implements a storage key match identity relation using 
       
   848 @see CMTPStorageMetaData::EStorageSuid.
       
   849 @param aSuid The storage SUID key value.
       
   850 @param aStorage The storage meta-data.
       
   851 @return ETrue if the storage matches the key relation, otherwise EFalse.
       
   852 */ 
       
   853 TBool CTestStorageMgr::StorageKeyMatchSuid( const TDesC* aSuid,
       
   854         const CMTPStorageMetaData& aStorage )
       
   855     {
       
   856     //PRINTF2( ">StorageKeyMatchSuid aSuid = %S storage id = 0x%x", &aSuid, aStorage.Uint( CMTPStorageMetaData::EStorageId ) );
       
   857     TBool match = ( *aSuid ==
       
   858             aStorage.DesC( CMTPStorageMetaData::EStorageSuid ) );
       
   859     PRINTF1( "<CTestStorageMgr::StorageKeyMatchSuid match = %d", match );
       
   860     return match;
       
   861     }
       
   862     
       
   863 /**
       
   864 Implements an @see TLinearOrder function for @see CMTPStorageMetaData objects 
       
   865 based on relative @see CMTPStorageMetaData::EStorageId.
       
   866 @param aL The first object instance.
       
   867 @param aR The second object instance.
       
   868 @return Zero, if the two objects are equal; A negative value, if the first 
       
   869 object is less than the second, or; A positive value, if the first object is 
       
   870 greater than the second.
       
   871 */
       
   872 TInt CTestStorageMgr::StorageOrder( const CMTPStorageMetaData& aL,
       
   873         const CMTPStorageMetaData& aR )
       
   874     {
       
   875     PRINTF2( ">CTestStorageMgr::StorageOrder left id = 0x%x right id = 0x%x", aL.Uint( CMTPStorageMetaData::EStorageId ), aR.Uint( CMTPStorageMetaData::EStorageId ) );
       
   876     TInt order = aL.Uint( CMTPStorageMetaData::EStorageId ) -
       
   877                  aR.Uint( CMTPStorageMetaData::EStorageId );
       
   878     PRINTF1( "<CTestStorageMgr::StorageOrder order = %d", order );
       
   879     return order;
       
   880     }
       
   881 
       
   882 /**
       
   883 Implements an @see CMTPStorageMetaData::EStorageId key order function.
       
   884 @param aKey The key value.
       
   885 @param aR The storage meta-data.
       
   886 @return Zero, if the two objects are equal; A negative value, if the first 
       
   887 object is less than the second, or; A positive value, if the first object is 
       
   888 greater than the second.
       
   889 */
       
   890 TInt CTestStorageMgr::StorageOrder( const TUint32* aKey, const CMTPStorageMetaData& aStorage )
       
   891     {
       
   892     PRINTF2( ">CTestStorageMgr::StorageOrder aKey = 0x%x storage id = 0x%x", *aKey, aStorage.Uint( CMTPStorageMetaData::EStorageId ) );
       
   893     TInt order = *aKey - aStorage.Uint( CMTPStorageMetaData::EStorageId );
       
   894     PRINTF1( "<CTestStorageMgr::StorageOrder order = %d", order );
       
   895     return order;
       
   896     }