os/kernelhwsrv/kerneltest/f32test/fileutils/src/t_chlffs.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1998-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 the License "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
// f32test\server\t_chlffs.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#define __E32TEST_EXTENSION__
sl@0
    19
#include <f32file.h>
sl@0
    20
#include <e32test.h>
sl@0
    21
#include <e32hal.h>
sl@0
    22
#include <hal.h>
sl@0
    23
sl@0
    24
// Now non-t_main programs need to chk if they are running on lffs,
sl@0
    25
#include "t_server.h"
sl@0
    26
#include "t_chlffs.h"
sl@0
    27
sl@0
    28
sl@0
    29
const TInt KInvalidDriveLetter=-1;
sl@0
    30
sl@0
    31
LOCAL_D TInt LFFSdriveNumber=KInvalidDriveLetter;
sl@0
    32
LOCAL_D TBool LFFSTesting=EFalse;
sl@0
    33
_LIT(KLFFSName,"Lffs");
sl@0
    34
sl@0
    35
sl@0
    36
LOCAL_C void FormatLFFS(RFs &anFsSession,TDes &aDrive)
sl@0
    37
//
sl@0
    38
// Format the LFFS drive
sl@0
    39
//
sl@0
    40
	{
sl@0
    41
    RFormat format;
sl@0
    42
    TInt count;
sl@0
    43
    TInt r;
sl@0
    44
    
sl@0
    45
    test.Printf(_L("Format LFFS drive %S\r\n"), &aDrive);
sl@0
    46
    r=format.Open(anFsSession,aDrive,EHighDensity,count);
sl@0
    47
    test.Printf(_L("Format open done. Count = %d\r\n"), count);
sl@0
    48
    test(r==KErrNone);
sl@0
    49
    
sl@0
    50
    while (count)
sl@0
    51
		{
sl@0
    52
        TInt r=format.Next(count);
sl@0
    53
    	test.Printf(_L("Format next done. Count = %d\r\n"), count);
sl@0
    54
        test(r==KErrNone);
sl@0
    55
    	}
sl@0
    56
    
sl@0
    57
    format.Close();
sl@0
    58
	}
sl@0
    59
sl@0
    60
GLDEF_C TBool IsFileSystemLFFS(RFs &aFsSession,TInt aDrive)
sl@0
    61
//
sl@0
    62
// return true if lffs on aDrive
sl@0
    63
//
sl@0
    64
	{
sl@0
    65
	TFileName f;
sl@0
    66
	TInt r=aFsSession.FileSystemName(f,aDrive);
sl@0
    67
	test(r==KErrNone || r==KErrNotFound);
sl@0
    68
	return (f.CompareF(KLFFSName)==0);
sl@0
    69
	}
sl@0
    70
sl@0
    71
GLDEF_C TInt CheckLFFSDriveForPlatform()
sl@0
    72
//
sl@0
    73
// Check the LFFS drive number for the current platform
sl@0
    74
//
sl@0
    75
	{
sl@0
    76
	TInt lffsDrvNum;
sl@0
    77
sl@0
    78
	TInt uid;
sl@0
    79
    TInt r=HAL::Get(HAL::EMachineUid,uid);
sl@0
    80
    test(r==KErrNone);
sl@0
    81
    
sl@0
    82
    if (uid==HAL::EMachineUid_Brutus)
sl@0
    83
		{
sl@0
    84
        lffsDrvNum=EDriveK;
sl@0
    85
    	test.Printf(_L("Test is running on BRUTUS\r\n"));
sl@0
    86
		}
sl@0
    87
    else if (uid==HAL::EMachineUid_Win32Emulator)
sl@0
    88
		{
sl@0
    89
        lffsDrvNum=EDriveW;
sl@0
    90
    	test.Printf(_L("Test is running on WINS Pc\r\n"));
sl@0
    91
		}
sl@0
    92
	else if (uid == HAL::EMachineUid_Integrator)
sl@0
    93
		{
sl@0
    94
		lffsDrvNum=EDriveK;
sl@0
    95
		test.Printf(_L("Test is running on INTEGRATOR\r\n"));
sl@0
    96
		}
sl@0
    97
	else if (uid == HAL::EMachineUid_Assabet)
sl@0
    98
		{
sl@0
    99
		lffsDrvNum=EDriveK;
sl@0
   100
		test.Printf(_L("Test is running on Assabet\r\n"));
sl@0
   101
		}
sl@0
   102
    else
sl@0
   103
		lffsDrvNum=KInvalidDriveLetter;
sl@0
   104
	return(lffsDrvNum);
sl@0
   105
	}
