[Prevent] Handle return value.
[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 <widget_uninstall/task_remove_files.h>
24 #include <widget_uninstall/job_widget_uninstall.h>
25 #include <widget_uninstall/uninstaller_context.h>
26 #include <dpl/wrt-dao-rw/widget_dao.h>
27 #include <dpl/wrt-dao-ro/widget_config.h>
28 #include <dpl/assert.h>
29 #include <dpl/utils/wrt_utility.h>
30 #include <ail.h>
31 #include <pkgmgr/pkgmgr_parser.h>
32 #include <errno.h>
33 #include <string.h>
34
35 namespace Jobs {
36 namespace WidgetUninstall {
37
38 using namespace WrtDB;
39
40 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
41     DPL::TaskDecl<TaskRemoveFiles>(this),
42     m_context(context)
43 {
44     AddStep(&TaskRemoveFiles::StepRemoveInstallationDirectory);
45     //AddStep(&TaskRemoveFiles::StepRemoveDesktop);
46     AddStep(&TaskRemoveFiles::StepRemoveManifest);
47     AddStep(&TaskRemoveFiles::StepRemoveExternalLocations);
48     AddStep(&TaskRemoveFiles::StepRemoveFinished);
49 }
50
51 TaskRemoveFiles::~TaskRemoveFiles()
52 {
53 }
54
55 void TaskRemoveFiles::StepRemoveInstallationDirectory()
56 {
57     LogInfo("StepRemoveInstallationDirectory started");
58
59     m_context.removeStarted = true;
60     std::string widgetDir =
61         m_context.locations->getPackageInstallationDir();
62     if(!WrtUtilRemove(widgetDir)){
63         LogWarning("Removing widget installation directory failed");
64     }
65     m_context.job->UpdateProgress(
66         UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
67         "Widget INstallation Directory Removal Finished");
68 }
69
70 void TaskRemoveFiles::StepRemoveFinished()
71 {
72     LogInfo("StepRemoveFinished finished");
73
74     m_context.job->UpdateProgress(
75         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
76         "Widget remove steps Finished");
77 }
78
79 void TaskRemoveFiles::StepRemoveDesktop()
80 {
81     std::ostringstream desktopFile;
82
83     desktopFile << GlobalConfig::GetUserWidgetDesktopPath() << "/";
84     desktopFile << m_context.pkgname << ".desktop";
85
86     unlink(desktopFile.str().c_str());
87
88     ail_appinfo_h ai = NULL;
89     ail_error_e ret;
90
91     const char* package = m_context.pkgname.c_str();
92     LogDebug("ail delete : " << package);
93
94     ret = ail_package_get_appinfo(package, &ai);
95     if (ai) {
96         ail_package_destroy_appinfo(ai);
97     }
98
99     if (AIL_ERROR_OK == ret) {
100         if ( 0 > ail_desktop_remove(package)) {
101             LogWarning("Failed to remove ail information : " << package);
102         }
103     }
104
105     m_context.job->UpdateProgress(
106         UninstallerContext::UNINSTALL_REMOVE_DESKTOP,
107         "Widget remove desktop Finished");
108 }
109
110 void TaskRemoveFiles::StepRemoveManifest()
111 {
112     std::ostringstream manifest_name;
113     manifest_name << m_context.pkgname << ".xml";
114     std::ostringstream destFile;
115     destFile << "/opt/share/packages" << "/"; //TODO constant with path
116     destFile << manifest_name.str();
117     int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(destFile.str().c_str(), NULL);
118     int ret2 = unlink(destFile.str().c_str());
119     if(ret1 != 0)
120     {
121         LogWarning("Manifest file failed to parse for uninstallation");
122     }
123     if(ret2 != 0)
124     {
125         LogWarning("No manifest file found: " << destFile.str());
126     }
127     else
128     {
129         LogDebug("Manifest file removed: " << destFile.str());
130     }
131 }
132
133 void TaskRemoveFiles::StepRemoveExternalLocations()
134 {
135     WidgetDAO dao(m_context.locations->getPkgname());
136     LogDebug("Removing external locations:");
137     WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
138     FOREACH(path, externalPaths)
139     {
140         if(WrtUtilFileExists(*path))
141         {
142             LogDebug("  -> " << *path);
143             int ret = remove(path->c_str());
144             if (ret != 0) {
145                 LogDebug("Failed to remove the file: " << path->c_str() << " with error: " << strerror(errno));
146             }
147         }
148         else if(WrtUtilDirExists(*path))
149         {
150             LogDebug("  -> " << *path);
151             if(!WrtUtilRemove(*path)){
152                 Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::RemoveFilesFailed);
153             }
154         }
155         else
156         {
157             LogWarning("  -> " << *path << "(no such a path)");
158         }
159     }
160     dao.unregisterAllExternalLocations();
161 }
162
163 } //namespace WidgetUninstall
164 } //namespace Jobs