Task database fix - TC11
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_database.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_new_db_insert.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @author  Soyoung kim(sy037.kim@samsung.com)
20  * @version 1.0
21  * @brief   Implementation file for installer task database updating for widget update
22  */
23 #include <time.h>
24 #include <sys/stat.h>
25 #include <widget_install/task_database.h>
26 #include <widget_install/job_widget_install.h>
27 #include <widget_install/widget_install_errors.h>
28 #include <widget_install/widget_install_context.h>
29 #include <dpl/wrt-dao-rw/widget_dao.h>
30 #include <dpl/wrt-dao-ro/vconf_config.h>
31 #include <dpl/foreach.h>
32 #include <dpl/utils/wrt_utility.h>
33 #include <dpl/log/log.h>
34 #include <dpl/assert.h>
35 #include <string>
36 #include <sstream>
37 #include <ace_api_install.h>
38 #include <ace_registration.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <vconf.h>
42 #include <map>
43
44 using namespace WrtDB;
45
46 namespace Jobs {
47 namespace WidgetInstall {
48
49 TaskDatabase::TaskDatabase(InstallerContext& context) :
50     DPL::TaskDecl<TaskDatabase>(this),
51     m_context(context),
52     m_handleToRemove(INVALID_WIDGET_HANDLE),
53     m_handle(INVALID_WIDGET_HANDLE)
54 {
55     AddStep(&TaskDatabase::StepRegisterExternalFiles);
56     AddStep(&TaskDatabase::StepWrtDBInsert);
57     AddStep(&TaskDatabase::StepAceDBInsert);
58     AddStep(&TaskDatabase::StepRemoveExternalFiles);
59     AddStep(&TaskDatabase::StepCreateVconf);
60
61     AddAbortStep(&TaskDatabase::StepAbortDBInsert);
62 }
63
64 void TaskDatabase::StepWrtDBInsert()
65 {
66     Try
67     {
68         /* Set install Time */
69         time(&m_context.widgetConfig.installedTime);
70
71         if (m_context.existingWidgetInfo.isExist) //update
72         {
73             LogInfo("Registering widget... (update)");
74             Try
75             {
76                 m_handleToRemove = WidgetDAOReadOnly::getHandle(
77                     m_context.locations->getPkgname());
78             }
79             Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
80             {
81                 LogError("Given tizenId not found for update installation (Same GUID?)");
82                 ThrowMsg(Exceptions::InvalidPackage, "Given tizenId not found for update installation");
83             }
84             WidgetDAO::registerOrUpdateWidget(
85                     m_context.locations->getPkgname(),
86                     m_context.widgetConfig,
87                     m_context.wacSecurity);
88             m_handle = WidgetDAOReadOnly::getHandle(
89                 m_context.locations->getPkgname());
90         }
91         else //new installation
92         {
93             LogInfo("Registering widget...");
94             WidgetDAO::registerWidget(
95                     m_context.locations->getPkgname(),
96                     m_context.widgetConfig,
97                     m_context.wacSecurity);
98             m_handle = WidgetDAOReadOnly::getHandle(
99                 m_context.locations->getPkgname());
100         }
101
102         FOREACH (cap, m_context.staticPermittedDevCaps) {
103             LogInfo("staticPermittedDevCaps : " << cap->first
104                     << " smack status: " << cap->second);
105         }
106
107         LogInfo("Widget registered");
108     }
109     Catch(WidgetDAO::Exception::DatabaseError)
110     {
111         LogError("Database failure!");
112         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
113     }
114     Catch(DPL::DB::SqlConnection::Exception::Base)
115     {
116         LogError("Database failure!");
117         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
118     }
119 }
120
121 void TaskDatabase::StepAceDBInsert()
122 {
123     LogDebug("Inserting Ace database entry. New handle: " << m_handle);
124     if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
125         LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
126         if (ACE_OK != ace_unregister_widget(
127                 static_cast<ace_widget_handle_t>(m_handleToRemove)))
128         {
129             LogWarning("Error while removing ace entry for previous insallation");
130         };
131     }
132
133     if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
134                                   m_context.wacSecurity.getCertificateList()))
135     {
136         LogError("ace database insert failed");
137         ThrowMsg(Exceptions::UpdateFailed, "Update failure. ace_register_widget failed");
138     }
139     LogDebug("Ace data inserted");
140
141     m_context.job->UpdateProgress(
142         InstallerContext::INSTALL_NEW_DB_INSERT,
143         "New Widget DB UPDATE Finished");
144 }
145
146 void TaskDatabase::StepRegisterExternalFiles()
147 {
148     WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
149     if (m_context.existingWidgetInfo.isExist) //update
150     {
151         Try
152         {
153             WidgetDAO dao(m_context.locations->getPkgname());
154             WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
155             FOREACH(file, externalLocationsDB)
156             {
157                 if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
158                 {
159                     m_externalLocationsToRemove.push_back(*file);
160                 }
161             }
162         }
163         Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
164         {
165             LogError("Given tizenId not found for update installation (Same GUID?)");
166             ThrowMsg(Exceptions::UpdateFailed, "Given tizenId not found for update installation");
167         }
168     }
169     LogDebug("Registering external files:");
170     FOREACH(file, externalLocationsUpdate)
171     {
172         LogDebug("  -> " << *file);
173     }
174
175     //set external locations to be registered
176     m_context.widgetConfig.externalLocations = externalLocationsUpdate;
177 }
178
179 void TaskDatabase::StepRemoveExternalFiles()
180 {
181     if(!m_externalLocationsToRemove.empty())
182     {
183         LogDebug("Removing external files:");
184     }
185
186     FOREACH(file, m_externalLocationsToRemove)
187     {
188         if(WrtUtilFileExists(*file))
189         {
190             LogDebug("  -> " << *file);
191             remove(file->c_str());
192         }
193         else if(WrtUtilDirExists(*file))
194         {
195             LogDebug("  -> " << *file);
196             if(!WrtUtilRemove(*file)){
197                 ThrowMsg(Exceptions::RemovingFolderFailure,
198                         "Failed to remove external directory");
199             }
200         }
201         else
202         {
203             LogWarning("  -> " << *file << "(no such a path)");
204         }
205     }
206 }
207
208 void TaskDatabase::StepCreateVconf()
209 {
210     LogDebug("StepCreateVconf");
211     std::map<std::string, WrtDB::SettingsType> vconfData;
212     vconfData[
213         WrtDB::VconfConfig::GetVconfKeyPopupUsage(
214             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
215     vconfData[
216         WrtDB::VconfConfig::GetVconfKeyGeolocationUsage(
217             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
218     vconfData[
219         WrtDB::VconfConfig::GetVconfKeyWebNotificationUsage(
220             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
221     vconfData[
222         WrtDB::VconfConfig::GetVconfKeyWebDatabaseUsage(
223             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
224     vconfData[
225         WrtDB::VconfConfig::GetVconfKeyFilesystemUsage(
226             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_ON;
227     vconfData[
228         WrtDB::VconfConfig::GetVconfKeyMemorySavingMode(
229             m_context.locations->getPkgname())] = WrtDB::SETTINGS_TYPE_OFF;
230
231     // vconftool -g 5000 set -t int <path> initialize value
232     // Current installer should use vconftool for setting group ID
233     // In case of install application by pkgcmd, permission for others
234     // set to read-only
235     FOREACH(it, vconfData) {
236         std::ostringstream command;
237         command << "vconftool -g 5000 set -t int ";
238         command << (*it).first;
239         command << " \"" << static_cast<int>((*it).second) << "\"";
240         int ret = system(command.str().c_str());
241         if (-1 == ret) {
242             ThrowMsg(Exceptions::CreateVconfFailure, "Failed to create vconf files");
243         }
244     }
245 }
246
247 void TaskDatabase::StepAbortDBInsert()
248 {
249     LogWarning("[DB Update Task] Aborting... (DB Clean)");
250     Try
251     {
252         WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
253         LogDebug("Cleaning DB successful!");
254     }
255     Catch(DPL::DB::SqlConnection::Exception::Base)
256     {
257         LogError("Failed to handle StepAbortDBClean!");
258     }
259
260     ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
261     // Remove also old one. If it was already updated nothing wrong will happen,
262     // but if not old widget will be removed.
263     if (INVALID_WIDGET_HANDLE != m_handleToRemove)
264         ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
265 }
266
267 } //namespace WidgetInstall
268 } //namespace Jobs