Fixed can not install to sdcard
[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 <dpl/wrt-dao-rw/widget_dao.h>
28 #include <dpl/wrt-dao-ro/widget_config.h>
29 #include <dpl/wrt-dao-ro/vconf_config.h>
30 #include <dpl/assert.h>
31 #include <dpl/utils/wrt_utility.h>
32 #include <ail.h>
33 #include <pkgmgr/pkgmgr_parser.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <widget_install_to_external.h>
37 #include <vconf.h>
38
39 namespace Jobs {
40 namespace WidgetUninstall {
41 using namespace WrtDB;
42
43 namespace {
44 const char * const VCONF_KEY_PREFIX = "file/private/";
45 }
46
47 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
48     DPL::TaskDecl<TaskRemoveFiles>(this),
49     m_context(context)
50 {
51     AddStep(&TaskRemoveFiles::StepRemoveInstallationDirectory);
52     AddStep(&TaskRemoveFiles::StepRemoveManifest);
53     AddStep(&TaskRemoveFiles::StepRemoveExternalLocations);
54     AddStep(&TaskRemoveFiles::StepRemoveVconf);
55     AddStep(&TaskRemoveFiles::StepRemoveFinished);
56 }
57
58 TaskRemoveFiles::~TaskRemoveFiles()
59 {}
60
61 void TaskRemoveFiles::StepRemoveInstallationDirectory()
62 {
63     LogInfo("StepRemoveInstallationDirectory started");
64     if (APP2EXT_SD_CARD !=
65             app2ext_get_app_location(m_context.tzPkgid.c_str()))
66     {
67         LogDebug("Removing directory");
68         m_context.removeStarted = true;
69         std::string widgetDir =
70             m_context.locations->getPackageInstallationDir();
71         if (!WrtUtilRemove(widgetDir)) {
72             LogWarning("Removing widget installation directory failed");
73         }
74         std::string dataDir = m_context.locations->getUserDataRootDir();
75         if (!WrtUtilRemove(dataDir)) {
76             LogWarning(dataDir + " is already removed");
77         }
78     } else {
79         LogDebug("Removing sdcard directory");
80         Try {
81             WidgetInstallToExtSingleton::Instance().initialize(m_context.tzPkgid);
82             WidgetInstallToExtSingleton::Instance().uninstallation();
83             WidgetInstallToExtSingleton::Instance().deinitialize();
84         }
85         Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
86         {
87             Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::
88                     RemoveFilesFailed);
89         }
90     }
91     m_context.job->UpdateProgress(
92         UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
93         "Widget INstallation Directory Removal Finished");
94 }
95
96 void TaskRemoveFiles::StepRemoveFinished()
97 {
98     LogInfo("StepRemoveFinished finished");
99
100     m_context.job->UpdateProgress(
101         UninstallerContext::UNINSTALL_REMOVE_FINISHED,
102         "Widget remove steps Finished");
103 }
104
105 void TaskRemoveFiles::StepRemoveManifest()
106 {
107     std::ostringstream manifest_name;
108     manifest_name << m_context.tzPkgid << ".xml";
109     std::ostringstream destFile;
110     destFile << "/opt/share/packages" << "/"; //TODO constant with path
111     destFile << manifest_name.str();
112     int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(
113             destFile.str().c_str(), NULL);
114     int ret2 = unlink(destFile.str().c_str());
115     if (ret1 != 0) {
116         LogWarning("Manifest file failed to parse for uninstallation");
117     }
118     if (ret2 != 0) {
119         LogWarning("No manifest file found: " << destFile.str());
120     } else {
121         LogDebug("Manifest file removed: " << destFile.str());
122     }
123 }
124
125 void TaskRemoveFiles::StepRemoveExternalLocations()
126 {
127     if (!m_context.removeAbnormal) {
128         WidgetDAO dao(DPL::FromUTF8String(m_context.tzAppid));
129         LogDebug("Removing external locations:");
130         WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
131         FOREACH(path, externalPaths)
132         {
133             if (WrtUtilFileExists(*path)) {
134                 LogDebug("  -> " << *path);
135                 int ret = remove(path->c_str());
136                 if (ret != 0) {
137                     LogError(
138                             "Failed to remove the file: " << path->c_str() <<
139                             " with error: " << strerror(errno));
140                 }
141             } else if (WrtUtilDirExists(*path)) {
142                 LogDebug("  -> " << *path);
143                 if (!WrtUtilRemove(*path)) {
144                     Throw(
145                             Jobs::WidgetUninstall::TaskRemoveFiles::Exception::
146                             RemoveFilesFailed);
147                 }
148             } else {
149                 LogWarning("  -> " << *path << "(no such a path)");
150             }
151         }
152         dao.unregisterAllExternalLocations();
153     }
154 }
155
156 void TaskRemoveFiles::StepRemoveVconf()
157 {
158     if (!m_context.removeAbnormal) {
159         std::string key =
160             WrtDB::VconfConfig::GetVconfKeyRootPath(DPL::FromUTF8String(m_context.
161                         tzAppid));
162         if (vconf_unset_recursive(key.c_str())) {
163             LogError("Fail to unset vconf file");
164         } else {
165             LogDebug("vconf file is removed");
166         }
167     }
168 }
169 } //namespace WidgetUninstall
170 } //namespace Jobs