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_config.h>
28 #include <dpl/assert.h>
29 #include <sys/types.h>
33 #include <dpl/utils/wrt_utility.h>
36 namespace WidgetUninstall {
38 using namespace WrtDB;
40 void TaskRemoveFiles::ReadDir(const std::string& path,
41 std::list<std::string>& filesList)
43 LogInfo("Reading directory " << path);
45 struct dirent* ptr = NULL;
46 dir = opendir(path.c_str());
47 std::string delim = "";
49 // adding / for path to directory to build a proper path to file under directory
50 if (path[path.size() - 1] != '/') {
55 while ((ptr = readdir(dir)) != NULL) {
56 if ((!strcmp(ptr->d_name, ".")) || (!strcmp(ptr->d_name, ".."))) {
57 LogPedantic("Omiting " << ptr->d_name);
60 std::string childPath = path + delim + ptr->d_name;
63 if (0 != lstat(childPath.c_str(), &st)) {
67 "EACCESS Error occured during lstat with path: " <<
72 "EBADF Error occured during lstat with path: " <<
77 "ENOENT Error occured during lstat with path: " <<
82 "ENOTDIR Error occured during lstat with path: " <<
87 "Unknown Error occured during lstat with path: " <<
92 if (S_ISDIR(st.st_mode)) {
94 "Calling ReadDir in recursive way " << childPath);
95 ReadDir(childPath, filesList);
96 } else if (S_ISREG(st.st_mode) ||
97 S_ISCHR(st.st_mode) ||
98 S_ISBLK(st.st_mode) ||
99 S_ISFIFO(st.st_mode) ||
100 S_ISLNK(st.st_mode) ||
101 S_ISSOCK(st.st_mode)) {
102 LogPedantic("Adding to list " << childPath);
103 filesList.push_front(childPath);
105 LogWarning("Uknown file type ??");
110 } else if (errno == ENOTDIR) {
111 LogDebug("Adding to list " << path);
112 filesList.push_front(path);
114 LogWarning("Unknown error");
118 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
119 DPL::TaskDecl<TaskRemoveFiles>(this),
122 AddStep(&TaskRemoveFiles::StepPrepare);
123 AddStep(&TaskRemoveFiles::StepRemoveOneFile);
124 AddStep(&TaskRemoveFiles::StepRemoveDirectories);
125 AddStep(&TaskRemoveFiles::StepRemoveDesktop);
126 AddStep(&TaskRemoveFiles::StepRemoveFinished);
129 TaskRemoveFiles::~TaskRemoveFiles()
133 void TaskRemoveFiles::StepPrepare()
135 LogInfo("StepPrepare started");
137 std::ostringstream widgetDir;
139 DPL::OptionalString pkgname = WidgetDAO(m_context.widgetHandle).getPkgname();
140 widgetDir << GlobalConfig::GetUserInstalledWidgetPath() << "/";
141 widgetDir << pkgname << "/";
143 uninstRootDir = widgetDir.str();
144 ReadDir(uninstRootDir, filesList);
146 LogInfo("StepPrepare finished");
148 m_context.job->UpdateProgress(
149 UninstallerContext::UNINSTALL_REMOVE_PREPARE,
150 "Widget remove prepare Finished");
151 m_context.removeStarted = true;
154 void TaskRemoveFiles::StepRemoveOneFile()
156 if (filesList.size() > 0) {
157 LogDebug("Removing " << filesList.front());
158 if (0 != unlink(filesList.front().c_str())) {
159 LogWarning("Failed to remove file" << filesList.front());
161 filesList.pop_front();
162 SwitchToStep(&TaskRemoveFiles::StepRemoveOneFile);
164 m_context.removeFinished = true;
167 m_context.job->UpdateProgress(
168 UninstallerContext::UNINSTALL_REMOVE_ONEFILE,
169 "Widget remove onefile Finished");
172 void TaskRemoveFiles::StepRemoveDirectories()
174 using namespace WrtDB;
175 LogInfo("StepRemoveDirectories started");
177 if (!_WrtUtilRemoveDir(uninstRootDir.c_str())) {
178 LogWarning("Failed to remove directory" << uninstRootDir.c_str());
180 LogInfo("StepRemoveDirectories finished");
182 m_context.job->UpdateProgress(
183 UninstallerContext::UNINSTALL_REMOVE_DIRECTORIES,
184 "Widget remove directories Finished");
187 void TaskRemoveFiles::StepRemoveFinished()
189 LogInfo("StepRemoveFinished finished");
191 m_context.job->UpdateProgress(
192 UninstallerContext::UNINSTALL_REMOVE_FINISHED,
193 "Widget remove steps Finished");
196 void TaskRemoveFiles::StepRemoveDesktop()
198 std::ostringstream desktopFile;
200 DPL::OptionalString pkgname = WidgetDAO(m_context.widgetHandle).getPkgname();
201 desktopFile << GlobalConfig::GetUserWidgetDesktopPath() << "/";
202 desktopFile << pkgname << ".desktop";
204 unlink(desktopFile.str().c_str());
206 m_context.job->UpdateProgress(
207 UninstallerContext::UNINSTALL_REMOVE_DESKTOP,
208 "Widget remove desktop Finished");
210 } //namespace WidgetUninstall