kerneltest/f32test/cfileman/t_cfileman_aux.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2008-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 the License "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 // f32test\cfileman\t_cfileman_aux.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "t_cfileman_aux.h"
       
    19 
       
    20 CFileMan* gFileMan = NULL;
       
    21 RPointerArray<RFile>* gFileHandles = NULL;
       
    22 TBool gAsynch = EFalse;
       
    23 TRequestStatus gStat;
       
    24 TBool testingInvalidPathLengths;
       
    25 
       
    26 void InitialiseL()
       
    27 	{
       
    28 	gFileMan=CFileMan::NewL(TheFs);
       
    29 	}
       
    30 
       
    31 void RmDir(const TDesC& aDirName)
       
    32 	{
       
    33 	TFileName filename_dir = aDirName;
       
    34 	TInt r = 0;
       
    35 	r = TheFs.SetAtt(filename_dir, 0, KEntryAttReadOnly);
       
    36 	r=gFileMan->RmDir(filename_dir);
       
    37 	test(r==KErrNone || r==KErrNotFound || r==KErrPathNotFound);
       
    38 	}
       
    39 
       
    40 // Cleanup test variables
       
    41 void Cleanup()
       
    42 	{
       
    43 	delete gFileMan;
       
    44 	}
       
    45 
       
    46 //Test that the contents of two directories are identical
       
    47 TBool CompareL(const TDesC& aDir1,const TDesC& aDir2)
       
    48 	{
       
    49 	TBool rel = ETrue;
       
    50 	CDirScan* scanDir1=CDirScan::NewL(TheFs);
       
    51 	scanDir1->SetScanDataL(aDir1,KEntryAttMaskSupported,ESortByName);
       
    52 	CDirScan* scanDir2=CDirScan::NewL(TheFs);
       
    53 	scanDir2->SetScanDataL(aDir2,KEntryAttMaskSupported,ESortByName);
       
    54 
       
    55 	FOREVER
       
    56 		{
       
    57 		CDir* entryList1;
       
    58 		CDir* entryList2;
       
    59 
       
    60 		scanDir1->NextL(entryList1);
       
    61 		scanDir2->NextL(entryList2);
       
    62 
       
    63 		if (entryList1==NULL || entryList2==NULL)
       
    64 			{
       
    65 			if (!(entryList1==NULL && entryList2==NULL))
       
    66 				{
       
    67 				delete entryList1, 
       
    68 				delete entryList2, 
       
    69 				delete scanDir1, 
       
    70 				delete scanDir2;
       
    71 				return(EFalse);
       
    72 				}
       
    73 			break;
       
    74 			}
       
    75 
       
    76 		TFileName abbPath1=scanDir1->AbbreviatedPath();
       
    77 		TFileName abbPath2=scanDir2->AbbreviatedPath();
       
    78 		if (!(abbPath1==abbPath2))
       
    79 			{
       
    80 			delete entryList1, 
       
    81 			delete entryList2, 
       
    82 			delete scanDir1, 
       
    83 			delete scanDir2;
       
    84 			return(EFalse);
       
    85 			}
       
    86 
       
    87 		TInt count1=entryList1->Count();
       
    88 		TInt count2=entryList2->Count();
       
    89 		if (!(count1==count2))
       
    90 			{
       
    91 			delete entryList1, 
       
    92 			delete entryList2, 
       
    93 			delete scanDir1, 
       
    94 			delete scanDir2;
       
    95 			return(EFalse);
       
    96 			}
       
    97 
       
    98 		while(count1--)
       
    99 			{
       
   100 			TEntry entry1=(*entryList1)[count1];
       
   101 			TEntry entry2=(*entryList2)[count1];
       
   102 			if (!(entry1.iName==entry2.iName))
       
   103 				{
       
   104 				delete entryList1, 
       
   105 				delete entryList2, 
       
   106 				delete scanDir1, 
       
   107 				delete scanDir2;
       
   108 				return(EFalse);
       
   109 				}
       
   110 			if (!(entry1.iAtt==entry2.iAtt))
       
   111 				{
       
   112 				delete entryList1, 
       
   113 				delete entryList2, 
       
   114 				delete scanDir1, 
       
   115 				delete scanDir2;
       
   116 				return(EFalse);
       
   117 				}
       
   118 
       
   119 			}
       
   120 
       
   121 		delete entryList1;
       
   122 		delete entryList2;
       
   123 		}
       
   124 
       
   125 	delete scanDir1;
       
   126 	delete scanDir2;
       
   127 	
       
   128 	return rel;
       
   129 	}
       
   130 
       
   131 /**
       
   132     Parsing Dir Data Block
       
   133     @param  aDataBlock		data block in TInt[] for parsing	
       
   134     @param  aDirDataArray	returning dir data array after parsing
       
   135 
       
   136     @panic 					if data setup error
       
   137 
       
   138 */
       
   139 void ParsingDirDataBlock(const TInt aDataBlock[], RArray<TInt>& aDirDataArray)
       
   140 	{
       
   141 	TInt err = KErrNone;
       
   142 	aDirDataArray.Reset();
       
   143 
       
   144 	if (aDataBlock[0] == EOB)
       
   145 		{
       
   146 		return;
       
   147 		}
       
   148 
       
   149 	TInt i = 1;
       
   150 	FOREVER
       
   151 		{
       
   152 		TInt lastItem = aDataBlock[i-1];
       
   153 		TInt currentItem = aDataBlock[i];
       
   154 		
       
   155 		// check currentItem
       
   156 		if (currentItem == EOB)
       
   157 			{
       
   158 			if (lastItem == CON || lastItem > LAST)
       
   159 			//check last
       
   160 				{
       
   161 				test.Printf(_L("ERROR<SetupDir>: wrong dir data setup! err=%d\n"), err);
       
   162 				test(EFalse);
       
   163 				}
       
   164 			else
       
   165 			// passed, insert last, break
       
   166 				{
       
   167 				err = aDirDataArray.InsertInOrder(lastItem);
       
   168 				if (err != KErrNone && err != KErrAlreadyExists)
       
   169 					{
       
   170 					test.Printf(_L("ERROR<SetupDir>: wrong dir data setup! err=%d\n"), err);
       
   171 					test(EFalse);
       
   172 					}
       
   173 				break;
       
   174 				}
       
   175 			}
       
   176 		
       
   177 		else if (currentItem == CON)
       
   178 		// if current == CON
       
   179 			{
       
   180 			if (lastItem == CON || lastItem >= LAST)
       
   181 			// check last item
       
   182 				{
       
   183 				test.Printf(_L("ERROR<SetupDir>: wrong dir data setup! err=%d\n"), err);
       
   184 				test(EFalse);
       
   185 				}
       
   186 			else // last < LAST, last != CON
       
   187 				{
       
   188 				// check next item
       
   189 				TInt nextItem = aDataBlock[i+1];
       
   190 				if (nextItem <= 0 || nextItem > LAST || lastItem >= nextItem)
       
   191 					{
       
   192 					test.Printf(_L("ERROR<SetupDir>: wrong dir data setup! err=%d\n"), err);
       
   193 					test(EFalse);
       
   194 					}
       
   195 				else
       
   196 					{
       
   197 					// all valid
       
   198 					for (TInt j = lastItem; j < nextItem; j++)
       
   199 						{
       
   200 						err = aDirDataArray.InsertInOrder(j);
       
   201 						if (err != KErrNone && err != KErrAlreadyExists)
       
   202 							{
       
   203 							test.Printf(_L("ERROR<SetupDir>: wrong dir data setup! err=%d\n"), err);
       
   204 							test(EFalse);
       
   205 							}
       
   206 						}
       
   207 					}
       
   208 				}
       
   209 			i++;
       
   210 			}
       
   211 
       
   212 		else if (0 <= currentItem && currentItem <= LAST)
       
   213 		// if current == normal item
       
   214 			{
       
   215 			if (lastItem == CON)
       
   216 				{
       
   217 				i++;
       
   218 				continue;
       
   219 				}
       
   220 			else if (lastItem >= LAST)
       
   221 			// check last item
       
   222 				{
       
   223 				test.Printf(_L("ERROR<SetupDir>: wrong dir data setup! err=%d\n"), err);
       
   224 				test(EFalse);
       
   225 				}
       
   226 			else
       
   227 			// passed, insert last
       
   228 				{
       
   229 				err = aDirDataArray.InsertInOrder(lastItem);
       
   230 				if (err != KErrNone && err != KErrAlreadyExists)
       
   231 					{
       
   232 					test.Printf(_L("ERROR<SetupDir>: wrong dir data setup! err=%d\n"), err);
       
   233 					test(EFalse);
       
   234 					}
       
   235 				}
       
   236 			i++;
       
   237 			}
       
   238 		
       
   239 		else	// invalid input
       
   240 			{
       
   241 			test.Printf(_L("ERROR<SetupDir>: wrong dir data setup! err=%d\n"), err);
       
   242 			test(EFalse);
       
   243 			}
       
   244 		}
       
   245 	}
       
   246 
       
   247 	
       
   248 /**
       
   249 	Setup attribs:
       
   250 	@param  filenamedir   		root path of dir data or a file data
       
   251 	@param  mode			    value of an mode to be set(Normal/Open/Shared..)
       
   252 */
       
   253 void OpenFile(TDesC& aFile, TFileMode aFileMode)
       
   254 	{
       
   255 	RFile* file = new (ELeave) RFile;
       
   256 	gFileHandles->Append(file);
       
   257 	TInt ret = 0;
       
   258 	ret = file->Open(TheFs, aFile, aFileMode);
       
   259 	test(ret==KErrNone);
       
   260 	}
       
   261 	
       
   262 
       
   263 /**
       
   264     Setup dir structure for testing and verifying functional results
       
   265     @param	datastr			data structure to setup directory
       
   266     @param  iOperation   	Operation to be performed 
       
   267     @param  SrcDrive		Source drive
       
   268     @param	Targetdrive		Target drive input
       
   269     @panic					if data structure definition is incorrect
       
   270 */
       
   271 void SetupDirFiles(const TDesC& aPath, const TDirSetupFiles& aDirFiles)
       
   272 	{
       
   273 	TFileName path = aPath;
       
   274 	if (path.Length() == 0)
       
   275 		{
       
   276 		test.Printf(_L("ERROR<SetupDirFiles()>: Zero length src path!\n"));
       
   277 		test(EFalse);
       
   278 		}
       
   279 	
       
   280 	RmDir(path);
       
   281 	MakeDir(path);
       
   282 
       
   283 	RArray<TInt> addBlockDataArray;
       
   284 	RArray<TInt> deductBlockDataArray;
       
   285 	
       
   286 	ParsingDirDataBlock(aDirFiles.iAddingBlock, addBlockDataArray);
       
   287 	ParsingDirDataBlock(aDirFiles.iDeductBlock, deductBlockDataArray);
       
   288 	
       
   289 	if (addBlockDataArray.Count() == 0)
       
   290 	// empty dir setup
       
   291 		{
       
   292 		return;
       
   293 		}
       
   294 	else
       
   295 		{
       
   296 		for (TInt i = 0; i < deductBlockDataArray.Count(); ++i)
       
   297 			{
       
   298 			TInt idxToDelete = addBlockDataArray.FindInOrder(deductBlockDataArray[i]);
       
   299 			if (idxToDelete >= 0)
       
   300 				{
       
   301 				addBlockDataArray.Remove(idxToDelete);
       
   302 				}
       
   303 			else if (idxToDelete == KErrNotFound)
       
   304 				{
       
   305 				continue;
       
   306 				}
       
   307 			else
       
   308 				{
       
   309 				test.Printf(_L("ERROR<<SetupDir>>: wrong dir data setup! err=%d\n"), idxToDelete);
       
   310 				test(EFalse);
       
   311 				}
       
   312 			}
       
   313 		}
       
   314 
       
   315 	if (addBlockDataArray.Count() > 0)
       
   316 		{
       
   317 		for (TInt i = 0; i < addBlockDataArray.Count(); ++i)
       
   318 			{
       
   319 			TInt idx = addBlockDataArray[i];
       
   320 			path = aPath;
       
   321 			path += gDirPatterns[idx];
       
   322 			if (path[path.Length() - 1] == '\\')
       
   323 				{
       
   324 				MakeDir(path);
       
   325 				}
       
   326 			else
       
   327 				{
       
   328 				MakeFile(path, _L8("blahblah"));
       
   329 
       
   330 				}
       
   331 			}
       
   332 		}
       
   333 
       
   334 	addBlockDataArray.Reset();
       
   335 	deductBlockDataArray.Reset();
       
   336 	}
       
   337 
       
   338 /**
       
   339     Print out all items in aPath
       
   340     @param  aPath 		root path for scanning
       
   341 
       
   342     @panic				SetScanData error
       
   343 */
       
   344 void PrintDir(const TDesC& aPath, const TChar& aDrv)
       
   345 	{
       
   346 	TFileName fn;
       
   347 	if (aPath.Length() == 0)
       
   348 		{
       
   349 		return;
       
   350 		}
       
   351 	else
       
   352 		{
       
   353 		fn = aPath;
       
   354 		fn[0] = (TUint16)aDrv;
       
   355 		test.Printf(_L("==============================\n"));
       
   356 		test.Printf(_L("<<PrintDir>>: root = \"%S\"\n"), &fn);
       
   357 		}
       
   358 
       
   359 	CDirScan* scan = CDirScan::NewL(TheFs);
       
   360 	TUint entryAttMask = KEntryAttMaskSupported;
       
   361 	TUint entrySortMask = EDirsLast|EAscending;
       
   362 	CDirScan::TScanDirection scanDirection = CDirScan::EScanDownTree;
       
   363 
       
   364 
       
   365 	TRAPD(err, scan->SetScanDataL(fn, entryAttMask, entrySortMask, scanDirection));
       
   366 	test(err == KErrNone);
       
   367 
       
   368 	CDir* dir = NULL;
       
   369 	TInt i = 0;
       
   370 	TBool temp_val = ETrue;
       
   371 	while (temp_val)
       
   372 		{
       
   373 		scan->NextL(dir);
       
   374 		if (dir != NULL)
       
   375 			{
       
   376 			for (TInt j = 0; j < dir->Count(); ++j)
       
   377 				{
       
   378 				TFileName item(fn.Left(aPath.Length() - 1));
       
   379 				item.Append(scan->AbbreviatedPath());
       
   380 				TEntry dummy = (*dir)[j];
       
   381 				item.Append(dummy.iName);
       
   382 				if (dummy.iAtt & KEntryAttDir)
       
   383 					{
       
   384 					item.Append('\\');
       
   385 					}
       
   386 				test.Printf(_L("<<PrintDir>>: item %d: \"%S\"\n"), i, &item);
       
   387 				++i;
       
   388 				}
       
   389 			delete dir;
       
   390 			dir = NULL;
       
   391 			}
       
   392 		else
       
   393 			{
       
   394 			break;
       
   395 			}
       
   396 		}
       
   397 	delete scan;
       
   398 	}