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