os/kernelhwsrv/kerneltest/f32test/plugins/version_2/src/exclusiveaccess_plugin.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 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
// Template_plugin.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "exclusiveaccess_plugin.h"
sl@0
    19
#include "plugincommon.h"
sl@0
    20
#include <f32pluginutils.h>
sl@0
    21
sl@0
    22
/**
sl@0
    23
Leaving New function for the plugin
sl@0
    24
@internalComponent
sl@0
    25
*/
sl@0
    26
CExclusiveAccessPlugin* CExclusiveAccessPlugin::NewL()
sl@0
    27
	{
sl@0
    28
	CExclusiveAccessPlugin* self = new(ELeave) CExclusiveAccessPlugin;
sl@0
    29
    CleanupStack::PushL(self);
sl@0
    30
    self->ConstructL();
sl@0
    31
    CleanupStack::Pop();
sl@0
    32
	return self;
sl@0
    33
	}
sl@0
    34
sl@0
    35
sl@0
    36
/**
sl@0
    37
Constructor for the plugin
sl@0
    38
@internalComponent
sl@0
    39
*/
sl@0
    40
CExclusiveAccessPlugin::CExclusiveAccessPlugin() : iInterceptsEnabled(EFalse),
sl@0
    41
									 iLogging(ETrue)
sl@0
    42
	{
sl@0
    43
	}
sl@0
    44
sl@0
    45
sl@0
    46
void CExclusiveAccessPlugin::ConstructL()
sl@0
    47
	{
sl@0
    48
	}
sl@0
    49
sl@0
    50
/**
sl@0
    51
The destructor for the plugin
sl@0
    52
@internalComponent
sl@0
    53
*/
sl@0
    54
CExclusiveAccessPlugin::~CExclusiveAccessPlugin()
sl@0
    55
	{
sl@0
    56
	iFs.Close();
sl@0
    57
	}
sl@0
    58
sl@0
    59
/**
sl@0
    60
Initialise the plugin.
sl@0
    61
@internalComponent
sl@0
    62
*/
sl@0
    63
void CExclusiveAccessPlugin::InitialiseL()
sl@0
    64
	{
sl@0
    65
	User::LeaveIfError(iFs.Connect());
sl@0
    66
	CleanupClosePushL(iFs);
sl@0
    67
sl@0
    68
	_LOG(_L("CExclusiveAccessPlugin InitialiseL"));
sl@0
    69
	EnableInterceptsL();
sl@0
    70
	
sl@0
    71
	CleanupStack::Pop(); // iFs
sl@0
    72
	}
sl@0
    73
sl@0
    74
/**
sl@0
    75
Enable the plugin's intercepts.
sl@0
    76
@internalComponent
sl@0
    77
*/
sl@0
    78
void CExclusiveAccessPlugin::EnableInterceptsL()
sl@0
    79
	{
sl@0
    80
	if (iInterceptsEnabled) return;
sl@0
    81
	
sl@0
    82
	User::LeaveIfError(RegisterIntercept(EFsFileRead,		EPrePostIntercept));
sl@0
    83
	User::LeaveIfError(RegisterIntercept(EFsFileWrite,		EPrePostIntercept));
sl@0
    84
sl@0
    85
	_LOG(_L("CExclusiveAccessPlugin : Enabled intercepts."));
sl@0
    86
    
sl@0
    87
	iInterceptsEnabled = ETrue;
sl@0
    88
	}
sl@0
    89
sl@0
    90
/**
sl@0
    91
Disable the plugin's intercepts.
sl@0
    92
@internalComponent
sl@0
    93
*/
sl@0
    94
void CExclusiveAccessPlugin::DisableInterceptsL()
sl@0
    95
	{
sl@0
    96
	if (!iInterceptsEnabled) return;
sl@0
    97
	
sl@0
    98
	User::LeaveIfError(UnregisterIntercept(EFsFileRead,		EPrePostIntercept));
sl@0
    99
	User::LeaveIfError(UnregisterIntercept(EFsFileWrite,	EPrePostIntercept));
sl@0
   100
	_LOG(_L("CExclusiveAccessPlugin : Disabled intercepts."));
sl@0
   101
    
sl@0
   102
	iInterceptsEnabled = EFalse;
sl@0
   103
	}
sl@0
   104
sl@0
   105
/**
sl@0
   106
Handle requests
sl@0
   107
@internalComponent
sl@0
   108
*/
sl@0
   109
