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