fixed uninstallation if web app is installed sd card.
[framework/web/wrt-installer.git] / src / jobs / widget_uninstall / task_remove_files.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    task_remove_files.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for uninstaller task for removing widget files
21  */
22
23 #include <unistd.h>
24 #include <widget_uninstall/task_remove_files.h>
25 #include <widget_uninstall/job_widget_uninstall.h>
26 #include <widget_uninstall/uninstaller_context.h>
27 #include <widget_uninstall/widget_uninstall_errors.h>
28 #include <dpl/wrt-dao-rw/widget_dao.h>
29 #include <dpl/wrt-dao-ro/widget_config.h>
30 #include <dpl/assert.h>
31 #include <dpl/exception.h>
32 #include <dpl/utils/wrt_utility.h>
33 #include <dpl/utils/path.h>
34 #include <ail.h>
35 #include <pkgmgr/pkgmgr_parser.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <widget_install_to_external.h>
39
40 namespace Jobs {
41 namespace WidgetUninstall {
42 using namespace WrtDB;
43
44 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
45     DPL::TaskDecl<TaskRemoveFiles>(this),
46     m_context(context)
47 {
48     AddStep(&TaskRemoveFiles::StepRemoveInstallationDirectory);
49     AddStep(&TaskRemoveFiles::StepRemoveManifest);
50     AddStep(&TaskRemoveFiles::StepRemoveExternalLocations);
51     AddStep(&TaskRemoveFiles::StepRemoveFinished);
52 }
53
54 TaskRemoveFiles::~TaskRemoveFiles()
55 {}
56
57 void TaskRemoveFiles::StepRemoveInstallationDirectory()
58 {
59     LogInfo("StepRemoveInstallationDirectory started");
60     Try {
61         int ret = app2ext_get_app_location(m_context.tzPkgid.c_str());
62
63         if (APP2EXT_INTERNAL_MEM == ret) {
64             LogDebug("Removing directory");
65             m_context.removeStarted = true;
66             DPL::Utils::Path widgetDir= m_context.installedPath;
67             Try{
68                 DPL::Utils::Remove(widgetDir);
69             } Catch(DPL::Utils::Path::BaseException){
70                 LogError("Removing widget installation directory failed : " <<
71                         widgetDir.Fullpath());
72             }
73             DPL::Utils::Path dataDir(m_context.locations->getUserDataRootDir());
74             Try{
75                 DPL::Utils::Remove(dataDir);
76             } Catch(DPL::Utils::Path::BaseException){
77                 LogWarning(dataDir.Fullpath() << " is already removed");
78             }
79         } else if (APP2EXT_SD_CARD == ret) {
80             LogDebug("Removing sdcard directory");
81             Try {
82                 WidgetInstallToExtSingleton::Instance().initialize(m_context.tzPkgid);
83                 WidgetInstallToExtSingleton::Instance().uninstallation();
84                 WidgetInstallToExtSingleton::Instance().deinitialize();
85             }
86             Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
87             {
88                 Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::
89                         RemoveFilesFailed);
90             }
91         } else {
92             LogError("app is not installed");
93             ThrowMsg(Exceptions::WidgetNotExist, "failed to get app location");
94         }
95     } Catch(Exception::RemoveFilesFailed) {
96         ThrowMsg(Exceptions::RemoveFileFailure, "Cann't remove directory");
97     }
98     m_context.job->UpdateProgress(
99         UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
100         "Widget INstallation Directory Removal Finished");
101 }
102
103 void TaskRemoveFiles::StepRemoveFinished()
104 {
105     LogInfo("StepRemoveFinished finished");
106
107     m_context.job->UpdateProgress(
108         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
109         "Widget remove steps Finished");
110 }
111
112 void TaskRemoveFiles::StepRemoveManifest()
113 {
114     std::ostringstream manifest_name;
115     manifest_name << m_context.tzPkgid << ".xml";
116     DPL::Utils::Path destFile;
117     const DPL::Utils::Path PRELOAD_INSTALLED_PATH("/usr/apps");
118     const DPL::Utils::Path USR_PACKAGES_PATH("/usr/share/packages");
119     const DPL::Utils::Path OPT_PACKAGES_PATH("/opt/share/packages");
120     if (0 == (m_context.installedPath.Fullpath()).compare(0,
121             PRELOAD_INSTALLED_PATH.Fullpath().length(),
122             PRELOAD_INSTALLED_PATH.Fullpath())) {
123         LogDebug("This widget is preloaded.");
124         destFile = USR_PACKAGES_PATH;
125     } else {
126         destFile = OPT_PACKAGES_PATH;
127     }
128     destFile /= manifest_name.str();
129     DPL::Utils::Path pre_manifest = USR_PACKAGES_PATH;
130     pre_manifest /= manifest_name.str();
131
132     if (!(destFile.Exists() == 0 && pre_manifest.Exists())) {
133         int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(
134                 destFile.Fullpath().c_str(), NULL);
135         if (ret1 != 0) {
136             LogWarning("Manifest file failed to parse for uninstallation");
137         }
138     }
139     if (!DPL::Utils::TryRemove(destFile)) {
140         LogWarning("No manifest file found: " << destFile.Fullpath());
141     } else {
142         LogDebug("Manifest file removed: " << destFile.Fullpath());
143     }
144 }
145
146 void TaskRemoveFiles::StepRemoveExternalLocations()
147 {
148     if (!m_context.removeAbnormal) {
149         WidgetDAO dao(DPL::FromUTF8String(m_context.tzAppid));
150         LogDebug("Removing external locations:");
151         WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
152         FOREACH(file, externalPaths)
153         {
154             DPL::Utils::Path path(*file);
155             if(path.Exists()){
156                 if(path.IsFile()){
157                     LogDebug("  -> " << path.Fullpath());
158                     Try{
159                         DPL::Utils::Remove(path);
160                     }Catch(DPL::Utils::Path::BaseException){
161                         LogError("Failed to remove the file: " << path.Fullpath());
162                     }
163                 } else if (path.IsDir()){
164                     LogDebug("  -> " << path.Fullpath());
165                     Try{
166                         DPL::Utils::Remove(path);
167                     }Catch(DPL::Utils::Path::BaseException){
168                         Throw(Jobs::WidgetUninstall::TaskRemoveFiles::
169                                 Exception::RemoveFilesFailed);
170                     }
171                 }
172             }else{
173                 LogWarning("  -> " << path.Fullpath() << "(no such a path)");
174             }
175         }
176         dao.unregisterAllExternalLocations();
177     }
178 }
179 } //namespace WidgetUninstall
180 } //namespace Jobs