TInt CExclusiveAccessPlugin::DoRequestL(TFsPluginRequest& aRequest)
sl@0
   110
	{
sl@0
   111
	TInt err = KErrNone;
sl@0
   112
sl@0
   113
	TInt function = aRequest.Function();
sl@0
   114
sl@0
   115
	switch(function)
sl@0
   116
		{
sl@0
   117
		case EFsFileRead:
sl@0
   118
			err = FsFileReadL(aRequest);
sl@0
   119
			break;
sl@0
   120
		case EFsFileWrite:
sl@0
   121
			err = FsFileWriteL(aRequest);
sl@0
   122
			break;
sl@0
   123
		default:
sl@0
   124
			//Only registered for Read/Write
sl@0
   125
			break;
sl@0
   126
		}
sl@0
   127
sl@0
   128
	return err;
sl@0
   129
	}
sl@0
   130
sl@0
   131
/*Test to ensure that when a file has been opened for exclusive access,
sl@0
   132
 * i.e. readonly, that froma  plugin we can still read from it
sl@0
   133
 */
sl@0
   134
TInt CExclusiveAccessPlugin::FsFileReadL(TFsPluginRequest& aRequest)
sl@0
   135
	{
sl@0
   136
	if(!aRequest.IsPostOperation()) // pre-operation
sl@0
   137
		{
sl@0
   138
		RFilePlugin file(aRequest);
sl@0
   139
sl@0
   140
		TInt err = file.AdoptFromClient();
sl@0
   141
		iLastError = err;
sl@0
   142
		iLineNumber = __LINE__;
sl@0
   143
		if(err!=KErrNone)
sl@0
   144
			return err;
sl@0
   145
		
sl@0
   146
		TInt64 pos;
sl@0
   147
		err = aRequest.Read(TFsPluginRequest::EPosition, pos); // get pos
sl@0
   148
		iLastError = err;
sl@0
   149
		iLineNumber = __LINE__;
sl@0
   150
		if(err!=KErrNone)
sl@0
   151
			return err;
sl@0
   152
		
sl@0
   153
		TInt length;
sl@0
   154
		err = aRequest.Read(TFsPluginRequest::ELength, length); //get length
sl@0
   155
		iLastError = err;
sl@0
   156
		iLineNumber = __LINE__;
sl@0
   157
		if(err!=KErrNone)
sl@0
   158
			return err;
sl@0
   159
		
sl@0
   160
		if(length>265)
sl@0
   161
			length=256;
sl@0
   162
		
sl@0
   163
		//we should check that this file is in fact registered as read only?
sl@0
   164
		//if not.. User::Invariant()?
sl@0
   165
		TEntry entry;
sl@0
   166
		RFsPlugin rfsplugin(aRequest);
sl@0
   167
		err = rfsplugin.Connect();
sl@0
   168
		iLastError = err;
sl@0
   169
		iLineNumber = __LINE__;
sl@0
   170
		if(err!=KErrNone)
sl@0
   171
			return err;
sl@0
   172
sl@0
   173
		TFileName fileName;
sl@0
   174
		err = aRequest.FileName(fileName);
sl@0
   175
		iLastError = err;
sl@0
   176
		iLineNumber = __LINE__;
sl@0
   177
		if(err!=KErrNone)
sl@0
   178
			return err;
sl@0
   179
		
sl@0
   180
		err = rfsplugin.Entry(fileName, entry);
sl@0
   181
		iLastError = err;
sl@0
   182
		iLineNumber = __LINE__;
sl@0
   183
		if(err!=KErrNone)
sl@0
   184
			return err;
sl@0
   185
		
sl@0
   186
		//can we read a readonly file? - should be fine.
sl@0
   187
		TBuf8<256> data;
sl@0
   188
		err = file.Read(pos,data,length);
sl@0
   189
		iLastError = err;
sl@0
   190
		iLineNumber = __LINE__;
sl@0
   191
		if(err!=KErrNone)
sl@0
   192
			return err;
sl@0
   193
				
sl@0
   194
		file.Close();
sl@0
   195
		rfsplugin.Close();
sl@0
   196
		}
sl@0
   197
sl@0
   198
	return KErrNone;
sl@0
   199
	}
sl@0
   200
sl@0
   201
/*Test to ensure that when a file has been opened for exclusive access,
sl@0
   202
 * i.e. readonly, that from a plugin we can still write to it regardless
sl@0
   203
 */
