persistentstorage/sqlite3api/OsLayer/test_fileutil.cpp
changeset 0 08ec8eefde2f
child 13 211563e4b919
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2007-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 "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 //
       
    15 
       
    16 #include <f32file.h>
       
    17 #include <sys/param.h>
       
    18 #include <stdlib.h>
       
    19 #include <string.h>
       
    20 #include <unistd.h>
       
    21 #include "tcl.h"
       
    22 
       
    23 _LIT(KTestExt, "*.test");
       
    24 _LIT(KTclExt, "*.tcl");
       
    25 _LIT(KExplainExt, "*.explain");
       
    26 _LIT(KTclTempFilePrefix, "tcl*");
       
    27 
       
    28 //Note: every time when you add a new KTestFileXX constant here, don't forget to add that constant as a new entry into
       
    29 //"TPtrC fileNames[]" array in DoDeleteTestFilesL() function.
       
    30 _LIT(KTestFile1, "test1.bt");
       
    31 _LIT(KTestFile2, "test.bu");
       
    32 _LIT(KTestFile3, "backup.db");
       
    33 _LIT(KTestFile4, "bak.db");
       
    34 _LIT(KTestFile5, "corrupt.db");
       
    35 _LIT(KTestFile6, "ptf2.db");
       
    36 _LIT(KTestFile7, "test1.db");
       
    37 _LIT(KTestFile8, "test2.db");
       
    38 _LIT(KTestFile9, "test3.db");
       
    39 _LIT(KTestFile10,"test4.db");
       
    40 _LIT(KTestFile11,"test.db");
       
    41 _LIT(KTestFile12,"test.db2");
       
    42 _LIT(KTestFile13,"bak.db-journal");
       
    43 _LIT(KTestFile14,"test.db-journal-bu");
       
    44 _LIT(KTestFile15,"mydir");
       
    45 _LIT(KTestFile16,"testdb");
       
    46 _LIT(KTestFile17,"test2-script.tcl");
       
    47 _LIT(KTestFile18,"test.tcl");
       
    48 _LIT(KTestFile19,"speed1.txt");
       
    49 _LIT(KTestFile20,"speed2.txt");
       
    50 _LIT(KTestFile21,"test.db-bu1");
       
    51 _LIT(KTestFile22,"test.db-bu2");
       
    52 _LIT(KTestFile23,"test.db-template");
       
    53 _LIT(KTestFile24,"invalid.db");
       
    54 _LIT(KTestFile25,"log");
       
    55 _LIT(KTestFile26,"test5.db");
       
    56 
       
    57 _LIT(KTestDir1,"mydir-journal");
       
    58 
       
    59 #ifdef _DEBUG 
       
    60 
       
    61 #define PRINT_DELFILE_ERROR(aFileName, aErr) \
       
    62 	if(aErr != KErrNone && aErr != KErrNotFound)\
       
    63 		{\
       
    64 		RDebug::Print(_L("###TclSqlite3: Failed to delete file \"%S\". Error=%d\r\n"), &aFileName, aErr);\
       
    65 		}
       
    66 	
       
    67 #else
       
    68 
       
    69 #define PRINT_DELFILE_ERROR(aFileName, aErr)	void(0)
       
    70 	
       
    71 #endif
       
    72 
       
    73 //Connects the file session argument.
       
    74 //Creates application's private datacage on drive C: (if does not exist).
       
    75 //Copies the private path as string to the aPrivatePath argument (without the drive name).
       
    76 //aPrivatePath must point to a big enough place (ideally TFileName object).
       
    77 static void GetFsAndPrivatePathL(RFs& aFs, TDes& aPrivatePath)
       
    78 	{
       
    79 	User::LeaveIfError(aFs.Connect());
       
    80 	TInt err = aFs.CreatePrivatePath(EDriveC);
       
    81 	if(!(err == KErrNone || err == KErrAlreadyExists))
       
    82 		{
       
    83 		User::Leave(err);
       
    84 		}
       
    85 
       
    86 	User::LeaveIfError(aFs.PrivatePath(aPrivatePath));
       
    87 	}
       
    88 
       
    89 //The function constructs a full script file path, containing wildcards, in aFullPath argument
       
    90 //	(from aDriveNumber and aPrivatePath arguments, using aMask parameter as a file name).
       
    91 //aFullPath must point to a big enough place (ideally TFileName object).
       
    92 static void ConstructFilePathByMask(TDriveNumber aDriveNumber, const TDesC& aPrivatePath, const TDesC& aMask, TDes& aFullPath)
       
    93 	{
       
    94 	TDriveName srcDriveName = TDriveUnit(aDriveNumber).Name();
       
    95 	aFullPath.Copy(srcDriveName);
       
    96 	aFullPath.Append(aMask);
       
    97 	TParse parse;
       
    98 	parse.Set(aPrivatePath, &aFullPath, 0);
       
    99 	aFullPath.Copy(parse.FullName());
       
   100 	}
       
   101 
       
   102 //The function constructs a full file path in aFullPath argument (from aDriveNumber, aFileName and aPrivatePath arguments).
       
   103 //aFullPath must point to a big enough place (ideally TFileName object).
       
   104 static void ConstructFilePathByName(TDriveNumber aDriveNumber, const TDesC& aFileName, const TDesC& aPrivatePath, TDes& aFullPath)
       
   105 	{
       
   106 	TDriveName srcDriveName = TDriveUnit(aDriveNumber).Name();
       
   107 	aFullPath.Copy(srcDriveName);
       
   108 	aFullPath.Append(aFileName);
       
   109 	TParse parse;
       
   110 	parse.Set(aPrivatePath, &aFullPath, 0);
       
   111 	aFullPath.Copy(parse.FullName());
       
   112 	}
       
   113 
       
   114 //The function constructs a full path in aDestPath argument (from aDriveNumber and aPrivatePath arguments).
       
   115 //aDestPath must point to a big enough place (ideally TFileName object).
       
   116 static void ConstructDestPath(TDriveNumber aDriveNumber, const TDesC& aPrivatePath, TDes& aDestPath)
       
   117 	{
       
   118 	TDriveName destDriveName = TDriveUnit(aDriveNumber).Name();
       
   119 	TParse parse;
       
   120 	parse.Set(aPrivatePath, &destDriveName, 0);
       
   121 	aDestPath.Copy(parse.FullName());
       
   122 	}
       
   123 
       
   124 //The function copies all test script files from Z: to C: drive, in application's private data cage.	
       
   125 static void DoCopyTestFilesL()
       
   126 	{
       
   127 	RDebug::Print(_L("###TclSqlite3: Construct private data cage on drive C:\r\n"));
       
   128 	RFs fs;
       
   129 	CleanupClosePushL(fs);
       
   130 	TFileName privatePath;
       
   131 	GetFsAndPrivatePathL(fs, privatePath);
       
   132 
       
   133 	CFileMan* fm = CFileMan::NewL(fs);
       
   134 	CleanupStack::PushL(fm);
       
   135 	
       
   136 	TFileName srcPath, destPath;
       
   137 	ConstructFilePathByMask(EDriveZ, privatePath, KTestExt, srcPath);
       
   138 	ConstructDestPath(EDriveC, privatePath, destPath);
       
   139 	RDebug::Print(_L("###TclSqlite3: Copying \"%S\" to \"%S\"\r\n"), &srcPath, &destPath);
       
   140 	User::LeaveIfError(fm->Copy(srcPath, destPath));
       
   141 
       
   142 	ConstructFilePathByMask(EDriveZ, privatePath, KTclExt, srcPath);
       
   143 	ConstructDestPath(EDriveC, privatePath, destPath);
       
   144 	RDebug::Print(_L("###TclSqlite3: Copying \"%S\" to \"%S\"\r\n"), &srcPath, &destPath);
       
   145 	User::LeaveIfError(fm->Copy(srcPath, destPath));
       
   146 
       
   147 	ConstructFilePathByMask(EDriveZ, privatePath, KExplainExt, srcPath);
       
   148 	ConstructDestPath(EDriveC, privatePath, destPath);
       
   149 	RDebug::Print(_L("###TclSqlite3: Copying \"%S\" to \"%S\"\r\n"), &srcPath, &destPath);
       
   150 	User::LeaveIfError(fm->Copy(srcPath, destPath));
       
   151 
       
   152 	CleanupStack::PopAndDestroy(2);
       
   153 	}
       
   154 
       
   155 //The function deletes a file, identified by the aFullPath argument.
       
   156 //The function leaves if the delete operation error is different than KErrNone and KErrNotFound.
       
   157 static void DoDeleteTestFileL(CFileMan& aFm, const TDesC& aFullPath)
       
   158 	{
       
   159 	TInt err = aFm.Attribs(aFullPath, 0, KEntryAttReadOnly, TTime(0));
       
   160 	if(err == KErrNone)
       
   161 		{
       
   162 		err = aFm.Delete(aFullPath);
       
   163 		}
       
   164 	if(err != KErrNone && err != KErrNotFound)
       
   165 		{
       
   166 		User::Leave(err);	
       
   167 		}
       
   168 	}
       
   169 
       
   170 //The function deletes a directory, identified by the aFullPath argument.
       
   171 //The function leaves if the delete operation error is different than KErrNone and KErrNotFound.
       
   172 static void DoDeleteTestDirL(CFileMan& aFm, const TDesC& aFullPath)
       
   173 	{
       
   174 	TInt err = aFm.Attribs(aFullPath, 0, KEntryAttReadOnly, TTime(0));
       
   175 	if(err == KErrNone)
       
   176 		{
       
   177 		err = aFm.RmDir(aFullPath);
       
   178 		}
       
   179 	if(err != KErrNone && err != KErrNotFound)
       
   180 		{
       
   181 		User::Leave(err);	
       
   182 		}
       
   183 	}
       
   184 
       
   185 //Deletes the test scripts and test output files from C: drive.
       
   186 static void DoDeleteTestFilesL()
       
   187 	{
       
   188 	RFs fs;
       
   189 	CleanupClosePushL(fs);
       
   190 	TFileName privatePath;
       
   191 	GetFsAndPrivatePathL(fs, privatePath);
       
   192 
       
   193 	CFileMan* fm = CFileMan::NewL(fs);
       
   194 	CleanupStack::PushL(fm);
       
   195 
       
   196 	TFileName filePath;
       
   197 	ConstructFilePathByMask(EDriveC, privatePath, KExplainExt, filePath);
       
   198 	TRAPD(err, DoDeleteTestFileL(*fm, filePath));
       
   199 	PRINT_DELFILE_ERROR(filePath, err);
       
   200 
       
   201 	ConstructFilePathByMask(EDriveC, privatePath, KTclExt, filePath);
       
   202 	TRAP(err, DoDeleteTestFileL(*fm, filePath));
       
   203 	PRINT_DELFILE_ERROR(filePath, err);
       
   204 
       
   205 	ConstructFilePathByMask(EDriveC, privatePath, KTestExt, filePath);
       
   206 	TRAP(err, DoDeleteTestFileL(*fm, filePath));
       
   207 	PRINT_DELFILE_ERROR(filePath, err);
       
   208 
       
   209 	ConstructFilePathByMask(EDriveC, privatePath, KTclTempFilePrefix, filePath);
       
   210 	TRAP(err, DoDeleteTestFileL(*fm, filePath));
       
   211 	PRINT_DELFILE_ERROR(filePath, err);
       
   212 
       
   213 	TPtrC fileNames[] = 
       
   214 		{
       
   215 		KTestFile1(), KTestFile2(), KTestFile3(), KTestFile4(), KTestFile5(), 
       
   216 		KTestFile6(), KTestFile7(), KTestFile8(), KTestFile9(), KTestFile10(),
       
   217 		KTestFile11(), KTestFile12(), KTestFile13(), KTestFile14(), KTestFile15(), 
       
   218 		KTestFile16(), KTestFile17(), KTestFile18(), KTestFile19(), KTestFile20(),
       
   219 		KTestFile21(), KTestFile22(), KTestFile23(), KTestFile24(), KTestFile25(), KTestFile26()
       
   220 		};
       
   221 	for(TInt i=0;i<(sizeof(fileNames)/sizeof(fileNames[0]));++i)
       
   222 		{
       
   223 		ConstructFilePathByName(EDriveC, fileNames[i], privatePath, filePath);
       
   224 		TRAP(err, DoDeleteTestFileL(*fm, filePath));
       
   225 		PRINT_DELFILE_ERROR(filePath, err);
       
   226 		}
       
   227 
       
   228 	ConstructFilePathByName(EDriveC, KTestDir1, privatePath, filePath);
       
   229 	RDebug::Print(_L("###TclSqlite3: test dir to be removed - \"%S\".\r\n"), &filePath);
       
   230 	TRAP(err, DoDeleteTestDirL(*fm, filePath));
       
   231 	PRINT_DELFILE_ERROR(filePath, err);
       
   232 
       
   233 	CleanupStack::PopAndDestroy(2);
       
   234 	}
       
   235 	
       
   236 //Deletes the test scripts from C: drive
       
   237 //Because the TCL SQLITE exe has mutiple exit points, there is no right place in the code where this function
       
   238 //can be called from.
       
   239 //The way how the test cleanup is implemented is:
       
   240 // - a new script function has been definied and registered (tclsqlite.c)- "delete_test_files"
       
   241 // - the "delete_test_files" function is called from the "finalize_testing" procedure (tester.tcl file),
       
   242 //	   that is guaranteed to be called at the end of any test script execution
       
   243 extern "C" int DeleteTestFiles(void)
       
   244 	{
       
   245 	RDebug::Print(_L("###TclSqlite3: Begin \"Delete test files\" operation\r\n"));
       
   246 	TRAP_IGNORE(DoDeleteTestFilesL());
       
   247 	RDebug::Print(_L("###TclSqlite3: \"Delete test files\" operation has completed\r\n"));
       
   248 	return 0;
       
   249 	}
       
   250 
       
   251 //Copies the test scripts from Z: to C: drive	
       
   252 //This function is called from main() (tclsqlite.c file)
       
   253 extern "C" TInt CopyTestFiles(void)
       
   254 	{
       
   255     RDebug::Print(_L("###TclSqlite3: Begin \"Copy test files\" operation\r\n"));
       
   256 	TRAPD(err, DoCopyTestFilesL());
       
   257 	if(err != KErrNone)
       
   258 		{
       
   259 		RDebug::Print(_L("###TclSqlite3: \"Copy test files\" operation has failed with error %d\r\n"), err);
       
   260 		DeleteTestFiles();
       
   261 		}
       
   262 	else
       
   263 		{
       
   264 		RDebug::Print(_L("###TclSqlite3: \"Copy test files\" operation has completed successfully\r\n"));
       
   265 		}
       
   266 	return err;
       
   267 	}
       
   268 
       
   269 //Used by GetFullFilePath() in test_hexio.c
       
   270 //Seems that the OpenEnv library does not provide _splitpath().
       
   271 extern "C" char* FullFilePath(char* aPath, const char* aFileName)
       
   272 	{
       
   273 	static TFileName fname;
       
   274 	fname.Copy(TPtrC8((const TUint8*)aFileName));
       
   275 	fname.Trim();
       
   276 	for(TInt i=0;i<fname.Length();++i)
       
   277 		{
       
   278 		if(fname[i] == TChar('/'))	
       
   279 			{
       
   280 			fname[i] = TChar('\\');
       
   281 			}
       
   282 		}
       
   283 	if(fname.Find(_L(".\\")) == 0) //TParsePtrC::TParsePtrC() panics if the first two characters are ".\"
       
   284 		{
       
   285 		fname.Delete(0, 2);
       
   286 		}
       
   287 	TParsePtrC parse(fname);
       
   288 	if(!parse.DrivePresent() || !parse.PathPresent())
       
   289 		{
       
   290 		if(!getcwd(aPath, MAXPATHLEN + 1)) 
       
   291 			{
       
   292 			return 0;	
       
   293 			}
       
   294 		aPath[0] = 'c';//a temporary patch. The defect number is: DEF116621.
       
   295 		strcat(aPath, "\\");
       
   296 		static TBuf8<KMaxFileName> fname2;
       
   297 		fname2.Copy(parse.NameAndExt());
       
   298 		fname2.Append(TChar('\x0'));
       
   299 		strcat(aPath, (const char*)fname2.Ptr());
       
   300 		}
       
   301 	else
       
   302 		{
       
   303 		strcpy(aPath, aFileName); 
       
   304 		}
       
   305 	return aPath;
       
   306 	}
       
   307 
       
   308 extern "C" int PrintText(void*, Tcl_Interp*, int objc, Tcl_Obj* const* objv)
       
   309 	{
       
   310 	if(objc == 3)
       
   311 		{
       
   312 	    const char* txt1 = Tcl_GetStringFromObj(objv[1], 0);
       
   313 	    const char* txt2 = Tcl_GetStringFromObj(objv[2], 0);
       
   314 	    if(txt1 && txt2)
       
   315 	    	{
       
   316 			TTime time;
       
   317 			time.HomeTime();
       
   318 			TDateTime dt = time.DateTime();
       
   319 			TBuf<16> tbuf;
       
   320 			tbuf.Format(_L("%02d:%02d:%02d.%06d"), dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond());
       
   321 	    	
       
   322 	    	TBuf<256> buf1;
       
   323 	    	buf1.Copy(TPtrC8((const TUint8*)txt1));
       
   324 	    	
       
   325 	    	TBuf<256> buf2;
       
   326 	    	buf2.Copy(TPtrC8((const TUint8*)txt2));
       
   327 	    	
       
   328 			RDebug::Print(_L("%S: %S %S.\n"), &tbuf, &buf1, &buf2);
       
   329 		    return TCL_OK;
       
   330 	    	}
       
   331 		}
       
   332     return TCL_ERROR;
       
   333 	}
       
   334 
       
   335 extern "C" void PrintS(const char* aTxt)
       
   336 	{
       
   337 	TBuf<128> buf;
       
   338 	buf.Copy(TPtrC8((const TUint8*)aTxt));
       
   339 	
       
   340 	RProcess process;
       
   341 	TProcessId processId = process.Id();
       
   342 	
       
   343 	RDebug::Print(_L("%S. Process Id=%ld.\n"), &buf, processId.Id());
       
   344 	}