sl@0
   106
sl@0
   107
GLDEF_C TBool CheckMountLFFS(RFs &anFsSession,TChar aDriveLetter)
sl@0
   108
//
sl@0
   109
// Check if test to be performed on LFFS drive. Mount the ELFFS.FSY if
sl@0
   110
// necessary
sl@0
   111
//
sl@0
   112
	{
sl@0
   113
sl@0
   114
    test.Next(_L("Check if LFFS drive (Mount LFFS if required)"));
sl@0
   115
sl@0
   116
	TInt r,drvNum;
sl@0
   117
	TBuf<4> lffsDriveLetter=_L("?:\\");
sl@0
   118
sl@0
   119
	// first check if the lffs is mounted on the drive
sl@0
   120
	r=anFsSession.CharToDrive(aDriveLetter,drvNum);
sl@0
   121
	test(r==KErrNone);
sl@0
   122
	if (IsFileSystemLFFS(anFsSession,drvNum))
sl@0
   123
		{
sl@0
   124
		lffsDriveLetter[0]=(TText)aDriveLetter;
sl@0
   125
		test.Printf(_L("Testing an LFFS drive (%S)"), &lffsDriveLetter);
sl@0
   126
		test.Printf(_L("LFFS already mounted on drive %S\r\n"), &lffsDriveLetter);
sl@0
   127
		LFFSdriveNumber=drvNum;
sl@0
   128
		LFFSTesting=ETrue;
sl@0
   129
		return(ETrue);
sl@0
   130
		}
sl@0
   131
sl@0
   132
	// check if platform expects lffs to be mounted on specified drive 
sl@0
   133
	TInt lffsDrvNum=CheckLFFSDriveForPlatform();
sl@0
   134
	if (drvNum!=lffsDrvNum)
sl@0
   135
		{
sl@0
   136
        test.Printf(_L("Not testing an LFFS drive\n"));
sl@0
   137
        return(EFalse);
sl@0
   138
    	}
sl@0
   139
sl@0
   140
	lffsDriveLetter[0]=(TText)aDriveLetter;
sl@0
   141
    test.Printf(_L("Testing an LFFS drive (%S)"), &lffsDriveLetter);
sl@0
   142
	LFFSdriveNumber=lffsDrvNum;
sl@0
   143
	LFFSTesting=ETrue;
sl@0
   144
sl@0
   145
    test.Next(_L("Load device driver: MEDLFS"));
sl@0
   146
    r=User::LoadPhysicalDevice(_L("MEDLFS"));
sl@0
   147
    test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   148
    
sl@0
   149
    test.Next(_L("Add file system: ELFFS"));
sl@0
   150
    r=anFsSession.AddFileSystem(_L("ELFFS"));
sl@0
   151
    test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   152
    
sl@0
   153
    TFullName name;
sl@0
   154
    r=anFsSession.FileSystemName(name,LFFSdriveNumber);
sl@0
   155
    test(r==KErrNone || r==KErrNotFound);
sl@0
   156
sl@0
   157
    if (name.MatchF(_L("Lffs")) != 0)
sl@0
   158
		{
sl@0
   159
        // Some other file system is at the "Lffs" drive. 
sl@0
   160
        if (name.Length() != 0)
sl@0
   161
			{
sl@0
   162
			// Not allowed to dismount the file system from the drive associated
sl@0
   163
			// with the default path - so temporarily change the default path.
sl@0
   164
            test.Printf(_L("Dismounting %S on drive %S\r\n"), &name, &lffsDriveLetter);
sl@0
   165
            r=anFsSession.DismountFileSystem(name,LFFSdriveNumber);
sl@0
   166
            test(r==KErrNone);
sl@0
   167
sl@0
   168
			}
sl@0
   169
    
sl@0
   170
        test.Printf(_L("Mount LFFS on drive %S\r\n"),&lffsDriveLetter);
sl@0
   171
        r=anFsSession.MountFileSystem(_L("Lffs"), LFFSdriveNumber);
sl@0
   172
        test.Printf(_L("  Mount result %d\r\n"), r);
sl@0
   173
        test(r==KErrNone || r==KErrCorrupt || r==KErrNotReady);
sl@0
   174
    
sl@0
   175
        if (r==KErrCorrupt)
sl@0
   176
			{
sl@0
   177
            test.Printf(_L("The volume was corrupt. Formatting...\r\n"));
sl@0
   178
            FormatLFFS(anFsSession,lffsDriveLetter);
sl@0
   179
        	}
sl@0
   180
        else if(r==KErrNotReady)
sl@0
   181
			{
sl@0
   182
            test.Printf(_L("The mount was not ready. Formatting...\r\n"));
sl@0
   183
            FormatLFFS(anFsSession,lffsDriveLetter);
sl@0
   184
        	}
sl@0
   185
        else
sl@0
   186
			{
sl@0
   187
            test.Printf(_L("The volume was mounted OK. Formatting...\r\n"));
sl@0
   188
            FormatLFFS(anFsSession,lffsDriveLetter); // ???
sl@0
   189
        	}
sl@0
   190
    	}
sl@0
   191
    else
sl@0
   192
		{
sl@0
   193
        test.Printf(_L("LFFS already mounted on drive %S\r\n"), &lffsDriveLetter);
sl@0
   194
    	}
sl@0
   195
    
sl@0
   196
    return(ETrue);
sl@0
   197
	}
