Fix prevent issue
[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_PackageManagerImpl.h>
30 #include <FAppPkg_PackageInfoImpl.h>
31 #include <FAppPkg_PackageAppInfoImpl.h>
32
33 #include "InstallationContext.h"
34 #include "UninstallStep.h"
35 #include "InstallerUtil.h"
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::App;
40 using namespace Tizen::App::Package;
41 using namespace Tizen::Io;
42
43 UninstallStep::UninstallStep(void)
44 :__state(STATE_GET_PACKAGEINFO)
45 ,__pContext(null)
46 {
47 }
48
49 UninstallStep::~UninstallStep(void)
50 {
51 }
52
53 InstallerError
54 UninstallStep::Run(InstallationContext* pContext)
55 {
56         InstallerError error = INSTALLER_ERROR_NONE;
57         AppLog(" UninstallStep - START");
58
59         __pContext = pContext;
60
61         while (true)
62         {
63                 switch (__state)
64                 {
65                 case STATE_GET_PACKAGEINFO:
66                         AppLog("[STATE_GET_PACKAGEINFO]");
67                         error = OnStateGetPackageInfo();
68                         break;
69
70                 case STATE_CHECK_APP_RUNNING:
71                         AppLog("[STATE_CHECK_APP_RUNNING]");
72                         error = OnStateCheckAppRunning();
73                         break;
74
75                 case STATE_TERMINATE_APP:
76                         AppLog("[STATE_TERMINATE_APP]");
77                         error = OnStateTerminateApp();
78                         break;
79
80                 case STATE_START_TIMER:
81                         AppLog("[STATE_START_TIMER]");
82                         error = OnStateStartTimer();
83                         break;
84
85                 case STATE_DELETE_DIR:
86                         AppLog("[STATE_DELETE_DIR]");
87                         error = OnStateRemoveDir();
88                         break;
89
90                 case STATE_DONE:
91                         AppLog("[STATE_DONE]");
92                         error = OnStateDone();
93                         break;
94
95                 default:
96                         break;
97                 }
98
99                 if (error != INSTALLER_ERROR_NONE)
100                 {
101                         break;
102                 }
103
104                 if (__state > STATE_DONE)
105                 {
106                         AppLog(" UninstallStep - END");
107                         break;
108                 }
109         }
110
111         return error;
112 }
113
114 void
115 UninstallStep::GoNextState(void)
116 {
117         __state++;
118 }
119
120 InstallerError
121 UninstallStep::OnStateGetPackageInfo(void)
122 {
123         InstallerError error = INSTALLER_ERROR_NONE;
124         PackageId packageId = __pContext->__packageId;
125
126         std::unique_ptr< _PackageInfoImpl > pPackageInfoImpl(new (std::nothrow) _PackageInfoImpl());
127         TryReturn(pPackageInfoImpl, INSTALLER_ERROR_OUT_OF_MEMORY, "pPackageInfoImpl is null.");
128
129         AppLog("packageId = %ls", packageId.GetPointer());
130
131         result r = pPackageInfoImpl->Construct(packageId);
132         TryReturn(r == E_SUCCESS, INSTALLER_ERROR_INTERNAL_STATE, "pPackageInfoImpl->Construct(%ls) failed.", packageId.GetPointer());
133
134         __pContext->__rootPath = pPackageInfoImpl->GetAppRootPath();
135         if (__pContext->__rootPath.StartsWith(PATH_USR_APPS, 0) == true)
136         {
137                 __pContext->__isPreloaded = true;
138         }
139
140         if (__pContext->__isHybridService == true)
141         {
142                 AppLog("Uninstallation for HybridService");
143                 __state = STATE_DELETE_DIR;
144                 return error;
145         }
146
147         if (pPackageInfoImpl->IsInstalledInExternalStorage() == true)
148         {
149                 int res = 0;
150                 app2ext_handle* pHandle = null;
151
152                 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
153                 TryReturn(pPackageId, INSTALLER_ERROR_INTERNAL_STATE, "pAppId is null");
154
155                 pHandle = app2ext_init(APP2EXT_SD_CARD);
156                 TryReturn(pHandle, INSTALLER_ERROR_INTERNAL_STATE, "app2ext_init() failed");
157
158                 res = pHandle->interface.pre_uninstall(pPackageId.get());
159                 TryReturn(res == 0, INSTALLER_ERROR_INTERNAL_STATE, "pHandle->interface.pre_uninstall() failed [%d]", res);
160
161                 __pContext->__pApp2ExtHandle = (void*)pHandle;
162
163                 AppLog("[app2sd] pre_uninstall(%s)", pPackageId.get());
164         }
165
166         ArrayList* pList = pPackageInfoImpl->GetAppInfoListN();
167         TryReturn(pList, INSTALLER_ERROR_DATABASE, "pList is null.");
168
169         for (int i = 0; i < pList->GetCount(); i++)
170         {
171                 _PackageAppInfoImpl* pAppInfoImpl = dynamic_cast<_PackageAppInfoImpl*>(pList->GetAt(i));
172                 if (pAppInfoImpl)
173                 {
174                         AppData* pAppData = new (std::nothrow) AppData;
175                         TryReturn(pAppData, INSTALLER_ERROR_OUT_OF_MEMORY, "pAppData is null");
176
177                         error = pAppData->Construct();
178                         TryReturn(error == INSTALLER_ERROR_NONE, INSTALLER_ERROR_INTERNAL_STATE, "pAppData->Construct() failed.");
179
180                         pAppData->__appId = pAppInfoImpl->GetPackageName();
181                         pAppData->__feature = pAppInfoImpl->GetAppFeature();
182
183                         __pContext->__pAppDataList->Add(pAppData);
184                 }
185         }
186         delete pList;
187
188         __state = STATE_DELETE_DIR;
189
190         return error;
191 }
192
193
194 InstallerError
195 UninstallStep::OnStateCheckAppRunning(void)
196 {
197         InstallerError error = INSTALLER_ERROR_NONE;
198
199         GoNextState();
200         return error;
201 }
202
203 InstallerError
204 UninstallStep::OnStateTerminateApp(void)
205 {
206         InstallerError error = INSTALLER_ERROR_NONE;
207
208         GoNextState();
209         return error;
210 }
211
212 InstallerError
213 UninstallStep::OnStateStartTimer(void)
214 {
215         InstallerError error = INSTALLER_ERROR_NONE;
216
217         GoNextState();
218         return error;
219 }
220
221 InstallerError
222 UninstallStep::OnStateRemoveDir(void)
223 {
224         InstallerError error = INSTALLER_ERROR_NONE;
225
226         String rootPath;
227         rootPath = __pContext->__rootPath;
228         AppLog("rootPath = [%ls]", rootPath.GetPointer());
229
230         String compatPath(rootPath);
231         compatPath.Append(L"/info/compat.info");
232         bool ospCompat = File::IsFileExist(compatPath);
233         result r = GetLastResult();
234         if (r == E_SUCCESS && ospCompat == true)
235         {
236                 if (_FileImpl::FinalizeDataCaging(rootPath) == false)
237                 {
238                         AppLog("[Tizen::Io] Failed to unmount directories for 2.0 application, appRootPath: %ls",
239                                         rootPath.GetPointer());
240                         return INSTALLER_ERROR_UNMOUNT_FAILED;
241                 }
242         }
243         else if (r != E_SUCCESS)
244         {
245                 AppLog("[Tizen::Io] Failed to access %ls", compatPath.GetPointer());
246                 return INSTALLER_ERROR_UNMOUNT_FAILED;
247         }
248
249         AppLog("Directory::Remove - START");
250
251         if (__pContext->__isHybridService == true)
252         {
253                 AppLog("Uninstallation for HybridService - skip Remove");
254
255         }
256         else
257         {
258                 String realPath;
259                 if (InstallerUtil::IsSymlink(rootPath) == true)
260                 {
261                         if (InstallerUtil::GetRealPath(rootPath, realPath) == true)
262                         {
263                                 InstallerUtil::Remove(realPath);
264                         }
265                 }
266
267                 InstallerUtil::Remove(rootPath);
268         }
269
270         IListT<AppData*>* pAppDataList = __pContext->__pAppDataList;
271         TryReturn(pAppDataList, INSTALLER_ERROR_INTERNAL_STATE, "pAppDataList is null");
272
273         int count = pAppDataList->GetCount();
274         for (int i = 0; i < count; i++)
275         {
276                 AppData* pAppData = null;
277                 pAppDataList->GetAt(i, pAppData);
278
279                 if (pAppData)
280                 {
281                         if (pAppData->__feature == CATEGORY_TYPE_IME)
282                         {
283                                 String symlinkPath;
284                                 symlinkPath.Format(1024, L"%s/%ls.so", IME_PATH, pAppData->__appId.GetPointer());
285                                 InstallerUtil::Remove(symlinkPath);
286                         }
287                 }
288         }
289
290         AppLog("Directory::Remove - END");
291
292         GoNextState();
293         return error;
294 }
295
296 InstallerError
297 UninstallStep::OnStateDone(void)
298 {
299         InstallerError error = INSTALLER_ERROR_NONE;
300
301         GoNextState();
302         return error;
303 }