sl@0
   204
TInt CExclusiveAccessPlugin::FsFileWriteL(TFsPluginRequest& aRequest)
sl@0
   205
	{
sl@0
   206
	if(!aRequest.IsPostOperation()) // pre-operation
sl@0
   207
		{
sl@0
   208
		//Make sure that the file is read only.
sl@0
   209
sl@0
   210
		RFilePlugin file(aRequest);
sl@0
   211
		TInt err = file.AdoptFromClient();
sl@0
   212
		iLastError = err;
sl@0
   213
		iLineNumber = __LINE__;
sl@0
   214
		if(err!=KErrNone)
sl@0
   215
			return err;
sl@0
   216
		
sl@0
   217
		RFsPlugin rfsplugin(aRequest);
sl@0
   218
		err = rfsplugin.Connect();
sl@0
   219
		iLastError = err;
sl@0
   220
		iLineNumber = __LINE__;
sl@0
   221
		if(err!=KErrNone)
sl@0
   222
			return err;
sl@0
   223
		
sl@0
   224
		TEntry entry;
sl@0
   225
		err = rfsplugin.Entry(aRequest.Src().FullName(), entry);
sl@0
   226
		iLastError = err;
sl@0
   227
		iLineNumber = __LINE__;
sl@0
   228
		if(err!=KErrNone)
sl@0
   229
			return err;
sl@0
   230
		
sl@0
   231
//		if(!entry.IsReadOnly())
sl@0
   232
//			{
sl@0
   233
//			//this test should only being being used for read only files.
sl@0
   234
//			User::Invariant();
sl@0
   235
//			}
sl@0
   236
		
sl@0
   237
		TInt64 pos;
sl@0
   238
		err = aRequest.Read(TFsPluginRequest::EPosition, pos); //get pos
sl@0
   239
		iLastError = err;
sl@0
   240
		iLineNumber = __LINE__;
sl@0
   241
		if(err!=KErrNone)
sl@0
   242
			return err;
sl@0
   243
		
sl@0
   244
		TInt length;
sl@0
   245
		err = aRequest.Read(TFsPluginRequest::ELength, length); //get length
sl@0
   246
		iLastError = err;
sl@0
   247
		iLineNumber = __LINE__;
sl@0
   248
		if(err!=KErrNone)
sl@0
   249
			return err;
sl@0
   250
		
sl@0
   251
		TBuf8<1024> data;
sl@0
   252
		err = aRequest.Read(TFsPluginRequest::EData, data); //get data to write
sl@0
   253
		iLastError = err;
sl@0
   254
		iLineNumber = __LINE__;
sl@0
   255
		if(err!=KErrNone)
sl@0
   256
			return err;
sl@0
   257
		
sl@0
   258
		//Now test that we can actually write to this read-only file
sl@0
   259
		//Should pass, kerrnone.
sl@0
   260
		err = file.Write(pos,data,length);
sl@0
   261
		iLastError = err;
sl@0
   262
		iLineNumber = __LINE__;
sl@0
   263
		if(err!=KErrNone)
sl@0
   264
			return err;
sl@0
   265
		
sl@0
   266
		file.Close();
sl@0
   267
sl@0
   268
		//We've performed the efsfilewrite, so return kerrcompletion.
sl@0
   269
		return KErrCompletion;		
sl@0
   270
		}
sl@0
   271
sl@0
   272
	return KErrNone;
sl@0
   273
	}
sl@0
   274
sl@0
   275
sl@0
   276
sl@0
   277
CFsPluginConn* CExclusiveAccessPlugin::NewPluginConnL()
sl@0
   278
	{
sl@0
   279
	return new(ELeave) CExclusiveAccessPluginConn();
sl@0
   280
	}
sl@0
   281
sl@0
   282
sl@0
   283
//Synchronous RPlugin::DoControl
sl@0
   284
