Update wrt-installer_0.0.69
[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 <errno.h>
29 #include <dpl/assert.h>
30 #include <dpl/utils/wrt_utility.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <dirent.h>
35 #include <ail.h>
36 #include <dpl/utils/wrt_utility.h>
37 #include <pkgmgr/pkgmgr_parser.h>
38
39 namespace Jobs {
40 namespace WidgetUninstall {
41
42 using namespace WrtDB;
43
44 void TaskRemoveFiles::ReadDir(const std::string& path,
45         std::list<std::string>& filesList)
46 {
47     LogInfo("Reading directory " << path);
48     DIR* dir = NULL;
49     struct dirent* ptr = NULL;
50     dir = opendir(path.c_str());
51     std::string delim = "";
52
53     // adding / for path to directory to build a proper path to file under directory
54     if (path[path.size() - 1] != '/') {
55         delim = "/";
56     }
57
58     if (dir) {
59         while ((ptr = readdir(dir)) != NULL) {
60             if ((!strcmp(ptr->d_name, ".")) || (!strcmp(ptr->d_name, ".."))) {
61                 LogPedantic("Omiting " << ptr->d_name);
62                 continue;
63             }
64             std::string childPath = path + delim + ptr->d_name;
65
66             struct stat st;
67             if (0 != lstat(childPath.c_str(), &st)) {
68                 switch (errno) {
69                 case EACCES:
70                     LogWarning(
71                         "EACCESS Error occured during lstat with path: " <<
72                         childPath);
73                     continue;
74                 case EBADF:
75                     LogWarning(
76                         "EBADF Error occured during lstat with path: " <<
77                         childPath);
78                     continue;
79                 case ENOENT:
80                     LogWarning(
81                         "ENOENT Error occured during lstat with path: " <<
82                         childPath);
83                     continue;
84                 case ENOTDIR:
85                     LogWarning(
86                         "ENOTDIR Error occured during lstat with path: " <<
87                         childPath);
88                     continue;
89                 default:
90                     LogWarning(
91                         "Unknown Error occured during lstat with path: " <<
92                         childPath);
93                     continue;
94                 }
95             } else {
96                 if (S_ISDIR(st.st_mode)) {
97                     LogPedantic(
98                         "Calling ReadDir in recursive way " << childPath);
99                     ReadDir(childPath, filesList);
100                 } else if (S_ISREG(st.st_mode) ||
101                            S_ISCHR(st.st_mode) ||
102                            S_ISBLK(st.st_mode) ||
103                            S_ISFIFO(st.st_mode) ||
104                            S_ISLNK(st.st_mode) ||
105                            S_ISSOCK(st.st_mode)) {
106                     LogPedantic("Adding to list  " << childPath);
107                     filesList.push_front(childPath);
108                 } else {
109                     LogWarning("Uknown file type ??");
110                 }
111             }
112         }
113         closedir(dir);
114     } else if (errno == ENOTDIR) {
115         LogDebug("Adding to list " << path);
116         filesList.push_front(path);
117     } else {
118         LogWarning("Unknown error");
119     }
120 }
121
122 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
123     DPL::TaskDecl<TaskRemoveFiles>(this),
124     m_context(context)
125 {
126     AddStep(&TaskRemoveFiles::StepPrepare);
127     AddStep(&TaskRemoveFiles::StepRemoveOneFile);
128     AddStep(&TaskRemoveFiles::StepRemoveDirectories);
129     //AddStep(&TaskRemoveFiles::StepRemoveDesktop);
130     AddStep(&TaskRemoveFiles::StepRemoveManifest);
131     AddStep(&TaskRemoveFiles::StepRemoveExternalLocations);
132     AddStep(&TaskRemoveFiles::StepRemoveFinished);
133 }
134
135 TaskRemoveFiles::~TaskRemoveFiles()
136 {
137 }
138
139 void TaskRemoveFiles::StepPrepare()
140 {
141     LogInfo("StepPrepare started");
142
143     std::ostringstream widgetDir;
144
145     widgetDir << m_context.locations->getPackageInstallationDir() << "/";
146
147     uninstRootDir = widgetDir.str();
148     ReadDir(uninstRootDir, filesList);
149
150     LogInfo("StepPrepare finished");
151
152     m_context.job->UpdateProgress(
153         UninstallerContext::UNINSTALL_REMOVE_PREPARE,
154         "Widget remove prepare Finished");
155     m_context.removeStarted = true;
156 }
157
158 void TaskRemoveFiles::StepRemoveOneFile()
159 {
160     if (filesList.size() > 0) {
161         LogDebug("Removing " << filesList.front());
162         if (0 != unlink(filesList.front().c_str())) {
163             LogWarning("Failed to remove file" << filesList.front());
164         }
165         filesList.pop_front();
166         SwitchToStep(&TaskRemoveFiles::StepRemoveOneFile);
167     } else {
168         m_context.removeFinished = true;
169     }
170
171     m_context.job->UpdateProgress(
172         UninstallerContext::UNINSTALL_REMOVE_ONEFILE,
173         "Widget remove onefile Finished");
174 }
175
176 void TaskRemoveFiles::StepRemoveDirectories()
177 {
178     using namespace WrtDB;
179     LogInfo("StepRemoveDirectories started");
180
181     if (!WrtUtilRemove(uninstRootDir)) {
182         LogWarning("Failed to remove directory" << uninstRootDir);
183     }
184     LogInfo("StepRemoveDirectories finished");
185
186     m_context.job->UpdateProgress(
187         UninstallerContext::UNINSTALL_REMOVE_DIRECTORIES,
188         "Widget remove directories Finished");
189 }
190
191 void TaskRemoveFiles::StepRemoveFinished()
192 {
193     LogInfo("StepRemoveFinished finished");
194
195     m_context.job->UpdateProgress(
196         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
197         "Widget remove steps Finished");
198 }
199
200 void TaskRemoveFiles::StepRemoveDesktop()
201 {
202     std::ostringstream desktopFile;
203
204     desktopFile << GlobalConfig::GetUserWidgetDesktopPath() << "/";
205     desktopFile << m_context.pkgname << ".desktop";
206
207     unlink(desktopFile.str().c_str());
208
209     ail_appinfo_h ai = NULL;
210     ail_error_e ret;
211
212     const char* package = m_context.pkgname.c_str();
213     LogDebug("ail delete : " << package);
214
215     ret = ail_package_get_appinfo(package, &ai);
216     if (ai) {
217         ail_package_destroy_appinfo(ai);
218     }
219
220     if (AIL_ERROR_OK == ret) {
221         if ( 0 > ail_desktop_remove(package)) {
222             LogWarning("Failed to remove ail information : " << package);
223         }
224     }
225
226     m_context.job->UpdateProgress(
227         UninstallerContext::UNINSTALL_REMOVE_DESKTOP,
228         "Widget remove desktop Finished");
229 }
230
231 void TaskRemoveFiles::StepRemoveManifest()
232 {
233     std::ostringstream manifest_name;
234     manifest_name << m_context.pkgname << ".xml";
235     std::ostringstream destFile;
236     destFile << "/opt/share/packages" << "/"; //TODO constant with path
237     destFile << manifest_name.str();
238     int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(destFile.str().c_str(), NULL);
239     int ret2 = unlink(destFile.str().c_str());
240     if(ret1 != 0)
241     {
242         LogWarning("Manifest file failed to parse for uninstallation");
243     }
244     if(ret2 != 0)
245     {
246         LogWarning("No manifest file found: " << destFile.str());
247     }
248     else
249     {
250         LogDebug("Manifest file removed: " << destFile.str());
251     }
252 }
253
254 void TaskRemoveFiles::StepRemoveExternalLocations()
255 {
256     WidgetDAO dao(m_context.widgetHandle);
257     LogDebug("Removing external locations:");
258     WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
259     FOREACH(path, externalPaths)
260     {
261         if(WrtUtilFileExists(*path))
262         {
263             LogDebug("  -> " << *path);
264             remove(path->c_str());
265         }
266         else if(WrtUtilDirExists(*path))
267         {
268             LogDebug("  -> " << *path);
269             if(!WrtUtilRemove(*path)){
270                 Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::RemoveFilesFailed);
271             }
272         }
273         else
274         {
275             LogWarning("  -> " << *path << "(no such a path)");
276         }
277     }
278     dao.unregisterAllExternalLocations();
279 }
280
281 } //namespace WidgetUninstall
282 } //namespace Jobs