[Release] wrt-installer_0.1.49
[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/wrt-dao-ro/vconf_config.h>
31 #include <dpl/assert.h>
32 #include <dpl/exception.h>
33 #include <dpl/utils/wrt_utility.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 #include <vconf.h>
40
41 namespace Jobs {
42 namespace WidgetUninstall {
43 using namespace WrtDB;
44
45 namespace {
46 const char * const VCONF_KEY_PREFIX = "file/private/";
47 }
48
49 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
50     DPL::TaskDecl<TaskRemoveFiles>(this),
51     m_context(context)
52 {
53     AddStep(&TaskRemoveFiles::StepRemoveInstallationDirectory);
54     AddStep(&TaskRemoveFiles::StepRemoveManifest);
55     AddStep(&TaskRemoveFiles::StepRemoveExternalLocations);
56     AddStep(&TaskRemoveFiles::StepRemoveVconf);
57     AddStep(&TaskRemoveFiles::StepRemoveFinished);
58 }
59
60 TaskRemoveFiles::~TaskRemoveFiles()
61 {}
62
63 void TaskRemoveFiles::StepRemoveInstallationDirectory()
64 {
65     LogInfo("StepRemoveInstallationDirectory started");
66     Try {
67         if (APP2EXT_SD_CARD !=
68                 app2ext_get_app_location(m_context.tzPkgid.c_str()))
69         {
70             LogDebug("Removing directory");
71             m_context.removeStarted = true;
72             if (!WrtUtilRemove(m_context.installedPath)) {
73                 LogError("Removing widget installation directory failed : " <<
74                         m_context.installedPath);
75             }
76             std::string dataDir = m_context.locations->getUserDataRootDir();
77             if (!WrtUtilRemove(dataDir)) {
78                 LogWarning(dataDir + " is already removed");
79             }
80         } else {
81             LogDebug("Removing sdcard directory");
82             Try {
83                 WidgetInstallToExtSingleton::Instance().initialize(m_context.tzPkgid);
84                 WidgetInstallToExtSingleton::Instance().uninstallation();
85                 WidgetInstallToExtSingleton::Instance().deinitialize();
86             }
87             Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
88             {
89                 Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::
90                         RemoveFilesFailed);
91             }
92         }
93     } Catch(Exception::RemoveFilesFailed) {
94         ThrowMsg(Exceptions::RemoveFileFailure, "Cann't remove directory");
95
96     }
97     m_context.job->UpdateProgress(
98         UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
99         "Widget INstallation Directory Removal Finished");
100 }
101
102 void TaskRemoveFiles::StepRemoveFinished()
103 {
104     LogInfo("StepRemoveFinished finished");
105
106     m_context.job->UpdateProgress(
107         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
108         "Widget remove steps Finished");
109 }
110
111 void TaskRemoveFiles::StepRemoveManifest()
112 {
113     std::ostringstream manifest_name;
114     manifest_name << m_context.tzPkgid << ".xml";
115     std::ostringstream destFile;
116     const std::string PRELOAD_INSTALLED_PATH = "/usr/apps";
117     if (0 == (m_context.installedPath).compare(0, PRELOAD_INSTALLED_PATH.length(),
118                 PRELOAD_INSTALLED_PATH)) {
119         LogDebug("This widget is prealoded.");
120         destFile << "/usr/share/packages" << "/"; //TODO constant with path
121     } else {
122         destFile << "/opt/share/packages" << "/"; //TODO constant with path
123     }
124     destFile << manifest_name.str();
125     int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(
126             destFile.str().c_str(), NULL);
127     int ret2 = unlink(destFile.str().c_str());
128     if (ret1 != 0) {
129         LogWarning("Manifest file failed to parse for uninstallation");
130     }
131     if (ret2 != 0) {
132         LogWarning("No manifest file found: " << destFile.str());
133     } else {
134         LogDebug("Manifest file removed: " << destFile.str());
135     }
136 }
137
138 void TaskRemoveFiles::StepRemoveExternalLocations()
139 {
140     if (!m_context.removeAbnormal) {
141         WidgetDAO dao(DPL::FromUTF8String(m_context.tzAppid));
142         LogDebug("Removing external locations:");
143         WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
144         FOREACH(path, externalPaths)
145         {
146             if (WrtUtilFileExists(*path)) {
147                 LogDebug("  -> " << *path);
148                 int ret = remove(path->c_str());
149                 if (ret != 0) {
150                     LogError(
151                             "Failed to remove the file: " << path->c_str() <<
152                             " with error: " << strerror(errno));
153                 }
154             } else if (WrtUtilDirExists(*path)) {
155                 LogDebug("  -> " << *path);
156                 if (!WrtUtilRemove(*path)) {
157                     Throw(
158                             Jobs::WidgetUninstall::TaskRemoveFiles::Exception::
159                             RemoveFilesFailed);
160                 }
161             } else {
162                 LogWarning("  -> " << *path << "(no such a path)");
163             }
164         }
165         dao.unregisterAllExternalLocations();
166     }
167 }
168
169 void TaskRemoveFiles::StepRemoveVconf()
170 {
171     if (!m_context.removeAbnormal) {
172         std::string key =
173             WrtDB::VconfConfig::GetVconfKeyRootPath(DPL::FromUTF8String(m_context.
174                         tzAppid));
175         if (vconf_unset_recursive(key.c_str())) {
176             LogError("Fail to unset vconf file");
177         } else {
178             LogDebug("vconf file is removed");
179         }
180     }
181 }
182 } //namespace WidgetUninstall
183 } //namespace Jobs