2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file task_remove_files.cpp
18 * @author Lukasz Wrzosek(l.wrzosek@samsung.com)
20 * @brief Implementation file for uninstaller task for removing widget files
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>
35 #include <pkgmgr/pkgmgr_parser.h>
38 #include <widget_install_to_external.h>
41 namespace WidgetUninstall {
42 using namespace WrtDB;
44 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
45 DPL::TaskDecl<TaskRemoveFiles>(this),
48 AddStep(&TaskRemoveFiles::StepRemoveInstallationDirectory);
49 AddStep(&TaskRemoveFiles::StepRemoveManifest);
50 AddStep(&TaskRemoveFiles::StepRemoveExternalLocations);
51 AddStep(&TaskRemoveFiles::StepRemoveFinished);
54 TaskRemoveFiles::~TaskRemoveFiles()
57 void TaskRemoveFiles::StepRemoveInstallationDirectory()
59 LogInfo("StepRemoveInstallationDirectory started");
61 int ret = app2ext_get_app_location(m_context.tzPkgid.c_str());
63 if (APP2EXT_INTERNAL_MEM == ret) {
64 LogDebug("Removing directory");
65 m_context.removeStarted = true;
66 DPL::Utils::Path widgetDir= m_context.installedPath;
68 DPL::Utils::Remove(widgetDir);
69 } Catch(DPL::Utils::Path::BaseException){
70 LogError("Removing widget installation directory failed : " <<
71 widgetDir.Fullpath());
73 DPL::Utils::Path dataDir(m_context.locations->getUserDataRootDir());
75 DPL::Utils::Remove(dataDir);
76 } Catch(DPL::Utils::Path::BaseException){
77 LogWarning(dataDir.Fullpath() << " is already removed");
79 } else if (APP2EXT_SD_CARD == ret) {
80 LogDebug("Removing sdcard directory");
82 WidgetInstallToExtSingleton::Instance().initialize(m_context.tzPkgid);
83 WidgetInstallToExtSingleton::Instance().uninstallation();
84 WidgetInstallToExtSingleton::Instance().deinitialize();
86 Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
88 Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::
92 LogError("app is not installed");
93 ThrowMsg(Exceptions::WidgetNotExist, "failed to get app location");
95 } Catch(Exception::RemoveFilesFailed) {
96 ThrowMsg(Exceptions::RemoveFileFailure, "Cann't remove directory");
98 m_context.job->UpdateProgress(
99 UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
100 "Widget INstallation Directory Removal Finished");
103 void TaskRemoveFiles::StepRemoveFinished()
105 LogInfo("StepRemoveFinished finished");
107 m_context.job->UpdateProgress(
108 UninstallerContext::UNINSTALL_REMOVE_FINISHED,
109 "Widget remove steps Finished");
112 void TaskRemoveFiles::StepRemoveManifest()
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;
126 destFile = OPT_PACKAGES_PATH;
128 destFile /= manifest_name.str();
129 DPL::Utils::Path pre_manifest = USR_PACKAGES_PATH;
130 pre_manifest /= manifest_name.str();
132 if (!(destFile.Exists() == 0 && pre_manifest.Exists())) {
133 int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(
134 destFile.Fullpath().c_str(), NULL);
136 LogWarning("Manifest file failed to parse for uninstallation");
139 if (!DPL::Utils::TryRemove(destFile)) {
140 LogWarning("No manifest file found: " << destFile.Fullpath());
142 LogDebug("Manifest file removed: " << destFile.Fullpath());
146 void TaskRemoveFiles::StepRemoveExternalLocations()
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)
154 DPL::Utils::Path path(*file);
157 LogDebug(" -> " << path.Fullpath());
159 DPL::Utils::Remove(path);
160 }Catch(DPL::Utils::Path::BaseException){
161 LogError("Failed to remove the file: " << path.Fullpath());
163 } else if (path.IsDir()){
164 LogDebug(" -> " << path.Fullpath());
166 DPL::Utils::Remove(path);
167 }Catch(DPL::Utils::Path::BaseException){
168 Throw(Jobs::WidgetUninstall::TaskRemoveFiles::
169 Exception::RemoveFilesFailed);
173 LogWarning(" -> " << path.Fullpath() << "(no such a path)");
176 dao.unregisterAllExternalLocations();
179 } //namespace WidgetUninstall