|
1 // restore.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 "restore.h" |
|
14 |
|
15 _LIT(KRestoreOp, "-r"); |
|
16 |
|
17 // |
|
18 // CRestoreDriver |
|
19 // |
|
20 CRestoreDriver* CRestoreDriver::NewL(MBackupParent& aParent) |
|
21 { |
|
22 CRestoreDriver* self = new (ELeave) CRestoreDriver(aParent); |
|
23 CleanupStack::PushL(self); |
|
24 self->ConstructL(); |
|
25 CleanupStack::Pop(self); |
|
26 return self; |
|
27 } |
|
28 |
|
29 CRestoreDriver::CRestoreDriver(MBackupParent& aParent): |
|
30 CActive(CActive::EPriorityStandard), iParent(aParent) |
|
31 { |
|
32 CActiveScheduler::Add(this); |
|
33 } |
|
34 |
|
35 CRestoreDriver::~CRestoreDriver() |
|
36 { |
|
37 Cancel(); |
|
38 } |
|
39 |
|
40 void CRestoreDriver::ConstructL() |
|
41 { |
|
42 } |
|
43 |
|
44 void CRestoreDriver::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 // CRestoreDriver::ChildProcessExists |
|
65 // checks whether the backup_child process |
|
66 // is already running |
|
67 // |
|
68 TBool CRestoreDriver::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 // CRestoreDriver::LaunchProcessL |
|
83 // spawns the backup_child process |
|
84 // & waits to rendezvous |
|
85 // |
|
86 void CRestoreDriver::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, KRestoreOp, EOwnerProcess)); |
|
95 iChild.Resume(); |
|
96 iChild.Rendezvous(iStatus); |
|
97 SetActive(); |
|
98 } |
|
99 |
|
100 // |
|
101 // CRestoreDriver::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 CRestoreDriver::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 CRestoreDriver::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 // CRestoreDriver::RunL |
|
129 // |
|
130 void CRestoreDriver::RunL() |
|
131 { |
|
132 iParent.Finished(iStatus.Int()); |
|
133 } |
|
134 |
|
135 // |
|
136 // CRestoreDriver::DoCancel |
|
137 // |
|
138 void CRestoreDriver::DoCancel() |
|
139 { |
|
140 ASSERT(EFalse); // not dealing with this! |
|
141 } |
|
142 |
|
143 |
|
144 // |
|
145 // CCmdRestore |
|
146 // |
|
147 CCommandBase* CCmdRestore::NewLC() |
|
148 { |
|
149 CCmdRestore* self = new (ELeave) CCmdRestore(); |
|
150 CleanupStack::PushL(self); |
|
151 self->BaseConstructL(); |
|
152 return self; |
|
153 } |
|
154 |
|
155 CCmdRestore::~CCmdRestore() |
|
156 { |
|
157 delete iRestore; |
|
158 } |
|
159 |
|
160 CCmdRestore::CCmdRestore() : |
|
161 CCommandBase(CCommandBase::EManualComplete) |
|
162 { |
|
163 } |
|
164 |
|
165 const TDesC& CCmdRestore::Name() const |
|
166 { |
|
167 _LIT(KName, "restore"); |
|
168 return KName; |
|
169 } |
|
170 |
|
171 void CCmdRestore::DoRunL() |
|
172 { |
|
173 // spawn the stuff I need to do the task |
|
174 iRestore = CRestoreDriver::NewL(*this); |
|
175 |
|
176 // process the argument |
|
177 MBackupObserver::TFileLockFlags flag = MBackupObserver::EReleaseLockNoAccess; |
|
178 |
|
179 switch (iOperation) |
|
180 { |
|
181 case EStart: |
|
182 flag = MBackupObserver::EReleaseLockNoAccess; |
|
183 break; |
|
184 case EStop: |
|
185 flag = MBackupObserver::ETakeLock; |
|
186 break; |
|
187 default: |
|
188 User::LeaveIfError(KErrArgument); |
|
189 } |
|
190 |
|
191 // send the instruction through |
|
192 iRestore->SendInstructionL(flag); |
|
193 } |
|
194 |
|
195 void CCmdRestore::ArgumentsL(RCommandArgumentList& aArguments) |
|
196 { |
|
197 _LIT(KArgOperation, "operation"); |
|
198 aArguments.AppendEnumL((TInt&)iOperation, KArgOperation); |
|
199 } |
|
200 |
|
201 void CCmdRestore::OptionsL(RCommandOptionList&) |
|
202 { |
|
203 } |
|
204 |
|
205 void CCmdRestore::Finished(const TInt aError) |
|
206 { |
|
207 Complete(aError); |
|
208 } |
|
209 |
|
210 EXE_BOILER_PLATE(CCmdRestore) |