TInt CExclusiveAccessPlugin::FsPluginDoControlL(CFsPluginConnRequest& aRequest)
sl@0
   285
	{	
sl@0
   286
	TInt err = KErrNone;
sl@0
   287
sl@0
   288
	//We can use this to set the drive
sl@0
   289
	//We can store this as a member of this class.
sl@0
   290
	TInt function = aRequest.Function();
sl@0
   291
	TPckg<TInt> errCodeDes(iLastError);
sl@0
   292
	TPckg<TInt> lineNumberDes(iLineNumber);
sl@0
   293
	
sl@0
   294
	switch(function)
sl@0
   295
		{
sl@0
   296
		case KPluginGetError:
sl@0
   297
			{
sl@0
   298
			TRAP(err,aRequest.WriteParam1L(errCodeDes));
sl@0
   299
			TRAP(err,aRequest.WriteParam2L(lineNumberDes));
sl@0
   300
			break;
sl@0
   301
			}
sl@0
   302
		default:
sl@0
   303
			break;
sl@0
   304
		}
sl@0
   305
sl@0
   306
	return err;
sl@0
   307
	}
sl@0
   308
sl@0
   309
sl@0
   310
TInt CExclusiveAccessPluginConn::DoControl(CFsPluginConnRequest& aRequest)
sl@0
   311
	{
sl@0
   312
	return ((CExclusiveAccessPlugin*)Plugin())->FsPluginDoControlL(aRequest);
sl@0
   313
	}
sl@0
   314
sl@0
   315
void CExclusiveAccessPluginConn::DoRequest(CFsPluginConnRequest& aRequest)
sl@0
   316
	{
sl@0
   317
	DoControl(aRequest);
sl@0
   318
	}
sl@0
   319
sl@0
   320
void CExclusiveAccessPluginConn::DoCancel(TInt /*aReqMask*/)
sl@0
   321
	{
sl@0
   322
	}
sl@0
   323
sl@0
   324
sl@0
   325
//factory functions
sl@0
   326
sl@0
   327
class CExclusiveAccessPluginFactory : public CFsPluginFactory
sl@0
   328
	{
sl@0
   329
public:
sl@0
   330
	CExclusiveAccessPluginFactory();
sl@0
   331
	virtual TInt Install();			
sl@0
   332
	virtual CFsPlugin* NewPluginL();
sl@0
   333
	virtual CFsPlugin* NewPluginConnL();
sl@0
   334
	virtual TInt UniquePosition();
sl@0
   335
	};
sl@0
   336
sl@0
   337
/**
sl@0
   338
Constructor for the plugin factory
sl@0
   339
@internalComponent
sl@0
   340
*/
sl@0
   341
CExclusiveAccessPluginFactory::CExclusiveAccessPluginFactory()
sl@0
   342
	{
sl@0
   343
	}
sl@0
   344
sl@0
   345
/**
sl@0
   346
Install function for the plugin factory
sl@0
   347
@internalComponent
sl@0
   348
*/
sl@0
   349
TInt CExclusiveAccessPluginFactory::Install()
sl@0
   350
	{
sl@0
   351
	SetSupportedDrives(KPluginSupportAllDrives);
sl@0
   352
	return(SetName(&KExclusiveAccessPluginName));
sl@0
   353
	}
sl@0
   354
sl@0
   355
/**
sl@0
   356
@internalComponent
sl@0
   357
*/
sl@0
   358
TInt CExclusiveAccessPluginFactory::UniquePosition()
sl@0
   359
	{
sl@0
   360
	return(KExclusiveAccessPos);
sl@0
   361
	}
sl@0
   362
sl@0
   363
/**
sl@0
   364
Plugin factory function
sl@0
   365
@internalComponent
sl@0
   366
*/
sl@0
   367
CFsPlugin* CExclusiveAccessPluginFactory::NewPluginL()
sl@0
   368
sl@0
   369
	{
sl@0
   370
	return CExclusiveAccessPlugin::NewL();
sl@0
   371
	}
sl@0
   372
sl@0
   373
/**
sl@0
   374
Plugin factory function
sl@0
   375
@internalComponent
sl@0
   376
*/
sl@0
   377
CFsPlugin* CExclusiveAccessPluginFactory::NewPluginConnL()
sl@0
   378
sl@0
   379
	{
sl@0
   380
	return CExclusiveAccessPlugin::NewL();
sl@0
   381
	}
sl@0
   382
sl@0
   383
/**
sl@0
   384
Create a new Plugin
sl@0
   385
@internalComponent
sl@0
   386
*/
sl@0
   387
extern "C" {
sl@0
   388
sl@0
   389
EXPORT_C CFsPluginFactory* CreateFileSystem()
sl@0
   390
	{
sl@0
   391
	return(new CExclusiveAccessPluginFactory());
sl@0
   392
	}
sl@0
   393
}
sl@0
   394