0a8f5c320880723203c07b32b006213d4e40598e
[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 <dpl/utils/wrt_utility.h>
33 #include <dpl/utils/path.h>
34 #include <ail.h>
35 #include <pkgmgr/pkgmgr_parser.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <widget_install_to_external.h>
39
40 namespace Jobs {
41 namespace WidgetUninstall {
42 using namespace WrtDB;
43
44 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
45     DPL::TaskDecl<TaskRemoveFiles>(this),
46     m_context(context)
47 {
48     AddStep(&TaskRemoveFiles::StartStep);
49     AddStep(&TaskRemoveFiles::StepRemoveInstallationDirectory);
50     AddStep(&TaskRemoveFiles::StepRemoveManifest);
51     AddStep(&TaskRemoveFiles::StepRemoveExternalLocations);
52     AddStep(&TaskRemoveFiles::StepRemoveFinished);
53     AddStep(&TaskRemoveFiles::EndStep);
54 }
55
56 TaskRemoveFiles::~TaskRemoveFiles()
57 {}
58
59 void TaskRemoveFiles::StepRemoveInstallationDirectory()
60 {
61     LogDebug("StepRemoveInstallationDirectory started");
62     Try {
63         int ret = app2ext_get_app_location(m_context.tzPkgid.c_str());
64
65         if (APP2EXT_INTERNAL_MEM == ret) {
66             LogDebug("Removing directory");
67             m_context.removeStarted = true;
68             DPL::Utils::Path widgetDir= m_context.installedPath;
69             Try{
70                 DPL::Utils::Remove(widgetDir);
71             } Catch(DPL::Utils::Path::BaseException){
72                 LogError("Removing widget installation directory failed : " <<
73                         widgetDir.Fullpath());
74             }
75             DPL::Utils::Path dataDir(m_context.locations->getUserDataRootDir());
76             Try{
77                 DPL::Utils::Remove(dataDir);
78             } Catch(DPL::Utils::Path::BaseException){
79                 LogWarning(dataDir.Fullpath() << " is already removed");
80             }
81         } else if (APP2EXT_SD_CARD == ret) {
82             LogDebug("Removing sdcard directory");
83             Try {
84                 WidgetInstallToExtSingleton::Instance().initialize(m_context.tzPkgid);
85                 WidgetInstallToExtSingleton::Instance().uninstallation();
86                 WidgetInstallToExtSingleton::Instance().deinitialize();
87             }
88             Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
89             {
90                 Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::
91                         RemoveFilesFailed);
92             }
93         } else {
94             LogError("app is not installed");
95             ThrowMsg(Exceptions::WidgetNotExist, "failed to get app location");
96         }
97     } Catch(Exception::RemoveFilesFailed) {
98         ThrowMsg(Exceptions::RemoveFileFailure, "Cann't remove directory");
99     }
100     m_context.job->UpdateProgress(
101         UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
102         "Widget INstallation Directory Removal Finished");
103 }
104
105 void TaskRemoveFiles::StepRemoveFinished()
106 {
107     LogDebug("StepRemoveFinished finished");
108
109     m_context.job->UpdateProgress(
110         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
111         "Widget remove steps Finished");
112 }
113
114 void TaskRemoveFiles::StepRemoveManifest()
115 {
116     std::ostringstream manifest_name;
117     manifest_name << m_context.tzPkgid << ".xml";
118     DPL::Utils::Path destFile;
119     const DPL::Utils::Path PRELOAD_INSTALLED_PATH("/usr/apps");
120     const DPL::Utils::Path USR_PACKAGES_PATH("/usr/share/packages");
121     const DPL::Utils::Path OPT_PACKAGES_PATH("/opt/share/packages");
122     if (0 == (m_context.installedPath.Fullpath()).compare(0,
123             PRELOAD_INSTALLED_PATH.Fullpath().length(),
124             PRELOAD_INSTALLED_PATH.Fullpath())) {
125         LogDebug("This widget is preloaded.");
126         destFile = USR_PACKAGES_PATH;
127     } else {
128         destFile = OPT_PACKAGES_PATH;
129     }
130     destFile /= manifest_name.str();
131     DPL::Utils::Path pre_manifest = USR_PACKAGES_PATH;
132     pre_manifest /= manifest_name.str();
133
134     if (!(destFile.Exists() == 0 && pre_manifest.Exists())) {
135         int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(
136                 destFile.Fullpath().c_str(), NULL);
137         if (ret1 != 0) {
138             LogWarning("Manifest file failed to parse for uninstallation");
139         }
140     }
141     if (!DPL::Utils::TryRemove(destFile)) {
142         LogWarning("No manifest file found: " << destFile.Fullpath());
143     } else {
144         LogDebug("Manifest file removed: " << destFile.Fullpath());
145     }
146 }
147
148 void TaskRemoveFiles::StepRemoveExternalLocations()
149 {
150     if (!m_context.removeAbnormal) {
151         WidgetDAO dao(DPL::FromUTF8String(m_context.tzAppid));
152         LogDebug("Removing external locations:");
153         WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
154         FOREACH(file, externalPaths)
155         {
156             DPL::Utils::Path path(*file);
157             if(path.Exists()){
158                 if(path.IsFile()){
159                     LogDebug("  -> " << path.Fullpath());
160                     Try{
161                         DPL::Utils::Remove(path);
162                     }Catch(DPL::Utils::Path::BaseException){
163                         LogError("Failed to remove the file: " << path.Fullpath());
164                     }
165                 } else if (path.IsDir()){
166                     LogDebug("  -> " << path.Fullpath());
167                     Try{
168                         DPL::Utils::Remove(path);
169                     }Catch(DPL::Utils::Path::BaseException){
170                         Throw(Jobs::WidgetUninstall::TaskRemoveFiles::
171                                 Exception::RemoveFilesFailed);
172                     }
173                 }
174             }else{
175                 LogWarning("  -> " << path.Fullpath() << "(no such a path)");
176             }
177         }
178         dao.unregisterAllExternalLocations();
179     }
180 }
181
182 void TaskRemoveFiles::StartStep()
183 {
184     LogDebug("--------- <TaskRemoveFiles> : START ----------");
185 }
186
187 void TaskRemoveFiles::EndStep()
188 {
189     LogDebug("--------- <TaskRemoveFiles> : END ----------");
190 }
191 } //namespace WidgetUninstall
192 } //namespace Jobs