4804bcebed3780309b641e2f258d53d110fa67da
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_db_update.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_db_update.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task database updating
21  */
22 #include <time.h>
23 #include <sys/stat.h>
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>
35 #include <string>
36 //#include <widget_controller.h>
37 #include <Ecore_File.h>
38 #include <sstream>
39
40 using namespace WrtDB;
41
42 namespace Jobs {
43 namespace WidgetInstall {
44 TaskDbUpdate::TaskDbUpdate(InstallerContext& context) :
45     DPL::TaskDecl<TaskDbUpdate>(this),
46     m_context(context)
47 {
48     AddStep(&TaskDbUpdate::StepDbUpdate);
49     AddStep(&TaskDbUpdate::StepCreateDirs);
50     AddStep(&TaskDbUpdate::StepRenamePath);
51
52
53     AddAbortStep(&TaskDbUpdate::StepAbortDBUpdate);
54     AddAbortStep(&TaskDbUpdate::StepAbortRenamePath);
55 }
56
57 void TaskDbUpdate::StepCreateDirs()
58 {
59     std::ostringstream widgetPath;
60     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
61     if (pkgname.IsNull()) {
62         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
63     }
64
65     widgetPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
66     widgetPath << pkgname << "/";
67
68     std::string widgetBinPath = widgetPath.str();
69     std::string widgetSrcPath = widgetPath.str();
70
71     _WrtMakeDir(widgetPath.str().c_str(), 0755, WRT_FILEUTILS_RECUR);
72
73     // If pakcage type is widget with osp service, we don't need to make bin
74     // and src directory
75     if (m_context.widgetConfig.pType == PKG_TYPE_TIZEN_WITHSVCAPP) {
76         LogDebug("Doesn't need to create resource directory");
77     } else {
78         LogDebug("Create resource directory");
79         widgetBinPath += GlobalConfig::GetUserWidgetExecPath();
80         _WrtMakeDir(widgetBinPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
81
82         widgetSrcPath += GlobalConfig::GetWidgetSrcPath();
83         _WrtMakeDir(widgetSrcPath.c_str(), 0755, WRT_FILEUTILS_RECUR);
84     }
85
86     m_context.job->UpdateProgress(
87         InstallerContext::INSTALL_DIR_CREATE,
88         "Widget Directory Created");
89 }
90
91 void TaskDbUpdate::StepDbUpdate()
92 {
93     Try
94     {
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");
101         }
102
103         /* Set install Time */
104         time(&m_context.widgetConfig.installedTime);
105
106         LogInfo("Registering widget...");
107
108         WidgetDAO::registerWidget(
109                 *(m_context.widgetHandle),
110                 m_context.widgetConfig,
111                 m_context.wacSecurity);
112
113         FOREACH (cap, m_context.staticPermittedDevCaps) {
114             LogInfo("staticPermittedDevCaps : " << cap->first
115                     << " smack status: " << cap->second);
116         }
117
118         Assert(!!m_context.widgetConfig.pkgname
119                && "pkgName should be initialized");
120
121         WrtDB::WidgetDAO widgetDao(*m_context.widgetHandle);
122         widgetDao.setPkgName(m_context.widgetConfig.pkgname);
123
124         LogInfo("Widget registered");
125     }
126     Catch(WidgetDAO::Exception::DatabaseError)
127     {
128         LogWarning("Database failure!");
129         ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
130     }
131     Catch(DPL::DB::SqlConnection::Exception::Base)
132     {
133         LogDebug("Database failure!");
134         ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
135     }
136
137     m_context.job->UpdateProgress(
138         InstallerContext::INSTALL_DB_UPDATE,
139         "Widget DB UPDATE Finished");
140 }
141
142 void TaskDbUpdate::StepRenamePath()
143 {
144     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
145     if (pkgname.IsNull()) {
146         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
147     }
148
149     if (!_WrtUtilRemoveDir(m_context.installPath.c_str())) {
150         _WrtUtilChangeDir(GlobalConfig::GetUserInstalledWidgetPath());
151         ThrowMsg(Exceptions::RemovingFolderFailure,
152                 "Error occurs during removing existing folder");
153     }
154
155     if (m_context.widgetConfig.pType == PKG_TYPE_TIZEN_WITHSVCAPP) {
156         LogDebug("Copy file from temp directory to " <<
157                 m_context.installPath);
158
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");
163         }
164     } else {
165         LogDebug("Copy file from temp directory to " <<
166                 m_context.installPath);
167
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");
172             }
173         }
174     }
175
176     m_context.job->UpdateProgress(
177         InstallerContext::INSTALL_RENAME_PATH,
178         "Widget Rename path Finished");
179 }
180
181 void TaskDbUpdate::StepAbortDBUpdate()
182 {
183     LogWarning("[DB Update Task] Aborting... (DB Clean)");
184     Assert(!!m_context.widgetHandle);
185     Try
186     {
187         WidgetDAO::unregisterWidget(*m_context.widgetHandle);
188
189         LogDebug("Cleaning DB successful!");
190     }
191     Catch(DPL::DB::SqlConnection::Exception::Base)
192     {
193         //TODO What should happen here?
194         LogError("Failed to handle StepAbortDBClean!");
195         //        ReThrowMsg(Exceptions::DbStepFailed, "Failed to handle StepAbortDBClean!");
196     }
197 }
198
199 void TaskDbUpdate::StepAbortRenamePath()
200 {
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;
206
207     struct stat fileInfo;
208     if (stat(widgetPath.str().c_str(), &fileInfo) != 0) {
209         LogError("Failed to get widget file path : " <<widgetPath.str());
210         return;
211     }
212
213     if (rename(widgetPath.str().c_str(),
214                m_context.tempWidgetPath.c_str()) < 0) {
215         LogError("Failed to rename");
216         //Ignoring failures in Abort
217     }
218     LogDebug("Rename widget path sucessful!");
219 }
220 } //namespace WidgetInstall
221 } //namespace Jobs