sl@0
   198
sl@0
   199
GLDEF_C TBool IsTestingLFFS()
sl@0
   200
//
sl@0
   201
// Return ETrue if testing LFFS
sl@0
   202
//
sl@0
   203
	{
sl@0
   204
    return(LFFSTesting);
sl@0
   205
	}
sl@0
   206
sl@0
   207
GLDEF_C void TestingLFFS(TBool aSetting)
sl@0
   208
//
sl@0
   209
// Set whether testing LFFS or not
sl@0
   210
//
sl@0
   211
	{
sl@0
   212
    LFFSTesting=aSetting;
sl@0
   213
	}
sl@0
   214
sl@0
   215
GLDEF_C TInt GetDriveLFFS()
sl@0
   216
//
sl@0
   217
// Return the LFFS drive number
sl@0
   218
//
sl@0
   219
	{
sl@0
   220
    return(LFFSdriveNumber);
sl@0
   221
	}
sl@0
   222
sl@0
   223
GLDEF_C TBool IsSessionDriveLFFS(RFs& aFs,TChar& aDriveLetter)
sl@0
   224
	{
sl@0
   225
//
sl@0
   226
// Quick method of testing if session drive is LFFS
sl@0
   227
//	
sl@0
   228
	TBool isLffs;
sl@0
   229
	TFileName path;
sl@0
   230
	TInt r=aFs.SessionPath(path);
sl@0
   231
	test(r==KErrNone);
sl@0
   232
	TInt drv;
sl@0
   233
	r=RFs::CharToDrive(path[0],drv);
sl@0
   234
	test(r==KErrNone);
sl@0
   235
sl@0
   236
	aDriveLetter=path[0];
sl@0
   237
	isLffs=IsFileSystemLFFS(aFs,drv);
sl@0
   238
	if(isLffs)
sl@0
   239
		return(ETrue);
sl@0
   240
sl@0
   241
	// check if platform expects lffs to be mounted on default drive
sl@0
   242
	TInt lffsDrv = CheckLFFSDriveForPlatform();
sl@0
   243
	if (lffsDrv == KInvalidDriveLetter)
sl@0
   244
		{
sl@0
   245
		test.Printf(_L("IsSessionDriveLFFS: platform does not support lffs.\r\n"));
sl@0
   246
		isLffs = EFalse;
sl@0
   247
		}
sl@0
   248
	else
sl@0
   249
		{
sl@0
   250
		TChar curCh=path[0];
sl@0
   251
		curCh.LowerCase();
sl@0
   252
sl@0
   253
		TChar lffsCh;								// lffs drv ltr
sl@0
   254
		test((r = RFs::DriveToChar(lffsDrv, lffsCh)) == KErrNone);
sl@0
   255
		lffsCh.LowerCase();
sl@0
   256
		
sl@0
   257
		test.Printf(_L("IsSessionDriveLFFS: cur drv = \'%c\', lffs drv = \'%c\'.\n"), (TText) curCh, (TText) lffsCh);
sl@0
   258
		isLffs = ((TText) curCh) == ((TText) lffsCh);
sl@0
   259
		}
sl@0
   260
sl@0
   261
	return(isLffs);
sl@0
   262
	}
sl@0
   263
sl@0
   264
GLDEF_C TBool IsDefaultDriveLFFS()
sl@0
   265
//
sl@0
   266
// Quick method of testing if running on LFFS for non t_main based tests.
sl@0
   267
