os/kernelhwsrv/baseapitest/basesvs/validation/f32/sfsrv/src/T_RDirData.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-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: 
    15 *
    16 */
    17 
    18 
    19 /**
    20 @test
    21 @internalComponent
    22 v
    23 This contains CT_RDirData
    24 */
    25 
    26 //	User includes
    27 #include "T_RDirData.h"
    28 #include "T_SfSrvServer.h"
    29 #include "FileserverUtil.h"
    30 
    31 
    32 
    33 /*@{*/
    34 ///	Parameters
    35 _LIT(KPath,										"path");
    36 _LIT(KTUidType,									"uidtype");
    37 _LIT(KRFsObjectName,							"rfsObject");
    38 _LIT(KAsync,									"async");
    39 _LIT(KArrayObjectName,							"arrayObject");
    40 _LIT(KEntryObjectName,							"entryObject");
    41 _LIT(KEntryAttMask,								"attmask");
    42 _LIT(KParamExpectedFileName,					"filename%d");
    43 _LIT(KReadArray,								"readarray");
    44 _LIT(KCompareFiles,								"compare");
    45 
    46 
    47 ///Commands
    48 _LIT(KCmdNew,									"new");
    49 _LIT(KCmdDestructor,							"~");
    50 _LIT(KCmdOpen,									"Open");
    51 _LIT(KCmdRead,									"Read");
    52 _LIT(KCmdClose,									"Close");
    53 
    54 //Constants
    55 const TInt	KBufferLength		= 64;
    56 
    57 CT_RDirData* CT_RDirData::NewL()
    58 /**
    59 * Two phase constructor
    60 */
    61 	{
    62 	CT_RDirData* ret = new (ELeave) CT_RDirData();
    63 	CleanupStack::PushL(ret);
    64 	ret->ConstructL();
    65 	CleanupStack::Pop(ret);
    66 	return ret;
    67 	}
    68 
    69 
    70 
    71 CT_RDirData::CT_RDirData()
    72 :	iRDir(NULL)
    73 ,	iRead(NULL)
    74 ,	iReadBlock(NULL)
    75 ,	iEntry(NULL)
    76 ,	iExpectedNames(NULL)
    77 ,	iObjName(NULL)
    78 ,	iCompare(NULL)
    79 /**
    80 * Protected constructor. First phase construction
    81 */
    82 	{
    83 	}
    84 
    85 
    86 void CT_RDirData::ConstructL()
    87 /**
    88 * Protected constructor. Second phase construction
    89 */
    90 	{
    91 	iRead = CActiveCallback::NewL( *this );
    92 	iReadBlock = CActiveCallback::NewL( *this );
    93 	iExpectedNames = new(ELeave) RPointerArray<TPath>();
    94 	}
    95 
    96 
    97 CT_RDirData::~CT_RDirData()
    98 /**
    99 * Destructor.
   100 */
   101 	{
   102 	DoCleanup();
   103 	}
   104 
   105 
   106 TAny* CT_RDirData::GetObject()
   107 	{
   108 	return iRDir;
   109 	}
   110 
   111 
   112 TBool CT_RDirData::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt aAsyncErrorIndex)
   113 /**
   114 * Process a command read from the ini file
   115 *
   116 * @param aCommand	the command to process
   117 * @param aSection		the entry in the ini file requiring the command to be processed
   118 *
   119 * @return ETrue if the command is processed
   120 */
   121 	{
   122 	TBool ret = ETrue;
   123 
   124 	if (aCommand == KCmdNew)
   125 		{
   126 		DoCmdNew();
   127 		}
   128 	else if (aCommand == KCmdOpen)
   129 		{
   130 		DoCmdOpenL(aSection);
   131 		}
   132 	else if (aCommand == KCmdRead)
   133 		{
   134 		DoCmdReadL(aSection, aAsyncErrorIndex);
   135 		}
   136 	else if (aCommand == KCmdClose)
   137 		{
   138 		DoCmdClose();
   139 		}
   140 	else if (aCommand == KCmdDestructor)
   141 		{
   142 		DoCmdDestructor();
   143 		}
   144 	else
   145 		{
   146 		ret = EFalse;
   147 		}
   148 	return ret;
   149 	}
   150 
   151 void CT_RDirData::DoCleanup()
   152 	{
   153 	INFO_PRINTF1(_L("Deleting current RDir"));
   154 	delete iEntry;
   155 	iEntry = NULL;
   156 	delete iRDir;
   157 	iRDir = NULL;
   158 
   159 	delete iRead;
   160 	iRead = NULL;
   161 	delete iReadBlock;
   162 	iReadBlock = NULL;
   163 	iExpectedNames->ResetAndDestroy();	
   164 	}
   165 
   166 void CT_RDirData::DoCmdNew()
   167 	{
   168 	INFO_PRINTF1(_L("Create new RDirs class instance"));
   169 
   170 	delete iRDir;
   171 	iRDir = NULL;
   172 
   173 	TRAPD(err, iRDir = new (ELeave) RDir());
   174 	if ( err != KErrNone )
   175 		{
   176 		ERR_PRINTF2(_L("new error %d"), err);
   177 		SetError( err );
   178 		}
   179 	}
   180 	
   181 void CT_RDirData::DoCmdClose()
   182 /** 
   183 *	Close RDir handle 
   184 */
   185 	{
   186 	INFO_PRINTF1(_L("Closing RDir"));
   187 	iRDir->Close();
   188 	}
   189 
   190 void CT_RDirData::DoCmdDestructor()
   191 /** 
   192 *	Destroy RDir the object 
   193 */
   194 	{
   195 	INFO_PRINTF1(_L("Destroying the RDir object"));
   196 	DoCleanup();
   197 	}
   198 	
   199 void CT_RDirData::DoCmdOpenL(const TDesC& aSection)
   200 	{		
   201 	INFO_PRINTF1(_L("Opening directory!"));
   202 	
   203 	TBool	dataOk = ETrue;
   204 		
   205 	TPtrC	rfsObjectName;
   206 	RFs*    rfsObject = NULL;
   207 	if(GET_MANDATORY_STRING_PARAMETER(KRFsObjectName(), aSection, rfsObjectName))
   208 	    {
   209 	    rfsObject = (RFs*)GetDataObjectL(rfsObjectName);
   210 	    }
   211 	else
   212 		{
   213 		dataOk = EFalse;
   214 		}
   215 	    
   216 	TPtrC path;
   217 	if(!GET_MANDATORY_STRING_PARAMETER(KPath(), aSection, path))
   218 		{
   219 		dataOk = EFalse;
   220 		}
   221 	
   222 	TUidType	uidType = KNullUid;
   223 	TInt		intUIDType;
   224 	TBool		isUidType = EFalse;
   225 
   226 	TUint		attMask = KEntryAttNormal;
   227 	INFO_PRINTF2(_L("section name - %S"),&aSection);
   228 	
   229 	if(!FileserverUtil::GetAttMask(*this, aSection, KEntryAttMask(), attMask))
   230 		{
   231 		if (GET_MANDATORY_INT_PARAMETER(KTUidType(), aSection, intUIDType))
   232 			{
   233     		TUid id = TUid::Uid(intUIDType);
   234 	   		uidType = TUidType(id);
   235 	   		INFO_PRINTF2(_L("UID type set to %d"), uidType[0].iUid);
   236 	   		isUidType = ETrue;
   237 			}
   238 		else
   239 			{
   240 			dataOk = EFalse;
   241 			ERR_PRINTF2(_L("attmask or %S must be declared !!!"), &KTUidType);
   242 			}
   243 		}
   244 		
   245 	if (dataOk)	
   246 		{
   247 		TInt 	err = KErrNone;
   248 		
   249 		if (isUidType)
   250 			{
   251 			err = iRDir->Open(*rfsObject, path, uidType);
   252 			}
   253 		else
   254 			{
   255 			err = iRDir->Open(*rfsObject, path, attMask);
   256 			}
   257 				
   258 		if (err != KErrNone)
   259 			{
   260 			INFO_PRINTF2(_L("Open() error: %d"), err);
   261 			SetError(err);
   262 			}
   263 		else
   264 			{
   265 			INFO_PRINTF1(_L("Directory has been opened!"));
   266 			}
   267 		}
   268 	}
   269 	
   270 void CT_RDirData::DoCmdReadL(const TDesC& aSection, const TInt aAsyncErrorIndex)
   271 	{
   272 	INFO_PRINTF1(_L("Reading directory!"));
   273 	
   274 	TBool 	async 		= EFalse;
   275 	TBool	readArray	= EFalse;
   276 	TBool	compare		= EFalse;
   277 	
   278 	GET_OPTIONAL_BOOL_PARAMETER(KAsync(), aSection, async);
   279 	
   280 	if(GET_OPTIONAL_BOOL_PARAMETER(KReadArray(),aSection,readArray))
   281 		{
   282 		ReadExpectedNamesL( aSection);
   283 		}
   284 		
   285 	if(GET_OPTIONAL_BOOL_PARAMETER(KCompareFiles(),aSection,compare))
   286 		{
   287 		iCompare = compare;
   288 		}
   289 
   290 	TPtrC entryArrayName;
   291 	if (GET_OPTIONAL_STRING_PARAMETER(KArrayObjectName(), aSection, entryArrayName))
   292 		{
   293 		TEntryArray* entryArray = new(ELeave) TEntryArray();
   294 		CleanupStack::PushL(entryArray);
   295 		
   296 		CT_EntryArrayData* entryArrayWrapper = 
   297 			static_cast<CT_EntryArrayData*> (GetDataWrapperL(entryArrayName));
   298 		if (entryArrayWrapper)
   299 			{
   300 			entryArrayWrapper->SetObjectL(entryArray);
   301 			}
   302 		else
   303 			{
   304 			ERR_PRINTF2(_L("Not initialized %S"), &entryArrayName);
   305 			SetBlockResult(EFail);
   306 			}
   307 		CleanupStack::Pop(entryArray);
   308 			
   309 		if (async)
   310 			{
   311 			iObjName = entryArrayName;
   312 			iRDir->Read(*entryArray, iReadBlock->iStatus);
   313 			iReadBlock->Activate(aAsyncErrorIndex);
   314 			IncOutstanding();
   315 			}
   316 		else
   317 			{
   318 			TInt err = iRDir->Read(*entryArray);
   319 			
   320 			if((err == KErrNone) || (err == KErrEof))
   321 				{
   322 				INFO_PRINTF1(_L("Read function completed successfully"));
   323 				SetError(err);
   324 				if(compare)
   325 					{
   326 					CompareEntryArray(entryArray);
   327 					}
   328 				}
   329 			else
   330 				{
   331 				ERR_PRINTF2(_L("Read function failed with error: %d"), err);
   332 				SetError(err);	
   333 				}
   334 			}
   335 		}
   336 	else
   337 		{
   338 		TPtrC entryName;
   339 		if (GET_MANDATORY_STRING_PARAMETER(KEntryObjectName(), aSection, entryName))
   340 			{
   341 			TEntry* entry = new(ELeave) TEntry();
   342 			CleanupStack::PushL(entry);
   343 			
   344 			CT_EntryData* entryWrapper = 
   345 				static_cast<CT_EntryData*> (GetDataWrapperL(entryName));
   346 			
   347 			if (entryWrapper)
   348 				{
   349 				entryWrapper->SetObjectL(entry);
   350 				}
   351 			else
   352 				{
   353 				ERR_PRINTF2(_L("Not initialized %S"), &entryName);
   354 				SetBlockResult(EFail);
   355 				}
   356 			
   357 			CleanupStack::Pop(entry);
   358 			
   359 			if (async)
   360 				{
   361 				iObjName = entryName;
   362 				delete iEntry;
   363 				iEntry = NULL;
   364 				iEntry = new(ELeave) TPckg<TEntry>(*entry);
   365 				iRDir->Read(*iEntry, iRead->iStatus);
   366 				iRead->Activate(aAsyncErrorIndex);
   367 				IncOutstanding();
   368 				}
   369 			else
   370 				{
   371 				TInt err = iRDir->Read(*entry);
   372 			
   373 				if((err == KErrNone) || (err == KErrEof))
   374 					{
   375 					INFO_PRINTF1(_L("Read function completed successfully"));
   376 					SetError(err);
   377 					if(compare)
   378 						{
   379 						CompareEntryData(entry);
   380 						}
   381 					}
   382 				else
   383 					{
   384 					ERR_PRINTF2(_L("Read function failed with error: %d"), err);
   385 					SetError(err);	
   386 					}
   387 				}
   388 			}
   389 		}
   390 	}
   391 	
   392 		
   393 void CT_RDirData::ReadExpectedNamesL( const TDesC& aSection )
   394 /**
   395 *	Read list of expected file names from ili file
   396 */
   397 	{
   398 	iExpectedNames->ResetAndDestroy();
   399 
   400 	TBool moreData = EFalse;
   401 	TInt index = 0;
   402 	do
   403 		{
   404 		TBuf<KBufferLength> tempStore;
   405 		tempStore.Format(KParamExpectedFileName(), ++index);
   406 		TPtrC fileName;
   407 		moreData = GET_OPTIONAL_STRING_PARAMETER(tempStore, aSection, fileName);
   408 		if (moreData)
   409 			{
   410 			TPath* path = new(ELeave) TPath(fileName);
   411 			CleanupStack::PushL(path);
   412 			iExpectedNames->AppendL(path);
   413 			CleanupStack::Pop();
   414 			}
   415 		}
   416 	while (moreData);
   417 	}
   418 
   419 
   420 void CT_RDirData::CompareEntryArray(TEntryArray* aEntryArray)
   421 /**
   422 *	Compare TEntryArray entryes with list of expected files
   423 */
   424 	{
   425 	if(aEntryArray->Count() == iExpectedNames->Count())
   426 		{
   427 		for(TInt i = 0; i < aEntryArray->Count(); i++)
   428 			{
   429 			TBool eof = EFalse;
   430 			for(TInt k = 0; !eof && (k < iExpectedNames->Count()); k++)
   431 				{
   432 				TEntry* tmpEntry = new(ELeave) TEntry();
   433 				*tmpEntry = aEntryArray->operator[](i);
   434 				if(*(iExpectedNames->operator[](k)) == tmpEntry->iName)
   435 					{
   436 					TPath* name = iExpectedNames->operator[](k);
   437 					INFO_PRINTF3( _L( "Entry name = expected name, %S = %S"), &tmpEntry->iName, name);
   438 					
   439 					iExpectedNames->Remove(k);
   440 					delete name;
   441 					eof = ETrue;	
   442 					}
   443 					
   444 				delete tmpEntry;
   445 				}
   446 			}
   447 		if(iExpectedNames->Count() > 0)
   448 			{
   449 			ERR_PRINTF1(_L("TEntryArray members are not equal to expected"));
   450 			SetBlockResult(EFail);
   451 			}
   452 		else
   453 			{
   454 			INFO_PRINTF1( _L( "TEntryArray members are equal to expected"));
   455 			}
   456 		}
   457 	else if(aEntryArray->Count() > iExpectedNames->Count())
   458 		{
   459 		ERR_PRINTF1(_L("Found unexpecded file(s)"));
   460 		SetBlockResult(EFail);
   461 		}
   462 	else if(aEntryArray->Count() < iExpectedNames->Count())
   463 		{
   464 		ERR_PRINTF1(_L("List of expected files more than amount of real files"));
   465 		SetBlockResult(EFail);
   466 		}
   467 	}
   468 	
   469 void CT_RDirData::CompareEntryData(TEntry* aEntry)
   470 /**
   471 *	Compare TEntry with entry in list of expected files
   472 */
   473 	{
   474 	TBool eof = EFalse;	
   475 	TBool fileCompare = EFalse;
   476 	for(TInt i = 0; !eof && (i < iExpectedNames->Count()); i++)
   477 		{
   478 		if(*(iExpectedNames->operator[](i)) == aEntry->iName)
   479 			{
   480 			TPath* name = iExpectedNames->operator[](i);
   481 			fileCompare = ETrue;
   482 				
   483 			iExpectedNames->Remove(i);
   484 			delete name;
   485 			eof = ETrue;
   486 			}
   487 		}
   488 	if(!fileCompare)
   489 		{
   490 		ERR_PRINTF2(_L("Unexpected file - %S"),&aEntry->iName);
   491 		SetBlockResult(EFail);
   492 		}
   493 	}
   494 	
   495 
   496 void CT_RDirData::RunL(CActive* aActive, TInt aIndex)
   497 	{	
   498 	INFO_PRINTF1(_L("RunL Called!"));
   499 	
   500 	if ((aActive == iReadBlock) || (aActive == iRead))
   501 		{
   502 
   503 		TInt err = aActive->iStatus.Int();
   504 		if (err == KErrNone)
   505 			{
   506 			TEntry	&actualEntry = iEntry->operator()();
   507 			INFO_PRINTF2(_L("Asynchronous Read function completed successfully, %S"),&(actualEntry.iName));
   508 			}
   509 		else if (err == KErrEof)
   510 			{
   511 			INFO_PRINTF1(_L("Asynchronous Read function reached the end and returned expectedly KErrEof"));
   512 			SetAsyncError(aIndex, err);
   513 			}
   514 		else
   515 			{
   516 			ERR_PRINTF2(_L("RunL Error: %d"), err);
   517 			SetAsyncError(aIndex, err);
   518 			}
   519 		
   520 
   521 		if(iCompare && ((err == KErrNone) || (err == KErrEof)))
   522 			{
   523 			if(aActive == iReadBlock)
   524 				{
   525 				TEntryArray *entryArray = static_cast<TEntryArray*>(GetDataObjectL(iObjName));
   526 				if (entryArray)
   527 					{
   528 					CompareEntryArray(entryArray);
   529 					}
   530 				else
   531 					{
   532 					SetBlockResult(EFail);
   533 					ERR_PRINTF2(_L("Empty Object: %S"), &iObjName);
   534 					}
   535 				}
   536 			else if(aActive == iRead)
   537 				{
   538 				TEntry* entry = static_cast<TEntry*>(GetDataObjectL(iObjName));
   539 				if (entry)
   540 					{
   541 					CompareEntryData(entry);
   542 					}
   543 				else
   544 					{
   545 					SetBlockResult(EFail);
   546 					ERR_PRINTF2(_L("Empty Object: %S"), &iObjName);
   547 					}
   548 				}
   549 			}
   550 
   551 		delete iEntry;
   552 		iEntry = NULL;
   553 		
   554 		DecOutstanding();
   555 		}
   556 	else
   557 		{
   558  		ERR_PRINTF1( _L("Stray RunL signal") );
   559  		SetBlockResult( EFail );
   560 		}
   561 	}
   562 	
   563 void CT_RDirData::DoCancel(CActive* aActive, TInt aIndex)
   564 	{
   565 	INFO_PRINTF1(_L("DoCancel Called!"));
   566 	
   567 	if ( aActive == iRead )
   568 		{	
   569 		TInt err = aActive->iStatus.Int();
   570 		if(err != KErrNone )
   571 			{
   572 			ERR_PRINTF2(_L("DoCancel Error: %d"), err);
   573 			SetAsyncError(aIndex, err);
   574 			}
   575 		DecOutstanding();
   576 		}
   577 	else
   578 		{
   579  		ERR_PRINTF1( _L("Stray RunL signal") );
   580  		SetBlockResult( EFail );
   581 		}	
   582 	}