iaupdate/IAD/engine/controller/src/iaupdatependingnodesfile.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-2007 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:   CIAUpdatePendingNodesFile
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <bautils.h>
       
    22 #include <s32file.h>
       
    23 #include <sysutil.h>
       
    24 
       
    25 #include "iaupdatependingnodesfile.h"
       
    26 #include "iaupdatectrlfileconsts.h"
       
    27 #include "iaupdateridentifier.h"
       
    28 #include "iaupdatedebug.h"
       
    29 
       
    30 
       
    31 // This constant will be used to check if the IAD application
       
    32 // and its updates are using the same version of the file. If the versions
       
    33 // differ, then this may require additional checking in the future versions
       
    34 // when data is internalized. For example, previous IAD may give old version data 
       
    35 // into the file during self update.
       
    36 const TInt KVersion( 1 );
       
    37 
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CIAUpdatePendingNodesFile::NewL
       
    41 //
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 EXPORT_C CIAUpdatePendingNodesFile* CIAUpdatePendingNodesFile::NewL()
       
    45     {
       
    46     CIAUpdatePendingNodesFile* self =
       
    47         CIAUpdatePendingNodesFile::NewLC();
       
    48     CleanupStack::Pop( self );
       
    49     return self;
       
    50     }
       
    51 
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CIAUpdatePendingNodesFile::NewLC
       
    55 //
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 EXPORT_C CIAUpdatePendingNodesFile* CIAUpdatePendingNodesFile::NewLC()
       
    59     {
       
    60     CIAUpdatePendingNodesFile* self =
       
    61         new( ELeave) CIAUpdatePendingNodesFile();
       
    62     CleanupStack::PushL( self );
       
    63     self->ConstructL();
       
    64     return self;    
       
    65     }
       
    66 
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CIAUpdatePendingNodesFile::CIAUpdatePendingNodesFile
       
    70 //
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CIAUpdatePendingNodesFile::CIAUpdatePendingNodesFile()
       
    74 : CBase()
       
    75     {
       
    76     }
       
    77 
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CIAUpdatePendingNodesFile::ConstructL
       
    81 //
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 void CIAUpdatePendingNodesFile::ConstructL()
       
    85     {
       
    86     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePendingNodesFile::ConstructL start");
       
    87     
       
    88     User::LeaveIfError( iFsSession.Connect() );
       
    89     User::LeaveIfError( 
       
    90         iFsSession.SetSessionToPrivate( 
       
    91             IAUpdateCtrlFileConsts::KDrive ) );    
       
    92     User::LeaveIfError( iFsSession.SessionPath( iPath ) );
       
    93     BaflUtils::EnsurePathExistsL( iFsSession, iPath );
       
    94     iPath.Append( IAUpdateCtrlFileConsts::KPendingNodesFile() );
       
    95     
       
    96     IAUPDATE_TRACE_1("[IAUPDATE] CIAUpdatePendingNodesFile::ConstructL end: %S", &iPath);
       
    97     }
       
    98 
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CIAUpdatePendingNodesFile::~CIAUpdatePendingNodesFile
       
   102 //
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 EXPORT_C CIAUpdatePendingNodesFile::~CIAUpdatePendingNodesFile()
       
   106     {
       
   107     iFsSession.Close();
       
   108     
       
   109     Reset();
       
   110     }
       
   111 
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CIAUpdatePendingNodesFile::Reset
       
   115 //
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 EXPORT_C void CIAUpdatePendingNodesFile::Reset()
       
   119     {
       
   120     iIndex = 0;
       
   121     iPendingNodes.ResetAndDestroy();
       
   122     }
       
   123 
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CIAUpdatePendingNodesFile::Index
       
   127 //
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 EXPORT_C TInt CIAUpdatePendingNodesFile::Index() const
       
   131     {
       
   132     return iIndex;
       
   133     }           
       
   134 
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CIAUpdatePendingNodesFile::SetIndex
       
   138 //
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 EXPORT_C void CIAUpdatePendingNodesFile::SetIndex( TInt aIndex )
       
   142     {
       
   143     iIndex = aIndex;
       
   144     }
       
   145 
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CIAUpdatePendingNodesFile::Results
       
   149 //
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 EXPORT_C RPointerArray< CIAUpdaterIdentifier >& CIAUpdatePendingNodesFile::PendingNodes()
       
   153     {
       
   154     return iPendingNodes;
       
   155     }
       
   156     
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CIAUpdatePendingNodesFile::FilePath
       
   160 //
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 EXPORT_C const TDesC& CIAUpdatePendingNodesFile::FilePath() const 
       
   164     {
       
   165     return iPath;
       
   166     }
       
   167 
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CIAUpdatePendingNodesFile::SetFilePathL
       
   171 //
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 EXPORT_C void CIAUpdatePendingNodesFile::SetFilePathL( const TDesC& aPath )
       
   175     {
       
   176     IAUPDATE_TRACE_1("[IAUPDATE] CIAUpdatePendingNodesFile::SetFilePathL old path: %S", &iPath);
       
   177 
       
   178     TParsePtrC path( aPath );
       
   179 
       
   180     if ( !path.NamePresent() )
       
   181         {
       
   182         IAUPDATE_TRACE("[IAUPDATE] No file name");
       
   183         // Do not accept path that does not contain file name.
       
   184         User::Leave( KErrArgument );
       
   185         }
       
   186 
       
   187     if ( path.PathPresent() || path.DrivePresent() )
       
   188         {
       
   189         IAUPDATE_TRACE("[IAUPDATE] Path present");
       
   190         if ( path.DrivePresent() )
       
   191             {
       
   192             IAUPDATE_TRACE("[IAUPDATE] Drive present");
       
   193             User::LeaveIfError( iFsSession.SetSessionPath( path.DriveAndPath() ) );
       
   194             // Because the path and the drive were given, the given parameter path
       
   195             // can be used as it was given.
       
   196             iPath.Copy( aPath );
       
   197             }
       
   198         else
       
   199             {
       
   200             IAUPDATE_TRACE("[IAUPDATE] Only path present");
       
   201             User::LeaveIfError( iFsSession.SetSessionPath( path.Path() ) );
       
   202             // Because the given path was missing the drive letter,
       
   203             // get the correct path with some default drive letter from the
       
   204             // session.
       
   205             User::LeaveIfError( iFsSession.SessionPath( iPath ) );
       
   206             // Append the file name to the end of the path.
       
   207             iPath.Append( path.NameAndExt() );
       
   208             }
       
   209         }
       
   210     else
       
   211         {
       
   212         IAUPDATE_TRACE("[IAUPDATE] Path was not present");
       
   213         // Only the file name was given. So, use the session path to get the whole
       
   214         // path of the file.
       
   215         User::LeaveIfError( iFsSession.SessionPath( iPath ) );
       
   216         iPath.Append( aPath );        
       
   217         }
       
   218 
       
   219     IAUPDATE_TRACE_1("[IAUPDATE] CIAUpdatePendingNodesFile::SetFilePathL new path: %S", &iPath);
       
   220     }
       
   221 
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CIAUpdatePendingNodesFile::ReadDataL
       
   225 //
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 EXPORT_C TBool CIAUpdatePendingNodesFile::ReadDataL()
       
   229 	{
       
   230     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePendingNodesFile::ReadDataL begin");
       
   231     
       
   232 	RFile file;
       
   233     TInt err( file.Open( iFsSession, iPath, EFileRead ) );
       
   234     User::LeaveIfError( err );
       
   235     	
       
   236     CleanupClosePushL( file );
       
   237     
       
   238     RFileReadStream stream( file, 0 );
       
   239     CleanupClosePushL( stream );
       
   240 
       
   241     InternalizeL( stream );
       
   242 
       
   243     CleanupStack::PopAndDestroy( &stream );
       
   244     CleanupStack::PopAndDestroy( &file );
       
   245 
       
   246     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePendingNodesFile::ReadDataL end");
       
   247     
       
   248     return ETrue;
       
   249 	}
       
   250 
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CIAUpdatePendingNodesFile::WriteDataL
       
   254 //
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 EXPORT_C void CIAUpdatePendingNodesFile::WriteDataL()
       
   258 	{
       
   259     IAUPDATE_TRACE_1("[IAUPDATE] CIAUpdatePendingNodesFile::ReadDataL path: %S", &iPath);
       
   260     TInt sizeOfFile = 2 * sizeof( TInt ); //version, count of identifiers
       
   261     
       
   262     TInt count( iPendingNodes.Count() );
       
   263 	for( TInt i = 0; i < count; ++i )
       
   264 	    {
       
   265 	    CIAUpdaterIdentifier* identifier( iPendingNodes[ i ] );
       
   266 	    sizeOfFile += sizeof( TInt ) // version 
       
   267 	                + identifier->Id().Size() //Id
       
   268 	                + sizeof( TInt ) //Id length
       
   269 	                + identifier->Namespace().Size() //namespace
       
   270 	                + sizeof( TInt ); //namespace length
       
   271 	    }
       
   272     if ( SysUtil::DiskSpaceBelowCriticalLevelL( 
       
   273             &iFsSession, 
       
   274 	        sizeOfFile, 
       
   275 	        IAUpdateCtrlFileConsts::KDrive ) )
       
   276 	    {
       
   277 		User::Leave( KErrDiskFull );
       
   278 	    }
       
   279     
       
   280 	RFile file;
       
   281     User::LeaveIfError( file.Replace( iFsSession, iPath, EFileWrite ) );
       
   282     CleanupClosePushL( file );
       
   283     
       
   284     RFileWriteStream stream( file, 0 );
       
   285     CleanupClosePushL( stream );
       
   286 
       
   287     ExternalizeL( stream );
       
   288     stream.CommitL();
       
   289     
       
   290     CleanupStack::PopAndDestroy( &stream );
       
   291     CleanupStack::PopAndDestroy( &file );
       
   292 
       
   293 
       
   294     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePendingNodesFile::WriteDataL end");
       
   295 	}
       
   296 
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // CIAUpdatePendingNodesFile::RemoveFile
       
   300 //
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 EXPORT_C TInt CIAUpdatePendingNodesFile::RemoveFile()
       
   304     {
       
   305     IAUPDATE_TRACE_1("[IAUPDATE] CIAUpdatePendingNodesfile::RemoveFile path: %S", &iPath);
       
   306     
       
   307     // May either be a full path, or relative to the session path.
       
   308     // Even if session path has already been set in the ConstructL,
       
   309     // we do not use the relative path and only the hard coded filename here. 
       
   310     // A new path may have been given by calling SetFilePathL. So, use that
       
   311     // value here.
       
   312     TInt errorCode( BaflUtils::DeleteFile( iFsSession, iPath ) );
       
   313 
       
   314     IAUPDATE_TRACE_1("[IAUPDATE] CIAUpdatePendingNodesfile::RemoveFile end: %d", errorCode);
       
   315     
       
   316     return errorCode;
       
   317     }
       
   318 
       
   319 
       
   320 // -----------------------------------------------------------------------------
       
   321 // CIAUpdatePendingNodesFile::InternalizeL
       
   322 //
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 void CIAUpdatePendingNodesFile::InternalizeL( RReadStream& aStream )
       
   326 	{
       
   327 	// If you make changes here, 
       
   328 	// remember to update ExternalizeL accordingly!!!
       
   329 
       
   330     TInt version( aStream.ReadInt32L() );
       
   331 
       
   332     // Notice! In the future, some checking maybe required here
       
   333     // to be sure that file version is correct and the data can be
       
   334     // internalized correctly between different versions.
       
   335     if ( version != KVersion )
       
   336         {
       
   337         // For now, just leave with the corrupt error.
       
   338         User::Leave( KErrCorrupt );
       
   339         }
       
   340 
       
   341     iPendingNodes.ResetAndDestroy();
       
   342 	TInt count( aStream.ReadInt32L() );
       
   343 	for( TInt i = 0; i < count; ++i )
       
   344 	    {
       
   345 	    CIAUpdaterIdentifier* identifier( CIAUpdaterIdentifier::NewLC() );
       
   346 	    identifier->InternalizeL( aStream );
       
   347         iPendingNodes.AppendL( identifier );
       
   348         CleanupStack::Pop( identifier ); 
       
   349 	    }
       
   350 
       
   351     iIndex = aStream.ReadInt32L();
       
   352 	}
       
   353 
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 // CIAUpdatePendingNodesFile::ExternalizeL
       
   357 //
       
   358 // -----------------------------------------------------------------------------
       
   359 //
       
   360 void CIAUpdatePendingNodesFile::ExternalizeL( RWriteStream& aStream )
       
   361 	{
       
   362 	// If you make changes here, 
       
   363 	// remember to update InternalizeL accordingly!!!
       
   364 
       
   365     aStream.WriteInt32L( KVersion );
       
   366 
       
   367     TInt count( iPendingNodes.Count() );
       
   368 	aStream.WriteInt32L( count );	
       
   369 	for( TInt i = 0; i < count; ++i )
       
   370 	    {
       
   371 	    CIAUpdaterIdentifier* identifier( iPendingNodes[ i ] );
       
   372 	    identifier->ExternalizeL( aStream );
       
   373 	    }
       
   374 
       
   375     aStream.WriteInt32L( iIndex );
       
   376 	}