tizen 2.4 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 <unistd.h>
24 #include <widget_uninstall/task_remove_files.h>
25 #include <widget_uninstall/job_widget_uninstall.h>
26 #include <widget_uninstall/uninstaller_context.h>
27 #include <widget_uninstall/widget_uninstall_errors.h>
28 #include <dpl/wrt-dao-rw/widget_dao.h>
29 #include <dpl/wrt-dao-ro/widget_config.h>
30 #include <dpl/assert.h>
31 #include <dpl/exception.h>
32 #include <pkgmgr/pkgmgr_parser.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <widget_install_to_external.h>
36 #include <boost/filesystem.hpp>
37 #include <dpl/log/secure_log.h>
38
39 namespace bf = boost::filesystem;
40
41 namespace Jobs {
42 namespace WidgetUninstall {
43 using namespace WrtDB;
44
45 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
46     DPL::TaskDecl<TaskRemoveFiles>(this),
47     m_context(context)
48 {
49     AddStep(&TaskRemoveFiles::StartStep);
50     AddStep(&TaskRemoveFiles::StepRemoveInstallationDirectory);
51     AddStep(&TaskRemoveFiles::StepRemoveFinished);
52     AddStep(&TaskRemoveFiles::EndStep);
53 }
54
55 TaskRemoveFiles::~TaskRemoveFiles()
56 {}
57
58 void TaskRemoveFiles::StepRemoveInstallationDirectory()
59 {
60     _D("StepRemoveInstallationDirectory started");
61     Try {
62         // In some case, app2ext fail to uninstall(remove directory) because of mount issue.
63         bool removed = false;
64
65         int ret = app2ext_get_app_location(m_context.tzPkgid.c_str());
66
67         // app2ext remove /opt/usr/apps[pkdid] directory in post_uninstall() function.
68         if (APP2EXT_SD_CARD == ret) {
69             _D("Remove external directory");
70             Try {
71                 WidgetInstallToExtSingleton::Instance().initialize(m_context.tzPkgid);
72                 WidgetInstallToExtSingleton::Instance().uninstallation();
73                 WidgetInstallToExtSingleton::Instance().deinitialize();
74                 removed = true;
75             }
76             Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
77             {
78                 // Continue uninstall even fail to remove external directory.
79                 // i.e.) SD card isn't inserted or unmounted.
80                 // This behavior is recommended by platform(app2sd maintainer).
81                 _E("Fail to remove external directory");
82             }
83         }
84
85         // If fail to remove directory in the app2ext retry remove directroy with below code.
86         if (!removed && APP2EXT_NOT_INSTALLED != ret) {
87             _D("Removing directory");
88
89             std::string userAppsPath = WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
90             userAppsPath += "/";
91
92             m_context.removeStarted = true;
93             bf::path widgetDir(m_context.installedPath);
94
95             // installedPath
96             std::string installedFullPath = bf::canonical(m_context.installedPath).string();
97             if (installedFullPath.find(userAppsPath) == std::string::npos ||
98                 installedFullPath.size() <= userAppsPath.size()) {
99                 _E("Invalid installed path : %s", installedFullPath.c_str());
100             } else {
101                 Try{
102                     JobWidgetUninstall::SecureRemove(widgetDir);
103                 } Catch (bf::filesystem_error){
104                     _E("Removing widget installation directory failed : %s", widgetDir.c_str());
105                 }
106             }
107             bf::path dataDir(m_context.locations->getUserDataRootDir());
108
109             // data root path
110             if (bf::exists(dataDir)) {
111                 std::string dataRootPath = bf::canonical(dataDir).string();
112                 if (dataRootPath.find(userAppsPath) == std::string::npos ||
113                     dataRootPath.size() <= userAppsPath.size()) {
114                     _E("Invalid data root path : %s", dataRootPath.c_str());
115                 } else {
116                     Try{
117                         JobWidgetUninstall::SecureRemove(dataDir);
118                     } Catch(bf::filesystem_error){
119                         _W("%s is already removed", dataDir.c_str());
120                     }
121                 }
122             }
123         } else {
124             _E("app is not installed");
125             ThrowMsg(Exceptions::WidgetNotExist, "failed to get app location");
126         }
127     } Catch(Exception::RemoveFilesFailed) {
128         ThrowMsg(Exceptions::RemoveFileFailure, "Cann't remove directory");
129     }
130     m_context.job->UpdateProgress(
131         UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
132         "Widget INstallation Directory Removal Finished");
133 }
134
135 void TaskRemoveFiles::StepRemoveFinished()
136 {
137     _D("StepRemoveFinished finished");
138
139     m_context.job->UpdateProgress(
140         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
141         "Widget remove steps Finished");
142 }
143
144 void TaskRemoveFiles::StartStep()
145 {
146     LOGI("--------- <TaskRemoveFiles> : START ----------");
147 }
148
149 void TaskRemoveFiles::EndStep()
150 {
151     LOGI("--------- <TaskRemoveFiles> : END ----------");
152 }
153 } //namespace WidgetUninstall
154 } //namespace Jobs