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