2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file task_db_update.cpp
18 * @author Lukasz Wrzosek(l.wrzosek@samsung.com)
20 * @brief Implementation file for installer task database updating
24 #include <widget_install/task_db_update.h>
25 #include <widget_install/job_widget_install.h>
26 #include <widget_install/widget_install_errors.h>
27 #include <widget_install/widget_install_context.h>
28 #include <dpl/wrt-dao-ro/config_parser_data.h>
29 #include <dpl/utils/wrt_utility.h>
30 #include <dpl/wrt-dao-rw/widget_dao.h>
31 #include <dpl/foreach.h>
32 #include <dpl/log/log.h>
33 #include <dpl/assert.h>
34 #include <dpl/wrt-dao-ro/global_config.h>
36 //#include <widget_controller.h>
37 #include <Ecore_File.h>
40 using namespace WrtDB;
43 namespace WidgetInstall {
44 TaskDbUpdate::TaskDbUpdate(InstallerContext& context) :
45 DPL::TaskDecl<TaskDbUpdate>(this),
48 AddStep(&TaskDbUpdate::StepDbUpdate);
49 AddStep(&TaskDbUpdate::StepCreateDirs);
50 AddStep(&TaskDbUpdate::StepRenamePath);
53 AddAbortStep(&TaskDbUpdate::StepAbortDBUpdate);
54 AddAbortStep(&TaskDbUpdate::StepAbortRenamePath);
57 void TaskDbUpdate::StepCreateDirs()
59 std::ostringstream widgetPath;
60 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
61 if (pkgname.IsNull()) {
62 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
65 widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
66 widgetPath << pkgname << "/";
68 std::string widgetBinPath = widgetPath.str();
69 std::string widgetSrcPath = widgetPath.str();
71 _WrtMakeDir(widgetPath.str().c_str(), 0755, WRT_FILEUTILS_RECUR);
73 // If pakcage type is widget with osp service, we don't need to make bin
75 if (m_context.widgetConfig.pType == PKG_TYPE_TIZEN_WITHSVCAPP) {
76 LogDebug("Doesn't need to create resource directory");
78 LogDebug("Create resource directory");
79 widgetBinPath += GlobalConfig::GetUserWidgetExecPath();
80 _WrtMakeDir(widgetBinPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
82 widgetSrcPath += GlobalConfig::GetWidgetSrcPath();
83 _WrtMakeDir(widgetSrcPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
86 m_context.job->UpdateProgress(
87 InstallerContext::INSTALL_DIR_CREATE,
88 "Widget Directory Created");
91 void TaskDbUpdate::StepDbUpdate()
95 // If there is existing model, remove its database data
96 if (true == m_context.existingWidgetInfo.isExist) {
97 WidgetHandle old = m_context.existingWidgetInfo.existingHandle;
98 LogInfo("Unregistering widget...: " << old);
99 WidgetDAO::unregisterWidget(old);
100 LogInfo("Widget unregistered");
103 /* Set install Time */
104 time(&m_context.widgetConfig.installedTime);
106 LogInfo("Registering widget...");
108 WidgetDAO::registerWidget(
109 *(m_context.widgetHandle),
110 m_context.widgetConfig,
111 m_context.wacSecurity);
113 FOREACH (cap, m_context.staticPermittedDevCaps) {
114 LogInfo("staticPermittedDevCaps : " << cap->first
115 << " smack status: " << cap->second);
118 Assert(!!m_context.widgetConfig.pkgname
119 && "pkgName should be initialized");
121 WrtDB::WidgetDAO widgetDao(*m_context.widgetHandle);
122 widgetDao.setPkgName(m_context.widgetConfig.pkgname);
124 LogInfo("Widget registered");
126 Catch(WidgetDAO::Exception::DatabaseError)
128 LogWarning("Database failure!");
129 ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
131 Catch(DPL::DB::SqlConnection::Exception::Base)
133 LogDebug("Database failure!");
134 ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
137 m_context.job->UpdateProgress(
138 InstallerContext::INSTALL_DB_UPDATE,
139 "Widget DB UPDATE Finished");
142 void TaskDbUpdate::StepRenamePath()
144 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
145 if (pkgname.IsNull()) {
146 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
149 if (!_WrtUtilRemoveDir(m_context.installPath.c_str())) {
150 _WrtUtilChangeDir(GlobalConfig::GetUserInstalledWidgetPath());
151 ThrowMsg(Exceptions::RemovingFolderFailure,
152 "Error occurs during removing existing folder");
155 if (m_context.widgetConfig.pType == PKG_TYPE_TIZEN_WITHSVCAPP) {
156 LogDebug("Copy file from temp directory to " <<
157 m_context.installPath);
159 if (rename(m_context.tempWidgetPath.c_str(),
160 m_context.installPath.c_str()) < 0) {
161 ThrowMsg(Exceptions::UnknownError,
162 "Error occurs during renaming widget folder");
165 LogDebug("Copy file from temp directory to " <<
166 m_context.installPath);
168 if (!m_context.browserRequest) {
169 if (rename(m_context.tempWidgetRoot.c_str(), m_context.installPath.c_str()) < 0) {
170 ThrowMsg(Exceptions::UnknownError,
171 "Error occurs during renaming widget folder");
176 m_context.job->UpdateProgress(
177 InstallerContext::INSTALL_RENAME_PATH,
178 "Widget Rename path Finished");
181 void TaskDbUpdate::StepAbortDBUpdate()
183 LogWarning("[DB Update Task] Aborting... (DB Clean)");
184 Assert(!!m_context.widgetHandle);
187 WidgetDAO::unregisterWidget(*m_context.widgetHandle);
189 LogDebug("Cleaning DB successful!");
191 Catch(DPL::DB::SqlConnection::Exception::Base)
193 //TODO What should happen here?
194 LogError("Failed to handle StepAbortDBClean!");
195 // ReThrowMsg(Exceptions::DbStepFailed, "Failed to handle StepAbortDBClean!");
199 void TaskDbUpdate::StepAbortRenamePath()
201 LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
202 Assert(!!m_context.widgetHandle);
203 std::ostringstream widgetPath;
204 widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
205 widgetPath << *m_context.widgetConfig.pkgname;
207 struct stat fileInfo;
208 if (stat(widgetPath.str().c_str(), &fileInfo) != 0) {
209 LogError("Failed to get widget file path : " <<widgetPath.str());
213 if (rename(widgetPath.str().c_str(),
214 m_context.tempWidgetPath.c_str()) < 0) {
215 LogError("Failed to rename");
216 //Ignoring failures in Abort
218 LogDebug("Rename widget path sucessful!");
220 } //namespace WidgetInstall