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