46d101d6ce4d6d24053f7c86279899f82a8e0275
[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_CHECK_APP_RUNNING:
70                         AppLog("[STATE_CHECK_APP_RUNNING]");
71                         error = OnStateCheckAppRunning();
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         InstallerError error = INSTALLER_ERROR_NONE;
123         PackageId packageId = __pContext->__packageId;
124
125         std::unique_ptr< _PackageInfoImpl > pPackageInfoImpl(new (std::nothrow) _PackageInfoImpl());
126         TryReturn(pPackageInfoImpl, INSTALLER_ERROR_OUT_OF_MEMORY, "pPackageInfoImpl is null.");
127
128         AppLog("package = %ls", packageId.GetPointer());
129
130         result r = pPackageInfoImpl->Construct(packageId);
131         TryReturn(r == E_SUCCESS, INSTALLER_ERROR_INTERNAL_STATE, "pPackageInfoImpl->Construct(%ls) failed.", packageId.GetPointer());
132
133 //      bool isUninstallable = pPackageInfoImpl->IsUninstallable();
134 //      if (isUninstallable == false)
135 //      {
136 //              __pContext->__additionalErrorString = L"Thrown when the application cannot be uninstalled because the application was preloaded.";
137 //      }
138 //      TryReturn(isUninstallable == true, INSTALLER_ERROR_PACKAGE_INVALID, "preload app cannot be uninstalled.");
139
140         String rwXmlPath;
141         rwXmlPath.Format(1024, DIR_RW_PACKAGE_SYSTEM_MANIFEST, packageId.GetPointer());
142         if (File::IsFileExist(rwXmlPath) == false)
143         {
144                 AppLog("This is a preload app = [%ls]", rwXmlPath.GetPointer());
145                 __pContext->__isPreloaded = true;
146         }
147
148         __pContext->__rootPath = pPackageInfoImpl->GetAppRootPath();
149
150         if (__pContext->__isHybridService == true)
151         {
152                 AppLog("Uninstallation for HybridService");
153                 __state = STATE_TERMINATE_APP;
154                 return error;
155         }
156
157         if (pPackageInfoImpl->IsInstalledInExternalStorage() == true)
158         {
159                 int res = 0;
160                 app2ext_handle* pHandle = null;
161
162                 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
163                 TryReturn(pPackageId, INSTALLER_ERROR_INTERNAL_STATE, "pAppId is null");
164
165                 pHandle = app2ext_init(APP2EXT_SD_CARD);
166                 TryReturn(pHandle, INSTALLER_ERROR_INTERNAL_STATE, "app2ext_init() failed");
167
168                 res = pHandle->interface.pre_uninstall(pPackageId.get());
169                 TryReturn(res == 0, INSTALLER_ERROR_INTERNAL_STATE, "pHandle->interface.pre_uninstall() failed [%d]", res);
170
171                 __pContext->__pApp2ExtHandle = (void*)pHandle;
172
173                 AppLog("[app2sd] pre_uninstall(%s)", pPackageId.get());
174         }
175
176         ArrayList* pList = pPackageInfoImpl->GetAppInfoListN();
177         TryReturn(pList, INSTALLER_ERROR_DATABASE, "pList is null.");
178
179         for (int i = 0; i < pList->GetCount(); i++)
180         {
181                 _PackageAppInfoImpl* pAppInfoImpl = dynamic_cast<_PackageAppInfoImpl*>(pList->GetAt(i));
182                 if (pAppInfoImpl)
183                 {
184                         AppData* pAppData = new (std::nothrow) AppData;
185                         TryReturn(pAppData, INSTALLER_ERROR_OUT_OF_MEMORY, "pAppData is null");
186
187                         error = pAppData->Construct();
188                         TryReturn(error == INSTALLER_ERROR_NONE, INSTALLER_ERROR_INTERNAL_STATE, "pAppData->Construct() failed.");
189
190                         pAppData->__appId = pAppInfoImpl->GetPackageName();
191                         pAppData->__feature = pAppInfoImpl->GetAppFeature();
192
193                         __pContext->__pAppDataList->Add(pAppData);
194                 }
195         }
196         delete pList;
197
198         __state = STATE_TERMINATE_APP;
199
200         return error;
201 }
202
203
204 InstallerError
205 UninstallStep::OnStateCheckAppRunning(void)
206 {
207         InstallerError error = INSTALLER_ERROR_NONE;
208
209         GoNextState();
210         return error;
211 }
212
213 InstallerError
214 UninstallStep::OnStateTerminateApp(void)
215 {
216         InstallerError error = INSTALLER_ERROR_NONE;
217
218         InstallerUtil::TerminateApps(__pContext->__packageId);
219
220         GoNextState();
221         return error;
222 }
223
224 InstallerError
225 UninstallStep::OnStateStartTimer(void)
226 {
227         InstallerError error = INSTALLER_ERROR_NONE;
228
229         GoNextState();
230         return error;
231 }
232
233 InstallerError
234 UninstallStep::OnStateRemoveDir(void)
235 {
236         InstallerError error = INSTALLER_ERROR_NONE;
237         bool res = true;
238
239         String rootPath;
240         rootPath = __pContext->__rootPath;
241         AppLog("rootPath = [%ls]", rootPath.GetPointer());
242
243         String compatPath(rootPath);
244         compatPath.Append(L"/info/compat.info");
245         bool ospCompat = File::IsFileExist(compatPath);
246         result r = GetLastResult();
247         if (r == E_SUCCESS && ospCompat == true)
248         {
249                 if (CompatibilityManager::FinalizeDataCaging(rootPath) == false)
250                 {
251                         AppLog("[Tizen::Io] Failed to unmount directories for 2.0 application, appRootPath: %ls",
252                                         rootPath.GetPointer());
253                         return INSTALLER_ERROR_UNMOUNT_FAILED;
254                 }
255         }
256         else if (r != E_SUCCESS)
257         {
258                 AppLog("[Tizen::Io] Failed to access %ls", compatPath.GetPointer());
259                 return INSTALLER_ERROR_UNMOUNT_FAILED;
260         }
261
262         CompatibilityManager::CleanDirectories(rootPath, __pContext->__packageId);
263
264         String virtualRoot = rootPath + DIR_VIRTUAL_ROOT;
265         if (File::IsFileExist(virtualRoot) == true)
266         {
267                 res = CompatibilityManager::FinalizeVirtualRoot(rootPath);
268                 TryReturn(res == true, INSTALLER_ERROR_UNMOUNT_FAILED, "CompatibilityManager::FinalizeVirtualRoot(%ls) failed.", rootPath.GetPointer());
269         }
270
271         AppLog("Directory::Remove - START");
272
273         if (__pContext->__isHybridService == true)
274         {
275                 AppLog("Uninstallation for HybridService - skip Remove");
276
277         }
278         else
279         {
280                 String realPath;
281                 if (InstallerUtil::IsSymlink(rootPath) == true)
282                 {
283                         if (InstallerUtil::GetRealPath(rootPath, realPath) == true)
284                         {
285                                 InstallerUtil::Remove(realPath);
286                         }
287                 }
288
289                 InstallerUtil::Remove(rootPath);
290         }
291
292         IListT<AppData*>* pAppDataList = __pContext->__pAppDataList;
293         TryReturn(pAppDataList, INSTALLER_ERROR_INTERNAL_STATE, "pAppDataList is null");
294
295         int count = pAppDataList->GetCount();
296         for (int i = 0; i < count; i++)
297         {
298                 AppData* pAppData = null;
299                 pAppDataList->GetAt(i, pAppData);
300
301                 if (pAppData)
302                 {
303                         if (pAppData->__feature == CATEGORY_TYPE_IME)
304                         {
305                                 String symlinkPath;
306                                 symlinkPath.Format(1024, L"%s/%ls.so", IME_PATH, pAppData->__appId.GetPointer());
307                                 InstallerUtil::Remove(symlinkPath);
308                         }
309                 }
310         }
311
312         AppLog("Directory::Remove - END");
313
314         GoNextState();
315         return error;
316 }
317
318 InstallerError
319 UninstallStep::OnStateDone(void)
320 {
321         InstallerError error = INSTALLER_ERROR_NONE;
322
323         GoNextState();
324         return error;
325 }