a653d2ac40d284f2cec1327c2cf6ab6f0346f0ff
[platform/framework/native/installer.git] / src / Step / UninstallStep.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        UninstallStep.cpp
19  * @brief       This is the implementation file for %UninstallStep class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <app2ext_interface.h>
25
26 #include <FIoDirectory.h>
27 #include <FIo_FileImpl.h>
28 #include <FBase_StringConverter.h>
29 #include <FAppPkg_PackageInfoImpl.h>
30
31 #include "InstallationContext.h"
32 #include "UninstallStep.h"
33 #include "InstallerUtil.h"
34 #include "CompatibilityManager.h"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::App;
39 using namespace Tizen::App::Package;
40 using namespace Tizen::Io;
41
42 UninstallStep::UninstallStep(void)
43 :__state(STATE_GET_PACKAGEINFO)
44 ,__pContext(null)
45 {
46 }
47
48 UninstallStep::~UninstallStep(void)
49 {
50 }
51
52 InstallerError
53 UninstallStep::Run(InstallationContext* pContext)
54 {
55         InstallerError error = INSTALLER_ERROR_NONE;
56         AppLog(" UninstallStep - START");
57
58         __pContext = pContext;
59
60         while (true)
61         {
62                 switch (__state)
63                 {
64                 case STATE_GET_PACKAGEINFO:
65                         AppLog("[STATE_GET_PACKAGEINFO]");
66                         error = OnStateGetPackageInfo();
67                         break;
68
69                 case STATE_POLICY_CHECK:
70                         AppLog("[STATE_POLICY_CHECK]");
71                         error = OnStatePolicyCheck();
72                         break;
73
74                 case STATE_TERMINATE_APP:
75                         AppLog("[STATE_TERMINATE_APP]");
76                         error = OnStateTerminateApp();
77                         break;
78
79                 case STATE_START_TIMER:
80                         AppLog("[STATE_START_TIMER]");
81                         error = OnStateStartTimer();
82                         break;
83
84                 case STATE_DELETE_DIR:
85                         AppLog("[STATE_DELETE_DIR]");
86                         error = OnStateRemoveDir();
87                         break;
88
89                 case STATE_DONE:
90                         AppLog("[STATE_DONE]");
91                         error = OnStateDone();
92                         break;
93
94                 default:
95                         break;
96                 }
97
98                 if (error != INSTALLER_ERROR_NONE)
99                 {
100                         break;
101                 }
102
103                 if (__state > STATE_DONE)
104                 {
105                         AppLog(" UninstallStep - END");
106                         break;
107                 }
108         }
109
110         return error;
111 }
112
113 void
114 UninstallStep::GoNextState(void)
115 {
116         __state++;
117 }
118
119 InstallerError
120 UninstallStep::OnStateGetPackageInfo(void)
121 {
122         bool res = true;
123         result r = E_SUCCESS;
124         InstallerError error = INSTALLER_ERROR_NONE;
125         PackageId packageId = __pContext->__packageId;
126
127         std::unique_ptr< _PackageInfoImpl > pPackageInfoImpl(new (std::nothrow) _PackageInfoImpl());
128         TryReturn(pPackageInfoImpl, INSTALLER_ERROR_OUT_OF_MEMORY, "pPackageInfoImpl is null.");
129
130         AppLog("package = %ls", packageId.GetPointer());
131
132         r = pPackageInfoImpl->Construct(packageId);
133         TryReturn(r == E_SUCCESS, INSTALLER_ERROR_INTERNAL_STATE, "pPackageInfoImpl->Construct(%ls) failed.", packageId.GetPointer());
134
135 //      bool isUninstallable = pPackageInfoImpl->IsUninstallable();
136 //      if (isUninstallable == false)
137 //      {
138 //              __pContext->__additionalErrorString = L"Thrown when the application cannot be uninstalled because the application was preloaded.";
139 //      }
140 //      TryReturn(isUninstallable == true, INSTALLER_ERROR_PACKAGE_INVALID, "preload app cannot be uninstalled.");
141
142         String rwXmlPath;
143         rwXmlPath.Format(1024, DIR_RW_PACKAGE_SYSTEM_MANIFEST, packageId.GetPointer());
144         if (File::IsFileExist(rwXmlPath) == false)
145         {
146                 AppLog("This is a preload app = [%ls]", rwXmlPath.GetPointer());
147                 __pContext->__isPreloaded = true;
148         }
149
150         //res = IsAvailableUninstall();
151         //TryReturn(res == true, INSTALLER_ERROR_DISABLED, "IsAvailableUninstall(%ls) failed.", packageId.GetPointer());
152
153         __pContext->__rootPath = pPackageInfoImpl->GetAppRootPath();
154
155         if (pPackageInfoImpl->IsInstalledInExternalStorage() == true)
156         {
157                 int res = 0;
158                 app2ext_handle* pHandle = null;
159
160                 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
161                 TryReturn(pPackageId, INSTALLER_ERROR_INTERNAL_STATE, "pAppId is null");
162
163                 pHandle = app2ext_init(APP2EXT_SD_CARD);
164                 TryReturn(pHandle, INSTALLER_ERROR_INTERNAL_STATE, "app2ext_init() failed");
165
166                 res = pHandle->interface.pre_uninstall(pPackageId.get());
167                 TryReturn(res == 0, INSTALLER_ERROR_INTERNAL_STATE, "pHandle->interface.pre_uninstall() failed [%d]", res);
168
169                 __pContext->__pApp2ExtHandle = (void*)pHandle;
170
171                 AppLog("[app2sd] pre_uninstall(%s)", pPackageId.get());
172         }
173
174         std::unique_ptr< ArrayList > pList(pPackageInfoImpl->GetAppInfoListN());
175         TryReturn(pList, INSTALLER_ERROR_DATABASE, "pList is null.");
176
177         for (int i = 0; i < pList.get()->GetCount(); i++)
178         {
179                 _PackageAppInfoImpl* pAppInfoImpl = dynamic_cast<_PackageAppInfoImpl*>(pList.get()->GetAt(i));
180                 if (pAppInfoImpl)
181                 {
182                         std::unique_ptr< AppData > pAppData(new (std::nothrow) AppData);
183                         TryReturn(pAppData, INSTALLER_ERROR_OUT_OF_MEMORY, "pAppData is null.");
184
185                         error = pAppData.get()->Construct();
186                         TryReturn(error == INSTALLER_ERROR_NONE, INSTALLER_ERROR_INTERNAL_STATE, "pAppData.get()->Construct() failed.");
187
188                         pAppData.get()->__appId = pAppInfoImpl->GetPackageName();
189                         pAppData.get()->__feature = pAppInfoImpl->GetAppFeature();
190
191                         __pContext->__pAppDataList->Add(pAppData.release());
192                 }
193         }
194
195         GoNextState();
196         return error;
197 }
198
199 InstallerError
200 UninstallStep::OnStatePolicyCheck(void)
201 {
202         InstallerError error = INSTALLER_ERROR_NONE;
203
204         GoNextState();
205         return error;
206 }
207
208 InstallerError
209 UninstallStep::OnStateTerminateApp(void)
210 {
211         InstallerError error = INSTALLER_ERROR_NONE;
212
213         InstallerUtil::TerminateApps(__pContext->__packageId, true);
214
215         GoNextState();
216         return error;
217 }
218
219 InstallerError
220 UninstallStep::OnStateStartTimer(void)
221 {
222         InstallerError error = INSTALLER_ERROR_NONE;
223
224         GoNextState();
225         return error;
226 }
227
228 InstallerError
229 UninstallStep::OnStateRemoveDir(void)
230 {
231         InstallerError error = INSTALLER_ERROR_NONE;
232         bool res = true;
233
234         CompatibilityManager compatibilityManager;
235         compatibilityManager.Construct(__pContext);
236
237         String rootPath;
238         rootPath = __pContext->__rootPath;
239         AppLog("rootPath = [%ls]", rootPath.GetPointer());
240
241         String compatPath(rootPath);
242         compatPath.Append(L"/info/compat.info");
243         bool ospCompat = File::IsFileExist(compatPath);
244         result r = GetLastResult();
245         if (r == E_SUCCESS && ospCompat == true)
246         {
247                 if (compatibilityManager.FinalizeDataCaging(rootPath) == false)
248                 {
249                         AppLog("[Tizen::Io] Failed to unmount directories for 2.0 application, appRootPath: %ls",
250                                         rootPath.GetPointer());
251                         return INSTALLER_ERROR_UNMOUNT_FAILED;
252                 }
253         }
254         else if (r != E_SUCCESS)
255         {
256                 AppLog("[Tizen::Io] Failed to access %ls", compatPath.GetPointer());
257                 return INSTALLER_ERROR_UNMOUNT_FAILED;
258         }
259
260         compatibilityManager.CleanDirectories(rootPath, __pContext->__packageId);
261
262
263         String virtualRootInfoFile = rootPath + VIRTUAL_ROOT_INFO_FILE;
264         if (File::IsFileExist(virtualRootInfoFile) == true)
265         {
266                 res = compatibilityManager.FinalizeVirtualRoot(rootPath, __pContext->__packageId);
267                 TryReturn(res == true, INSTALLER_ERROR_UNMOUNT_FAILED, "compatibilityManager.FinalizeVirtualRoot(%ls) failed.", rootPath.GetPointer());
268         }
269
270         AppLog("Directory::Remove - START");
271
272         String realPath;
273         if (InstallerUtil::IsSymlink(rootPath) == true)
274         {
275                 if (InstallerUtil::GetRealPath(rootPath, realPath) == true)
276                 {
277                         InstallerUtil::Remove(realPath);
278                 }
279         }
280
281         InstallerUtil::Remove(rootPath);
282
283
284         IListT<AppData*>* pAppDataList = __pContext->__pAppDataList;
285         TryReturn(pAppDataList, INSTALLER_ERROR_INTERNAL_STATE, "pAppDataList is null");
286
287         int count = pAppDataList->GetCount();
288         for (int i = 0; i < count; i++)
289         {
290                 AppData* pAppData = null;
291                 pAppDataList->GetAt(i, pAppData);
292
293                 if (pAppData)
294                 {
295                         if (pAppData->__feature == CATEGORY_TYPE_IME)
296                         {
297                                 String symlinkPath;
298                                 symlinkPath.Format(1024, L"%s/%ls.so", IME_PATH, pAppData->__appId.GetPointer());
299                                 InstallerUtil::Remove(symlinkPath);
300                         }
301                 }
302         }
303
304         AppLog("Directory::Remove - END");
305
306         GoNextState();
307         return error;
308 }
309
310 InstallerError
311 UninstallStep::OnStateDone(void)
312 {
313         InstallerError error = INSTALLER_ERROR_NONE;
314
315         GoNextState();
316         return error;
317 }