tizen beta release
[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_config.h>
26
27 #include <errno.h>
28 #include <dpl/assert.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <dirent.h>
33 #include <dpl/utils/wrt_utility.h>
34
35 namespace Jobs {
36 namespace WidgetUninstall {
37
38 using namespace WrtDB;
39
40 void TaskRemoveFiles::ReadDir(const std::string& path,
41         std::list<std::string>& filesList)
42 {
43     LogInfo("Reading directory " << path);
44     DIR* dir = NULL;
45     struct dirent* ptr = NULL;
46     dir = opendir(path.c_str());
47     std::string delim = "";
48
49     // adding / for path to directory to build a proper path to file under directory
50     if (path[path.size() - 1] != '/') {
51         delim = "/";
52     }
53
54     if (dir) {
55         while ((ptr = readdir(dir)) != NULL) {
56             if ((!strcmp(ptr->d_name, ".")) || (!strcmp(ptr->d_name, ".."))) {
57                 LogPedantic("Omiting " << ptr->d_name);
58                 continue;
59             }
60             std::string childPath = path + delim + ptr->d_name;
61
62             struct stat st;
63             if (0 != lstat(childPath.c_str(), &st)) {
64                 switch (errno) {
65                 case EACCES:
66                     LogWarning(
67                         "EACCESS Error occured during lstat with path: " <<
68                         childPath);
69                     continue;
70                 case EBADF:
71                     LogWarning(
72                         "EBADF Error occured during lstat with path: " <<
73                         childPath);
74                     continue;
75                 case ENOENT:
76                     LogWarning(
77                         "ENOENT Error occured during lstat with path: " <<
78                         childPath);
79                     continue;
80                 case ENOTDIR:
81                     LogWarning(
82                         "ENOTDIR Error occured during lstat with path: " <<
83                         childPath);
84                     continue;
85                 default:
86                     LogWarning(
87                         "Unknown Error occured during lstat with path: " <<
88                         childPath);
89                     continue;
90                 }
91             } else {
92                 if (S_ISDIR(st.st_mode)) {
93                     LogPedantic(
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);
104                 } else {
105                     LogWarning("Uknown file type ??");
106                 }
107             }
108         }
109         closedir(dir);
110     } else if (errno == ENOTDIR) {
111         LogDebug("Adding to list " << path);
112         filesList.push_front(path);
113     } else {
114         LogWarning("Unknown error");
115     }
116 }
117
118 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
119     DPL::TaskDecl<TaskRemoveFiles>(this),
120     m_context(context)
121 {
122     AddStep(&TaskRemoveFiles::StepPrepare);
123     AddStep(&TaskRemoveFiles::StepRemoveOneFile);
124     AddStep(&TaskRemoveFiles::StepRemoveDirectories);
125     AddStep(&TaskRemoveFiles::StepRemoveDesktop);
126     AddStep(&TaskRemoveFiles::StepRemoveFinished);
127 }
128
129 TaskRemoveFiles::~TaskRemoveFiles()
130 {
131 }
132
133 void TaskRemoveFiles::StepPrepare()
134 {
135     LogInfo("StepPrepare started");
136
137     std::ostringstream widgetDir;
138
139     DPL::OptionalString pkgname = WidgetDAO(m_context.widgetHandle).getPkgname();
140     widgetDir << GlobalConfig::GetUserInstalledWidgetPath() << "/";
141     widgetDir << pkgname << "/";
142
143     uninstRootDir = widgetDir.str();
144     ReadDir(uninstRootDir, filesList);
145
146     LogInfo("StepPrepare finished");
147
148     m_context.job->UpdateProgress(
149         UninstallerContext::UNINSTALL_REMOVE_PREPARE,
150         "Widget remove prepare Finished");
151     m_context.removeStarted = true;
152 }
153
154 void TaskRemoveFiles::StepRemoveOneFile()
155 {
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());
160         }
161         filesList.pop_front();
162         SwitchToStep(&TaskRemoveFiles::StepRemoveOneFile);
163     } else {
164         m_context.removeFinished = true;
165     }
166
167     m_context.job->UpdateProgress(
168         UninstallerContext::UNINSTALL_REMOVE_ONEFILE,
169         "Widget remove onefile Finished");
170 }
171
172 void TaskRemoveFiles::StepRemoveDirectories()
173 {
174     using namespace WrtDB;
175     LogInfo("StepRemoveDirectories started");
176
177     if (!_WrtUtilRemoveDir(uninstRootDir.c_str())) {
178         LogWarning("Failed to remove directory" << uninstRootDir.c_str());
179     }
180     LogInfo("StepRemoveDirectories finished");
181
182     m_context.job->UpdateProgress(
183         UninstallerContext::UNINSTALL_REMOVE_DIRECTORIES,
184         "Widget remove directories Finished");
185 }
186
187 void TaskRemoveFiles::StepRemoveFinished()
188 {
189     LogInfo("StepRemoveFinished finished");
190
191     m_context.job->UpdateProgress(
192         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
193         "Widget remove steps Finished");
194 }
195
196 void TaskRemoveFiles::StepRemoveDesktop()
197 {
198     std::ostringstream desktopFile;
199
200     DPL::OptionalString pkgname = WidgetDAO(m_context.widgetHandle).getPkgname();
201     desktopFile << GlobalConfig::GetUserWidgetDesktopPath() << "/";
202     desktopFile << pkgname << ".desktop";
203
204     unlink(desktopFile.str().c_str());
205
206     m_context.job->UpdateProgress(
207         UninstallerContext::UNINSTALL_REMOVE_DESKTOP,
208         "Widget remove desktop Finished");
209 }
210 } //namespace WidgetUninstall
211 } //namespace Jobs