commonservices/PlatformEnv/src/driveinfo.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Holds the drive information of the system.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "driveinfo.h"
       
    22 #include "driveinfopatchdata.h"
       
    23 #include "platformenvdebug.h"
       
    24 
       
    25 // CONSTANTS
       
    26 const TInt KMaxFsName = 256;
       
    27 _LIT( KMsFsName, "MassStorageFileSystem" );
       
    28 
       
    29 
       
    30 // ============================= LOCAL FUNCTIONS ===============================
       
    31 // -----------------------------------------------------------------------------
       
    32 // IsUserHiddenDrive
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 static TBool IsUserHiddenDrive( TInt aDrive )
       
    36     {
       
    37     FUNC_LOG
       
    38 
       
    39     return ( aDrive >= 0 &&
       
    40              ( aDrive == KDriveInfoUserHiddenDrive1 ||
       
    41                aDrive == KDriveInfoUserHiddenDrive2 ||
       
    42                aDrive == KDriveInfoUserHiddenDrive3 ||
       
    43                aDrive == KDriveInfoUserHiddenDrive4 ||
       
    44                aDrive == KDriveInfoUserHiddenDrive5 ||
       
    45                aDrive == KDriveInfoUserHiddenDrive6 ||
       
    46                aDrive == KDriveInfoUserHiddenDrive7 ||
       
    47                aDrive == KDriveInfoUserHiddenDrive8 ) );
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // IsUsbDrive
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 static TBool IsUsbDrive( TInt aDrive )
       
    55     {
       
    56     FUNC_LOG
       
    57 
       
    58     return ( aDrive >= 0 &&
       
    59              aDrive >= KDriveInfoUsbDrivesStart &&
       
    60              aDrive < KDriveInfoUsbDrivesStart + KDriveInfoUsbDrivesMaxCount );
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // ExternallyMountableInternalDrive
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 static TBool ExternallyMountableInternalDrive( TInt aDrive, TInt aDriveAtt )
       
    68     {
       
    69     FUNC_LOG
       
    70 
       
    71     if ( aDriveAtt & KDriveAttLogicallyRemovable )
       
    72         {
       
    73         return ETrue;
       
    74         }
       
    75 
       
    76     return ( aDrive >= 0 &&
       
    77              ( aDrive == KDriveInfoLogicallyRemovableDrive1 ||
       
    78                aDrive == KDriveInfoLogicallyRemovableDrive2 ||
       
    79                aDrive == KDriveInfoLogicallyRemovableDrive3 ||
       
    80                aDrive == KDriveInfoLogicallyRemovableDrive4 ) );
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // MassStorageFsInUse
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 static TBool MassStorageFsInUse( TInt aDrive, TInt aDriveAtt, RFs& aFs )
       
    88     {
       
    89     FUNC_LOG
       
    90 
       
    91     if ( !( aDriveAtt & KDriveAttRemovable ) )
       
    92         {
       
    93         return EFalse;
       
    94         }
       
    95     HBufC* buffer = HBufC::New( KMaxFsName );
       
    96     if ( !buffer )
       
    97         {
       
    98         return EFalse;
       
    99         }
       
   100     TBool ret( EFalse );
       
   101     TPtr ptr( buffer->Des() );
       
   102     if ( aFs.FileSystemName( ptr, aDrive ) == KErrNone )
       
   103         {
       
   104         if ( !ptr.CompareF( KMsFsName ) )
       
   105             {
       
   106             ret = ETrue;
       
   107             }
       
   108         }
       
   109     delete buffer;
       
   110     return ret;
       
   111     }
       
   112 
       
   113 
       
   114 // ============================ MEMBER FUNCTIONS ===============================
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // DriveInfo::GetDefaultDrive
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 EXPORT_C TInt DriveInfo::GetDefaultDrive( TInt aDefaultDrive, TInt& aDrive )
       
   121     {
       
   122     FUNC_LOG_WITH_CLIENT_NAME
       
   123 
       
   124     TInt ret( KErrNone );
       
   125     switch ( aDefaultDrive )
       
   126         {
       
   127         case DriveInfo::EDefaultRom:
       
   128             ret = KDriveInfoDefaultRomDrive;
       
   129             break;
       
   130         case DriveInfo::EDefaultRam:
       
   131             ret = KDriveInfoDefaultRamDrive;
       
   132             break;
       
   133         case DriveInfo::EDefaultSystem:
       
   134             ret = KDriveInfoDefaultSystemDrive >= 0 ?
       
   135                   KDriveInfoDefaultSystemDrive : RFs::GetSystemDrive();
       
   136             break;
       
   137         case DriveInfo::EDefaultPhoneMemory:
       
   138             ret = KDriveInfoDefaultPhoneMemory;
       
   139             break;
       
   140         case DriveInfo::EDefaultMassStorage:
       
   141             ret = KDriveInfoDefaultMassStorage;
       
   142             break;
       
   143         case DriveInfo::EDefaultRemovableMassStorage:
       
   144             ret = KDriveInfoDefaultRemovableMassStorage;
       
   145             break;
       
   146         default:
       
   147             ret = KErrArgument;
       
   148             break;
       
   149         }
       
   150 
       
   151     if ( ret >= 0 )
       
   152         {
       
   153         aDrive = ret;
       
   154         ret = KErrNone;
       
   155         }
       
   156 
       
   157     INFO_LOG2( "DriveInfo::GetDefaultDrive-aDefaultDrive=%d,aDrive=%d",
       
   158         aDefaultDrive, aDrive )
       
   159 
       
   160     LOG_IF_ERROR2( ret,
       
   161         "DriveInfo::GetDefaultDrive-aDefaultDrive=%d,ret=%d",
       
   162         aDefaultDrive, ret )
       
   163 
       
   164     return ret;
       
   165     }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // DriveInfo::GetDriveStatus
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 EXPORT_C TInt DriveInfo::GetDriveStatus(
       
   172         RFs& aFs, TInt aDrive, TUint& aStatus )
       
   173     {
       
   174     FUNC_LOG_WITH_CLIENT_NAME
       
   175 
       
   176     aStatus = 0;
       
   177 
       
   178     if ( !RFs::IsValidDrive( aDrive ) )
       
   179         {
       
   180         ERROR_LOG2( "DriveInfo::GetDriveStatus-aDrive=%d,IsValidDriveRet=%d",
       
   181             aDrive, KErrArgument )
       
   182 
       
   183         return KErrArgument;
       
   184         }
       
   185 
       
   186     // Get volume info
       
   187     TVolumeInfo volumeInfo;
       
   188     TInt ret( aFs.Volume( volumeInfo, aDrive ) );
       
   189 
       
   190     INFO_LOG2( "DriveInfo::GetDriveStatus-aDrive=%d,VolumeInfoRet=%d",
       
   191         aDrive, ret )
       
   192 
       
   193     switch( ret )
       
   194         {
       
   195         case KErrNone:
       
   196             {
       
   197             if ( volumeInfo.iDrive.iType != EMediaNotPresent )
       
   198                 {
       
   199                 aStatus |= EDrivePresent;
       
   200                 if( volumeInfo.iSize )
       
   201                     {
       
   202                     aStatus |= EDriveFormatted;
       
   203                     }
       
   204                 }
       
   205             else
       
   206                 {
       
   207                 ERROR_LOG1(
       
   208                     "DriveInfo::GetDriveStatus-aDrive=%d,InvalidVolume",
       
   209                     aDrive )
       
   210                 ret = KErrNotReady;
       
   211                 }
       
   212             break;
       
   213             }
       
   214         case KErrLocked:
       
   215             {
       
   216             aStatus |= EDrivePresent | EDriveLocked | EDriveFormatted |
       
   217                 EDriveLockable | EDriveHasPassword;
       
   218             break;
       
   219             }
       
   220         case KErrCorrupt:
       
   221             {
       
   222             aStatus |= EDrivePresent | EDriveCorrupt;
       
   223             break;
       
   224             }
       
   225         case KErrInUse:
       
   226             {
       
   227             aStatus |= EDrivePresent | EDriveInUse;
       
   228             break;
       
   229             }
       
   230         default:
       
   231             {
       
   232             break;
       
   233             }
       
   234         }
       
   235 
       
   236     // Get drive info if not already done by volume info read
       
   237     TDriveInfo& driveInfo( volumeInfo.iDrive );
       
   238     if ( ret != KErrNone )
       
   239         {
       
   240         TInt err( aFs.Drive( driveInfo, aDrive ) );
       
   241 
       
   242         INFO_LOG2( "DriveInfo::GetDriveStatus-aDrive=%d,DriveInfoRet=%d",
       
   243             aDrive, err )
       
   244 
       
   245         if ( err != KErrNone )
       
   246             {
       
   247             ERROR_LOG2( "DriveInfo::GetDriveStatus-aDrive=%d,DriveInfoRet=%d",
       
   248                 aDrive, err )
       
   249 
       
   250             return err; // Nothing can be done
       
   251             }
       
   252 
       
   253         // Mass storage mode may return some other error than KErrInUse in 
       
   254         // volume info read. Check is mass storage file system in use.
       
   255         if( driveInfo.iType != EMediaNotPresent &&
       
   256             ret != KErrInUse &&
       
   257             MassStorageFsInUse( aDrive, driveInfo.iDriveAtt, aFs ) )
       
   258             {
       
   259             INFO_LOG1( "DriveInfo::GetDriveStatus-aDrive=%d,DriveInUse",
       
   260                 aDrive )
       
   261 
       
   262             aStatus |= EDrivePresent | EDriveInUse;
       
   263             }
       
   264         }
       
   265 
       
   266     INFO_LOG3(
       
   267         "DriveInfo::GetDriveStatus-DriveAttr=%d,MediaAttr=%d,MediaType=%d",
       
   268         driveInfo.iDriveAtt,
       
   269         driveInfo.iMediaAtt,
       
   270         driveInfo.iType )
       
   271 
       
   272     // If drive attributes are set, suppress volume read error
       
   273     if ( driveInfo.iDriveAtt )
       
   274         {
       
   275         ret = KErrNone;
       
   276         }
       
   277 
       
   278     if ( !IsUserHiddenDrive( aDrive ) )
       
   279         {
       
   280         aStatus |= EDriveUserVisible;
       
   281         }
       
   282 
       
   283     // Check media type
       
   284     TInt defaultRom( KErrNotFound );
       
   285     GetDefaultDrive( EDefaultRom, defaultRom );
       
   286     TInt defaultPhoneMem( KErrNotFound );
       
   287     GetDefaultDrive( EDefaultPhoneMemory, defaultPhoneMem );
       
   288 
       
   289     if ( driveInfo.iType == EMediaRom || defaultRom == aDrive )
       
   290         {
       
   291         aStatus |= EDriveRom | EDriveReadOnly;
       
   292         }
       
   293     // Default phone memory should always be non volatile media.
       
   294     // If it is defined to RAM media, there is something wrong in 
       
   295     // media configurations.
       
   296     else if ( aDrive != defaultPhoneMem )
       
   297         {
       
   298         TInt defaultRam( KErrNotFound );
       
   299         GetDefaultDrive( EDefaultRam, defaultRam );
       
   300         if ( driveInfo.iType == EMediaRam || defaultRam == aDrive )
       
   301             {
       
   302             aStatus |= EDriveRam;
       
   303             }
       
   304         }
       
   305 
       
   306     // Check drive attributes
       
   307 #ifdef __WINS__
       
   308     // On emulator, handle memory card defined as local drive in epoc ini
       
   309     // that has invalid drive attributes
       
   310     TInt defaultRemovable( KErrNotFound );
       
   311     GetDefaultDrive( EDefaultRemovableMassStorage, defaultRemovable );
       
   312 #endif // __WINS__
       
   313 
       
   314     if ( ( driveInfo.iDriveAtt & KDriveAttInternal )
       
   315 #ifdef __WINS__
       
   316           && ( defaultRemovable != aDrive ) 
       
   317 #endif // __WINS__
       
   318           )
       
   319         {
       
   320         aStatus |= EDriveInternal;
       
   321 
       
   322         // Note that internal mass storage may have 'internal' file
       
   323         // server attribute
       
   324         if ( ExternallyMountableInternalDrive( aDrive, driveInfo.iDriveAtt ) )
       
   325             {
       
   326             aStatus |= EDriveExternallyMountable;
       
   327             }
       
   328         }
       
   329     else if ( ( driveInfo.iDriveAtt & KDriveAttRemovable )
       
   330 #ifdef __WINS__
       
   331              || ( defaultRemovable == aDrive ) 
       
   332 #endif // __WINS__
       
   333              )
       
   334         {
       
   335         // Note that internal mass storage may have 'removable' file
       
   336         // server attribute
       
   337         if ( ExternallyMountableInternalDrive( aDrive, driveInfo.iDriveAtt ) )
       
   338             {
       
   339             aStatus |= EDriveInternal | EDriveExternallyMountable;
       
   340             }
       
   341         else
       
   342             {
       
   343             aStatus |= EDriveRemovable | EDriveExternallyMountable |
       
   344                 EDriveSwEjectable;
       
   345             if ( IsUsbDrive( aDrive ) )
       
   346                 {
       
   347                 aStatus |= EDriveUsbMemory;
       
   348                 }
       
   349             }
       
   350         }
       
   351     else if ( driveInfo.iDriveAtt & KDriveAttRemote )
       
   352         {
       
   353         aStatus |= EDriveRemote | EDriveExternallyMountable;
       
   354         }
       
   355 
       
   356     if ( driveInfo.iDriveAtt & KDriveAttSubsted )
       
   357         {
       
   358         aStatus |= EDriveSubsted;
       
   359         }
       
   360 
       
   361     // Check media attributes
       
   362     if ( driveInfo.iMediaAtt & KMediaAttLocked )
       
   363         {
       
   364         aStatus |= EDriveLocked;
       
   365         }
       
   366 
       
   367     if ( driveInfo.iMediaAtt & KMediaAttWriteProtected )
       
   368         {
       
   369         aStatus |= EDriveReadOnly;
       
   370         }
       
   371 
       
   372     if ( !( aStatus & EDriveReadOnly ) &&
       
   373         ( driveInfo.iMediaAtt & KMediaAttFormattable ) )
       
   374         {
       
   375         aStatus |= EDriveFormattable;
       
   376         }
       
   377 
       
   378     if ( !( aStatus & EDriveInternal ) &&
       
   379         ( driveInfo.iMediaAtt & KMediaAttLockable ) )
       
   380         {
       
   381         aStatus |= EDriveLockable;
       
   382         }
       
   383 
       
   384     if ( driveInfo.iMediaAtt & EDriveHasPassword )
       
   385         {
       
   386         aStatus |= EDriveHasPassword;
       
   387         }
       
   388 
       
   389     INFO_LOG3( "DriveInfo::GetDriveStatus-aDrive=%d,aStatus=0x%x,ret=%d",
       
   390         aDrive, aStatus, ret )
       
   391 
       
   392     LOG_IF_ERROR2( ret, "DriveInfo::GetDriveStatus-aDrive=%d,ret=%d",
       
   393         aDrive, ret )
       
   394 
       
   395     return ret;
       
   396     }
       
   397 
       
   398 // -----------------------------------------------------------------------------
       
   399 // DriveInfo::GetUserVisibleDrives
       
   400 // -----------------------------------------------------------------------------
       
   401 //
       
   402 EXPORT_C TInt DriveInfo::GetUserVisibleDrives(
       
   403         RFs& aFs, TDriveList& aDriveList, TInt& aDriveCount )
       
   404     {
       
   405     FUNC_LOG_WITH_CLIENT_NAME
       
   406 
       
   407     return GetUserVisibleDrives(
       
   408         aFs, aDriveList, aDriveCount, KDriveAttExclude | KDriveAttRemote );
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // DriveInfo::GetUserVisibleDrives
       
   413 // -----------------------------------------------------------------------------
       
   414 //
       
   415 EXPORT_C TInt DriveInfo::GetUserVisibleDrives(
       
   416         RFs& aFs, TDriveList& aDriveList, TInt& aDriveCount, TUint aFlags )
       
   417     {
       
   418     FUNC_LOG_WITH_CLIENT_NAME
       
   419 
       
   420     aDriveCount = 0;
       
   421 
       
   422     TInt ret( aFs.DriveList( aDriveList, aFlags ) );
       
   423     if ( ret == KErrNone )
       
   424         {
       
   425         aDriveCount = StripUserHiddenDrives( aDriveList );
       
   426         }
       
   427 
       
   428     INFO_LOG2( "DriveInfo::GetUserVisibleDrives-aDriveCount=%d,aFlags=%d",
       
   429         aDriveCount, aFlags )
       
   430 
       
   431     LOG_IF_ERROR1( ret, "DriveInfo::GetUserVisibleDrives-ret=%d", ret )
       
   432 
       
   433     return ret;
       
   434     }
       
   435 
       
   436 // -----------------------------------------------------------------------------
       
   437 // DriveInfo::StripUserHiddenDrives
       
   438 // -----------------------------------------------------------------------------
       
   439 //
       
   440 EXPORT_C TInt DriveInfo::StripUserHiddenDrives( TDriveList& aDriveList )
       
   441     {
       
   442     FUNC_LOG_WITH_CLIENT_NAME
       
   443 
       
   444     TInt ret( 0 );
       
   445     TInt count( aDriveList.Length() );
       
   446     for( TInt i( 0 ); i < count; ++i )
       
   447         {
       
   448         if ( aDriveList[ i ] )
       
   449             {
       
   450             if ( !IsUserHiddenDrive( i ) )
       
   451                 {
       
   452                 ++ret;
       
   453                 }
       
   454             else
       
   455                 {
       
   456                 aDriveList[ i ] = 0; // Remove user hidden drive
       
   457                 }
       
   458             }
       
   459         }
       
   460     return ret;
       
   461     }
       
   462 
       
   463 // -----------------------------------------------------------------------------
       
   464 // DriveInfo::DriveCount
       
   465 // -----------------------------------------------------------------------------
       
   466 //
       
   467 EXPORT_C TInt DriveInfo::DriveCount( const TDriveList& aDriveList )
       
   468     {
       
   469     FUNC_LOG_WITH_CLIENT_NAME
       
   470 
       
   471     TInt ret( 0 );
       
   472     TInt count( aDriveList.Length() );
       
   473     for( TInt i( 0 ); i < count; ++i )
       
   474         {
       
   475         if ( aDriveList[ i ] )
       
   476             {
       
   477             ++ret;
       
   478             }
       
   479         }
       
   480     return ret;
       
   481     }
       
   482 
       
   483 // -----------------------------------------------------------------------------
       
   484 // DriveInfo::TDriveArray::Set
       
   485 // -----------------------------------------------------------------------------
       
   486 //
       
   487 EXPORT_C void DriveInfo::TDriveArray::Set( const TDriveList& aDriveList )
       
   488     {
       
   489     FUNC_LOG_WITH_CLIENT_NAME
       
   490 
       
   491     iArray.Zero();
       
   492     TInt count( Min( aDriveList.Length(), KMaxDrives ) );
       
   493     for( TInt i( 0 ); i < count; ++i )
       
   494         {
       
   495         if ( aDriveList[ i ] )
       
   496             {
       
   497             iArray.Append( static_cast< TUint8 >( i ) );
       
   498             }
       
   499         }
       
   500     }
       
   501 
       
   502 // -----------------------------------------------------------------------------
       
   503 // DriveInfo::TDriveArray::LetterAt
       
   504 // -----------------------------------------------------------------------------
       
   505 //
       
   506 EXPORT_C TChar DriveInfo::TDriveArray::LetterAt( TInt aIndex ) const
       
   507     {
       
   508     FUNC_LOG_WITH_CLIENT_NAME
       
   509 
       
   510     TChar ret( 0 );
       
   511     // Ignore return value, Set() guarantees that valid drives are used
       
   512     RFs::DriveToChar( iArray[ aIndex ], ret );
       
   513     return ret;
       
   514     }
       
   515 
       
   516 // -----------------------------------------------------------------------------
       
   517 // DriveInfo::GetUserVisibleDrives
       
   518 // -----------------------------------------------------------------------------
       
   519 //
       
   520 EXPORT_C TInt DriveInfo::GetUserVisibleDrives(
       
   521         RFs& aFs, TDriveArray& aDriveArray )
       
   522     {
       
   523     FUNC_LOG_WITH_CLIENT_NAME
       
   524 
       
   525     return GetUserVisibleDrives( aFs, aDriveArray, KDriveAttAll );
       
   526     }
       
   527 
       
   528 // -----------------------------------------------------------------------------
       
   529 // DriveInfo::GetUserVisibleDrives
       
   530 // -----------------------------------------------------------------------------
       
   531 //
       
   532 EXPORT_C TInt DriveInfo::GetUserVisibleDrives(
       
   533         RFs& aFs, TDriveArray& aDriveArray, TUint aFlags )
       
   534     {
       
   535     FUNC_LOG_WITH_CLIENT_NAME
       
   536 
       
   537     TDriveList drvList;
       
   538     TInt ret( aFs.DriveList( drvList, aFlags ) );
       
   539     if ( ret == KErrNone )
       
   540         {
       
   541         StripUserHiddenDrives( drvList );
       
   542         aDriveArray.Set( drvList );
       
   543         }
       
   544     else
       
   545         {
       
   546         aDriveArray.Reset();
       
   547         }
       
   548 
       
   549     INFO_LOG2( "DriveInfo::GetUserVisibleDrives-aDriveArrayCount=%d,aFlags=%d",
       
   550         aDriveArray.Count(), aFlags )
       
   551 
       
   552     LOG_IF_ERROR1( ret, "DriveInfo::GetUserVisibleDrives-ret=%d", ret )
       
   553 
       
   554     return ret;
       
   555     }
       
   556 
       
   557 // -----------------------------------------------------------------------------
       
   558 // DriveInfo::GetDefaultDrive
       
   559 // -----------------------------------------------------------------------------
       
   560 //
       
   561 EXPORT_C TInt DriveInfo::GetDefaultDrive(
       
   562         TInt aDefaultDrive, TChar& aDriveLetter )
       
   563     {
       
   564     FUNC_LOG_WITH_CLIENT_NAME
       
   565 
       
   566     TInt drive( 0 );
       
   567     TInt ret( GetDefaultDrive( aDefaultDrive, drive ) );
       
   568     if ( ret == KErrNone )
       
   569         {
       
   570         ret = RFs::DriveToChar( drive, aDriveLetter );
       
   571         }
       
   572 
       
   573     LOG_IF_ERROR1( ret, "DriveInfo::GetDefaultDrive-ret=%d", ret )
       
   574 
       
   575     return ret;
       
   576     }
       
   577 
       
   578 //  End of File