commands/backup/backup.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // backup.cpp
       
     2 // 
       
     3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include "backup.h"
       
    14 
       
    15 _LIT(KBackupOp,		"-b");
       
    16 
       
    17 //
       
    18 // CBackupDriver
       
    19 //
       
    20 CBackupDriver* CBackupDriver::NewL(MBackupParent& aParent)
       
    21 	{
       
    22 	CBackupDriver* self = new (ELeave) CBackupDriver(aParent);
       
    23 	CleanupStack::PushL(self);
       
    24 	self->ConstructL();
       
    25 	CleanupStack::Pop(self);
       
    26 	return self;
       
    27 	}
       
    28 
       
    29 CBackupDriver::CBackupDriver(MBackupParent& aParent):
       
    30 CActive(CActive::EPriorityStandard), iParent(aParent)
       
    31 	{
       
    32 	CActiveScheduler::Add(this);
       
    33 	}
       
    34 
       
    35 CBackupDriver::~CBackupDriver()
       
    36 	{
       
    37 	Cancel();
       
    38 	}
       
    39 
       
    40 void CBackupDriver::ConstructL()
       
    41 	{
       
    42 	}
       
    43 
       
    44 void CBackupDriver::SendInstructionL(MBackupObserver::TFileLockFlags aInstruction)
       
    45 	{
       
    46 	iInstruction = aInstruction;
       
    47 	switch (iInstruction)
       
    48 		{
       
    49 		case MBackupObserver::EReleaseLockNoAccess:
       
    50 			LaunchProcessL();
       
    51 		break;
       
    52 
       
    53 		case MBackupObserver::ETakeLock:
       
    54 			TerminateProcessL();
       
    55 		break;
       
    56 
       
    57 		default:
       
    58 			User::Leave(KErrNotSupported);
       
    59 		break;		
       
    60 		};
       
    61 	}
       
    62 
       
    63 //
       
    64 // CBackupDriver::ChildProcessExists
       
    65 // checks whether the backup_child process
       
    66 // is already running
       
    67 //
       
    68 TBool CBackupDriver::ChildProcessExists()
       
    69 	{
       
    70 	_LIT(KWildCard, "*");
       
    71 	TName woop(KBackupProcessName);
       
    72 	woop.Append(KWildCard);
       
    73 	TFindProcess check(woop);
       
    74 	TFullName name;
       
    75 	TInt result = check.Next(name);
       
    76 	if (result == KErrNone)
       
    77 		return ETrue;	
       
    78 	return EFalse;
       
    79 	}
       
    80 
       
    81 //
       
    82 // CBackupDriver::LaunchProcessL
       
    83 // spawns the backup_child process
       
    84 // & waits to rendezvous
       
    85 //
       
    86 void CBackupDriver::LaunchProcessL()
       
    87 	{
       
    88 	if (ChildProcessExists())
       
    89 		User::Leave(KErrInUse);
       
    90 	
       
    91 	_LIT(KExeExt, ".exe");
       
    92 	TName name(KBackupProcessName);
       
    93 	name.Append(KExeExt);
       
    94 	User::LeaveIfError(iChild.Create(name, KBackupOp, EOwnerProcess));
       
    95 	iChild.Resume();
       
    96 	iChild.Rendezvous(iStatus);
       
    97 	SetActive();
       
    98 	}
       
    99 
       
   100 //
       
   101 // CBackupDriver::TerminateProcessL
       
   102 // open the backup_child process & signal it's semaphore
       
   103 // that semaphore is the signal to shut itself down cleanly
       
   104 //
       
   105 void CBackupDriver::TerminateProcessL()
       
   106 	{
       
   107 	if (!ChildProcessExists())
       
   108 		User::Leave(KErrNotFound);
       
   109 	
       
   110 	// signal the semaphore 
       
   111 	RSemaphore sem;
       
   112 	User::LeaveIfError(sem.OpenGlobal(KBackupSemaphore, EOwnerProcess));
       
   113 	sem.Signal();
       
   114 
       
   115 	SelfComplete(KErrNone);
       
   116 	}
       
   117 
       
   118 void CBackupDriver::SelfComplete(const TInt aError)
       
   119 	{
       
   120 	ASSERT(!IsActive());
       
   121 	iStatus = KRequestPending;
       
   122 	TRequestStatus* status = &iStatus;
       
   123 	User::RequestComplete(status, aError);
       
   124 	SetActive();
       
   125 	}
       
   126 
       
   127 //
       
   128 // CBackupDriver::RunL
       
   129 // 
       
   130 void CBackupDriver::RunL()
       
   131 	{
       
   132 	iParent.Finished(iStatus.Int());
       
   133 	}
       
   134 
       
   135 //
       
   136 // CBackupDriver::DoCancel
       
   137 //
       
   138 void CBackupDriver::DoCancel()
       
   139 	{
       
   140 	ASSERT(EFalse); // not dealing with this!
       
   141 	}
       
   142 
       
   143 
       
   144 //
       
   145 // CCmdBackup
       
   146 //
       
   147 CCommandBase* CCmdBackup::NewLC()
       
   148 	{
       
   149 	CCmdBackup* self = new (ELeave) CCmdBackup();
       
   150 	CleanupStack::PushL(self);
       
   151 	self->BaseConstructL();
       
   152 	return self;
       
   153 	}
       
   154 
       
   155 CCmdBackup::~CCmdBackup()
       
   156 	{
       
   157 	delete iBackup;
       
   158 	}
       
   159 
       
   160 CCmdBackup::CCmdBackup() : 
       
   161 CCommandBase(CCommandBase::EManualComplete)
       
   162 	{
       
   163 	}
       
   164 
       
   165 const TDesC& CCmdBackup::Name() const
       
   166 	{
       
   167 	_LIT(KName, "backup");	
       
   168 	return KName;
       
   169 	}
       
   170 
       
   171 void CCmdBackup::DoRunL()
       
   172 	{
       
   173 	// spawn the stuff I need to do the task
       
   174 	iBackup = CBackupDriver::NewL(*this);
       
   175 
       
   176 	// process the argument
       
   177 	MBackupObserver::TFileLockFlags flag = MBackupObserver::EReleaseLockNoAccess;
       
   178 	switch (iOperation)
       
   179 		{
       
   180 	case EStart:
       
   181 		flag = MBackupObserver::EReleaseLockNoAccess;
       
   182 		break;
       
   183 	case EStop:
       
   184 		flag = MBackupObserver::ETakeLock;
       
   185 		break;
       
   186 	default:
       
   187 		User::LeaveIfError(KErrArgument);
       
   188 		}
       
   189 			
       
   190 	// send the instruction through
       
   191 	iBackup->SendInstructionL(flag);
       
   192 	}
       
   193 
       
   194 void CCmdBackup::ArgumentsL(RCommandArgumentList& aArguments)
       
   195 	{
       
   196 	_LIT(KArgOperation, "operation");
       
   197 	aArguments.AppendEnumL((TInt&)iOperation, KArgOperation);
       
   198 	}
       
   199 
       
   200 void CCmdBackup::Finished(const TInt aError)
       
   201 	{
       
   202 	Complete(aError);
       
   203 	}
       
   204 
       
   205 EXE_BOILER_PLATE(CCmdBackup)