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
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>
29 #include <dpl/assert.h>
30 #include <sys/types.h>
35 #include <dpl/utils/wrt_utility.h>
36 #include <pkgmgr/pkgmgr_parser.h>
39 namespace WidgetUninstall {
41 using namespace WrtDB;
43 void TaskRemoveFiles::ReadDir(const std::string& path,
44 std::list<std::string>& filesList)
46 LogInfo("Reading directory " << path);
48 struct dirent* ptr = NULL;
49 dir = opendir(path.c_str());
50 std::string delim = "";
52 // adding / for path to directory to build a proper path to file under directory
53 if (path[path.size() - 1] != '/') {
58 while ((ptr = readdir(dir)) != NULL) {
59 if ((!strcmp(ptr->d_name, ".")) || (!strcmp(ptr->d_name, ".."))) {
60 LogPedantic("Omiting " << ptr->d_name);
63 std::string childPath = path + delim + ptr->d_name;
66 if (0 != lstat(childPath.c_str(), &st)) {
70 "EACCESS Error occured during lstat with path: " <<
75 "EBADF Error occured during lstat with path: " <<
80 "ENOENT Error occured during lstat with path: " <<
85 "ENOTDIR Error occured during lstat with path: " <<
90 "Unknown Error occured during lstat with path: " <<
95 if (S_ISDIR(st.st_mode)) {
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);
108 LogWarning("Uknown file type ??");
113 } else if (errno == ENOTDIR) {
114 LogDebug("Adding to list " << path);
115 filesList.push_front(path);
117 LogWarning("Unknown error");
121 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
122 DPL::TaskDecl<TaskRemoveFiles>(this),
125 AddStep(&TaskRemoveFiles::StepPrepare);
126 AddStep(&TaskRemoveFiles::StepRemoveOneFile);
127 AddStep(&TaskRemoveFiles::StepRemoveDirectories);
128 //AddStep(&TaskRemoveFiles::StepRemoveDesktop);
129 AddStep(&TaskRemoveFiles::StepRemoveManifest);
130 AddStep(&TaskRemoveFiles::StepRemoveFinished);
133 TaskRemoveFiles::~TaskRemoveFiles()
137 void TaskRemoveFiles::StepPrepare()
139 LogInfo("StepPrepare started");
141 std::ostringstream widgetDir;
143 widgetDir << GlobalConfig::GetUserInstalledWidgetPath() << "/";
144 widgetDir << m_context.pkgname << "/";
146 uninstRootDir = widgetDir.str();
147 ReadDir(uninstRootDir, filesList);
149 LogInfo("StepPrepare finished");
151 m_context.job->UpdateProgress(
152 UninstallerContext::UNINSTALL_REMOVE_PREPARE,
153 "Widget remove prepare Finished");
154 m_context.removeStarted = true;
157 void TaskRemoveFiles::StepRemoveOneFile()
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());
164 filesList.pop_front();
165 SwitchToStep(&TaskRemoveFiles::StepRemoveOneFile);
167 m_context.removeFinished = true;
170 m_context.job->UpdateProgress(
171 UninstallerContext::UNINSTALL_REMOVE_ONEFILE,
172 "Widget remove onefile Finished");
175 void TaskRemoveFiles::StepRemoveDirectories()
177 using namespace WrtDB;
178 LogInfo("StepRemoveDirectories started");
180 if (!_WrtUtilRemoveDir(uninstRootDir.c_str())) {
181 LogWarning("Failed to remove directory" << uninstRootDir.c_str());
183 LogInfo("StepRemoveDirectories finished");
185 m_context.job->UpdateProgress(
186 UninstallerContext::UNINSTALL_REMOVE_DIRECTORIES,
187 "Widget remove directories Finished");
190 void TaskRemoveFiles::StepRemoveFinished()
192 LogInfo("StepRemoveFinished finished");
194 m_context.job->UpdateProgress(
195 UninstallerContext::UNINSTALL_REMOVE_FINISHED,
196 "Widget remove steps Finished");
199 void TaskRemoveFiles::StepRemoveDesktop()
201 std::ostringstream desktopFile;
203 desktopFile << GlobalConfig::GetUserWidgetDesktopPath() << "/";
204 desktopFile << m_context.pkgname << ".desktop";
206 unlink(desktopFile.str().c_str());
208 ail_appinfo_h ai = NULL;
211 const char* package = m_context.pkgname.c_str();
212 LogDebug("ail delete : " << package);
214 ret = ail_package_get_appinfo(package, &ai);
216 ail_package_destroy_appinfo(ai);
219 if (AIL_ERROR_OK == ret) {
220 if ( 0 > ail_desktop_remove(package)) {
221 LogWarning("Failed to remove ail information : " << package);
225 m_context.job->UpdateProgress(
226 UninstallerContext::UNINSTALL_REMOVE_DESKTOP,
227 "Widget remove desktop Finished");
230 void TaskRemoveFiles::StepRemoveManifest()
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());
241 LogWarning("Manifest file failed to parse for uninstallation");
245 LogWarning("No manifest file found: " << destFile.str());
249 LogDebug("Manifest file removed: " << destFile.str());
253 } //namespace WidgetUninstall