// 
sl@0
   268
	{
sl@0
   269
	// check if lffs mounted on default drive
sl@0
   270
	TBool isLffs;
sl@0
   271
	RFs fs;
sl@0
   272
	TInt r=fs.Connect();
sl@0
   273
	test(r==KErrNone);
sl@0
   274
	TFileName path;
sl@0
   275
	r=fs.SessionPath(path);
sl@0
   276
	test(r==KErrNone);
sl@0
   277
	TInt drv;
sl@0
   278
	r=TheFs.CharToDrive(path[0],drv);
sl@0
   279
	test(r==KErrNone);
sl@0
   280
sl@0
   281
	isLffs=IsFileSystemLFFS(fs,drv);
sl@0
   282
	fs.Close();
sl@0
   283
	if(isLffs)
sl@0
   284
		return(ETrue);
sl@0
   285
sl@0
   286
	// check if platform expects lffs to be mounted on default drive
sl@0
   287
	TInt lffsDrv = CheckLFFSDriveForPlatform();
sl@0
   288
	if (lffsDrv == KInvalidDriveLetter)
sl@0
   289
		{
sl@0
   290
		test.Printf(_L("IsCurrentDriveLFFS: platform does not support lffs.\r\n"));
sl@0
   291
		isLffs = EFalse;
sl@0
   292
		}
sl@0
   293
	else
sl@0
   294
		{
sl@0
   295
		TChar curCh=path[0];
sl@0
   296
		curCh.LowerCase();
sl@0
   297
sl@0
   298
		TChar lffsCh;								// lffs drv ltr
sl@0
   299
		test((r = RFs::DriveToChar(lffsDrv, lffsCh)) == KErrNone);
sl@0
   300
		lffsCh.LowerCase();
sl@0
   301
		
sl@0
   302
		test.Printf(_L("IsCurrentDriveLFFS: cur drv = \'%c\', lffs drv = \'%c\'.\n"), (TText) curCh, (TText) lffsCh);
sl@0
   303
		isLffs = ((TText) curCh) == ((TText) lffsCh);
sl@0
   304
		}
sl@0
   305
sl@0
   306
	return(isLffs);
sl@0
   307
	}
sl@0
   308
sl@0
   309
GLDEF_C TBool IsNamedDriveLFFS(RFs &aFsSession,TText aDrv)
sl@0
   310
//
sl@0
   311
// Quick method of testing if running on LFFS for non t_main based tests.
sl@0
   312
// 
sl@0
   313
	{
sl@0
   314
	TInt d;
sl@0
   315
	TInt r=RFs::CharToDrive(aDrv,d);
sl@0
   316
	test(r==KErrNone);
sl@0
   317
	return(IsFileSystemLFFS(aFsSession,d));
sl@0
   318
	}
sl@0
   319
sl@0
   320
GLDEF_C TInt GetLFFSControlModeSize()
sl@0
   321
//
sl@0
   322
// For LFFS, the media may not exhibit a contiguous data region. This is the case if the 
sl@0
   323
// Control Mode Size is non-zero.
sl@0
   324
//
sl@0
   325
	{
sl@0
   326
	TLocalDriveCapsV7 caps;		// V7 to allow for devices exhibiting Control Mode
sl@0
   327
	TPckg<TLocalDriveCapsV7> capsPckg(caps);
sl@0
   328
	TBusLocalDrive localDrive;
sl@0
   329
	TBool lffsMediaFound = EFalse;
sl@0
   330
	TBool dumBool = EFalse;		// Arbitrary if LFFS is mounted on non-removable media
sl@0
   331
	// Loop to find the local drive for LFFS - this is always of type EMediaFlash
sl@0
   332
	for(TInt drvNum=0; drvNum<KMaxLocalDrives; drvNum++)
sl@0
   333
		{
sl@0
   334
		TInt r=localDrive.Connect(drvNum,dumBool);
sl@0
   335
		if(r==KErrNotSupported)
sl@0
   336
			continue;		// Local drive not present
sl@0
   337
		test_KErrNone(r);
sl@0
   338
		r=localDrive.Caps(capsPckg);
sl@0
   339
		localDrive.Disconnect();
sl@0
   340
		if(r==KErrNotSupported||r==KErrNotReady)
sl@0
   341
			continue;		// Local drive not available
sl@0
   342
		test_KErrNone(r);
sl@0
   343
		if(capsPckg().iType==EMediaFlash)
sl@0
   344
			{
sl@0
   345
			lffsMediaFound=ETrue;
sl@0
   346
			break;
sl@0
   347
			}
sl@0
   348
		}
sl@0
   349
	if(!lffsMediaFound)
sl@0
   350
		{
sl@0
   351
		test.Printf(_L("GetLFFSControlModeSize: LFFS media not found !\n"));
sl@0
   352
		return KErrGeneral;
sl@0
   353
		}
sl@0
   354
	return (capsPckg().iControlModeSize);
sl@0
   355
	}