Tizen 2.4.0 rev3 SDK Public 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         int ret = app2ext_get_app_location(m_context.tzPkgid.c_str());
63
64         // app2ext remove /opt/usr/apps[pkdid] directory in post_uninstall() function.
65         if (APP2EXT_SD_CARD == ret) {
66             _D("Remove external directory");
67             Try {
68                 WidgetInstallToExtSingleton::Instance().initialize(m_context.tzPkgid);
69                 WidgetInstallToExtSingleton::Instance().uninstallation();
70                 WidgetInstallToExtSingleton::Instance().deinitialize();
71             }
72             Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
73             {
74                 // Continue uninstall even fail to remove external directory.
75                 // i.e.) SD card isn't inserted or unmounted.
76                 // This behavior is recommended by platform(app2sd maintainer).
77                 _E("Fail to remove external directory");
78                 ThrowMsg(Exceptions::RemoveFileFailure, "Cann't remove external directory");
79             }
80         } else if (APP2EXT_NOT_INSTALLED != ret) {
81             _D("Removing directory");
82
83             std::string userAppsPath = WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
84             userAppsPath += "/";
85
86             m_context.removeStarted = true;
87             bf::path widgetDir(m_context.installedPath);
88
89             // installedPath
90             std::string installedFullPath = bf::canonical(m_context.installedPath).string();
91             if (installedFullPath.find(userAppsPath) == std::string::npos ||
92                 installedFullPath.size() <= userAppsPath.size()) {
93                 _E("Invalid installed path : %s", installedFullPath.c_str());
94             } else {
95                 Try{
96                     JobWidgetUninstall::SecureRemove(widgetDir);
97                 } Catch (bf::filesystem_error){
98                     _E("Removing widget installation directory failed : %s", widgetDir.c_str());
99                 }
100             }
101             bf::path dataDir(m_context.locations->getUserDataRootDir());
102
103             // data root path
104             if (bf::exists(dataDir)) {
105                 std::string dataRootPath = bf::canonical(dataDir).string();
106                 if (dataRootPath.find(userAppsPath) == std::string::npos ||
107                     dataRootPath.size() <= userAppsPath.size()) {
108                     _E("Invalid data root path : %s", dataRootPath.c_str());
109                 } else {
110                     Try{
111                         JobWidgetUninstall::SecureRemove(dataDir);
112                     } Catch(bf::filesystem_error){
113                         _W("%s is already removed", dataDir.c_str());
114                     }
115                 }
116             }
117         } else {
118             _E("app is not installed");
119             ThrowMsg(Exceptions::WidgetNotExist, "failed to get app location");
120         }
121     } Catch(Exception::RemoveFilesFailed) {
122         ThrowMsg(Exceptions::RemoveFileFailure, "Cann't remove directory");
123     }
124     m_context.job->UpdateProgress(
125         UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
126         "Widget INstallation Directory Removal Finished");
127 }
128
129 void TaskRemoveFiles::StepRemoveFinished()
130 {
131     _D("StepRemoveFinished finished");
132
133     m_context.job->UpdateProgress(
134         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
135         "Widget remove steps Finished");
136 }
137
138 void TaskRemoveFiles::StartStep()
139 {
140     LOGI("--------- <TaskRemoveFiles> : START ----------");
141 }
142
143 void TaskRemoveFiles::EndStep()
144 {
145     LOGI("--------- <TaskRemoveFiles> : END ----------");
146 }
147 } //namespace WidgetUninstall
148 } //namespace Jobs