os/persistentdata/persistentstorage/sqlite3api/OsLayer/test_fileutil.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/OsLayer/test_fileutil.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,364 @@
     1.4 +// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <f32file.h>
    1.20 +#include <sys/param.h>
    1.21 +#include <stdlib.h>
    1.22 +#include <string.h>
    1.23 +#include <unistd.h>
    1.24 +#include "tcl.h"
    1.25 +
    1.26 +_LIT(KTestExt, "*.test");
    1.27 +_LIT(KTclExt, "*.tcl");
    1.28 +_LIT(KExplainExt, "*.explain");
    1.29 +_LIT(KTclTempFilePrefix, "tcl*");
    1.30 +
    1.31 +//Note: every time when you add a new KTestFileXX constant here, don't forget to add that constant as a new entry into
    1.32 +//"TPtrC fileNames[]" array in DoDeleteTestFilesL() function.
    1.33 +_LIT(KTestFile1, "test1.bt");
    1.34 +_LIT(KTestFile2, "test.bu");
    1.35 +_LIT(KTestFile3, "backup.db");
    1.36 +_LIT(KTestFile4, "bak.db");
    1.37 +_LIT(KTestFile5, "corrupt.db");
    1.38 +_LIT(KTestFile6, "ptf2.db");
    1.39 +_LIT(KTestFile7, "test1.db");
    1.40 +_LIT(KTestFile8, "test2.db");
    1.41 +_LIT(KTestFile9, "test3.db");
    1.42 +_LIT(KTestFile10,"test4.db");
    1.43 +_LIT(KTestFile11,"test.db");
    1.44 +_LIT(KTestFile12,"test.db2");
    1.45 +_LIT(KTestFile13,"bak.db-journal");
    1.46 +_LIT(KTestFile14,"test.db-journal-bu");
    1.47 +_LIT(KTestFile15,"mydir");
    1.48 +_LIT(KTestFile16,"testdb");
    1.49 +_LIT(KTestFile17,"test2-script.tcl");
    1.50 +_LIT(KTestFile18,"test.tcl");
    1.51 +_LIT(KTestFile19,"speed1.txt");
    1.52 +_LIT(KTestFile20,"speed2.txt");
    1.53 +_LIT(KTestFile21,"test.db-bu1");
    1.54 +_LIT(KTestFile22,"test.db-bu2");
    1.55 +_LIT(KTestFile23,"test.db-template");
    1.56 +_LIT(KTestFile24,"invalid.db");
    1.57 +_LIT(KTestFile25,"log");
    1.58 +_LIT(KTestFile26,"test5.db");
    1.59 +
    1.60 +_LIT(KTestDir1,"mydir-journal");
    1.61 +
    1.62 +#ifdef _DEBUG 
    1.63 +
    1.64 +#define PRINT_DELFILE_ERROR(aFileName, aErr) \
    1.65 +	if(aErr != KErrNone && aErr != KErrNotFound)\
    1.66 +		{\
    1.67 +		RDebug::Print(_L("###TclSqlite3: Failed to delete file \"%S\". Error=%d\r\n"), &aFileName, aErr);\
    1.68 +		}
    1.69 +	
    1.70 +#else
    1.71 +
    1.72 +#define PRINT_DELFILE_ERROR(aFileName, aErr)	void(0)
    1.73 +	
    1.74 +#endif
    1.75 +
    1.76 +//Connects the file session argument.
    1.77 +//Creates application's private datacage on drive C: (if does not exist).
    1.78 +//Copies the private path as string to the aPrivatePath argument (without the drive name).
    1.79 +//aPrivatePath must point to a big enough place (ideally TFileName object).
    1.80 +static void GetFsAndPrivatePathL(RFs& aFs, TDes& aPrivatePath)
    1.81 +	{
    1.82 +	User::LeaveIfError(aFs.Connect());
    1.83 +	TInt err = aFs.CreatePrivatePath(EDriveC);
    1.84 +	if(!(err == KErrNone || err == KErrAlreadyExists))
    1.85 +		{
    1.86 +		User::Leave(err);
    1.87 +		}
    1.88 +
    1.89 +	User::LeaveIfError(aFs.PrivatePath(aPrivatePath));
    1.90 +	}
    1.91 +
    1.92 +//The function constructs a full script file path, containing wildcards, in aFullPath argument
    1.93 +//	(from aDriveNumber and aPrivatePath arguments, using aMask parameter as a file name).
    1.94 +//aFullPath must point to a big enough place (ideally TFileName object).
    1.95 +static void ConstructFilePathByMask(TDriveNumber aDriveNumber, const TDesC& aPrivatePath, const TDesC& aMask, TDes& aFullPath)
    1.96 +	{
    1.97 +	TDriveName srcDriveName = TDriveUnit(aDriveNumber).Name();
    1.98 +	aFullPath.Copy(srcDriveName);
    1.99 +	aFullPath.Append(aMask);
   1.100 +	TParse parse;
   1.101 +	parse.Set(aPrivatePath, &aFullPath, 0);
   1.102 +	aFullPath.Copy(parse.FullName());
   1.103 +	}
   1.104 +
   1.105 +//The function constructs a full file path in aFullPath argument (from aDriveNumber, aFileName and aPrivatePath arguments).
   1.106 +//aFullPath must point to a big enough place (ideally TFileName object).
   1.107 +static void ConstructFilePathByName(TDriveNumber aDriveNumber, const TDesC& aFileName, const TDesC& aPrivatePath, TDes& aFullPath)
   1.108 +	{
   1.109 +	TDriveName srcDriveName = TDriveUnit(aDriveNumber).Name();
   1.110 +	aFullPath.Copy(srcDriveName);
   1.111 +	aFullPath.Append(aFileName);
   1.112 +	TParse parse;
   1.113 +	parse.Set(aPrivatePath, &aFullPath, 0);
   1.114 +	aFullPath.Copy(parse.FullName());
   1.115 +	}
   1.116 +
   1.117 +//The function constructs a full path in aDestPath argument (from aDriveNumber and aPrivatePath arguments).
   1.118 +//aDestPath must point to a big enough place (ideally TFileName object).
   1.119 +static void ConstructDestPath(TDriveNumber aDriveNumber, const TDesC& aPrivatePath, TDes& aDestPath)
   1.120 +	{
   1.121 +	TDriveName destDriveName = TDriveUnit(aDriveNumber).Name();
   1.122 +	TParse parse;
   1.123 +	parse.Set(aPrivatePath, &destDriveName, 0);
   1.124 +	aDestPath.Copy(parse.FullName());
   1.125 +	}
   1.126 +
   1.127 +//The function copies all test script files from Z: to C: drive, in application's private data cage.	
   1.128 +static void DoCopyTestFilesL()
   1.129 +	{
   1.130 +	RDebug::Print(_L("###TclSqlite3: Construct private data cage on drive C:\r\n"));
   1.131 +	RFs fs;
   1.132 +	CleanupClosePushL(fs);
   1.133 +	TFileName privatePath;
   1.134 +	GetFsAndPrivatePathL(fs, privatePath);
   1.135 +
   1.136 +	CFileMan* fm = CFileMan::NewL(fs);
   1.137 +	CleanupStack::PushL(fm);
   1.138 +	
   1.139 +	TFileName srcPath, destPath;
   1.140 +	ConstructFilePathByMask(EDriveZ, privatePath, KTestExt, srcPath);
   1.141 +	ConstructDestPath(EDriveC, privatePath, destPath);
   1.142 +	RDebug::Print(_L("###TclSqlite3: Copying \"%S\" to \"%S\"\r\n"), &srcPath, &destPath);
   1.143 +	User::LeaveIfError(fm->Copy(srcPath, destPath));
   1.144 +
   1.145 +	ConstructFilePathByMask(EDriveZ, privatePath, KTclExt, srcPath);
   1.146 +	ConstructDestPath(EDriveC, privatePath, destPath);
   1.147 +	RDebug::Print(_L("###TclSqlite3: Copying \"%S\" to \"%S\"\r\n"), &srcPath, &destPath);
   1.148 +	User::LeaveIfError(fm->Copy(srcPath, destPath));
   1.149 +
   1.150 +	ConstructFilePathByMask(EDriveZ, privatePath, KExplainExt, srcPath);
   1.151 +	ConstructDestPath(EDriveC, privatePath, destPath);
   1.152 +	RDebug::Print(_L("###TclSqlite3: Copying \"%S\" to \"%S\"\r\n"), &srcPath, &destPath);
   1.153 +	User::LeaveIfError(fm->Copy(srcPath, destPath));
   1.154 +
   1.155 +	CleanupStack::PopAndDestroy(2);
   1.156 +	}
   1.157 +
   1.158 +//The function deletes a file, identified by the aFullPath argument.
   1.159 +//The function leaves if the delete operation error is different than KErrNone and KErrNotFound.
   1.160 +static void DoDeleteTestFileL(CFileMan& aFm, const TDesC& aFullPath)
   1.161 +	{
   1.162 +	TInt err = aFm.Attribs(aFullPath, 0, KEntryAttReadOnly, TTime(0));
   1.163 +	if(err == KErrNone)
   1.164 +		{
   1.165 +		err = aFm.Delete(aFullPath);
   1.166 +		}
   1.167 +	if(err != KErrNone && err != KErrNotFound)
   1.168 +		{
   1.169 +		User::Leave(err);	
   1.170 +		}
   1.171 +	}
   1.172 +
   1.173 +//The function deletes a directory, identified by the aFullPath argument.
   1.174 +//The function leaves if the delete operation error is different than KErrNone and KErrNotFound.
   1.175 +static void DoDeleteTestDirL(CFileMan& aFm, const TDesC& aFullPath)
   1.176 +	{
   1.177 +	TInt err = aFm.Attribs(aFullPath, 0, KEntryAttReadOnly, TTime(0));
   1.178 +	if(err == KErrNone)
   1.179 +		{
   1.180 +		err = aFm.RmDir(aFullPath);
   1.181 +		}
   1.182 +	if(err != KErrNone && err != KErrNotFound)
   1.183 +		{
   1.184 +		User::Leave(err);	
   1.185 +		}
   1.186 +	}
   1.187 +
   1.188 +//Deletes the test scripts and test output files from C: drive.
   1.189 +static void DoDeleteTestFilesL()
   1.190 +	{
   1.191 +	RFs fs;
   1.192 +	CleanupClosePushL(fs);
   1.193 +	TFileName privatePath;
   1.194 +	GetFsAndPrivatePathL(fs, privatePath);
   1.195 +
   1.196 +	CFileMan* fm = CFileMan::NewL(fs);
   1.197 +	CleanupStack::PushL(fm);
   1.198 +
   1.199 +	TFileName filePath;
   1.200 +	ConstructFilePathByMask(EDriveC, privatePath, KExplainExt, filePath);
   1.201 +	TRAPD(err, DoDeleteTestFileL(*fm, filePath));
   1.202 +	PRINT_DELFILE_ERROR(filePath, err);
   1.203 +
   1.204 +	ConstructFilePathByMask(EDriveC, privatePath, KTclExt, filePath);
   1.205 +	TRAP(err, DoDeleteTestFileL(*fm, filePath));
   1.206 +	PRINT_DELFILE_ERROR(filePath, err);
   1.207 +
   1.208 +	ConstructFilePathByMask(EDriveC, privatePath, KTestExt, filePath);
   1.209 +	TRAP(err, DoDeleteTestFileL(*fm, filePath));
   1.210 +	PRINT_DELFILE_ERROR(filePath, err);
   1.211 +
   1.212 +	ConstructFilePathByMask(EDriveC, privatePath, KTclTempFilePrefix, filePath);
   1.213 +	TRAP(err, DoDeleteTestFileL(*fm, filePath));
   1.214 +	PRINT_DELFILE_ERROR(filePath, err);
   1.215 +
   1.216 +	TPtrC fileNames[] = 
   1.217 +		{
   1.218 +		KTestFile1(), KTestFile2(), KTestFile3(), KTestFile4(), KTestFile5(), 
   1.219 +		KTestFile6(), KTestFile7(), KTestFile8(), KTestFile9(), KTestFile10(),
   1.220 +		KTestFile11(), KTestFile12(), KTestFile13(), KTestFile14(), KTestFile15(), 
   1.221 +		KTestFile16(), KTestFile17(), KTestFile18(), KTestFile19(), KTestFile20(),
   1.222 +		KTestFile21(), KTestFile22(), KTestFile23(), KTestFile24(), KTestFile25(), KTestFile26()
   1.223 +		};
   1.224 +	for(TInt i=0;i<(sizeof(fileNames)/sizeof(fileNames[0]));++i)
   1.225 +		{
   1.226 +		ConstructFilePathByName(EDriveC, fileNames[i], privatePath, filePath);
   1.227 +		TRAP(err, DoDeleteTestFileL(*fm, filePath));
   1.228 +		PRINT_DELFILE_ERROR(filePath, err);
   1.229 +		}
   1.230 +
   1.231 +	ConstructFilePathByName(EDriveC, KTestDir1, privatePath, filePath);
   1.232 +	RDebug::Print(_L("###TclSqlite3: test dir to be removed - \"%S\".\r\n"), &filePath);
   1.233 +	TRAP(err, DoDeleteTestDirL(*fm, filePath));
   1.234 +	PRINT_DELFILE_ERROR(filePath, err);
   1.235 +
   1.236 +	CleanupStack::PopAndDestroy(2);
   1.237 +	}
   1.238 +	
   1.239 +//Deletes the test scripts from C: drive
   1.240 +//Because the TCL SQLITE exe has mutiple exit points, there is no right place in the code where this function
   1.241 +//can be called from.
   1.242 +//The way how the test cleanup is implemented is:
   1.243 +// - a new script function has been definied and registered (tclsqlite.c)- "delete_test_files"
   1.244 +// - the "delete_test_files" function is called from the "finalize_testing" procedure (tester.tcl file),
   1.245 +//	   that is guaranteed to be called at the end of any test script execution
   1.246 +extern "C" int DeleteTestFiles(void)
   1.247 +	{
   1.248 +	RDebug::Print(_L("###TclSqlite3: Begin \"Delete test files\" operation\r\n"));
   1.249 +	TRAP_IGNORE(DoDeleteTestFilesL());
   1.250 +	RDebug::Print(_L("###TclSqlite3: \"Delete test files\" operation has completed\r\n"));
   1.251 +	return 0;
   1.252 +	}
   1.253 +
   1.254 +//Copies the test scripts from Z: to C: drive	
   1.255 +//This function is called from main() (tclsqlite.c file)
   1.256 +extern "C" TInt CopyTestFiles(void)
   1.257 +	{
   1.258 +    RDebug::Print(_L("###TclSqlite3: Begin \"Copy test files\" operation\r\n"));
   1.259 +	TRAPD(err, DoCopyTestFilesL());
   1.260 +	if(err != KErrNone)
   1.261 +		{
   1.262 +		RDebug::Print(_L("###TclSqlite3: \"Copy test files\" operation has failed with error %d\r\n"), err);
   1.263 +		DeleteTestFiles();
   1.264 +		}
   1.265 +	else
   1.266 +		{
   1.267 +		RDebug::Print(_L("###TclSqlite3: \"Copy test files\" operation has completed successfully\r\n"));
   1.268 +		}
   1.269 +	return err;
   1.270 +	}
   1.271 +
   1.272 +//Used by GetFullFilePath() in test_hexio.c
   1.273 +//Seems that the OpenEnv library does not provide _splitpath().
   1.274 +extern "C" char* FullFilePath(char* aPath, const char* aFileName)
   1.275 +	{
   1.276 +	static TFileName fname;
   1.277 +	fname.Copy(TPtrC8((const TUint8*)aFileName));
   1.278 +	fname.Trim();
   1.279 +	for(TInt i=0;i<fname.Length();++i)
   1.280 +		{
   1.281 +		if(fname[i] == TChar('/'))	
   1.282 +			{
   1.283 +			fname[i] = TChar('\\');
   1.284 +			}
   1.285 +		}
   1.286 +	if(fname.Find(_L(".\\")) == 0) //TParsePtrC::TParsePtrC() panics if the first two characters are ".\"
   1.287 +		{
   1.288 +		fname.Delete(0, 2);
   1.289 +		}
   1.290 +	TParsePtrC parse(fname);
   1.291 +	if(!parse.DrivePresent() || !parse.PathPresent())
   1.292 +		{
   1.293 +		if(!getcwd(aPath, MAXPATHLEN + 1)) 
   1.294 +			{
   1.295 +			return 0;	
   1.296 +			}
   1.297 +		aPath[0] = 'c';//a temporary patch. The defect number is: DEF116621.
   1.298 +		strcat(aPath, "\\");
   1.299 +		static TBuf8<KMaxFileName> fname2;
   1.300 +		fname2.Copy(parse.NameAndExt());
   1.301 +		fname2.Append(TChar('\x0'));
   1.302 +		strcat(aPath, (const char*)fname2.Ptr());
   1.303 +		}
   1.304 +	else
   1.305 +		{
   1.306 +		strcpy(aPath, aFileName); 
   1.307 +		}
   1.308 +	return aPath;
   1.309 +	}
   1.310 +
   1.311 +extern "C" int PrintText(void*, Tcl_Interp*, int objc, Tcl_Obj* const* objv)
   1.312 +	{
   1.313 +	if(objc == 3)
   1.314 +		{
   1.315 +	    const char* txt1 = Tcl_GetStringFromObj(objv[1], 0);
   1.316 +	    const char* txt2 = Tcl_GetStringFromObj(objv[2], 0);
   1.317 +	    if(txt1 && txt2)
   1.318 +	    	{
   1.319 +			TTime time;
   1.320 +			time.HomeTime();
   1.321 +			TDateTime dt = time.DateTime();
   1.322 +			TBuf<16> tbuf;
   1.323 +			tbuf.Format(_L("%02d:%02d:%02d.%06d"), dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond());
   1.324 +	    	
   1.325 +	    	TBuf<256> buf1;
   1.326 +	    	buf1.Copy(TPtrC8((const TUint8*)txt1));
   1.327 +	    	
   1.328 +	    	TBuf<256> buf2;
   1.329 +	    	buf2.Copy(TPtrC8((const TUint8*)txt2));
   1.330 +	    	
   1.331 +			RDebug::Print(_L("%S: %S %S.\n"), &tbuf, &buf1, &buf2);
   1.332 +		    return TCL_OK;
   1.333 +	    	}
   1.334 +		}
   1.335 +    return TCL_ERROR;
   1.336 +	}
   1.337 +
   1.338 +extern "C" void PrintS(const char* aTxt)
   1.339 +	{
   1.340 +	if(!aTxt)
   1.341 +	    {
   1.342 +	    return;
   1.343 +	    }
   1.344 +	TPtrC8 msg((const TUint8*)aTxt);
   1.345 +    TInt msglen = msg.Length();
   1.346 +    TInt pos = 0;
   1.347 +    const TInt KMaxLineLength = 220;
   1.348 +    TBuf<KMaxLineLength> line;
   1.349 +    do
   1.350 +        {
   1.351 +        if(pos == 0)
   1.352 +            {
   1.353 +            RProcess process;
   1.354 +            TProcessId processId = process.Id();
   1.355 +            line.Format(_L("Process Id=%ld: "), processId.Id());
   1.356 +            }
   1.357 +        TInt len = Min(msglen, (line.MaxLength() - line.Length()));
   1.358 +        TPtrC8 ptr(msg.Ptr() + pos, len);
   1.359 +        pos += len;
   1.360 +        msglen -= len;
   1.361 +        TPtr p2((TUint16*)line.Ptr() + line.Length(), 0, len);  
   1.362 +        p2.Copy(ptr);
   1.363 +        line.SetLength(line.Length() + p2.Length());
   1.364 +        RDebug::Print(_L("%S\n"), &line);
   1.365 +        line.Zero();
   1.366 +        } while(msglen > 0);
   1.367 +	}