os/kernelhwsrv/kerneltest/f32test/filesystem/fat/t_tscan32.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-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\scndrv\t_tscan32.cpp
    15 // 
    16 //
    17 
    18 #include <f32file.h>
    19 #include <e32test.h>
    20 
    21 #include "t_server.h"
    22 
    23 #include "fat_utils.h"
    24 using namespace Fat_Test_Utils;
    25 
    26 /*
    27 Series of tests to check that the combination of a ruggedised fat file
    28 system and scandisk prevents the file system from becoming corrupt in the
    29 event of a power failure. CheckDisk is used to test that directory
    30 structure is not corrupt. This test is only suitable with a drive that uses
    31 the fat file system but not the internal ram drive (due to the indirection)
    32 table. Only works with debug builds due to RFs::ControlIo only debug function
    33 */
    34 
    35 GLDEF_D TFileName StartupExeName=_L(""); // initialised at run time
    36 
    37 #ifdef _DEBUG
    38 GLREF_D RTest test;
    39 GLDEF_D TInt TheFunctionNumber;
    40 GLDEF_D TInt TheOpNumber;
    41 GLDEF_D TInt TheFailCount;
    42 GLDEF_D TBool IsReset;
    43 GLDEF_D TFileName TestExeName=_L("?:\\T_SCANDR.EXE"); //Renaming it to fit in one root dir entry.
    44 GLDEF_D TFileName LogFileName=_L("?:\\T_SCANDR.LOG"); //Renaming it to fit in one root dir entry.
    45 
    46 const TInt KControlIoWriteFailOn=0;		// commands to pass into RFs::ControlIo
    47 const TInt KControlIoWriteFailOff=1;
    48 const TInt KMaxFatEntries  = 2048;
    49 const TInt KDirAttrReadOnly  = 0x01;
    50 const TInt KDirAttrHidden    = 0x02;
    51 const TInt KDirAttrSystem    = 0x04;
    52 const TInt KDirAttrVolumeId  = 0x08;
    53 const TInt KDirAttrDirectory = 0x10;
    54 const TInt KDirAttrArchive   = 0x20;
    55 const TInt KDirAttrLongName  = KDirAttrReadOnly | KDirAttrHidden | KDirAttrSystem | KDirAttrVolumeId;
    56 const TInt KDirAttrLongMask  = KDirAttrLongName | KDirAttrDirectory | KDirAttrArchive;
    57 const TInt KDirLastLongEntry = 0x40;
    58 
    59 GLDEF_D TInt WriteFailValue;
    60 
    61 LOCAL_C TFatBootSector BootSector;	
    62 LOCAL_D RRawDisk TheRawDisk;
    63 
    64 static TFatType gDiskType = EInvalid;
    65 
    66 LOCAL_D TInt gTotalSectors;
    67 LOCAL_D TInt gBytesPerCluster;
    68 LOCAL_D TInt gRootDirSectors;
    69 LOCAL_D TInt gRootDirEntries;
    70 LOCAL_D TInt gRootDirStart;
    71 LOCAL_D TInt gRootSector;
    72 LOCAL_D TInt gFatStartBytes;
    73 LOCAL_D TInt gFatTestEntries;
    74 LOCAL_D TInt gFatSizeSectors;
    75 LOCAL_D TInt gFirstDataSector;
    76 LOCAL_D TInt gDataStartBytes;
    77 LOCAL_D TInt gClusterCount;
    78 
    79 LOCAL_D HBufC8* gFatBuf  = NULL;
    80 LOCAL_D TInt    gFatAddr = -1;
    81 
    82 enum TFatChain {EChainStd,EChainAlternate,EChainBackwards,EChainForwards};
    83 
    84 LOCAL_C TBool IsInternalRam()
    85 //
    86 // Returns true if the selected drive is variable size (i.e. RAM drive)
    87 //
    88 	{
    89 	TVolumeInfo v;
    90 	TInt r=TheFs.Volume(v,gSessionPath[0]-'A');
    91 	test(r==KErrNone);
    92 	return(v.iDrive.iMediaAtt&KMediaAttVariableSize);
    93 	}
    94 
    95 LOCAL_C void WriteLogFile()
    96 //
    97 // Writes state of test to end of LogFileName
    98 //
    99 	{
   100 	test.Printf(_L("Writelogfile()\n"));
   101 	RFile log;
   102 	TInt r=log.Open(TheFs,LogFileName,EFileShareExclusive|EFileWrite);
   103 	if(r!=KErrNone)
   104 		test.Printf(_L("error=%d\n"),r);
   105 	test(r==KErrNone);
   106 	TInt size;
   107 	r=log.Size(size);
   108 	test(r==KErrNone);
   109 	TBuf8<16> buf;
   110 	buf.SetLength(4);
   111 	buf[0]=(TUint8)TheFunctionNumber;
   112 	buf[1]=(TUint8)TheOpNumber;
   113 	buf[2]=(TUint8)TheFailCount;
   114 	buf[3]='\n';
   115 	r=log.Write(size,buf,buf.Length());
   116 	test(r==KErrNone);
   117 	test.Printf(_L("Written func=%d,op=%d,fail=%d\n"),TheFunctionNumber,TheOpNumber,TheFailCount);
   118 	log.Close();
   119 	}
   120 
   121 LOCAL_C TInt SetWriteFailOn(TInt aFailCount)
   122 //
   123 // Sets write to metadata to fail on aFailCount with WriteFailValue
   124 //
   125 	{
   126 	TInt16 args[2];
   127 	TPtr8 des((TUint8*)args,4,4);
   128 	args[0]=(TUint16)aFailCount;
   129 	args[1]=(TUint16)WriteFailValue;
   130 	TInt r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOn,des);
   131 	return(r);
   132 	}
   133 
   134 GLDEF_C void ReadLogFile()
   135 //
   136 // Reads state of test from end of LogFileName and sets global variables
   137 //
   138 	{
   139 	test.Next(_L("ReadLogFile"));
   140 	RFile log;
   141 	TInt r=log.Open(TheFs,LogFileName,EFileShareExclusive);
   142 	if(r!=KErrNone)
   143 		test.Printf(_L("error in ReadLogFile()=%d\n"),r);
   144 	test(r==KErrNone);
   145 	test(r==KErrNone);
   146 	TInt fileSize;
   147 	r=log.Size(fileSize);
   148 	if(fileSize==0)
   149 		{
   150 		TheFunctionNumber=0;
   151 		TheOpNumber=0;
   152 		TheFailCount=0;
   153 		}
   154 	else
   155 		{
   156 		TBuf8<4> buf;
   157 		r=log.Read(fileSize-4,buf,4);
   158 		TheFunctionNumber=buf[0];
   159 		TheOpNumber=buf[1];
   160 		TheFailCount=buf[2];
   161 		}
   162 	log.Close();
   163 	test.Printf(_L("func=%d,op=%d,fail=%d\n"),TheFunctionNumber,TheOpNumber,TheFailCount);
   164 	}
   165 
   166 LOCAL_C TUint32 MaxClusters()
   167 	//
   168 	// Return the number of data clusters on the disk
   169 	//
   170 	{
   171 	TUint32 totSec = (BootSector.TotalSectors() ? BootSector.TotalSectors() : BootSector.HugeSectors());
   172 	TUint32 numSec = totSec - gFirstDataSector;
   173 	return numSec / BootSector.SectorsPerCluster();
   174 	}
   175 
   176 LOCAL_C TInt ClusterToByte(TInt aCluster)
   177 	//
   178 	// converts cluster number to byte offset on disk
   179 	//
   180 	{
   181 	TInt sector = (aCluster - 2) * gBytesPerCluster + gFirstDataSector * BootSector.BytesPerSector();
   182 	return sector;
   183 	}
   184 
   185 /**
   186     Fill media with zeroes from aStartPos to aEndPos
   187 */
   188 static void DoZeroFillMedia(TInt64 aStartPos, TInt64 aEndPos, RRawDisk& aWriter)
   189 {
   190     test(aStartPos >=0 && aEndPos >=0 && aStartPos < aEndPos);
   191     
   192     if(aStartPos == aEndPos)
   193         return;
   194 
   195     RBuf8 buf;
   196     TInt  nRes;  
   197 
   198     const TUint32 KBufSz=65536*2; //-- buffer with zeroes
   199     
   200     nRes = buf.CreateMax(KBufSz);
   201     test(nRes == KErrNone);
   202 
   203     buf.FillZ();
   204 
   205     TUint32 rem = (TUint32)(aEndPos - aStartPos);
   206     while(rem)
   207     {
   208         const TUint32 bytesToWrite=Min(rem, KBufSz);
   209     
   210         TPtrC8 ptr(buf.Ptr(), bytesToWrite);
   211         nRes = aWriter.Write(aStartPos, ptr);
   212         test(nRes == KErrNone || nRes == KErrDiskFull);
   213 
   214         aStartPos+=bytesToWrite;
   215         rem-=bytesToWrite;
   216     }
   217 
   218 
   219     buf.Close();
   220 }
   221 
   222 //
   223 // Clear the disk data area to a known value which won't be confused with
   224 // directory entries etc.
   225 //
   226 LOCAL_C void ClearDiskData()
   227 	{
   228 
   229 	TInt r=TheRawDisk.Open(TheFs,gSessionPath[0]-'A');
   230 	test(r==KErrNone);
   231 
   232 	TUint32 startPos = gDataStartBytes;
   233 	if (gDiskType == EFat32)
   234 		startPos += gBytesPerCluster;
   235 
   236     const TUint32 endPos = startPos + gFatTestEntries*gBytesPerCluster;
   237 
   238     test.Printf(_L("ClearDiskData() from pos:%u to pos:%u\n"), startPos, endPos);
   239 
   240     DoZeroFillMedia(startPos, endPos, TheRawDisk);
   241 
   242 	TheRawDisk.Close();
   243 	}
   244 
   245 LOCAL_C TInt PosInBytes(TInt aFatIndex)
   246 //
   247 // Return number of bytes into the FAT
   248 //
   249 	{
   250 	TInt fatPosInBytes = -1;
   251 	switch (gDiskType)
   252 		{
   253 		case EFat32:
   254 			fatPosInBytes=aFatIndex<<2;
   255 			break;
   256 		case EFat16:
   257 			fatPosInBytes=aFatIndex<<1;
   258 			break;
   259 		case EFat12:
   260 			fatPosInBytes=(aFatIndex*3>>1);
   261 			break;
   262 		default:
   263 			test(0);
   264 		}
   265 	return(fatPosInBytes);
   266 	}
   267 
   268 static void DoReadBootSector()
   269 	{
   270 	
   271     TInt nRes = ReadBootSector(TheFs, CurrentDrive(), KBootSectorNum<<KDefaultSectorLog2, BootSector);
   272     test(nRes == KErrNone);
   273 
   274     if(!BootSector.IsValid())
   275         {
   276         test.Printf(_L("Wrong bootsector! Dump:\n"));
   277         BootSector.PrintDebugInfo();
   278         test(0);
   279         }
   280 	// Calculate derived variables (fixed for a particular disk format)
   281 	gBytesPerCluster   = BootSector.BytesPerSector() * BootSector.SectorsPerCluster();
   282 	gRootDirSectors    = ((BootSector.RootDirEntries() * KSizeOfFatDirEntry + BootSector.BytesPerSector() - 1) / BootSector.BytesPerSector());
   283 	gFatStartBytes     = BootSector.ReservedSectors() * BootSector.BytesPerSector();
   284 	gFatSizeSectors    = (BootSector.FatSectors() ? BootSector.FatSectors() : BootSector.FatSectors32());
   285 	gRootSector        = BootSector.ReservedSectors() + BootSector.NumberOfFats() * gFatSizeSectors;
   286 	gRootDirStart      = gRootSector * BootSector.BytesPerSector();
   287 	gFirstDataSector   = gRootSector + gRootDirSectors;
   288 
   289 	gFatTestEntries = MaxClusters();
   290 	if (gFatTestEntries > KMaxFatEntries)
   291 		gFatTestEntries = KMaxFatEntries;
   292 
   293 	if (BootSector.RootDirEntries() == 0)
   294 		{
   295 		test.Printf(_L("**** Is Fat32\n"));
   296 		gDiskType = EFat32;
   297 		gRootDirEntries = BootSector.RootDirEntries();
   298 		}
   299 	else if (BootSector.FatType() == EFat16)
   300 		{
   301 		test.Printf(_L("**** Is Fat16\n"));
   302 		gDiskType = EFat16;
   303 		gRootDirEntries = BootSector.RootDirEntries();
   304 		}
   305 	else
   306 		{
   307 		test.Printf(_L("**** Is Fat12\n"));
   308 		gDiskType = EFat12;
   309 		gRootDirEntries = gBytesPerCluster * 1 / KSizeOfFatDirEntry;
   310 		}
   311 	gTotalSectors   = (BootSector.TotalSectors() ? BootSector.TotalSectors() : BootSector.HugeSectors());
   312 	gClusterCount   = (gTotalSectors - gFirstDataSector) / BootSector.SectorsPerCluster();
   313 	gDataStartBytes = gFirstDataSector * BootSector.BytesPerSector();
   314 	}
   315 
   316 GLDEF_C TUint32 GetFatEntry(TUint32 aIndex, const TUint8* aFat=NULL)
   317 //
   318 // Read a single FAT entry from disk or FAT copy and return it
   319 //
   320 	{
   321 
   322 
   323 	if (!gFatBuf)
   324 		{
   325 		gFatBuf=HBufC8::New(gBytesPerCluster);
   326 		test(gFatBuf!=NULL);
   327 		gFatAddr = -1;
   328 		}
   329 
   330 	const TUint8* ptr;
   331 
   332 	if (aFat)
   333 		{
   334 		ptr = (TUint8*)aFat + PosInBytes(aIndex);
   335 		}
   336 	else
   337 		{
   338 		TInt pos = PosInBytes(aIndex) + gFatStartBytes;
   339 		if (gFatAddr < 0 || pos < gFatAddr || pos >= gFatAddr + gBytesPerCluster)
   340 			{
   341 			TPtr8 ptr=gFatBuf->Des();
   342 		TInt r=TheRawDisk.Open(TheFs,gSessionPath[0]-'A');
   343 			test(r==KErrNone);
   344 			r=TheRawDisk.Read(pos, ptr);
   345 			test(r==KErrNone);
   346 			TheRawDisk.Close();
   347 			gFatAddr = pos;
   348 			}
   349 		ptr = gFatBuf->Ptr() + pos-gFatAddr;
   350 		}
   351 
   352 	TUint32 val = 0;
   353 	switch (gDiskType)
   354 		{
   355 		case EFat32:
   356 			val = *(TUint32*)ptr;
   357 			break;
   358 		case EFat16:
   359 			val = *(TUint16*)ptr;
   360 			break;
   361 		case EFat12:
   362 			val = *(TUint16*)ptr;
   363 			if (aIndex & 1)
   364 				val >>= 4;
   365 			val &= 0xFFF;
   366 			break;
   367 		default:
   368 			test(0);
   369 		}
   370 	return val;
   371 	}
   372 
   373 GLDEF_C void DumpBootSector()
   374 //
   375 // Display (in log) TFatBootSector structure
   376 //
   377 	{
   378 	RDebug::Print(_L("iBytesPerSector    = %8d"), BootSector.BytesPerSector());
   379 	RDebug::Print(_L("iSectorsPerCluster = %8d"), BootSector.SectorsPerCluster());
   380 	RDebug::Print(_L("iReservedSectors   = %8d"), BootSector.ReservedSectors());
   381 	RDebug::Print(_L("iNumberOfFats      = %8d"), BootSector.NumberOfFats());
   382 	RDebug::Print(_L("iRootDirEntries    = %8d"), BootSector.RootDirEntries());
   383 	RDebug::Print(_L("iTotalSectors      = %8d"), BootSector.TotalSectors());
   384 	RDebug::Print(_L("iMediaDescriptor   = %8d"), BootSector.MediaDescriptor());
   385 	RDebug::Print(_L("iFatSectors        = %8d"), BootSector.FatSectors());
   386 	RDebug::Print(_L("iSectorsPerTrack   = %8d"), BootSector.SectorsPerTrack());
   387 	RDebug::Print(_L("iNumberOfHeads     = %8d"), BootSector.NumberOfHeads());
   388 	RDebug::Print(_L("iHiddenSectors     = %8d"), BootSector.HiddenSectors());
   389 	RDebug::Print(_L("iHugeSectors       = %8d"), BootSector.HugeSectors());
   390 
   391 	//New for FAT32
   392 
   393 	if(BootSector.RootDirEntries() == 0)	//indicates we have FAT32 volume
   394 		{
   395 		RDebug::Print(_L("FatSectors32      = %8d"), BootSector.FatSectors32());
   396 		RDebug::Print(_L("FATFlags          = %8d"), BootSector.FATFlags());
   397 		RDebug::Print(_L("VersionNumber     = %8d"), BootSector.VersionNumber());
   398 		RDebug::Print(_L("RootClusterNum    = %8d (0x%08X)"), BootSector.RootClusterNum(), gRootDirStart);
   399 		RDebug::Print(_L("FSInfoSectorNum   = %8d (0x%08X)"), BootSector.FSInfoSectorNum(), BootSector.FSInfoSectorNum() * BootSector.BytesPerSector());
   400 		RDebug::Print(_L("BkBootRecSector   = %8d (0x%08X)"), BootSector.BkBootRecSector(), BootSector.BkBootRecSector() * BootSector.BytesPerSector());
   401 		}
   402 	TInt fatEntries = gFatSizeSectors*BootSector.BytesPerSector();
   403 	switch (gDiskType)
   404 		{
   405 		case EFat32:
   406 			fatEntries /= 4;
   407 			break;
   408 		case EFat16:
   409 			fatEntries /= 2;
   410 			break;
   411 		case EFat12:
   412 			fatEntries *= 3;
   413 			fatEntries /= 2;
   414 			break;
   415 		default:
   416 			test(0);
   417 		}
   418 	RDebug::Print(_L("ClusterCount      = %8d (%d bytes)"), gClusterCount, gClusterCount*gBytesPerCluster);
   419 	RDebug::Print(_L("FatEntries        = %8d (%d sectors)"), fatEntries, gFatSizeSectors);
   420 	RDebug::Print(_L("RootSector        = %8d (0x%08X)"), gRootSector, gRootDirStart);
   421 	RDebug::Print(_L("FirstDataSector   = %8d (0x%08X)"), gFirstDataSector, gDataStartBytes);
   422 	}
   423 
   424 GLDEF_C void DumpFat(const TUint8* aFat=NULL)
   425 // 
   426 // Dump to the log all those FAT entries which are non-zero
   427 //
   428 	{
   429 	TInt32 max = MaxClusters();
   430 	if (max > KMaxFatEntries)
   431 		max = KMaxFatEntries;
   432 	RDebug::Print(_L("---------------- DUMP OF FAT ---------------"));
   433 	for (TInt32 i = 0; i < max; i++)
   434 		{
   435 		TInt32 val = GetFatEntry(i, aFat);
   436 		TInt32 msk = 0x0FFFFFFF;
   437 		switch (gDiskType)
   438 			{
   439 			case EFat32:
   440 				msk = 0x0FFFFFFF;
   441 				break;
   442 			case EFat16:
   443 				msk = 0xFFFF;
   444 				break;
   445 			case EFat12:
   446 				msk = 0x0FFF;
   447 				break;
   448 			default:
   449 				test(0);
   450 			}
   451 		if ((val & msk) == (0x0FFFFFFF & msk))
   452 			RDebug::Print(_L("    %8d -> EOC"), i);
   453 		else if ((val & msk) == (0x0FFFFFF8 & msk))
   454 			RDebug::Print(_L("    %8d -> Media"), i);
   455 		else if ((val & msk) == (0x0FFFFFF7 & msk))
   456 			RDebug::Print(_L("    %8d -> BAD"), i);
   457 		else if (val > max)
   458 			RDebug::Print(_L("    %8d -> 0x%08X"), i, val);
   459 		else if (val != 0)
   460 			RDebug::Print(_L("    %8d -> %d"), i, val);
   461 		}
   462 	RDebug::Print(_L("--------------------------------------------"));
   463 	}
   464 
   465 GLDEF_C TDes* DirAttributes(TInt aAttrib)
   466 // 
   467 // Return a pointer to a local buffer containing the attribute letters.
   468 //
   469 	{
   470 	LOCAL_D TBuf<6> str(_L("------"));
   471 	LOCAL_D char*   atr = "RHSVDA";
   472 	for (TInt i = 0; i < 6; i++)
   473 		if ((aAttrib >> i) & 1)
   474 			str[i] = atr[i];
   475 	return &str;
   476 	}
   477 
   478 GLDEF_C TBool IsValidDirChar(TUint8 aChar, TUint8 aMin=0x20)
   479 //
   480 // Test whether a character is valid as part of a short filename, aMin is to
   481 // distinguish between first character (which can't be space) and later ones
   482 // which can include space but nothing less.  Note that E5 is a valid character
   483 // in any position, even though it means 'erased' in the first character.
   484 //
   485 	{
   486 	const TUint8* inval = (TUint8*)"\x22\x2A\x2B\x2C\x2E\x2F\x3A\x3B\x3C\x3D\x3E\x3F\x5B\x5C\x5D\x7C";
   487 	if (aChar < aMin)
   488 		return EFalse;
   489 	for (const TUint8* p = inval; *p; p++)
   490 		if (aChar == *p)
   491 			return EFalse;
   492 	return ETrue;
   493 	}
   494 
   495 GLDEF_C TBool IsValidDirEntry(TFatDirEntry* aDir)
   496 // 
   497 // Test whether buffer is a valid normal directory entry
   498 //
   499 	{
   500 	// top two bits of attributes must be zero
   501 	if (aDir->iData[11] & 0xC0)
   502 		return EFalse;
   503 	// first character must be 0x05 or greater than space
   504 	if (!IsValidDirChar(aDir->iData[0], 0x21) && aDir->iData[0] != 0x05)
   505 		return EFalse;
   506 	// other characters in name must be not less than space
   507 	for (TInt i = 1; i < 11; i++)
   508 		if (!IsValidDirChar(aDir->iData[i]))
   509 			return EFalse;
   510 	return ETrue;
   511 	}
   512 
   513 GLDEF_C void GetLongNamePart(TDes16& aName, const TUint8* aEntry, TInt aPos, TInt aOffset, TInt aLength)
   514 // 
   515 // Extract part of a long name entry into the name buffer.
   516 //
   517 // @param aName   buffer to put name
   518 // @param aEntry  directory entry raw data
   519 // @param aPos    character in buffer to start name segment
   520 // @param aOffset offset in directory entry of the segment
   521 // @param aLength number of characters in the segment
   522 // 
   523 	{
   524 	for (TInt i = 0; i < aLength; i++)
   525 		{
   526 		TInt at = i * 2 + aOffset;
   527 		TInt ch = aEntry[at] + aEntry[at+1] * 256;
   528 		aName[aPos++] = TText(ch);
   529 		}
   530 	}
   531 
   532 GLDEF_C void ExtractNameString(TDes16& aName, const TUint8* aEntry)
   533 //
   534 // Extract a long name part from a directory entry, truncate it at the first
   535 // NUL (0) character and put quotes round it.
   536 //
   537 	{
   538 	aName.SetLength(15);
   539 	TInt len = aName.Length() - 1;
   540 	TText qu = '\'';
   541 	aName[0] = qu;
   542 	GetLongNamePart(aName, aEntry,  1,  1, 5);
   543 	GetLongNamePart(aName, aEntry,  6, 14, 6);
   544 	GetLongNamePart(aName, aEntry, 12, 28, 2);
   545 	TInt i;
   546 	for (i = 0; i < len; i++)
   547 		if (aName[i] == 0)
   548 			break;
   549 	aName[i++] = qu;
   550 	aName.SetLength(i);
   551 	}
   552 
   553 GLDEF_C TBool DumpDirEntry(TInt aNum, const TUint8* aEntry)
   554 // 
   555 // Dump a single directory entry to the log.  Return false if it was end of
   556 // directory or an invalid entry (and don't display it).
   557 // 
   558 	{
   559 	TFatDirEntry* d = (TFatDirEntry*)aEntry;
   560 	if (d->IsErased())
   561 		{
   562 		// RDebug::Print(_L("%5d: ERASED"), aNum);
   563 		}
   564 	else if (d->IsEndOfDirectory())
   565 		return EFalse;
   566 	else if ((d->Attributes() & KDirAttrLongMask) == KDirAttrLongName)
   567 		{
   568 		TBuf16<15> name;
   569 		ExtractNameString(name, aEntry);
   570 		TInt ord = aEntry[0];
   571 		if (ord & KDirLastLongEntry)
   572 			RDebug::Print(_L("%5d: %-15S #%-2d LAST"), aNum, &name, ord & ~KDirLastLongEntry);
   573 		else
   574 			RDebug::Print(_L("%5d: %-15S #%-2d"), aNum, &name, ord & ~KDirLastLongEntry);
   575 		}
   576 	else if (!IsValidDirEntry(d))
   577 		return EFalse;
   578 	else
   579 		{
   580 		TBuf<11> name;
   581 		name.Copy(d->Name());
   582 		RDebug::Print(_L("%5d: '%S'  %S  cluster %d"),
   583 					  aNum, &name, DirAttributes(d->Attributes()), d->StartCluster());
   584 		}
   585 	return ETrue;
   586 	}
   587 
   588 GLDEF_C void DumpDirCluster(const TUint8* aData, TInt aCluster=0)
   589 //
   590 // Dump directory entries until end of cluster or invalid/end entry found.
   591 //
   592 	{
   593 	if (aCluster > 2)
   594 		aData += (aCluster-2) * gBytesPerCluster;
   595 	for (TInt i = 0; i < gBytesPerCluster; i += KSizeOfFatDirEntry)
   596 		{
   597 		if (DumpDirEntry(i/KSizeOfFatDirEntry, aData))
   598 			aData += KSizeOfFatDirEntry;
   599 		else
   600 			break;
   601 		}
   602 	}
   603 
   604 GLDEF_C void DumpData(const TUint8* aFat, TInt aStart, TInt aEnd)
   605 //
   606 // Dump clusters from disk (allows dumping of clusters not in our buffers).
   607 // Only look at clusters marked as 'used' in the FAT.  Note that if aFat is
   608 // NULL the FAT entries will also be read from disk (slower but allows for ones
   609 // outside our copy in memory).
   610 //
   611 	{
   612 	if (aStart > gFatTestEntries)
   613 		return;
   614 	RDebug::Print(_L("--------------- DATA AREA ------------------"));
   615 	if (aEnd > gFatTestEntries)
   616 		aEnd = gFatTestEntries;
   617 	for (TInt cluster = aStart; cluster < aEnd; cluster++)
   618 		{
   619 		if (GetFatEntry(cluster, aFat) != 0)
   620 			{
   621 			HBufC8* buf=HBufC8::New(gBytesPerCluster);
   622 			test(buf!=NULL);
   623 			TPtr8 ptr=buf->Des();
   624 			TInt r=TheRawDisk.Open(TheFs,gSessionPath[0]-'A');
   625 			test(r==KErrNone);
   626 			r=TheRawDisk.Read(ClusterToByte(cluster), ptr);
   627 			test(r==KErrNone);
   628 			TheRawDisk.Close();
   629 			RDebug::Print(_L("Cluster %d @ 0x%08X:"), cluster, ClusterToByte(cluster));
   630 			DumpDirCluster(ptr.Ptr());
   631 			delete buf;
   632 			}
   633 		}
   634 	RDebug::Print(_L("--------------------------------------------"));
   635 	}
   636 
   637 GLDEF_C void DumpHex(const TUint8* aData, TInt aLen)
   638 //
   639 // Dump a block of memory to the log in hex.
   640 //
   641 	{
   642 	for (TInt base = 0; base < aLen; base += 16)
   643 		{
   644 		TBuf<16*3> buf;
   645 		TInt off;
   646 		for (off = base; off < aLen && off < base + 16; off++)
   647 			{
   648 			buf.Append(TText(' '));
   649 			buf.AppendNumFixedWidth(aData[off], EHex, 2);
   650 			}
   651 		RDebug::Print(_L("%04X: %S"), off, &buf);
   652 		}
   653 	}
   654 
   655 static void QuickFormat()
   656     {
   657         /*
   658         TFatFormatParam fmt;
   659         fmt.iFatType = EFat32;
   660         fmt.iSecPerCluster =1;
   661         FormatFatDrive(TheFs, CurrentDrive(), ETrue, &fmt);
   662         */
   663         FormatFatDrive(TheFs, CurrentDrive(), ETrue);
   664     }
   665 
   666 
   667 LOCAL_C void MakeVeryLongName(TFileName& aLong)
   668 //
   669 // appends a very long file name to aLong
   670 //
   671 	{
   672 	// create a name to take up 18 vfat entries - (1 sector + 2 entries)
   673 	for(TInt i=0;i<234;++i)
   674 		{
   675 		TInt c='a'+i%26;
   676 		aLong.Append(c);
   677 		}
   678 	}
   679 
   680 LOCAL_C void MakeEntryName(TFileName& aName,TInt aLength)
   681 //
   682 // Appends aLength characters to aName
   683 //
   684 	{
   685 	for(TInt i=0;i<aLength;++i)
   686 		{
   687 		TInt c='A'+i%26;
   688 		aName.Append(c);
   689 		}
   690 	}
   691 
   692 LOCAL_C void FillUpRootDir(TInt aFree=0)
   693 //
   694 // Fill up root directory
   695 //
   696 	{
   697 	TInt maxRootEntries = gRootDirEntries -aFree;
   698 	TFileName dir=_L("\\??\\");
   699 	TInt count=0;
   700 	TInt entriesSoFar;
   701 	if(IsReset)
   702 		entriesSoFar=2+2+2+2; // \\t_scn32dr3.exe + \\sys + \\t_scn32dr3.log + \\f32-tst
   703 	else
   704 		entriesSoFar=0;
   705 	TInt r;
   706 	while(entriesSoFar<maxRootEntries)
   707 		{
   708 		dir[1]=TUint16(count/26+'a');
   709 		dir[2]=TUint16(count%26+'a');
   710 		r=TheFs.MkDir(dir);
   711 		test(r==KErrNone);
   712 		entriesSoFar+=2;
   713 		++count;
   714 		}
   715 	}
   716 
   717 LOCAL_C void UnFillUpRootDir(TInt aFree=0)
   718 //
   719 // Reverse changes from FillUpRootDir()
   720 //
   721 	{
   722 	TFileName dir=_L("\\??\\");
   723 	TInt entriesSoFar=gRootDirEntries -aFree;
   724 	TInt count=0;
   725 	TInt r;
   726 	TInt existing;
   727 	if(IsReset)
   728 		existing=2+2+2+2; // \\t_scn32dr3.exe + \\sys + \\t_scn32dr3.log + \\f32-tst
   729 	else
   730 		existing=0;
   731 	while(entriesSoFar>existing)
   732 		{
   733 		dir[1]=TUint16(count/26+'a');
   734 		dir[2]=TUint16(count%26+'a');
   735 		r=TheFs.RmDir(dir);
   736 		test(r==KErrNone);
   737 		entriesSoFar-=2;
   738 		++count;
   739 		}
   740 	}
   741 
   742 void InitialiseWriteBuffer(TDes8& buf)
   743 //
   744 //
   745 //
   746 	{
   747 	for(TInt i=0;i<buf.Length();++i)
   748 		buf[i]=(TUint8)('a'+i%26);
   749 	}
   750 
   751 LOCAL_C TBool EntryExists(const TDesC& aName)	
   752 //
   753 // Returns ETrue if aName is found
   754 //
   755 	{
   756 	TEntry entry;
   757 	TInt r=TheFs.Entry(aName,entry);
   758 	test(r==KErrNone||r==KErrNotFound);
   759 	return(r==KErrNone?(TBool)ETrue:(TBool)EFalse);
   760 	}
   761 
   762 LOCAL_C TInt EntriesPerFatSector()
   763 //
   764 // Returns number of entries in one fat table sector
   765 //
   766 	{
   767 	switch (gDiskType)
   768 		{
   769 		case EFat32:
   770 			return(BootSector.BytesPerSector()/4);
   771 		case EFat16:
   772 			return(BootSector.BytesPerSector()/2);
   773 		case EFat12:
   774 			return(BootSector.BytesPerSector()*2/3);
   775 		default:
   776 			test(0);
   777 		}
   778 	return -1;
   779 	}
   780 
   781 LOCAL_C TBool OneEntryExists(const TDesC& aOldName,const TDesC& aNewName)
   782 //
   783 // Returns ETrue if only one of two entries exists
   784 //
   785 	{
   786 	TBool oldExists=EntryExists(aOldName);
   787 	TBool newExists=EntryExists(aNewName);
   788 	return((!oldExists&&newExists)||(oldExists&&!newExists));
   789 	}
   790 
   791 LOCAL_C void GetEntryDetails(const TDesC& aName,TEntry& aEntry)
   792 //
   793 // returns entry details for the entry with aName
   794 //
   795 	{
   796 	TInt r=TheFs.Entry(aName,aEntry);
   797 	test(r==KErrNone);
   798 	}
   799 
   800 LOCAL_C TBool IsSameEntryDetails(TEntry aOldEntry,TEntry aNewEntry)
   801 //
   802 // 
   803 //
   804 	{
   805 	return(aOldEntry.iAtt==aNewEntry.iAtt&&aOldEntry.iSize==aNewEntry.iSize&&aOldEntry.iModified==aNewEntry.iModified);
   806 	}
   807 
   808 LOCAL_C void CreateAlternate(const TDesC& aNameOne,const TDesC& aNameTwo)
   809 //
   810 // Creates altenate entries which take up one sector of fat table.
   811 // By subsequently deleting  one of these entries a new entry can be made 
   812 // with cluster chain that is not contiguous.
   813 //
   814 	{
   815 	TInt entries=EntriesPerFatSector();
   816 	RFile file1,file2;
   817 	TInt size1,size2;
   818 	size1=size2=0;
   819 	TInt r=file1.Create(TheFs,aNameOne,EFileShareAny);
   820 	test(r==KErrNone);
   821 	r=file2.Create(TheFs,aNameTwo,EFileShareAny);
   822 	test(r==KErrNone);
   823 	// one entry for file1 for every 40 entries for file2
   824 	// if file 1 subseqently deleted then 7 entries available
   825 	// in that fat sector - ~3.5kb file size - for fat16
   826 	TInt ratio=40;
   827 	TBool first=ETrue;
   828 	while(entries>0)
   829 		{
   830 		if(first)
   831 			{
   832 			size1+=gBytesPerCluster;
   833 			r=file1.SetSize(size1);
   834 			test(r==KErrNone);
   835 			first=EFalse;
   836 			--entries;
   837 			}
   838 		else
   839 			{
   840 			size2+=gBytesPerCluster*ratio;
   841 			r=file1.SetSize(size1);
   842 			test(r==KErrNone);
   843 			first=ETrue;
   844 			entries-=ratio;
   845 			}
   846 		}
   847 	file1.Close();
   848 	file2.Close();
   849 	}
   850 
   851 LOCAL_C TInt ThrottleDirEntries(TInt aDirEntries)
   852 	{
   853 	// throttle the number of entries needed, since for large cluster
   854 	// sizes, this can take forever (eg 2GB card -> a cluster size of 32K
   855 	// -> 1024 entries per cluster
   856 	const TInt KMaxDirEntries = 2048;
   857 	if (aDirEntries > KMaxDirEntries)
   858 		{
   859 		RDebug::Print(_L("Reducing directory entries from %d to %d"), 
   860 			aDirEntries, KMaxDirEntries);
   861 		aDirEntries = KMaxDirEntries;
   862 		}
   863 	return aDirEntries;
   864 	}
   865 
   866 
   867 LOCAL_C void CleanDirectory(const TDesC& aName,TInt aClusters)
   868 //
   869 // Removes entries in the directory
   870 //
   871 	{
   872 	if (aClusters < 1)
   873 		return;
   874 	TInt entriesPerCluster=gBytesPerCluster/32;
   875 	TInt entriesNeeded = entriesPerCluster * aClusters;
   876 
   877 	entriesNeeded = ThrottleDirEntries(entriesNeeded);
   878 
   879 	TInt maxFileNameLen = 250 - aName.Length();
   880 	TInt nameEntries = 1 + (maxFileNameLen+12) / 13;
   881 	TInt namesNeeded = (entriesNeeded + nameEntries-1) / nameEntries;
   882 	TInt entry = 0;
   883 	for(TInt i = 0; i < namesNeeded; ++i)
   884 		{
   885 		if (entriesNeeded - entry < nameEntries)
   886 			maxFileNameLen = (entriesNeeded - entry - 1) * 13;
   887 		TFileName fn;
   888 		fn.AppendNum(entry);
   889 		fn.Append('_');
   890 		while (fn.Length() < maxFileNameLen)
   891 			fn.Append('0');
   892 		TFileName fullName(aName);
   893 		fullName.Append(fn);
   894 		TInt r = TheFs.Delete(fullName);
   895 		test(r == KErrNone);
   896 		entry += 1 + (fn.Length() + 12) / 13;
   897 		}
   898 	RDebug::Print(_L("CleanDirectory(%S, %d)"), &aName, aClusters);
   899 	}
   900 
   901 LOCAL_C void ExpandDirectory(const TDesC& aName,TInt aClusters)
   902 //
   903 // Expands the directory by aClusters
   904 //
   905 	{
   906 	if (aClusters < 1)
   907 		return;
   908 	TInt entriesPerCluster=gBytesPerCluster/32;
   909 	TInt entriesNeeded = entriesPerCluster * aClusters;
   910 
   911 	entriesNeeded = ThrottleDirEntries(entriesNeeded);
   912 
   913 	TInt maxFileNameLen = 250 - aName.Length();
   914 	TInt nameEntries = 1 + (maxFileNameLen+12) / 13;
   915 	TInt namesNeeded = (entriesNeeded + nameEntries-1) / nameEntries;
   916 	TInt entry = 0;
   917 	for(TInt i = 0; i < namesNeeded; ++i)
   918 		{
   919 		if (entriesNeeded - entry < nameEntries)
   920 			maxFileNameLen = (entriesNeeded - entry - 1) * 13;
   921 		TFileName fn;
   922 		fn.AppendNum(entry);
   923 		fn.Append('_');
   924 		while (fn.Length() < maxFileNameLen)
   925 			fn.Append('0');
   926 		TFileName fullName(aName);
   927 		fullName.Append(fn);
   928 		RFile file;
   929 		TInt r = file.Create(TheFs,fullName,EFileShareAny);
   930 		test(r == KErrNone);
   931 		file.Close();
   932 		entry += 1 + (fn.Length() + 12) / 13;
   933 		}
   934 	// to leave a directory expanded by aClusters but with no additional entries
   935 	RDebug::Print(_L("ExpandDirectory(%S, %d)"), &aName, aClusters);
   936 	CleanDirectory(aName,aClusters);
   937 	}
   938 
   939 LOCAL_C TInt DeleteAlternateEntry(const TDesC& aName,TBool aIsDir)
   940 //
   941 // Deletes entry aName and corresponding entries created for EChainAlternate
   942 //
   943 	{
   944 	TInt r=TheFs.Delete(_L("\\fat\\file2"));
   945 	test(r==KErrNone||KErrNotFound);
   946 	if(aIsDir)
   947 		return(TheFs.RmDir(aName));
   948 	else
   949 		return(TheFs.Delete(aName));
   950 	}
   951 
   952 LOCAL_C TInt CreateAlternateEntry(const TDesC& aName,TBool aIsDir,TInt aSize)
   953 //
   954 // Creates entry with aName where cluster chain grows forward but not contiguously.
   955 // Assumes that no holes in fat clusters.
   956 //
   957 	{
   958 	TInt r=DeleteAlternateEntry(aName,aIsDir);
   959 	test(r==KErrNone||r==KErrNotFound);
   960 	RFile file;
   961 	if(aIsDir)
   962 		{
   963 		r=TheFs.MkDir(aName);
   964 		if(r!=KErrNone)
   965 			return(r);
   966 		}
   967 	else
   968 		{
   969 		r=file.Create(TheFs,aName,EFileShareAny);
   970 		if(r!=KErrNone)
   971 			return(r);
   972 		r=file.SetSize(1); //ensure file allocated a start cluster
   973 		test(r==KErrNone);
   974 		}
   975 	CreateAlternate(_L("\\fat\\file1"),_L("\\fat\\file2"));
   976 	r=TheFs.Delete(_L("\\fat\\file1"));
   977 	test(r==KErrNone);
   978 	if(aIsDir)
   979 		ExpandDirectory(aName,aSize);
   980 	else
   981 		{
   982 		r=file.SetSize(aSize);
   983 		test(r==KErrNone);
   984 		file.Close();
   985 		}
   986 	return(KErrNone);
   987 	}
   988 
   989 LOCAL_C TInt DeleteForwardEntry(const TDesC& aName,TBool aIsDir)
   990 //
   991 // Deletes entry with aName and corresponding entries created for EChainForward
   992 //
   993 	{
   994 	TInt r=TheFs.Delete(_L("\\fat\\file2"));
   995 	test(r==KErrNone||r==KErrNotFound);
   996 	r=TheFs.Delete(_L("\\fat\\file4"));
   997 	test(r==KErrNone||r==KErrNotFound);
   998 	r=TheFs.Delete(_L("\\fat\\file5"));
   999 	test(r==KErrNone||r==KErrNotFound);
  1000 	if(aIsDir)
  1001 		r=TheFs.RmDir(aName);
  1002 	else
  1003 		r=TheFs.Delete(aName);
  1004 	return r;
  1005 	}
  1006 	
  1007 LOCAL_C TInt CreateForwardEntry(const TDesC& aName,TBool aIsDir,TInt aSize)
  1008 //
  1009 // Creates an entry whose cluster chain first goes forward (upto 3.5kb for fat16 file)
  1010 // and then backwards
  1011 //	
  1012 	{
  1013 	TInt r=DeleteForwardEntry(aName,aIsDir);
  1014 	test(r==KErrNone||r==KErrNotFound);
  1015 	RFile file1,file2,entry;
  1016 	r=file1.Create(TheFs,_L("\\fat\\file1"),EFileShareAny);
  1017 	test(r==KErrNone);
  1018 	r=file1.SetSize(EntriesPerFatSector()*gBytesPerCluster);
  1019 	test(r==KErrNone);
  1020 	r=file2.Create(TheFs,_L("\\fat\\file2"),EFileShareAny);
  1021 	test(r==KErrNone);
  1022 	r=file2.SetSize(EntriesPerFatSector()*gBytesPerCluster);
  1023 	test(r==KErrNone);
  1024 	if(aIsDir)
  1025 		{
  1026 		r=TheFs.MkDir(aName);
  1027 		if(r!=KErrNone)
  1028 			return(r);
  1029 		}
  1030 	else
  1031 		{
  1032 		r=entry.Create(TheFs,aName,EFileShareAny);
  1033 		if(r!=KErrNone)
  1034 			return(r);
  1035 		r=entry.SetSize(1);	// ensure entry has start cluster allocated
  1036 		test(r==KErrNone);
  1037 		}
  1038 	CreateAlternate(_L("\\fat\\file3"),_L("\\fat\\file4"));
  1039 	RFile file5;
  1040 	r=file5.Create(TheFs,_L("\\fat\\file5"),EFileShareAny);
  1041 	test(r==KErrNone);
  1042 	r=file5.SetSize(EntriesPerFatSector()*gBytesPerCluster*2);
  1043 	test(r==KErrNone);
  1044 	file1.Close();
  1045 	file2.Close();
  1046 	file5.Close();
  1047 	r=TheFs.Delete(_L("\\fat\\file1"));
  1048 	test(r==KErrNone);
  1049 	r=TheFs.Delete(_L("\\fat\\file3"));
  1050 	test(r==KErrNone);
  1051 	if(aIsDir)
  1052 		ExpandDirectory(aName,aSize);
  1053 	else
  1054 		{
  1055 		r=entry.SetSize(aSize);
  1056 		test(r==KErrNone);
  1057 		entry.Close();
  1058 		}
  1059 	return(KErrNone);
  1060 	}
  1061 
  1062 LOCAL_C TInt DeleteBackwardEntry(const TDesC& aName,TBool aIsDir)
  1063 //
  1064 // Deletes entry with aName and corresponding entries created for EChainBackwards
  1065 //
  1066 	{
  1067 	TInt r=TheFs.Delete(_L("\\fat\\file2"));
  1068 	test(r==KErrNone||r==KErrNotFound);
  1069 	r=TheFs.Delete(_L("\\fat\\file3"));
  1070 	test(r==KErrNone||r==KErrNotFound);
  1071 	if(aIsDir)
  1072 		r=TheFs.RmDir(aName);
  1073 	else
  1074 		r=TheFs.Delete(aName);
  1075 	return r;
  1076 	}
  1077 
  1078 LOCAL_C TInt CreateBackwardEntry(const TDesC& aName,TBool aIsDir,TInt aSize)
  1079 //
  1080 // Creates an entry whose fat cluster chain first goes backwards(upto 3.5kb for fat16 file)
  1081 // and then forwards
  1082 //
  1083 	{
  1084 	TInt r=DeleteBackwardEntry(aName,aIsDir);
  1085 	test(r==KErrNone||r==KErrNotFound);
  1086 	CreateAlternate(_L("\\fat\\file1"),_L("\\fat\\file2"));
  1087 	RFile entry;
  1088 	if(aIsDir)
  1089 		{
  1090 		r=TheFs.MkDir(aName);
  1091 		if(r!=KErrNone)
  1092 			return(r);
  1093 		}
  1094 	else
  1095 		{
  1096 		r=entry.Create(TheFs,aName,EFileShareAny);
  1097 		if(r!=KErrNone)
  1098 			return(r);
  1099 		r=entry.SetSize(1);
  1100 		test(r==KErrNone);
  1101 		}
  1102 	RFile file3;
  1103 	r=file3.Create(TheFs,_L("\\fat\\file3"),EFileShareAny);
  1104 	test(r==KErrNone);
  1105 	r=file3.SetSize(EntriesPerFatSector()*gBytesPerCluster);
  1106 	test(r==KErrNone);
  1107 	r=TheFs.Delete(_L("\\fat\\file1"));
  1108 	test(r==KErrNone);
  1109 	file3.Close();
  1110 	if(aIsDir)
  1111 		ExpandDirectory(aName,aSize);
  1112 	else
  1113 		{
  1114 		r=entry.SetSize(aSize);
  1115 		test(r==KErrNone);
  1116 		entry.Close();
  1117 		}
  1118 	return(KErrNone);	
  1119 	}
  1120 
  1121 LOCAL_C TInt DeleteStdEntry(const TDesC& aName,TBool aIsDir)
  1122 //
  1123 // Deletes entry with aName
  1124 //
  1125 	{
  1126 	if(aIsDir)
  1127 		return(TheFs.RmDir(aName));
  1128 	else
  1129 		return(TheFs.Delete(aName));
  1130 	}
  1131 
  1132 LOCAL_C TInt CreateStdEntry(const TDesC& aName,TBool aIsDir,TInt aSize)
  1133 //
  1134 // Creates entry with aName where the cluster chain grows contiguously
  1135 //
  1136 	{
  1137 	TInt r=DeleteStdEntry(aName,aIsDir);
  1138 	test(r==KErrNone||r==KErrNotFound);
  1139 	if(aIsDir)
  1140 		{
  1141 		r=TheFs.MkDir(aName);
  1142 		if(r==KErrNone)
  1143 			ExpandDirectory(aName,aSize);
  1144 		return(r);
  1145 		}
  1146 	else
  1147 		{
  1148 		RFile file;
  1149 		r=file.Create(TheFs,aName,EFileShareAny);
  1150 		if(r==KErrNone)
  1151 			{
  1152 			r=file.SetSize(aSize);
  1153 			test(r==KErrNone);
  1154 			}
  1155 		else if(r==KErrAlreadyExists)
  1156 			{
  1157 			TInt res =file.Open(TheFs,aName,EFileShareAny);
  1158 			test(res==KErrNone);
  1159 			}
  1160 		else
  1161 			return(r);
  1162 		file.Close();
  1163 		return(r);
  1164 		}
  1165 	}
  1166 
  1167 LOCAL_C TInt CreateEntry(const TDesC& aName,TBool aIsDir,TFatChain aChain,TInt aSize)
  1168 //
  1169 // Creates entry with aName whose fat cluster chain characteristics determined by aChain
  1170 //
  1171 	{
  1172 	switch(aChain)
  1173 		{
  1174 		case(EChainStd):return(CreateStdEntry(aName,aIsDir,aSize));
  1175 		case(EChainAlternate):return(CreateAlternateEntry(aName,aIsDir,aSize));
  1176 		case(EChainBackwards):return(CreateBackwardEntry(aName,aIsDir,aSize));
  1177 		case(EChainForwards):return(CreateForwardEntry(aName,aIsDir,aSize));
  1178 		default:return(KErrGeneral);
  1179 		}
  1180 	}
  1181 
  1182 LOCAL_C TInt DeleteEntry(const TDesC& aName,TBool aIsDir,TFatChain aChain)
  1183 //
  1184 // Delete entry with aName
  1185 //
  1186 	{
  1187 	switch(aChain)
  1188 		{
  1189 		case(EChainStd):return(DeleteStdEntry(aName,aIsDir));
  1190 		case(EChainAlternate):return(DeleteAlternateEntry(aName,aIsDir));
  1191 		case(EChainBackwards):return(DeleteBackwardEntry(aName,aIsDir));
  1192 		case(EChainForwards):return(DeleteForwardEntry(aName,aIsDir));
  1193 		default:return(KErrGeneral);
  1194 		}
  1195 	}
  1196 
  1197 LOCAL_C void TestRFsDelete(const TDesC& aName,TFatChain aChain,TInt aFileSize)
  1198 //
  1199 // test RFs::Delete
  1200 //
  1201 	{
  1202 	TInt failCount=TheFailCount;
  1203 	TInt r;
  1204 	test.Start(_L("TestRFsDelete"));
  1205 	FOREVER
  1206 		{
  1207 		test.Printf(_L("failCount=%d\n"),failCount);
  1208 		r=CreateEntry(aName,EFalse,aChain,aFileSize);
  1209 		test(r==KErrNone||r==KErrAlreadyExists);
  1210 		if(IsReset)
  1211 			{
  1212 			++TheFailCount;
  1213 			WriteLogFile();
  1214 			}
  1215 		r=SetWriteFailOn(failCount);
  1216 		test(r==KErrNone);
  1217 		r=TheFs.Delete(aName);
  1218 		if(r==KErrNone)
  1219 			break;
  1220 		test(r==WriteFailValue);
  1221 		r=TheFs.ScanDrive(gSessionPath);
  1222 		test(r==KErrNone);
  1223 		r=TheFs.CheckDisk(gSessionPath);
  1224 		test(r==KErrNone);
  1225 		++failCount;
  1226 		}
  1227 	r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1228 	test(r==KErrNone);
  1229 	r=TheFs.CheckDisk(gSessionPath);
  1230 	test(r==KErrNone);
  1231 	test(!EntryExists(aName));
  1232 	++TheOpNumber;
  1233 	TheFailCount=0;
  1234 	}
  1235 
  1236 LOCAL_C void TestRFsRmDir(const TDesC& aName,TFatChain aChain,TInt aDirSize)
  1237 //
  1238 // test RFs::RmDir
  1239 //
  1240 	{
  1241 	TInt failCount=TheFailCount;
  1242 	TInt r;
  1243 	test.Next(_L("TestRFsRmDir"));
  1244 	switch (aChain)
  1245 		{
  1246 		case EChainStd:
  1247 			RDebug::Print(_L("Chain Std %S size %d"), &aName, aDirSize);
  1248 			break;
  1249 		case EChainAlternate:
  1250 			RDebug::Print(_L("Chain Alternate %S size %d"), &aName, aDirSize);
  1251 			break;
  1252 		case EChainBackwards:
  1253 			RDebug::Print(_L("Chain Backwards %S size %d"), &aName, aDirSize);
  1254 			break;
  1255 		case EChainForwards:
  1256 			RDebug::Print(_L("Chain Forwards %S size %d"), &aName, aDirSize);
  1257 			break;
  1258 		default:
  1259 			break;
  1260 		}
  1261 	FOREVER
  1262 		{
  1263 		test.Printf(_L("failCount=%d\n"),failCount);
  1264 		r=CreateEntry(aName,ETrue,aChain,aDirSize);
  1265 		test(r==KErrNone||r==KErrAlreadyExists);
  1266 		if(IsReset)
  1267 			{
  1268 			++TheFailCount;
  1269 			WriteLogFile();
  1270 			}
  1271 		r=SetWriteFailOn(failCount);
  1272 		test(r==KErrNone);
  1273 		r=TheFs.RmDir(aName);
  1274 		if(r==KErrNone)
  1275 			break;
  1276 		test(r==WriteFailValue);
  1277 		r=TheFs.ScanDrive(gSessionPath);
  1278 		RDebug::Print(_L("%6d: ScanDrive = %d"), __LINE__, r);
  1279 		if (r != KErrNone)
  1280 		{
  1281 			RDebug::Print(_L("ScanDrive fail %d"), r);
  1282 			DumpFat();
  1283 			DumpData(NULL, 0, 200);
  1284 		}
  1285 		test(r==KErrNone);
  1286 		r=TheFs.CheckDisk(gSessionPath);
  1287 		RDebug::Print(_L("%6d: CheckDisk = %d"), __LINE__, r);
  1288 		test(r==KErrNone);
  1289 		++failCount;
  1290 		}
  1291 	r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1292 	test(r==KErrNone);
  1293 	r=TheFs.CheckDisk(gSessionPath);
  1294 	test(r==KErrNone);
  1295 	test(!EntryExists(aName));
  1296 	++TheOpNumber;
  1297 	TheFailCount=0;
  1298 	}
  1299 
  1300 LOCAL_C void TestRFsMkDir(const TDesC& aName)
  1301 //
  1302 // test RFs::MkDir
  1303 //
  1304 	{
  1305 	TInt failCount=TheFailCount;
  1306 	TInt r;
  1307 	test.Next(_L("TestRFsMkDir"));
  1308 	FOREVER
  1309 		{
  1310 		test.Printf(_L("failCount=%d\n"),failCount);
  1311 		r=DeleteEntry(aName,ETrue,EChainStd);
  1312 		test(r==KErrNone||r==KErrNotFound);
  1313 		if(IsReset)
  1314 			{
  1315 			++TheFailCount;
  1316 			WriteLogFile();
  1317 			}
  1318 		r=SetWriteFailOn(failCount);
  1319 		test(r==KErrNone);
  1320 		r=TheFs.MkDir(aName);
  1321 		if(r==KErrNone)
  1322 			break;
  1323 		test(r==WriteFailValue);
  1324 		r=TheFs.ScanDrive(gSessionPath);
  1325 		test(r==KErrNone);
  1326 		r=TheFs.CheckDisk(gSessionPath);
  1327 		test(r==KErrNone);
  1328 		++failCount;
  1329 		}
  1330 	r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1331 	test(r==KErrNone);
  1332 	r=TheFs.CheckDisk(gSessionPath);
  1333 	test(r==KErrNone);
  1334 	test(EntryExists(aName));
  1335 	r=DeleteEntry(aName,ETrue,EChainStd);
  1336 	test(r==KErrNone);
  1337 	++TheOpNumber;
  1338 	TheFailCount=0;
  1339 	}
  1340 
  1341 LOCAL_C void TestRFsRename(const TDesC& aOldName,const TDesC& aNewName,TBool aIsDir,TFatChain aChain,TInt aSize)
  1342 //
  1343 // test RFs::Rename
  1344 //
  1345 	{
  1346 	test.Next(_L("TestRFsRename"));
  1347 	TInt failCount=TheFailCount;
  1348 	TInt r;
  1349 	TEntry oldEntryInfo,newEntryInfo;
  1350 	FOREVER
  1351 		{
  1352 		test.Printf(_L("failCount=%d\n"),failCount);
  1353 		r=CreateEntry(aOldName,aIsDir,aChain,aSize);
  1354 		test(r==KErrNone||r==KErrAlreadyExists);
  1355 		r=DeleteEntry(aNewName,aIsDir,aChain);
  1356 		test(r==KErrNone||r==KErrNotFound);
  1357 		GetEntryDetails(aOldName,oldEntryInfo);
  1358 		if(IsReset)
  1359 			{
  1360 			++TheFailCount;
  1361 			WriteLogFile();
  1362 			}
  1363 		r=SetWriteFailOn(failCount);
  1364 		test(r==KErrNone);
  1365 		r=TheFs.Rename(aOldName,aNewName);
  1366 		if(r==KErrNone)
  1367 			break;
  1368 		if(r!=WriteFailValue)
  1369 			{
  1370 			test.Printf(_L("r=%d\n"),r);
  1371 			test(EFalse);
  1372 			}
  1373 		test(r==WriteFailValue);
  1374 		r=TheFs.ScanDrive(gSessionPath);
  1375 		test(r==KErrNone);
  1376 		r=TheFs.CheckDisk(gSessionPath);
  1377 		test(r==KErrNone);
  1378 		// no start cluster if aSize==0
  1379 		if(aSize!=0)
  1380 			test(OneEntryExists(aOldName,aNewName));
  1381 		++failCount;
  1382 		}
  1383 	r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1384 	test(r==KErrNone);
  1385 	r=TheFs.CheckDisk(gSessionPath);
  1386 	test(r==KErrNone);
  1387 	test(EntryExists(aNewName) && !EntryExists(aOldName));
  1388 	GetEntryDetails(aNewName,newEntryInfo);
  1389 	test(IsSameEntryDetails(oldEntryInfo,newEntryInfo));
  1390 	r=DeleteEntry(aNewName,aIsDir,aChain);
  1391 	test(r==KErrNone);
  1392 	++TheOpNumber;
  1393 	TheFailCount=0;
  1394 	}
  1395 
  1396 LOCAL_C void TestRFsReplace(const TDesC& aOldName, const TDesC& aNewName,TBool aBothExist,TFatChain aChain,TInt aFileSize)
  1397 //
  1398 // test RFs::Replace
  1399 //
  1400 	{
  1401 	
  1402 	TInt failCount=TheFailCount;
  1403 	TInt r;
  1404 	if(aBothExist)
  1405 		test.Next(_L("TestRFsReplace with new name existing"));
  1406 	else
  1407 		test.Next(_L("TestRFsReplace with new name not existing"));
  1408 	TEntry oldEntryInfo,newEntryInfo;
  1409 	FOREVER
  1410 		{
  1411 		test.Printf(_L("failCount=%d\n"),failCount);
  1412 		r=CreateEntry(aOldName,EFalse,aChain,aFileSize);
  1413 		test(r==KErrNone||r==KErrAlreadyExists);
  1414 		if(aBothExist)
  1415 			{
  1416 			r=CreateEntry(aNewName,EFalse,aChain,aFileSize);
  1417 			test(r==KErrNone||r==KErrAlreadyExists);
  1418 			}
  1419 		else
  1420 			{
  1421 			r=DeleteEntry(aNewName,EFalse,aChain);
  1422 			test(r==KErrNone||r==KErrNotFound);
  1423 			}
  1424 		GetEntryDetails(aOldName,oldEntryInfo);
  1425 		if(IsReset)
  1426 			{
  1427 			++TheFailCount;
  1428 			WriteLogFile();
  1429 			}
  1430 		r=SetWriteFailOn(failCount);
  1431 		test(r==KErrNone);
  1432 		r=TheFs.Replace(aOldName,aNewName);
  1433 		if(r==KErrNone)
  1434 			break;
  1435 		test(r==WriteFailValue);
  1436 		r=TheFs.ScanDrive(gSessionPath);
  1437 		test(r==KErrNone);
  1438 		r=TheFs.CheckDisk(gSessionPath);
  1439 		test(r==KErrNone);
  1440 		if(!aBothExist && aFileSize!=0)
  1441 			test(OneEntryExists(aOldName,aNewName));
  1442 		else if(aBothExist)
  1443 			test(EntryExists(aOldName)||EntryExists(aNewName));
  1444 		++failCount;
  1445 		}
  1446 	r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1447 	test(r==KErrNone);
  1448 	r=TheFs.CheckDisk(gSessionPath);
  1449 	test(r==KErrNone);
  1450 	test(EntryExists(aNewName) && !EntryExists(aOldName));
  1451 	GetEntryDetails(aNewName,newEntryInfo);
  1452 	test(IsSameEntryDetails(oldEntryInfo,newEntryInfo));
  1453 	r=DeleteEntry(aNewName,EFalse,aChain);
  1454 	test(r==KErrNone);
  1455 	++TheOpNumber;
  1456 	TheFailCount=0;
  1457 	}
  1458 
  1459 LOCAL_C void TestRFileCreate(const TDesC& aName)
  1460 //
  1461 // test RFile::Create
  1462 //
  1463 	{
  1464 	TInt failCount=TheFailCount;
  1465 	TInt r;
  1466 	test.Next(_L("TestRFileCreate"));
  1467 	FOREVER
  1468 		{
  1469 		test.Printf(_L("failCount=%d\n"),failCount);
  1470 		r=DeleteEntry(aName,EFalse,EChainStd);
  1471 		test(r==KErrNone||r==KErrNotFound);
  1472 		if(IsReset)
  1473 			{
  1474 			++TheFailCount;
  1475 			WriteLogFile();
  1476 			}
  1477 		r=SetWriteFailOn(failCount);
  1478 		test(r==KErrNone);
  1479 		RFile file;
  1480 		r=file.Create(TheFs,aName,EFileShareAny);
  1481 		if(r==KErrNone)
  1482 			{
  1483 			r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1484 			test(r==KErrNone);
  1485 			file.Close();
  1486 			break;
  1487 			}
  1488 		test(r==WriteFailValue);
  1489 		r=TheFs.ScanDrive(gSessionPath);
  1490 		test(r==KErrNone);
  1491 		r=TheFs.CheckDisk(gSessionPath);
  1492 		test(r==KErrNone);
  1493 		++failCount;
  1494 		}
  1495 	r=TheFs.CheckDisk(gSessionPath);
  1496 	test(r==KErrNone);
  1497 	test(EntryExists(aName));
  1498 	r=DeleteEntry(aName,EFalse,EChainStd);
  1499 	test(r==KErrNone);
  1500 	++TheOpNumber;
  1501 	TheFailCount=0;
  1502 	}
  1503 
  1504 LOCAL_C void TestRFileTemp(const TDesC& aPath)
  1505 //
  1506 // test RFile::Temp
  1507 //
  1508 	{
  1509 	TInt failCount=TheFailCount;
  1510 	TInt r;
  1511 	test.Next(_L("TestRFileTemp"));
  1512 	TFileName temp;
  1513 	FOREVER
  1514 		{
  1515 		test.Printf(_L("failCount=%d\n"),failCount);
  1516 		if(IsReset)
  1517 			{
  1518 			++TheFailCount;
  1519 			WriteLogFile();
  1520 			}
  1521 		r=SetWriteFailOn(failCount);
  1522 		test(r==KErrNone);
  1523 		RFile file;
  1524  		r=file.Temp(TheFs,aPath,temp,EFileShareAny);
  1525 		if(r==KErrNone)
  1526 			{
  1527 			r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1528 			test(r==KErrNone);
  1529 			file.Close();
  1530 			break;
  1531 			}
  1532 		test(r==WriteFailValue);
  1533 		r=TheFs.ScanDrive(gSessionPath);
  1534 		test(r==KErrNone);
  1535 		r=TheFs.CheckDisk(gSessionPath);
  1536 		test(r==KErrNone);
  1537 		++failCount;
  1538 		}
  1539 	r=TheFs.CheckDisk(gSessionPath);
  1540 	test(r==KErrNone);
  1541 	test(EntryExists(temp));
  1542 	r=DeleteEntry(temp,EFalse,EChainStd);
  1543 	test(r==KErrNone);
  1544 	++TheOpNumber;
  1545 	TheFailCount=0;
  1546 	}
  1547 
  1548 LOCAL_C void TestRFileRename(const TDesC& aOldName, const TDesC& aNewName,TFatChain aChain,TInt aFileSize)
  1549 //
  1550 // test RFile::Rename
  1551 //
  1552 	{
  1553 	TInt failCount=TheFailCount;
  1554 	TInt r;
  1555 	test.Next(_L("TestRFileRename"));
  1556 	TEntry oldEntryInfo,newEntryInfo;
  1557 	FOREVER
  1558 		{
  1559 		test.Printf(_L("failCount=%d\n"),failCount);
  1560 		r=CreateEntry(aOldName,EFalse,aChain,aFileSize);
  1561 		test(r==KErrNone||r==KErrAlreadyExists);
  1562 		r=DeleteEntry(aNewName,EFalse,aChain);
  1563 		test(r==KErrNone||r==KErrNotFound);
  1564 		GetEntryDetails(aOldName,oldEntryInfo);
  1565 		if(IsReset)
  1566 			{
  1567 			++TheFailCount;
  1568 			WriteLogFile();
  1569 			}
  1570 		RFile file;
  1571 		r=file.Open(TheFs,aOldName,EFileShareExclusive|EFileWrite);
  1572 		test(r==KErrNone);
  1573 		r=SetWriteFailOn(failCount);
  1574 		test(r==KErrNone);
  1575 		r=file.Rename(aNewName);
  1576 		if(r==KErrNone)
  1577 			{
  1578 			r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1579 			test(r==KErrNone);
  1580 			file.Close();
  1581 			break;
  1582 			}
  1583 		test(r==WriteFailValue);
  1584 		file.Close();
  1585 		r=TheFs.ScanDrive(gSessionPath);
  1586 		test(r==KErrNone);
  1587 		r=TheFs.CheckDisk(gSessionPath);
  1588 		test(r==KErrNone);
  1589 		if(aFileSize)
  1590 			test(OneEntryExists(aOldName,aNewName));
  1591 		++failCount;
  1592 		}
  1593 	r=TheFs.CheckDisk(gSessionPath);
  1594 	test(r==KErrNone);
  1595 	test(EntryExists(aNewName) && !EntryExists(aOldName));
  1596 	GetEntryDetails(aNewName,newEntryInfo);
  1597 	test(IsSameEntryDetails(oldEntryInfo,newEntryInfo));
  1598 	r=DeleteEntry(aNewName,EFalse,aChain);
  1599 	test(r==KErrNone);
  1600 	++TheOpNumber;
  1601 	TheFailCount=0;
  1602 	}
  1603 
  1604 LOCAL_C void TestRFileReplace(const TDesC& aName,TBool aAlreadyExists,TFatChain aChain,TInt aFileSize)
  1605 //
  1606 // test RFile::Replace
  1607 //
  1608 	{
  1609 	TInt failCount=TheFailCount;
  1610 	TInt r;
  1611 	test.Next(_L("TestRFileReplace"));
  1612 	FOREVER
  1613 		{
  1614 		test.Printf(_L("failCount=%d\n"),failCount);
  1615 		if(aAlreadyExists)
  1616 			{
  1617 			r=CreateEntry(aName,EFalse,aChain,aFileSize);
  1618 			test(r==KErrNone||r==KErrAlreadyExists);
  1619 			}
  1620 		else
  1621 			{
  1622 			r=DeleteEntry(aName,EFalse,aChain);
  1623 			test(r==KErrNone||r==KErrNotFound);
  1624 			}
  1625 		if(IsReset)
  1626 			{
  1627 			++TheFailCount;
  1628 			WriteLogFile();
  1629 			}
  1630 		r=SetWriteFailOn(failCount);
  1631 		test(r==KErrNone);
  1632 		RFile file;
  1633 		r=file.Replace(TheFs,aName,EFileShareAny);
  1634 		if(r==KErrNone)
  1635 			{
  1636 			r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1637 			test(r==KErrNone);
  1638 			file.Close();
  1639 			break;
  1640 			}
  1641 		test(r==WriteFailValue);
  1642 		r=TheFs.ScanDrive(gSessionPath);
  1643 		test(r==KErrNone);
  1644 		r=TheFs.CheckDisk(gSessionPath);
  1645 		test(r==KErrNone);
  1646 		++failCount;
  1647 		}
  1648 	r=TheFs.CheckDisk(gSessionPath);
  1649 	test(r==KErrNone);
  1650 	test(EntryExists(aName));
  1651 	r=DeleteEntry(aName,EFalse,aChain);
  1652 	test(r==KErrNone);
  1653 	if(!aAlreadyExists)
  1654 		{
  1655 		++TheOpNumber;
  1656 		TheFailCount=0;
  1657 		}
  1658 	else
  1659 		{
  1660 		++TheFunctionNumber;
  1661 		TheOpNumber=TheFailCount=0;
  1662 		}
  1663 	}
  1664 
  1665 LOCAL_C void TestRFileSetSize(const TDesC& aName,TFatChain aChain,TInt aOldFileSize,TInt aNewFileSize)
  1666 //
  1667 // test RFile::SetSize
  1668 //
  1669 	{
  1670 	TInt failCount=TheFailCount;
  1671 	TInt r;
  1672 	test.Next(_L("TestRFileSetSize"));
  1673 	test.Printf(_L("old size=%d new size=%d\n"),aOldFileSize,aNewFileSize);
  1674 	FOREVER
  1675 		{
  1676 		test.Printf(_L("failCount=%d\n"),failCount);
  1677 		r=CreateEntry(aName,EFalse,aChain,aOldFileSize);
  1678 		test(r==KErrNone||r==KErrAlreadyExists);
  1679 		if(IsReset)
  1680 			{
  1681 			++TheFailCount;
  1682 			WriteLogFile();
  1683 			}
  1684 		r=SetWriteFailOn(failCount);
  1685 		test(r==KErrNone);
  1686 		RFile file;
  1687 		r=file.Open(TheFs,aName,EFileShareAny|EFileWrite);
  1688 		test(r==KErrNone);
  1689 		r=file.SetSize(aNewFileSize);
  1690 		// close the file before testing the return value!
  1691 		file.Close();
  1692 		if(r==KErrNone)
  1693 			{
  1694 			r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1695 			test(r==KErrNone);
  1696 			file.Close();
  1697 			break;
  1698 			}
  1699 		file.Close();
  1700 		test(r==WriteFailValue);
  1701 		r=TheFs.ScanDrive(gSessionPath);
  1702 		test(r==KErrNone);
  1703 		r=TheFs.CheckDisk(gSessionPath);
  1704 		test(r==KErrNone);
  1705 		r=file.Open(TheFs,aName,EFileShareAny|EFileWrite);
  1706 		test(r==KErrNone);
  1707 		TInt size;
  1708 		r=file.Size(size);
  1709 		test(r==KErrNone);
  1710 		test(size==aNewFileSize||size==aOldFileSize);
  1711 		file.Close();
  1712 		++failCount;
  1713 		}
  1714 	r=TheFs.CheckDisk(gSessionPath);
  1715 	test(r==KErrNone);
  1716 	RFile file;
  1717 	r=file.Open(TheFs,aName,EFileShareAny);
  1718 	test(r==KErrNone);
  1719 	TInt fileSize;
  1720 	r=file.Size(fileSize);
  1721 	test(r==KErrNone);
  1722 	test(aNewFileSize==fileSize);
  1723 	file.Close();
  1724 	r=DeleteEntry(aName,EFalse,aChain);
  1725 	test(r==KErrNone);
  1726 	++TheFunctionNumber;
  1727 	TheFailCount=0;
  1728 	}
  1729 
  1730 LOCAL_C void TestRFileWrite(const TDesC& aName,TFatChain aChain,TInt aFileSize,TInt aPos,TInt aLength)
  1731 //
  1732 // test RFile::Write
  1733 //
  1734 	{
  1735 	TInt failCount=TheFailCount;
  1736 	TInt r;
  1737 	test.Next(_L("TestRFileWrite"));
  1738 	test.Printf(_L("aFileSize=%d,aPos=%d,aLength=%d\n"),aFileSize,aPos,aLength);
  1739 	TInt newSize=(aFileSize>=aPos+aLength)?aFileSize:aPos+aLength;
  1740 	HBufC8* desPtr;
  1741 	desPtr=HBufC8::New(aLength);
  1742 	test(desPtr!=NULL);
  1743 	TPtr8 des=desPtr->Des();
  1744 	des.SetLength(aLength);
  1745 	InitialiseWriteBuffer(des);
  1746 	FOREVER
  1747 		{
  1748 		test.Printf(_L("failCount=%d\n"),failCount);
  1749 		r=CreateEntry(aName,EFalse,aChain,aFileSize);
  1750 		test(r==KErrNone||r==KErrAlreadyExists);
  1751 		if(IsReset)
  1752 			{
  1753 			++TheFailCount;
  1754 			WriteLogFile();
  1755 			}
  1756 		r=SetWriteFailOn(failCount);
  1757 		test(r==KErrNone);
  1758 		RFile file;
  1759 		r=file.Open(TheFs,aName,EFileShareAny|EFileWrite);
  1760 		test(r==KErrNone);
  1761 		r=file.Write(aPos,des,aLength);
  1762 		if(r==KErrNone)
  1763 			{
  1764 			r=TheFs.ControlIo(gSessionPath[0]-'A',KControlIoWriteFailOff);
  1765 			test(r==KErrNone);
  1766 			file.Close();
  1767 			break;
  1768 			}
  1769 		test(r==WriteFailValue);
  1770 		file.Close();
  1771 		r=TheFs.ScanDrive(gSessionPath);
  1772 		test(r==KErrNone);
  1773 		r=TheFs.CheckDisk(gSessionPath);
  1774 		test(r==KErrNone);
  1775 		file.Open(TheFs,aName,EFileShareAny);
  1776 		test(r==KErrNone);
  1777 		TInt fileSize;
  1778 		r=file.Size(fileSize);
  1779 		// with fair scheduling enabled it's possible for the file 
  1780 		// size to grow even if the write appears to have failed...
  1781 //		test(fileSize==aFileSize||fileSize==newSize);
  1782 		test(fileSize>=aFileSize && fileSize <= newSize);
  1783 
  1784 		file.Close();
  1785 		++failCount;
  1786 		}
  1787 	r=TheFs.CheckDisk(gSessionPath);
  1788 	test(r==KErrNone);
  1789 	RFile file;
  1790 	r=file.Open(TheFs,aName,EFileShareAny);
  1791 	test(r==KErrNone);
  1792 	TInt fileSize;
  1793 	r=file.Size(fileSize);
  1794 	test(r==KErrNone);
  1795 	test(newSize==fileSize);
  1796 	HBufC8* desPtr2;
  1797 	desPtr2=HBufC8::New(aLength);
  1798 	test(desPtr2!=NULL);
  1799 	TPtr8 des2=desPtr2->Des();
  1800 	des2.SetLength(aLength);
  1801 	r=file.Read(aPos,des2,des2.Length());
  1802 	test(r==KErrNone);
  1803 	r=des2.Compare(des);
  1804 	test(r==0);
  1805 	file.Close();
  1806 	r=DeleteEntry(aName,EFalse,aChain);
  1807 	test(r==KErrNone);
  1808 	delete desPtr;
  1809 	delete desPtr2;
  1810 	++TheFunctionNumber;
  1811 	TheFailCount=0;
  1812 	}
  1813 
  1814 
  1815 LOCAL_C void TestOperations(const TDesC& aOldName,const TDesC& aNewName,TFatChain aChain,TInt aFileSize, TInt aDirClusters)
  1816 //
  1817 // Tests the specified operations
  1818 //
  1819 	{
  1820 	TFileName oldDirName=aOldName;
  1821 	TFileName newDirName=aNewName;
  1822 	// create directory for directory operations
  1823 	oldDirName+=_L("\\");
  1824 	newDirName+=_L("\\");
  1825 	// locate path for RFile::Temp
  1826 	TInt pathPos=aOldName.LocateReverse('\\')+1;
  1827 	TFileName tempPath=aOldName.Left(pathPos);
  1828 	test.Printf(_L("aOldName=%S\n"),&aOldName);
  1829 	test.Printf(_L("aNewName=%S\n"),&aNewName);
  1830 	test.Printf(_L("tempPath=%S\n"),&tempPath);
  1831 	switch(TheOpNumber)
  1832 		{
  1833 		case(0):TestRFsDelete(aOldName,aChain,aFileSize);
  1834 		case(1):TestRFsRmDir(oldDirName,aChain,aDirClusters);
  1835 		case(2):TestRFsMkDir(oldDirName);
  1836 		case(3):TestRFsRename(aOldName,aNewName,EFalse,aChain,aFileSize);
  1837 		case(4):TestRFsRename(oldDirName,newDirName,ETrue,aChain,aDirClusters);
  1838 		case(5):TestRFsReplace(aOldName,aNewName,EFalse,aChain,aFileSize);
  1839 		case(6):TestRFsReplace(aOldName,aNewName,ETrue,aChain,aFileSize);
  1840 		case(7):TestRFileCreate(aOldName);
  1841 		case(8):TestRFileTemp(tempPath);
  1842 		case(9):TestRFileRename(aOldName,aNewName,aChain,aFileSize);
  1843 		case(10):TestRFileReplace(aOldName,EFalse,aChain,aFileSize);
  1844 		case(11):TestRFileReplace(aOldName,ETrue,aChain,aFileSize);break;
  1845 		default:test(EFalse);
  1846 		}
  1847 	test.End();
  1848 	}
  1849 
  1850 LOCAL_C void TestOperation0()
  1851 //
  1852 //
  1853 //
  1854 	{
  1855 	// tests entries in root directory
  1856 	test.Next(_L("TestOperation0"));
  1857 	TestOperations(_L("\\entryWithTwoVfats"),_L("\\anotherEntryWithTwo"),EChainStd,0,0);
  1858 	}
  1859 	
  1860 LOCAL_C void TestOperation1()
  1861 //
  1862 //
  1863 //
  1864 	{
  1865 	// tests entries in a full root directory
  1866 	test.Next(_L("TestOperation1"));
  1867 	if(TheFailCount==0)
  1868 		FillUpRootDir(4);
  1869 	TestOperations(_L("\\entryOne"),_L("\\entryTwo"),EChainStd,512,0);
  1870 	UnFillUpRootDir(4);
  1871 	}
  1872 
  1873 LOCAL_C void TestOperation2()
  1874 //
  1875 //
  1876 //
  1877 	{
  1878 	// tests entries in same subdir
  1879 	test.Next(_L("TestOperation2"));
  1880 	TestOperations(_L("\\test\\subdir1\\NameWithFourVFatEntriesWaffle"),_L("\\test\\subdir1\\aEntry"),EChainAlternate,5120,1);
  1881 	}
  1882 
  1883 
  1884 LOCAL_C void TestOperation3()
  1885 //
  1886 //
  1887 //
  1888 	{
  1889 	// tests entries in different subdir
  1890 	test.Next(_L("TestOperation3"));
  1891 	TestOperations(_L("\\test\\subdir1\\NameWithThreeEntries"),_L("\\ANother\\aEntrytwo"),EChainAlternate,15000,10);
  1892 	}
  1893 
  1894 
  1895 LOCAL_C void TestOperation4()
  1896 //
  1897 //
  1898 //
  1899 	{
  1900 	// tests entries with cluster chain of EChainForwards
  1901 	test.Next(_L("TestOperation4"));
  1902 	TestOperations(_L("\\test\\subdir1\\aEntry"),_L("\\aEntry"),EChainForwards,12799,25);
  1903 	}
  1904 
  1905 
  1906 LOCAL_C void TestOperation5()
  1907 //
  1908 //
  1909 //
  1910 	{
  1911 	// tests entries with cluster chain of EChainBackwards
  1912 	test.Next(_L("TestOperation5"));
  1913 	TestOperations(_L("\\test\\subdir1\\aEntry"),_L("\\ANother\\EntrywithThree"),EChainBackwards,51199,10);
  1914 	}
  1915 
  1916 LOCAL_C void TestOperation6()
  1917 //
  1918 //
  1919 //
  1920 	{
  1921 	// tests entries where old name has a very long name
  1922 	test.Next(_L("TestOperation6"));
  1923 	TFileName longName=_L("\\test\\subdir1\\");
  1924 	MakeVeryLongName(longName);
  1925 	TestOperations(longName,_L("\\ANother\\e1"),EChainAlternate,5100,0);
  1926 	}
  1927 
  1928 LOCAL_C void TestOperation7()
  1929 //
  1930 //
  1931 //
  1932 	{
  1933 	// tests entries where new name fills up subdir cluster
  1934 	test.Next(_L("TestOperation7"));
  1935 	TFileName name=_L("\\test\\subdir2\\");
  1936 	// add entry with 7 vfat entries
  1937 	MakeEntryName(name,80);
  1938 	if(TheFailCount==0)
  1939 		CreateEntry(name,EFalse,EChainStd,1);
  1940 	TestOperations(_L("\\test\\subdir2\\EntryWithThree"),_L("\\test\\subdir2\\EntryWithThree-"),EChainStd,512,0);
  1941 	DeleteEntry(name,EFalse,EChainStd);
  1942 	}
  1943 
  1944 LOCAL_C void TestOperation8()
  1945 //
  1946 //
  1947 //
  1948 	{
  1949 	// tests entries where new name is first entry in new subdir cluster
  1950 	test.Next(_L("TestOperation8"));
  1951 	TFileName name=_L("\\test\\subdir2\\");
  1952 	// add entry with 10 vfat entries
  1953 	MakeEntryName(name,125);
  1954 	if(TheFailCount==0)
  1955 		CreateEntry(name,EFalse,EChainStd,175000);
  1956 	TestOperations(_L("\\test\\subdir2\\Entrywith3three"),_L("\\test\\subdir2\\entrywiththree-"),EChainStd,512,1);
  1957 	DeleteEntry(name,EFalse,EChainStd);
  1958 	}
  1959 
  1960 GLDEF_C void DoTests()
  1961 	{
  1962 	TInt r;
  1963 	if(!IsReset && IsInternalRam())
  1964 		{
  1965 		test.Printf(_L("Error: Internal ram drive not tested\n"));
  1966 		return;
  1967 		}
  1968 	if(!IsReset)
  1969 		QuickFormat();
  1970 
  1971 	DoReadBootSector();
  1972 	DumpBootSector();
  1973 	ClearDiskData();
  1974 
  1975 	r=TheFs.SetSessionPath(gSessionPath);
  1976 	test(r==KErrNone);
  1977 
  1978 	switch(TheFunctionNumber)
  1979 		{
  1980 		case(0):TestOperation0();
  1981 		case(1):{
  1982 				TestOperation1();
  1983 				r=TheFs.MkDir(_L("\\fat\\"));
  1984 				test(r==KErrNone);
  1985 				r=TheFs.MkDir(_L("\\test\\"));
  1986 				test(r==KErrNone);
  1987 				r=TheFs.MkDir(_L("\\ANother\\"));
  1988 				test(r==KErrNone);
  1989 				r=TheFs.MkDir(_L("\\test\\subdir1\\"));
  1990 				test(r==KErrNone);
  1991 				r=TheFs.MkDir(_L("\\test\\subdir2\\"));
  1992 				test(r==KErrNone);}
  1993 		case(2):{
  1994 				TestOperation2();
  1995 				// add some filler files
  1996 				CreateEntry(_L("\\test\\subdir1\\FillerOne"),EFalse,EChainStd,512);
  1997 				CreateEntry(_L("\\test\\subdir1\\FillerTwo"),EFalse,EChainStd,1024);}
  1998 		case(3):TestOperation3();
  1999 		case(4):{
  2000 				TestOperation4();
  2001 				// add some filler files
  2002 				CreateEntry(_L("\\ANother\\FillerThree"),EFalse,EChainStd,1536);
  2003 				CreateEntry(_L("\\test\\subdir1\\FillerFour"),EFalse,EChainStd,2048);}
  2004 		case(5):TestOperation5();
  2005 		case(6):TestOperation6();
  2006 		case(7):TestOperation7();
  2007 		case(8):TestOperation8();
  2008 		// increase size of file
  2009 		case(9):TestRFileSetSize(_L("\\entry1"),EChainStd,0,512);
  2010 		case(10):TestRFileSetSize(_L("\\entry1"),EChainAlternate,0,1025);
  2011 		case(11):TestRFileSetSize(_L("\\entry1"),EChainStd,1,512);
  2012 		// seek index (of CFatFileCB) resized
  2013 		case(12):TestRFileSetSize(_L("\\entry1"),EChainForwards,512,66666);
  2014 		// seek index resized
  2015 		case(13):TestRFileSetSize(_L("\\entry1"),EChainBackwards,32779,131074);
  2016 		// decrease size of file
  2017 		// seek index resized
  2018 		case(14):TestRFileSetSize(_L("\\entry1"),EChainForwards,133000,32768);
  2019 		// seek index resized
  2020 		case(15):TestRFileSetSize(_L("\\entry1"),EChainBackwards,65536,1);
  2021 		// seek index resized
  2022 		case(16):TestRFileSetSize(_L("\\entry1"),EChainAlternate,66554,0);
  2023 		case(17):TestRFileSetSize(_L("\\entry1"),EChainStd,1024,1);
  2024 		case(18):TestRFileSetSize(_L("\\entry1"),EChainAlternate,512,0);
  2025 		case(19):TestRFileWrite(_L("\\entry2"),EChainStd,0,0,512);
  2026 		case(20):TestRFileWrite(_L("\\entry2"),EChainAlternate,5120,512,1024);
  2027 		case(21):TestRFileWrite(_L("\\entry2"),EChainForwards,3584,3584,5000);
  2028 		case(22):TestRFileWrite(_L("\\entry2"),EChainBackwards,3000,2999,2000);
  2029 		// seek index resized
  2030 		case(23):TestRFileWrite(_L("\\entry2"),EChainBackwards,64000,64000,3000);
  2031 		// seek index resized
  2032 		case(24):TestRFileWrite(_L("\\entry2"),EChainForwards,131072,2,4000);break;
  2033 		default:test(EFalse);
  2034 		}
  2035 	DeleteEntry(_L("\\test\\subdir1\\FillerFour"),EFalse,EChainStd);
  2036 	DeleteEntry(_L("\\ANother\\FillerThree"),EFalse,EChainStd);
  2037 	DeleteEntry(_L("\\test\\subdir1\\FillerTwo"),EFalse,EChainStd);
  2038 	DeleteEntry(_L("\\test\\subdir1\\FillerOne"),EFalse,EChainStd);
  2039 	r=TheFs.RmDir(_L("\\test\\subdir2\\"));
  2040 	test(r==KErrNone);
  2041 	r=TheFs.RmDir(_L("\\test\\subdir1\\"));
  2042 	test(r==KErrNone);
  2043 	r=TheFs.RmDir(_L("\\ANother\\"));
  2044 	test(r==KErrNone);
  2045 	r=TheFs.RmDir(_L("\\test\\"));
  2046 	test(r==KErrNone);
  2047 	r=TheFs.RmDir(_L("\\fat\\"));
  2048 	test(r==KErrNone);
  2049 	if (gFatBuf)
  2050 		{
  2051 		delete gFatBuf;
  2052 		gFatBuf = NULL;
  2053 		}
  2054 	}
  2055 #endif