testexecfw/stf/stfext/testmodules/teftestmod/teftestmodulefw/tefutilities/TEFUtilityServer/src/tefformatdrive.cpp
changeset 2 8bb370ba6d1d
equal deleted inserted replaced
1:bbd31066657e 2:8bb370ba6d1d
       
     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 * This class implements the formdrive test step to format given drive
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 /**
       
    22  @file TEFFormaDrive.cpp
       
    23 */
       
    24 
       
    25 #include "tefformatdrive.h"
       
    26 
       
    27 //literals
       
    28 
       
    29 _LIT(KFormatType,		"formattype");
       
    30 _LIT(KDriveName,		"drivename");
       
    31 
       
    32 _LIT(KDriveSeperator,	":");
       
    33 _LIT(KDeviceSeperator,	"\\\\");
       
    34 _LIT(KDirSeperator,		"\\");
       
    35 
       
    36 
       
    37 /**
       
    38  This is constructor,which sets the test step name
       
    39 */
       
    40 CTEFFormatDrive::CTEFFormatDrive()
       
    41 	{
       
    42 	SetTestStepName(KFormatDrive);
       
    43 	}
       
    44 
       
    45 /**
       
    46  * Run preamble scripts for the test
       
    47  * Make the connection with the File Server and creates a session object
       
    48  * @return - TVerdict code
       
    49  * Override of base class pure virtual
       
    50  */
       
    51 TVerdict CTEFFormatDrive::doTestStepPreambleL()
       
    52 	{
       
    53 	User::LeaveIfError(iFs.Connect());
       
    54 	return TestStepResult();
       
    55 	}
       
    56 
       
    57 /**
       
    58  * Run postample scripts for the test
       
    59  * @return - TVerdict code
       
    60  * Override of base class pure virtual
       
    61  */
       
    62 TVerdict CTEFFormatDrive::doTestStepPostambleL()
       
    63 	{
       
    64 	iFs.Close();
       
    65 	return TestStepResult();
       
    66 	}
       
    67 
       
    68 /**
       
    69  * Overrides base class virtual
       
    70  * @return - TVerdict codes
       
    71  * Parses buildinfo.txt from \epoc32\... and prints it to TEF log file outout
       
    72  */
       
    73 TVerdict CTEFFormatDrive::doTestStepL()
       
    74 	{
       
    75 	INFO_PRINTF1(_L("Formating Disk"));
       
    76 	
       
    77 	TDriveUnit	driveUnit;
       
    78 	ReadDriveNameL(driveUnit);
       
    79 
       
    80 	//	Get the format type
       
    81 	TPtrC	formatType;
       
    82 	if(!GetStringFromConfig(ConfigSection(), KFormatType, formatType))
       
    83 		{
       
    84 		WARN_PRINTF1(_L("No format type specified - default is EFullFormat"));
       
    85 		}
       
    86 
       
    87 	if(TestStepResult() == EPass)
       
    88 		{
       
    89 		TBool		formatFlag=EFalse;
       
    90 		TDriveName	drivePath(driveUnit.Name());
       
    91 		//Format the specified driver.
       
    92 		TInt		errCode= FormatDiskL(drivePath, formatType, formatFlag);
       
    93 
       
    94 		if(formatFlag)
       
    95 			{
       
    96 			INFO_PRINTF1(_L("Disk is fully formatted"));
       
    97 			SetTestStepResult(EPass);
       
    98 			}
       
    99 		else
       
   100 			{
       
   101 			INFO_PRINTF2(_L("Error in formatting %D"), errCode);
       
   102 			SetTestStepResult(EFail);
       
   103 			SetTestStepError(errCode);
       
   104 			}
       
   105 		}
       
   106 
       
   107 	return TestStepResult();
       
   108 	}
       
   109 /**
       
   110 This functions reads the drive name specified in the ini file
       
   111 @param aDriveName - specifies the drive name
       
   112 @panic - system wide error
       
   113 */
       
   114 void CTEFFormatDrive::ReadDriveNameL(TDriveUnit &aDriveName)
       
   115 {
       
   116 	TPtrC driveName;
       
   117 	//read the drive name from ini file
       
   118 	TInt result = GetStringFromConfig(ConfigSection(),KDriveName,driveName);
       
   119 	if(result)
       
   120 		{
       
   121 		INFO_PRINTF2(_L("Drive name is %S"),&driveName);
       
   122 		HBufC*	drivePath;
       
   123 		TInt	parseRet=PreParseFileNameL(driveName, drivePath);
       
   124 		CleanupStack::PushL(drivePath);
       
   125 
       
   126 		if ( parseRet==KErrNone )
       
   127 			{
       
   128 			aDriveName=drivePath->Des();
       
   129 			}
       
   130 		else
       
   131 			{
       
   132 			ERR_PRINTF2(_L("Drive %S illegal"), &driveName);
       
   133 			SetTestStepResult(EFail);
       
   134 			}
       
   135 			CleanupStack::PopAndDestroy(drivePath);
       
   136 		}
       
   137 		else
       
   138 		{
       
   139 		 ERR_PRINTF1(_L("Unable to read the drive name"));
       
   140 		 SetTestStepResult(EFail);
       
   141 		}	
       
   142 }
       
   143 /**
       
   144 This method to parse a file name which may include the device name
       
   145 @param aInputFileName - input file name which may contain the drive name also
       
   146 @param aOutputFileName - holds the return value.
       
   147 @panic - system wide error
       
   148 */
       
   149 TInt CTEFFormatDrive::PreParseFileNameL(const TDesC& aInputFileName, HBufC*& aOutputFileName)
       
   150 	{
       
   151 	aOutputFileName=HBufC::NewL(aInputFileName.Length());
       
   152 
       
   153 	TInt	ret=KErrNone;
       
   154 	const	TDesC&	sep=KDeviceSeperator;
       
   155 
       
   156 	if ( aInputFileName.FindC(sep) == 0 )
       
   157 		{
       
   158 		//	Starts with device name
       
   159 		TPtrC	strippedFileName=aInputFileName.Right(aInputFileName.Length()-sep.Length());
       
   160 		INFO_PRINTF2(_L("DeviceSeperator stripped %S"), &strippedFileName);
       
   161 
       
   162 		TInt	find=strippedFileName.Find(KDirSeperator);
       
   163 		TPtrC	driveName(find==KErrNotFound
       
   164 							? strippedFileName
       
   165 							: strippedFileName.Left(find));
       
   166 
       
   167 		INFO_PRINTF2(_L("Drive name %S"), &driveName);
       
   168 		TFileName	driveNameRead;
       
   169 		TInt		drive;
       
   170 		TBool		found=EFalse;
       
   171 		for ( drive=EDriveA; (drive<=EDriveZ) && (!found); )
       
   172 			{
       
   173 			if ( iFs.GetDriveName(drive, driveNameRead)==KErrNone )
       
   174 				{
       
   175 				if ( driveNameRead.Length()>0 )
       
   176 					{
       
   177 					INFO_PRINTF3(_L("Drive %c:='%S'"), TUint(drive+'A'), &driveNameRead);
       
   178 					}
       
   179 				found=(driveName.Compare(driveNameRead)==0);
       
   180 				}
       
   181 			if ( !found )
       
   182 				{
       
   183 				++drive;
       
   184 				}
       
   185 			}
       
   186 
       
   187 		if ( found )
       
   188 			{
       
   189 			TPtr	ptrTemp=aOutputFileName->Des();
       
   190 			ptrTemp.Format(_L("%c"), TUint(drive+'A'));
       
   191 			ptrTemp.Append(KDriveSeperator);
       
   192 			ptrTemp.Append(strippedFileName.Right(strippedFileName.Length()-driveName.Length()));
       
   193 			}
       
   194 		else
       
   195 			{
       
   196 			WARN_PRINTF2(_L("Drive %S not found"), &driveName);
       
   197 			ret=KErrNotFound;
       
   198 			SetTestStepResult(EFail);
       
   199 			}
       
   200 		}
       
   201 	else
       
   202 		{
       
   203 		*aOutputFileName=aInputFileName;
       
   204 		}
       
   205 
       
   206 	return ret;
       
   207 	}
       
   208 /**
       
   209  This functions reads the type of format that users wants to do and also drive name
       
   210  @param aDrivePath - path of the drive to be formated
       
   211  @param aFormatType - type of format 
       
   212  @param aFormatFlag - flag
       
   213  
       
   214 */
       
   215 TInt CTEFFormatDrive::FormatDiskL(TPtrC aDrivePath, TPtrC aFormatType, TBool& aFormatFlag)
       
   216 {
       
   217 
       
   218 	TInt	errCode=KErrNone;
       
   219 	//Format type default to EFullFormat
       
   220 	TUint	formatType=EFullFormat;
       
   221 	if(aFormatType == _L("EHighDensity"))
       
   222 		{
       
   223 		formatType = EHighDensity;
       
   224 		}
       
   225 	else if(aFormatType == _L("ELowDensity"))
       
   226 		{
       
   227 		formatType = ELowDensity;
       
   228 		}
       
   229 	else if(aFormatType == _L("EQuickFormat"))
       
   230 		{
       
   231 		formatType = EQuickFormat;
       
   232 		}
       
   233 	else if((aFormatType == _L("EFullFormat")) || (aFormatType.Length() == 0))
       
   234 		{
       
   235 		formatType = EFullFormat;
       
   236 		}
       
   237 	else
       
   238 		{
       
   239 		ERR_PRINTF2(_L("Illegal format type %S."), &aFormatType);
       
   240 		errCode=KErrArgument;
       
   241 		}
       
   242 
       
   243 	aFormatFlag = EFalse;
       
   244 
       
   245 	if ( errCode == KErrNone )
       
   246 		{
       
   247 		//Open the format object for format
       
   248 		//Format object to format.
       
   249 		RFormat	formatObj;
       
   250 
       
   251 		//To hold the number of tracks in the disk
       
   252 		TInt	trackCount=0;
       
   253 
       
   254 		//Error code
       
   255 		errCode = formatObj.Open(iFs, aDrivePath, formatType, trackCount);
       
   256 		switch ( errCode )
       
   257 			{
       
   258 		case KErrNone:
       
   259 			//Format while track remains for formating
       
   260 			while(trackCount>0)
       
   261 				{
       
   262 				errCode = formatObj.Next(trackCount);
       
   263 				if(errCode != KErrNone)
       
   264 					{
       
   265 					ERR_PRINTF2(_L("Error %d formatObj.Next."), errCode);
       
   266 					ERR_PRINTF2(_L("Drive Path  %S."), &aDrivePath);
       
   267 					ERR_PRINTF2(_L("Format Type %d."), formatType);
       
   268 					ERR_PRINTF2(_L("Track Count %d."), trackCount);
       
   269 					aFormatFlag = EFalse;
       
   270 					break;
       
   271 					}
       
   272 				else
       
   273 					{
       
   274 					aFormatFlag = ETrue;
       
   275 					}
       
   276 				}
       
   277 			formatObj.Close();
       
   278 			break;
       
   279 		default:
       
   280 			ERR_PRINTF2(_L("Error %d formatObj.Open."), errCode);
       
   281 			ERR_PRINTF2(_L("Drive Path  %S."), &aDrivePath);
       
   282 			ERR_PRINTF2(_L("Format Type %d."), formatType);
       
   283 			ERR_PRINTF2(_L("Track Count %d."), trackCount);
       
   284 			}
       
   285 		}
       
   286 
       
   287 	return errCode;
       
   288 }