deviceencryption/DevEncUi/src/DevEncUiFileManager.cpp
branchRCL_3
changeset 21 65326cf895ed
parent 0 6a9f87576119
equal deleted inserted replaced
20:491b3ed49290 21:65326cf895ed
       
     1 /*
       
     2 * Copyright (c) 2005 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:  Implementation of CDevEncUiFileManager.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "DevEncLog.h"
       
    19 #include "DevEncUids.hrh"
       
    20 #include "DevEncUiFileManager.h"
       
    21 
       
    22 #include <pathinfo.h>
       
    23 #include <s32file.h>
       
    24 #include <s32std.h>
       
    25 
       
    26 // --------------------------------------------------------------------------
       
    27 // CDevEncUiFileManager::CDevEncUiFileManager()
       
    28 //
       
    29 // --------------------------------------------------------------------------
       
    30 void CDevEncUiFileManager::ConstructL()
       
    31     {
       
    32     User::LeaveIfError( iFs.Connect() );
       
    33     }
       
    34 
       
    35 // --------------------------------------------------------------------------
       
    36 // CDevEncUiFileManager::CDevEncUiFileManager()
       
    37 //
       
    38 // --------------------------------------------------------------------------
       
    39 CDevEncUiFileManager::~CDevEncUiFileManager()
       
    40     {
       
    41     iFs.Close();
       
    42     }
       
    43 
       
    44 // --------------------------------------------------------------------------
       
    45 // CDevEncUiFileManager::SaveKeyL()
       
    46 //
       
    47 // --------------------------------------------------------------------------
       
    48 void CDevEncUiFileManager::SaveKeyL( CFileStore* aStore,
       
    49                                      const TDesC8& aPkcs5Key ) const
       
    50     {
       
    51     RStoreWriteStream write;
       
    52     
       
    53     aStore->SetTypeL( aStore->Layout() );
       
    54     
       
    55     //write the encryption key to a new stream
       
    56     write.CreateLC( *aStore );
       
    57     write << aPkcs5Key;
       
    58     write.CommitL();
       
    59     CleanupStack::PopAndDestroy(); //CreateLC()
       
    60 
       
    61     aStore->Commit();
       
    62     }
       
    63 
       
    64 // --------------------------------------------------------------------------
       
    65 // CDevEncUiFileManager::LoadKeyL()
       
    66 //
       
    67 // --------------------------------------------------------------------------
       
    68 void CDevEncUiFileManager::LoadKeyL( const TFileName& aFileName,
       
    69                                      HBufC8*& aPkcs5Key )
       
    70     {
       
    71     DFLOG2( "CDevEncUiFileManager::LoadKeyL Filename %S", &aFileName );
       
    72     //prepare to read the streams back in, creating a new TPBEncryptionData
       
    73     RStoreReadStream read;
       
    74      
       
    75     // open the next PFS
       
    76     CFileStore *store = CPermanentFileStore::OpenLC( iFs,
       
    77                                                      aFileName,
       
    78                                                      EFileRead );
       
    79     
       
    80     DFLOG("CPermanentFileStore::OpenLC passed");
       
    81     
       
    82     TStreamId dataStreamId( 1 ); // we know it was the first stream written
       
    83     read.OpenLC( *store, dataStreamId );
       
    84     DFLOG("RStoreReadStream::OpenLC passed");
       
    85     //read in Encryption key
       
    86     aPkcs5Key = HBufC8::NewL( read, KMaxTInt );
       
    87     DFLOG("HBufC8::NewL passed");
       
    88     CleanupStack::Pop(); // read
       
    89     read.Close();
       
    90     CleanupStack::PopAndDestroy( store );
       
    91     }
       
    92 
       
    93 // --------------------------------------------------------------------------
       
    94 // CDevEncUiFileManager::OpenFileStore()
       
    95 //
       
    96 // --------------------------------------------------------------------------
       
    97 TInt CDevEncUiFileManager::OpenFileStore( const TDesC& aFilename,
       
    98                                           CFileStore*& aStore )
       
    99     {
       
   100     // Leaves with KErrAlreadyExists if file exists from before
       
   101     TInt error( KErrNone );
       
   102     CFileStore* store( NULL );
       
   103     TRAP( error, store = CPermanentFileStore::CreateL( iFs,
       
   104                                                 aFilename,
       
   105                                                 EFileRead | EFileWrite ) );
       
   106     if ( !error )
       
   107         {
       
   108         aStore = store;
       
   109         }
       
   110     return error;
       
   111     }
       
   112 
       
   113 // --------------------------------------------------------------------------
       
   114 // CDevEncUiFileManager::GetKeyListL()
       
   115 //
       
   116 // --------------------------------------------------------------------------
       
   117 void CDevEncUiFileManager::GetKeyListL( CDir*& aList )
       
   118     {
       
   119     // Construct file path
       
   120     _LIT( KAsterisk, "*" );
       
   121     _LIT( KDevEncKeyFileExtension, ".pk5");
       
   122     
       
   123     TBuf<KMaxFileName> filePath;
       
   124     User::LeaveIfError( KeyFolder( filePath ) );
       
   125     filePath.Append( KAsterisk );
       
   126     filePath.Append( KDevEncKeyFileExtension );
       
   127     
       
   128     TInt error = iFs.GetDir( filePath,
       
   129                     KEntryAttNormal, // Any file, not hidden and system files
       
   130                     ESortByName,
       
   131                     aList );
       
   132     if ( error )
       
   133         {
       
   134         DFLOG2( "Could not get dir listing, error %d", error );
       
   135         User::Leave( error );
       
   136         }
       
   137     }
       
   138 
       
   139 // --------------------------------------------------------------------------
       
   140 // CDevEncUiFileManager::DriveToChar()
       
   141 //
       
   142 // --------------------------------------------------------------------------
       
   143 TInt CDevEncUiFileManager::DriveToChar( TInt aDrive, TChar &aChar )
       
   144     {
       
   145     return iFs.DriveToChar( aDrive, aChar );
       
   146     }
       
   147 
       
   148 // --------------------------------------------------------------------------
       
   149 // CDevEncUiFileManager::KeyFolder()
       
   150 //
       
   151 // --------------------------------------------------------------------------
       
   152 TInt CDevEncUiFileManager::KeyFolder( TDes& aResult )
       
   153     {
       
   154     DFLOG("KeyFolder()");
       
   155     TInt result( KErrNone );
       
   156     if ( !result )
       
   157         {
       
   158         DFLOG("1st Append");
       
   159         aResult.Append( PathInfo::PhoneMemoryRootPath() );
       
   160         DFLOG("2nd Append");
       
   161         aResult.Append( PathInfo::OthersPath() );
       
   162         }
       
   163     DFLOG2( "CDevEncUiFileManager::KeyFolder %S", &aResult );
       
   164     return result;
       
   165     }
       
   166 
       
   167 // --------------------------------------------------------------------------
       
   168 // CDevEncUiFileManager::RemountMmc()
       
   169 //
       
   170 // --------------------------------------------------------------------------
       
   171 TInt CDevEncUiFileManager::RemountMmc()
       
   172     {
       
   173     return iFs.RemountDrive( /*EDriveE*/EDriveF );
       
   174     }
       
   175 
       
   176 // End of file