filebrowser/fileopclient/src/FBFileOpClient.cpp
changeset 0 d6fe6244b863
equal deleted inserted replaced
-1:000000000000 0:d6fe6244b863
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // needed for client interface
       
    20 #include "FBFileOpClient.h"
       
    21 
       
    22 const TUint KDefaultMessageSlots=4;
       
    23 
       
    24 // --------------------------------------------------------------------------------------------
       
    25 
       
    26 static TInt StartServer()
       
    27 //
       
    28 // Start the server process. Simultaneous launching
       
    29 // of two such processes should be detected when the second one attempts to
       
    30 // create the server object, failing with KErrAlreadyExists.
       
    31 //
       
    32 	{
       
    33 	const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
       
    34 	RProcess server;
       
    35 	TInt r=server.Create(KMyServerImg,KNullDesC,serverUid);
       
    36 	if (r!=KErrNone)
       
    37 		return r;
       
    38 	TRequestStatus stat;
       
    39 	server.Rendezvous(stat);
       
    40 	if (stat!=KRequestPending)
       
    41 		server.Kill(0);		// abort startup
       
    42 	else
       
    43 		server.Resume();	// logon OK - start the server
       
    44 	User::WaitForRequest(stat);		// wait for start or death
       
    45 	// we can't use the 'exit reason' if the server panicked as this
       
    46 	// is the panic 'reason' and may be '0' which cannot be distinguished
       
    47 	// from KErrNone
       
    48 	r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
       
    49 	server.Close();
       
    50 	return r;
       
    51 	}
       
    52 
       
    53 // --------------------------------------------------------------------------------------------
       
    54 
       
    55 TVersion RFBFileOpServerSession::Version(void) const
       
    56 	{
       
    57 	return(TVersion(KCopyServMajorVersionNumber,KCopyServMinorVersionNumber,KCopyServBuildVersionNumber));
       
    58 	}
       
    59 
       
    60 // --------------------------------------------------------------------------------------------
       
    61 
       
    62 TInt RFBFileOpServerSession::Connect()
       
    63 //
       
    64 // Connect to the server, attempting to start it if necessary
       
    65 //
       
    66 	{
       
    67 	TInt retry=2;
       
    68 	for (;;)
       
    69 		{
       
    70 		TInt r=CreateSession(KMyServerName,Version(),KDefaultMessageSlots);
       
    71 		if (r!=KErrNotFound && r!=KErrServerTerminated)
       
    72 			return r;
       
    73 		if (--retry==0)
       
    74 			return r;
       
    75 		r=StartServer();
       
    76 		if (r!=KErrNone && r!=KErrAlreadyExists)
       
    77 			return r;
       
    78 		}
       
    79 	}
       
    80 
       
    81 // --------------------------------------------------------------------------------------------
       
    82 
       
    83 void RFBFileOpServerSession::Copy(const TDesC& aSourceFullName, const TDesC& aTargetFullName, TUint aSwitch, TRequestStatus& aStatus) 
       
    84     {
       
    85     TFileOpArgs argsStruct = TFileOpArgs();
       
    86     argsStruct.iBuf1.Copy(aSourceFullName);
       
    87     argsStruct.iBuf2.Copy(aTargetFullName);
       
    88     argsStruct.iUint1 = aSwitch;
       
    89     
       
    90     iPckgBuf = argsStruct;
       
    91     TIpcArgs args(&iPckgBuf);
       
    92     SendReceive(EFileOpCopy, args, aStatus);
       
    93     }
       
    94 
       
    95 // --------------------------------------------------------------------------------------------
       
    96 
       
    97 TInt RFBFileOpServerSession::Rename(const TDesC& anOld, const TDesC& aNew, TUint aSwitch) 
       
    98     {
       
    99     TFileOpArgs argsStruct = TFileOpArgs();
       
   100     argsStruct.iBuf1.Copy(anOld);
       
   101     argsStruct.iBuf2.Copy(aNew);
       
   102     argsStruct.iUint1 = aSwitch;
       
   103     
       
   104     TPckgBuf<TFileOpArgs> pckgBuf;
       
   105     pckgBuf = argsStruct;
       
   106 	TIpcArgs args(&pckgBuf);
       
   107 	return SendReceive(EFileOpRename, args);
       
   108     }
       
   109 
       
   110 // --------------------------------------------------------------------------------------------
       
   111 
       
   112 TInt RFBFileOpServerSession::Attribs(const TDesC& aName, TUint aSetMask, TUint aClearMask, const TTime& aTime, TUint aSwitch) 
       
   113     {
       
   114     TFileOpArgs argsStruct = TFileOpArgs();
       
   115     argsStruct.iBuf1.Copy(aName);
       
   116     argsStruct.iUint1 = aSetMask;
       
   117     argsStruct.iUint2 = aClearMask;
       
   118     argsStruct.iTime1 = aTime;
       
   119     argsStruct.iUint3 = aSwitch;
       
   120     
       
   121     TPckgBuf<TFileOpArgs> pckgBuf;
       
   122     pckgBuf = argsStruct;
       
   123 	TIpcArgs args(&pckgBuf);
       
   124 	return SendReceive(EFileOpAttribs, args);
       
   125     }
       
   126 
       
   127 // --------------------------------------------------------------------------------------------
       
   128 
       
   129 void RFBFileOpServerSession::RmDir( const TDesC& aDirName,
       
   130                                     TUint aSwitch,
       
   131                                     TRequestStatus& aStatus ) 
       
   132     {
       
   133     TFileOpArgs argsStruct = TFileOpArgs();
       
   134     argsStruct.iBuf1.Copy(aDirName);
       
   135     argsStruct.iUint1 = aSwitch;
       
   136 
       
   137     iPckgBuf = argsStruct;
       
   138     TIpcArgs args(&iPckgBuf);
       
   139 	SendReceive(EFileOpRmDir, args, aStatus);
       
   140     }
       
   141 
       
   142 // --------------------------------------------------------------------------------------------
       
   143 
       
   144 void RFBFileOpServerSession::Delete(const TDesC& aName, TUint aSwitch, TRequestStatus& aStatus) 
       
   145     {
       
   146     TFileOpArgs argsStruct = TFileOpArgs();
       
   147     argsStruct.iBuf1.Copy(aName);
       
   148     argsStruct.iUint1 = aSwitch;
       
   149 
       
   150     iPckgBuf = argsStruct;
       
   151     TIpcArgs args(&iPckgBuf);
       
   152 	SendReceive(EFileOpDelete, args, aStatus);
       
   153     }
       
   154 
       
   155 // --------------------------------------------------------------------------------------------
       
   156 
       
   157 TInt RFBFileOpServerSession::MkDirAll(const TDesC& aPath) 
       
   158     {
       
   159     TFileOpArgs argsStruct = TFileOpArgs();
       
   160     argsStruct.iBuf1.Copy(aPath);
       
   161     
       
   162     TPckgBuf<TFileOpArgs> pckgBuf;
       
   163     pckgBuf = argsStruct;
       
   164 	TIpcArgs args(&pckgBuf);
       
   165 	return SendReceive(EFileOpMkDirAll, args);
       
   166     }
       
   167 
       
   168 // --------------------------------------------------------------------------------------------
       
   169 
       
   170 TInt RFBFileOpServerSession::CreateEmptyFile(const TDesC& aName) 
       
   171     {
       
   172     TFileOpArgs argsStruct = TFileOpArgs();
       
   173     argsStruct.iBuf1.Copy(aName);
       
   174     
       
   175     TPckgBuf<TFileOpArgs> pckgBuf;
       
   176     pckgBuf = argsStruct;
       
   177 	TIpcArgs args(&pckgBuf);
       
   178 	return SendReceive(EFileOpCreateEmptyFile, args);
       
   179     }
       
   180 // --------------------------------------------------------------------------------------------
       
   181 
       
   182 TInt RFBFileOpServerSession::EraseMBR(TUint aDriveNumber) 
       
   183     {
       
   184     TFileOpArgs argsStruct = TFileOpArgs();
       
   185     argsStruct.iUint1 = aDriveNumber;
       
   186         
       
   187     TPckgBuf<TFileOpArgs> pckgBuf;
       
   188     pckgBuf = argsStruct;
       
   189 	TIpcArgs args(&pckgBuf);
       
   190 	return SendReceive(EFileOpEraseMBR, args);
       
   191     }
       
   192     
       
   193 // --------------------------------------------------------------------------------------------
       
   194 
       
   195 TInt RFBFileOpServerSession::PartitionDrive(TUint aDriveNumber, TUint aNumberOfPartitions) 
       
   196     {
       
   197     TFileOpArgs argsStruct = TFileOpArgs();
       
   198     argsStruct.iUint1 = aDriveNumber;
       
   199     argsStruct.iUint2 = aNumberOfPartitions;
       
   200         
       
   201     TPckgBuf<TFileOpArgs> pckgBuf;
       
   202     pckgBuf = argsStruct;
       
   203 	TIpcArgs args(&pckgBuf);
       
   204 	return SendReceive(EFileOpPartitionDrive, args);
       
   205     }
       
   206 
       
   207 // --------------------------------------------------------------------------------------------
       
   208 
       
   209 TInt RFBFileOpServerSession::CancelOp() 
       
   210     {
       
   211     TIpcArgs args( NULL );
       
   212     return SendReceive( EFileOpCancel, args );
       
   213     }
       
   214 
       
   215 // --------------------------------------------------------------------------------------------
       
   216 // --------------------------------------------------------------------------------------------
       
   217 
       
   218 EXPORT_C CFBFileOpClient* CFBFileOpClient::NewL()
       
   219     {
       
   220     CFBFileOpClient* self = new(ELeave) CFBFileOpClient;
       
   221     CleanupStack::PushL(self);
       
   222     self->ConstructL();
       
   223     CleanupStack::Pop(self);
       
   224     return self;
       
   225     }
       
   226 
       
   227 // --------------------------------------------------------------------------------------------
       
   228 
       
   229 EXPORT_C CFBFileOpClient::~CFBFileOpClient()
       
   230     {
       
   231     iFBFileOpServerSession.Close();
       
   232     }
       
   233 
       
   234 // --------------------------------------------------------------------------------------------
       
   235 
       
   236 CFBFileOpClient::CFBFileOpClient()
       
   237     {
       
   238     }
       
   239 
       
   240 // --------------------------------------------------------------------------------------------
       
   241 
       
   242 void CFBFileOpClient::ConstructL()
       
   243     {	
       
   244     User::LeaveIfError(iFBFileOpServerSession.Connect());
       
   245     }
       
   246 
       
   247 // --------------------------------------------------------------------------------------------
       
   248 
       
   249 EXPORT_C TInt CFBFileOpClient::Copy( const TDesC& aSourceFullName,
       
   250                                      const TDesC& aTargetFullName,
       
   251                                      TUint aSwitch ) 
       
   252     {
       
   253     CAsyncWaiterClient* waiter = CAsyncWaiterClient::NewLC();
       
   254     iFBFileOpServerSession.Copy( aSourceFullName, aTargetFullName, aSwitch, waiter->iStatus );
       
   255     waiter->StartAndWait();
       
   256     TInt err = waiter->Result();
       
   257     
       
   258     if ( err == KErrServerTerminated )
       
   259         {
       
   260         User::LeaveIfError( iFBFileOpServerSession.Connect() );
       
   261         iFBFileOpServerSession.Copy( aSourceFullName, aTargetFullName, aSwitch, waiter->iStatus );
       
   262         waiter->StartAndWait();
       
   263         err = waiter->Result();
       
   264         }
       
   265     CleanupStack::PopAndDestroy( waiter );
       
   266     return err;
       
   267     }
       
   268 
       
   269 // --------------------------------------------------------------------------------------------
       
   270 
       
   271 EXPORT_C TInt CFBFileOpClient::Rename(const TDesC& anOld, const TDesC& aNew, TUint aSwitch) 
       
   272     {
       
   273     TInt err = iFBFileOpServerSession.Rename(anOld, aNew, aSwitch);
       
   274         
       
   275     if (err == KErrServerTerminated)
       
   276         {
       
   277         User::LeaveIfError(iFBFileOpServerSession.Connect());
       
   278         err = iFBFileOpServerSession.Rename(anOld, aNew, aSwitch);
       
   279         }
       
   280     
       
   281     return err;
       
   282     }
       
   283 
       
   284 // --------------------------------------------------------------------------------------------
       
   285 
       
   286 EXPORT_C TInt CFBFileOpClient::Attribs(const TDesC& aName, TUint aSetMask, TUint aClearMask, const TTime& aTime, TUint aSwitch) 
       
   287     {
       
   288     TInt err = iFBFileOpServerSession.Attribs(aName, aSetMask, aClearMask, aTime, aSwitch);
       
   289     
       
   290     if (err == KErrServerTerminated)
       
   291         {
       
   292         User::LeaveIfError(iFBFileOpServerSession.Connect());
       
   293         err = iFBFileOpServerSession.Attribs(aName, aSetMask, aClearMask, aTime, aSwitch);
       
   294         }
       
   295     
       
   296     return err;
       
   297     }
       
   298 
       
   299 // --------------------------------------------------------------------------------------------
       
   300 
       
   301 EXPORT_C TInt CFBFileOpClient::RmDir(const TDesC& aDirName, TUint aSwitch)
       
   302     {
       
   303     CAsyncWaiterClient* waiter = CAsyncWaiterClient::NewLC();
       
   304     iFBFileOpServerSession.RmDir( aDirName, aSwitch, waiter->iStatus );
       
   305     waiter->StartAndWait();
       
   306     TInt err = waiter->Result();
       
   307     
       
   308     if ( err == KErrServerTerminated )
       
   309         {
       
   310         User::LeaveIfError( iFBFileOpServerSession.Connect() );
       
   311         iFBFileOpServerSession.RmDir( aDirName, aSwitch, waiter->iStatus );
       
   312         waiter->StartAndWait();
       
   313         err = waiter->Result();
       
   314         }
       
   315     CleanupStack::PopAndDestroy( waiter );
       
   316     return err;
       
   317     }
       
   318 
       
   319 // --------------------------------------------------------------------------------------------
       
   320 
       
   321 EXPORT_C TInt CFBFileOpClient::Delete(const TDesC& aName, TUint aSwitch)
       
   322     {
       
   323     CAsyncWaiterClient* waiter = CAsyncWaiterClient::NewLC();
       
   324     iFBFileOpServerSession.Delete(aName, aSwitch, waiter->iStatus );
       
   325     waiter->StartAndWait();
       
   326     TInt err = waiter->Result();
       
   327     
       
   328     if ( err == KErrServerTerminated )
       
   329         {
       
   330         User::LeaveIfError( iFBFileOpServerSession.Connect() );
       
   331         iFBFileOpServerSession.Delete(aName, aSwitch, waiter->iStatus );
       
   332         waiter->StartAndWait();
       
   333         err = waiter->Result();
       
   334         }
       
   335     CleanupStack::PopAndDestroy( waiter );
       
   336     return err;
       
   337     }
       
   338 
       
   339 // --------------------------------------------------------------------------------------------
       
   340 
       
   341 EXPORT_C TInt CFBFileOpClient::MkDirAll(const TDesC& aPath) 
       
   342     {
       
   343     TInt err = iFBFileOpServerSession.MkDirAll(aPath);
       
   344     
       
   345     if (err == KErrServerTerminated)
       
   346         {
       
   347         User::LeaveIfError(iFBFileOpServerSession.Connect());
       
   348         err = iFBFileOpServerSession.MkDirAll(aPath);
       
   349         }
       
   350             
       
   351     return err;
       
   352     }
       
   353 
       
   354 // --------------------------------------------------------------------------------------------
       
   355 
       
   356 EXPORT_C TInt CFBFileOpClient::CreateEmptyFile(const TDesC& aName) 
       
   357     {
       
   358     TInt err = iFBFileOpServerSession.CreateEmptyFile(aName);
       
   359     
       
   360     if (err == KErrServerTerminated)
       
   361         {
       
   362         User::LeaveIfError(iFBFileOpServerSession.Connect());
       
   363         err = iFBFileOpServerSession.CreateEmptyFile(aName);
       
   364         }
       
   365 
       
   366     return err;
       
   367     }
       
   368 
       
   369 // --------------------------------------------------------------------------------------------
       
   370 
       
   371 EXPORT_C TInt CFBFileOpClient::EraseMBR(TUint aDriveNumber) 
       
   372     {
       
   373     TInt err = iFBFileOpServerSession.EraseMBR(aDriveNumber);
       
   374     
       
   375     if (err == KErrServerTerminated)
       
   376         {
       
   377         User::LeaveIfError(iFBFileOpServerSession.Connect());
       
   378         err = iFBFileOpServerSession.EraseMBR(aDriveNumber);
       
   379         }
       
   380 
       
   381     return err;
       
   382     }
       
   383 
       
   384 // --------------------------------------------------------------------------------------------
       
   385 
       
   386 EXPORT_C TInt CFBFileOpClient::PartitionDrive(TUint aDriveNumber, TUint aNumberOfPartitions) 
       
   387     {
       
   388     TInt err = iFBFileOpServerSession.PartitionDrive(aDriveNumber, aNumberOfPartitions);
       
   389     
       
   390     if (err == KErrServerTerminated)
       
   391         {
       
   392         User::LeaveIfError(iFBFileOpServerSession.Connect());
       
   393         err = iFBFileOpServerSession.PartitionDrive(aDriveNumber, aNumberOfPartitions);
       
   394         }
       
   395 
       
   396     return err;
       
   397     }
       
   398 
       
   399 // --------------------------------------------------------------------------------------------
       
   400 
       
   401 EXPORT_C void CFBFileOpClient::CancelOp() 
       
   402     {
       
   403     TInt err = iFBFileOpServerSession.CancelOp();
       
   404     
       
   405     if ( err == KErrServerTerminated )
       
   406         {
       
   407         User::LeaveIfError( iFBFileOpServerSession.Connect() );
       
   408         err = iFBFileOpServerSession.CancelOp(); // Ignore return value this time
       
   409         }
       
   410     }
       
   411 
       
   412 // --------------------------------------------------------------------------------------------
       
   413 // --------------------------------------------------------------------------------------------
       
   414 
       
   415 CAsyncWaiterClient* CAsyncWaiterClient::NewL( TInt aPriority )
       
   416     {
       
   417     CAsyncWaiterClient* self = new(ELeave) CAsyncWaiterClient( aPriority );
       
   418     return self;
       
   419     }
       
   420 
       
   421 // --------------------------------------------------------------------------------------------
       
   422 
       
   423 CAsyncWaiterClient* CAsyncWaiterClient::NewLC( TInt aPriority )
       
   424     {
       
   425     CAsyncWaiterClient* self = new (ELeave) CAsyncWaiterClient( aPriority );
       
   426     CleanupStack::PushL( self );
       
   427     return self;
       
   428     }
       
   429 
       
   430 // --------------------------------------------------------------------------------------------
       
   431     
       
   432 CAsyncWaiterClient::CAsyncWaiterClient( TInt aPriority ) : CActive( aPriority )
       
   433     {
       
   434     CActiveScheduler::Add( this );
       
   435     }   
       
   436 
       
   437 // --------------------------------------------------------------------------------------------
       
   438 
       
   439 CAsyncWaiterClient::~CAsyncWaiterClient()
       
   440     {
       
   441     Cancel();
       
   442     }
       
   443 
       
   444 // --------------------------------------------------------------------------------------------
       
   445     
       
   446 void CAsyncWaiterClient::StartAndWait()
       
   447     {
       
   448     SetActive();
       
   449     iWait.Start();
       
   450     }
       
   451 
       
   452 // --------------------------------------------------------------------------------------------
       
   453     
       
   454 TInt CAsyncWaiterClient::Result() const
       
   455     {
       
   456     return iError;
       
   457     }
       
   458 
       
   459 // --------------------------------------------------------------------------------------------
       
   460     
       
   461 void CAsyncWaiterClient::RunL()
       
   462     {
       
   463     iError = iStatus.Int();
       
   464     iWait.AsyncStop();
       
   465     }
       
   466 
       
   467 // --------------------------------------------------------------------------------------------
       
   468     
       
   469 void CAsyncWaiterClient::DoCancel()
       
   470     {
       
   471     iError = KErrCancel;
       
   472     if( iStatus == KRequestPending )
       
   473         {
       
   474         TRequestStatus* s = &iStatus;
       
   475         User::RequestComplete( s, KErrCancel );
       
   476         }
       
   477     iWait.AsyncStop();
       
   478     }
       
   479 
       
   480 // --------------------------------------------------------------------------------------------
       
   481