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